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
feat(s3): support If-None-Match / If-Match preconditions on PutObject (#680)
Matt Cook56a05f4cf2dd
diff --git a/ministack/services/s3.py b/ministack/services/s3.pyindex a60e4405..08c81df2 100644--- a/ministack/services/s3.py+++ b/ministack/services/s3.py@@ -405,6 +405,83 @@ def _validate_content_md5(headers: dict, body: bytes):return None+def _check_put_preconditions(headers: dict, existing_obj: dict | None):+ """Evaluate `If-Match` and `If-None-Match` on PUT.++ AWS S3 added native conditional writes on PutObject in November 2024:+ - `If-None-Match: "*"` — succeed only when no object exists at the key.+ Used to implement create-once semantics (idempotent writes, distributed+ leader election via S3, two-file pair serialization).+ - `If-None-Match: "<etag>"` — succeed only when the existing object's+ ETag does NOT match.+ - `If-Match: "*"` — succeed only when an object already exists.+ - `If-Match: "<etag>"` — succeed only when the existing object's ETag+ matches.++ Returns a 412 PreconditionFailed tuple when a condition is violated;+ otherwise returns None and the caller proceeds with the PUT.++ ETag comparison strips surrounding quotes on both sides — S3 stores ETags+ with quotes but client code is inconsistent about including them.+ """+ if_none_match = headers.get("if-none-match", "").strip()+ if_match = headers.get("if-match", "").strip()++ if not if_none_match and not if_match:+ return None++ existing_etag = (+ existing_obj["etag"].strip('"') if existing_obj is not None else None+ )++ if if_none_match:+ # "*" form: any existing object violates the condition.+ if if_none_match == "*":+ if existing_obj is not None:+ return _error(+ "PreconditionFailed",+ "At least one of the pre-conditions you specified did not hold",+ 412,+ )+ # ETag form: existing object with matching ETag violates the condition.+ elif existing_obj is not None and if_none_match.strip('"') == existing_etag:+ return _error(+ "PreconditionFailed",+ "At least one of the pre-conditions you specified did not hold",+ 412,+ )++ if if_match:+ # "*" form: any existing object satisfies the condition; missing → 412 per RFC 7232.+ # (AWS docs don't explicitly document If-Match:* for PutObject, but the inverse case+ # is well-defined by RFC and real S3 follows it.)+ if if_match == "*":+ if existing_obj is None:+ return _error(+ "PreconditionFailed",+ "At least one of the pre-conditions you specified did not hold",+ 412,+ )+ elif existing_obj is None:+ # AWS S3 specifically returns 404 (NoSuchKey) — not 412 — when If-Match: <etag>+ # targets a key that doesn't exist (or whose current version is a delete marker).+ # Documented under "Conditional write behavior" in the user guide:+ # https://docs.aws.amazon.com/AmazonS3/latest/userguide/conditional-writes.html#conditional-error-response+ return _error(+ "NoSuchKey",+ "The specified key does not exist.",+ 404,+ )+ elif if_match.strip('"') != existing_etag:+ return _error(+ "PreconditionFailed",+ "At least one of the pre-conditions you specified did not hold",+ 412,+ )++ return None++def _find_xml_tag(parent, tag_name, ns=S3_NS):el = parent.find("{%s}%s" % (ns, tag_name))if el is None:@@ -1776,6 +1853,10 @@ def _put_object(bucket_name: str, key: str, body: bytes, headers: dict):if sc_err:return sc_err+ precondition_err = _check_put_preconditions(headers, bucket.get("objects", {}).get(key))+ if precondition_err:+ return precondition_err+etag = f'"{md5_hash(body)}"'obj = _build_object_record(body, headers, etag=etag)bucket["objects"][key] = objdiff --git a/tests/test_s3.py b/tests/test_s3.pyindex b382d817..d0f2ccfe 100644--- a/tests/test_s3.py+++ b/tests/test_s3.py@@ -75,6 +75,170 @@ def test_s3_put_object_no_bucket(s3):s3.put_object(Bucket="intg-s3-nobucket-xyz", Key="k", Body=b"x")assert exc.value.response["Error"]["Code"] == "NoSuchBucket"++# ─── Conditional PUT (If-Match / If-None-Match) ──────────────────────────────++def test_s3_put_object_if_none_match_star_no_existing(s3):+ """If-None-Match: * succeeds when no object exists at the key (create-once)."""+ bucket = "intg-s3-ifnm-star-create"+ s3.create_bucket(Bucket=bucket)++ # botocore strips IfNoneMatch on PutObject (added by S3 in 2024); send via low-level+ # event handler so the header reaches the wire.+ def _add_ifnm(request, **_kwargs):+ request.headers["If-None-Match"] = "*"++ s3.meta.events.register_first(+ "before-send.s3.PutObject", _add_ifnm,+ )+ try:+ s3.put_object(Bucket=bucket, Key="first.txt", Body=b"hello")+ finally:+ s3.meta.events.unregister("before-send.s3.PutObject", _add_ifnm)++ resp = s3.get_object(Bucket=bucket, Key="first.txt")+ assert resp["Body"].read() == b"hello"+++def test_s3_put_object_if_none_match_star_existing_fails(s3):+ """If-None-Match: * returns 412 when an object already exists at the key."""+ bucket = "intg-s3-ifnm-star-conflict"+ s3.create_bucket(Bucket=bucket)+ s3.put_object(Bucket=bucket, Key="taken.txt", Body=b"original")++ def _add_ifnm(request, **_kwargs):+ request.headers["If-None-Match"] = "*"++ s3.meta.events.register_first(+ "before-send.s3.PutObject", _add_ifnm,+ )+ try:+ with pytest.raises(ClientError) as exc:+ s3.put_object(Bucket=bucket, Key="taken.txt", Body=b"second")+ finally:+ s3.meta.events.unregister("before-send.s3.PutObject", _add_ifnm)++ assert exc.value.response["Error"]["Code"] == "PreconditionFailed"+ assert exc.value.response["ResponseMetadata"]["HTTPStatusCode"] == 412++ # The original bytes must remain — the failed PUT must not overwrite.+ resp = s3.get_object(Bucket=bucket, Key="taken.txt")+ assert resp["Body"].read() == b"original"+++def test_s3_put_object_if_none_match_etag(s3):+ """If-None-Match: <etag> succeeds when existing ETag differs, fails when it matches."""+ bucket = "intg-s3-ifnm-etag"+ s3.create_bucket(Bucket=bucket)+ first = s3.put_object(Bucket=bucket, Key="obj.txt", Body=b"v1")+ first_etag = first["ETag"]++ # Wrong ETag → condition satisfied, PUT succeeds.+ def _add_wrong(request, **_kwargs):+ request.headers["If-None-Match"] = '"00000000000000000000000000000000"'++ s3.meta.events.register_first("before-send.s3.PutObject", _add_wrong)+ try:+ s3.put_object(Bucket=bucket, Key="obj.txt", Body=b"v2")+ finally:+ s3.meta.events.unregister("before-send.s3.PutObject", _add_wrong)++ # Matching ETag → condition violated, PUT fails 412.+ def _add_match(request, **_kwargs):+ # Use the new ETag from v2.+ v2_etag = s3.head_object(Bucket=bucket, Key="obj.txt")["ETag"]+ request.headers["If-None-Match"] = v2_etag++ s3.meta.events.register_first("before-send.s3.PutObject", _add_match)+ try:+ with pytest.raises(ClientError) as exc:+ s3.put_object(Bucket=bucket, Key="obj.txt", Body=b"v3")+ finally:+ s3.meta.events.unregister("before-send.s3.PutObject", _add_match)++ assert exc.value.response["Error"]["Code"] == "PreconditionFailed"+ _ = first_etag # unused; kept to show v1 etag captured at write time+++def test_s3_put_object_if_match_star_requires_existing(s3):+ """If-Match: * succeeds when an object exists, fails when none does."""+ bucket = "intg-s3-ifm-star"+ s3.create_bucket(Bucket=bucket)++ def _add_ifm_star(request, **_kwargs):+ request.headers["If-Match"] = "*"++ # No existing object → 412.+ s3.meta.events.register_first("before-send.s3.PutObject", _add_ifm_star)+ try:+ with pytest.raises(ClientError) as exc:+ s3.put_object(Bucket=bucket, Key="missing.txt", Body=b"x")+ finally:+ s3.meta.events.unregister("before-send.s3.PutObject", _add_ifm_star)+ assert exc.value.response["Error"]["Code"] == "PreconditionFailed"++ # Now create it, then If-Match: * succeeds.+ s3.put_object(Bucket=bucket, Key="present.txt", Body=b"a")+ s3.meta.events.register_first("before-send.s3.PutObject", _add_ifm_star)+ try:+ s3.put_object(Bucket=bucket, Key="present.txt", Body=b"b")+ finally:+ s3.meta.events.unregister("before-send.s3.PutObject", _add_ifm_star)+ assert s3.get_object(Bucket=bucket, Key="present.txt")["Body"].read() == b"b"+++def test_s3_put_object_if_match_etag(s3):+ """If-Match: <etag> succeeds when ETag matches, 412 when stale."""+ bucket = "intg-s3-ifm-etag"+ s3.create_bucket(Bucket=bucket)+ initial = s3.put_object(Bucket=bucket, Key="obj.txt", Body=b"v1")+ initial_etag = initial["ETag"]++ def _add_match(request, **_kwargs):+ request.headers["If-Match"] = initial_etag++ # Matching ETag → succeed.+ s3.meta.events.register_first("before-send.s3.PutObject", _add_match)+ try:+ s3.put_object(Bucket=bucket, Key="obj.txt", Body=b"v2")+ finally:+ s3.meta.events.unregister("before-send.s3.PutObject", _add_match)++ # Old (stale) ETag against the new object → 412.+ s3.meta.events.register_first("before-send.s3.PutObject", _add_match)+ try:+ with pytest.raises(ClientError) as exc:+ s3.put_object(Bucket=bucket, Key="obj.txt", Body=b"v3")+ finally:+ s3.meta.events.unregister("before-send.s3.PutObject", _add_match)++ assert exc.value.response["Error"]["Code"] == "PreconditionFailed"+ assert exc.value.response["ResponseMetadata"]["HTTPStatusCode"] == 412+++def test_s3_put_object_if_match_etag_missing_object_returns_404(s3):+ """If-Match: <etag> against a non-existent key returns 404 NoSuchKey (per AWS docs).++ AWS S3 specifically returns 404 — not 412 — when If-Match: <etag> targets a key+ that doesn't exist (or whose current version is a delete marker). Documented at+ https://docs.aws.amazon.com/AmazonS3/latest/userguide/conditional-writes.html#conditional-error-response+ """+ bucket = "intg-s3-ifm-missing"+ s3.create_bucket(Bucket=bucket)++ def _add_etag(request, **_kwargs):+ request.headers["If-Match"] = '"00000000000000000000000000000000"'++ s3.meta.events.register_first("before-send.s3.PutObject", _add_etag)+ try:+ with pytest.raises(ClientError) as exc:+ s3.put_object(Bucket=bucket, Key="absent.txt", Body=b"x")+ finally:+ s3.meta.events.unregister("before-send.s3.PutObject", _add_etag)++ assert exc.value.response["Error"]["Code"] == "NoSuchKey"+ assert exc.value.response["ResponseMetadata"]["HTTPStatusCode"] == 404+def test_s3_put_get_json_chunked(s3):"""AWS SDK v2 sends PutObject with chunked Transfer-Encoding — body must be decoded cleanly."""import json as _json