mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(logs): reject filtering nonexistent log groups (#6372)
Check the scoped log group before parsing a FilterLogEvents pattern. Reuse the missing-group error and cover missing, deleted, empty and populated groups through service and SDK tests. Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
1 parent
2cda0110a6
commit
f50a698ab7
3 files changed
+116
No files matched your search
+4
@@ -980,6 +980,10 @@ public class CloudWatchLogsService implements ResourceProvider, Resettable {
|
||||
Long startTime, Long endTime,
|
||||
String filterPattern, int limit,
|
||||
String nextToken, String region) {
|
||||
groupStore.get(groupKey(region, groupName))
|
||||
.filter(stored -> groupName.equals(stored.getLogGroupName()))
|
||||
.orElseThrow(CloudWatchLogsService::logGroupNotFound);
|
||||
|
||||
int maxEvents = Math.min(limit > 0 ? limit : Integer.MAX_VALUE,
|
||||
maxEventsPerQuery);
|
||||
// The filter pattern syntax metric and subscription filters use: terms, quoted phrases,
|
||||
|
||||
+23
@@ -45,6 +45,29 @@ class CloudWatchLogsMissingResourceIntegrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void filterLogEventsChecksGroupBeforePatternAndPreservesExistingGroupResults() {
|
||||
String group = "/filter-missing-resource/" + UUID.randomUUID();
|
||||
try (CloudWatchLogsClient logs = client()) {
|
||||
assertResourceNotFound("The specified log group does not exist.",
|
||||
() -> logs.filterLogEvents(request -> request.logGroupName(group)));
|
||||
assertResourceNotFound("The specified log group does not exist.",
|
||||
() -> logs.filterLogEvents(request -> request.logGroupName(group).filterPattern("[")));
|
||||
logs.createLogGroup(request -> request.logGroupName(group));
|
||||
try {
|
||||
assertEquals(0, logs.filterLogEvents(request -> request.logGroupName(group)).events().size());
|
||||
logs.createLogStream(request -> request.logGroupName(group).logStreamName(STREAM));
|
||||
put(logs, group);
|
||||
assertEquals("x", logs.filterLogEvents(request -> request.logGroupName(group))
|
||||
.events().getFirst().message());
|
||||
} finally {
|
||||
logs.deleteLogGroup(request -> request.logGroupName(group));
|
||||
}
|
||||
assertResourceNotFound("The specified log group does not exist.",
|
||||
() -> logs.filterLogEvents(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")));
|
||||
|
||||
+89
@@ -32,6 +32,15 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class CloudWatchLogsServiceTest {
|
||||
|
||||
@Test
|
||||
void filterLogEventsReportsMissingGroupBeforeInvalidFilterPattern() {
|
||||
AwsException error = assertThrows(AwsException.class, () -> service.filterLogEvents(
|
||||
"/missing", List.of(), null, null, "[", 10, null, REGION));
|
||||
assertEquals("ResourceNotFoundException", error.getErrorCode());
|
||||
assertEquals("The specified log group does not exist.", error.getMessage());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
}
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String KEY_ARN =
|
||||
"arn:aws:kms:us-east-1:000000000000:key/1234abcd-12ab-34cd-56ef-1234567890ab";
|
||||
@@ -1281,6 +1290,86 @@ class CloudWatchLogsServiceTest {
|
||||
assertEquals("new", result.events().getFirst().getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void filterLogEventsAllowsExistingEmptyGroup() {
|
||||
service.createLogGroup("/app/empty", null, null, REGION);
|
||||
|
||||
CloudWatchLogsService.FilteredLogEventsResult result = service.filterLogEvents(
|
||||
"/app/empty", null, null, null, null, 100, null, REGION);
|
||||
|
||||
assertTrue(result.events().isEmpty());
|
||||
assertNull(result.nextToken());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void filterLogEventsRejectsMissingOrDeletedGroup(boolean deleted) {
|
||||
service.createLogGroup("/app/retained", null, null, REGION);
|
||||
service.createLogStream("/app/retained", "stream", REGION);
|
||||
service.putLogEvents("/app/retained", "stream",
|
||||
List.of(Map.of("timestamp", 1L, "message", "retained")), REGION);
|
||||
if (deleted) {
|
||||
service.createLogGroup("/app/missing", null, null, REGION);
|
||||
service.createLogStream("/app/missing", "stream", REGION);
|
||||
service.putLogEvents("/app/missing", "stream",
|
||||
List.of(Map.of("timestamp", 2L, "message", "deleted")), REGION);
|
||||
service.deleteLogGroup("/app/missing", REGION);
|
||||
}
|
||||
|
||||
AwsException error = assertThrows(AwsException.class, () -> service.filterLogEvents(
|
||||
"/app/missing", null, null, null, null, 100, null, REGION));
|
||||
|
||||
assertEquals("ResourceNotFoundException", error.getErrorCode());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
assertEquals(List.of("/app/retained"), service.describeLogGroups(null, REGION).stream()
|
||||
.map(LogGroup::getLogGroupName).toList());
|
||||
assertEquals(List.of("retained"), service.filterLogEvents(
|
||||
"/app/retained", null, null, null, null, 100, null, REGION).events().stream()
|
||||
.map(event -> event.event().getMessage()).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void filterLogEventsRequiresGroupInRequestedRegion() {
|
||||
service.createLogGroup("/app/scoped", null, null, REGION);
|
||||
|
||||
AwsException error = assertThrows(AwsException.class, () -> service.filterLogEvents(
|
||||
"/app/scoped", null, null, null, null, 100, null, "eu-west-1"));
|
||||
|
||||
assertEquals("ResourceNotFoundException", error.getErrorCode());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
assertTrue(service.describeLogGroups(null, "eu-west-1").isEmpty());
|
||||
assertEquals(1, service.describeLogGroups(null, REGION).size());
|
||||
assertTrue(service.filterLogEvents(
|
||||
"/app/scoped", null, null, null, null, 100, null, REGION).events().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void filterLogEventsRequiresGroupInCurrentAccount() {
|
||||
String accountA = "111111111111";
|
||||
String accountB = "222222222222";
|
||||
InMemoryStorage<String, LogGroup> rawGroups = new InMemoryStorage<>();
|
||||
CloudWatchLogsService scoped = new CloudWatchLogsService(
|
||||
new AccountAwareStorageBackend<>(rawGroups, null, accountB),
|
||||
new AccountAwareStorageBackend<>(new InMemoryStorage<>(), null, accountB),
|
||||
new AccountAwareStorageBackend<>(new InMemoryStorage<>(), null, accountB),
|
||||
new AccountAwareStorageBackend<>(new InMemoryStorage<>(), null, accountB),
|
||||
new AccountAwareStorageBackend<>(new InMemoryStorage<>(), null, accountB),
|
||||
10_000, new RegionResolver(REGION, accountB));
|
||||
scoped.createLogGroupForAccount(accountA, "/app/scoped", null, null, REGION);
|
||||
Set<String> before = Set.copyOf(rawGroups.keys());
|
||||
|
||||
AwsException error = assertThrows(AwsException.class, () -> scoped.filterLogEvents(
|
||||
"/app/scoped", null, null, null, null, 100, null, REGION));
|
||||
|
||||
assertEquals("ResourceNotFoundException", error.getErrorCode());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
assertEquals(before, Set.copyOf(rawGroups.keys()));
|
||||
assertTrue(scoped.describeLogGroups(null, REGION).isEmpty());
|
||||
scoped.createLogGroup("/app/scoped", null, null, REGION);
|
||||
assertTrue(scoped.filterLogEvents(
|
||||
"/app/scoped", null, null, null, null, 100, null, REGION).events().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void filterLogEvents() {
|
||||
service.createLogGroup("/app/logs", null, null, REGION);
|
||||
|
||||
Reference in new issue
Block a user