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): return the existing thing group on a repeated CreateThingGroup (#2006)
iot-rocket98bd00bde37f
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 14d25bac..31af947a 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -9,6 +9,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).### Fixed+- **IoT — `CreateThingGroup` on an existing name returns the group when nothing differs** — a repeated `CreateThingGroup` always failed with `ResourceAlreadyExistsException`. It now returns the existing group's name, ARN and id when the description, attributes (in any order), parent and tags match, and answers 409 with AWS's message otherwise, leaving the group unchanged, as AWS does. An `AWS::IoT::ThingGroup` that names an existing group still fails its stack, with or without matching properties, now with AWS's name-conflict message. Contributed by @iot-rocket.- **IoT — just-in-time registration under a CA in `SNI_ONLY` mode** — a device certificate signed by a CA registered with `certificateMode` `SNI_ONLY` and auto-registration enabled was refused on its first mTLS connect, so only `DEFAULT` CAs auto-registered. Such a CA now registers the certificate `PENDING_ACTIVATION` and publishes the registered event when the device's TLS ClientHello names the account's endpoint (its DescribeEndpoint prefix in any region) or a host the gateway serves (`localhost`, a compose service name, `MINISTACK_HOST`), and does nothing without a server name. Under a `DEFAULT` CA, a server name with another account's endpoint prefix or a name the gateway does not serve (`mqtt.example.com`) no longer auto-registers, as on AWS. Contributed by @iot-rocket.- **IoT — `RegisterCertificate` links the CA that signed the certificate** — a certificate registered without `caCertificatePem` carried no `caCertificateId`, even when a registered ACTIVE CA had signed it, so `DescribeCertificate` named no CA and `ListCertificatesByCA` left it out. It is now linked to that CA, as AWS does; when several registered CAs share a subject, the signature decides. Contributed by @iot-rocket.diff --git a/ministack/services/cloudformation/provisioners.py b/ministack/services/cloudformation/provisioners.pyindex c51e42a5..1299183c 100644--- a/ministack/services/cloudformation/provisioners.py+++ b/ministack/services/cloudformation/provisioners.py@@ -12471,6 +12471,11 @@ def _iot_thing_group_properties(props):def _iot_thing_group_create(logical_id, props, stack_name):name = props.get("ThingGroupName") or _physical_name(stack_name, logical_id)+ # CreateThingGroup answers 200 for an identical existing group, but a stack+ # never adopts one: AWS fails its name-conflict validation before creating+ # anything, whatever the properties (measured eu-central-1, 2026-10-05).+ if name in _iot._thing_groups:+ raise ValueError(f"Resource of type 'AWS::IoT::ThingGroup' with identifier '{name}' already exists.")payload = {"thingGroupProperties": _iot_thing_group_properties(props)}if props.get("ParentGroupName"):payload["parentGroupName"] = props["ParentGroupName"]diff --git a/ministack/services/iot.py b/ministack/services/iot.pyindex a8031c75..f1ec016a 100644--- a/ministack/services/iot.py+++ b/ministack/services/iot.py@@ -1030,7 +1030,11 @@ def _handle_thing_group(method: str, path: str, body: bytes, qp: dict) -> tuple:if err:return errif method == "POST":- return _create_thing_group(name, _parse_body(body))+ payload = _parse_body(body)+ existing = _thing_groups.get(name)+ if existing is not None:+ return _create_existing_thing_group(existing, payload)+ return _create_thing_group(name, payload)if method == "GET":return _describe_thing_group(name)if method == "PATCH":@@ -1042,6 +1046,47 @@ def _handle_thing_group(method: str, path: str, body: bytes, qp: dict) -> tuple:)+def _thing_group_tag_pairs(tags: list | None) -> list:+ return sorted((t.get("Key"), t.get("Value")) for t in tags or [])+++def _create_existing_thing_group(group: dict, payload: dict) -> tuple:+ """CreateThingGroup on a name that exists. AWS answers 200 with the+ existing group when the request describes it exactly: description,+ attributes (in any key order), parent and tags. The merge flag does not+ count, and an empty attribute map or tag list equals none. Any difference+ is a 409 that leaves the group as it was; a property or parent difference+ names the group, a tag difference names the ARN, and both carry+ resourceArn/resourceId as null (measured eu-central-1, 2026-10-05).++ CloudFormation creates through ``_create_thing_group``, which keeps+ refusing an existing name as the AWS::IoT::ThingGroup handler does."""+ props = payload.get("thingGroupProperties") or {}+ stored = group["thingGroupProperties"]+ stored_parent = (group.get("thingGroupMetadata") or {}).get("parentGroupName")+ if (+ props.get("thingGroupDescription") != stored.get("thingGroupDescription")+ or ((props.get("attributePayload") or {}).get("attributes") or {})+ != ((stored.get("attributePayload") or {}).get("attributes") or {})+ or (payload.get("parentGroupName") or None) != stored_parent+ ):+ message = f"Thing Group {group['thingGroupName']} already exists in current account"+ elif _thing_group_tag_pairs(payload.get("tags")) != _thing_group_tag_pairs(group.get("tags")):+ message = f"Resource {group['thingGroupArn']} already exists in current account"+ else:+ return json_response({+ "thingGroupName": group["thingGroupName"],+ "thingGroupArn": group["thingGroupArn"],+ "thingGroupId": group["thingGroupId"],+ })+ return error_response_json(+ "ResourceAlreadyExistsException",+ f"{message} with different properties",+ 409,+ extra={"resourceArn": None, "resourceId": None},+ )++def _create_thing_group(name: str, payload: dict) -> tuple:if name in _thing_groups:return error_response_json(@@ -1065,6 +1110,8 @@ def _create_thing_group(name: str, payload: dict) -> tuple:"version": 1,"things": [],"creationDate": _now_epoch(),+ # Kept for a repeated CreateThingGroup to compare; not echoed.+ "tags": list(payload.get("tags") or []),}if parent:# DescribeThingGroup reports the parent under thingGroupMetadata, as@@ -1088,7 +1135,7 @@ def _describe_thing_group(name: str) -> tuple:g = _thing_groups.get(name)if g is None:return _error_not_found("ThingGroup", name)- return json_response(g)+ return json_response({k: v for k, v in g.items() if k != "tags"})def _list_thing_groups(qp: dict) -> tuple:diff --git a/tests/test_cfn.py b/tests/test_cfn.pyindex f94e1ec7..5ac8bbf3 100644--- a/tests/test_cfn.py+++ b/tests/test_cfn.py@@ -25726,6 +25726,39 @@ def test_cfn_iot_thing_group_delete_with_child_group_fails(cfn, iot_client):_delete_cfn_test_stack(cfn, stack_name)+def test_cfn_iot_thing_group_existing_name_fails_even_with_identical_properties(cfn, iot_client):+ """CreateThingGroup returns an identical existing group, but a stack does not+ adopt it: with the same or other properties the stack rolls back with the+ name-conflict message, the group stays as it was and survives the stack's+ delete."""+ uid = _uuid_mod.uuid4().hex[:8]+ name = f"cfn-tg-taken-{uid}"+ original = iot_client.create_thing_group(thingGroupName=name, thingGroupProperties={+ "thingGroupDescription": "first", "attributePayload": {"attributes": {"a": "1"}}})+ try:+ for description in ("first", "second"):+ stack_name = f"cfn-tg-taken-{description}-{uid}"+ cfn.create_stack(StackName=stack_name, TemplateBody=json.dumps({"Resources": {+ "Group": {"Type": "AWS::IoT::ThingGroup", "Properties": {+ "ThingGroupName": name,+ "ThingGroupProperties": {+ "ThingGroupDescription": description,+ "AttributePayload": {"Attributes": {"a": "1"}}}}}}}))+ try:+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "ROLLBACK_COMPLETE", stack.get("StackStatusReason")+ assert (f"Resource of type 'AWS::IoT::ThingGroup' with identifier '{name}' already exists."+ in _stack_event_reasons(cfn, stack_name))+ finally:+ _delete_cfn_test_stack(cfn, stack_name)+ group = iot_client.describe_thing_group(thingGroupName=name)+ assert group["thingGroupId"] == original["thingGroupId"]+ assert group["version"] == 1+ assert group["thingGroupProperties"]["thingGroupDescription"] == "first"+ finally:+ iot_client.delete_thing_group(thingGroupName=name)++def test_cfn_apigateway_authorizer_update_in_place_and_replacement(cfn, apigw_v1):"""An authorizer property change updates the authorizer under the same id(Ref and AuthorizerId keep their value), a property the template dropsdiff --git a/tests/test_iot.py b/tests/test_iot.pyindex b552916c..ebb3a393 100644--- a/tests/test_iot.py+++ b/tests/test_iot.py@@ -305,6 +305,95 @@ def test_iot_delete_thing_type_active_rejected(iot_client):# ---------------------------------------------------------------------------+def test_iot_create_thing_group_repeat_returns_the_existing_group(iot_client):+ parent, other, name = _unique("parent"), _unique("parent"), _unique("group")+ iot_client.create_thing_group(thingGroupName=parent)+ iot_client.create_thing_group(thingGroupName=other)+ props = {+ "thingGroupDescription": "first",+ "attributePayload": {"attributes": {"a": "1", "b": "2"}},+ }+ first = iot_client.create_thing_group(+ thingGroupName=name, parentGroupName=parent, thingGroupProperties=props,+ )++ # Attribute order and the merge flag do not count (AWS, 2026-10-05).+ for repeat in (+ props,+ {**props, "attributePayload": {"attributes": {"b": "2", "a": "1"}}},+ {**props, "attributePayload": {"attributes": {"a": "1", "b": "2"}, "merge": True}},+ ):+ again = iot_client.create_thing_group(+ thingGroupName=name, parentGroupName=parent, thingGroupProperties=repeat,+ )+ assert (again["thingGroupName"], again["thingGroupArn"], again["thingGroupId"]) == (+ name, first["thingGroupArn"], first["thingGroupId"])++ for differs in (+ {"thingGroupProperties": {**props, "thingGroupDescription": "second"}},+ {"thingGroupProperties": {"thingGroupDescription": "first"}},+ {"thingGroupProperties": {**props, "attributePayload": {+ "attributes": {"a": "1"}, "merge": True}}},+ {},+ ):+ kwargs = {"parentGroupName": parent, **differs}+ for params in (kwargs, {**kwargs, "parentGroupName": other}):+ with pytest.raises(ClientError) as ei:+ iot_client.create_thing_group(thingGroupName=name, **params)+ assert ei.value.response["Error"]["Code"] == "ResourceAlreadyExistsException"+ assert ei.value.response["Error"]["Message"] == (+ f"Thing Group {name} already exists in current account with different properties")+ with pytest.raises(ClientError) as ei:+ iot_client.create_thing_group(thingGroupName=name, thingGroupProperties=props)+ assert ei.value.response["Error"]["Code"] == "ResourceAlreadyExistsException"++ described = iot_client.describe_thing_group(thingGroupName=name)+ assert described["version"] == 1+ assert described["thingGroupProperties"]["thingGroupDescription"] == "first"+ assert described["thingGroupMetadata"]["parentGroupName"] == parent+ iot_client.delete_thing_group(thingGroupName=name)+ iot_client.delete_thing_group(thingGroupName=parent)+ iot_client.delete_thing_group(thingGroupName=other)+++def test_iot_create_thing_group_repeat_compares_tags(iot_client):+ bare, tagged = _unique("group"), _unique("group")+ iot_client.create_thing_group(thingGroupName=bare)+ first = iot_client.create_thing_group(+ thingGroupName=tagged, tags=[{"Key": "k", "Value": "v"}])++ # No properties equals empty properties, empty attributes and no tags.+ for repeat in (+ {},+ {"thingGroupProperties": {}},+ {"thingGroupProperties": {"attributePayload": {"attributes": {}}}},+ {"tags": []},+ ):+ iot_client.create_thing_group(thingGroupName=bare, **repeat)+ again = iot_client.create_thing_group(+ thingGroupName=tagged, tags=[{"Key": "k", "Value": "v"}])+ assert again["thingGroupId"] == first["thingGroupId"]++ for name, tags in (+ (bare, [{"Key": "k", "Value": "v"}]),+ (tagged, []),+ (tagged, [{"Key": "k", "Value": "w"}]),+ ):+ with pytest.raises(ClientError) as ei:+ iot_client.create_thing_group(thingGroupName=name, tags=tags)+ arn = iot_client.describe_thing_group(thingGroupName=name)["thingGroupArn"]+ assert ei.value.response["Error"]["Message"] == (+ f"Resource {arn} already exists in current account with different properties")+ # A property difference is reported before a tag difference.+ with pytest.raises(ClientError) as ei:+ iot_client.create_thing_group(+ thingGroupName=tagged, thingGroupProperties={"thingGroupDescription": "x"})+ assert ei.value.response["Error"]["Message"] == (+ f"Thing Group {tagged} already exists in current account with different properties")+ iot_client.delete_thing_group(thingGroupName=bare)+ iot_client.delete_thing_group(thingGroupName=tagged)++def test_iot_delete_thing_group_with_child_groups_is_refused(iot_client):parent, child = _unique("parent"), _unique("child")iot_client.create_thing_group(thingGroupName=parent)