Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

feat(logs): support log group deletion protection (#2256)

Saatvik Arya4215006fe29c
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudWatchLogsDeletionProtectionTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudWatchLogsDeletionProtectionTest.java
new file mode 100644
index 0000000000..d8e65cddb7
--- /dev/null
+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudWatchLogsDeletionProtectionTest.java
@@ -0,0 +1,97 @@
+package com.floci.test;
+
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
+import software.amazon.awssdk.services.cloudwatchlogs.model.LogGroup;
+import software.amazon.awssdk.services.cloudwatchlogs.model.ValidationException;
+
+import java.util.Optional;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+@DisplayName("CloudWatch Logs deletion protection")
+class CloudWatchLogsDeletionProtectionTest {
+
+ @Test
+ @DisplayName("defaults off and can be enabled and disabled by name")
+ void defaultsOffAndUpdatesByName() {
+ String groupName = "/test/" + TestFixtures.uniqueName("logs-deletion-protection-default");
+ try (CloudWatchLogsClient logs = TestFixtures.cloudWatchLogsClient()) {
+ try {
+ logs.createLogGroup(request -> request.logGroupName(groupName));
+ assertThat(findLogGroup(logs, groupName))
+ .get()
+ .extracting(LogGroup::deletionProtectionEnabled)
+ .isEqualTo(false);
+
+ logs.putLogGroupDeletionProtection(request -> request
+ .logGroupIdentifier(groupName)
+ .deletionProtectionEnabled(true));
+ assertThat(findLogGroup(logs, groupName))
+ .get()
+ .extracting(LogGroup::deletionProtectionEnabled)
+ .isEqualTo(true);
+
+ logs.putLogGroupDeletionProtection(request -> request
+ .logGroupIdentifier(groupName)
+ .deletionProtectionEnabled(false));
+ assertThat(findLogGroup(logs, groupName))
+ .get()
+ .extracting(LogGroup::deletionProtectionEnabled)
+ .isEqualTo(false);
+ } finally {
+ deleteIfPresent(logs, groupName);
+ }
+ }
+ }
+
+ @Test
+ @DisplayName("create-time protection blocks deletion until disabled by ARN")
+ void createTimeProtectionBlocksDeletionUntilDisabledByArn() {
+ String groupName = "/test/" + TestFixtures.uniqueName("logs-deletion-protection-create");
+ try (CloudWatchLogsClient logs = TestFixtures.cloudWatchLogsClient()) {
+ try {
+ logs.createLogGroup(request -> request
+ .logGroupName(groupName)
+ .deletionProtectionEnabled(true));
+ LogGroup group = findLogGroup(logs, groupName).orElseThrow();
+ assertThat(group.deletionProtectionEnabled()).isTrue();
+
+ assertThatThrownBy(() -> logs.deleteLogGroup(request -> request.logGroupName(groupName)))
+ .isInstanceOfSatisfying(ValidationException.class, error -> {
+ assertThat(error.awsErrorDetails().errorCode()).isEqualTo("ValidationException");
+ assertThat(error.getMessage()).containsIgnoringCase("deletion protection");
+ });
+
+ logs.putLogGroupDeletionProtection(request -> request
+ .logGroupIdentifier(group.arn())
+ .deletionProtectionEnabled(false));
+ logs.deleteLogGroup(request -> request.logGroupName(groupName));
+
+ assertThat(findLogGroup(logs, groupName)).isEmpty();
+ } finally {
+ deleteIfPresent(logs, groupName);
+ }
+ }
+ }
+
+ private static Optional<LogGroup> findLogGroup(CloudWatchLogsClient logs, String groupName) {
+ return logs.describeLogGroups(request -> request.logGroupNamePrefix(groupName))
+ .logGroups()
+ .stream()
+ .filter(group -> groupName.equals(group.logGroupName()))
+ .findFirst();
+ }
+
+ private static void deleteIfPresent(CloudWatchLogsClient logs, String groupName) {
+ if (findLogGroup(logs, groupName).isEmpty()) {
+ return;
+ }
+ logs.putLogGroupDeletionProtection(request -> request
+ .logGroupIdentifier(groupName)
+ .deletionProtectionEnabled(false));
+ logs.deleteLogGroup(request -> request.logGroupName(groupName));
+ }
+}
diff --git a/docs/services/cloudwatch.md b/docs/services/cloudwatch.md
index 1abe72902e..e6126391bf 100644
--- a/docs/services/cloudwatch.md
+++ b/docs/services/cloudwatch.md
@@ -15,6 +15,7 @@ Floci supports both CloudWatch Logs and CloudWatch Metrics.
|---|---|
| `CreateLogGroup` | Create a log group |
| `DeleteLogGroup` | Delete a log group |
+| `PutLogGroupDeletionProtection` | Enable or disable deletion protection for a log group by name or ARN |
| `DescribeLogGroups` | List log groups |
| `CreateLogStream` | Create a log stream inside a log group |
| `DeleteLogStream` | Delete a log stream |
@@ -38,6 +39,10 @@ Floci supports both CloudWatch Logs and CloudWatch Metrics.
| `GetQueryResults` | Get the status and results of a Logs Insights query |
| `StopQuery` | Stop a query that has not completed yet |
+Log group deletion protection defaults to disabled and is persisted with the log group. When it is
+enabled, `DeleteLogGroup` returns `ValidationException` until protection is explicitly disabled
+with `PutLogGroupDeletionProtection`.
+
Two actions are currently simplified:
- **`PutSubscriptionFilter`** stores the filter so that `DescribeSubscriptionFilters`
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 c37b0f2b21..83837ffa2c 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
@@ -45,6 +45,7 @@ public class CloudWatchLogsHandler {
case "FilterLogEvents" -> handleFilterLogEvents(request, region);
case "PutRetentionPolicy" -> handlePutRetentionPolicy(request, region);
case "DeleteRetentionPolicy" -> handleDeleteRetentionPolicy(request, region);
+ case "PutLogGroupDeletionProtection" -> handlePutLogGroupDeletionProtection(request, region);
case "TagLogGroup" -> handleTagLogGroup(request, region);
case "UntagLogGroup" -> handleUntagLogGroup(request, region);
case "ListTagsLogGroup" -> handleListTagsLogGroup(request, region);
@@ -68,8 +69,9 @@ public class CloudWatchLogsHandler {
String name = request.path("logGroupName").asText();
Integer retentionInDays = request.has("retentionInDays")
? request.path("retentionInDays").asInt() : null;
+ boolean deletionProtectionEnabled = request.path("deletionProtectionEnabled").asBoolean(false);
Map<String, String> tags = extractTags(request.path("tags"));
- logsService.createLogGroup(name, retentionInDays, tags, region);
+ logsService.createLogGroup(name, retentionInDays, tags, deletionProtectionEnabled, region);
return Response.ok(objectMapper.createObjectNode()).build();
}
@@ -79,6 +81,20 @@ public class CloudWatchLogsHandler {
return Response.ok(objectMapper.createObjectNode()).build();
}
+ private Response handlePutLogGroupDeletionProtection(JsonNode request, String region) {
+ String identifier = request.path("logGroupIdentifier").asText(null);
+ if (identifier == null || identifier.isBlank()) {
+ throw new AwsException("InvalidParameterException", "logGroupIdentifier is required.", 400);
+ }
+ JsonNode enabled = request.get("deletionProtectionEnabled");
+ if (enabled == null || !enabled.isBoolean()) {
+ throw new AwsException("InvalidParameterException", "deletionProtectionEnabled is required.", 400);
+ }
+ String groupName = extractLogGroupNameFromArn(identifier);
+ logsService.putLogGroupDeletionProtection(groupName, enabled.booleanValue(), region);
+ return Response.ok(objectMapper.createObjectNode()).build();
+ }
+
private Response handleDescribeLogGroups(JsonNode request, String region) {
String prefix = request.path("logGroupNamePrefix").asText(null);
List<LogGroup> groups = logsService.describeLogGroups(prefix, region);
@@ -93,6 +109,7 @@ public class CloudWatchLogsHandler {
if (g.getRetentionInDays() != null) {
node.put("retentionInDays", g.getRetentionInDays());
}
+ node.put("deletionProtectionEnabled", g.isDeletionProtectionEnabled());
node.put("storedBytes", 0);
node.put("metricFilterCount", 0);
groupsArray.add(node);
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 8416fc0be2..e703a342f6 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
@@ -126,6 +126,11 @@ public class CloudWatchLogsService {
// ──────────────────────────── Log Groups ────────────────────────────
public void createLogGroup(String name, Integer retentionInDays, Map<String, String> tags, String region) {
+ createLogGroup(name, retentionInDays, tags, false, region);
+ }
+
+ public void createLogGroup(String name, Integer retentionInDays, Map<String, String> tags,
+ boolean deletionProtectionEnabled, String region) {
if (name == null || name.isBlank()) {
throw new AwsException("InvalidParameterException", "logGroupName is required.", 400);
}
@@ -138,6 +143,7 @@ public class CloudWatchLogsService {
group.setLogGroupName(name);
group.setCreatedTime(System.currentTimeMillis());
group.setRetentionInDays(retentionInDays);
+ group.setDeletionProtectionEnabled(deletionProtectionEnabled);
if (tags != null) {
group.setTags(new HashMap<>(tags));
}
@@ -147,9 +153,15 @@ public class CloudWatchLogsService {
public void deleteLogGroup(String name, String region) {
String key = groupKey(region, name);
- groupStore.get(key)
+ LogGroup group = groupStore.get(key)
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"The specified log group does not exist: " + name, 400));
+ if (group.isDeletionProtectionEnabled()) {
+ throw new AwsException("ValidationException",
+ "The specified log group has deletion protection enabled. "
+ + "Disable deletion protection before deleting the log group.",
+ 400);
+ }
// Cascade: delete all streams and events for this group
String streamPrefix = streamKeyPrefix(region, name);
@@ -187,6 +199,15 @@ public class CloudWatchLogsService {
return result;
}
+ public void putLogGroupDeletionProtection(String groupName, boolean enabled, String region) {
+ String key = groupKey(region, groupName);
+ LogGroup group = groupStore.get(key)
+ .orElseThrow(() -> new AwsException("ResourceNotFoundException",
+ "The specified log group does not exist: " + groupName, 400));
+ group.setDeletionProtectionEnabled(enabled);
+ groupStore.put(key, group);
+ }
+
public void putRetentionPolicy(String groupName, int days, String region) {
String key = groupKey(region, groupName);
LogGroup group = groupStore.get(key)
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/model/LogGroup.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/model/LogGroup.java
index 930e1a6d23..eae852a095 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/model/LogGroup.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/model/LogGroup.java
@@ -13,6 +13,7 @@ public class LogGroup {
private String logGroupName;
private long createdTime;
private Integer retentionInDays;
+ private boolean deletionProtectionEnabled;
private Map<String, String> tags = new HashMap<>();
public LogGroup() {}
@@ -26,6 +27,11 @@ public class LogGroup {
public Integer getRetentionInDays() { return retentionInDays; }
public void setRetentionInDays(Integer retentionInDays) { this.retentionInDays = retentionInDays; }
+ public boolean isDeletionProtectionEnabled() { return deletionProtectionEnabled; }
+ public void setDeletionProtectionEnabled(boolean deletionProtectionEnabled) {
+ this.deletionProtectionEnabled = deletionProtectionEnabled;
+ }
+
public Map<String, String> getTags() { return tags; }
public void setTags(Map<String, String> tags) { this.tags = tags; }
}
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 541bed0510..db516b42b0 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
@@ -16,6 +16,7 @@ import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -98,6 +99,102 @@ class CloudWatchLogsHandlerTest {
assertEquals(GROUP, ((JsonNode) response.getEntity()).path("logGroupIdentifier").asText());
}
+ @Test
+ void createLogGroupWithDeletionProtectionIsReturnedByDescribeLogGroups() {
+ String protectedGroup = GROUP + "-created-protected";
+ ObjectNode create = MAPPER.createObjectNode();
+ create.put("logGroupName", protectedGroup);
+ create.put("deletionProtectionEnabled", true);
+
+ handler.handle("CreateLogGroup", create, REGION);
+
+ ObjectNode describe = MAPPER.createObjectNode();
+ describe.put("logGroupNamePrefix", protectedGroup);
+ JsonNode group = ((JsonNode) handler.handle("DescribeLogGroups", describe, REGION).getEntity())
+ .path("logGroups").get(0);
+ assertEquals(protectedGroup, group.path("logGroupName").asText());
+ assertTrue(group.path("deletionProtectionEnabled").asBoolean());
+ }
+
+ @Test
+ void putLogGroupDeletionProtectionPersistsByName() {
+ ObjectNode request = MAPPER.createObjectNode();
+ request.put("logGroupIdentifier", GROUP);
+ request.put("deletionProtectionEnabled", true);
+
+ Response response = handler.handle("PutLogGroupDeletionProtection", request, REGION);
+
+ assertEquals(200, response.getStatus());
+ assertTrue(service.describeLogGroups(GROUP, REGION).getFirst().isDeletionProtectionEnabled());
+ }
+
+ @Test
+ void putLogGroupDeletionProtectionAcceptsArnIdentifier() {
+ ObjectNode request = MAPPER.createObjectNode();
+ request.put("logGroupIdentifier", GROUP_ARN);
+ request.put("deletionProtectionEnabled", true);
+
+ handler.handle("PutLogGroupDeletionProtection", request, REGION);
+
+ assertTrue(service.describeLogGroups(GROUP, REGION).getFirst().isDeletionProtectionEnabled());
+ }
+
+ @Test
+ void putLogGroupDeletionProtectionRequiresIdentifier() {
+ ObjectNode request = MAPPER.createObjectNode();
+ request.put("deletionProtectionEnabled", true);
+
+ AwsException error = assertThrows(AwsException.class,
+ () -> handler.handle("PutLogGroupDeletionProtection", request, REGION));
+
+ assertEquals("InvalidParameterException", error.getErrorCode());
+ }
+
+ @Test
+ void putLogGroupDeletionProtectionRequiresBoolean() {
+ ObjectNode request = MAPPER.createObjectNode();
+ request.put("logGroupIdentifier", GROUP);
+
+ AwsException error = assertThrows(AwsException.class,
+ () -> handler.handle("PutLogGroupDeletionProtection", request, REGION));
+
+ assertEquals("InvalidParameterException", error.getErrorCode());
+ }
+
+ @Test
+ void putLogGroupDeletionProtectionRequiresExistingGroup() {
+ ObjectNode request = MAPPER.createObjectNode();
+ request.put("logGroupIdentifier", "/missing");
+ request.put("deletionProtectionEnabled", true);
+
+ AwsException error = assertThrows(AwsException.class,
+ () -> handler.handle("PutLogGroupDeletionProtection", request, REGION));
+
+ assertEquals("ResourceNotFoundException", error.getErrorCode());
+ }
+
+ @Test
+ void protectedLogGroupCannotBeDeletedUntilProtectionIsDisabled() {
+ ObjectNode protection = MAPPER.createObjectNode();
+ protection.put("logGroupIdentifier", GROUP);
+ protection.put("deletionProtectionEnabled", true);
+ handler.handle("PutLogGroupDeletionProtection", protection, REGION);
+
+ ObjectNode deletion = MAPPER.createObjectNode();
+ deletion.put("logGroupName", GROUP);
+ AwsException error = assertThrows(AwsException.class,
+ () -> handler.handle("DeleteLogGroup", deletion, REGION));
+ assertEquals("ValidationException", error.getErrorCode());
+ assertThat(error.getMessage(), containsString("Disable deletion protection"));
+
+ protection.put("deletionProtectionEnabled", false);
+ handler.handle("PutLogGroupDeletionProtection", protection, REGION);
+ Response response = handler.handle("DeleteLogGroup", deletion, REGION);
+
+ assertEquals(200, response.getStatus());
+ assertFalse(service.logGroupExists(GROUP, REGION));
+ }
+
@Test
void describeLogStreamsByLogGroupIdentifierArnWithWildcardSuffix() {
ObjectNode request = MAPPER.createObjectNode();
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsServiceTest.java
index e5e9733bc6..33e223033f 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsServiceTest.java
@@ -43,6 +43,15 @@ class CloudWatchLogsServiceTest {
List<LogGroup> groups = service.describeLogGroups(null, REGION);
assertEquals(1, groups.size());
assertEquals("/app/logs", groups.getFirst().getLogGroupName());
+ assertFalse(groups.getFirst().isDeletionProtectionEnabled());
+ }
+
+ @Test
+ void createLogGroupWithDeletionProtectionEnabled() {
+ service.createLogGroup("/app/protected", null, null, true, REGION);
+
+ LogGroup group = service.describeLogGroups("/app/protected", REGION).getFirst();
+ assertTrue(group.isDeletionProtectionEnabled());
}
@Test
@@ -72,6 +81,29 @@ class CloudWatchLogsServiceTest {
service.deleteLogGroup("/missing", REGION));
}
+ @Test
+ void protectedLogGroupMustBeDisabledBeforeDeletion() {
+ service.createLogGroup("/app/protected", null, null, true, REGION);
+
+ AwsException error = assertThrows(AwsException.class,
+ () -> service.deleteLogGroup("/app/protected", REGION));
+ assertEquals("ValidationException", error.getErrorCode());
+ assertTrue(service.logGroupExists("/app/protected", REGION));
+
+ service.putLogGroupDeletionProtection("/app/protected", false, REGION);
+ service.deleteLogGroup("/app/protected", REGION);
+
+ assertFalse(service.logGroupExists("/app/protected", REGION));
+ }
+
+ @Test
+ void putLogGroupDeletionProtectionRequiresExistingGroup() {
+ AwsException error = assertThrows(AwsException.class,
+ () -> service.putLogGroupDeletionProtection("/missing", true, REGION));
+
+ assertEquals("ResourceNotFoundException", error.getErrorCode());
+ }
+
@Test
void describeLogGroupsWithPrefix() {
service.createLogGroup("/app/alpha", null, null, REGION);
@@ -503,4 +535,4 @@ class CloudWatchLogsServiceTest {
assertEquals(0, atStart.events().size());
assertEquals("b/0", atStart.nextBackwardToken(), "token must echo back to signal start of stream");
}
-}
\ No newline at end of file
+}