Skip to content

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

fix():1322-1345-1359 (#1373)

Nahuel Nucera561af3489e50
diff --git a/ministack/app.py b/ministack/app.py
index 5bee83e6..905559db 100644
--- a/ministack/app.py
+++ b/ministack/app.py
@@ -345,6 +345,7 @@ SERVICE_HANDLERS = {
# tests/test_persistence.py.
_state_map = {
"apigateway": "apigateway", "apigateway_v1": "apigateway_v1",
+ "cloudformation": "cloudformation",
"sqs": "sqs", "sns": "sns", "ssm": "ssm",
"secretsmanager": "secretsmanager", "iam": "iam",
"dynamodb": "dynamodb", "kms": "kms", "eventbridge": "eventbridge",
diff --git a/ministack/services/cloudformation/__init__.py b/ministack/services/cloudformation/__init__.py
index 65d4222d..0de1b134 100644
--- a/ministack/services/cloudformation/__init__.py
+++ b/ministack/services/cloudformation/__init__.py
@@ -9,11 +9,13 @@ Supports: CreateStack, UpdateStack, DeleteStack, DescribeStacks, ListStacks,
Uses Query API (Action=...) with form-encoded body.
"""
+import copy
import json
import logging
import os
from urllib.parse import parse_qs
+from ministack.core.persistence import load_state
from ministack.core.responses import AccountRegionScopedDict
logger = logging.getLogger("cloudformation")
@@ -78,7 +80,52 @@ def reset():
_cr.reset()
+# Stores that need to survive a PERSIST_STATE=1 stop/restore cycle. The actual
+# provisioned resources (buckets, functions, tables, …) are persisted by their
+# own services; only the CloudFormation metadata (stack records, events,
+# exports, and change sets) lived nowhere, so ListStacks/DescribeStacks/
+# ListExports came back empty after a warm boot. (#1345, item 8)
+_PERSISTED_STORES = (
+ (lambda: _stacks, "stacks"),
+ (lambda: _stack_events, "stack_events"),
+ (lambda: _exports, "exports"),
+ (lambda: _change_sets, "change_sets"),
+)
+
+
+def get_state():
+ return {key: copy.deepcopy(store()) for store, key in _PERSISTED_STORES}
+
+
+def restore_state(data):
+ if not data:
+ return
+ for store, key in _PERSISTED_STORES:
+ restored = data.get(key)
+ target = store()
+ if isinstance(restored, AccountRegionScopedDict):
+ # Merge the (account, region, key)-scoped entries directly. Restore
+ # runs at import time with no request scope, so re-scoping through
+ # the public dict interface would misattribute every entry.
+ target._data.update(restored._data)
+ elif isinstance(restored, dict):
+ for k, v in restored.items():
+ target[k] = v
+
+
# Must be last — handlers imports from this module
from ministack.core.responses import get_account_id
from .handlers import _ACTION_HANDLERS, _validate_template # noqa: E402
+
+# Restore persisted stack metadata on first import (a CloudFormation request, or
+# the eager boot import when a state file exists). Failure falls back to a fresh
+# store rather than blocking startup.
+try:
+ _restored = load_state("cloudformation")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ logger.exception(
+ "Failed to restore persisted CloudFormation state; continuing with a fresh store"
+ )
diff --git a/ministack/services/cloudformation/provisioners.py b/ministack/services/cloudformation/provisioners.py
index 9f1600f9..1210119b 100644
--- a/ministack/services/cloudformation/provisioners.py
+++ b/ministack/services/cloudformation/provisioners.py
@@ -295,6 +295,74 @@ def _opensearch_domain_delete(physical_id, props):
# --- S3 Bucket ---
+def _s3_notification_json_to_xml(notif: dict) -> bytes:
+ """Serialize an ``AWS::S3::Bucket`` ``NotificationConfiguration`` (CloudFormation
+ JSON) into the S3 REST XML that ``_put_bucket_notification`` parses. The property
+ names differ between the CloudFormation resource and the S3 API:
+ ``Function``→``LambdaFunctionArn``, ``Event`` (single string)→``Event``,
+ ``Filter.S3Key.Rules``→``Filter.S3Key.FilterRule``; ``Queue``/``Topic`` keep their
+ names. ``EventBridgeConfiguration.EventBridgeEnabled`` (bool) becomes the S3 API's
+ presence-only empty element."""
+ from xml.etree.ElementTree import Element, SubElement, tostring
+
+ root = Element("NotificationConfiguration", xmlns=_s3.S3_NS)
+
+ def _events(entry):
+ ev = entry.get("Event")
+ if isinstance(ev, list):
+ return [str(e) for e in ev if e]
+ return [str(ev)] if ev else []
+
+ def _add_common(cfg_el, entry):
+ if entry.get("Id"):
+ SubElement(cfg_el, "Id").text = str(entry["Id"])
+ for ev in _events(entry):
+ SubElement(cfg_el, "Event").text = ev
+ s3key = ((entry.get("Filter") or {}).get("S3Key") or {})
+ rules = s3key.get("Rules") or []
+ if rules:
+ s3key_el = SubElement(SubElement(cfg_el, "Filter"), "S3Key")
+ for rule in rules:
+ rule_el = SubElement(s3key_el, "FilterRule")
+ SubElement(rule_el, "Name").text = str(rule.get("Name", ""))
+ SubElement(rule_el, "Value").text = str(rule.get("Value", ""))
+
+ for entry in notif.get("LambdaConfigurations") or []:
+ cfg = SubElement(root, "LambdaFunctionConfiguration")
+ SubElement(cfg, "LambdaFunctionArn").text = str(entry.get("Function", ""))
+ _add_common(cfg, entry)
+ for entry in notif.get("QueueConfigurations") or []:
+ cfg = SubElement(root, "QueueConfiguration")
+ SubElement(cfg, "Queue").text = str(entry.get("Queue", ""))
+ _add_common(cfg, entry)
+ for entry in notif.get("TopicConfigurations") or []:
+ cfg = SubElement(root, "TopicConfiguration")
+ SubElement(cfg, "Topic").text = str(entry.get("Topic", ""))
+ _add_common(cfg, entry)
+
+ eb = notif.get("EventBridgeConfiguration")
+ if eb is not None:
+ enabled = eb.get("EventBridgeEnabled", True) if isinstance(eb, dict) else bool(eb)
+ if enabled:
+ SubElement(root, "EventBridgeConfiguration")
+
+ return tostring(root, encoding="utf-8")
+
+
+def _s3_apply_notification(name, notif):
+ """Route a CloudFormation ``NotificationConfiguration`` through the same S3 API
+ path a ``PutBucketNotificationConfiguration`` call takes — validation, the
+ ``s3:TestEvent``, storage, and the delivery wiring — so the two paths cannot
+ drift. An invalid destination fails the stack loudly instead of silently, which
+ is the whole point of the bug report."""
+ xml = _s3_notification_json_to_xml(notif or {})
+ result = _s3._put_bucket_notification(name, xml)
+ if result[0] >= 400:
+ raise ValueError(
+ f"AWS::S3::Bucket NotificationConfiguration rejected: {result[2]!r}"
+ )
+
+
def _s3_create(logical_id, props, stack_name):
name = props.get("BucketName") or _physical_name(stack_name, logical_id, lowercase=True, max_len=63)
_s3._buckets.setdefault(name, {
@@ -305,6 +373,8 @@ def _s3_create(logical_id, props, stack_name):
versioning = props.get("VersioningConfiguration", {})
if versioning.get("Status") == "Enabled":
_s3._bucket_versioning[name] = "Enabled"
+ if "NotificationConfiguration" in props:
+ _s3_apply_notification(name, props["NotificationConfiguration"])
attrs = {
"Arn": f"arn:aws:s3:::{name}",
"DomainName": f"{name}.s3.amazonaws.com",
@@ -329,6 +399,11 @@ def _s3_update(physical_id, old_props, new_props, stack_name):
versioning = new_props.get("VersioningConfiguration", {})
if versioning.get("Status") == "Enabled":
_s3._bucket_versioning[name] = "Enabled"
+ if "NotificationConfiguration" in new_props:
+ _s3_apply_notification(name, new_props["NotificationConfiguration"])
+ elif "NotificationConfiguration" in old_props:
+ # Property removed on update — clear the configuration, as AWS does.
+ _s3._bucket_notifications.pop(name, None)
else:
return _s3_create(name, new_props, stack_name)
attrs = {
@@ -5443,6 +5518,84 @@ def _firehose_delivery_stream_delete(physical_id, props):
_firehose._delete_delivery_stream({"DeliveryStreamName": physical_id})
+# --- IoT ThingType / Policy, Cognito IdentityPoolRoleAttachment,
+# Lambda LayerVersionPermission (#1345, item 5) ---
+# Each maps onto the service's own control-plane create, so the resource is
+# readable back through its real API instead of the stack rolling back.
+
+
+def _iot_thing_type_create(logical_id, props, stack_name):
+ name = props.get("ThingTypeName") or _physical_name(stack_name, logical_id)
+ payload = {"thingTypeProperties": _pascal_to_camel(props.get("ThingTypeProperties") or {})}
+ resp = _iot._create_thing_type(name, payload)
+ if resp[0] >= 400:
+ raise ValueError(f"AWS::IoT::ThingType create failed: {resp[2]!r}")
+ rec = _iot._thing_types.get(name) or {}
+ return name, {"Arn": rec.get("thingTypeArn", _iot._thing_type_arn(name)),
+ "Id": rec.get("thingTypeId", "")}
+
+
+def _iot_thing_type_delete(physical_id, props):
+ _iot._thing_types.pop(physical_id, None)
+
+
+def _iot_policy_create(logical_id, props, stack_name):
+ name = props.get("PolicyName") or _physical_name(stack_name, logical_id)
+ doc = props.get("PolicyDocument")
+ if isinstance(doc, (dict, list)):
+ doc = json.dumps(doc)
+ resp = _iot._create_policy(name, {"policyDocument": doc})
+ if resp[0] >= 400:
+ raise ValueError(f"AWS::IoT::Policy create failed: {resp[2]!r}")
+ return name, {"Arn": _iot._policy_arn(name), "Id": name}
+
+
+def _iot_policy_delete(physical_id, props):
+ _iot._policies.pop(physical_id, None)
+
+
+def _cognito_identity_pool_role_attachment_create(logical_id, props, stack_name):
+ iid = props.get("IdentityPoolId")
+ if not iid:
+ raise ValueError("AWS::Cognito::IdentityPoolRoleAttachment requires IdentityPoolId")
+ resp = _cognito._set_identity_pool_roles({"IdentityPoolId": iid, "Roles": props.get("Roles", {})})
+ if resp[0] >= 400:
+ raise ValueError(f"AWS::Cognito::IdentityPoolRoleAttachment create failed: {resp[2]!r}")
+ return iid, {"Id": iid}
+
+
+def _cognito_identity_pool_role_attachment_delete(physical_id, props):
+ pool = _cognito._identity_pools.get(physical_id)
+ if pool is not None:
+ pool["_roles"] = {}
+
+
+def _lambda_layer_version_permission_create(logical_id, props, stack_name):
+ arn = props.get("LayerVersionArn", "")
+ m = re.search(r":layer:([^:]+):(\d+)$", arn)
+ if not m:
+ raise ValueError(
+ f"AWS::Lambda::LayerVersionPermission: unparseable LayerVersionArn {arn!r}")
+ layer_name, version = m.group(1), int(m.group(2))
+ sid = props.get("StatementId", "")
+ data = {"Action": props.get("Action", ""), "StatementId": sid,
+ "Principal": props.get("Principal", "*")}
+ if props.get("OrganizationId"):
+ data["OrganizationId"] = props["OrganizationId"]
+ resp = _lambda_svc._add_layer_version_permission(layer_name, version, data)
+ if resp[0] >= 400:
+ raise ValueError(f"AWS::Lambda::LayerVersionPermission create failed: {resp[2]!r}")
+ # Ref is the layer-version ARN and statement id joined by '#', as AWS returns.
+ return f"{arn}#{sid}", {}
+
+
+def _lambda_layer_version_permission_delete(physical_id, props):
+ arn, _, sid = physical_id.partition("#")
+ m = re.search(r":layer:([^:]+):(\d+)$", arn)
+ if m:
+ _lambda_svc._remove_layer_version_permission(m.group(1), int(m.group(2)), sid)
+
+
_RESOURCE_HANDLERS = {
"AWS::OpenSearchService::Domain": {
"create": _opensearch_domain_create,
@@ -5655,6 +5808,16 @@ _RESOURCE_HANDLERS = {
"AWS::RDS::DBCluster": {"create": _rds_db_cluster_create, "delete": _rds_db_cluster_delete},
"AWS::RDS::DBInstance": {"create": _rds_db_instance_create, "delete": _rds_db_instance_delete},
"AWS::IoT::TopicRule": {"create": _iot_topic_rule_create, "delete": _iot_topic_rule_delete},
+ "AWS::IoT::ThingType": {"create": _iot_thing_type_create, "delete": _iot_thing_type_delete},
+ "AWS::IoT::Policy": {"create": _iot_policy_create, "delete": _iot_policy_delete},
+ "AWS::Cognito::IdentityPoolRoleAttachment": {
+ "create": _cognito_identity_pool_role_attachment_create,
+ "delete": _cognito_identity_pool_role_attachment_delete,
+ },
+ "AWS::Lambda::LayerVersionPermission": {
+ "create": _lambda_layer_version_permission_create,
+ "delete": _lambda_layer_version_permission_delete,
+ },
# EventBridge Scheduler
"AWS::Scheduler::Schedule": {"create": _scheduler_schedule_create, "delete": _scheduler_schedule_delete},
"AWS::Scheduler::ScheduleGroup": {"create": _scheduler_group_create, "delete": _scheduler_group_delete},
diff --git a/ministack/services/s3.py b/ministack/services/s3.py
index 605073db..ec947a6c 100644
--- a/ministack/services/s3.py
+++ b/ministack/services/s3.py
@@ -275,7 +275,11 @@ def _validate_bucket_name(name: str) -> bool:
def _url_encode(value: str) -> str:
- return url_quote(value, safe="")
+ # S3's `encoding-type=url` percent-encodes key names (spaces, `+`, unicode,
+ # control chars) but leaves the forward slash intact, so a delimiter-collapsed
+ # `CommonPrefixes`/`Delimiter`/`Key` keeps its `/` separators readable — matching
+ # real S3 and RGW. Encoding `/` as %2F broke folder-tree listings. (#1322)
+ return url_quote(value, safe="/")
def _parse_bucket_key(path: str, headers: dict):
@@ -409,11 +413,18 @@ def _validate_content_md5(headers: dict, body: bytes):
if not md5_header:
return None
try:
- expected = base64.b64decode(md5_header)
+ expected = base64.b64decode(md5_header, validate=True)
except Exception:
return _error(
"InvalidDigest", "The Content-MD5 you specified is not valid.", 400
)
+ # A valid MD5 digest is exactly 16 bytes. A wrong-length (e.g. truncated) value
+ # is malformed, so AWS answers InvalidDigest, not BadDigest (which is reserved
+ # for a well-formed digest that simply doesn't match the body). (#1322)
+ if len(expected) != 16:
+ return _error(
+ "InvalidDigest", "The Content-MD5 you specified is not valid.", 400
+ )
actual = hashlib.md5(body).digest()
if expected != actual:
return _error(
@@ -943,7 +954,7 @@ def _dispatch(
if "uploads" in query_params:
return _create_multipart_upload(bucket, key, headers)
if "uploadId" in query_params:
- return _complete_multipart_upload(bucket, key, body, query_params)
+ return _complete_multipart_upload(bucket, key, body, query_params, headers)
return _error(
"MethodNotAllowed",
"The specified method is not allowed against this resource.",
@@ -1315,6 +1326,39 @@ def _delete_bucket_encryption(name: str):
return 204, {}, b""
+def _bucket_default_sse_headers(bucket_name: str) -> dict:
+ """SSE headers a PutObject echoes when the bucket has explicit default
+ encryption (PutBucketEncryption). Real S3 stamps the applied algorithm on the
+ reply (`x-amz-server-side-encryption`, plus the KMS key id for `aws:kms`), which
+ lets a client confirm the object was encrypted as configured. (#1322)"""
+ raw = _bucket_encryption.get(bucket_name)
+ if not raw:
+ return {}
+ try:
+ root = fromstring(raw)
+ except Exception:
+ return {}
+
+ def _first_text(local_name):
+ # SSEAlgorithm / KMSMasterKeyID sit under Rule/ApplyServerSideEncryptionByDefault,
+ # so search descendants by local name (namespace-agnostic), not direct children.
+ for el in root.iter():
+ tag = el.tag.split("}")[-1] if "}" in el.tag else el.tag
+ if tag == local_name and el.text and el.text.strip():
+ return el.text.strip()
+ return None
+
+ algo = _first_text("SSEAlgorithm")
+ if not algo:
+ return {}
+ out = {"x-amz-server-side-encryption": algo}
+ if algo == "aws:kms":
+ kms = _first_text("KMSMasterKeyID")
+ if kms:
+ out["x-amz-server-side-encryption-aws-kms-key-id"] = kms
+ return out
+
+
def _get_bucket_lifecycle(name: str):
if name not in _buckets:
return _no_such_bucket(name)
@@ -2467,9 +2511,18 @@ def _put_object(bucket_name: str, key: str, body: bytes, headers: dict):
if csum_err:
return csum_err
+ # A canned ACL supplied at PutObject time is validated up front (an invalid
+ # value rejects the whole request, as AWS does) and applied below, so
+ # GetObjectAcl reflects it instead of dropping it. (#1322, rest of defect 9)
+ canned_acl = headers.get("x-amz-acl")
+ if canned_acl and canned_acl not in _CANNED_OBJECT_ACLS:
+ return _error("InvalidArgument", f"Invalid x-amz-acl value: {canned_acl}", 400)
+
etag = f'"{md5_hash(body)}"'
obj = _build_object_record(body, headers, etag=etag, checksums=checksums)
bucket["objects"][key] = obj
+ if canned_acl:
+ _object_acl[(bucket_name, key)] = _canned_acl_policy_xml(canned_acl, get_account_id())
# --- Object Lock headers on PutObject ---
_apply_object_lock_from_headers(bucket_name, key, headers)
@@ -2489,6 +2542,7 @@ def _put_object(bucket_name: str, key: str, body: bytes, headers: dict):
)
resp_headers = {"ETag": obj["etag"], "Content-Length": "0"}
+ resp_headers.update(_bucket_default_sse_headers(bucket_name))
if _bucket_versioning.get(bucket_name) in ("Enabled", "Suspended"):
version_id = new_uuid()
obj["version_id"] = version_id
@@ -3255,6 +3309,33 @@ def _check_object_lock(bucket_name: str, key: str, headers: dict) -> tuple | Non
return None
+def _parse_http_date(value: str):
+ """Parse an RFC 7231 HTTP-date header into a tz-aware UTC datetime, or None."""
+ from email.utils import parsedate_to_datetime
+ try:
+ dt = parsedate_to_datetime(value)
+ except (TypeError, ValueError):
+ return None
+ if dt is None:
+ return None
+ if dt.tzinfo is None:
+ dt = dt.replace(tzinfo=_dt.timezone.utc)
+ return dt
+
+
+def _object_mtime_dt(obj: dict):
+ """The object's Last-Modified as a tz-aware datetime at second granularity
+ (Last-Modified is second-precise, so preconditions must compare at that resolution)."""
+ iso = obj.get("last_modified")
+ if not iso:
+ return None
+ try:
+ dt = _dt.datetime.fromisoformat(iso.replace("Z", "+00:00"))
+ except ValueError:
+ return None
+ return dt.replace(microsecond=0)
+
+
def _copy_object(bucket_name: str, dest_key: str, headers: dict):
source = url_unquote(headers.get("x-amz-copy-source", "").lstrip("/"))
src_path, _, src_query = source.partition("?")
@@ -3320,23 +3401,32 @@ def _copy_object(bucket_name: str, dest_key: str, headers: dict):
# AWS echoes the copied source version on a versioned source.
copy_src_vid = src_version_id or src_obj.get("version_id")
- # Precondition: x-amz-copy-source-if-match
+ # Copy-source preconditions. Per the AWS CopyObject reference (RFC 7232
+ # precedence): x-amz-copy-source-if-match takes precedence over
+ # -if-unmodified-since, and -if-none-match over -if-modified-since, so the
+ # date header only applies when its ETag counterpart is absent.
+ _precond_msg = "At least one of the pre-conditions you specified did not hold"
+ src_mtime = _object_mtime_dt(src_obj)
+
if_match = headers.get("x-amz-copy-source-if-match", "")
- if if_match and if_match.strip('"') != src_obj["etag"].strip('"'):
- return _error(
- "PreconditionFailed",
- "At least one of the pre-conditions you specified did not hold",
- 412,
- )
+ if if_match:
+ if if_match.strip('"') != src_obj["etag"].strip('"'):
+ return _error("PreconditionFailed", _precond_msg, 412)
+ else:
+ unmod = _parse_http_date(headers.get("x-amz-copy-source-if-unmodified-since", ""))
+ # "Unmodified since" fails when the source was modified after the given time.
+ if unmod and src_mtime and src_mtime > unmod:
+ return _error("PreconditionFailed", _precond_msg, 412)
- # Precondition: x-amz-copy-source-if-none-match — 412 for PUT-like operations.
if_none_match = headers.get("x-amz-copy-source-if-none-match", "")
- if if_none_match and if_none_match.strip('"') == src_obj["etag"].strip('"'):
- return _error(
- "PreconditionFailed",
- "At least one of the pre-conditions you specified did not hold",
- 412,
- )
+ if if_none_match:
+ if if_none_match.strip('"') == src_obj["etag"].strip('"'):
+ return _error("PreconditionFailed", _precond_msg, 412)
+ else:
+ mod = _parse_http_date(headers.get("x-amz-copy-source-if-modified-since", ""))
+ # "Modified since" fails when the source has NOT changed since the given time.
+ if mod and src_mtime and src_mtime <= mod:
+ return _error("PreconditionFailed", _precond_msg, 412)
directive = headers.get("x-amz-metadata-directive", "COPY").upper()
if directive == "REPLACE":
@@ -3839,6 +3929,48 @@ _CANNED_OBJECT_ACLS = {
}
+_ACL_GROUP_ALL_USERS = "http://acs.amazonaws.com/groups/global/AllUsers"
+_ACL_GROUP_AUTH_USERS = "http://acs.amazonaws.com/groups/global/AuthenticatedUsers"
+
+# Group grants each canned ACL adds on top of the owner's FULL_CONTROL, per the
+# AWS S3 "Canned ACL" reference. The bucket-owner-* / aws-exec-read variants add
+# only owner/bucket-owner grants, which collapse to the owner in MiniStack's
+# single-account model, so they carry no extra group grant here.
+_CANNED_ACL_GROUP_GRANTS = {
+ "public-read": [(_ACL_GROUP_ALL_USERS, "READ")],
+ "public-read-write": [(_ACL_GROUP_ALL_USERS, "READ"), (_ACL_GROUP_ALL_USERS, "WRITE")],
+ "authenticated-read": [(_ACL_GROUP_AUTH_USERS, "READ")],
+}
+
+
+def _canned_acl_policy_xml(canned: str, owner_id: str) -> str:
+ """AccessControlPolicy XML for a canned ACL: the owner's FULL_CONTROL plus the
+ group grants the canned value implies (e.g. public-read grants AllUsers READ),
+ so GetObjectAcl reflects what the canned ACL actually means rather than a bare
+ owner grant. (#1322, rest of defect 9)"""
+ grants = [
+ '<Grant>'
+ '<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
+ 'xsi:type="CanonicalUser">'
+ f"<ID>{owner_id}</ID><DisplayName>ministack</DisplayName></Grantee>"
+ "<Permission>FULL_CONTROL</Permission></Grant>"
+ ]
+ for uri, perm in _CANNED_ACL_GROUP_GRANTS.get(canned, []):
+ grants.append(
+ '<Grant>'
+ '<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
+ f'xsi:type="Group"><URI>{uri}</URI></Grantee>'
+ f"<Permission>{perm}</Permission></Grant>"
+ )
+ return (
+ XML_DECL.decode() + "\n"
+ f'<AccessControlPolicy xmlns="{S3_NS}">'
+ f"<Owner><ID>{owner_id}</ID><DisplayName>ministack</DisplayName></Owner>"
+ f'<AccessControlList>{"".join(grants)}</AccessControlList>'
+ "</AccessControlPolicy>"
+ )
+
+
def _default_object_acl_xml() -> bytes:
"""Default ACL real AWS returns when no ACL has been set on an object:
a single Grant of FULL_CONTROL to the bucket owner (CanonicalUser).
@@ -3898,20 +4030,7 @@ def _put_object_acl(bucket_name: str, key: str, body: bytes, headers: dict):
if canned not in _CANNED_OBJECT_ACLS:
return _error("InvalidArgument",
f"Invalid x-amz-acl value: {canned}", 400)
- owner_id = get_account_id()
- _object_acl[(bucket_name, key)] = (
- XML_DECL.decode() + "\n"
- f'<AccessControlPolicy xmlns="{S3_NS}">'
- f"<Owner><ID>{owner_id}</ID>"
- f"<DisplayName>ministack</DisplayName></Owner>"
- f'<AccessControlList><!-- canned: {canned} --><Grant>'
- f'<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
- f'xsi:type="CanonicalUser">'
- f"<ID>{owner_id}</ID>"
- f"<DisplayName>ministack</DisplayName></Grantee>"
- f"<Permission>FULL_CONTROL</Permission></Grant></AccessControlList>"
- f"</AccessControlPolicy>"
- )
+ _object_acl[(bucket_name, key)] = _canned_acl_policy_xml(canned, get_account_id())
return 200, {}, b""
if not body:
@@ -4054,54 +4173,47 @@ def _delete_bucket_replication(bucket_name: str):
def _collect_list_entries(
bucket_objects: dict, prefix: str, delimiter: str, max_keys: int, start_after: str
):
- """Walk sorted keys, collecting contents and common prefixes with correct
- pagination. When a key falls under a common-prefix group the iterator
- advances past *all* remaining keys in that group so the next page's
- marker cleanly skips the entire prefix.
+ """Collect contents and common prefixes with pagination as an ordered list of
+ "rows": a delimiter-collapsed key becomes a single common-prefix row (value =
+ the prefix), any other key is its own contents row (value = the key). Rows are
+ globally sorted, ``marker`` excludes rows at or before it, and ``next_marker``
+ is the value of the LAST row emitted — so when a page ends on a collapsed group
+ the continuation marker is the prefix (e.g. ``boo/``), not an underlying key
+ (``boo/baz/xyzzy``), and resuming from it skips the whole group instead of
+ re-walking it. (#1322, defect 5)
Returns (contents, common_prefixes, is_truncated, next_marker).
"""
- all_keys = sorted(
- k for k in bucket_objects if k.startswith(prefix) and k > start_after
- )
- contents: list[str] = []
- common_prefixes: set[str] = set()
- is_truncated = False
- count = 0
- next_marker = ""
-
- i = 0
- while i < len(all_keys):
- k = all_keys[i]
-
+ rows: list[tuple[str, bool]] = [] # (value, is_common_prefix)
+ seen_prefixes: set[str] = set()
+ for k in sorted(k for k in bucket_objects if k.startswith(prefix)):
if delimiter:
- suffix = k[len(prefix) :]
+ suffix = k[len(prefix):]
delim_idx = suffix.find(delimiter)
if delim_idx >= 0:
cp = prefix + suffix[: delim_idx + len(delimiter)]
- is_new_prefix = cp not in common_prefixes
- if is_new_prefix:
- if count >= max_keys:
- is_truncated = True
- break
- common_prefixes.add(cp)
- count += 1
- # Advance past every remaining key belonging to this prefix
- # group so the marker lands after the whole group.
- next_marker = k
- i += 1
- while i < len(all_keys) and all_keys[i].startswith(cp):
- next_marker = all_keys[i]
- i += 1
+ if cp not in seen_prefixes:
+ seen_prefixes.add(cp)
+ rows.append((cp, True))
continue
+ rows.append((k, False))
- if count >= max_keys:
+ if start_after:
+ rows = [row for row in rows if row[0] > start_after]
+
+ contents: list[str] = []
+ common_prefixes: list[str] = []
+ is_truncated = False
+ next_marker = ""
+ for i, (value, is_prefix) in enumerate(rows):
+ if i >= max_keys:
is_truncated = True
break
- contents.append(k)
- count += 1
- next_marker = k
- i += 1
+ if is_prefix:
+ common_prefixes.append(value)
+ else:
+ contents.append(value)
+ next_marker = value
return contents, common_prefixes, is_truncated, next_marker
@@ -4511,7 +4623,7 @@ def _upload_part_copy(bucket_name: str, dest_key: str, query_params: dict, heade
def _complete_multipart_upload(
- bucket_name: str, key: str, body: bytes, query_params: dict
+ bucket_name: str, key: str, body: bytes, query_params: dict, headers: dict | None = None
):
bucket = _ensure_bucket(bucket_name)
if bucket is None:
@@ -4649,7 +4761,15 @@ def _complete_multipart_upload(
_persist_object(bucket_name, key, obj)
root = Element("CompleteMultipartUploadResult", xmlns=S3_NS)
- s3_host = os.environ.get("MINISTACK_HOST", os.environ.get("AWS_ENDPOINT_URL", "http://localhost:4566"))
+ # Location echoes the endpoint the client actually reached (real S3 returns the
+ # request host), so it is correct on any port/host — not a hard-coded 4566. Falls
+ # back to the configured host only when the request carried no Host header. (#1322)
+ request_host = (headers or {}).get("host")
+ if request_host:
+ scheme = (headers or {}).get("x-forwarded-proto") or "http"
+ s3_host = f"{scheme}://{request_host}"
+ else:
+ s3_host = os.environ.get("MINISTACK_HOST", os.environ.get("AWS_ENDPOINT_URL", "http://localhost:4566"))
SubElement(root, "Location").text = f"{s3_host}/{bucket_name}/{key}"
SubElement(root, "Bucket").text = bucket_name
SubElement(root, "Key").text = key
diff --git a/tests/test_cfn.py b/tests/test_cfn.py
index 4bcfb019..126c1566 100644
--- a/tests/test_cfn.py
+++ b/tests/test_cfn.py
@@ -450,6 +450,133 @@ def test_cfn_create_describe_delete_stack(cfn, s3):
s3.head_bucket(Bucket="cfn-t01-bucket")
+def test_cfn_s3_bucket_notification_configuration(cfn, s3, sqs):
+ """AWS::S3::Bucket NotificationConfiguration is applied, not silently dropped:
+ it round-trips through GetBucketNotificationConfiguration (with the
+ CloudFormation property names translated to the S3 API's), an upload matching
+ the event and key filter is delivered to the target, and removing the property
+ on a stack update clears the configuration. (#1359)
+ """
+ queue_url = sqs.create_queue(QueueName="cfn-notif-q")["QueueUrl"]
+ queue_arn = sqs.get_queue_attributes(
+ QueueUrl=queue_url, AttributeNames=["QueueArn"],
+ )["Attributes"]["QueueArn"]
+
+ def template(with_notif):
+ props = {"BucketName": "cfn-notif-bucket"}
+ if with_notif:
+ props["NotificationConfiguration"] = {
+ "QueueConfigurations": [{
+ "Queue": queue_arn,
+ "Event": "s3:ObjectCreated:*",
+ "Filter": {"S3Key": {"Rules": [{"Name": "suffix", "Value": ".csv"}]}},
+ }],
+ }
+ return json.dumps({
+ "AWSTemplateFormatVersion": "2010-09-09",
+ "Resources": {"Bucket": {"Type": "AWS::S3::Bucket", "Properties": props}},
+ })
+
+ cfn.create_stack(StackName="cfn-notif", TemplateBody=template(True))
+ assert _wait_stack(cfn, "cfn-notif")["StackStatus"] == "CREATE_COMPLETE"
+
+ # The property survived: the config is readable back, with the CloudFormation
+ # names (Queue/Event/Rules) translated to the S3 API's (QueueArn/Events/Key).
+ qcfgs = s3.get_bucket_notification_configuration(
+ Bucket="cfn-notif-bucket")["QueueConfigurations"]
+ assert len(qcfgs) == 1
+ assert qcfgs[0]["QueueArn"] == queue_arn
+ assert qcfgs[0]["Events"] == ["s3:ObjectCreated:*"]
+ assert qcfgs[0]["Filter"]["Key"]["FilterRules"] == [{"Name": "suffix", "Value": ".csv"}]
+
+ # Delivery works end to end through the CloudFormation path, honouring the filter.
+ s3.put_object(Bucket="cfn-notif-bucket", Key="skip.txt", Body=b"no")
+ s3.put_object(Bucket="cfn-notif-bucket", Key="take.csv", Body=b"yes")
+ time.sleep(0.5)
+ msgs = sqs.receive_message(
+ QueueUrl=queue_url, MaxNumberOfMessages=10, WaitTimeSeconds=2)
+ keys = [
+ json.loads(m["Body"])["Records"][0]["s3"]["object"]["key"]
+ for m in msgs.get("Messages", []) if "Records" in json.loads(m["Body"])
+ ]
+ assert "take.csv" in keys
+ assert "skip.txt" not in keys
+
+ # Removing the property on an update clears the configuration.
+ cfn.update_stack(StackName="cfn-notif", TemplateBody=template(False))
+ assert _wait_stack(cfn, "cfn-notif")["StackStatus"] == "UPDATE_COMPLETE"
+ cleared = s3.get_bucket_notification_configuration(Bucket="cfn-notif-bucket")
+ assert not cleared.get("QueueConfigurations")
+
+ cfn.delete_stack(StackName="cfn-notif")
+ _wait_stack(cfn, "cfn-notif")
+
+
+def test_cfn_iot_and_cognito_role_attachment(cfn, iot_client, cognito_identity):
+ """AWS::IoT::ThingType, AWS::IoT::Policy, and
+ AWS::Cognito::IdentityPoolRoleAttachment provision onto their real services
+ instead of rolling the stack back — each is readable through its own API. (#1345, item 5)
+ """
+ template = {
+ "AWSTemplateFormatVersion": "2010-09-09",
+ "Resources": {
+ "TT": {"Type": "AWS::IoT::ThingType", "Properties": {
+ "ThingTypeName": "cfn-tt",
+ "ThingTypeProperties": {"ThingTypeDescription": "d", "SearchableAttributes": ["room"]}}},
+ "Pol": {"Type": "AWS::IoT::Policy", "Properties": {
+ "PolicyName": "cfn-pol",
+ "PolicyDocument": {"Version": "2012-10-17", "Statement": [
+ {"Effect": "Allow", "Action": "iot:Connect", "Resource": "*"}]}}},
+ "Pool": {"Type": "AWS::Cognito::IdentityPool", "Properties": {
+ "IdentityPoolName": "cfn-pool", "AllowUnauthenticatedIdentities": True}},
+ "Roles": {"Type": "AWS::Cognito::IdentityPoolRoleAttachment", "Properties": {
+ "IdentityPoolId": {"Ref": "Pool"},
+ "Roles": {"authenticated": "arn:aws:iam::000000000000:role/auth"}}},
+ },
+ "Outputs": {"PoolId": {"Value": {"Ref": "Pool"}}},
+ }
+ cfn.create_stack(StackName="cfn-iot-cog", TemplateBody=json.dumps(template))
+ stack = _wait_stack(cfn, "cfn-iot-cog")
+ assert stack["StackStatus"] == "CREATE_COMPLETE"
+
+ tt = iot_client.describe_thing_type(thingTypeName="cfn-tt")
+ assert tt["thingTypeProperties"]["searchableAttributes"] == ["room"]
+ pol = iot_client.get_policy(policyName="cfn-pol")
+ assert pol["policyArn"].endswith("policy/cfn-pol")
+
+ pool_id = next(o["OutputValue"] for o in stack["Outputs"] if o["OutputKey"] == "PoolId")
+ roles = cognito_identity.get_identity_pool_roles(IdentityPoolId=pool_id)["Roles"]
+ assert roles["authenticated"] == "arn:aws:iam::000000000000:role/auth"
+
+ cfn.delete_stack(StackName="cfn-iot-cog")
+ _wait_stack(cfn, "cfn-iot-cog")
+
+
+def test_cfn_lambda_layer_version_permission(cfn, lam):
+ """AWS::Lambda::LayerVersionPermission attaches a statement to the real
+ layer version's policy, readable via GetLayerVersionPolicy. (#1345, item 5)"""
+ buf = io.BytesIO()
+ with zipfile.ZipFile(buf, "w") as zf:
+ zf.writestr("lib.txt", "x")
+ layer = lam.publish_layer_version(
+ LayerName="cfn-perm-layer", Content={"ZipFile": buf.getvalue()})
+ arn = layer["LayerVersionArn"]
+
+ template = {"Resources": {"Perm": {
+ "Type": "AWS::Lambda::LayerVersionPermission", "Properties": {
+ "LayerVersionArn": arn, "Action": "lambda:GetLayerVersion",
+ "Principal": "123456789012", "StatementId": "cfn-sid"}}}}
+ cfn.create_stack(StackName="cfn-layer-perm", TemplateBody=json.dumps(template))
+ assert _wait_stack(cfn, "cfn-layer-perm")["StackStatus"] == "CREATE_COMPLETE"
+
+ pol = json.loads(lam.get_layer_version_policy(
+ LayerName="cfn-perm-layer", VersionNumber=1)["Policy"])
+ assert any(s["Sid"] == "cfn-sid" for s in pol["Statement"])
+
+ cfn.delete_stack(StackName="cfn-layer-perm")
+ _wait_stack(cfn, "cfn-layer-perm")
+
+
def test_cfn_deleted_stack_name_is_reusable(cfn):
"""A DELETE_COMPLETE stack is addressable only by stack ID; its name is free
to re-create, and an UpdateStack against the deleted name is "does not
diff --git a/tests/test_persistence.py b/tests/test_persistence.py
index 385c25f2..62d964a4 100644
--- a/tests/test_persistence.py
+++ b/tests/test_persistence.py
@@ -646,6 +646,36 @@ def test_backup_round_trip():
_round_trip("backup", "backup", populate, observe)
+def test_cloudformation_round_trip():
+ """CloudFormation stack metadata (stacks, events, exports, change sets)
+ survives a PERSIST_STATE stop/restore cycle — otherwise ListStacks /
+ DescribeStacks / ListExports come back empty after a warm boot even though
+ the provisioned resources persist. (#1345, item 8)"""
+ def populate(mod):
+ mod._stacks["stk-rt"] = {
+ "StackName": "stk-rt",
+ "StackId": "arn:aws:cloudformation:us-east-1:000000000000:stack/stk-rt/rt",
+ "StackStatus": "CREATE_COMPLETE",
+ "Outputs": [{"OutputKey": "K", "OutputValue": "V"}],
+ }
+ mod._stack_events["arn:aws:cloudformation:us-east-1:000000000000:stack/stk-rt/rt"] = [
+ {"ResourceStatus": "CREATE_COMPLETE"},
+ ]
+ mod._exports["exp-rt"] = {"Name": "exp-rt", "Value": "V", "ExportingStackId": "stk-rt"}
+ mod._change_sets["cs-rt"] = {"ChangeSetName": "cs-rt", "Status": "CREATE_COMPLETE"}
+
+ def observe(mod):
+ stack = mod._stacks.get("stk-rt")
+ assert stack is not None and stack["StackStatus"] == "CREATE_COMPLETE"
+ assert stack["Outputs"] == [{"OutputKey": "K", "OutputValue": "V"}]
+ assert mod._stack_events.get(
+ "arn:aws:cloudformation:us-east-1:000000000000:stack/stk-rt/rt") is not None
+ assert mod._exports.get("exp-rt") is not None
+ assert mod._change_sets.get("cs-rt") is not None
+
+ _round_trip("cloudformation", "cloudformation", populate, observe)
+
+
def test_backup_region_scoped_v3_state_is_idempotent(monkeypatch, tmp_path):
import json as _json
diff --git a/tests/test_s3.py b/tests/test_s3.py
index 923b1af3..0d5f7f39 100644
--- a/tests/test_s3.py
+++ b/tests/test_s3.py
@@ -4173,3 +4173,201 @@ def test_s3_head_object_specific_version(s3):
with pytest.raises(ClientError) as exc:
s3.head_object(Bucket=bucket, Key="k", VersionId="does-not-exist")
assert exc.value.response["Error"]["Code"] in ("NoSuchVersion", "404")
+
+
+def test_s3_list_encoding_type_url_preserves_slash(s3):
+ """`encoding-type=url` percent-encodes spaces and `+` but leaves `/` intact,
+ so delimiter-collapsed CommonPrefixes/Delimiter stay readable — matching real
+ S3 and RGW. (#1322, defect 4)"""
+ import urllib.request
+
+ bkt = f"enc-url-{_uuid_mod.uuid4().hex[:8]}"
+ s3.create_bucket(Bucket=bkt)
+ s3.put_object(Bucket=bkt, Key="foo+1/bar", Body=b"x")
+ s3.put_object(Bucket=bkt, Key="quux ab/xyz", Body=b"x")
+ s3.put_object(Bucket=bkt, Key="asdf", Body=b"x")
+
+ # Raw GET (boto3 auto-decodes the response, hiding the wire form). A dummy
+ # Authorization header keeps the request off the anonymous path; the signature
+ # is not verified.
+ url = f"{ENDPOINT}/{bkt}?delimiter=/&encoding-type=url"
+ req = urllib.request.Request(url, headers={
+ "Authorization": "AWS4-HMAC-SHA256 Credential=test/20240101/us-east-1/s3/aws4_request, "
+ "SignedHeaders=host, Signature=0",
+ })
+ body = urllib.request.urlopen(req, timeout=5).read().decode()
+
+ assert "<Delimiter>/</Delimiter>" in body, body
+ assert "<Prefix>foo%2B1/</Prefix>" in body, body
+ assert "<Prefix>quux%20ab/</Prefix>" in body, body
+ assert "%2F" not in body, f"forward slash was percent-encoded: {body}"
+
+
+def test_s3_complete_multipart_location_uses_request_host(s3):
+ """CompleteMultipartUpload's Location echoes the endpoint the client reached,
+ so it is correct on any port/host rather than a hard-coded localhost:4566.
+ (#1322, smaller wire omission)"""
+ from urllib.parse import urlparse
+
+ bkt = f"cmu-loc-{_uuid_mod.uuid4().hex[:8]}"
+ s3.create_bucket(Bucket=bkt)
+ u = s3.create_multipart_upload(Bucket=bkt, Key="k")["UploadId"]
+ etag = s3.upload_part(Bucket=bkt, Key="k", UploadId=u, PartNumber=1, Body=b"hello")["ETag"]
+ resp = s3.complete_multipart_upload(
+ Bucket=bkt, Key="k", UploadId=u,
+ MultipartUpload={"Parts": [{"ETag": etag, "PartNumber": 1}]})
+ loc = resp["Location"]
+ host = urlparse(ENDPOINT).netloc
+ assert host in loc, f"Location {loc!r} should reflect the request host {host!r}"
+ assert loc.endswith(f"/{bkt}/k"), loc
+
+
+def test_s3_content_md5_invalid_vs_bad_digest(s3):
+ """A malformed or wrong-length Content-MD5 is InvalidDigest; only a well-formed
+ 16-byte digest that mismatches the body is BadDigest. (#1322, smaller wire omission)"""
+ import base64
+ import hashlib
+
+ bkt = f"md5-{_uuid_mod.uuid4().hex[:8]}"
+ s3.create_bucket(Bucket=bkt)
+
+ # Wrong-length digest (valid base64, 3 bytes) -> InvalidDigest.
+ with pytest.raises(ClientError) as e1:
+ s3.put_object(Bucket=bkt, Key="k", Body=b"hello", ContentMD5="1234")
+ assert e1.value.response["Error"]["Code"] == "InvalidDigest", e1.value.response["Error"]
+
+ # Well-formed 16-byte digest that does not match the body -> BadDigest.
+ wrong = base64.b64encode(hashlib.md5(b"other").digest()).decode()
+ with pytest.raises(ClientError) as e2:
+ s3.put_object(Bucket=bkt, Key="k", Body=b"hello", ContentMD5=wrong)
+ assert e2.value.response["Error"]["Code"] == "BadDigest", e2.value.response["Error"]
+
+ # Correct digest succeeds.
+ good = base64.b64encode(hashlib.md5(b"hello").digest()).decode()
+ s3.put_object(Bucket=bkt, Key="k", Body=b"hello", ContentMD5=good)
+
+
+def test_s3_put_object_echoes_bucket_default_sse(s3):
+ """After PutBucketEncryption, PutObject echoes the applied SSE algorithm on the
+ reply (AES256, or aws:kms + key id). (#1322, smaller wire omission)"""
+ bkt = f"sse-{_uuid_mod.uuid4().hex[:8]}"
+ s3.create_bucket(Bucket=bkt)
+ s3.put_bucket_encryption(Bucket=bkt, ServerSideEncryptionConfiguration={
+ "Rules": [{"ApplyServerSideEncryptionByDefault": {"SSEAlgorithm": "AES256"}}]})
+ r = s3.put_object(Bucket=bkt, Key="k", Body=b"x")
+ assert r.get("ServerSideEncryption") == "AES256", r
+
+ bkt2 = f"sse-kms-{_uuid_mod.uuid4().hex[:8]}"
+ s3.create_bucket(Bucket=bkt2)
+ s3.put_bucket_encryption(Bucket=bkt2, ServerSideEncryptionConfiguration={
+ "Rules": [{"ApplyServerSideEncryptionByDefault": {
+ "SSEAlgorithm": "aws:kms",
+ "KMSMasterKeyID": "arn:aws:kms:us-east-1:000000000000:key/abc"}}]})
+ r2 = s3.put_object(Bucket=bkt2, Key="k", Body=b"x")
+ assert r2.get("ServerSideEncryption") == "aws:kms", r2
+ assert r2.get("SSEKMSKeyId", "").endswith("key/abc"), r2
+
+ # A bucket without explicit encryption does not stamp the header here.
+ bkt3 = f"noenc-{_uuid_mod.uuid4().hex[:8]}"
+ s3.create_bucket(Bucket=bkt3)
+ r3 = s3.put_object(Bucket=bkt3, Key="k", Body=b"x")
+ assert "ServerSideEncryption" not in r3, r3
+
+
+def test_s3_copy_object_source_date_preconditions(s3):
+ """CopyObject honours the copy-source date preconditions with AWS precedence:
+ If-Match beats If-Unmodified-Since, If-None-Match beats If-Modified-Since.
+ (#1322, rest of defect 7)"""
+ from datetime import datetime, timedelta, timezone
+
+ bkt = f"copycond-{_uuid_mod.uuid4().hex[:8]}"
+ s3.create_bucket(Bucket=bkt)
+ src_etag = s3.put_object(Bucket=bkt, Key="src", Body=b"data")["ETag"]
+ past = datetime.now(timezone.utc) - timedelta(days=1)
+ future = datetime.now(timezone.utc) + timedelta(days=1)
+
+ def copy(**kw):
+ return s3.copy_object(Bucket=bkt, Key="dst",
+ CopySource={"Bucket": bkt, "Key": "src"}, **kw)
+
+ # If-Unmodified-Since in the past -> source modified after -> 412.
+ with pytest.raises(ClientError) as e:
+ copy(CopySourceIfUnmodifiedSince=past)
+ assert e.value.response["ResponseMetadata"]["HTTPStatusCode"] == 412
+ # In the future -> succeeds.
+ copy(CopySourceIfUnmodifiedSince=future)
+
+ # If-Modified-Since in the future -> not modified since -> 412.
+ with pytest.raises(ClientError) as e2:
+ copy(CopySourceIfModifiedSince=future)
+ assert e2.value.response["ResponseMetadata"]["HTTPStatusCode"] == 412
+ # In the past -> succeeds.
+ copy(CopySourceIfModifiedSince=past)
+
+ # Precedence: If-Match true wins over a failing If-Unmodified-Since -> copies.
+ copy(CopySourceIfMatch=src_etag, CopySourceIfUnmodifiedSince=past)
+
+ # Precedence: If-None-Match matching (fails) wins over If-Modified-Since -> 412.
+ with pytest.raises(ClientError) as e3:
+ copy(CopySourceIfNoneMatch=src_etag, CopySourceIfModifiedSince=past)
+ assert e3.value.response["ResponseMetadata"]["HTTPStatusCode"] == 412
+
+
+def test_s3_canned_acl_expands_to_group_grants(s3):
+ """A canned ACL (at PutObject or PutObjectAcl) expands to the group grants it
+ implies: public-read -> AllUsers READ; authenticated-read -> AuthenticatedUsers
+ READ; plus the owner's FULL_CONTROL. Invalid canned values are rejected. (#1322)"""
+ bkt = f"acl-{_uuid_mod.uuid4().hex[:8]}"
+ s3.create_bucket(Bucket=bkt)
+
+ def grants(key):
+ acl = s3.get_object_acl(Bucket=bkt, Key=key)
+ return [(g["Grantee"].get("URI", g["Grantee"].get("ID")), g["Permission"])
+ for g in acl["Grants"]]
+
+ # Canned ACL at PutObject time.
+ s3.put_object(Bucket=bkt, Key="pub", Body=b"x", ACL="public-read")
+ g = grants("pub")
+ assert ("http://acs.amazonaws.com/groups/global/AllUsers", "READ") in g, g
+ assert any(perm == "FULL_CONTROL" for _, perm in g), g
+
+ # Canned ACL via PutObjectAcl subresource.
+ s3.put_object(Bucket=bkt, Key="auth", Body=b"x")
+ s3.put_object_acl(Bucket=bkt, Key="auth", ACL="authenticated-read")
+ assert ("http://acs.amazonaws.com/groups/global/AuthenticatedUsers", "READ") in grants("auth")
+
+ # public-read-write adds AllUsers WRITE too.
+ s3.put_object(Bucket=bkt, Key="rw", Body=b"x", ACL="public-read-write")
+ grw = grants("rw")
+ assert ("http://acs.amazonaws.com/groups/global/AllUsers", "WRITE") in grw, grw
+
+ # Invalid canned ACL is rejected.
+ with pytest.raises(ClientError) as e:
+ s3.put_object(Bucket=bkt, Key="bad", Body=b"x", ACL="nonsense-acl")
+ assert e.value.response["Error"]["Code"] == "InvalidArgument", e.value.response["Error"]
+
+
+def test_s3_list_delimiter_next_marker_is_common_prefix(s3):
+ """When a delimited page ends on a collapsed group, NextMarker is the common
+ prefix (boo/), not an underlying key, and resuming from it walks the rest of
+ the bucket without re-emitting the group. (#1322, defect 5)"""
+ bkt = f"delim-nm-{_uuid_mod.uuid4().hex[:8]}"
+ s3.create_bucket(Bucket=bkt)
+ for k in ("asdf", "boo/bar", "boo/baz/xyzzy", "cquux/thud", "cquux/bla"):
+ s3.put_object(Bucket=bkt, Key=k, Body=b"x")
+
+ # Page 1: marker=asdf, one row -> CommonPrefix boo/, NextMarker boo/.
+ p1 = s3.list_objects(Bucket=bkt, Delimiter="/", MaxKeys=1, Marker="asdf")
+ assert p1["IsTruncated"] is True
+ assert [c["Prefix"] for c in p1.get("CommonPrefixes", [])] == ["boo/"]
+ assert p1["NextMarker"] == "boo/"
+
+ # Page 2: resume from boo/ -> cquux/ only, no repeat of boo/, terminates.
+ p2 = s3.list_objects(Bucket=bkt, Delimiter="/", MaxKeys=1, Marker=p1["NextMarker"])
+ assert [c["Prefix"] for c in p2.get("CommonPrefixes", [])] == ["cquux/"]
+ assert p2.get("IsTruncated") is False
+
+ # Full unpaginated listing: the top-level view is asdf + boo/ + cquux/.
+ full = s3.list_objects(Bucket=bkt, Delimiter="/")
+ assert [c["Key"] for c in full.get("Contents", [])] == ["asdf"]
+ assert [c["Prefix"] for c in full.get("CommonPrefixes", [])] == ["boo/", "cquux/"]