feat(cloudwatchlogs): add cross-account policy operations (#3089)

* feat(cloudwatchlogs): add cross-account policy operations

* fix(cloudwatchlogs): align cross-account policy contracts

* fix(cloudwatchlogs): enforce account policy quotas

* fix(cloudwatchlogs): validate field index policy scopes

* fix(cloudwatchlogs): parse field index criteria safely

* fix(cloudwatchlogs): align field index quota parsing
This commit is contained in:
Franklin G. Mendoza authored and GitHub committed 2026-09-05 21:08:07 -04:00
1 parent 1aa981fef2
commit 6a728f1b9f
10 files changed
+758 -3

No files matched your search

@@ -0,0 +1,59 @@
package io.github.hectorvent.floci.services.cloudwatch.logs;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
@QuarkusTest
class CloudWatchLogsCrossAccountIntegrationTest {
private static final String CONTENT_TYPE = "application/x-amz-json-1.1";
private static final String AUTH = "AWS4-HMAC-SHA256 Credential=AKID/20260904/us-east-1/logs/aws4_request";
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@Test
void destinationAndAccountPolicyLifecycle() {
json("Logs_20140328.PutDestination",
"{\"destinationName\":\"cross-account\",\"targetArn\":\"arn:aws:kinesis:us-east-1:000000000000:stream/logs\",\"roleArn\":\"arn:aws:iam::000000000000:role/logs\"}")
.then().statusCode(200).body("destination.destinationName", equalTo("cross-account"));
json("Logs_20140328.PutDestinationPolicy",
"{\"destinationName\":\"cross-account\",\"accessPolicy\":\"{}\"}")
.then().statusCode(200);
json("Logs_20140328.PutAccountPolicy",
"{\"policyName\":\"central-subscription\",\"policyDocument\":\"{}\",\"policyType\":\"SUBSCRIPTION_FILTER_POLICY\",\"scope\":\"ALL\"}")
.then().statusCode(200).body("accountPolicy.policyName", equalTo("central-subscription"));
json("Logs_20140328.DescribeAccountPolicies",
"{\"policyType\":\"SUBSCRIPTION_FILTER_POLICY\"}")
.then().statusCode(200).body("accountPolicies", hasSize(1));
}
@Test
void transformerPolicyAcceptsProcessorArrayDocument() {
json("Logs_20140328.PutAccountPolicy",
"{\"policyName\":\"account-transformer\",\"policyDocument\":\"[{\\\"parseJSON\\\":{}}]\","
+ "\"policyType\":\"TRANSFORMER_POLICY\",\"scope\":\"ALL\"}")
.then().statusCode(200)
.body("accountPolicy.policyType", equalTo("TRANSFORMER_POLICY"))
.body("accountPolicy.policyDocument", equalTo("[{\"parseJSON\":{}}]"));
}
@Test
void destinationPolicyRequiresExistingDestination() {
json("Logs_20140328.PutDestinationPolicy",
"{\"destinationName\":\"missing\",\"accessPolicy\":\"{}\"}")
.then().statusCode(400).body("__type", equalTo("InvalidParameterException"));
}
private static io.restassured.response.Response json(String target, String body) {
return given().contentType(CONTENT_TYPE).header("Authorization", AUTH).header("X-Amz-Target", target)
.body(body).post("/");
}
}
@@ -0,0 +1,125 @@
package io.github.hectorvent.floci.services.cloudwatch.logs;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
class CloudWatchLogsCrossAccountServiceTest {
private static final String REGION = "us-east-1";
private static final String DOCUMENT = "{}";
private final CloudWatchLogsCrossAccountService service = new CloudWatchLogsCrossAccountService(
new InMemoryStorage<>(), new InMemoryStorage<>(),
new RegionResolver(REGION, "000000000000"), new ObjectMapper());
@Test
void transformerGlobalAndScopedPoliciesCannotCoexist() {
service.putAccountPolicy("global", "[{\"parseJSON\":{}}]", "TRANSFORMER_POLICY", null, "ALL", REGION);
AwsException error = assertThrows(AwsException.class,
() -> service.putAccountPolicy("scoped", "[{\"parseJSON\":{}}]", "TRANSFORMER_POLICY",
"LogGroupNamePrefix = 'aws/'", "ALL", REGION));
assertEquals("LimitExceededException", error.getErrorCode());
}
@Test
void updatingScopedTransformerToGlobalRevalidatesQuota() {
service.putAccountPolicy("first", "[{\"parseJSON\":{}}]", "TRANSFORMER_POLICY",
"LogGroupNamePrefix = 'aws/'", "ALL", REGION);
service.putAccountPolicy("second", "[{\"parseJSON\":{}}]", "TRANSFORMER_POLICY",
"LogGroupNamePrefix = 'app/'", "ALL", REGION);
AwsException error = assertThrows(AwsException.class,
() -> service.putAccountPolicy("first", "[{\"parseJSON\":{}}]", "TRANSFORMER_POLICY",
null, "ALL", REGION));
assertEquals("LimitExceededException", error.getErrorCode());
}
@Test
void metricExtractionRejectsMalformedSelectionCriteria() {
AwsException error = assertThrows(AwsException.class,
() -> service.putAccountPolicy("metrics", DOCUMENT, "METRIC_EXTRACTION_POLICY",
"not a criterion", "ALL", REGION));
assertEquals("InvalidParameterException", error.getErrorCode());
}
@Test
void metricExtractionAcceptsDocumentedSelectionCriteria() {
service.putAccountPolicy("metrics", DOCUMENT, "METRIC_EXTRACTION_POLICY",
"LogGroupNamePrefix IN [\"/aws/lambda/\",\"/app/\"]", "ALL", REGION);
assertEquals(1, service.describeAccountPolicies("METRIC_EXTRACTION_POLICY", null, REGION).size());
}
@Test
void fieldIndexRejectsMixedScopeForms() {
AwsException error = assertThrows(AwsException.class,
() -> service.putAccountPolicy("mixed-index", DOCUMENT, "FIELD_INDEX_POLICY",
"LogGroupNamePrefix = 'aws/' AND DataSourceName = 'amazon_vpc' AND DataSourceType = 'flow'",
"ALL", REGION));
assertEquals("InvalidParameterException", error.getErrorCode());
}
@Test
void fieldIndexRequiresCompleteDataSourcePair() {
AwsException error = assertThrows(AwsException.class,
() -> service.putAccountPolicy("partial-index", DOCUMENT, "FIELD_INDEX_POLICY",
"DataSourceName = 'amazon_vpc'", "ALL", REGION));
assertEquals("InvalidParameterException", error.getErrorCode());
}
@Test
void fieldIndexIgnoresReservedFieldNamesInsidePrefixValue() {
service.putAccountPolicy("prefix-index", DOCUMENT, "FIELD_INDEX_POLICY",
"LogGroupNamePrefix = \"/DataSourceName/DataSourceType/\"", "ALL", REGION);
assertEquals(1, service.describeAccountPolicies("FIELD_INDEX_POLICY", null, REGION).size());
}
@Test
void fieldIndexIgnoresReservedFieldNamesInsideDataSourceValues() {
service.putAccountPolicy("data-source-index", DOCUMENT, "FIELD_INDEX_POLICY",
"DataSourceName = \"LogGroupNamePrefix\" AND DataSourceType = \"flow\"", "ALL", REGION);
assertEquals(1, service.describeAccountPolicies("FIELD_INDEX_POLICY", null, REGION).size());
}
@Test
void quotedPrefixStillConflictsWithGlobalFieldIndexPolicy() {
service.putAccountPolicy("global-index", DOCUMENT, "FIELD_INDEX_POLICY", null, "ALL", REGION);
AwsException error = assertThrows(AwsException.class,
() -> service.putAccountPolicy("prefix-index", DOCUMENT, "FIELD_INDEX_POLICY",
"LogGroupNamePrefix = \"/DataSourceName/DataSourceType/\"", "ALL", REGION));
assertEquals("LimitExceededException", error.getErrorCode());
}
@Test
void metricExtractionRejectsMoreThanFiftySelectionValues() {
StringBuilder criteria = new StringBuilder("LogGroupName IN [");
for (int i = 0; i < 51; i++) {
if (i > 0) {
criteria.append(',');
}
criteria.append('"').append("group-").append(i).append('"');
}
criteria.append(']');
AwsException error = assertThrows(AwsException.class,
() -> service.putAccountPolicy("metrics", DOCUMENT, "METRIC_EXTRACTION_POLICY",
criteria.toString(), "ALL", REGION));
assertEquals("InvalidParameterException", error.getErrorCode());
}
}
@@ -53,7 +53,12 @@ class CloudWatchLogsHandlerTest {
10_000,
new RegionResolver(REGION, ACCOUNT)
);
handler = new CloudWatchLogsHandler(service, MAPPER);
handler = new CloudWatchLogsHandler(
service,
new CloudWatchLogsCrossAccountService(
new InMemoryStorage<>(), new InMemoryStorage<>(),
new RegionResolver(REGION, ACCOUNT), MAPPER),
MAPPER);
service.createLogGroup(GROUP, null, null, REGION);
service.createLogStream(GROUP, STREAM, REGION);
@@ -38,7 +38,12 @@ class CloudWatchLogsInputConstraintTest {
10_000,
new RegionResolver(REGION, ACCOUNT)
);
handler = new CloudWatchLogsHandler(service, MAPPER);
handler = new CloudWatchLogsHandler(
service,
new CloudWatchLogsCrossAccountService(
new InMemoryStorage<>(), new InMemoryStorage<>(),
new RegionResolver(REGION, ACCOUNT), MAPPER),
MAPPER);
service.createLogGroup(GROUP, null, null, REGION);
service.createLogStream(GROUP, "s1", REGION);
}