Skip to content

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

fix(logs): take resourceArn alone for log-group resource policies (#2196)

iot-rocket96cb241237ea
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 736e0f5b..95c9ec9e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
### Fixed
+- **CloudWatch Logs — a log group's resource policy by `resourceArn` alone** — `PutResourcePolicy` and `DeleteResourcePolicy` refused a `resourceArn` without `policyName`; they now take it, store the group's policy without a name and with a counting `revisionId`, and answer a request with neither field or a revision conflict with AWS's errors. `AWS::Logs::LogGroup` now creates, rewrites and removes the group's own policy from `ResourcePolicyDocument`. Contributed by @iot-rocket.
- **CloudTrail — trail read-backs and recording switched on at runtime** — `GetTrail` and `DescribeTrails` returned the stored `IsLogging`, `_StartedAt` and `_StoppedAt` keys and empty `S3KeyPrefix` and `SnsTopicName` members, and `GetTrailStatus` left out the six deprecated string members and moved `StopLoggingTime` when a stopped trail was stopped again; they now answer in AWS's shapes, with the start and stop times in milliseconds, and keep the last stop time. Recording switched on through `/_ministack/config` began only after the first CloudTrail API call and now starts with the next request. Contributed by @iot-rocket.
- **ACM / Signer — certificate key and in-use refusals** — `ImportCertificate` accepted a call without a `PrivateKey` and now answers AWS's `ValidationException`; `DeleteCertificate` removed a certificate an Active Signer signing profile uses and now answers `ResourceInUseException`, with those profiles listed in `DescribeCertificate`'s `InUseBy`; `CancelSigningProfile` answered 200 with `{}` and now answers 204 with an empty body, as on AWS. Contributed by @iot-rocket.
- **IoT — not-found messages match AWS** — a call naming a thing, thing type, thing group, certificate, CA certificate, principal, policy, policy version, job, job execution, job target or provisioning template that does not exist answered `ResourceNotFoundException` with a generic `<Resource> '<name>' not found`, and the device jobs plane (HTTP and MQTT `/rejected`) with `No job execution found for thing … and job …`; each now carries the wording AWS uses for that operation, with the same error code and status. Contributed by @iot-rocket.
diff --git a/ministack/services/cloudformation/provisioners.py b/ministack/services/cloudformation/provisioners.py
index a6123768..f8c4e89f 100644
--- a/ministack/services/cloudformation/provisioners.py
+++ b/ministack/services/cloudformation/provisioners.py
@@ -3370,9 +3370,34 @@ def _cwlogs_create(logical_id, props, stack_name):
"streams": {},
"subscriptionFilters": {},
}
+ _cwlogs_sync_resource_policy(name, arn, props)
return name, {"Arn": arn}
+def _cwlogs_policy_document(props):
+ document = props.get("ResourcePolicyDocument")
+ if document is None or isinstance(document, str):
+ return document
+ return json.dumps(document, separators=(",", ":"))
+
+
+def _cwlogs_sync_resource_policy(name, arn, props, old_props=None):
+ """Put or delete the group's own resource policy when ResourcePolicyDocument changes."""
+ document = _cwlogs_policy_document(props)
+ if document == (_cwlogs_policy_document(old_props) if old_props else None):
+ return
+ current = _cw_logs._log_group_policies.get(name)
+ request = {"resourceArn": arn.removesuffix(":*")}
+ if current:
+ request["expectedRevisionId"] = current["revisionId"]
+ if document is None:
+ _cw_logs._delete_resource_policy(request)
+ return
+ resp = _cw_logs._put_resource_policy({**request, "policyDocument": document})
+ if resp[0] >= 400:
+ raise ValueError(f"AWS::Logs::LogGroup ResourcePolicyDocument failed: {resp[2]!r}")
+
+
def _cwlogs_update(physical_id, old_props, new_props, stack_name, logical_id=None):
"""Update a log group in place — retention is the mutable property; the
LogGroupName is create-only, so a change replaces the group. Keeping the
@@ -3393,6 +3418,7 @@ def _cwlogs_update(physical_id, old_props, new_props, stack_name, logical_id=Non
# it was created with and only the key moves.
group["kmsKeyId"] = new_props.get("KmsKeyId") or None
_reconcile_tag_map(group.setdefault("tags", {}), old_props, new_props)
+ _cwlogs_sync_resource_policy(name, group["arn"], new_props, old_props)
return name, {"Arn": group["arn"]}
diff --git a/ministack/services/cloudwatch_logs.py b/ministack/services/cloudwatch_logs.py
index 51b9ed4c..c1023d9a 100644
--- a/ministack/services/cloudwatch_logs.py
+++ b/ministack/services/cloudwatch_logs.py
@@ -1626,14 +1626,16 @@ def _put_destination_policy(data):
def _format_resource_policy(policy):
- return {
- "policyName": policy["policyName"],
+ formatted = {
"policyDocument": policy["policyDocument"],
"lastUpdatedTime": policy["lastUpdatedTime"],
"policyScope": policy.get("policyScope", "ACCOUNT"),
"resourceArn": policy.get("resourceArn"),
"revisionId": policy["revisionId"],
}
+ if policy.get("policyName"):
+ formatted["policyName"] = policy["policyName"]
+ return formatted
def _revision_mismatch():
@@ -1643,15 +1645,22 @@ def _revision_mismatch():
)
+def _policy_target_missing(action):
+ return error_response_json(
+ "InvalidParameterException", f"{action} request must contain policy name or resource arn.", 400)
+
+
def _put_resource_policy(data):
name = data.get("policyName")
document = data.get("policyDocument")
- if not name or not document:
+ resource_arn = data.get("resourceArn")
+ if not name and not resource_arn:
+ return _policy_target_missing("PutResourcePolicy")
+ if not document:
return error_response_json(
"InvalidParameterException",
"policyName and policyDocument are required.", 400,
)
- resource_arn = data.get("resourceArn")
expected_revision = data.get("expectedRevisionId")
policy = {
"policyName": name,
@@ -1660,15 +1669,21 @@ def _put_resource_policy(data):
"revisionId": new_uuid(),
}
if resource_arn:
- # RESOURCE scope: one policy per existing log group, guarded by its revision.
+ # RESOURCE scope: one policy per existing log group, its revision a counter.
group = _resolve_group_by_arn(resource_arn)
if group is None:
return error_response_json(
"ResourceNotFoundException", "The specified log group does not exist.", 400)
existing = _log_group_policies.get(group)
- if (existing["revisionId"] if existing else None) != expected_revision:
- return _revision_mismatch()
- policy.update(policyScope="RESOURCE", resourceArn=resource_arn)
+ if (existing is None) != (expected_revision is None):
+ return error_response_json(
+ "OperationAbortedException",
+ "A conflicting operation is currently in progress against this resource. Please try again.", 400)
+ if existing and existing["revisionId"] != expected_revision:
+ return error_response_json("InvalidParameterException", "Invalid ExpectedRevisionId", 400)
+ current = existing["revisionId"] if existing else ""
+ revision = str(int(current) + 1) if current.isdigit() else "1"
+ policy.update(policyScope="RESOURCE", resourceArn=resource_arn, revisionId=revision)
_log_group_policies[group] = policy
else:
existing = _resource_policies.get(name)
@@ -1681,8 +1696,10 @@ def _put_resource_policy(data):
f"{_MAX_ACCOUNT_RESOURCE_POLICIES} resource policies.", 400)
policy["policyScope"] = "ACCOUNT"
_resource_policies[name] = policy
- return json_response({"resourcePolicy": _format_resource_policy(policy),
- "revisionId": policy["revisionId"]})
+ formatted = _format_resource_policy(policy)
+ if resource_arn:
+ del formatted["revisionId"] # a log group's policy carries it only at the top level
+ return json_response({"resourcePolicy": formatted, "revisionId": policy["revisionId"]})
def _describe_resource_policies(data):
@@ -1690,7 +1707,9 @@ def _describe_resource_policies(data):
scope = data.get("policyScope") or "ACCOUNT"
if scope not in ("ACCOUNT", "RESOURCE"):
return error_response_json(
- "InvalidParameterException", f"Invalid policyScope: {scope}", 400)
+ "InvalidParameterException",
+ "1 validation error detected: Value at 'policyScope' failed to satisfy constraint: "
+ "Member must satisfy enum value set: [ACCOUNT, RESOURCE]", 400)
if resource_arn:
group = _resolve_group_by_arn(resource_arn)
found = _log_group_policies.get(group) if group else None
@@ -1705,21 +1724,22 @@ def _describe_resource_policies(data):
def _delete_resource_policy(data):
name = data.get("policyName")
resource_arn = data.get("resourceArn")
+ if not name and not resource_arn:
+ return _policy_target_missing("DeleteResourcePolicy")
if resource_arn:
group = _resolve_group_by_arn(resource_arn)
existing = _log_group_policies.get(group) if group else None
if existing is None:
return error_response_json(
- "ResourceNotFoundException",
- f"The specified resource policy does not exist: {name}", 400)
+ "ResourceNotFoundException", "No policy found for ResourceArn", 400)
if data.get("expectedRevisionId") != existing["revisionId"]:
- return _revision_mismatch()
+ return error_response_json("InvalidParameterException", "Invalid ExpectedRevisionId", 400)
del _log_group_policies[group]
return json_response({})
- if not name or name not in _resource_policies:
+ if name not in _resource_policies:
return error_response_json(
"ResourceNotFoundException",
- f"The specified resource policy does not exist: {name}", 400,
+ f"Policy with name [{name}] does not exist.", 400,
)
del _resource_policies[name]
return json_response({})
diff --git a/tests/test_cfn.py b/tests/test_cfn.py
index ea4bdeb7..5b2472de 100644
--- a/tests/test_cfn.py
+++ b/tests/test_cfn.py
@@ -21769,6 +21769,63 @@ def test_cfn_update_logs_log_group_in_place(cfn, logs):
_delete_cfn_test_stack(cfn, stack_name)
+def test_cfn_logs_log_group_resource_policy_document(cfn, logs):
+ """ResourcePolicyDocument is the group's own resource policy through create, update and delete."""
+ uid = _uuid_mod.uuid4().hex[:8]
+ stack_name = f"cfn-logs-rpd-{uid}"
+ group = f"/cfn/logs-rpd-{uid}"
+
+ def tpl(sid):
+ props = {"LogGroupName": group}
+ if sid:
+ props["ResourcePolicyDocument"] = {
+ "Version": "2012-10-17",
+ "Statement": [{
+ "Sid": sid, "Effect": "Allow",
+ "Principal": {"Service": "route53.amazonaws.com"},
+ "Action": "logs:PutLogEvents",
+ "Resource": {"Fn::Sub": f"arn:aws:logs:${{AWS::Region}}:${{AWS::AccountId}}:log-group:{group}:*"},
+ }],
+ }
+ return json.dumps({"Resources": {"Group": {"Type": "AWS::Logs::LogGroup", "Properties": props}}})
+
+ def policy():
+ arn = logs.describe_log_groups(logGroupNamePrefix=group)["logGroups"][0]["logGroupArn"]
+ found = logs.describe_resource_policies(resourceArn=arn, policyScope="RESOURCE")["resourcePolicies"]
+ if not found:
+ return None
+ assert "policyName" not in found[0] and found[0]["resourceArn"] == arn
+ document = found[0]["policyDocument"]
+ assert document == json.dumps(json.loads(document), separators=(",", ":"))
+ return json.loads(document)["Statement"][0]["Sid"], found[0]["revisionId"]
+
+ def update(sid):
+ cfn.update_stack(StackName=stack_name, TemplateBody=tpl(sid))
+ stack = _wait_stack(cfn, stack_name)
+ assert stack["StackStatus"] == "UPDATE_COMPLETE", stack.get("StackStatusReason")
+
+ cfn.create_stack(StackName=stack_name, TemplateBody=tpl("First"))
+ try:
+ stack = _wait_stack(cfn, stack_name)
+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")
+ assert policy() == ("First", "1")
+ assert not any(group in (p.get("policyDocument") or "")
+ for p in logs.describe_resource_policies()["resourcePolicies"])
+ update("Second")
+ assert policy() == ("Second", "2")
+ update(None)
+ assert policy() is None
+ update("Third")
+ assert policy() == ("Third", "1")
+
+ cfn.delete_stack(StackName=stack_name)
+ _wait_stack(cfn, stack_name)
+ assert not any(group in (p.get("resourceArn") or "") for p in logs.describe_resource_policies(
+ policyScope="RESOURCE")["resourcePolicies"])
+ finally:
+ _delete_cfn_test_stack(cfn, stack_name)
+
+
@pytest.mark.parametrize("rename", [False, True], ids=["delete", "rename"])
def test_cfn_logs_log_group_removal_drops_api_metric_filters(cfn, logs, rename):
"""A stack delete or a LogGroupName replacement drops the group's API metric filters."""
diff --git a/tests/test_cloudwatch_logs.py b/tests/test_cloudwatch_logs.py
index 3ac467cf..76ab656a 100644
--- a/tests/test_cloudwatch_logs.py
+++ b/tests/test_cloudwatch_logs.py
@@ -1141,6 +1141,79 @@ def test_logs_resource_scoped_policy_lives_on_its_log_group(logs):
logs.delete_log_group(logGroupName=group)
+def test_logs_resource_scoped_policy_takes_resource_arn_alone(logs):
+ """A log group's policy named by resourceArn alone has no name and a counter revision."""
+ uid = _uuid_mod.uuid4().hex[:8]
+ group = f"/intg/resource-policy-arn/{uid}"
+ logs.create_log_group(logGroupName=group)
+ arn = logs.describe_log_groups(logGroupNamePrefix=group)["logGroups"][0]["logGroupArn"]
+ try:
+ first = logs.put_resource_policy(policyDocument="{}", resourceArn=arn)
+ assert first["revisionId"] == "1"
+ assert first["resourcePolicy"]["policyScope"] == "RESOURCE"
+ assert "policyName" not in first["resourcePolicy"]
+ assert "revisionId" not in first["resourcePolicy"]
+ second = logs.put_resource_policy(policyDocument="{}", resourceArn=arn, expectedRevisionId="1")
+ assert second["revisionId"] == "2"
+ listed = logs.describe_resource_policies(resourceArn=arn, policyScope="RESOURCE")["resourcePolicies"]
+ assert [(p.get("policyName"), p["revisionId"]) for p in listed] == [(None, "2")]
+ logs.delete_resource_policy(resourceArn=arn, expectedRevisionId="2")
+ assert logs.put_resource_policy(policyDocument="{}", resourceArn=arn)["revisionId"] == "1"
+ finally:
+ logs.delete_log_group(logGroupName=group)
+
+
+def test_logs_resource_scoped_policy_revision_checks(logs):
+ """A log group's policy is written and deleted against its current revision only."""
+ uid = _uuid_mod.uuid4().hex[:8]
+ group = f"/intg/resource-policy-rev/{uid}"
+ logs.create_log_group(logGroupName=group)
+ arn = logs.describe_log_groups(logGroupNamePrefix=group)["logGroups"][0]["logGroupArn"]
+
+ def refused(call, **kwargs):
+ with pytest.raises(ClientError) as exc:
+ call(resourceArn=arn, **kwargs)
+ return exc.value.response["Error"]["Code"], exc.value.response["Error"]["Message"]
+
+ conflict = ("OperationAbortedException",
+ "A conflicting operation is currently in progress against this resource. Please try again.")
+ invalid = ("InvalidParameterException", "Invalid ExpectedRevisionId")
+ try:
+ assert refused(logs.put_resource_policy, policyDocument="{}", expectedRevisionId="1") == conflict
+ assert refused(logs.delete_resource_policy) == (
+ "ResourceNotFoundException", "No policy found for ResourceArn")
+ logs.put_resource_policy(policyDocument="{}", resourceArn=arn)
+ assert refused(logs.put_resource_policy, policyDocument="{}") == conflict
+ assert refused(logs.put_resource_policy, policyDocument="{}", expectedRevisionId="7") == invalid
+ assert refused(logs.delete_resource_policy) == invalid
+ assert refused(logs.delete_resource_policy, expectedRevisionId="7") == invalid
+ finally:
+ logs.delete_log_group(logGroupName=group)
+
+
[email protected]("call, action", [
+ ("put_resource_policy", "PutResourcePolicy"),
+ ("delete_resource_policy", "DeleteResourcePolicy"),
+])
+def test_logs_resource_policy_needs_name_or_resource_arn(logs, call, action):
+ kwargs = {"policyDocument": "{}"} if call == "put_resource_policy" else {}
+ with pytest.raises(ClientError) as exc:
+ getattr(logs, call)(**kwargs)
+ assert exc.value.response["Error"]["Code"] == "InvalidParameterException"
+ assert exc.value.response["Error"]["Message"] == f"{action} request must contain policy name or resource arn."
+
+
+def test_logs_resource_policy_error_messages(logs):
+ with pytest.raises(ClientError) as exc:
+ logs.delete_resource_policy(policyName="does-not-exist")
+ assert exc.value.response["Error"]["Message"] == "Policy with name [does-not-exist] does not exist."
+ with pytest.raises(ClientError) as exc:
+ logs.describe_resource_policies(policyScope="OTHER")
+ assert exc.value.response["Error"]["Message"] == (
+ "1 validation error detected: Value at 'policyScope' failed to satisfy constraint: "
+ "Member must satisfy enum value set: [ACCOUNT, RESOURCE]")
+
+
@pytest.mark.serial
def test_logs_account_resource_policies_are_limited_to_ten(logs):
existing = len(logs.describe_resource_policies()["resourcePolicies"])