mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
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:
1 parent
5182449625
commit
6133474079
5 files changed
+155
-42
No files matched your search
+30
-38
@@ -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);
|
||||
}
|
||||
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
package io.github.hectorvent.floci.services.cloudwatch.logs;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.RestAssured;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.function.Executable;
|
||||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
||||
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
|
||||
import software.amazon.awssdk.services.cloudwatchlogs.model.ResourceNotFoundException;
|
||||
|
||||
import java.net.URI;
|
||||
import java.time.Instant;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
@QuarkusTest
|
||||
class CloudWatchLogsMissingResourceIntegrationTest {
|
||||
|
||||
private static final String STREAM = "app";
|
||||
|
||||
@Test
|
||||
void putLogEventsReportsTheMissingGroupThenTheMissingStreamAsAwsDoes() {
|
||||
String group = "/missing-resource/" + UUID.randomUUID();
|
||||
try (CloudWatchLogsClient logs = client()) {
|
||||
assertResourceNotFound("The specified log group does not exist.", () -> put(logs, group));
|
||||
assertResourceNotFound("The specified log group does not exist.",
|
||||
() -> logs.describeLogStreams(request -> request.logGroupName(group)));
|
||||
|
||||
logs.createLogGroup(request -> request.logGroupName(group));
|
||||
try {
|
||||
assertResourceNotFound("The specified log stream does not exist.", () -> put(logs, group));
|
||||
|
||||
logs.createLogStream(request -> request.logGroupName(group).logStreamName(STREAM));
|
||||
put(logs, group);
|
||||
assertEquals(1, logs.getLogEvents(request -> request.logGroupName(group).logStreamName(STREAM))
|
||||
.events().size());
|
||||
} finally {
|
||||
logs.deleteLogGroup(request -> request.logGroupName(group));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void put(CloudWatchLogsClient logs, String group) {
|
||||
logs.putLogEvents(request -> request.logGroupName(group).logStreamName(STREAM)
|
||||
.logEvents(event -> event.timestamp(Instant.now().toEpochMilli()).message("x")));
|
||||
}
|
||||
|
||||
private static void assertResourceNotFound(String message, Executable call) {
|
||||
ResourceNotFoundException error = assertThrows(ResourceNotFoundException.class, call);
|
||||
assertEquals(400, error.statusCode());
|
||||
assertEquals(message, error.awsErrorDetails().errorMessage());
|
||||
}
|
||||
|
||||
private static CloudWatchLogsClient client() {
|
||||
return CloudWatchLogsClient.builder()
|
||||
.endpointOverride(URI.create("http://localhost:" + RestAssured.port))
|
||||
.httpClientBuilder(UrlConnectionHttpClient.builder())
|
||||
.region(Region.US_EAST_1)
|
||||
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test")))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
+2
-3
@@ -15,7 +15,6 @@ import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
@@ -70,7 +69,7 @@ class CloudWatchLogsQueryIdentifiersIntegrationTest {
|
||||
|
||||
startQuery(List.of(group + ":")).then().statusCode(400)
|
||||
.body("__type", equalTo("ResourceNotFoundException"))
|
||||
.body("message", containsString(group + ":"));
|
||||
.body("message", equalTo("The specified log group does not exist."));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -79,7 +78,7 @@ class CloudWatchLogsQueryIdentifiersIntegrationTest {
|
||||
|
||||
startQuery(List.of("not-an-arn:log-group:" + group + ":")).then().statusCode(400)
|
||||
.body("__type", equalTo("ResourceNotFoundException"))
|
||||
.body("message", containsString(group + ":"));
|
||||
.body("message", equalTo("The specified log group does not exist."));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
|
||||
+55
@@ -12,6 +12,7 @@ import io.github.hectorvent.floci.testing.ThreadStates;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.Timeout;
|
||||
import org.junit.jupiter.api.function.Executable;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
@@ -78,6 +79,60 @@ class CloudWatchLogsServiceTest {
|
||||
}
|
||||
}
|
||||
|
||||
// ──────────────────────────── Missing resources ────────────────────────────
|
||||
|
||||
@Test
|
||||
void missingLogGroupIsReportedWithTheAwsText() {
|
||||
String group = "/missing";
|
||||
Map<String, Executable> calls = new LinkedHashMap<>();
|
||||
calls.put("DeleteLogGroup", () -> service.deleteLogGroup(group, REGION));
|
||||
calls.put("PutLogGroupDeletionProtection", () -> service.putLogGroupDeletionProtection(group, true, REGION));
|
||||
calls.put("PutRetentionPolicy", () -> service.putRetentionPolicy(group, 1, REGION));
|
||||
calls.put("DeleteRetentionPolicy", () -> service.deleteRetentionPolicy(group, REGION));
|
||||
calls.put("TagLogGroup", () -> service.tagLogGroup(group, Map.of("a", "b"), REGION));
|
||||
calls.put("UntagLogGroup", () -> service.untagLogGroup(group, List.of("a"), REGION));
|
||||
calls.put("ListTagsLogGroup", () -> service.listTagsLogGroup(group, REGION));
|
||||
calls.put("AssociateKmsKey", () -> service.associateKmsKey(group, KEY_ARN, REGION));
|
||||
calls.put("DisassociateKmsKey", () -> service.disassociateKmsKey(group, REGION));
|
||||
calls.put("CreateLogStream", () -> service.createLogStream(group, "s", REGION));
|
||||
calls.put("DescribeLogStreams", () -> service.describeLogStreams(group, null, REGION));
|
||||
calls.put("PutLogEvents", () -> service.putLogEvents(group, "s", List.of(Map.of("timestamp", 1L, "message", "x")), REGION));
|
||||
calls.put("StartQuery", () -> service.startQuery(List.of(group), 0, 1, "fields @message", null, REGION));
|
||||
calls.put("PutSubscriptionFilter", () -> service.putSubscriptionFilter(
|
||||
group, "f", "", "arn:aws:lambda:us-east-1:000000000000:function:f", null, REGION));
|
||||
calls.put("DescribeSubscriptionFilters", () -> service.describeSubscriptionFilters(group, null, null, 0, REGION));
|
||||
calls.put("DeleteSubscriptionFilter", () -> service.deleteSubscriptionFilter(group, "f", REGION));
|
||||
|
||||
assertAll(calls.entrySet().stream().map(call -> () ->
|
||||
assertResourceNotFound(call.getKey(), "The specified log group does not exist.", call.getValue())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingLogStreamIsReportedWithTheAwsText() {
|
||||
service.createLogGroup("/app", null, null, REGION);
|
||||
|
||||
assertAll(
|
||||
() -> assertResourceNotFound("PutLogEvents", "The specified log stream does not exist.",
|
||||
() -> service.putLogEvents("/app", "s", List.of(Map.of("timestamp", 1L, "message", "x")), REGION)),
|
||||
() -> assertResourceNotFound("DeleteLogStream", "The specified log stream does not exist.",
|
||||
() -> service.deleteLogStream("/app", "s", REGION)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingSubscriptionFilterIsReportedWithTheAwsText() {
|
||||
service.createLogGroup("/app", null, null, REGION);
|
||||
|
||||
assertResourceNotFound("DeleteSubscriptionFilter", "The specified subscription filter does not exist.",
|
||||
() -> service.deleteSubscriptionFilter("/app", "f", REGION));
|
||||
}
|
||||
|
||||
private static void assertResourceNotFound(String action, String message, Executable call) {
|
||||
AwsException error = assertThrows(AwsException.class, call, action);
|
||||
assertEquals("ResourceNotFoundException", error.getErrorCode(), action);
|
||||
assertEquals(message, error.getMessage(), action);
|
||||
assertEquals(400, error.getHttpStatus(), action);
|
||||
}
|
||||
|
||||
// ──────────────────────────── Log Groups ────────────────────────────
|
||||
|
||||
@Test
|
||||
|
||||
@@ -486,7 +486,7 @@ class IotServiceTest {
|
||||
"actions": [{"cloudwatchLogs": {"logGroupName": "/iot/missing", "roleArn": "arn:aws:iam::000000000000:role/rule"}}],
|
||||
"errorAction": %s}
|
||||
""".formatted(lambdaErrorAction()));
|
||||
doThrow(new AwsException("ResourceNotFoundException", "The specified log group does not exist: /iot/missing", 400))
|
||||
doThrow(new AwsException("ResourceNotFoundException", "The specified log group does not exist.", 400))
|
||||
.when(logs).createLogStream("/iot/missing", "metricsRule", REGION);
|
||||
|
||||
assertDoesNotThrow(() -> publish("{\"v\":1}"));
|
||||
|
||||
Reference in new issue
Block a user