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(iam): authorize a Secrets Manager request against the stored secret ARN (#1737)
iot-rocketf81253151f57
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 3496703a..8efd75a3 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -8,6 +8,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).## [Unreleased]### Fixed+- **IAM — a Secrets Manager request is authorized against the stored secret ARN** — the resource was built from the name in the request, `secret:<name>`, while AWS evaluates the stored ARN with the six random characters minted at `CreateSecret`. The grant shape the CDK writes for a secret looked up by name, `secret:<name>-??????` or `secret:<name>-*`, matched nothing under `AUTH=true`. The resource now comes from the store through the handlers' own lookup; a secret that does not exist keeps the name-derived ARN. Contributed by @iot-rocket.- **Lambda — a layer shared through `AddLayerVersionPermission` attaches** — every layer ARN from another account was refused before the stored policy was read. Attachment, from the API and from a CloudFormation function, and `GetLayerVersionByArn` now evaluate it for account, root, public and organization grants, and an attached function keeps the content after the grant is revoked or the version is deleted. Contributed by @iot-rocket.## [1.5.12] — 2026-09-15diff --git a/ministack/core/iam_actions.py b/ministack/core/iam_actions.pyindex 0130603f..759b5d7b 100644--- a/ministack/core/iam_actions.py+++ b/ministack/core/iam_actions.py@@ -899,6 +899,14 @@ def extract_resource_arn(service: str, method: str, path: str,if not secret_id:secret_id = _safe_json_field(body, "Name")if secret_id:+ # AWS evaluates against the stored ARN, whose six random characters+ # the request need not carry. The handlers' own lookup reads them;+ # a miss keeps the name-derived ARN.+ from ministack.services import secretsmanager as secretsmanager_svc++ _, secret = secretsmanager_svc._resolve(secret_id)+ if secret:+ return secret["ARN"]if secret_id.startswith("arn:"):return secret_idreturn f"arn:aws:secretsmanager:{region}:{account_id}:secret:{secret_id}"diff --git a/tests/test_iam_auth.py b/tests/test_iam_auth.pyindex 033a3a5a..371a3d96 100644--- a/tests/test_iam_auth.py+++ b/tests/test_iam_auth.py@@ -702,6 +702,67 @@ class TestEnforce:iam_svc._users.pop("enforce-user", None)iam_svc._user_inline_policies.pop("enforce-user", None)+ def test_secretsmanager_suffix_grant_matches_the_stored_arn(self):+ """The grant shape the CDK writes for a secret looked up by name.++ Measured on AWS: a policy on "secret:<name>-??????" allows a request+ against "secret:<name>-0ac6da". The resolved resource therefore has to+ carry the stored suffix, or a correctly scoped policy denies.+ """+ from ministack.core.iam_actions import extract_resource_arn+ from ministack.core.responses import get_account_id+ from ministack.services import iam as iam_svc+ from ministack.services import secretsmanager as sm++ fake_key = "AKIATESTSECRET0001"+ stored = sm.create_secret_in_process("enforce-suffix-secret", "v")+ prefix = stored.rsplit("-", 1)[0]+ iam_svc._access_keys[fake_key] = {+ "UserName": "secret-user", "AccessKeyId": fake_key,+ "SecretAccessKey": "s", "Status": "Active", "CreateDate": "2024-01-01",+ }+ iam_svc._users["secret-user"] = {+ "UserName": "secret-user",+ "Arn": "arn:aws:iam::000000000000:user/secret-user",+ "UserId": "AIDA789", "CreateDate": "2024-01-01", "Path": "/",+ "AttachedPolicies": [], "Tags": [],+ }+ iam_svc._user_inline_policies["secret-user"] = {+ "p": json.dumps({"Statement": [{+ "Effect": "Allow",+ "Action": "secretsmanager:GetSecretValue",+ "Resource": f"{prefix}-??????",+ }]})+ }++ def decide(secret_id):+ resource = extract_resource_arn(+ "secretsmanager", "POST", "/", {},+ json.dumps({"SecretId": secret_id}).encode(), {},+ "us-east-1", get_account_id(),+ )+ return enforce(+ fake_key, "secretsmanager:GetSecretValue", "secretsmanager",+ "us-east-1", resource_arn=resource,+ )++ try:+ assert decide("enforce-suffix-secret") is None+ # The same grant on a different secret still denies.+ assert decide("some-other-secret").decision == "ImplicitDeny"+ # An explicit Deny on the stored ARN now matches as well.+ iam_svc._user_inline_policies["secret-user"]["d"] = json.dumps({"Statement": [{+ "Effect": "Deny",+ "Action": "secretsmanager:GetSecretValue",+ "Resource": f"{prefix}-*",+ }]})+ assert decide("enforce-suffix-secret").decision == "Deny"+ finally:+ sm._secrets.pop("enforce-suffix-secret", None)+ iam_svc._access_keys.pop(fake_key, None)+ iam_svc._users.pop("secret-user", None)+ iam_svc._user_inline_policies.pop("secret-user", None)+def test_explicit_deny_in_policy_blocks(self):from ministack.services import iam as iam_svcfake_key = "AKIATESTDENY000001"@@ -1069,6 +1130,112 @@ class TestResourceArn:body = json.dumps({"SecretId": "my-secret"}).encode()assert extract_resource_arn("secretsmanager", "POST", "/", {}, body, {}, "us-east-1", "123") == "arn:aws:secretsmanager:us-east-1:123:secret:my-secret"+ @pytest.mark.parametrize("form", ["name", "arn", "partial_arn"])+ def test_secretsmanager_resolves_the_stored_arn(self, form):+ # AWS evaluates against the stored ARN with its six random characters.+ # A request may name the secret by name, full ARN or ARN without them.+ from ministack.core.iam_actions import extract_resource_arn+ from ministack.core.responses import get_account_id+ from ministack.services import secretsmanager as sm++ stored = sm.create_secret_in_process("suffix-secret", "v")+ try:+ secret_id = {+ "name": "suffix-secret",+ "arn": stored,+ "partial_arn": stored.rsplit("-", 1)[0],+ }[form]+ body = json.dumps({"SecretId": secret_id}).encode()+ assert extract_resource_arn(+ "secretsmanager", "POST", "/", {}, body, {}, "us-east-1", get_account_id()+ ) == stored+ finally:+ sm._secrets.pop("suffix-secret", None)++ def test_secretsmanager_resolves_the_stored_arn_when_routed_through_detect_service(self):+ """A signed GetSecretValue reaches this branch through detect_service."""+ from ministack.core.iam_actions import extract_resource_arn+ from ministack.core.responses import get_account_id+ from ministack.core.router import detect_service+ from ministack.services import secretsmanager as sm++ stored = sm.create_secret_in_process("router-secret", "v")+ try:+ headers = {+ **_sigv4_headers("secretsmanager", "secretsmanager.us-east-1.amazonaws.com"),+ "x-amz-target": "secretsmanager.GetSecretValue",+ "content-type": "application/x-amz-json-1.1",+ }+ body = json.dumps({"SecretId": "router-secret"}).encode()+ service = detect_service("POST", "/", headers, {})+ assert service == "secretsmanager"+ assert extract_resource_arn(+ service, "POST", "/", headers, body, {}, "us-east-1", get_account_id()+ ) == stored+ finally:+ sm._secrets.pop("router-secret", None)++ @pytest.mark.parametrize("owner_region,owner_account", [+ ("eu-central-1", None),+ ("us-east-1", "111122223333"),+ ])+ def test_secretsmanager_secret_in_another_scope_is_not_resolved(self, owner_region, owner_account):+ """The lookup is scoped to the request's account and region, like the+ handlers'. A name held only in another region or account keeps the+ name-derived ARN, and that secret's full ARN passes through unchanged."""+ from ministack.core.iam_actions import extract_resource_arn+ from ministack.core.responses import (+ _request_account_id,+ get_account_id,+ get_region,+ set_request_region,+ )+ from ministack.services import secretsmanager as sm++ account_id = get_account_id()+ owner = owner_account or account_id+ prev_region = get_region()+ token = _request_account_id.set(owner)+ set_request_region(owner_region)+ try:+ stored = sm.create_secret_in_process("scoped-secret", "v")+ finally:+ _request_account_id.reset(token)+ set_request_region("us-east-1")+ try:+ name_arn = f"arn:aws:secretsmanager:us-east-1:{account_id}:secret:scoped-secret"+ for secret_id, expected in (("scoped-secret", name_arn), (stored, stored)):+ body = json.dumps({"SecretId": secret_id}).encode()+ assert extract_resource_arn(+ "secretsmanager", "POST", "/", {}, body, {}, "us-east-1", account_id+ ) == expected+ finally:+ set_request_region(prev_region)+ sm._secrets.pop_scoped(owner, owner_region, "scoped-secret", None)++ def test_createsecret_on_an_existing_name_resolves_the_stored_arn(self):+ """CreateSecret on a name the store already holds resolves the stored+ ARN; a new name keeps the name-derived ARN. Which ARN AWS evaluates+ here is unmeasured."""+ from ministack.core.iam_actions import extract_resource_arn+ from ministack.core.responses import get_account_id+ from ministack.services import secretsmanager as sm++ account_id = get_account_id()+ stored = sm.create_secret_in_process("existing-createsecret-name", "v")+ try:+ existing_body = json.dumps({"Name": "existing-createsecret-name"}).encode()+ assert extract_resource_arn(+ "secretsmanager", "POST", "/", {}, existing_body, {}, "us-east-1", account_id+ ) == stored++ new_body = json.dumps({"Name": "brand-new-createsecret-name"}).encode()+ assert extract_resource_arn(+ "secretsmanager", "POST", "/", {}, new_body, {}, "us-east-1", account_id+ ) == f"arn:aws:secretsmanager:us-east-1:{account_id}:secret:brand-new-createsecret-name"+ finally:+ sm._secrets.pop("existing-createsecret-name", None)+def test_iam_role(self):from ministack.core.iam_actions import extract_resource_arnassert extract_resource_arn("iam", "POST", "/", {}, b"", {"RoleName": ["my-role"]}, "us-east-1", "123") == "arn:aws:iam::123:role/my-role"