mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(logs): refuse FilterLogEvents requests that name no log group (#6693)
This commit is contained in:
1 parent
45d4c28e33
commit
322a6a0977
5 files changed
+84
-3
No files matched your search
+11
@@ -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<>();
|
||||
|
||||
@@ -74,7 +74,8 @@ One action is currently simplified:
|
||||
`FilterLogEvents` reads `filterPattern` with the filter pattern syntax described below, the one
|
||||
metric 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}
|
||||
|
||||
|
||||
+5
@@ -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,
|
||||
|
||||
+33
-1
@@ -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());
|
||||
}
|
||||
|
||||
@Test
|
||||
void putSubscriptionFilter_unknownDistribution_isRejected() {
|
||||
ObjectNode request = MAPPER.createObjectNode();
|
||||
|
||||
+33
-1
@@ -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();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user