Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

Region-scope SNS state stores (#1064)

Ian Oberstb331bb90a990
diff --git a/ministack/services/sns.py b/ministack/services/sns.py
index cf3fed34..f19db67a 100644
--- a/ministack/services/sns.py
+++ b/ministack/services/sns.py
@@ -30,7 +30,7 @@ _PORT = os.environ.get("GATEWAY_PORT", "4566")
import ministack.services.lambda_svc as _lambda_svc
from ministack.core.arn import ArnParseError, parse_arn
-from ministack.core.responses import AccountScopedDict, get_account_id, get_region, new_uuid
+from ministack.core.responses import AccountRegionScopedDict, get_account_id, get_region, new_uuid
from ministack.services import sqs as _sqs
logger = logging.getLogger("sns")
@@ -119,10 +119,10 @@ def _resolve_topic_tag_arn(arn: str):
from ministack.core.persistence import PERSIST_STATE, load_state
-_topics = AccountScopedDict()
-_sub_arn_to_topic = AccountScopedDict()
-_platform_applications = AccountScopedDict()
-_platform_endpoints = AccountScopedDict()
+_topics = AccountRegionScopedDict()
+_sub_arn_to_topic = AccountRegionScopedDict()
+_platform_applications = AccountRegionScopedDict()
+_platform_endpoints = AccountRegionScopedDict()
# ── Persistence ────────────────────────────────────────────
diff --git a/tests/test_persistence.py b/tests/test_persistence.py
index fa40bef0..a0480f0d 100644
--- a/tests/test_persistence.py
+++ b/tests/test_persistence.py
@@ -857,6 +857,83 @@ def test_sns_platform_endpoints_survive_warm_boot():
mod.reset()
+def test_sns_region_scoped_stores_survive_warm_boot_in_original_scope():
+ """SNS topic, subscription, application, and endpoint stores stay scoped
+ after the real JSON persistence path."""
+ from ministack.core.responses import (
+ get_account_id,
+ get_region,
+ set_request_account_id,
+ set_request_region,
+ )
+
+ mod = _get_module("sns")
+ mod.reset()
+ original_account = get_account_id()
+ original_region = get_region()
+ try:
+ set_request_account_id("111111111111")
+ set_request_region("us-west-2")
+
+ topic_arn = "arn:aws:sns:us-west-2:111111111111:persisted-topic"
+ sub_arn = f"{topic_arn}:sub-1"
+ app_arn = "arn:aws:sns:us-west-2:111111111111:app/GCM/PersistedApp"
+ endpoint_arn = f"{app_arn}/endpoint-1"
+ subscription = {
+ "arn": sub_arn,
+ "protocol": "email",
+ "endpoint": "[email protected]",
+ "confirmed": True,
+ "topic_arn": topic_arn,
+ "owner": "111111111111",
+ "attributes": {"SubscriptionArn": sub_arn, "TopicArn": topic_arn},
+ }
+ mod._topics[topic_arn] = {
+ "name": "persisted-topic",
+ "arn": topic_arn,
+ "attributes": {"TopicArn": topic_arn, "Owner": "111111111111"},
+ "subscriptions": [subscription],
+ "messages": [],
+ "tags": {},
+ }
+ mod._sub_arn_to_topic[sub_arn] = topic_arn
+ mod._platform_applications[app_arn] = {
+ "arn": app_arn,
+ "name": "PersistedApp",
+ "platform": "GCM",
+ "attributes": {},
+ }
+ mod._platform_endpoints[endpoint_arn] = {
+ "arn": endpoint_arn,
+ "application_arn": app_arn,
+ "attributes": {"Token": "persisted-token", "Enabled": "true"},
+ }
+
+ _round_trip_dict(mod, "sns")
+
+ assert mod._topics[topic_arn]["subscriptions"][0]["arn"] == sub_arn
+ assert mod._sub_arn_to_topic[sub_arn] == topic_arn
+ assert mod._platform_applications[app_arn]["arn"] == app_arn
+ assert mod._platform_endpoints[endpoint_arn]["attributes"]["Token"] == "persisted-token"
+
+ set_request_region("us-east-1")
+ assert mod._topics.get(topic_arn) is None
+ assert mod._sub_arn_to_topic.get(sub_arn) is None
+ assert mod._platform_applications.get(app_arn) is None
+ assert mod._platform_endpoints.get(endpoint_arn) is None
+
+ set_request_account_id("222222222222")
+ set_request_region("us-west-2")
+ assert mod._topics.get(topic_arn) is None
+ assert mod._sub_arn_to_topic.get(sub_arn) is None
+ assert mod._platform_applications.get(app_arn) is None
+ assert mod._platform_endpoints.get(endpoint_arn) is None
+ finally:
+ mod.reset()
+ set_request_account_id(original_account)
+ set_request_region(original_region)
+
+
# ── Import-order regression for the ECS NameError trap ───────────────
def test_ecs_module_reload_with_persisted_attributes_does_not_namerror():
diff --git a/tests/test_sns.py b/tests/test_sns.py
index 78bdc262..20c6a24d 100644
--- a/tests/test_sns.py
+++ b/tests/test_sns.py
@@ -59,6 +59,29 @@ def test_sns_get_topic_attributes(sns):
assert resp["Attributes"]["TopicArn"] == arn
assert resp["Attributes"]["DisplayName"] == "" # AWS default is empty, not topic name
+
+def test_sns_topics_are_region_scoped_by_name(sns):
+ name = f"mr-sns-same-name-{_uuid_mod.uuid4().hex[:8]}"
+ west = _regional_client("sns", "us-west-2")
+
+ east_arn = sns.create_topic(Name=name)["TopicArn"]
+ west_arn = west.create_topic(Name=name)["TopicArn"]
+
+ assert east_arn == f"arn:aws:sns:us-east-1:000000000000:{name}"
+ assert west_arn == f"arn:aws:sns:us-west-2:000000000000:{name}"
+
+ east_arns = [t["TopicArn"] for t in sns.list_topics()["Topics"]]
+ west_arns = [t["TopicArn"] for t in west.list_topics()["Topics"]]
+ assert east_arn in east_arns
+ assert west_arn not in east_arns
+ assert west_arn in west_arns
+ assert east_arn not in west_arns
+
+ with pytest.raises(ClientError) as exc:
+ sns.get_topic_attributes(TopicArn=west_arn)
+ assert exc.value.response["Error"]["Code"] == "NotFound"
+
+
def test_sns_set_topic_attributes(sns):
arn = sns.create_topic(Name="intg-sns-setattr")["TopicArn"]
sns.set_topic_attributes(
@@ -123,6 +146,31 @@ def test_sns_list_subscriptions_by_topic(sns):
assert len(subs) >= 1
assert all(s["TopicArn"] == arn for s in subs)
+
+def test_sns_subscription_attributes_are_region_scoped(sns):
+ west = _regional_client("sns", "us-west-2")
+ name = f"mr-sns-sub-region-{_uuid_mod.uuid4().hex[:8]}"
+ east_arn = sns.create_topic(Name=name)["TopicArn"]
+ west_arn = west.create_topic(Name=name)["TopicArn"]
+ east_sub = sns.subscribe(
+ TopicArn=east_arn, Protocol="email", Endpoint=f"{name}[email protected]",
+ )["SubscriptionArn"]
+ west_sub = west.subscribe(
+ TopicArn=west_arn, Protocol="email", Endpoint=f"{name}[email protected]",
+ )["SubscriptionArn"]
+
+ assert sns.get_subscription_attributes(
+ SubscriptionArn=east_sub,
+ )["Attributes"]["TopicArn"] == east_arn
+ assert west.get_subscription_attributes(
+ SubscriptionArn=west_sub,
+ )["Attributes"]["TopicArn"] == west_arn
+
+ with pytest.raises(ClientError) as exc:
+ sns.get_subscription_attributes(SubscriptionArn=west_sub)
+ assert exc.value.response["Error"]["Code"] == "NotFound"
+
+
def test_sns_publish(sns):
arn = sns.create_topic(Name="intg-sns-publish")["TopicArn"]
resp = sns.publish(
@@ -1489,6 +1537,31 @@ def test_sns_create_platform_application(sns):
assert ":app/GCM/intg-sns-pe-create-app" in app_arn
+def test_sns_platform_applications_and_endpoints_are_region_scoped(sns):
+ west = _regional_client("sns", "us-west-2")
+ name = f"mr-sns-platform-region-{_uuid_mod.uuid4().hex[:8]}"
+ token = _uuid_mod.uuid4().hex
+
+ east_app = _create_gcm_app(sns, name)
+ west_app = _create_gcm_app(west, name)
+ east_endpoint = sns.create_platform_endpoint(
+ PlatformApplicationArn=east_app, Token=token,
+ )["EndpointArn"]
+ west_endpoint = west.create_platform_endpoint(
+ PlatformApplicationArn=west_app, Token=token,
+ )["EndpointArn"]
+
+ assert east_app == f"arn:aws:sns:us-east-1:000000000000:app/GCM/{name}"
+ assert west_app == f"arn:aws:sns:us-west-2:000000000000:app/GCM/{name}"
+ assert east_endpoint != west_endpoint
+ assert sns.get_endpoint_attributes(EndpointArn=east_endpoint)["Attributes"]["Token"] == token
+ assert west.get_endpoint_attributes(EndpointArn=west_endpoint)["Attributes"]["Token"] == token
+
+ with pytest.raises(ClientError) as exc:
+ sns.get_endpoint_attributes(EndpointArn=west_endpoint)
+ assert exc.value.response["Error"]["Code"] == "NotFound"
+
+
def test_sns_create_platform_endpoint_stores_token_and_enabled(sns):
app_arn = _create_gcm_app(sns, "intg-sns-pe-token")
token = _uuid_mod.uuid4().hex
@@ -1605,3 +1678,98 @@ def test_sns_publish_to_platform_endpoint(sns):
)["EndpointArn"]
resp = sns.publish(TargetArn=arn, Message="hi")
assert resp["MessageId"]
+
+
+def test_sns_restore_legacy_account_scoped_state_adopts_arn_regions():
+ import ministack.services.sns as _sns
+ from ministack.core.responses import (
+ AccountScopedDict,
+ get_account_id,
+ get_region,
+ set_request_account_id,
+ set_request_region,
+ )
+
+ original_account = get_account_id()
+ original_region = get_region()
+ original_topics = dict(_sns._topics._data)
+ original_subs = dict(_sns._sub_arn_to_topic._data)
+ original_apps = dict(_sns._platform_applications._data)
+ original_endpoints = dict(_sns._platform_endpoints._data)
+ try:
+ _sns.reset()
+ set_request_account_id("000000000000")
+ set_request_region("us-east-1")
+
+ suffix = _uuid_mod.uuid4().hex[:8]
+ topic_arn = f"arn:aws:sns:us-west-2:000000000000:legacy-sns-{suffix}"
+ sub_arn = f"{topic_arn}:{_uuid_mod.uuid4()}"
+ app_arn = f"arn:aws:sns:us-west-2:000000000000:app/GCM/LegacyApp-{suffix}"
+ endpoint_arn = f"{app_arn}/{_uuid_mod.uuid4()}"
+
+ legacy_topics = AccountScopedDict()
+ legacy_topics[topic_arn] = {
+ "name": f"legacy-sns-{suffix}",
+ "arn": topic_arn,
+ "attributes": {
+ "TopicArn": topic_arn,
+ "SubscriptionsConfirmed": "1",
+ "SubscriptionsPending": "0",
+ },
+ "subscriptions": [{
+ "arn": sub_arn,
+ "protocol": "email",
+ "endpoint": "[email protected]",
+ "confirmed": True,
+ "topic_arn": topic_arn,
+ "owner": "000000000000",
+ "attributes": {"SubscriptionArn": sub_arn, "TopicArn": topic_arn},
+ }],
+ "messages": [],
+ "tags": {},
+ }
+
+ legacy_subs = AccountScopedDict()
+ legacy_subs[sub_arn] = topic_arn
+ legacy_apps = AccountScopedDict()
+ legacy_apps[app_arn] = {
+ "arn": app_arn,
+ "name": f"LegacyApp-{suffix}",
+ "platform": "GCM",
+ "attributes": {},
+ }
+ legacy_endpoints = AccountScopedDict()
+ legacy_endpoints[endpoint_arn] = {
+ "arn": endpoint_arn,
+ "application_arn": app_arn,
+ "attributes": {"Token": "legacy-token", "Enabled": "true"},
+ }
+
+ _sns.restore_state({
+ "topics": legacy_topics,
+ "sub_arn_to_topic": legacy_subs,
+ "platform_applications": legacy_apps,
+ "platform_endpoints": legacy_endpoints,
+ })
+
+ assert _sns._topics.get(topic_arn) is None
+ assert _sns._sub_arn_to_topic.get(sub_arn) is None
+ assert _sns._platform_applications.get(app_arn) is None
+ assert _sns._platform_endpoints.get(endpoint_arn) is None
+
+ set_request_region("us-west-2")
+ assert _sns._topics[topic_arn]["arn"] == topic_arn
+ assert _sns._sub_arn_to_topic[sub_arn] == topic_arn
+ assert _sns._platform_applications[app_arn]["arn"] == app_arn
+ assert _sns._platform_endpoints[endpoint_arn]["application_arn"] == app_arn
+ finally:
+ _sns._topics.clear()
+ _sns._topics._data.update(original_topics)
+ _sns._sub_arn_to_topic.clear()
+ _sns._sub_arn_to_topic._data.update(original_subs)
+ _sns._platform_applications.clear()
+ _sns._platform_applications._data.update(original_apps)
+ _sns._platform_endpoints.clear()
+ _sns._platform_endpoints._data.update(original_endpoints)
+ set_request_account_id(original_account)
+ set_request_region(original_region)