Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
fix(iot): answer AWS's validation message for bad topic rule names (#2285)
iot-rocket7288430de045
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex ac1e7284..235fe50a 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -8,6 +8,8 @@ Versioning follows [Semantic Versioning](https://semver.org/).## [Unreleased]### Fixed+- **IoT — topic rule name errors** — `CreateTopicRule` refused a name outside `^[a-zA-Z0-9_]+$` or longer than 128 characters with its own `Invalid ruleName: must match [a-zA-Z0-9_]{1,128}`; it now answers AWS's 400 `InvalidRequestException` validation message, which names the failed pattern, the failed length or both, and the topic rule calls that already checked the name give the length part too. A valid name followed by a newline passed the name check, so `CreateTopicRule` stored a rule under it; all six calls refuse it now. Contributed by @iot-rocket.+- **IoT — unknown topic rules answer 401** — `GetTopicRule`, `ReplaceTopicRule`, `EnableTopicRule` and `DisableTopicRule` on a rule that does not exist answered 404 `ResourceNotFoundException` and `DeleteTopicRule` answered 200; they now answer 401 `UnauthorizedException` (`Access to topic rule '<name>' was denied`) as AWS does, and `GetTopicRule`, `ReplaceTopicRule` and `DeleteTopicRule` refuse a name outside the rule name pattern with the 400 `InvalidRequestException` that `EnableTopicRule` gives. Contributed by @iot-rocket.- **IoT — MQTT clients are held to the reserved topics AWS lets them use** — a client could subscribe to any `$` filter and publish to the replies AWS IoT writes itself (fleet provisioning, Jobs, Device Defender, MQTT file delivery, Device Location and commands), and an MQTT 5 publish to another `$` topic got DISCONNECT 0x87; the broker now refuses, before the policy check, a filter outside the reserved filters a client may use (SUBACK 0x8F on MQTT 5, a closed connection on 3.1.1) and a publish to those replies or to any other unknown `$` topic (PUBACK 0x90 on MQTT 5 and DISCONNECT 0x90 at QoS 0, a closed connection on 3.1.1), and HTTPS `Publish` answers "Topic can't start with $" for the fleet provisioning and Device Location replies, as AWS does. Contributed by @iot-rocket.- **IAM — trust policies with `NotPrincipal`** — `CreateRole`, `UpdateAssumeRolePolicy` and `AWS::IAM::Role` stored a trust policy with a `NotPrincipal` statement; they now answer `MalformedPolicyDocument` with IAM's message, `Allow with NotPrincipal is not allowed.` when an `Allow` statement carries it and `AssumeRole policy must not contain NotPrincipal field.` for a `Deny`, with `AUTH` on or off. Contributed by @iot-rocket.- **S3 — `x-amz-grant-*` headers** — `PutBucketAcl` and `PutObjectAcl` answered the grant headers with 400 `MissingSecurityHeader`, and `CreateBucket`, `PutObject` and `CopyObject` dropped them. On buckets with ACLs enabled all five now set an ACL of exactly the named grants, in the order S3 lists them, and refuse what S3 refuses: a malformed grant, an unknown group, an email grantee, and grant headers next to `x-amz-acl` or an ACL body. On a bucket with ACLs disabled (`BucketOwnerEnforced`) `PutBucketAcl` and `PutObjectAcl` refuse grant headers with 400 `AccessControlListNotSupported`. Contributed by @iot-rocket.diff --git a/ministack/services/iot.py b/ministack/services/iot.pyindex be5a1b11..5d677721 100644--- a/ministack/services/iot.py+++ b/ministack/services/iot.py@@ -5787,12 +5787,8 @@ def _handle_topic_rule(method: str, path: str, body: bytes, headers: dict) -> tudef _create_topic_rule(name: str, payload: dict, tagging: str | None = None) -> tuple:"""CreateTopicRule; its tags arrive as the ``x-amz-tagging`` header, ``k1=v1&k2=v2``."""- if not _RULE_NAME_RE.match(name or ""):- return error_response_json(- "InvalidRequestException",- "Invalid ruleName: must match [a-zA-Z0-9_]{1,128}",- 400,- )+ if not _RULE_NAME_RE.fullmatch(name or ""):+ return _error_rule_name(name or "")if name in _topic_rules:return error_response_json("ResourceAlreadyExistsException", f"Rule {name!r} already exists", 409@@ -5827,16 +5823,21 @@ def _error_rule_denied(name: str) -> tuple:def _error_rule_name(name: str) -> tuple:- return error_response_json(- "InvalidRequestException",- f"1 validation error detected: Value '{name}' at 'ruleName' failed to satisfy "- "constraint: Member must satisfy regular expression pattern: ^[a-zA-Z0-9_]+$",- 400,+ """The 400 for a rule name outside the pattern, longer than 128, or both."""+ failed = []+ if not re.fullmatch(r"[a-zA-Z0-9_]+", name):+ failed.append("satisfy regular expression pattern: ^[a-zA-Z0-9_]+$")+ if len(name) > 128:+ failed.append("have length less than or equal to 128")+ detail = "; ".join(+ f"Value '{name}' at 'ruleName' failed to satisfy constraint: Member must {c}" for c in failed)+ count = "1 validation error" if len(failed) == 1 else f"{len(failed)} validation errors"+ return error_response_json("InvalidRequestException", f"{count} detected: {detail}", 400)def _replace_topic_rule(name: str, payload: dict) -> tuple:- if not _RULE_NAME_RE.match(name):+ if not _RULE_NAME_RE.fullmatch(name):return _error_rule_name(name)if name not in _topic_rules:return _error_rule_denied(name)@@ -5850,7 +5851,7 @@ def _replace_topic_rule(name: str, payload: dict) -> tuple:def _get_topic_rule(name: str) -> tuple:- if not _RULE_NAME_RE.match(name):+ if not _RULE_NAME_RE.fullmatch(name):return _error_rule_name(name)rule = _topic_rules.get(name)if rule is None:@@ -5860,7 +5861,7 @@ def _get_topic_rule(name: str) -> tuple:def _set_topic_rule_disabled(name: str, disabled: bool) -> tuple:"""EnableTopicRule and DisableTopicRule: 200 with an empty body, also when nothing changes."""- if not _RULE_NAME_RE.match(name):+ if not _RULE_NAME_RE.fullmatch(name):return _error_rule_name(name)rule = _topic_rules.get(name)if rule is None:@@ -5870,7 +5871,7 @@ def _set_topic_rule_disabled(name: str, disabled: bool) -> tuple:def _delete_topic_rule(name: str) -> tuple:- if not _RULE_NAME_RE.match(name):+ if not _RULE_NAME_RE.fullmatch(name):return _error_rule_name(name)if name not in _topic_rules:return _error_rule_denied(name)diff --git a/tests/test_iot.py b/tests/test_iot.pyindex 126ef75e..89c9cdbf 100644--- a/tests/test_iot.py+++ b/tests/test_iot.py@@ -4102,6 +4102,62 @@ def test_iot_topic_rule_duplicate_rejected(iot_client):iot_client.delete_topic_rule(ruleName=name)+def test_iot_topic_rule_name_refused_by_pattern_and_length():+ from conftest import make_client++ raw = make_client("iot", {"parameter_validation": False})+ long_name, long_dash = "r" * 129, "bad-" + "r" * 125+ pattern = "Member must satisfy regular expression pattern: ^[a-zA-Z0-9_]+$"+ length = "Member must have length less than or equal to 128"+ cases = (+ ("bad-name", f"1 validation error detected: Value 'bad-name' at 'ruleName' failed to satisfy "+ f"constraint: {pattern}"),+ (long_name, f"1 validation error detected: Value '{long_name}' at 'ruleName' failed to satisfy "+ f"constraint: {length}"),+ (long_dash, f"2 validation errors detected: Value '{long_dash}' at 'ruleName' failed to satisfy "+ f"constraint: {pattern}; Value '{long_dash}' at 'ruleName' failed to satisfy "+ f"constraint: {length}"),+ )+ for name, message in cases:+ calls = (+ lambda: raw.create_topic_rule(+ ruleName=name, topicRulePayload={"sql": "SELECT * FROM 'a'", "actions": []}+ ),+ lambda: raw.get_topic_rule(ruleName=name),+ lambda: raw.disable_topic_rule(ruleName=name),+ )+ for call in calls:+ with pytest.raises(ClientError) as ei:+ call()+ err = ei.value.response+ assert (err["ResponseMetadata"]["HTTPStatusCode"], err["Error"]["Code"], err["Error"]["Message"]) == (+ 400, "InvalidRequestException", message)+ assert not [r for r in raw.list_topic_rules()["rules"] if r["ruleName"] in (long_name, long_dash)]+++def test_iot_topic_rule_name_with_trailing_newline_refused(iot_client):+ name = _rule_name() + "\n"+ message = (+ f"1 validation error detected: Value '{name}' at 'ruleName' failed to satisfy "+ "constraint: Member must satisfy regular expression pattern: ^[a-zA-Z0-9_]+$"+ )+ calls = (+ lambda: iot_client.create_topic_rule(+ ruleName=name, topicRulePayload={"sql": "SELECT * FROM 'a'", "actions": []}+ ),+ lambda: iot_client.get_topic_rule(ruleName=name),+ lambda: iot_client.delete_topic_rule(ruleName=name),+ lambda: iot_client.enable_topic_rule(ruleName=name),+ )+ for call in calls:+ with pytest.raises(ClientError) as ei:+ call()+ err = ei.value.response+ assert (err["ResponseMetadata"]["HTTPStatusCode"], err["Error"]["Code"], err["Error"]["Message"]) == (+ 400, "InvalidRequestException", message)+ assert name not in [r["ruleName"] for r in iot_client.list_topic_rules()["rules"]]++def test_iot_replace_topic_rule(iot_client):name = _rule_name()iot_client.create_topic_rule(