mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(eventbridge): match an event pattern's wildcard filter (#7357)
* fix(eventbridge): match an event pattern's wildcard filter
This commit is contained in:
1 parent
4d12c76411
commit
69b80fb353
5 files changed
+128
-2
No files matched your search
@@ -463,6 +463,7 @@ refuses it. Across accounts the bus policy and the caller's identity policies bo
|
||||
## Current Behavior
|
||||
|
||||
- `PutRule` validates schedule expressions before creating or updating a rule, including disabled rules and rules with an event pattern. Rate values must be positive integers, with singular `minute`, `hour` or `day` for 1 and plural units for larger values. Invalid expressions return `ValidationException` without changing an existing rule or its timer.
|
||||
- An event pattern's `wildcard` filter matches a string value, each `*` covering any run of characters, an empty one included, with `\*` and `\\` standing for a literal `*` and `\`, as EventBridge's comparison operators page describes it. A `wildcard` inside `anything-but` is not evaluated and matches nothing.
|
||||
- `PutEvents` reports success once the source bus accepts an event. Target delivery failures are retried and dead-lettered as described above, and otherwise surface only as a `WARN` in the Floci logs.
|
||||
- A `PutEvents` entry for an event bus that does not exist is answered as AWS answers it: with an event id, not counted in `FailedEntryCount`, and dropped, as no rule matches it. Floci logs the drop at `INFO`. Floci's own deliveries to a bus, for a rule, a pipe or a schedule, are unchanged: an entry for a bus that does not exist fails there.
|
||||
- A `Detail` forwarded to an event bus must be a JSON object, as in AWS; anything else is dropped, including an `InputPath` selecting a scalar such as `$.detail.orderId` or an envelope carrying `"detail": null`.
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package io.floci.aws.core.common;
|
||||
|
||||
import java.util.BitSet;
|
||||
import java.util.function.IntPredicate;
|
||||
|
||||
/** Patterns whose only wildcard is a star. */
|
||||
public final class StarWildcard {
|
||||
|
||||
@@ -14,12 +17,40 @@ public final class StarWildcard {
|
||||
* pattern holds. A caller that wants a case-insensitive match passes both in one case.
|
||||
*/
|
||||
public static boolean matches(String pattern, String value) {
|
||||
return scan(pattern, index -> pattern.charAt(index) == '*', value);
|
||||
}
|
||||
|
||||
/**
|
||||
* As {@link #matches}, where a backslash makes the star or the backslash after it literal:
|
||||
* {@code \*} stands for a {@code *} and {@code \\} for a {@code \}, the two escapes
|
||||
* EventBridge's {@code wildcard} filter takes. A backslash before any other character stands
|
||||
* for itself.
|
||||
*/
|
||||
public static boolean matchesEscaped(String pattern, String value) {
|
||||
StringBuilder literal = new StringBuilder(pattern.length());
|
||||
BitSet stars = new BitSet(pattern.length());
|
||||
for (int i = 0; i < pattern.length(); i++) {
|
||||
char c = pattern.charAt(i);
|
||||
if (c == '\\' && i + 1 < pattern.length()
|
||||
&& (pattern.charAt(i + 1) == '*' || pattern.charAt(i + 1) == '\\')) {
|
||||
literal.append(pattern.charAt(++i));
|
||||
} else {
|
||||
if (c == '*') {
|
||||
stars.set(literal.length());
|
||||
}
|
||||
literal.append(c);
|
||||
}
|
||||
}
|
||||
return scan(literal, stars::get, value);
|
||||
}
|
||||
|
||||
private static boolean scan(CharSequence pattern, IntPredicate isStar, String value) {
|
||||
int patternIndex = 0;
|
||||
int valueIndex = 0;
|
||||
int starPattern = -1;
|
||||
int starValue = -1;
|
||||
while (valueIndex < value.length()) {
|
||||
if (patternIndex < pattern.length() && pattern.charAt(patternIndex) == '*') {
|
||||
if (patternIndex < pattern.length() && isStar.test(patternIndex)) {
|
||||
starPattern = patternIndex++;
|
||||
starValue = valueIndex;
|
||||
} else if (patternIndex < pattern.length()
|
||||
@@ -33,7 +64,7 @@ public final class StarWildcard {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
while (patternIndex < pattern.length() && pattern.charAt(patternIndex) == '*') {
|
||||
while (patternIndex < pattern.length() && isStar.test(patternIndex)) {
|
||||
patternIndex++;
|
||||
}
|
||||
return patternIndex == pattern.length();
|
||||
|
||||
@@ -20,6 +20,7 @@ import io.floci.aws.core.common.PaginatedResult;
|
||||
import io.floci.aws.core.common.Pagination;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.SmithyValidationErrors;
|
||||
import io.floci.aws.core.common.StarWildcard;
|
||||
import io.floci.aws.core.common.StoredResourceArns;
|
||||
import io.floci.aws.core.resource.ExplorerResource;
|
||||
import io.floci.aws.core.resource.ResourceProvider;
|
||||
@@ -1508,6 +1509,14 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
if (element.has("suffix")) {
|
||||
return value != null && value.endsWith(element.get("suffix").asText());
|
||||
}
|
||||
// Amazon EventBridge User Guide, Comparison operators for use in event patterns,
|
||||
// Matching using wildcards: each * matches any run of characters in a string value,
|
||||
// and \* and \\ stand for a literal * and \.
|
||||
if (element.has("wildcard")) {
|
||||
JsonNode wildcard = element.get("wildcard");
|
||||
return value != null && wildcard.isTextual()
|
||||
&& StarWildcard.matchesEscaped(wildcard.asText(), value);
|
||||
}
|
||||
if (element.has("equals-ignore-case")) {
|
||||
return value != null && value.equalsIgnoreCase(element.get("equals-ignore-case").asText());
|
||||
}
|
||||
|
||||
@@ -1683,6 +1683,55 @@ class EventBridgeServiceTest {
|
||||
assertFalse(service.matchesPattern(event, "{\"detail-type\":[{\"suffix\":\".json\"}]}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchesPatternByWildcard_matchesOnlyTheKeysItsStarsCover() {
|
||||
String pattern = "{\"detail\":{\"object\":{\"key\":[{\"wildcard\":\"templates/*/v*/.switchboard.yaml\"}]}}}";
|
||||
assertTrue(service.matchesPattern(objectCreated("templates/switchboard/v1.2.3/.switchboard.yaml"), pattern));
|
||||
assertFalse(service.matchesPattern(objectCreated("templates/switchboard/v1.2.3/assets/Mutation.js"), pattern));
|
||||
assertFalse(service.matchesPattern(objectCreated("configs/switchboard/v1.2.3/.switchboard.yaml"), pattern));
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchesPatternByWildcard_isAnchoredAndAStarMatchesNothingToo() {
|
||||
assertTrue(service.matchesPattern(objectCreated("dir/a.png"), wildcardOnKey("dir/*.png")));
|
||||
assertTrue(service.matchesPattern(objectCreated("dir/.png"), wildcardOnKey("dir/*.png")));
|
||||
assertFalse(service.matchesPattern(objectCreated("dir/a.png.txt"), wildcardOnKey("dir/*.png")));
|
||||
assertFalse(service.matchesPattern(objectCreated("top/dir/a.png"), wildcardOnKey("dir/*.png")));
|
||||
assertTrue(service.matchesPattern(objectCreated("a.b"), wildcardOnKey("a.b")));
|
||||
assertFalse(service.matchesPattern(objectCreated("axb"), wildcardOnKey("a.b")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchesPatternByWildcard_takesABackslashedStarOrBackslashLiterally() {
|
||||
// The pattern's JSON is "dir/\\*.png": a literal star.
|
||||
assertTrue(service.matchesPattern(objectCreated("dir/*.png"), wildcardOnKey("dir/\\\\*.png")));
|
||||
assertFalse(service.matchesPattern(objectCreated("dir/a.png"), wildcardOnKey("dir/\\\\*.png")));
|
||||
// "a\\\\*": a literal backslash, then a star.
|
||||
assertTrue(service.matchesPattern(objectCreated("a\\b"), wildcardOnKey("a\\\\\\\\*")));
|
||||
assertFalse(service.matchesPattern(objectCreated("ab"), wildcardOnKey("a\\\\\\\\*")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchesPatternByWildcard_matchesNoValueThatIsNotAString() {
|
||||
Map<String, Object> event = Map.of("Source", "my.app", "Detail", "{\"size\":12}");
|
||||
assertFalse(service.matchesPattern(event, "{\"detail\":{\"size\":[{\"wildcard\":\"*\"}]}}"));
|
||||
assertFalse(service.matchesPattern(event, "{\"detail\":{\"other\":[{\"wildcard\":\"*\"}]}}"));
|
||||
}
|
||||
|
||||
private static Map<String, Object> objectCreated(String key) {
|
||||
try {
|
||||
String detail = OBJECT_MAPPER.writeValueAsString(
|
||||
Map.of("bucket", Map.of("name", "registry"), "object", Map.of("key", key)));
|
||||
return Map.of("Source", "aws.s3", "DetailType", "Object Created", "Detail", detail);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static String wildcardOnKey(String wildcardJson) {
|
||||
return "{\"detail\":{\"object\":{\"key\":[{\"wildcard\":\"" + wildcardJson + "\"}]}}}";
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchesPatternByEqualsIgnoreCase_matches() {
|
||||
Map<String, Object> event = Map.of("Source", "my.app", "DetailType", "PROD");
|
||||
|
||||
+36
@@ -777,6 +777,42 @@ class EventBridgeTestEventPatternIntegrationTest {
|
||||
.statusCode(400);
|
||||
}
|
||||
|
||||
@Test
|
||||
void wildcardMatchesAnS3ObjectKey() {
|
||||
given()
|
||||
.contentType(EVENT_BRIDGE_CONTENT_TYPE)
|
||||
.header("X-Amz-Target", TARGET)
|
||||
.body("""
|
||||
{
|
||||
"EventPattern": "{\\"source\\":[\\"aws.s3\\"],\\"detail-type\\":[\\"Object Created\\"],\\"detail\\":{\\"object\\":{\\"key\\":[{\\"wildcard\\":\\"templates/*/v*/.switchboard.yaml\\"}]}}}",
|
||||
"Event": "{\\"source\\":\\"aws.s3\\",\\"detail-type\\":\\"Object Created\\",\\"detail\\":{\\"bucket\\":{\\"name\\":\\"registry\\"},\\"object\\":{\\"key\\":\\"templates/app/v1.0.0/.switchboard.yaml\\"}}}"
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Result", equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void wildcardNoMatchOnAnS3ObjectKey() {
|
||||
given()
|
||||
.contentType(EVENT_BRIDGE_CONTENT_TYPE)
|
||||
.header("X-Amz-Target", TARGET)
|
||||
.body("""
|
||||
{
|
||||
"EventPattern": "{\\"source\\":[\\"aws.s3\\"],\\"detail-type\\":[\\"Object Created\\"],\\"detail\\":{\\"object\\":{\\"key\\":[{\\"wildcard\\":\\"templates/*/v*/.switchboard.yaml\\"}]}}}",
|
||||
"Event": "{\\"source\\":\\"aws.s3\\",\\"detail-type\\":\\"Object Created\\",\\"detail\\":{\\"bucket\\":{\\"name\\":\\"registry\\"},\\"object\\":{\\"key\\":\\"templates/app/v1.0.0/assets/index.js\\"}}}"
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Result", equalTo(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingEventRejected() {
|
||||
given()
|
||||
|
||||
Reference in new issue
Block a user