Light, fluffy, and always free - The AWS Local Emulator alternative
fix(logs): refuse FilterLogEvents requests that name no log group (#6693)
Jackson Price322a6a097737
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudWatchLogsFilterPatternTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudWatchLogsFilterPatternTest.javaindex 71667ba9ad..e139c43b94 100644--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudWatchLogsFilterPatternTest.java+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudWatchLogsFilterPatternTest.java@@ -85,6 +85,17 @@ class CloudWatchLogsFilterPatternTest {}}+ @Test+ @DisplayName("a request naming no log group is refused with InvalidParameterException")+ void aRequestNamingNoLogGroupIsRefused() {+ // Not measured on AWS: the API reference requires logGroupName or logGroupIdentifier and models+ // InvalidParameterException for a parameter specified incorrectly.+ try (CloudWatchLogsClient logs = TestFixtures.cloudWatchLogsClient()) {+ assertThatThrownBy(() -> logs.filterLogEvents(request -> request.filterPattern("ERROR")))+ .isInstanceOf(InvalidParameterException.class);+ }+ }+/** The 1-based positions in {@link #EVENTS} of the events the pattern matches, oldest first. */private static List<Integer> matches(CloudWatchLogsClient logs, String groupName, String pattern) {List<Integer> positions = new ArrayList<>();diff --git a/docs/services/cloudwatch.md b/docs/services/cloudwatch.mdindex d7c8b1c244..e8a8053fdf 100644--- a/docs/services/cloudwatch.md+++ b/docs/services/cloudwatch.md@@ -74,7 +74,8 @@ One action is currently simplified:`FilterLogEvents` reads `filterPattern` with the filter pattern syntax described below, the onemetric filters and subscription filters use: terms, quoted phrases, `?` alternatives, `-`exclusions, JSON and space-delimited patterns. An invalid pattern is refused with-`InvalidParameterException`.+`InvalidParameterException`, and so is a request that names no log group by `logGroupName` or+`logGroupIdentifier`.### Subscription filters {#subscription-filters}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.javaindex bcefa42495..9476b8f008 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@@ -400,6 +400,11 @@ public class CloudWatchLogsHandler {requireListSize(request.path("logStreamNames"), "logStreamNames", 1, 100);}request.path("logStreamNames").forEach(n -> streamNames.add(resolveLogStreamName(n.asText(null))));+ // After the logStreamNames check, so a request that check refuses gets its message rather than this one.+ if (groupName == null || groupName.isBlank()) {+ throw new AwsException("InvalidParameterException",+ "logGroupName or logGroupIdentifier is required.", 400);+ }CloudWatchLogsService.FilteredLogEventsResult result =logsService.filterLogEvents(groupName, streamNames, startTime, endTime, filterPattern, limit,diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsInputConstraintTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsInputConstraintTest.javaindex bfe595c8e4..9bc6fbe4ba 100644--- a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsInputConstraintTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsInputConstraintTest.java@@ -1,5 +1,6 @@package io.github.hectorvent.floci.services.cloudwatch.logs;+import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.node.ArrayNode;import com.fasterxml.jackson.databind.node.ObjectNode;@@ -9,6 +10,8 @@ import io.github.hectorvent.floci.core.storage.InMemoryStorage;import io.github.hectorvent.floci.services.cloudwatch.metrics.CloudWatchMetricsService;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;+import org.junit.jupiter.params.ParameterizedTest;+import org.junit.jupiter.params.provider.ValueSource;import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertThrows;@@ -19,7 +22,8 @@ import static org.mockito.Mockito.mock;* pre-existing operations: logEvents list 1-10000 on PutLogEvents,* logStreamNames list 1-100 on FilterLogEvents, and the Random/ByLogStream* distribution enum on PutSubscriptionFilter. All were previously accepted- * verbatim.+ * verbatim. Also FilterLogEvents' rule that a request names its log group,+ * by logGroupName or by logGroupIdentifier.*/class CloudWatchLogsInputConstraintTest {@@ -90,6 +94,34 @@ class CloudWatchLogsInputConstraintTest {assertEquals(200, handler.handle("FilterLogEvents", request, REGION).getStatus());}+ @ParameterizedTest+ @ValueSource(strings = {"{}", "{\"logGroupName\":\"\"}", "{\"logGroupIdentifier\":\"\"}",+ "{\"logGroupName\":null,\"logGroupIdentifier\":null}"})+ void filterLogEvents_noLogGroup_isRejected(String body) throws Exception {+ JsonNode request = MAPPER.readTree(body);+ AwsException e = expect400(() -> handler.handle("FilterLogEvents", request, REGION));+ assertEquals("logGroupName or logGroupIdentifier is required.", e.getMessage());+ }++ @Test+ void filterLogEvents_noLogGroup_isRejectedWhenAGroupIsNamedNull() {+ // "null" is a valid log group name, and the one a missing name is keyed under.+ handler.handle("CreateLogGroup", MAPPER.createObjectNode().put("logGroupName", "null"), REGION);+ AwsException e = expect400(() -> handler.handle("FilterLogEvents", MAPPER.createObjectNode(), REGION));+ assertEquals("logGroupName or logGroupIdentifier is required.", e.getMessage());+ }++ @Test+ void filterLogEvents_noLogGroupAndTooManyStreamNames_reportsTheStreamNames() {+ ObjectNode request = MAPPER.createObjectNode();+ ArrayNode names = request.putArray("logStreamNames");+ for (int i = 0; i <= 100; i++) {+ names.add("s" + i);+ }+ AwsException e = expect400(() -> handler.handle("FilterLogEvents", request, REGION));+ assertEquals("logStreamNames must contain between 1 and 100 entries.", e.getMessage());+ }+@Testvoid putSubscriptionFilter_unknownDistribution_isRejected() {ObjectNode request = MAPPER.createObjectNode();diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMissingResourceIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMissingResourceIntegrationTest.javaindex 77ef51847c..445a6025ea 100644--- a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMissingResourceIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMissingResourceIntegrationTest.java@@ -9,6 +9,7 @@ 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.InvalidParameterException;import software.amazon.awssdk.services.cloudwatchlogs.model.ResourceNotFoundException;import java.net.URI;@@ -68,6 +69,33 @@ class CloudWatchLogsMissingResourceIntegrationTest {}}+ @Test+ void filterLogEventsNamingNoGroupIsAnInvalidParameter() {+ try (CloudWatchLogsClient logs = client()) {+ assertNoGroupNamed(() -> logs.filterLogEvents(request -> request.filterPattern("x")));+ }+ }++ @Test+ void filterLogEventsNamingNoGroupIsAnInvalidParameterBesideAGroupNamedNull() {+ // "null" is a valid log group name, and the one a missing name is keyed under. A region of+ // its own keeps that group out of the other tests sharing this server.+ try (CloudWatchLogsClient logs = client(Region.EU_WEST_3)) {+ logs.createLogGroup(request -> request.logGroupName("null"));+ try {+ assertNoGroupNamed(() -> logs.filterLogEvents(request -> request.filterPattern("x")));+ } finally {+ logs.deleteLogGroup(request -> request.logGroupName("null"));+ }+ }+ }++ private static void assertNoGroupNamed(Executable call) {+ InvalidParameterException error = assertThrows(InvalidParameterException.class, call);+ assertEquals(400, error.statusCode());+ assertEquals("logGroupName or logGroupIdentifier is required.", error.awsErrorDetails().errorMessage());+ }+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")));@@ -80,10 +108,14 @@ class CloudWatchLogsMissingResourceIntegrationTest {}private static CloudWatchLogsClient client() {+ return client(Region.US_EAST_1);+ }++ private static CloudWatchLogsClient client(Region region) {return CloudWatchLogsClient.builder().endpointOverride(URI.create("http://localhost:" + RestAssured.port)).httpClientBuilder(UrlConnectionHttpClient.builder())- .region(Region.US_EAST_1)+ .region(region).credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test"))).build();}