From cc69af299109a5f80c749923594fb7e336923def Mon Sep 17 00:00:00 2001 From: Charlie Groves Date: Mon, 20 Jan 2025 07:30:51 -0500 Subject: [PATCH] Match an empty prefix or suffix eventbridge pattern I was using this pattern to match all events: https://stackoverflow.com/questions/62406933/aws-eventbridge-pattern-to-capture-all-events It works in AWS and in localstack before 4.0.3. Because the new rule engine was using truthiness to determine if the condition was a prefix one in `condition.get('prefix')`, it would assume a prefix pattern of `''` isn't actually a prefix pattern. This instead checks for `None` to determine the condition type and gets empty prefixes to match again. --- .../services/events/event_rule_engine.py | 4 ++-- .../event_pattern_templates/empty_prefix.json5 | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 tests/aws/services/events/event_pattern_templates/empty_prefix.json5 diff --git a/localstack-core/localstack/services/events/event_rule_engine.py b/localstack-core/localstack/services/events/event_rule_engine.py index eb22237431689..f6d1da1b70d8b 100644 --- a/localstack-core/localstack/services/events/event_rule_engine.py +++ b/localstack-core/localstack/services/events/event_rule_engine.py @@ -95,14 +95,14 @@ def _evaluate_condition(self, value, condition, field_exists: bool): elif value is None: # the remaining conditions require the value to not be None return False - elif prefix := condition.get("prefix"): + elif (prefix := condition.get("prefix")) is not None: if isinstance(prefix, dict): if prefix_equal_ignore_case := prefix.get("equals-ignore-case"): return self._evaluate_prefix(prefix_equal_ignore_case.lower(), value.lower()) else: return self._evaluate_prefix(prefix, value) - elif suffix := condition.get("suffix"): + elif (suffix := condition.get("suffix")) is not None: if isinstance(suffix, dict): if suffix_equal_ignore_case := suffix.get("equals-ignore-case"): return self._evaluate_suffix(suffix_equal_ignore_case.lower(), value.lower()) diff --git a/tests/aws/services/events/event_pattern_templates/empty_prefix.json5 b/tests/aws/services/events/event_pattern_templates/empty_prefix.json5 new file mode 100644 index 0000000000000..027df9fc438f1 --- /dev/null +++ b/tests/aws/services/events/event_pattern_templates/empty_prefix.json5 @@ -0,0 +1,17 @@ +// Based on https://stackoverflow.com/questions/62406933/aws-eventbridge-pattern-to-capture-all-events +{ + "Event": { + "id": "1", + "source": "test-source", + "detail-type": "test-detail-type", + "account": "123456789012", + "region": "us-east-2", + "time": "2022-07-13T13:48:01Z", + "detail": { + "state": "pending" + } + }, + "EventPattern": { + "source": [{"prefix": ""}] + } +}