mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 14:10:49 -05:00
feat(cloudwatch-logs): add resource policy actions (#2574)
This commit is contained in:
1 parent
247bd590bc
commit
859db2f352
5 files changed
+130
-1
No files matched your search
@@ -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 |
|
||||
|
||||
+36
@@ -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();
|
||||
|
||||
+46
-1
@@ -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<String, LogStream> streamStore;
|
||||
private final StorageBackend<String, LogEvent> eventStore;
|
||||
private final StorageBackend<String, SubscriptionFilter> subscriptionFilterStore;
|
||||
private final StorageBackend<String, ResourcePolicy> 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<String, SubscriptionFilter> 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<String, LogGroup> groupStore,
|
||||
StorageBackend<String, LogStream> streamStore,
|
||||
StorageBackend<String, LogEvent> eventStore,
|
||||
StorageBackend<String, SubscriptionFilter> subscriptionFilterStore,
|
||||
StorageBackend<String, ResourcePolicy> 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<ResourcePolicy> describeResourcePolicies(String region) {
|
||||
List<ResourcePolicy> 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;
|
||||
|
||||
+24
@@ -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; }
|
||||
}
|
||||
+22
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user