mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(logs): enforce stream resource permissions (#5694)
* fix(logs): enforce stream resource permissions * fix(logs): retain request partitions for stream permissions
This commit is contained in:
1 parent
e357353472
commit
dbe7fdb92e
15 files changed
+1142
-5
No files matched your search
+49
-1
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.core.common;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsService;
|
||||
import io.github.hectorvent.floci.services.dynamodb.DynamoDbFacade;
|
||||
import io.github.hectorvent.floci.services.dynamodb.DynamoDbService;
|
||||
import io.github.hectorvent.floci.services.dynamodb.backend.NativeDynamoDbBackend;
|
||||
@@ -36,9 +37,12 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -50,6 +54,7 @@ class IamConditionContextResolverTest {
|
||||
private Instance<DynamoDbFacade> dynamoDbFacadeInstance;
|
||||
private Ec2Service ec2Service;
|
||||
private S3Service s3Service;
|
||||
private CloudWatchLogsService logsService;
|
||||
private RequestContext requestContext;
|
||||
private EmulatorConfig config;
|
||||
private IamConditionContextResolver resolver;
|
||||
@@ -71,12 +76,55 @@ class IamConditionContextResolverTest {
|
||||
Instance<S3Service> s3ServiceInstance = mock(Instance.class);
|
||||
when(s3ServiceInstance.isResolvable()).thenReturn(true);
|
||||
when(s3ServiceInstance.get()).thenReturn(s3Service);
|
||||
logsService = mock(CloudWatchLogsService.class);
|
||||
Instance<CloudWatchLogsService> logsServiceInstance = mock(Instance.class);
|
||||
when(logsServiceInstance.isResolvable()).thenReturn(true);
|
||||
when(logsServiceInstance.get()).thenReturn(logsService);
|
||||
requestContext = new RequestContext();
|
||||
requestContext.setRegion("us-east-1");
|
||||
config = mock(EmulatorConfig.class);
|
||||
when(config.defaultRegion()).thenReturn("us-east-1");
|
||||
resolver = new IamConditionContextResolver(
|
||||
dynamoDbFacadeInstance, ec2ServiceInstance, s3ServiceInstance, requestContext, config);
|
||||
dynamoDbFacadeInstance, ec2ServiceInstance, s3ServiceInstance, logsServiceInstance, requestContext, config);
|
||||
}
|
||||
|
||||
@Test
|
||||
void logStreamTagsUseRawParentNameAndRequestRegion() throws Exception {
|
||||
ContainerRequestContext request = mock(ContainerRequestContext.class);
|
||||
when(request.getProperty("floci.bufferedJsonBody"))
|
||||
.thenReturn(mapper.readTree("{\"logGroupName\":\" group \",\"logStreamName\":42}"));
|
||||
when(logsService.listTagsLogGroup(" group ", "eu-west-1")).thenReturn(Map.of("team", "blue"));
|
||||
requestContext.setRegion("eu-west-1");
|
||||
for (String action : List.of("logs:CreateLogStream", "logs:DeleteLogStream")) {
|
||||
assertEquals(Map.of("aws:ResourceTag/team", List.of("blue")), resolver.resolve("logs", action, request));
|
||||
}
|
||||
requestContext.setRegion(null);
|
||||
when(logsService.listTagsLogGroup(" group ", "us-east-1")).thenReturn(Map.of("team", "red"));
|
||||
assertEquals(Map.of("aws:ResourceTag/team", List.of("red")),
|
||||
resolver.resolve("logs", "logs:CreateLogStream", request));
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingLogGroupTagsDoNotReplaceTheHandlersMissingResourceError() throws Exception {
|
||||
ContainerRequestContext request = mock(ContainerRequestContext.class);
|
||||
when(request.getProperty("floci.bufferedJsonBody")).thenReturn(mapper.readTree("{\"logGroupName\":42}"));
|
||||
when(logsService.listTagsLogGroup("42", "us-east-1"))
|
||||
.thenThrow(new AwsException("ResourceNotFoundException", "missing", 400));
|
||||
assertNull(resolver.resolve("logs", "logs:DeleteLogStream", request));
|
||||
}
|
||||
|
||||
@Test
|
||||
void unexpectedLogGroupLookupFailuresCannotEraseTagDenies() throws Exception {
|
||||
ContainerRequestContext request = mock(ContainerRequestContext.class);
|
||||
when(request.getProperty("floci.bufferedJsonBody")).thenReturn(mapper.readTree("{\"logGroupName\":\"group\"}"));
|
||||
AwsException failure = new AwsException("InternalFailure", "storage unavailable", 500);
|
||||
when(logsService.listTagsLogGroup("group", "us-east-1")).thenThrow(failure);
|
||||
assertSame(failure, assertThrows(AwsException.class,
|
||||
() -> resolver.resolve("logs", "logs:CreateLogStream", request)));
|
||||
IllegalStateException unexpected = new IllegalStateException("unavailable");
|
||||
doThrow(unexpected).when(logsService).listTagsLogGroup("group", "us-east-1");
|
||||
assertSame(unexpected, assertThrows(IllegalStateException.class,
|
||||
() -> resolver.resolve("logs", "logs:DeleteLogStream", request)));
|
||||
}
|
||||
|
||||
/** A form request whose body can be read again, as a real request's restored stream can. */
|
||||
|
||||
+1
-1
@@ -152,7 +152,7 @@ class DynamoDbFacadeTest {
|
||||
RequestContext requestContext = new RequestContext();
|
||||
requestContext.setRegion(OTHER_REGION);
|
||||
IamConditionContextResolver resolver = new IamConditionContextResolver(dynamoDb,
|
||||
mock(Instance.class), mock(Instance.class), requestContext, mock(EmulatorConfig.class));
|
||||
mock(Instance.class), mock(Instance.class), mock(Instance.class), requestContext, mock(EmulatorConfig.class));
|
||||
ContainerRequestContext request = mock(ContainerRequestContext.class);
|
||||
JsonNode body = mapper.readTree("""
|
||||
{"TableName": "recorded-table", "Key": {"id": {"S": "USER_alice"}}}
|
||||
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
|
||||
@QuarkusTest
|
||||
@TestProfile(LogsNoncommercialUnknownRegionAuthorizationIntegrationTest.GovProfile.class)
|
||||
class LogsGovUnknownRegionAuthorizationIntegrationTest extends LogsNoncommercialUnknownRegionAuthorizationIntegrationTest {
|
||||
@Override
|
||||
String deploymentPartition() {
|
||||
return "aws-us-gov";
|
||||
}
|
||||
}
|
||||
+134
@@ -0,0 +1,134 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import io.github.hectorvent.floci.testing.EnforcementFixtures;
|
||||
import io.github.hectorvent.floci.testing.PartitionCleanup;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.common.http.TestHTTPResource;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.QuarkusTestProfile;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import io.restassured.response.Response;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.jupiter.api.Assertions.assertAll;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@QuarkusTest
|
||||
@TestProfile(LogsNoncommercialUnknownRegionAuthorizationIntegrationTest.ChinaProfile.class)
|
||||
class LogsNoncommercialUnknownRegionAuthorizationIntegrationTest {
|
||||
public static class ChinaProfile implements QuarkusTestProfile {
|
||||
@Override
|
||||
public Map<String, String> getConfigOverrides() {
|
||||
return Map.of("floci.services.iam.enforcement-enabled", "true",
|
||||
"floci.partitions.allow-unknown-regions", "true", "floci.default-region", "cn-north-1");
|
||||
}
|
||||
}
|
||||
|
||||
public static class GovProfile implements QuarkusTestProfile {
|
||||
@Override
|
||||
public Map<String, String> getConfigOverrides() {
|
||||
return Map.of("floci.services.iam.enforcement-enabled", "true",
|
||||
"floci.partitions.allow-unknown-regions", "true", "floci.default-region", "us-gov-west-1");
|
||||
}
|
||||
}
|
||||
|
||||
private static final String ACCOUNT = "111111111111";
|
||||
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
@TestHTTPResource
|
||||
URI endpoint;
|
||||
|
||||
@BeforeAll
|
||||
static void configureContentTypes() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"CreateLogStream,false,polygondwanaland-west-1", "DeleteLogStream,false,polygondwanaland-west-1",
|
||||
"CreateLogStream,true,polygondwanaland-west-1", "DeleteLogStream,true,polygondwanaland-west-1",
|
||||
"CreateLogStream,false,cn-north-1", "DeleteLogStream,false,cn-north-1",
|
||||
"CreateLogStream,true,cn-north-1", "DeleteLogStream,true,cn-north-1",
|
||||
"CreateLogStream,false,us-gov-west-1", "DeleteLogStream,false,us-gov-west-1",
|
||||
"CreateLogStream,true,us-gov-west-1", "DeleteLogStream,true,us-gov-west-1",
|
||||
"CreateLogStream,false,us-east-1", "DeleteLogStream,false,us-east-1",
|
||||
"CreateLogStream,true,us-east-1", "DeleteLogStream,true,us-east-1"})
|
||||
void streamPoliciesUseTheSamePartitionAsTheStoredGroup(String action, boolean deny, String region) {
|
||||
int port = endpoint.getPort();
|
||||
String group = "logs-partition-" + UUID.randomUUID();
|
||||
cleanup.register(() -> call(ACCOUNT, region, "DeleteLogGroup", Map.of("logGroupName", group))
|
||||
.then().statusCode(200));
|
||||
call(ACCOUNT, region, "CreateLogGroup", Map.of("logGroupName", group)).then().statusCode(200);
|
||||
String partition = switch (region) {
|
||||
case "cn-north-1" -> "aws-cn";
|
||||
case "us-gov-west-1" -> "aws-us-gov";
|
||||
case "us-east-1" -> "aws";
|
||||
default -> deploymentPartition();
|
||||
};
|
||||
String groupArn = "arn:" + partition + ":logs:" + region + ":" + ACCOUNT + ":log-group:" + group;
|
||||
call(ACCOUNT, region, "DescribeLogGroups", Map.of("logGroupNamePrefix", group))
|
||||
.then().statusCode(200).body("logGroups[0].arn", equalTo(groupArn + ":*"));
|
||||
boolean delete = action.equals("DeleteLogStream");
|
||||
if (delete) {
|
||||
call(ACCOUNT, region, "CreateLogStream", Map.of("logGroupName", group, "logStreamName", "s"))
|
||||
.then().statusCode(200);
|
||||
call(ACCOUNT, region, "PutLogEvents", Map.of("logGroupName", group, "logStreamName", "s",
|
||||
"logEvents", List.of(Map.of("timestamp", System.currentTimeMillis(), "message", "retained"))))
|
||||
.then().statusCode(200);
|
||||
}
|
||||
String user = "logs-partition-" + UUID.randomUUID();
|
||||
String root = EnforcementFixtures.auth(ACCOUNT, "iam");
|
||||
String caller = EnforcementFixtures.createUser(cleanup, port, root, user).accessKeyId();
|
||||
String allow = deny ? "{\"Effect\":\"Allow\",\"Action\":\"logs:" + action
|
||||
+ "\",\"Resource\":\"*\"}," : "";
|
||||
String effect = deny ? "Deny" : "Allow";
|
||||
String statement = "{\"Effect\":\"" + effect + "\",\"Action\":\"logs:" + action
|
||||
+ "\",\"Resource\":\"" + groupArn + ":log-stream:s\"}";
|
||||
given().port(port).header("Authorization", root).formParam("Action", "PutUserPolicy")
|
||||
.formParam("UserName", user).formParam("PolicyName", "streams")
|
||||
.formParam("PolicyDocument", "{\"Version\":\"2012-10-17\",\"Statement\":[" + allow + statement + "]}")
|
||||
.when().post("/").then().statusCode(200);
|
||||
Response response = call(caller, region, action, Map.of("logGroupName", group, "logStreamName", "s"));
|
||||
assertAll("canonical partition governs the same stream and retained events",
|
||||
() -> response.then().statusCode(deny ? 400 : 200),
|
||||
() -> {
|
||||
if (deny) {
|
||||
response.then().body(containsString("AccessDeniedException"));
|
||||
}
|
||||
},
|
||||
() -> {
|
||||
List<Map<String, Object>> streams = call(ACCOUNT, region, "DescribeLogStreams",
|
||||
Map.of("logGroupName", group)).then().statusCode(200).extract().path("logStreams");
|
||||
assertEquals(deny == delete, streams.stream().anyMatch(s -> "s".equals(s.get("logStreamName"))));
|
||||
},
|
||||
() -> {
|
||||
if (deny && delete) {
|
||||
call(ACCOUNT, region, "GetLogEvents", Map.of("logGroupName", group, "logStreamName", "s"))
|
||||
.then().statusCode(200).body("events[0].message", equalTo("retained"));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
String deploymentPartition() {
|
||||
return "aws-cn";
|
||||
}
|
||||
|
||||
private Response call(String caller, String region, String action, Map<String, ?> body) {
|
||||
String auth = "AWS4-HMAC-SHA256 Credential=" + caller + "/20261015/" + region
|
||||
+ "/logs/aws4_request, SignedHeaders=host, Signature=abc";
|
||||
return given().port(endpoint.getPort()).contentType("application/x-amz-json-1.1")
|
||||
.header("Authorization", auth).header("X-Amz-Target", "Logs_20140328." + action)
|
||||
.body(body).when().post("/");
|
||||
}
|
||||
}
|
||||
+453
@@ -0,0 +1,453 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsService;
|
||||
import io.github.hectorvent.floci.testing.EnforcementFixtures;
|
||||
import io.github.hectorvent.floci.testing.IamEnforcementProfile;
|
||||
import io.github.hectorvent.floci.testing.PartitionCleanup;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.common.http.TestHTTPResource;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import io.restassured.response.Response;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.junit.jupiter.api.Assertions.assertAll;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@QuarkusTest
|
||||
@TestProfile(IamEnforcementProfile.class)
|
||||
class LogsStreamAuthorizationIntegrationTest {
|
||||
|
||||
private static final String ACCOUNT = "111111111111";
|
||||
private static final String REGION = "us-east-1";
|
||||
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
|
||||
@TestHTTPResource
|
||||
URI endpoint;
|
||||
|
||||
@Inject
|
||||
CloudWatchLogsService logsService;
|
||||
|
||||
@BeforeAll
|
||||
static void configureContentTypes() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"CreateLogStream", "DeleteLogStream"})
|
||||
void exactStreamPermissionAllowsOnlyThatStream(String action) {
|
||||
String group = group(Map.of());
|
||||
prepare(action, group, "selected");
|
||||
prepare(action, group, "other");
|
||||
String caller = user(policy("Allow", action, streamArn(group, "selected"), ""));
|
||||
|
||||
stream(caller, action, group, "other").statusCode(400)
|
||||
.body(containsString("AccessDeniedException"));
|
||||
assertPresent(group, "other", action.equals("DeleteLogStream"));
|
||||
stream(caller, action, group, "selected").statusCode(200);
|
||||
assertPresent(group, "selected", action.equals("CreateLogStream"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"CreateLogStream", "DeleteLogStream"})
|
||||
void groupTagDenyPreventsMutationEvenWithWildcardAllow(String action) {
|
||||
String group = group(Map.of("protected", "true"));
|
||||
prepare(action, group, "selected");
|
||||
String caller = user(policy("Allow", action, "*", "") + ","
|
||||
+ policy("Deny", action, "*",
|
||||
",\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/protected\":\"true\"}}"));
|
||||
|
||||
stream(caller, action, group, "selected").statusCode(400)
|
||||
.body(containsString("AccessDeniedException"));
|
||||
assertPresent(group, "selected", action.equals("DeleteLogStream"));
|
||||
if (action.equals("DeleteLogStream")) {
|
||||
assertEvent(group, "selected");
|
||||
}
|
||||
for (Map<String, String> tags : List.of(Map.of("protected", "false"), Map.<String, String>of())) {
|
||||
String allowed = group(tags);
|
||||
prepare(action, allowed, "selected");
|
||||
stream(caller, action, allowed, "selected").statusCode(200);
|
||||
assertPresent(allowed, "selected", action.equals("CreateLogStream"));
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"CreateLogStream", "DeleteLogStream"})
|
||||
void resourceTagAllowReadsTheOwningGroup(String action) {
|
||||
String group = group(Map.of("team", "blue"));
|
||||
String other = group(Map.of("team", "red"));
|
||||
String untagged = group(Map.of());
|
||||
prepare(action, group, "selected");
|
||||
prepare(action, other, "selected");
|
||||
prepare(action, untagged, "selected");
|
||||
String caller = user(policy("Allow", action, "*",
|
||||
",\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"blue\"}}"));
|
||||
|
||||
stream(caller, action, other, "selected").statusCode(400)
|
||||
.body(containsString("AccessDeniedException"));
|
||||
assertPresent(other, "selected", action.equals("DeleteLogStream"));
|
||||
stream(caller, action, untagged, "selected").statusCode(400)
|
||||
.body(containsString("AccessDeniedException"));
|
||||
assertPresent(untagged, "selected", action.equals("DeleteLogStream"));
|
||||
stream(caller, action, group, "selected").statusCode(200);
|
||||
assertPresent(group, "selected", action.equals("CreateLogStream"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"CreateLogStream", "DeleteLogStream"})
|
||||
void exactDenyOverridesWildcardAllowWithoutChangingTheStream(String action) {
|
||||
String group = group(Map.of());
|
||||
prepare(action, group, "protected");
|
||||
prepare(action, group, "allowed");
|
||||
String caller = user(policy("Allow", action, "*", "") + ","
|
||||
+ policy("Deny", action, streamArn(group, "protected"), ""));
|
||||
stream(caller, action, group, "protected").statusCode(400)
|
||||
.body(containsString("AccessDeniedException"));
|
||||
assertPresent(group, "protected", action.equals("DeleteLogStream"));
|
||||
if (action.equals("DeleteLogStream")) {
|
||||
assertEvent(group, "protected");
|
||||
}
|
||||
stream(caller, action, group, "allowed").statusCode(200);
|
||||
assertPresent(group, "allowed", action.equals("CreateLogStream"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"CreateLogStream", "DeleteLogStream"})
|
||||
void numericNamesUseTheSameTargetAsTheHandler(String action) {
|
||||
String group = group(Map.of());
|
||||
prepare(action, group, "42");
|
||||
String caller = user(policy("Allow", action, "*", "") + ","
|
||||
+ policy("Deny", action, streamArn(group, "42"), ""));
|
||||
call(endpoint.getPort(), caller, action, Map.of("logGroupName", group, "logStreamName", 42))
|
||||
.statusCode(400).body(containsString("AccessDeniedException"));
|
||||
assertPresent(group, "42", action.equals("DeleteLogStream"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"CreateLogStream", "DeleteLogStream"})
|
||||
void sameNamedGroupsKeepTheirAccountAndRegionTags(String action) {
|
||||
String group = "logs-stream-scope-" + UUID.randomUUID();
|
||||
String otherAccount = "222222222222";
|
||||
String otherRegion = "eu-west-1";
|
||||
createGroup(ACCOUNT, REGION, group, Map.of("team", "blue"));
|
||||
createGroup(ACCOUNT, otherRegion, group, Map.of("team", "red"));
|
||||
createGroup(otherAccount, REGION, group, Map.of("team", "red"));
|
||||
if (action.equals("DeleteLogStream")) {
|
||||
for (String[] scope : List.of(new String[]{ACCOUNT, REGION}, new String[]{ACCOUNT, otherRegion},
|
||||
new String[]{otherAccount, REGION})) {
|
||||
call(endpoint.getPort(), scope[0], scope[1], "CreateLogStream",
|
||||
Map.of("logGroupName", group, "logStreamName", "selected")).statusCode(200);
|
||||
}
|
||||
}
|
||||
String statements = policy("Allow", action, "*",
|
||||
",\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"blue\"}}");
|
||||
String caller = user(statements);
|
||||
String otherCaller = user(otherAccount, statements);
|
||||
call(endpoint.getPort(), caller, otherRegion, action,
|
||||
Map.of("logGroupName", group, "logStreamName", "selected"))
|
||||
.statusCode(400).body(containsString("AccessDeniedException"));
|
||||
call(endpoint.getPort(), otherCaller, REGION, action,
|
||||
Map.of("logGroupName", group, "logStreamName", "selected"))
|
||||
.statusCode(400).body(containsString("AccessDeniedException"));
|
||||
assertPresent(ACCOUNT, otherRegion, group, "selected", action.equals("DeleteLogStream"));
|
||||
assertPresent(otherAccount, REGION, group, "selected", action.equals("DeleteLogStream"));
|
||||
stream(caller, action, group, "selected").statusCode(200);
|
||||
assertPresent(group, "selected", action.equals("CreateLogStream"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"CreateLogStream", "DeleteLogStream"})
|
||||
void missingGroupKeepsTheHandlersResourceNotFoundResponse(String action) {
|
||||
String caller = user(policy("Allow", action, "*", ""));
|
||||
stream(caller, action, "missing-" + UUID.randomUUID(), "selected")
|
||||
.statusCode(400).body(containsString("ResourceNotFoundException"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"CreateLogStream", "DeleteLogStream"})
|
||||
void queryCredentialsAuthorizeAndMutateTheSameRegion(String action) {
|
||||
String group = "logs-query-scope-" + UUID.randomUUID();
|
||||
String region = "eu-west-1";
|
||||
createGroup(ACCOUNT, REGION, group, Map.of("team", "red"));
|
||||
createGroup(ACCOUNT, region, group, Map.of("team", "blue"));
|
||||
if (action.equals("DeleteLogStream")) {
|
||||
for (String targetRegion : List.of(REGION, region)) {
|
||||
call(endpoint.getPort(), ACCOUNT, targetRegion, "CreateLogStream",
|
||||
Map.of("logGroupName", group, "logStreamName", "selected")).statusCode(200);
|
||||
}
|
||||
}
|
||||
String resource = "arn:aws:logs:" + region + ":" + ACCOUNT + ":log-group:" + group + ":log-stream:selected";
|
||||
String caller = user(policy("Allow", action, resource,
|
||||
",\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"blue\"}}"));
|
||||
given().port(endpoint.getPort()).contentType("application/x-amz-json-1.1")
|
||||
.queryParam("X-Amz-Algorithm", "AWS4-HMAC-SHA256")
|
||||
.queryParam("X-Amz-Credential", caller + "/20261015/" + region + "/logs/aws4_request")
|
||||
.queryParam("X-Amz-Signature", "abc")
|
||||
.header("X-Amz-Target", "Logs_20140328." + action)
|
||||
.body(Map.of("logGroupName", group, "logStreamName", "selected"))
|
||||
.when().post("/").then().statusCode(200);
|
||||
assertPresent(ACCOUNT, REGION, group, "selected", action.equals("DeleteLogStream"));
|
||||
assertPresent(ACCOUNT, region, group, "selected", action.equals("CreateLogStream"));
|
||||
// A real Authorization header remains authoritative over conflicting query credentials.
|
||||
given().port(endpoint.getPort()).contentType("application/x-amz-json-1.1")
|
||||
.header("Authorization", EnforcementFixtures.auth(caller, "logs"))
|
||||
.queryParam("X-Amz-Algorithm", "AWS4-HMAC-SHA256")
|
||||
.queryParam("X-Amz-Credential", caller + "/20261015/" + region + "/logs/aws4_request")
|
||||
.header("X-Amz-Target", "Logs_20140328." + action)
|
||||
.body(Map.of("logGroupName", group, "logStreamName", "selected"))
|
||||
.when().post("/").then().statusCode(400).body(containsString("AccessDeniedException"));
|
||||
assertPresent(ACCOUNT, REGION, group, "selected", action.equals("DeleteLogStream"));
|
||||
assertPresent(ACCOUNT, region, group, "selected", action.equals("CreateLogStream"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"CreateLogStream,blank-header", "DeleteLogStream,blank-header",
|
||||
"CreateLogStream,missing-algorithm", "DeleteLogStream,missing-algorithm"})
|
||||
void credentialEdgeCasesKeepTheCanonicalRequestRegion(String action, String mode) {
|
||||
String account = "000000000000";
|
||||
String remote = "eu-west-1";
|
||||
String group = "logs-query-edge-" + UUID.randomUUID();
|
||||
boolean blankHeader = mode.equals("blank-header");
|
||||
String selectedRegion = blankHeader ? remote : REGION;
|
||||
String otherRegion = blankHeader ? REGION : remote;
|
||||
createGroup(account, selectedRegion, group, Map.of("team", "blue"));
|
||||
createGroup(account, otherRegion, group, Map.of("team", "red"));
|
||||
if (action.equals("DeleteLogStream")) {
|
||||
for (String region : List.of(REGION, remote)) {
|
||||
call(endpoint.getPort(), account, region, "CreateLogStream",
|
||||
Map.of("logGroupName", group, "logStreamName", "selected")).statusCode(200);
|
||||
}
|
||||
}
|
||||
String resource = "arn:aws:logs:" + selectedRegion + ":" + account
|
||||
+ ":log-group:" + group + ":log-stream:selected";
|
||||
String caller = user(account, policy("Allow", action, resource,
|
||||
",\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"blue\"}}"));
|
||||
RequestSpecification request = given().port(endpoint.getPort()).contentType("application/x-amz-json-1.1")
|
||||
.queryParam("X-Amz-Credential", caller + "/20261015/" + remote + "/logs/aws4_request")
|
||||
.queryParam("X-Amz-Signature", "abc")
|
||||
.header("X-Amz-Target", "Logs_20140328." + action)
|
||||
.body(Map.of("logGroupName", group, "logStreamName", "selected"));
|
||||
if (blankHeader) {
|
||||
request.header("Authorization", "").queryParam("X-Amz-Algorithm", "AWS4-HMAC-SHA256");
|
||||
}
|
||||
request.when().post("/").then().statusCode(200);
|
||||
assertPresent(account, otherRegion, group, "selected", action.equals("DeleteLogStream"));
|
||||
assertPresent(account, selectedRegion, group, "selected", action.equals("CreateLogStream"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void storageKeyAliasesCannotDeleteAnotherGroupsStream(boolean exactGrant) {
|
||||
String group = "logs-key-alias-" + UUID.randomUUID();
|
||||
String protectedGroup = group + "::x";
|
||||
createGroup(ACCOUNT, REGION, group, Map.of("team", "blue"));
|
||||
createGroup(ACCOUNT, REGION, protectedGroup, Map.of("team", "red"));
|
||||
seedHistoricalStream(protectedGroup, "s");
|
||||
assertPresent(protectedGroup, "s", true);
|
||||
assertEvent(protectedGroup, "s");
|
||||
String caller = user(policy("Allow", "DeleteLogStream", exactGrant ? streamArn(group, "x::s") : "*",
|
||||
",\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"blue\"}}"));
|
||||
Response response = stream(caller, "DeleteLogStream", group, "x::s").extract().response();
|
||||
assertAll("reject storage alias and retain historical stream and event",
|
||||
() -> response.then().statusCode(400).body(containsString("InvalidParameterException")),
|
||||
() -> assertPresent(protectedGroup, "s", true),
|
||||
() -> assertEvent(protectedGroup, "s"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"CreateLogStream", "DeleteLogStream"})
|
||||
void arnDelimiterAliasesCannotGrantAccessToAnotherGroup(String action) {
|
||||
String group = "logs-arn-alias-" + UUID.randomUUID();
|
||||
String actualGroup = group + ":log-stream:x";
|
||||
createGroup(ACCOUNT, REGION, group, Map.of());
|
||||
createGroup(ACCOUNT, REGION, actualGroup, Map.of());
|
||||
boolean delete = action.equals("DeleteLogStream");
|
||||
if (delete) {
|
||||
seedHistoricalStream(actualGroup, "s");
|
||||
}
|
||||
prepare(action, group, "safe");
|
||||
assertPresent(actualGroup, "s", delete);
|
||||
String caller = user(policy("Allow", action, streamArn(group, "x:log-stream:s"), "")
|
||||
+ "," + policy("Allow", action, streamArn(group, "safe"), ""));
|
||||
Response response = stream(caller, action, actualGroup, "s").extract().response();
|
||||
assertAll("reject ambiguous ARN identity without changing the stored target",
|
||||
() -> response.then().statusCode(400).body(containsString("InvalidParameterException")),
|
||||
() -> assertPresent(actualGroup, "s", delete),
|
||||
() -> {
|
||||
if (delete) {
|
||||
assertEvent(actualGroup, "s");
|
||||
}
|
||||
});
|
||||
stream(caller, action, group, "safe").statusCode(200);
|
||||
assertPresent(group, "safe", !delete);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"CreateLogStream", "DeleteLogStream"})
|
||||
void publicStreamNamesFollowTheDocumentedConstraints(String action) {
|
||||
String group = group(Map.of());
|
||||
String caller = user(policy("Allow", action, "*", ""));
|
||||
prepare(action, group, "selected");
|
||||
for (String invalid : List.of("", "bad:group", "bad*group", "bad group", "g".repeat(513))) {
|
||||
stream(caller, action, invalid, "selected").statusCode(400)
|
||||
.body(containsString("InvalidParameterException"));
|
||||
}
|
||||
for (String invalid : List.of("", "bad:stream", "bad*stream", "s".repeat(513))) {
|
||||
stream(caller, action, group, invalid).statusCode(400)
|
||||
.body(containsString("InvalidParameterException"));
|
||||
}
|
||||
assertPresent(group, "selected", action.equals("DeleteLogStream"));
|
||||
if (action.equals("DeleteLogStream")) {
|
||||
assertEvent(group, "selected");
|
||||
}
|
||||
stream(caller, action, group, "selected").statusCode(200);
|
||||
assertPresent(group, "selected", action.equals("CreateLogStream"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"CreateLogStream", "DeleteLogStream"})
|
||||
void maximumLengthAndWhitespaceStreamNamesRemainUsable(String action) {
|
||||
String prefix = "logs-name-boundary-" + UUID.randomUUID();
|
||||
String group = prefix + "g".repeat(512 - prefix.length());
|
||||
createGroup(ACCOUNT, REGION, group, Map.of());
|
||||
String caller = user(policy("Allow", action, "*", ""));
|
||||
for (String stream : List.of("s", " ", "🚀".repeat(512))) {
|
||||
prepare(action, group, stream);
|
||||
stream(caller, action, group, stream).statusCode(200);
|
||||
assertPresent(group, stream, action.equals("CreateLogStream"));
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"s::child", "s::child::nested"})
|
||||
void deletingAnAuthorizedStreamPreservesHistoricalSiblingEvents(String sibling) {
|
||||
String group = group(Map.of());
|
||||
seedHistoricalStream(group, "s", "selected-event");
|
||||
seedHistoricalStream(group, sibling, "protected-sibling-event");
|
||||
assertPresent(group, "s", true);
|
||||
assertPresent(group, sibling, true);
|
||||
call(endpoint.getPort(), ACCOUNT, "GetLogEvents", Map.of("logGroupName", group, "logStreamName", sibling))
|
||||
.statusCode(200).body("events[0].message", equalTo("protected-sibling-event"));
|
||||
String caller = user(policy("Allow", "DeleteLogStream", streamArn(group, "s"), ""));
|
||||
stream(caller, "DeleteLogStream", group, "s").statusCode(200);
|
||||
assertAll("delete only the selected stream and its emitted events",
|
||||
() -> assertPresent(group, "s", false),
|
||||
() -> assertPresent(group, sibling, true),
|
||||
() -> call(endpoint.getPort(), ACCOUNT, "GetLogEvents",
|
||||
Map.of("logGroupName", group, "logStreamName", sibling))
|
||||
.statusCode(200).body("events[0].message", equalTo("protected-sibling-event")),
|
||||
() -> call(endpoint.getPort(), ACCOUNT, "GetLogEvents",
|
||||
Map.of("logGroupName", group, "logStreamName", "s"))
|
||||
.statusCode(200).body("events.message", not(hasItem("selected-event"))));
|
||||
}
|
||||
|
||||
/** Existing service storage APIs seed historical invalid names without bypassing the HTTP request under test. */
|
||||
private void seedHistoricalStream(String group, String stream) {
|
||||
seedHistoricalStream(group, stream, "retained");
|
||||
}
|
||||
|
||||
private void seedHistoricalStream(String group, String stream, String message) {
|
||||
logsService.createLogStreamForAccount(ACCOUNT, group, stream, REGION);
|
||||
logsService.putLogEventsForAccount(ACCOUNT, group, stream,
|
||||
List.of(Map.of("timestamp", System.currentTimeMillis(), "message", message)), REGION);
|
||||
}
|
||||
|
||||
private void prepare(String action, String group, String name) {
|
||||
if (action.equals("DeleteLogStream")) {
|
||||
stream(ACCOUNT, "CreateLogStream", group, name).statusCode(200);
|
||||
call(endpoint.getPort(), ACCOUNT, "PutLogEvents", Map.of("logGroupName", group,
|
||||
"logStreamName", name, "logEvents", List.of(Map.of("timestamp", System.currentTimeMillis(),
|
||||
"message", "retained")))).statusCode(200);
|
||||
}
|
||||
}
|
||||
|
||||
private String group(Map<String, String> tags) {
|
||||
String group = "logs-stream-auth-" + UUID.randomUUID();
|
||||
createGroup(ACCOUNT, REGION, group, tags);
|
||||
return group;
|
||||
}
|
||||
|
||||
private void createGroup(String account, String region, String group, Map<String, String> tags) {
|
||||
int port = endpoint.getPort();
|
||||
cleanup.register(() -> call(port, account, region, "DeleteLogGroup", Map.of("logGroupName", group))
|
||||
.statusCode(200));
|
||||
call(port, account, region, "CreateLogGroup", Map.of("logGroupName", group, "tags", tags)).statusCode(200);
|
||||
}
|
||||
|
||||
private String user(String statements) {
|
||||
return user(ACCOUNT, statements);
|
||||
}
|
||||
|
||||
private String user(String account, String statements) {
|
||||
int port = endpoint.getPort();
|
||||
String name = "logs-stream-auth-" + UUID.randomUUID();
|
||||
String root = EnforcementFixtures.auth(account, "iam");
|
||||
EnforcementFixtures.UserCredentials credentials = EnforcementFixtures.createUser(cleanup, port, root, name);
|
||||
given().port(port).header("Authorization", root).formParam("Action", "PutUserPolicy")
|
||||
.formParam("UserName", name).formParam("PolicyName", "streams")
|
||||
.formParam("PolicyDocument", "{\"Version\":\"2012-10-17\",\"Statement\":[" + statements + "]}")
|
||||
.when().post("/").then().statusCode(200);
|
||||
return credentials.accessKeyId();
|
||||
}
|
||||
|
||||
private static String policy(String effect, String action, String resource, String condition) {
|
||||
return "{\"Effect\":\"" + effect + "\",\"Action\":\"logs:" + action
|
||||
+ "\",\"Resource\":\"" + resource + "\"" + condition + "}";
|
||||
}
|
||||
|
||||
private static String streamArn(String group, String stream) {
|
||||
return "arn:aws:logs:" + REGION + ":" + ACCOUNT + ":log-group:" + group + ":log-stream:" + stream;
|
||||
}
|
||||
|
||||
private ValidatableResponse stream(String caller, String action, String group, String stream) {
|
||||
return call(endpoint.getPort(), caller, action, Map.of("logGroupName", group, "logStreamName", stream));
|
||||
}
|
||||
|
||||
private void assertPresent(String group, String stream, boolean expected) {
|
||||
assertPresent(ACCOUNT, REGION, group, stream, expected);
|
||||
}
|
||||
|
||||
private void assertEvent(String group, String stream) {
|
||||
call(endpoint.getPort(), ACCOUNT, "GetLogEvents", Map.of("logGroupName", group, "logStreamName", stream))
|
||||
.statusCode(200).body("events[0].message", equalTo("retained"));
|
||||
}
|
||||
|
||||
private void assertPresent(String account, String region, String group, String stream, boolean expected) {
|
||||
List<Map<String, Object>> streams = call(endpoint.getPort(), account, region, "DescribeLogStreams",
|
||||
Map.of("logGroupName", group)).statusCode(200).extract().path("logStreams");
|
||||
assertEquals(expected, streams.stream().anyMatch(item -> stream.equals(item.get("logStreamName"))));
|
||||
}
|
||||
|
||||
private static ValidatableResponse call(int port, String caller, String action, Map<String, ?> body) {
|
||||
return call(port, caller, REGION, action, body);
|
||||
}
|
||||
|
||||
private static ValidatableResponse call(int port, String caller, String region, String action, Map<String, ?> body) {
|
||||
String authorization = "AWS4-HMAC-SHA256 Credential=" + caller + "/20261015/" + region
|
||||
+ "/logs/aws4_request, SignedHeaders=host, Signature=abc";
|
||||
return given().port(port).contentType("application/x-amz-json-1.1")
|
||||
.header("Authorization", authorization)
|
||||
.header("X-Amz-Target", "Logs_20140328." + action)
|
||||
.body(new LinkedHashMap<>(body)).when().post("/").then();
|
||||
}
|
||||
}
|
||||
+179
@@ -0,0 +1,179 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsService;
|
||||
import io.github.hectorvent.floci.testing.EnforcementFixtures;
|
||||
import io.github.hectorvent.floci.testing.PartitionCleanup;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.common.http.TestHTTPResource;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.QuarkusTestProfile;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import io.restassured.response.Response;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.jupiter.api.Assertions.assertAll;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@QuarkusTest
|
||||
@TestProfile(LogsUnknownRegionAuthorizationIntegrationTest.UnknownRegionProfile.class)
|
||||
class LogsUnknownRegionAuthorizationIntegrationTest {
|
||||
|
||||
public static class UnknownRegionProfile implements QuarkusTestProfile {
|
||||
@Override
|
||||
public Map<String, String> getConfigOverrides() {
|
||||
return Map.of("floci.services.iam.enforcement-enabled", "true",
|
||||
"floci.partitions.allow-unknown-regions", "true");
|
||||
}
|
||||
}
|
||||
|
||||
private static final String ACCOUNT = "111111111111";
|
||||
private static final String REGION = "us-east-1";
|
||||
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
|
||||
@TestHTTPResource
|
||||
URI endpoint;
|
||||
|
||||
@Inject
|
||||
CloudWatchLogsService logsService;
|
||||
|
||||
@BeforeAll
|
||||
static void configureContentTypes() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"CreateLogStream,false,false", "DeleteLogStream,false,false", "CreateLogStream,true,false",
|
||||
"DeleteLogStream,true,false", "DeleteLogStream,false,true"})
|
||||
void optedInRegionLabelsCannotAliasHistoricalGroupNames(String action, boolean conditional, boolean streamAlias) {
|
||||
int port = endpoint.getPort();
|
||||
String group = "logs-region-alias-" + UUID.randomUUID();
|
||||
String historical = streamAlias ? group : "x::" + group;
|
||||
String historicalStream = streamAlias ? "x::" + group + "::s" : "s";
|
||||
String aliasRegion = streamAlias ? REGION + "::" + group + "::x" : REGION + "::x";
|
||||
String validUnknownRegion = "custom-region-1";
|
||||
cleanup.register(() -> call(port, ACCOUNT, REGION, "DeleteLogGroup", Map.of("logGroupName", historical))
|
||||
.then().statusCode(200));
|
||||
cleanup.register(() -> call(port, ACCOUNT, validUnknownRegion, "DeleteLogGroup", Map.of("logGroupName", group))
|
||||
.then().statusCode(200));
|
||||
logsService.createLogGroupForAccount(ACCOUNT, historical, null, Map.of("team", "blue"), REGION);
|
||||
logsService.createLogGroupForAccount(ACCOUNT, group, null, Map.of("team", "blue"), validUnknownRegion);
|
||||
boolean delete = action.equals("DeleteLogStream");
|
||||
if (delete) {
|
||||
for (String[] target : List.of(new String[]{historical, REGION, historicalStream},
|
||||
new String[]{group, validUnknownRegion, "s"})) {
|
||||
logsService.createLogStreamForAccount(ACCOUNT, target[0], target[2], target[1]);
|
||||
logsService.putLogEventsForAccount(ACCOUNT, target[0], target[2],
|
||||
List.of(Map.of("timestamp", System.currentTimeMillis(), "message", "retained")), target[1]);
|
||||
}
|
||||
}
|
||||
assertPresent(port, REGION, historical, historicalStream, delete);
|
||||
String user = "logs-region-alias-" + UUID.randomUUID();
|
||||
String root = EnforcementFixtures.auth(ACCOUNT, "iam");
|
||||
String caller = EnforcementFixtures.createUser(cleanup, port, root, user).accessKeyId();
|
||||
String condition = conditional ? ",\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"blue\"}}" : "";
|
||||
String resources = "[\"arn:aws:logs:" + aliasRegion + ":" + ACCOUNT + ":log-group:" + group
|
||||
+ ":log-stream:s\",\"arn:aws:logs:" + validUnknownRegion + ":" + ACCOUNT
|
||||
+ ":log-group:" + group + ":log-stream:s\"]";
|
||||
given().port(port).header("Authorization", root).formParam("Action", "PutUserPolicy")
|
||||
.formParam("UserName", user).formParam("PolicyName", "streams")
|
||||
.formParam("PolicyDocument", "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
|
||||
+ "\"Action\":\"logs:" + action + "\",\"Resource\":" + resources + condition + "}]}")
|
||||
.when().post("/").then().statusCode(200);
|
||||
Response response = call(port, caller, aliasRegion, action,
|
||||
Map.of("logGroupName", group, "logStreamName", "s"));
|
||||
assertAll("preserve stored identity across an opted-in region delimiter",
|
||||
() -> response.then().statusCode(400).body(containsString(
|
||||
conditional ? "AccessDeniedException" : "ResourceNotFoundException")),
|
||||
() -> assertPresent(port, REGION, historical, historicalStream, delete),
|
||||
() -> {
|
||||
if (delete) {
|
||||
call(port, ACCOUNT, REGION, "GetLogEvents",
|
||||
Map.of("logGroupName", historical, "logStreamName", historicalStream))
|
||||
.then().statusCode(200).body("events[0].message", equalTo("retained"));
|
||||
}
|
||||
});
|
||||
call(port, caller, validUnknownRegion, action, Map.of("logGroupName", group, "logStreamName", "s"))
|
||||
.then().statusCode(200);
|
||||
assertPresent(port, validUnknownRegion, group, !delete);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"CreateLogStream,Allow,false", "DeleteLogStream,Allow,false",
|
||||
"CreateLogStream,Allow,true", "DeleteLogStream,Allow,true",
|
||||
"CreateLogStream,Deny,false", "DeleteLogStream,Deny,false",
|
||||
"CreateLogStream,Deny,true", "DeleteLogStream,Deny,true"})
|
||||
void resourceAccountUsesStorageOwnerInOptedInRegion(String action, String effect, boolean actualOwner) {
|
||||
int port = endpoint.getPort();
|
||||
String otherAccount = "222222222222";
|
||||
String region = "custom:" + otherAccount;
|
||||
String group = "logs-resource-owner-" + UUID.randomUUID();
|
||||
cleanup.register(() -> call(port, ACCOUNT, region, "DeleteLogGroup", Map.of("logGroupName", group))
|
||||
.then().statusCode(200));
|
||||
logsService.createLogGroupForAccount(ACCOUNT, group, null, null, region);
|
||||
boolean delete = action.equals("DeleteLogStream");
|
||||
if (delete) {
|
||||
logsService.createLogStreamForAccount(ACCOUNT, group, "s", region);
|
||||
logsService.putLogEventsForAccount(ACCOUNT, group, "s",
|
||||
List.of(Map.of("timestamp", System.currentTimeMillis(), "message", "retained")), region);
|
||||
}
|
||||
String user = "logs-resource-owner-" + UUID.randomUUID();
|
||||
String root = EnforcementFixtures.auth(ACCOUNT, "iam");
|
||||
String caller = EnforcementFixtures.createUser(cleanup, port, root, user).accessKeyId();
|
||||
String grant = effect.equals("Deny")
|
||||
? "{\"Effect\":\"Allow\",\"Action\":\"logs:" + action + "\",\"Resource\":\"*\"}," : "";
|
||||
String owner = actualOwner ? ACCOUNT : otherAccount;
|
||||
String statement = "{\"Effect\":\"" + effect + "\",\"Action\":\"logs:" + action
|
||||
+ "\",\"Resource\":\"*\",\"Condition\":{\"StringEquals\":{\"aws:ResourceAccount\":\""
|
||||
+ owner + "\"}}}";
|
||||
given().port(port).header("Authorization", root).formParam("Action", "PutUserPolicy")
|
||||
.formParam("UserName", user).formParam("PolicyName", "streams")
|
||||
.formParam("PolicyDocument", "{\"Version\":\"2012-10-17\",\"Statement\":[" + grant + statement + "]}")
|
||||
.when().post("/").then().statusCode(200);
|
||||
boolean allowed = effect.equals("Allow") == actualOwner;
|
||||
Response response = call(port, caller, region, action, Map.of("logGroupName", group, "logStreamName", "s"));
|
||||
assertAll("use the canonical account independently of region punctuation",
|
||||
() -> response.then().statusCode(allowed ? 200 : 400),
|
||||
() -> assertPresent(port, region, group, allowed != delete),
|
||||
() -> {
|
||||
if (!allowed) {
|
||||
response.then().body(containsString("AccessDeniedException"));
|
||||
if (delete) {
|
||||
call(port, ACCOUNT, region, "GetLogEvents",
|
||||
Map.of("logGroupName", group, "logStreamName", "s"))
|
||||
.then().statusCode(200).body("events[0].message", equalTo("retained"));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void assertPresent(int port, String region, String group, boolean expected) {
|
||||
assertPresent(port, region, group, "s", expected);
|
||||
}
|
||||
|
||||
private static void assertPresent(int port, String region, String group, String name, boolean expected) {
|
||||
List<Map<String, Object>> streams = call(port, ACCOUNT, region, "DescribeLogStreams",
|
||||
Map.of("logGroupName", group)).then().statusCode(200).extract().path("logStreams");
|
||||
assertEquals(expected, streams.stream().anyMatch(stream -> name.equals(stream.get("logStreamName"))));
|
||||
}
|
||||
|
||||
private static Response call(int port, String caller, String region, String action, Map<String, ?> body) {
|
||||
String auth = "AWS4-HMAC-SHA256 Credential=" + caller + "/20261015/" + region
|
||||
+ "/logs/aws4_request, SignedHeaders=host, Signature=abc";
|
||||
return given().port(port).contentType("application/x-amz-json-1.1").header("Authorization", auth)
|
||||
.header("X-Amz-Target", "Logs_20140328." + action).body(body).when().post("/");
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsRegions;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.RequestContext;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
@@ -646,6 +647,28 @@ class ResourceArnBuilderTest {
|
||||
}).when(ctx).setEntityStream(any(InputStream.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void logStreamTargetsUseTheServingActionAndRequestPartition() {
|
||||
for (String action : List.of("logs:CreateLogStream", "logs:DeleteLogStream")) {
|
||||
for (String region : List.of("us-east-1", "cn-north-1", "us-gov-west-1")) {
|
||||
setJsonBody("{\"logGroupName\":\" /group \",\"logStreamName\":42}");
|
||||
when(ctx.getHeaderString("X-Amz-Target")).thenReturn("Logs_20140328.DeleteLogGroup");
|
||||
assertEquals(List.of("arn:" + AwsRegions.partitionFor(region)
|
||||
+ ":logs:" + region + ":123456789012:log-group: /group :log-stream:42"),
|
||||
builder.buildLogStreamResources(action, ctx, region, "123456789012", AwsRegions.partitionFor(region)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void excludedLogsActionsKeepWildcardResources() {
|
||||
for (String action : List.of("logs:CreateLogGroup", "logs:DeleteLogGroup", "logs:PutRetentionPolicy",
|
||||
"logs:TagResource", "logs:UntagResource", "logs:ListTagsForResource", "logs:PutLogEvents")) {
|
||||
setJsonBody("{\"logGroupName\":\"group\",\"logStreamName\":\"stream\"}");
|
||||
assertEquals(List.of("*"), builder.buildLogStreamResources(action, ctx, "us-east-1", ACCOUNT, "aws"));
|
||||
}
|
||||
}
|
||||
|
||||
// ── DynamoDB ─────────────────────────────────────────────────────────────────
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user