diff --git a/docs/services/cloudwatch.md b/docs/services/cloudwatch.md index 09b1d5bb3f..0ee7b457fd 100644 --- a/docs/services/cloudwatch.md +++ b/docs/services/cloudwatch.md @@ -34,6 +34,8 @@ Floci supports both CloudWatch Logs and CloudWatch Metrics. | `PutSubscriptionFilter` | Create or update a subscription filter (stored only, see note below) | | `DescribeSubscriptionFilters` | List subscription filters on a log group | | `DeleteSubscriptionFilter` | Delete a subscription filter | +| `PutResourcePolicy` | Create or update an account-level resource policy | +| `DescribeResourcePolicies` | List account-level resource policies | | `GetDataProtectionPolicy` | Return the resolved log group identifier (see note below) | | `StartQuery` | Start a Logs Insights query (see [Logs Insights](#logs-insights)) | | `GetQueryResults` | Get the status and results of a Logs Insights query | diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsHandler.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsHandler.java index ed3f900978..6136b61668 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsHandler.java @@ -5,6 +5,7 @@ import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.services.cloudwatch.logs.model.LogEvent; import io.github.hectorvent.floci.services.cloudwatch.logs.model.LogGroup; import io.github.hectorvent.floci.services.cloudwatch.logs.model.LogStream; +import io.github.hectorvent.floci.services.cloudwatch.logs.model.ResourcePolicy; import io.github.hectorvent.floci.services.cloudwatch.logs.model.SubscriptionFilter; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; @@ -55,6 +56,8 @@ public class CloudWatchLogsHandler { case "PutSubscriptionFilter" -> handlePutSubscriptionFilter(request, region); case "DescribeSubscriptionFilters" -> handleDescribeSubscriptionFilters(request, region); case "DeleteSubscriptionFilter" -> handleDeleteSubscriptionFilter(request, region); + case "PutResourcePolicy" -> handlePutResourcePolicy(request, region); + case "DescribeResourcePolicies" -> handleDescribeResourcePolicies(region); case "GetDataProtectionPolicy" -> handleGetDataProtectionPolicy(request, region); case "StartQuery" -> handleStartQuery(request, region); case "GetQueryResults" -> handleGetQueryResults(request, region); @@ -118,6 +121,39 @@ public class CloudWatchLogsHandler { return Response.ok(response).build(); } + private Response handlePutResourcePolicy(JsonNode request, String region) { + String policyName = request.path("policyName").asText(null); + if (policyName == null || policyName.isBlank()) { + throw new AwsException("InvalidParameterException", "policyName is required.", 400); + } + String policyDocument = request.path("policyDocument").asText(null); + if (policyDocument == null || policyDocument.isBlank()) { + throw new AwsException("InvalidParameterException", "policyDocument is required.", 400); + } + + ObjectNode response = objectMapper.createObjectNode(); + response.set("resourcePolicy", buildResourcePolicy( + logsService.putResourcePolicy(policyName, policyDocument, region))); + return Response.ok(response).build(); + } + + private Response handleDescribeResourcePolicies(String region) { + ArrayNode policies = objectMapper.createArrayNode(); + logsService.describeResourcePolicies(region).forEach( + policy -> policies.add(buildResourcePolicy(policy))); + ObjectNode response = objectMapper.createObjectNode(); + response.set("resourcePolicies", policies); + return Response.ok(response).build(); + } + + private ObjectNode buildResourcePolicy(ResourcePolicy policy) { + ObjectNode node = objectMapper.createObjectNode(); + node.put("policyName", policy.getPolicyName()); + node.put("policyDocument", policy.getPolicyDocument()); + node.put("lastUpdatedTime", policy.getLastUpdatedTime()); + return node; + } + private Response handleCreateLogStream(JsonNode request, String region) { String groupName = request.path("logGroupName").asText(); String streamName = request.path("logStreamName").asText(); diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsService.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsService.java index e18e8d215e..0cf95999e3 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsService.java @@ -4,11 +4,13 @@ import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; +import io.github.hectorvent.floci.core.storage.InMemoryStorage; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; import io.github.hectorvent.floci.services.cloudwatch.logs.model.LogEvent; import io.github.hectorvent.floci.services.cloudwatch.logs.model.LogGroup; import io.github.hectorvent.floci.services.cloudwatch.logs.model.LogStream; +import io.github.hectorvent.floci.services.cloudwatch.logs.model.ResourcePolicy; import io.github.hectorvent.floci.services.cloudwatch.logs.model.SubscriptionFilter; import com.fasterxml.jackson.core.type.TypeReference; import jakarta.enterprise.context.ApplicationScoped; @@ -55,6 +57,7 @@ public class CloudWatchLogsService implements ResourceProvider { private final StorageBackend streamStore; private final StorageBackend eventStore; private final StorageBackend subscriptionFilterStore; + private final StorageBackend resourcePolicyStore; private final RegionResolver regionResolver; private final int maxEventsPerQuery; /** @@ -99,6 +102,8 @@ public class CloudWatchLogsService implements ResourceProvider { new TypeReference<>() {}), storageFactory.create("cloudwatchlogs", "cwlogs-subscription-filters.json", new TypeReference<>() {}), + storageFactory.create("cloudwatchlogs", "cwlogs-resource-policies.json", + new TypeReference<>() {}), config.services().cloudwatchlogs().maxEventsPerQuery(), regionResolver, config.services().cloudwatchlogs().queryCompletionDelayMs(), @@ -112,7 +117,7 @@ public class CloudWatchLogsService implements ResourceProvider { StorageBackend subscriptionFilterStore, int maxEventsPerQuery, RegionResolver regionResolver) { - this(groupStore, streamStore, eventStore, subscriptionFilterStore, + this(groupStore, streamStore, eventStore, subscriptionFilterStore, new InMemoryStorage<>(), maxEventsPerQuery, regionResolver, 0L, System::currentTimeMillis); } @@ -124,10 +129,24 @@ public class CloudWatchLogsService implements ResourceProvider { RegionResolver regionResolver, long queryCompletionDelayMs, LongSupplier clock) { + this(groupStore, streamStore, eventStore, subscriptionFilterStore, new InMemoryStorage<>(), + maxEventsPerQuery, regionResolver, queryCompletionDelayMs, clock); + } + + CloudWatchLogsService(StorageBackend groupStore, + StorageBackend streamStore, + StorageBackend eventStore, + StorageBackend subscriptionFilterStore, + StorageBackend resourcePolicyStore, + int maxEventsPerQuery, + RegionResolver regionResolver, + long queryCompletionDelayMs, + LongSupplier clock) { this.groupStore = groupStore; this.streamStore = streamStore; this.eventStore = eventStore; this.subscriptionFilterStore = subscriptionFilterStore; + this.resourcePolicyStore = resourcePolicyStore; this.maxEventsPerQuery = maxEventsPerQuery; this.regionResolver = regionResolver; long maxSequence = eventStore.scan(k -> true).stream() @@ -896,6 +915,24 @@ public class CloudWatchLogsService implements ResourceProvider { LOG.infov("Deleted subscription filter: {0} on log group: {1}", filterName, logGroupName); } + // ──────────────────────────── Resource Policies ──────────────────────────── + + public ResourcePolicy putResourcePolicy(String policyName, String policyDocument, String region) { + ResourcePolicy policy = new ResourcePolicy(); + policy.setPolicyName(policyName); + policy.setPolicyDocument(policyDocument); + policy.setLastUpdatedTime(System.currentTimeMillis()); + resourcePolicyStore.put(resourcePolicyKey(region, policyName), policy); + return policy; + } + + public List describeResourcePolicies(String region) { + List policies = resourcePolicyStore.scan( + key -> key.startsWith(resourcePolicyKeyPrefix(region))); + policies.sort(Comparator.comparing(ResourcePolicy::getPolicyName)); + return policies; + } + // ──────────────────────────── Helpers ──────────────────────────── private void deleteEventsForStream(String region, String groupName, String streamName) { @@ -959,6 +996,14 @@ public class CloudWatchLogsService implements ResourceProvider { return region + "::" + logGroupName + "::filter::" + filterName; } + private static String resourcePolicyKeyPrefix(String region) { + return region + "::policy::"; + } + + private static String resourcePolicyKey(String region, String policyName) { + return resourcePolicyKeyPrefix(region) + policyName; + } + private static long toLong(Object value, long defaultValue) { if (value == null) { return defaultValue; diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/model/ResourcePolicy.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/model/ResourcePolicy.java new file mode 100644 index 0000000000..ebdd6951b0 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/model/ResourcePolicy.java @@ -0,0 +1,24 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs.model; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import io.quarkus.runtime.annotations.RegisterForReflection; + +@RegisterForReflection +@JsonIgnoreProperties(ignoreUnknown = true) +public class ResourcePolicy { + + private String policyName; + private String policyDocument; + private long lastUpdatedTime; + + public ResourcePolicy() {} + + public String getPolicyName() { return policyName; } + public void setPolicyName(String policyName) { this.policyName = policyName; } + + public String getPolicyDocument() { return policyDocument; } + public void setPolicyDocument(String policyDocument) { this.policyDocument = policyDocument; } + + public long getLastUpdatedTime() { return lastUpdatedTime; } + public void setLastUpdatedTime(long lastUpdatedTime) { this.lastUpdatedTime = lastUpdatedTime; } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsHandlerTest.java index ed4af602e9..f5cbe71877 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsHandlerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsHandlerTest.java @@ -145,6 +145,28 @@ class CloudWatchLogsHandlerTest { assertTrue(group.path("deletionProtectionEnabled").asBoolean()); } + @Test + void putResourcePolicyIsReturnedByDescribeResourcePolicies() { + String document = "{\"Version\":\"2012-10-17\",\"Statement\":[]}"; + ObjectNode put = MAPPER.createObjectNode(); + put.put("policyName", "delivery-policy"); + put.put("policyDocument", document); + + JsonNode created = (JsonNode) handler.handle("PutResourcePolicy", put, REGION).getEntity(); + + assertEquals("delivery-policy", created.path("resourcePolicy").path("policyName").asText()); + assertEquals(document, created.path("resourcePolicy").path("policyDocument").asText()); + assertTrue(created.path("resourcePolicy").path("lastUpdatedTime").asLong() > 0); + + JsonNode described = (JsonNode) handler.handle( + "DescribeResourcePolicies", MAPPER.createObjectNode(), REGION).getEntity(); + assertEquals(1, described.path("resourcePolicies").size()); + assertEquals("delivery-policy", + described.path("resourcePolicies").get(0).path("policyName").asText()); + assertEquals(document, + described.path("resourcePolicies").get(0).path("policyDocument").asText()); + } + @Test void putLogGroupDeletionProtectionPersistsByName() { ObjectNode request = MAPPER.createObjectNode();