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(s3): return ownership-controls default until explicit delete (#933)
Julin Chandrarajah88d0cb798a7f
diff --git a/ministack/services/s3.py b/ministack/services/s3.pyindex 10f81ef9..d2ef4216 100644--- a/ministack/services/s3.py+++ b/ministack/services/s3.py@@ -1385,6 +1385,7 @@ def _put_bucket_ownership_controls(name: str, body: bytes):if name not in _buckets:return _no_such_bucket(name)_buckets[name]["_ownership_controls"] = body.decode("utf-8", errors="replace")+ _buckets[name].pop("_ownership_controls_deleted", None)return 200, {}, b""@@ -1394,6 +1395,16 @@ def _get_bucket_ownership_controls(name: str):stored = _buckets[name].get("_ownership_controls")if stored:return 200, {"Content-Type": "application/xml"}, stored+ if _buckets[name].get("_ownership_controls_deleted"):+ # Explicitly deleted: real S3 returns 404 (not a default block) so the+ # Terraform delete waiter can complete.+ return _error(+ "OwnershipControlsNotFoundError",+ "The bucket ownership controls were not found",+ 404,+ f"/{name}",+ )+ # Never configured: real S3 reports the default Object Ownership.root = Element("OwnershipControls", xmlns=S3_NS)rule = SubElement(root, "Rule")SubElement(rule, "ObjectOwnership").text = "BucketOwnerEnforced"@@ -1404,6 +1415,7 @@ def _delete_bucket_ownership_controls(name: str):if name not in _buckets:return _no_such_bucket(name)_buckets[name].pop("_ownership_controls", None)+ _buckets[name]["_ownership_controls_deleted"] = Truereturn 204, {}, b""diff --git a/tests/test_s3.py b/tests/test_s3.pyindex eb439e91..525769f2 100644--- a/tests/test_s3.py+++ b/tests/test_s3.py@@ -891,6 +891,9 @@ def test_s3_public_access_block(s3):def test_s3_ownership_controls(s3):bkt = "intg-s3-ownership"s3.create_bucket(Bucket=bkt)+ # Never configured: real S3 reports the default Object Ownership, not a 404.+ resp = s3.get_bucket_ownership_controls(Bucket=bkt)+ assert resp["OwnershipControls"]["Rules"][0]["ObjectOwnership"] == "BucketOwnerEnforced"s3.put_bucket_ownership_controls(Bucket=bkt,OwnershipControls={"Rules": [{"ObjectOwnership": "BucketOwnerPreferred"}]},@@ -898,6 +901,11 @@ def test_s3_ownership_controls(s3):resp = s3.get_bucket_ownership_controls(Bucket=bkt)assert resp["OwnershipControls"]["Rules"][0]["ObjectOwnership"] == "BucketOwnerPreferred"s3.delete_bucket_ownership_controls(Bucket=bkt)+ # After delete the config is gone: GetBucketOwnershipControls must 404 instead+ # of returning a default block (otherwise Terraform's delete waiter times out).+ with pytest.raises(ClientError) as exc:+ s3.get_bucket_ownership_controls(Bucket=bkt)+ assert exc.value.response["Error"]["Code"] == "OwnershipControlsNotFoundError"def test_s3_object_lock_configuration(s3):bkt = "intg-s3-objlock-cfg"