fix(logs): bind ARN tag operations to their owning group (#5810)

* fix(logs): bind ARN tag operations to their owning group

* fix(logs): preserve literal names in tag requests

* test(logs): share authorization fixtures
This commit is contained in:
David Phillips authored and GitHub committed 2026-10-16 18:23:59 -04:00
1 parent 1f6261fc1c
commit 49a3ff9a10
16 files changed
+720 -169

No files matched your search

@@ -10,6 +10,8 @@ import io.github.hectorvent.floci.services.cloudwatch.logs.model.LogStream;
import io.github.hectorvent.floci.services.cloudwatch.logs.model.SubscriptionFilter;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.util.ArrayList;
import java.util.List;
@@ -38,6 +40,34 @@ class CloudWatchLogsServiceTest {
);
}
@ParameterizedTest
@ValueSource(strings = {"TagLogGroup", "UntagLogGroup"})
void tagMutationCannotBorrowHistoricalGroupThroughRegionAlias(String action) {
String historical = "x::victim";
String aliasRegion = REGION + "::x";
service.createLogGroup(historical, null, Map.of("team", "blue"), REGION);
assertThrows(AwsException.class, () -> service.listTagsLogGroup("victim", aliasRegion));
assertAll("the shared tag mutator preserves the stored group identity",
() -> {
AwsException error = assertThrows(AwsException.class, () -> {
if (action.equals("TagLogGroup")) {
service.tagLogGroup("victim", Map.of("team", "red"), aliasRegion);
} else {
service.untagLogGroup("victim", List.of("team"), aliasRegion);
}
});
assertEquals("ResourceNotFoundException", error.getErrorCode());
},
() -> assertEquals(Map.of("team", "blue"), service.listTagsLogGroup(historical, REGION)));
if (action.equals("TagLogGroup")) {
service.tagLogGroup(historical, Map.of("team", "red"), REGION);
assertEquals(Map.of("team", "red"), service.listTagsLogGroup(historical, REGION));
} else {
service.untagLogGroup(historical, List.of("team"), REGION);
assertEquals(Map.of(), service.listTagsLogGroup(historical, REGION));
}
}
// ──────────────────────────── Log Groups ────────────────────────────
@Test
@@ -0,0 +1,249 @@
package io.github.hectorvent.floci.services.iam;
import io.github.hectorvent.floci.testing.IamEnforcementProfile;
import io.github.hectorvent.floci.testing.LogsAuthorizationFixtures;
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.ValidatableResponse;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
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.List;
import java.util.Map;
import java.util.UUID;
import static io.github.hectorvent.floci.testing.LogsAuthorizationFixtures.expectedTags;
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;
import static org.junit.jupiter.api.Assertions.assertTrue;
@QuarkusTest
@TestProfile(IamEnforcementProfile.class)
class LogsArnTagAuthorizationIntegrationTest {
private static final String ACCOUNT = "111111111111";
private static final String REGION = "us-east-1";
@RegisterExtension
final PartitionCleanup cleanup = new PartitionCleanup();
@TestHTTPResource
URI endpoint;
@BeforeAll
static void configureContentTypes() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@ParameterizedTest
@ValueSource(strings = {"TagResource", "UntagResource", "ListTagsForResource"})
void exactBareGroupArnGrant(String action) {
String selected = group();
String other = group(Map.of("team", "green"));
String caller = user(statement("Allow", action, arn(selected)));
invoke(caller, action, arn(other)).statusCode(400).body(containsString("AccessDeniedException"));
assertTags(other, Map.of("team", "green"));
assertSuccess(invoke(caller, action, arn(selected)), action);
assertTags(selected, expectedTags(action));
}
@ParameterizedTest
@ValueSource(strings = {"TagResource", "UntagResource", "ListTagsForResource"})
void exactBareGroupArnDenyOverridesWildcard(String action) {
String selected = group();
String other = group();
String caller = user(statement("Allow", action, "*") + "," + statement("Deny", action, arn(selected)));
ValidatableResponse response = invoke(caller, action, arn(selected));
assertAll("the exact deny prevents access and preserves tags",
() -> response.statusCode(400).body(containsString("AccessDeniedException")),
() -> assertTags(selected, Map.of("team", "blue")));
assertSuccess(invoke(caller, action, arn(other)), action);
assertTags(other, expectedTags(action));
}
@ParameterizedTest
@CsvSource({"TagResource,account", "UntagResource,account", "ListTagsForResource,account",
"TagResource,region", "UntagResource,region", "ListTagsForResource,region",
"TagResource,partition", "UntagResource,partition", "ListTagsForResource,partition"})
void foreignArnCannotBorrowLocalGroup(String action, String dimension) {
verifyForeignArn(action, dimension, false);
}
@ParameterizedTest
@CsvSource({"TagResource,account", "UntagResource,account", "ListTagsForResource,account",
"TagResource,region", "UntagResource,region", "ListTagsForResource,region",
"TagResource,partition", "UntagResource,partition", "ListTagsForResource,partition"})
void rootBypassCannotBorrowLocalGroup(String action, String dimension) {
verifyForeignArn(action, dimension, true);
}
private void verifyForeignArn(String action, String dimension, boolean root) {
String local = group();
String caller = root ? ACCOUNT : user(statement("Allow", action, "*"));
String foreign = switch (dimension) {
case "account" -> "arn:aws:logs:" + REGION + ":222222222222:log-group:" + local;
case "region" -> "arn:aws:logs:us-west-2:" + ACCOUNT + ":log-group:" + local;
case "partition" -> "arn:aws-cn:logs:" + REGION + ":" + ACCOUNT + ":log-group:" + local;
default -> throw new IllegalArgumentException(dimension);
};
ValidatableResponse response = invoke(caller, action, foreign);
response.statusCode(400).body(containsString("ResourceNotFoundException"));
if (action.equals("ListTagsForResource")) {
Map<String, String> returned = response.extract().path("tags");
assertTrue(returned == null || !"blue".equals(returned.get("team")),
"foreign identity must not disclose the local tag entry");
}
assertTags(local, Map.of("team", "blue"));
assertSuccess(invoke(caller, action, arn(local)), action);
assertTags(local, expectedTags(action));
}
@ParameterizedTest
@ValueSource(strings = {"TagResource", "UntagResource", "ListTagsForResource"})
void incompleteArnPrefixRemainsALiteralLegacyName(String action) {
String name = groupNamed("arn:archive-" + UUID.randomUUID(), Map.of("team", "blue"));
String caller = user(statement("Allow", action, arn(name)));
assertSuccess(invoke(caller, action, name), action);
assertTags(name, expectedTags(action));
}
@ParameterizedTest
@CsvSource({"TagResource,uppercase,false", "TagResource,uppercase,true",
"TagResource,space,false", "TagResource,space,true",
"TagResource,marker,false", "TagResource,marker,true",
"UntagResource,uppercase,false", "UntagResource,uppercase,true",
"UntagResource,space,false", "UntagResource,space,true",
"UntagResource,marker,false", "UntagResource,marker,true",
"ListTagsForResource,uppercase,false", "ListTagsForResource,uppercase,true",
"ListTagsForResource,space,false", "ListTagsForResource,space,true",
"ListTagsForResource,marker,false", "ListTagsForResource,marker,true"})
void noncanonicalMarkerInputCannotBorrowAnotherGroup(String action, String shape, boolean root) {
String local = group();
String input = switch (shape) {
case "uppercase" -> "ARN:aws:logs:eu-west-1:999999999999:log-group:" + local;
case "space" -> " arn:aws:logs:eu-west-1:999999999999:log-group:" + local;
case "marker" -> "x:log-group:" + local;
default -> throw new IllegalArgumentException(shape);
};
String caller = root ? ACCOUNT : user(statement("Allow", action, "*"));
ValidatableResponse response = invoke(caller, action, input);
assertAll("noncanonical input cannot access the shortened local name",
() -> response.statusCode(400).body(containsString("ResourceNotFoundException")),
() -> assertTags(local, Map.of("team", "blue")));
String literal = groupNamed(input, Map.of("team", "blue"));
assertSuccess(invoke(caller, action, input), action);
assertTags(literal, expectedTags(action));
assertTags(local, Map.of("team", "blue"));
assertSuccess(invoke(caller, action, arn(local)), action);
assertTags(local, expectedTags(action));
}
@ParameterizedTest
@ValueSource(strings = {"TagResource", "UntagResource", "ListTagsForResource"})
void storedTagDenyUsesTheOwningGroup(String action) {
String selected = group();
String other = group(Map.of("team", "green"));
String caller = user(statement("Allow", action, "*") + ","
+ "{\"Effect\":\"Deny\",\"Action\":\"logs:" + action + "\",\"Resource\":\"*\","
+ "\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"blue\"}}}");
invoke(caller, action, arn(selected)).statusCode(400).body(containsString("AccessDeniedException"));
assertTags(selected, Map.of("team", "blue"));
ValidatableResponse allowed = invoke(caller, action, arn(other)).statusCode(200);
if (action.equals("ListTagsForResource")) {
allowed.body("tags", equalTo(Map.of("team", "green")));
assertTags(other, Map.of("team", "green"));
} else {
assertTags(other, expectedTags(action));
}
}
@Test
void requestedTagValuesAndKeysControlMutation() {
String selected = group();
String caller = user("{\"Effect\":\"Allow\",\"Action\":\"logs:TagResource\",\"Resource\":\""
+ arn(selected) + "\",\"Condition\":{\"StringEquals\":{\"aws:RequestTag/team\":\"red\"},"
+ "\"ForAllValues:StringEquals\":{\"aws:TagKeys\":[\"team\"]}}}");
for (Map<String, String> denied : List.of(Map.of("team", "blue"),
Map.of("team", "red", "protected", "changed"))) {
call(endpoint.getPort(), caller, "TagResource", Map.of("resourceArn", arn(selected), "tags", denied))
.statusCode(400).body(containsString("AccessDeniedException"));
assertTags(selected, Map.of("team", "blue"));
}
invoke(caller, "TagResource", arn(selected)).statusCode(200);
assertTags(selected, Map.of("team", "red"));
}
@Test
void removalKeysDenyBeforeAnyTagIsRemoved() {
String selected = group(Map.of("team", "blue", "protected", "retained"));
String caller = user("{\"Effect\":\"Allow\",\"Action\":\"logs:UntagResource\",\"Resource\":\""
+ arn(selected) + "\",\"Condition\":{\"ForAllValues:StringEquals\":{\"aws:TagKeys\":[\"team\"]}}}");
call(endpoint.getPort(), caller, "UntagResource", Map.of("resourceArn", arn(selected),
"tagKeys", List.of("team", "protected")))
.statusCode(400).body(containsString("AccessDeniedException"));
assertTags(selected, Map.of("team", "blue", "protected", "retained"));
invoke(caller, "UntagResource", arn(selected)).statusCode(200);
assertTags(selected, Map.of("protected", "retained"));
}
private void assertSuccess(ValidatableResponse response, String action) {
response.statusCode(200);
if (action.equals("ListTagsForResource")) {
response.body("tags", equalTo(Map.of("team", "blue")));
}
}
private String group() {
return group(Map.of("team", "blue"));
}
private String group(Map<String, String> tags) {
return groupNamed("logs-arn-tag-" + UUID.randomUUID(), tags);
}
private String groupNamed(String name, Map<String, String> tags) {
int port = endpoint.getPort();
cleanup.register(() -> call(port, ACCOUNT, "DeleteLogGroup", Map.of("logGroupName", name)).statusCode(200));
call(port, ACCOUNT, "CreateLogGroup", Map.of("logGroupName", name, "tags", tags))
.statusCode(200);
return name;
}
private String user(String statements) {
return LogsAuthorizationFixtures.user(cleanup, endpoint.getPort(), ACCOUNT, "logs-arn-tag-",
"arn-tags", statements);
}
private static String statement(String effect, String action, String resource) {
return "{\"Effect\":\"" + effect + "\",\"Action\":\"logs:" + action
+ "\",\"Resource\":\"" + resource + "\"}";
}
private static String arn(String group) {
return "arn:aws:logs:" + REGION + ":" + ACCOUNT + ":log-group:" + group;
}
private ValidatableResponse invoke(String caller, String action, String resource) {
return call(endpoint.getPort(), caller, action, LogsAuthorizationFixtures.tagBody(action, resource));
}
private void assertTags(String group, Map<String, String> expected) {
Map<String, String> actual = call(endpoint.getPort(), ACCOUNT, "ListTagsLogGroup", Map.of("logGroupName", group))
.statusCode(200).extract().path("tags");
assertEquals(expected, actual);
}
private static ValidatableResponse call(int port, String caller, String action, Map<String, ?> body) {
return LogsAuthorizationFixtures.call(port, caller, REGION, action, body).then();
}
}
@@ -1,6 +1,6 @@
package io.github.hectorvent.floci.services.iam;
import io.github.hectorvent.floci.testing.EnforcementFixtures;
import io.github.hectorvent.floci.testing.LogsAuthorizationFixtures;
import io.github.hectorvent.floci.testing.PartitionCleanup;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.common.http.TestHTTPResource;
@@ -18,7 +18,6 @@ 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;
@@ -87,18 +86,13 @@ class LogsNoncommercialUnknownRegionAuthorizationIntegrationTest {
"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);
String caller = LogsAuthorizationFixtures.user(cleanup, port, ACCOUNT, "logs-partition-", "streams",
allow + statement);
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),
@@ -125,10 +119,6 @@ class LogsNoncommercialUnknownRegionAuthorizationIntegrationTest {
}
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("/");
return LogsAuthorizationFixtures.call(endpoint.getPort(), caller, region, action, body);
}
}
@@ -3,6 +3,7 @@ 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.LogsAuthorizationFixtures;
import io.github.hectorvent.floci.testing.PartitionCleanup;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.common.http.TestHTTPResource;
@@ -19,7 +20,6 @@ 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;
@@ -399,15 +399,8 @@ class LogsStreamAuthorizationIntegrationTest {
}
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();
return LogsAuthorizationFixtures.user(cleanup, endpoint.getPort(), account, "logs-stream-auth-",
"streams", statements);
}
private static String policy(String effect, String action, String resource, String condition) {
@@ -443,11 +436,6 @@ class LogsStreamAuthorizationIntegrationTest {
}
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();
return LogsAuthorizationFixtures.call(port, caller, region, action, body).then();
}
}
@@ -1,7 +1,7 @@
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.LogsAuthorizationFixtures;
import io.github.hectorvent.floci.testing.PartitionCleanup;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.common.http.TestHTTPResource;
@@ -14,13 +14,14 @@ 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.List;
import java.util.Map;
import java.util.UUID;
import static io.restassured.RestAssured.given;
import static io.github.hectorvent.floci.testing.LogsAuthorizationFixtures.expectedTags;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertAll;
@@ -55,6 +56,33 @@ class LogsUnknownRegionAuthorizationIntegrationTest {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@ParameterizedTest
@ValueSource(strings = {"TagResource", "UntagResource", "ListTagsForResource"})
void directTagArnRejectsColonRegionAndAcceptsUnambiguousUnknownRegion(String action) {
String region = "custom-region-1";
String group = "logs-tag-marker-" + UUID.randomUUID();
int port = endpoint.getPort();
cleanup.register(() -> call(port, ACCOUNT, region, "DeleteLogGroup", Map.of("logGroupName", group))
.then().statusCode(200));
logsService.createLogGroupForAccount(ACCOUNT, group, null, Map.of("team", "blue"), region);
String caller = LogsAuthorizationFixtures.user(cleanup, port, ACCOUNT, "logs-tag-marker-", "marker-tags",
"{\"Effect\":\"Allow\",\"Action\":\"logs:" + action + "\",\"Resource\":\"*\"}");
Map<String, Object> body = LogsAuthorizationFixtures.tagBody(action,
"arn:aws:logs:" + region + ":" + ACCOUNT + ":log-group:" + group);
call(port, caller, "custom:log-group:zone", action, body)
.then().statusCode(400).body(containsString("InvalidSignatureException"));
call(port, ACCOUNT, region, "ListTagsLogGroup", Map.of("logGroupName", group))
.then().statusCode(200).body("tags", equalTo(Map.of("team", "blue")));
Response result = call(port, caller, region, action, body);
result.then().statusCode(200);
Map<String, String> expected = expectedTags(action);
if (action.equals("ListTagsForResource")) {
result.then().body("tags", equalTo(expected));
}
call(port, ACCOUNT, region, "ListTagsLogGroup", Map.of("logGroupName", group))
.then().statusCode(200).body("tags", equalTo(expected));
}
@ParameterizedTest
@CsvSource({"CreateLogStream,false,false", "DeleteLogStream,false,false", "CreateLogStream,true,false",
"DeleteLogStream,true,false", "DeleteLogStream,false,true"})
@@ -81,18 +109,13 @@ class LogsUnknownRegionAuthorizationIntegrationTest {
}
}
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);
String caller = LogsAuthorizationFixtures.user(cleanup, port, ACCOUNT, "logs-region-alias-", "streams",
"{\"Effect\":\"Allow\",\"Action\":\"logs:" + action
+ "\",\"Resource\":" + resources + condition + "}");
Response response = call(port, caller, aliasRegion, action,
Map.of("logGroupName", group, "logStreamName", "s"));
assertAll("preserve stored identity across an opted-in region delimiter",
@@ -130,19 +153,14 @@ class LogsUnknownRegionAuthorizationIntegrationTest {
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);
String caller = LogsAuthorizationFixtures.user(cleanup, port, ACCOUNT, "logs-resource-owner-", "streams",
grant + statement);
boolean allowed = effect.equals("Allow") == actualOwner;
Response response = call(port, caller, region, action, Map.of("logGroupName", group, "logStreamName", "s"));
assertAll("use the owning account in an admitted custom region",
@@ -171,9 +189,6 @@ class LogsUnknownRegionAuthorizationIntegrationTest {
}
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("/");
return LogsAuthorizationFixtures.call(port, caller, region, action, body);
}
}
@@ -660,10 +660,23 @@ class ResourceArnBuilderTest {
}
}
@Test
void directLogTagTargetsUseBareArnAndServingAction() {
for (String action : List.of("logs:TagResource", "logs:UntagResource", "logs:ListTagsForResource")) {
for (String region : List.of("us-east-1", "cn-north-1", "us-gov-west-1", "custom-region-1")) {
String partition = AwsRegions.partitionFor(region);
String arn = "arn:" + partition + ":logs:" + region + ":" + ACCOUNT + ":log-group:group";
setJsonBody("{\"resourceArn\":\"" + arn + "\"}");
when(ctx.getHeaderString("X-Amz-Target")).thenReturn("Logs_20140328.DeleteLogGroup");
assertEquals(List.of(arn), builder.buildLogStreamResources(action, ctx, region, ACCOUNT, partition));
}
}
}
@Test
void excludedLogsActionsKeepWildcardResources() {
for (String action : List.of("logs:CreateLogGroup", "logs:DeleteLogGroup", "logs:PutRetentionPolicy",
"logs:TagResource", "logs:UntagResource", "logs:ListTagsForResource", "logs:PutLogEvents")) {
"logs:DeleteRetentionPolicy", "logs:PutLogEvents")) {
setJsonBody("{\"logGroupName\":\"group\",\"logStreamName\":\"stream\"}");
assertEquals(List.of("*"), builder.buildLogStreamResources(action, ctx, "us-east-1", ACCOUNT, "aws"));
}
@@ -0,0 +1,56 @@
package io.github.hectorvent.floci.testing;
import io.restassured.response.Response;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import static io.restassured.RestAssured.given;
/** Shared signed requests and IAM users for Logs authorization tests. */
public final class LogsAuthorizationFixtures {
private LogsAuthorizationFixtures() {
}
public static String user(PartitionCleanup cleanup, int port, String account, String prefix,
String policyName, String statements) {
String name = prefix + UUID.randomUUID();
String root = EnforcementFixtures.auth(account, "iam");
String key = EnforcementFixtures.createUser(cleanup, port, root, name).accessKeyId();
given().port(port).header("Authorization", root).formParam("Action", "PutUserPolicy")
.formParam("UserName", name).formParam("PolicyName", policyName)
.formParam("PolicyDocument", "{\"Version\":\"2012-10-17\",\"Statement\":[" + statements + "]}")
.when().post("/").then().statusCode(200);
return key;
}
public static Response 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("/");
}
public static Map<String, Object> tagBody(String action, String resource) {
Map<String, Object> body = new LinkedHashMap<>();
body.put("resourceArn", resource);
if (action.equals("TagResource")) {
body.put("tags", Map.of("team", "red"));
} else if (action.equals("UntagResource")) {
body.put("tagKeys", List.of("team"));
}
return body;
}
public static Map<String, String> expectedTags(String action) {
return switch (action) {
case "TagResource" -> Map.of("team", "red");
case "UntagResource" -> Map.of();
case "ListTagsForResource" -> Map.of("team", "blue");
default -> throw new IllegalArgumentException(action);
};
}
}