mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(sns): support FilterPolicyScope=MessageBody (#863)
Parse the message body as JSON and evaluate the subscription filter policy against the parsed tree, with nested-key descent and type-aware rule matching (string/number/boolean/null values, array-value OR, exists/prefix/anything-but/numeric). Previously the scope was accepted but filtering was skipped, so all messages were delivered. Bodies that are not valid JSON fail closed. The body is parsed lazily once per publish, only when at least one subscription uses MessageBody scope, to avoid O(N-subscriptions) parses. The MessageAttributes scope evaluator is untouched.
This commit is contained in:
1 parent
43ba8a73b0
commit
9a10f81fb7
3 files changed
+464
-9
No files matched your search
@@ -1,5 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.sns;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
@@ -263,4 +265,155 @@ class SnsServiceTest {
|
||||
Map<String, String> attrs = snsService.getTopicAttributes(topic.getTopicArn(), REGION);
|
||||
assertEquals("2", attrs.get("SubscriptionsConfirmed"));
|
||||
}
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
private Subscription subscriptionWithPolicy(String filterPolicy, String scope) {
|
||||
Subscription sub = new Subscription(
|
||||
"arn:aws:sns:us-east-1:000000000000:t:sub", "arn:aws:sns:us-east-1:000000000000:t",
|
||||
"sqs", "arn:aws:sqs:us-east-1:000000000000:q", ACCOUNT);
|
||||
if (filterPolicy != null) {
|
||||
sub.getAttributes().put("FilterPolicy", filterPolicy);
|
||||
}
|
||||
if (scope != null) {
|
||||
sub.getAttributes().put("FilterPolicyScope", scope);
|
||||
}
|
||||
return sub;
|
||||
}
|
||||
|
||||
private static JsonNode body(String json) {
|
||||
try {
|
||||
return MAPPER.readTree(json);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void filterPolicy_messageBody_topLevelStringMatch() {
|
||||
Subscription sub = subscriptionWithPolicy("{\"store\":[\"shoes\"]}", "MessageBody");
|
||||
assertTrue(snsService.matchesFilterPolicy(sub, body("{\"store\":\"shoes\"}"), null));
|
||||
assertFalse(snsService.matchesFilterPolicy(sub, body("{\"store\":\"books\"}"), null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void filterPolicy_messageBody_nestedKeyDescent() {
|
||||
Subscription sub = subscriptionWithPolicy(
|
||||
"{\"store\":{\"city\":[\"seattle\",\"portland\"]}}", "MessageBody");
|
||||
assertTrue(snsService.matchesFilterPolicy(sub,
|
||||
body("{\"store\":{\"city\":\"seattle\"}}"), null));
|
||||
assertFalse(snsService.matchesFilterPolicy(sub,
|
||||
body("{\"store\":{\"city\":\"boston\"}}"), null));
|
||||
assertFalse(snsService.matchesFilterPolicy(sub,
|
||||
body("{\"store\":{\"region\":\"west\"}}"), null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void filterPolicy_messageBody_numericRule() {
|
||||
Subscription sub = subscriptionWithPolicy(
|
||||
"{\"price\":[{\"numeric\":[\">=\",100,\"<\",200]}]}", "MessageBody");
|
||||
assertTrue(snsService.matchesFilterPolicy(sub, body("{\"price\":150}"), null));
|
||||
assertFalse(snsService.matchesFilterPolicy(sub, body("{\"price\":50}"), null));
|
||||
assertFalse(snsService.matchesFilterPolicy(sub, body("{\"price\":\"150\"}"), null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void filterPolicy_messageBody_arrayValueOr() {
|
||||
Subscription sub = subscriptionWithPolicy("{\"tag\":[\"a\",\"b\"]}", "MessageBody");
|
||||
assertTrue(snsService.matchesFilterPolicy(sub, body("{\"tag\":[\"x\",\"b\",\"y\"]}"), null));
|
||||
assertFalse(snsService.matchesFilterPolicy(sub, body("{\"tag\":[\"x\",\"y\"]}"), null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void filterPolicy_messageBody_existsTrueAndFalse() {
|
||||
Subscription present = subscriptionWithPolicy("{\"k\":[{\"exists\":true}]}", "MessageBody");
|
||||
assertTrue(snsService.matchesFilterPolicy(present, body("{\"k\":\"v\"}"), null));
|
||||
assertFalse(snsService.matchesFilterPolicy(present, body("{\"other\":\"v\"}"), null));
|
||||
|
||||
Subscription absent = subscriptionWithPolicy("{\"k\":[{\"exists\":false}]}", "MessageBody");
|
||||
assertTrue(snsService.matchesFilterPolicy(absent, body("{\"other\":\"v\"}"), null));
|
||||
assertFalse(snsService.matchesFilterPolicy(absent, body("{\"k\":\"v\"}"), null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void filterPolicy_messageBody_prefixAndAnythingBut() {
|
||||
Subscription prefix = subscriptionWithPolicy(
|
||||
"{\"name\":[{\"prefix\":\"foo\"}]}", "MessageBody");
|
||||
assertTrue(snsService.matchesFilterPolicy(prefix, body("{\"name\":\"foobar\"}"), null));
|
||||
assertFalse(snsService.matchesFilterPolicy(prefix, body("{\"name\":\"bar\"}"), null));
|
||||
|
||||
Subscription anythingBut = subscriptionWithPolicy(
|
||||
"{\"name\":[{\"anything-but\":[\"foo\",\"bar\"]}]}", "MessageBody");
|
||||
assertTrue(snsService.matchesFilterPolicy(anythingBut, body("{\"name\":\"baz\"}"), null));
|
||||
assertFalse(snsService.matchesFilterPolicy(anythingBut, body("{\"name\":\"foo\"}"), null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void filterPolicy_messageBody_prefixIsTextOnly() {
|
||||
Subscription sub = subscriptionWithPolicy(
|
||||
"{\"price\":[{\"prefix\":\"1\"}]}", "MessageBody");
|
||||
assertFalse(snsService.matchesFilterPolicy(sub, body("{\"price\":100}"), null));
|
||||
assertFalse(snsService.matchesFilterPolicy(sub, body("{\"price\":true}"), null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void filterPolicy_messageBody_anythingButIsTypeAware() {
|
||||
Subscription strRule = subscriptionWithPolicy(
|
||||
"{\"x\":[{\"anything-but\":[\"foo\"]}]}", "MessageBody");
|
||||
assertTrue(snsService.matchesFilterPolicy(strRule, body("{\"x\":100}"), null));
|
||||
|
||||
Subscription numRule = subscriptionWithPolicy(
|
||||
"{\"x\":[{\"anything-but\":[100]}]}", "MessageBody");
|
||||
assertFalse(snsService.matchesFilterPolicy(numRule, body("{\"x\":100}"), null));
|
||||
assertTrue(snsService.matchesFilterPolicy(numRule, body("{\"x\":200}"), null));
|
||||
assertTrue(snsService.matchesFilterPolicy(numRule, body("{\"x\":\"100\"}"), null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void filterPolicy_messageBody_existsRequiresNonEmptyValue() {
|
||||
Subscription existsTrue = subscriptionWithPolicy("{\"k\":[{\"exists\":true}]}", "MessageBody");
|
||||
assertFalse(snsService.matchesFilterPolicy(existsTrue, body("{\"k\":\"\"}"), null));
|
||||
assertFalse(snsService.matchesFilterPolicy(existsTrue, body("{\"k\":[]}"), null));
|
||||
assertFalse(snsService.matchesFilterPolicy(existsTrue, body("{\"k\":{}}"), null));
|
||||
assertTrue(snsService.matchesFilterPolicy(existsTrue, body("{\"k\":0}"), null));
|
||||
assertTrue(snsService.matchesFilterPolicy(existsTrue, body("{\"k\":false}"), null));
|
||||
|
||||
Subscription existsFalse = subscriptionWithPolicy("{\"k\":[{\"exists\":false}]}", "MessageBody");
|
||||
assertTrue(snsService.matchesFilterPolicy(existsFalse, body("{\"k\":\"\"}"), null));
|
||||
assertTrue(snsService.matchesFilterPolicy(existsFalse, body("{\"k\":[]}"), null));
|
||||
assertTrue(snsService.matchesFilterPolicy(existsFalse, body("{\"k\":{}}"), null));
|
||||
assertFalse(snsService.matchesFilterPolicy(existsFalse, body("{\"k\":0}"), null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void filterPolicy_messageBody_anythingButRequiresKeyPresent() {
|
||||
Subscription sub = subscriptionWithPolicy(
|
||||
"{\"x\":[{\"anything-but\":[\"foo\"]}]}", "MessageBody");
|
||||
assertFalse(snsService.matchesFilterPolicy(sub, body("{}"), null));
|
||||
assertFalse(snsService.matchesFilterPolicy(sub, body("{\"other\":\"v\"}"), null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void filterPolicy_messageBody_invalidJson_doesNotDeliver() {
|
||||
Subscription sub = subscriptionWithPolicy("{\"k\":[\"v\"]}", "MessageBody");
|
||||
assertFalse(snsService.matchesFilterPolicy(sub, body("not json"), null));
|
||||
assertFalse(snsService.matchesFilterPolicy(sub, body(""), null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void filterPolicy_messageBody_noFilterPolicy_alwaysDelivers() {
|
||||
Subscription sub = subscriptionWithPolicy(null, "MessageBody");
|
||||
assertTrue(snsService.matchesFilterPolicy(sub, body("not json"), null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void filterPolicy_messageAttributes_unchanged() {
|
||||
Subscription sub = subscriptionWithPolicy("{\"event\":[\"order\"]}", null);
|
||||
Map<String, io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue> matching = Map.of(
|
||||
"event", new io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue("order", "String"));
|
||||
Map<String, io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue> nonMatching = Map.of(
|
||||
"event", new io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue("refund", "String"));
|
||||
assertTrue(snsService.matchesFilterPolicy(sub, null, matching));
|
||||
assertFalse(snsService.matchesFilterPolicy(sub, null, nonMatching));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user