Skip to content

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

fix(iam): validate a group policy document and fail on a missing entity (#1738)

iot-rocketbbcb91cd846b
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8efd75a3..bc99e04a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
### Fixed
+- **IAM — a group policy document is validated, and an `AWS::IAM::Policy` naming a missing entity fails** — `PutGroupPolicy` stored a malformed document; all three inline handlers now answer `NoSuchEntity` before `MalformedPolicyDocument`, the order measured on AWS. An `AWS::IAM::Policy` skipped a Role, User or Group that was not there and reported `CREATE_COMPLETE`; the resource now fails with `The role with name <name> cannot be found.`, and an update checks names and document before it takes the old policy off. Contributed by @iot-rocket.
- **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.
diff --git a/ministack/services/cloudformation/provisioners.py b/ministack/services/cloudformation/provisioners.py
index 14ca112b..2dddc1dd 100644
--- a/ministack/services/cloudformation/provisioners.py
+++ b/ministack/services/cloudformation/provisioners.py
@@ -2276,6 +2276,39 @@ def _iam_inline_policy_targets(props):
yield param, entity_name, put, remove
+def _iam_policy_require_entities(props):
+ """Raise the sentence IAM answers for the first entity the props name that
+ does not exist.
+
+ Measured: a stack whose AWS::IAM::Policy names a role, user or group that
+ is not there reports CREATE_FAILED for the resource, with "The <kind> with
+ name <name> cannot be found." from IAM as the reason. Called before the
+ first document is written, so a failed resource leaves no half written
+ policy behind.
+ """
+ for prop, kind, store in (("Roles", "role", _iam._roles),
+ ("Users", "user", _iam._users),
+ ("Groups", "group", _iam._groups)):
+ for entity_name in props.get(prop, []) or []:
+ if store.get(entity_name) is None:
+ raise ValueError(f"The {kind} with name {entity_name} cannot be found.")
+
+
+def _iam_policy_require_valid_document(props):
+ """Raise the MalformedPolicyDocument sentence for a PolicyDocument that
+ does not validate.
+
+ _iam_policy_put only sees a malformed document at the first entity's put,
+ which on an update runs after the old policies came off. Called ahead of
+ create and update, after the entities are resolved, so a malformed
+ document never reaches the drops or the writes.
+ """
+ from ministack.core.iam_evaluator import validate_policy_document
+ validation_err = validate_policy_document(props.get("PolicyDocument", {}))
+ if validation_err:
+ raise ValueError(validation_err)
+
+
def _iam_policy_put(props, name):
"""Embed the PolicyDocument on every entity the resource names.
@@ -2316,6 +2349,8 @@ def _iam_policy_create(logical_id, props, stack_name):
a PolicyName still get ids of their own.
"""
physical_id = _physical_name(stack_name, logical_id, max_len=128)
+ _iam_policy_require_entities(props)
+ _iam_policy_require_valid_document(props)
_iam_policy_put(props, props.get("PolicyName") or physical_id)
return physical_id, {"Id": physical_id}
@@ -2333,6 +2368,14 @@ def _iam_policy_update(physical_id, old_props, new_props, stack_name, logical_id
old_name = old_props.get("PolicyName") or physical_id
new_name = new_props.get("PolicyName") or physical_id
+ # Every name the new template lists is resolved before anything comes off,
+ # and the new document is validated here too: an in place change is not
+ # undone by the rollback of an update, so a put that failed after the
+ # drops below (on a missing entity or a malformed document alike) would
+ # leave the entity holding nothing.
+ _iam_policy_require_entities(new_props)
+ _iam_policy_require_valid_document(new_props)
+
# Whatever the old template left behind has to come off first: everything
# under the old name if the name changed, and the entities this template
# no longer lists.
diff --git a/ministack/services/iam.py b/ministack/services/iam.py
index 450dcf2d..cf261216 100644
--- a/ministack/services/iam.py
+++ b/ministack/services/iam.py
@@ -999,17 +999,20 @@ def _list_attached_role_policies(p):
# -------------------- Inline role policies --------------------
def _put_role_policy(p):
+ """The role is resolved before the document is read. Measured: a call
+ naming a role that does not exist with a malformed document is answered
+ NoSuchEntity. PutUserPolicy and PutGroupPolicy use the same order."""
role_name = _p(p, "RoleName")
policy_name = _p(p, "PolicyName")
policy_doc = _p(p, "PolicyDocument")
+ role = _roles.get(role_name)
+ if not role:
+ return _error(404, "NoSuchEntity",
+ f"The role with name {role_name} cannot be found.", ns="iam")
from ministack.core.iam_evaluator import validate_policy_document
validation_err = validate_policy_document(policy_doc)
if validation_err:
return _error(400, "MalformedPolicyDocument", validation_err, ns="iam")
- role = _roles.get(role_name)
- if not role:
- return _error(404, "NoSuchEntity",
- f"Role {role_name} not found.", ns="iam")
role["InlinePolicies"][policy_name] = policy_doc
return _xml(200, "PutRolePolicyResponse", "", ns="iam")
@@ -1777,6 +1780,10 @@ def _put_group_policy(p):
if group_name not in _groups:
return _error(404, "NoSuchEntity",
f"The group with name {group_name} cannot be found.", ns="iam")
+ from ministack.core.iam_evaluator import validate_policy_document
+ validation_err = validate_policy_document(policy_doc)
+ if validation_err:
+ return _error(400, "MalformedPolicyDocument", validation_err, ns="iam")
group_policies = _group_inline_policies.get(group_name)
if group_policies is None:
group_policies = {}
@@ -1846,13 +1853,13 @@ def _put_user_policy(p):
user_name = _p(p, "UserName")
policy_name = _p(p, "PolicyName")
policy_doc = _p(p, "PolicyDocument")
+ if user_name not in _users:
+ return _error(404, "NoSuchEntity",
+ f"The user with name {user_name} cannot be found.", ns="iam")
from ministack.core.iam_evaluator import validate_policy_document
validation_err = validate_policy_document(policy_doc)
if validation_err:
return _error(400, "MalformedPolicyDocument", validation_err, ns="iam")
- if user_name not in _users:
- return _error(404, "NoSuchEntity",
- f"The user with name {user_name} cannot be found.", ns="iam")
user_policies = _user_inline_policies.get(user_name)
if user_policies is None:
user_policies = {}
diff --git a/tests/test_cfn.py b/tests/test_cfn.py
index 005f2116..dcdbc3f3 100644
--- a/tests/test_cfn.py
+++ b/tests/test_cfn.py
@@ -19318,6 +19318,16 @@ def _inline_role_document(iam, role, policy_name):
return json.loads(doc) if isinstance(doc, str) else doc
+def _cfn_inline_policy_template(policy_name, document=None, **entities):
+ """One AWS::IAM::Policy on the Roles / Users / Groups passed as keywords."""
+ return json.dumps({"Resources": {"Pol": {"Type": "AWS::IAM::Policy", "Properties": {
+ "PolicyName": policy_name,
+ "PolicyDocument": document or {"Version": "2012-10-17", "Statement": [
+ {"Effect": "Allow", "Action": "s3:GetObject", "Resource": "*"}]},
+ **entities,
+ }}}})
+
+
def test_cfn_iam_policy_is_inline_not_managed(cfn, iam):
""""Adds or updates an inline policy document that is embedded in the
specified IAM group, user or role" (aws-resource-iam-policy.html) — the
@@ -19466,7 +19476,8 @@ def test_cfn_iam_policy_rename_keeps_the_id_and_leaves_no_stale_policy(cfn, iam)
""""PolicyName ... Update requires: No interruption", and "GetAtt Id: The
stable and unique string identifying the policy"
(aws-resource-iam-policy.html) — a renamed policy keeps its id, the role
- ends up holding only the new name, and the old one is gone.
+ ends up holding only the new name, and the old one is gone. A rename that
+ cannot finish takes nothing off.
The id staying put is what keeps the rename safe: the engine reads a
changed physical id as a replacement and deletes the predecessor once the
@@ -19477,13 +19488,13 @@ def test_cfn_iam_policy_rename_keeps_the_id_and_leaves_no_stale_policy(cfn, iam)
role = f"cfn-pol-ren-role-{uid}"
_cfn_policy_test_roles(iam, [role])
- def template(name, action):
+ def template(name, action, role_names=None):
return json.dumps({
"Resources": {"Pol": {"Type": "AWS::IAM::Policy", "Properties": {
"PolicyName": name,
"PolicyDocument": {"Version": "2012-10-17", "Statement": [
{"Effect": "Allow", "Action": action, "Resource": "*"}]},
- "Roles": [role],
+ "Roles": role_names or [role],
}}},
"Outputs": {"Name": {"Value": {"Ref": "Pol"}},
"Id": {"Value": {"Fn::GetAtt": ["Pol", "Id"]}}},
@@ -19516,6 +19527,48 @@ def test_cfn_iam_policy_rename_keeps_the_id_and_leaves_no_stale_policy(cfn, iam)
with pytest.raises(ClientError) as exc_info:
iam.get_role_policy(RoleName=role, PolicyName=f"cfn-pol-a-{uid}")
assert exc_info.value.response["Error"]["Code"] == "NoSuchEntity"
+
+ # A rename that names a role which is not there takes nothing off. The
+ # rollback of an update leaves a resource that kept its physical id
+ # alone, so the drop has to wait until every name resolves.
+ cfn.update_stack(StackName=stack_name,
+ TemplateBody=template(f"cfn-pol-c-{uid}", "s3:PutObject",
+ [role, f"cfn-pol-ren-gone-{uid}"]))
+ stack = _wait_stack(cfn, stack_name)
+ assert stack["StackStatus"] == "UPDATE_ROLLBACK_COMPLETE", stack.get("StackStatusReason")
+ assert iam.list_role_policies(RoleName=role)["PolicyNames"] == [f"cfn-pol-b-{uid}"]
+ finally:
+ _delete_cfn_test_stack(cfn, stack_name)
+ _cfn_policy_test_roles_cleanup(iam, [role])
+
+
+def test_cfn_iam_policy_malformed_update_rolls_back_before_touching_the_role(cfn, iam):
+ """An update whose new PolicyDocument does not validate rolls back, and
+ the role keeps its last good document. Measured on a real account with a
+ statement that has no Resource."""
+ uid = _uuid_mod.uuid4().hex[:8]
+ stack_name = f"cfn-iam-pol-bad-doc-{uid}"
+ role = f"cfn-pol-bad-doc-role-{uid}"
+ policy_name = f"cfn-pol-bad-doc-{uid}"
+ _cfn_policy_test_roles(iam, [role])
+
+ good_document = {"Version": "2012-10-17", "Statement": [
+ {"Effect": "Allow", "Action": "s3:GetObject", "Resource": "*"}]}
+ bad_document = {"Version": "2012-10-17", "Statement": [
+ {"Effect": "Allow", "Action": "s3:GetObject"}]} # no Resource
+
+ try:
+ cfn.create_stack(StackName=stack_name, TemplateBody=_cfn_inline_policy_template(
+ policy_name, good_document, Roles=[role]))
+ stack = _wait_stack(cfn, stack_name)
+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")
+
+ cfn.update_stack(StackName=stack_name, TemplateBody=_cfn_inline_policy_template(
+ policy_name, bad_document, Roles=[role]))
+ stack = _wait_stack(cfn, stack_name)
+ assert stack["StackStatus"] == "UPDATE_ROLLBACK_COMPLETE", stack.get("StackStatusReason")
+
+ assert _inline_role_document(iam, role, policy_name) == good_document
finally:
_delete_cfn_test_stack(cfn, stack_name)
_cfn_policy_test_roles_cleanup(iam, [role])
@@ -19755,6 +19808,101 @@ def test_cfn_iam_policy_grant_survives_a_second_stack_with_the_same_name(cfn, ia
_cfn_policy_test_roles_cleanup(iam, roles)
+def test_cfn_iam_policy_missing_entity_fails_the_resource(cfn):
+ """An AWS::IAM::Policy that names a role, user or group which does not
+ exist fails the resource instead of skipping it.
+
+ Measured on a real account with one stack per kind, rollback disabled so
+ the events survive: each reports CREATE_FAILED for the resource, with the
+ IAM sentence "The <kind> with name <name> cannot be found." in the reason,
+ and then CREATE_FAILED for the stack. Rollback is on here, so the stack
+ ends in ROLLBACK_COMPLETE and the reason is read off the events."""
+ uid = _uuid_mod.uuid4().hex[:8]
+ for prop, kind in (("Roles", "role"), ("Users", "user"), ("Groups", "group")):
+ stack_name = f"cfn-iam-pol-miss-{kind}-{uid}"
+ missing = f"cfn-pol-miss-{kind}-{uid}"
+ try:
+ cfn.create_stack(StackName=stack_name, TemplateBody=_cfn_inline_policy_template(
+ f"cfn-pol-miss-{uid}", **{prop: [missing]}))
+ stack = _wait_stack(cfn, stack_name)
+ assert stack["StackStatus"] == "ROLLBACK_COMPLETE", stack.get("StackStatusReason")
+ assert f"The {kind} with name {missing} cannot be found." in \
+ _stack_event_reasons(cfn, stack_name)
+ finally:
+ _delete_cfn_test_stack(cfn, stack_name)
+
+
+def test_cfn_iam_policy_missing_entity_writes_nothing_at_all(cfn, iam):
+ """A resource that names one role that exists and one that does not writes
+ the policy on neither: the names are resolved before the first document is
+ embedded, so the failed resource leaves nothing behind for the rollback to
+ miss."""
+ uid = _uuid_mod.uuid4().hex[:8]
+ stack_name = f"cfn-iam-pol-part-{uid}"
+ role = f"cfn-pol-part-role-{uid}"
+ _cfn_policy_test_roles(iam, [role])
+ try:
+ cfn.create_stack(StackName=stack_name, TemplateBody=_cfn_inline_policy_template(
+ f"cfn-pol-part-{uid}", Roles=[role, f"cfn-pol-part-gone-{uid}"]))
+ stack = _wait_stack(cfn, stack_name)
+ assert stack["StackStatus"] == "ROLLBACK_COMPLETE", stack.get("StackStatusReason")
+ assert iam.list_role_policies(RoleName=role)["PolicyNames"] == []
+ finally:
+ _delete_cfn_test_stack(cfn, stack_name)
+ _cfn_policy_test_roles_cleanup(iam, [role])
+
+
[email protected]("prop,kind", [("Users", "user"), ("Groups", "group")])
+def test_cfn_iam_policy_update_adding_a_missing_entity_keeps_the_policy(cfn, iam, prop, kind):
+ """An update that adds a user or group which does not exist rolls back, and
+ the entity that held the policy still holds it. The rename test covers the
+ same case for a role."""
+ uid = _uuid_mod.uuid4().hex[:8]
+ stack_name = f"cfn-iam-pol-add-{kind}-{uid}"
+ entity = {"UserName" if kind == "user" else "GroupName": f"cfn-pol-add-{kind}-{uid}"}
+ policy_name = f"cfn-pol-add-{uid}"
+ missing = f"cfn-pol-add-gone-{uid}"
+ getattr(iam, f"create_{kind}")(**entity)
+ try:
+ cfn.create_stack(StackName=stack_name, TemplateBody=_cfn_inline_policy_template(
+ policy_name, **{prop: list(entity.values())}))
+ stack = _wait_stack(cfn, stack_name)
+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")
+
+ cfn.update_stack(StackName=stack_name, TemplateBody=_cfn_inline_policy_template(
+ policy_name, **{prop: [*entity.values(), missing]}))
+ stack = _wait_stack(cfn, stack_name)
+ assert stack["StackStatus"] == "UPDATE_ROLLBACK_COMPLETE", stack.get("StackStatusReason")
+ assert f"The {kind} with name {missing} cannot be found." in \
+ _stack_event_reasons(cfn, stack_name)
+ assert getattr(iam, f"list_{kind}_policies")(**entity)["PolicyNames"] == [policy_name]
+ finally:
+ _delete_cfn_test_stack(cfn, stack_name)
+ for name in getattr(iam, f"list_{kind}_policies")(**entity)["PolicyNames"]:
+ getattr(iam, f"delete_{kind}_policy")(PolicyName=name, **entity)
+ getattr(iam, f"delete_{kind}")(**entity)
+
+
+def test_cfn_iam_policy_delete_tolerates_an_entity_that_is_already_gone(cfn, iam):
+ """A role deleted out from under a live stack does not fail the stack
+ delete: an entity is routinely torn down before the policy on it."""
+ uid = _uuid_mod.uuid4().hex[:8]
+ stack_name = f"cfn-iam-pol-gone-{uid}"
+ role = f"cfn-pol-gone-role-{uid}"
+ _cfn_policy_test_roles(iam, [role])
+ try:
+ try:
+ cfn.create_stack(StackName=stack_name, TemplateBody=_cfn_inline_policy_template(
+ f"cfn-pol-gone-{uid}", Roles=[role]))
+ assert _wait_stack(cfn, stack_name)["StackStatus"] == "CREATE_COMPLETE"
+ finally:
+ _cfn_policy_test_roles_cleanup(iam, [role])
+ cfn.delete_stack(StackName=stack_name)
+ assert _wait_stack(cfn, stack_name)["StackStatus"] == "DELETE_COMPLETE"
+ finally:
+ _delete_cfn_test_stack(cfn, stack_name)
+
+
def test_cfn_iam_managed_policy_delete_detaches_entities(cfn, iam):
"""Deleting a stack detaches its AWS::IAM::ManagedPolicy from the role it
named before the policy goes, so the role does not keep a dangling ARN
diff --git a/tests/test_iam_auth.py b/tests/test_iam_auth.py
index 371a3d96..216858f8 100644
--- a/tests/test_iam_auth.py
+++ b/tests/test_iam_auth.py
@@ -2187,6 +2187,54 @@ def test_put_role_policy_rejects_malformed_document(iam):
iam.delete_role(RoleName="validation-test-role-2")
+def test_put_group_policy_rejects_malformed_document(iam):
+ """PutGroupPolicy validates its document like PutRolePolicy and
+ PutUserPolicy. Measured: a real account answers MalformedPolicyDocument for
+ a document that is not JSON, one without a Statement, one whose Effect is
+ neither Allow nor Deny, and one whose statement has no Resource."""
+ iam.create_group(GroupName="validation-test-group")
+ try:
+ for document in ("not json",
+ json.dumps({"Version": "2012-10-17"}),
+ json.dumps({"Statement": [{"Effect": "Maybe",
+ "Action": "s3:GetObject",
+ "Resource": "*"}]}),
+ json.dumps({"Statement": [{"Effect": "Allow",
+ "Action": "s3:GetObject"}]})):
+ with pytest.raises(ClientError) as exc:
+ iam.put_group_policy(GroupName="validation-test-group",
+ PolicyName="bad", PolicyDocument=document)
+ assert exc.value.response["Error"]["Code"] == "MalformedPolicyDocument"
+ assert iam.list_group_policies(
+ GroupName="validation-test-group")["PolicyNames"] == []
+ finally:
+ iam.delete_group(GroupName="validation-test-group")
+
+
+def test_put_inline_policy_reports_the_missing_entity_before_the_document(iam):
+ """A call that is wrong in both ways at once is answered NoSuchEntity, not
+ MalformedPolicyDocument.
+
+ Measured per kind on a real account, with a document whose Effect is
+ neither Allow nor Deny on a group, a role and a user, and again, on a role
+ and a user, with one that is not JSON: the missing entity is reported
+ before the document is read."""
+ for call, kind, kwargs in (
+ (iam.put_group_policy, "group", {"GroupName": "validation-absent-group"}),
+ (iam.put_role_policy, "role", {"RoleName": "validation-absent-role"}),
+ (iam.put_user_policy, "user", {"UserName": "validation-absent-user"}),
+ ):
+ for document in ("not json",
+ json.dumps({"Statement": [{"Effect": "Maybe",
+ "Action": "s3:GetObject",
+ "Resource": "*"}]})):
+ with pytest.raises(ClientError) as exc:
+ call(PolicyName="bad", PolicyDocument=document, **kwargs)
+ assert exc.value.response["Error"]["Code"] == "NoSuchEntity"
+ assert exc.value.response["Error"]["Message"] == \
+ f"The {kind} with name validation-absent-{kind} cannot be found."
+
+
# ---------------------------------------------------------------------------
# Resource ARN extraction (#1504, #1505)
#