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 unknown topic rules with 401 and check rule names as AWS does (#2254)
iot-rocket7212057e6bde
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex e97fa065..173813d8 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -8,6 +8,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).## [Unreleased]### Fixed+- **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.- **S3 — bucket configuration validation** — `PutBucketVersioning` accepted any `Status` or `MfaDelete` and stored it, `PutBucketLifecycleConfiguration` accepted any rule `Status` or transition `StorageClass` and non-positive days, and `PutBucketEncryption` accepted any `SSEAlgorithm`. Out-of-enum values now return 400 `MalformedXML` (`The XML you provided was not well-formed or did not validate against our published schema`, now the message for every `MalformedXML`). A zero or negative `Expiration` `Days` or `NoncurrentVersionExpiration` `NoncurrentDays` returns 400 `InvalidArgument` (`'Days' for Expiration action must be a positive integer`).## [1.5.29] — 2026-10-17diff --git a/ministack/services/iot.py b/ministack/services/iot.pyindex a296c2b8..573120fe 100644--- a/ministack/services/iot.py+++ b/ministack/services/iot.py@@ -5820,9 +5820,26 @@ def _create_topic_rule(name: str, payload: dict, tagging: str | None = None) ->return json_response({})+def _error_rule_denied(name: str) -> tuple:+ return error_response_json(+ "UnauthorizedException", f"Access to topic rule '{name}' was denied", 401+ )+++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,+ )++def _replace_topic_rule(name: str, payload: dict) -> tuple:+ if not _RULE_NAME_RE.match(name):+ return _error_rule_name(name)if name not in _topic_rules:- return _error_not_found("Rule", name)+ return _error_rule_denied(name)try:put_topic_rule(name, payload)except RuleSqlError as exc:@@ -5833,29 +5850,30 @@ def _replace_topic_rule(name: str, payload: dict) -> tuple:def _get_topic_rule(name: str) -> tuple:+ if not _RULE_NAME_RE.match(name):+ return _error_rule_name(name)rule = _topic_rules.get(name)if rule is None:- return _error_not_found("Rule", name)+ return _error_rule_denied(name)return json_response({"ruleArn": _topic_rule_arn(name), "rule": rule})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):- 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,- )+ return _error_rule_name(name)rule = _topic_rules.get(name)if rule is None:- return _error_not_found("Rule", name)+ return _error_rule_denied(name)rule["ruleDisabled"] = disabledreturn 200, {}, b""def _delete_topic_rule(name: str) -> tuple:+ if not _RULE_NAME_RE.match(name):+ return _error_rule_name(name)+ if name not in _topic_rules:+ return _error_rule_denied(name)delete_topic_rule(name)return json_response({})diff --git a/tests/test_cfn.py b/tests/test_cfn.pyindex 5cf405da..350adb3d 100644--- a/tests/test_cfn.py+++ b/tests/test_cfn.py@@ -21525,7 +21525,7 @@ def test_cfn_iot_topic_rule_republish_inside_own_filter_fails_create(cfn, iot_cl"Republish topic cannot be within topic space of rule's topic pattern 'things/in/#'")with pytest.raises(ClientError) as ei:iot_client.get_topic_rule(ruleName=rule_name)- assert ei.value.response["Error"]["Code"] == "ResourceNotFoundException"+ assert ei.value.response["Error"]["Code"] == "UnauthorizedException"finally:_delete_cfn_test_stack(cfn, stack_name)diff --git a/tests/test_iot.py b/tests/test_iot.pyindex f7e5d517..49c95e70 100644--- a/tests/test_iot.py+++ b/tests/test_iot.py@@ -3988,7 +3988,7 @@ def test_iot_topic_rule_lifecycle(iot_client):iot_client.delete_topic_rule(ruleName=name)with pytest.raises(ClientError) as ei:iot_client.get_topic_rule(ruleName=name)- assert ei.value.response["Error"]["Code"] == "ResourceNotFoundException"+ assert ei.value.response["Error"]["Code"] == "UnauthorizedException"def test_iot_topic_rule_duplicate_rejected(iot_client):@@ -4051,7 +4051,9 @@ def test_iot_enable_disable_topic_rule_refusals(iot_client):for call in (iot_client.enable_topic_rule, iot_client.disable_topic_rule):with pytest.raises(ClientError) as ei:call(ruleName=missing)- assert ei.value.response["Error"]["Code"] == "ResourceNotFoundException"+ err = ei.value.response+ assert (err["ResponseMetadata"]["HTTPStatusCode"], err["Error"]["Code"], err["Error"]["Message"]) == (+ 401, "UnauthorizedException", f"Access to topic rule '{missing}' was denied")with pytest.raises(ClientError) as ei:call(ruleName="bad-name")assert ei.value.response["Error"]["Code"] == "InvalidRequestException"@@ -4279,7 +4281,7 @@ def test_iot_create_topic_rule_republish_inside_own_filter_rejected()with pytest.raises(ClientError) as ei:iot_client.get_topic_rule(ruleName=name)- assert ei.value.response["Error"]["Code"] == "ResourceNotFoundException"+ assert ei.value.response["Error"]["Code"] == "UnauthorizedException"@pytest.mark.parametrize(@@ -4339,12 +4341,41 @@ def test_iot_replace_topic_rule_republish_inside_own_filter_rejected(iot_client,iot_client.delete_topic_rule(ruleName=name)-def test_iot_replace_missing_topic_rule_404(iot_client):- with pytest.raises(ClientError) as ei:- iot_client.replace_topic_rule(- ruleName=_rule_name(), topicRulePayload={"sql": "SELECT * FROM 'a'", "actions": []}- )- assert ei.value.response["Error"]["Code"] == "ResourceNotFoundException"+def test_iot_missing_topic_rule_unauthorized(iot_client):+ """Get, Replace and Delete of an unknown rule answer 401 as AWS does."""+ missing = _rule_name()+ calls = (+ lambda: iot_client.get_topic_rule(ruleName=missing),+ lambda: iot_client.replace_topic_rule(+ ruleName=missing, topicRulePayload={"sql": "SELECT * FROM 'a'", "actions": []}+ ),+ lambda: iot_client.delete_topic_rule(ruleName=missing),+ )+ 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"]) == (+ 401, "UnauthorizedException", f"Access to topic rule '{missing}' was denied")+++def test_iot_get_replace_delete_topic_rule_name_pattern_refused(iot_client):+ """Get, Replace and Delete refuse a rule name outside the pattern with 400, as Enable does."""+ calls = (+ lambda: iot_client.get_topic_rule(ruleName="bad-name"),+ lambda: iot_client.replace_topic_rule(+ ruleName="bad-name", topicRulePayload={"sql": "SELECT * FROM 'a'", "actions": []}+ ),+ lambda: iot_client.delete_topic_rule(ruleName="bad-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",+ "1 validation error detected: Value 'bad-name' at 'ruleName' failed to satisfy "+ "constraint: Member must satisfy regular expression pattern: ^[a-zA-Z0-9_]+$")def test_iot_topic_rule_cfn_deploy(cfn, iot_client):@@ -4434,7 +4465,7 @@ def test_iot_topic_rule_cfn_invalid_sql_fails_resource(cfn, iot_client):# Nothing was stored: the invalid rule never entered the rule store.with pytest.raises(ClientError) as ei:iot_client.get_topic_rule(ruleName=rule)- assert ei.value.response["Error"]["Code"] == "ResourceNotFoundException"+ assert ei.value.response["Error"]["Code"] == "UnauthorizedException"finally:try:cfn.delete_stack(StackName=stack)