fix(logs): report missing log groups and streams with AWS's messages (#6340)

ResourceNotFoundException for a missing log group, log stream or
subscription filter now carries AWS's fixed text, ending with a period
and without the resource name. PutLogEvents looks up the log group
before the stream, so a put to a missing group reports the group, as
AWS does, instead of the stream.

Closes #6338
Closes #6339
This commit is contained in:
Mario Toffia authored and GitHub committed 2026-10-23 10:08:26 -04:00
1 parent 5182449625
commit 6133474079
5 files changed
+155 -42

No files matched your search

@@ -332,8 +332,7 @@ public class CloudWatchLogsService implements ResourceProvider, Resettable {
String key = groupKey(region, name);
LogGroup group = groupStore.get(key)
.filter(stored -> name.equals(stored.getLogGroupName()))
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"The specified log group does not exist: " + name, 400));
.orElseThrow(CloudWatchLogsService::logGroupNotFound);
if (group.isDeletionProtectionEnabled()) {
throw new AwsException("ValidationException",
"The specified log group has deletion protection enabled. "
@@ -438,8 +437,7 @@ public class CloudWatchLogsService implements ResourceProvider, Resettable {
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));
.orElseThrow(CloudWatchLogsService::logGroupNotFound);
group.setDeletionProtectionEnabled(enabled);
groupStore.put(key, group);
}
@@ -448,8 +446,7 @@ public class CloudWatchLogsService implements ResourceProvider, Resettable {
String key = groupKey(region, groupName);
LogGroup group = groupStore.get(key)
.filter(stored -> groupName.equals(stored.getLogGroupName()))
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"The specified log group does not exist: " + groupName, 400));
.orElseThrow(CloudWatchLogsService::logGroupNotFound);
group.setRetentionInDays(days);
groupStore.put(key, group);
}
@@ -458,8 +455,7 @@ public class CloudWatchLogsService implements ResourceProvider, Resettable {
String key = groupKey(region, groupName);
LogGroup group = groupStore.get(key)
.filter(stored -> groupName.equals(stored.getLogGroupName()))
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"The specified log group does not exist: " + groupName, 400));
.orElseThrow(CloudWatchLogsService::logGroupNotFound);
group.setRetentionInDays(null);
groupStore.put(key, group);
}
@@ -472,8 +468,7 @@ public class CloudWatchLogsService implements ResourceProvider, Resettable {
String key = groupKey(region, groupName);
LogGroup group = groupStore.get(key)
.filter(stored -> groupName.equals(stored.getLogGroupName()))
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"The specified log group does not exist: " + groupName, 400));
.orElseThrow(CloudWatchLogsService::logGroupNotFound);
group.getTags().putAll(tags);
groupStore.put(key, group);
}
@@ -482,8 +477,7 @@ public class CloudWatchLogsService implements ResourceProvider, Resettable {
String key = groupKey(region, groupName);
LogGroup group = groupStore.get(key)
.filter(stored -> groupName.equals(stored.getLogGroupName()))
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"The specified log group does not exist: " + groupName, 400));
.orElseThrow(CloudWatchLogsService::logGroupNotFound);
tagKeys.forEach(group.getTags()::remove);
groupStore.put(key, group);
}
@@ -501,8 +495,7 @@ public class CloudWatchLogsService implements ResourceProvider, Resettable {
}
String key = groupKey(region, groupName);
LogGroup group = groupStore.get(key)
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"The specified log group does not exist: " + groupName, 400));
.orElseThrow(CloudWatchLogsService::logGroupNotFound);
group.setKmsKeyId(kmsKeyId);
groupStore.put(key, group);
LOG.infov("Associated KMS key {0} with log group {1}", kmsKeyId, groupName);
@@ -511,8 +504,7 @@ public class CloudWatchLogsService implements ResourceProvider, Resettable {
public void disassociateKmsKey(String groupName, 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));
.orElseThrow(CloudWatchLogsService::logGroupNotFound);
group.setKmsKeyId(null);
groupStore.put(key, group);
LOG.infov("Disassociated KMS key from log group {0}", groupName);
@@ -522,8 +514,7 @@ public class CloudWatchLogsService implements ResourceProvider, Resettable {
String key = groupKey(region, groupName);
LogGroup group = groupStore.get(key)
.filter(stored -> groupName.equals(stored.getLogGroupName()))
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"The specified log group does not exist: " + groupName, 400));
.orElseThrow(CloudWatchLogsService::logGroupNotFound);
return group.getTags();
}
@@ -538,8 +529,7 @@ public class CloudWatchLogsService implements ResourceProvider, Resettable {
String groupKey = groupKey(region, groupName);
getForAccount(groupStore, accountId, groupKey)
.filter(stored -> groupName.equals(stored.getLogGroupName()))
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"The specified log group does not exist: " + groupName, 400));
.orElseThrow(CloudWatchLogsService::logGroupNotFound);
String streamKey = streamKey(region, groupName, streamName);
if (getForAccount(streamStore, accountId, streamKey).isPresent()) {
@@ -561,8 +551,7 @@ public class CloudWatchLogsService implements ResourceProvider, Resettable {
streamStore.get(streamKey)
.filter(stored -> groupName.equals(stored.getLogGroupName())
&& streamName.equals(stored.getLogStreamName()))
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"The specified log stream does not exist: " + streamName, 400));
.orElseThrow(CloudWatchLogsService::logStreamNotFound);
deleteStreamAndEvents(region, groupName, streamName, streamKey);
LOG.infov("Deleted log stream: {0}/{1}", groupName, streamName);
@@ -591,8 +580,7 @@ public class CloudWatchLogsService implements ResourceProvider, Resettable {
// Verify group exists
groupStore.get(groupKey(region, groupName))
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"The specified log group does not exist: " + groupName, 400));
.orElseThrow(CloudWatchLogsService::logGroupNotFound);
int maxResults = Math.min(limit > 0 ? limit : 50, 50);
@@ -706,6 +694,14 @@ public class CloudWatchLogsService implements ResourceProvider, Resettable {
return new AwsException("InvalidParameterException", "The specified nextToken is invalid.", 400);
}
static AwsException logGroupNotFound() {
return new AwsException("ResourceNotFoundException", "The specified log group does not exist.", 400);
}
static AwsException logStreamNotFound() {
return new AwsException("ResourceNotFoundException", "The specified log stream does not exist.", 400);
}
// ──────────────────────────── Log Events ────────────────────────────
public String putLogEvents(String groupName, String streamName,
@@ -716,10 +712,12 @@ public class CloudWatchLogsService implements ResourceProvider, Resettable {
public String putLogEventsForAccount(
String accountId, String groupName, String streamName,
List<Map<String, Object>> events, String region) {
getForAccount(groupStore, accountId, groupKey(region, groupName))
.filter(stored -> groupName.equals(stored.getLogGroupName()))
.orElseThrow(CloudWatchLogsService::logGroupNotFound);
String streamKey = streamKey(region, groupName, streamName);
getForAccount(streamStore, accountId, streamKey)
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"The specified log stream does not exist: " + streamName, 400));
.orElseThrow(CloudWatchLogsService::logStreamNotFound);
long now = System.currentTimeMillis();
long totalBytes = 0;
@@ -753,8 +751,7 @@ public class CloudWatchLogsService implements ResourceProvider, Resettable {
// Again under the lock: DeleteLogStream removes the stream and its events while holding it,
// and the stream's metadata is written here too, so a put can't bring a deleted stream back.
LogStream stream = getForAccount(streamStore, accountId, streamKey)
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"The specified log stream does not exist: " + streamName, 400));
.orElseThrow(CloudWatchLogsService::logStreamNotFound);
boolean done = false;
try {
refillIfStale(index, accountId);
@@ -1176,9 +1173,7 @@ public class CloudWatchLogsService implements ResourceProvider, Resettable {
for (String groupName : distinctGroups) {
// Real AWS StartQuery returns ResourceNotFoundException for a log group that does not exist,
// rather than a successful empty query; mirror that instead of silently scanning nothing.
groupStore.get(groupKey(region, groupName)).orElseThrow(() ->
new AwsException("ResourceNotFoundException",
"The specified log group does not exist: " + groupName, 400));
groupStore.get(groupKey(region, groupName)).orElseThrow(CloudWatchLogsService::logGroupNotFound);
String prefix = region + "::" + groupName + "::";
for (LogEvent e : eventStore.scan(k -> k.startsWith(prefix))) {
if (e.getTimestamp() >= startMs && e.getTimestamp() <= endMs) {
@@ -1306,8 +1301,7 @@ public class CloudWatchLogsService implements ResourceProvider, Resettable {
synchronized (metricFilterStore) {
String groupKey = groupKey(region, logGroupName);
groupStore.get(groupKey)
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"The specified log group does not exist: " + logGroupName, 400));
.orElseThrow(CloudWatchLogsService::logGroupNotFound);
String normalizedPattern = filterPattern != null ? filterPattern : "";
FilterPattern pattern = requireFilterPattern(normalizedPattern);
validateFilterRegexQuota(logGroupName, filterName, pattern, false, region);
@@ -1332,8 +1326,7 @@ public class CloudWatchLogsService implements ResourceProvider, Resettable {
String nextToken, int limit, String region) {
String groupKey = groupKey(region, logGroupName);
groupStore.get(groupKey)
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"The specified log group does not exist: " + logGroupName, 400));
.orElseThrow(CloudWatchLogsService::logGroupNotFound);
String prefix = subscriptionFilterKeyPrefix(region, logGroupName);
List<SubscriptionFilter> all = subscriptionFilterStore.scan(k -> {
@@ -1364,13 +1357,12 @@ public class CloudWatchLogsService implements ResourceProvider, Resettable {
synchronized (metricFilterStore) {
String groupKey = groupKey(region, logGroupName);
groupStore.get(groupKey)
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"The specified log group does not exist: " + logGroupName, 400));
.orElseThrow(CloudWatchLogsService::logGroupNotFound);
String filterKey = subscriptionFilterKey(region, logGroupName, filterName);
subscriptionFilterStore.get(filterKey)
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"The specified subscription filter does not exist: " + filterName, 400));
"The specified subscription filter does not exist.", 400));
subscriptionFilterStore.delete(filterKey);
LOG.infov("Deleted subscription filter: {0} on log group: {1}", filterName, logGroupName);
}