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

* fix(logs): accept log group deletion protection lifecycle

* feat(logs): support log group deletion protection
This commit is contained in:
Saatvik Arya authored and GitHub committed 2026-08-12 22:39:39 -04:00
1 parent 6ca7d44af0
commit 4215006fe2
7 files changed
+278 -3

No files matched your search

@@ -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);
@@ -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)
@@ -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; }
}