Light, fluffy, and always free - The AWS Local Emulator alternative
fix(s3): preserve archived null versions (#6104)
TiMe01fbd8bf8d0
diff --git a/docs/services/s3.md b/docs/services/s3.mdindex ca5b7cfad4..e7887c3110 100644--- a/docs/services/s3.md+++ b/docs/services/s3.md@@ -93,11 +93,9 @@ specific object version through the four `?annotation` operations. Notes on theA request that names `versionId=null`, the id ListObjectVersions reports for an object writtenwhile versioning was off or suspended, acts on that null version only: GetObject, HeadObject,GetObjectAttributes, the object ACL, tagging, retention, legal hold, annotations and a copy source.-A key whose current version was written with versioning on has no null version, so such a request-answers as for any missing version: `404 NoSuchVersion`, or `404 NoSuchKey` for the annotation-operations. S3 keeps a null version that a later-versioned write replaces as a noncurrent version; Floci does not keep it, so after such a write the-key has no null version.+A key with no null version answers as for any missing version: `404 NoSuchVersion`, or `404 NoSuchKey` for the annotation operations. If+the key already has a null version, a later versioned write or delete marker keeps it as a+noncurrent version. Deleting newer versions can make that null version current again.## Lifecycle Expiration@@ -189,14 +187,14 @@ Floci chooses the execution engine automatically based on the input format and w| Condition | Engine | Notes ||---|---|---|-| Input is Parquet | floci-duck (required) | DuckDB's `read_parquet` — sidecar must be available |+| Input is Parquet | floci-duck (required) | DuckDB's `read_parquet`: sidecar must be available || Input is CSV with `FileHeaderInfo=USE` and floci-duck is running | floci-duck | Full DuckDB SQL: all operators, LIKE, BETWEEN, IN, IS NULL, AND/OR/NOT |-| Input is JSON and floci-duck is running | floci-duck | `read_json_auto` — supports JSON Lines and JSON arrays |+| Input is JSON and floci-duck is running | floci-duck | `read_json_auto`: supports JSON Lines and JSON arrays || Input is CSV with `FileHeaderInfo=NONE` or `IGNORE`, or floci-duck is not running | Java evaluator | Supports SELECT *, column projection, simple WHERE with =, !=, <, >, <=, >=, LIKE, BETWEEN, IN, IS NULL, AND/OR/NOT, LIMIT |The floci-duck sidecar starts lazily on the **first Athena query**. Until then, `isAvailable()` returns false and S3 Select falls back to the Java evaluator for CSV and JSON. Once the sidecar is running, subsequent S3 Select calls route through DuckDB automatically.-If floci-duck is not running and the object is Parquet, S3 Select returns an error — Parquet decoding requires DuckDB.+If floci-duck is not running and the object is Parquet, S3 Select returns an error: Parquet decoding requires DuckDB.The Java evaluator reads the object a record at a time and writes the result in `Records` events asit goes, stopping once a `LIMIT` is reached, so neither the object nor the result is held in memory.@@ -233,9 +231,9 @@ S3 Select supports cross-format output: a CSV object can produce JSON output and| Input | Output | Format ||---|---|---|-| CSV | CSV | Default — comma-separated values |+| CSV | CSV | Default: comma-separated values || CSV | JSON | One JSON object per row: `{"col1":"val1","col2":"val2"}` |-| JSON | JSON | Default — one JSON object per line |+| JSON | JSON | Default: one JSON object per line || JSON | CSV | Comma-separated values, values quoted when they contain commas or newlines |### Example@@ -320,7 +318,7 @@ the original caller, and `DeleteBucket` remains scoped to that caller's account.By default S3 buckets are isolated per account (see [Multi-Account Isolation](../configuration/multi-account.md)),so two accounts can each hold a bucket with the same name. Set`FLOCI_SERVICES_S3_GLOBAL_BUCKET_NAMESPACE=true` to make bucket and object resolution span every-account's partition — a bucket created in one account then resolves cross-account, matching real S3+account's partition: a bucket created in one account then resolves cross-account, matching real S3where bucket names are globally unique. Enable this when a workload (for example an LZA log-archive orCDK asset bucket) is created in one account and read from another.@@ -335,7 +333,7 @@ Floci-managed staging buckets for Lambda packages, Spectrum, Firehose conversionaccount scoped even when the global namespace is enabled. Their existing names and object paths arepreserved; the internal creation path does not change public `CreateBucket` collision handling.-**Why the flag exists, and what it trades away.** Floci partitions every resource by the caller's account, which is the right model for services whose names are account-scoped in AWS. S3 bucket names are not: they are unique across all accounts, a bucket lives in exactly one owning account, and whether another account may touch it is decided by policy rather than by the name being unreachable. A hard per-account partition therefore lets two accounts hold different buckets under one name — a state AWS cannot reach — and makes a legitimate cross-account call fail with `NoSuchBucket` instead of being authorized or denied on its merits. With the flag on, a caller that names another account's bucket resolves it and only IAM and bucket-policy evaluation stand between the caller and the object: AWS-faithful, but stricter than the implicit isolation Floci gives you elsewhere — **provided both `FLOCI_SERVICES_IAM_ENFORCEMENT_ENABLED` (identity-policy checks, applied before the request reaches S3) and `FLOCI_SERVICES_S3_ENFORCE_AUTH` (the S3-specific flag that gates bucket-policy evaluation itself, in `S3Service.authorizeS3Read`/`authorizeS3Write`) are also `true`**. Both default to `false`, and bucket-policy evaluation in particular is a no-op while `FLOCI_SERVICES_S3_ENFORCE_AUTH` is off — regardless of the IAM flag — so in the default configuration enabling the global namespace does not trade per-account isolation for a policy-gated boundary — it removes the only isolation Floci gives S3 for that bucket and replaces it with nothing. Mutations are written back to the bucket's owning account rather than forked into the caller's, and `ListBuckets` stays caller-scoped while `ListObjects` and `ListObjectsV2` list the named bucket's owner-scoped objects, so the flag never reassigns ownership or leaks a bucket inventory. It is off by default — turn it on when emulating a multi-account estate whose real behaviour depends on the global namespace, and leave it off when the per-account partition is the isolation you are relying on. Account-scoped S3 state, notably the account-level Block Public Access configuration, is never widened by this flag. Note also that retrofitting a global namespace onto state that was previously partitioned per account means that if two accounts already hold a bucket under the same name — a state real AWS can't reach, but Floci's prior per-account isolation could — resolution picks whichever account's copy the backend happens to iterate to first; this is unlikely to be reachable on a fresh LZA-driven account structure, but is worth knowing if you enable the flag on an estate with pre-existing same-name buckets.+**Why the flag exists, and what it trades away.** Floci partitions every resource by the caller's account, which is the right model for services whose names are account-scoped in AWS. S3 bucket names are not: they are unique across all accounts, a bucket lives in exactly one owning account, and whether another account may touch it is decided by policy rather than by the name being unreachable. A hard per-account partition therefore lets two accounts hold different buckets under one name (a state AWS cannot reach), and makes a legitimate cross-account call fail with `NoSuchBucket` instead of being authorized or denied on its merits. With the flag on, a caller that names another account's bucket resolves it and only IAM and bucket-policy evaluation stand between the caller and the object: AWS-faithful, but stricter than the implicit isolation Floci gives you elsewhere, **provided both `FLOCI_SERVICES_IAM_ENFORCEMENT_ENABLED` (identity-policy checks, applied before the request reaches S3) and `FLOCI_SERVICES_S3_ENFORCE_AUTH` (the S3-specific flag that gates bucket-policy evaluation itself, in `S3Service.authorizeS3Read`/`authorizeS3Write`) are also `true`**. Both default to `false`, and bucket-policy evaluation in particular is a no-op while `FLOCI_SERVICES_S3_ENFORCE_AUTH` is off, regardless of the IAM flag, so in the default configuration enabling the global namespace does not trade per-account isolation for a policy-gated boundary, it removes the only isolation Floci gives S3 for that bucket and replaces it with nothing. Mutations are written back to the bucket's owning account rather than forked into the caller's, and `ListBuckets` stays caller-scoped while `ListObjects` and `ListObjectsV2` list the named bucket's owner-scoped objects, so the flag never reassigns ownership or leaks a bucket inventory. It is off by default, turn it on when emulating a multi-account estate whose real behaviour depends on the global namespace, and leave it off when the per-account partition is the isolation you are relying on. Account-scoped S3 state, notably the account-level Block Public Access configuration, is never widened by this flag. Note also that retrofitting a global namespace onto state that was previously partitioned per account means that if two accounts already hold a bucket under the same name (a state real AWS can't reach, but Floci's prior per-account isolation could), resolution picks whichever account's copy the backend happens to iterate to first; this is unlikely to be reachable on a fresh LZA-driven account structure, but is worth knowing if you enable the flag on an estate with pre-existing same-name buckets.## Signature Verification@@ -471,7 +469,7 @@ A single public statement makes the whole policy public. As on AWS, `RestrictPub### Account-level operations-The s3control `PutPublicAccessBlock`, `GetPublicAccessBlock` and `DeletePublicAccessBlock` operations are keyed by the `x-amz-account-id` header, stored per account, and persisted like the rest of S3's state so a restart does not silently drop the control. As in AWS, the header must name the caller's own account; a mismatch returns `AccessDenied` (403). Floci keeps one deliberate exception: the configured default (management) account may act on any account, because Floci's launched Lambdas run on placeholder credentials that resolve to the management account instead of assuming a role in the target account — which is how LZA's `Custom::PutPublicAccessBlock` resource reaches this API.+The s3control `PutPublicAccessBlock`, `GetPublicAccessBlock` and `DeletePublicAccessBlock` operations are keyed by the `x-amz-account-id` header, stored per account, and persisted like the rest of S3's state so a restart does not silently drop the control. As in AWS, the header must name the caller's own account; a mismatch returns `AccessDenied` (403). Floci keeps one deliberate exception: the configured default (management) account may act on any account, because Floci's launched Lambdas run on placeholder credentials that resolve to the management account instead of assuming a role in the target account, which is how LZA's `Custom::PutPublicAccessBlock` resource reaches this API.## Not Implemented@@ -630,7 +628,7 @@ Virtual-hosted style puts the bucket name in the hostname:http://my-bucket.s3.localhost.floci.io:4566/my-key```-Floci supports this natively — no `forcePathStyle` needed. The following wildcard DNS domains resolve to `127.0.0.1` via public DNS, so virtual-hosted requests reach Floci on the host machine automatically:+Floci supports this natively, no `forcePathStyle` needed. The following wildcard DNS domains resolve to `127.0.0.1` via public DNS, so virtual-hosted requests reach Floci on the host machine automatically:| Domain pattern | Resolves to ||---|---|@@ -639,7 +637,7 @@ Floci supports this natively — no `forcePathStyle` needed. The following wildc| `*.localhost.localstack.cloud` | `127.0.0.1` || `*.s3.localhost.localstack.cloud` | `127.0.0.1` |-Plain `http://localhost:4566` also works without `forcePathStyle` — the SDK sends `Host: my-bucket.localhost:4566` and Floci's filter extracts the bucket name from that header.+Plain `http://localhost:4566` also works without `forcePathStyle`, the SDK sends `Host: my-bucket.localhost:4566` and Floci's filter extracts the bucket name from that header.Configure the SDK endpoint to one of the base domains (no `forcePathStyle`):@@ -660,7 +658,7 @@ Configure the SDK endpoint to one of the base domains (no `forcePathStyle`):```javascriptconst s3 = new S3Client({endpoint: "http://s3.localhost.floci.io:4566",- // no forcePathStyle — SDK uses virtual-hosted style by default+ // no forcePathStyle, SDK uses virtual-hosted style by default});// SDK sends requests to: my-bucket.s3.localhost.floci.io:4566```@@ -674,7 +672,7 @@ Configure the SDK endpoint to one of the base domains (no `forcePathStyle`):#### Virtual-Hosted Style Inside Docker-Inside Docker containers, `127.0.0.1` resolves to the container itself — not Floci. Floci's **embedded DNS server** handles this automatically: it resolves `*.localhost.floci.io`, `*.s3.localhost.floci.io`, `*.localhost.localstack.cloud`, and `*.s3.localhost.localstack.cloud` to Floci's container IP on the Docker network.+Inside Docker containers, `127.0.0.1` resolves to the container itself, not Floci. Floci's **embedded DNS server** handles this automatically: it resolves `*.localhost.floci.io`, `*.s3.localhost.floci.io`, `*.localhost.localstack.cloud`, and `*.s3.localhost.localstack.cloud` to Floci's container IP on the Docker network.To use virtual-hosted style from a test container, point its DNS at Floci and set the endpoint to Floci's service hostname:diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.javaindex ac722394e9..3fd7dd6531 100644--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java@@ -98,6 +98,7 @@ import java.util.function.Predicate;import java.util.function.Supplier;import java.util.regex.Matcher;import java.util.regex.Pattern;+import java.util.stream.Collectors;import java.util.stream.Stream;@ApplicationScoped@@ -722,6 +723,7 @@ public class S3Service implements Resettable, ResourceProvider {*/S3Object putObject(String bucketName, String key, InputStream body, UploadChecksums checksums,String contentType, Map<String, String> metadata, PutObjectOptions options) {+ requirePublicObjectKey(key);PutObjectOptions effectiveOptions = options != null ? options : new PutObjectOptions();checksums.requireWellFormedContentMd5();AccountAwareStorageBackend.OwnedEntry<Bucket> checkedBucket = resolveBucketEntry(bucketName)@@ -946,6 +948,10 @@ public class S3Service implements Resettable, ResourceProvider {if (effectiveOptions.getTagging() != null && !effectiveOptions.getTagging().isEmpty()) {object.setTags(new HashMap<>(effectiveOptions.getTagging()));}+ // Assigned before any metadata publication so persistent and WAL stores retain the same+ // write order used by in-memory promotion. A failed body write may consume a value, which+ // is harmless because sequencers only need to be monotonic for writes that become visible.+ object.setEventSequencer(nextEventSequencer());if (bucket.isVersioningEnabled()) {String versionId = UUID.randomUUID().toString();@@ -955,24 +961,19 @@ public class S3Service implements Resettable, ResourceProvider {// S3Object) - reusing versionId needs no extra random value.object.setDataGeneration(versionId);- // Check lock protection on the current latest before overwriting+ // Preserve the current null version before replacing its canonical body.String latestKey = objectKey(bucketName, key);- // A pre-versioning object being replaced: its annotations were keyed at the plain- // object key and would be left unreachable by the new version. Cleanup is deferred- // until the replacement body is on disk, so a failed write does not drop them.- boolean[] dropPreVersioningAnnotations = {false};- resolveObjectForAccount(bucketOwnerAccount, latestKey).ifPresent(prev -> {- if (prev.isLatest() && !prev.isDeleteMarker() && bucket.isObjectLockEnabled() && prev.getVersionId() == null) {- checkLockProtection(prev, false);- }- if (prev.getVersionId() != null) {- prev.setLatest(false);- putObjectForAccount(bucketOwnerAccount,- versionedKey(bucketName, key, prev.getVersionId()), prev);- } else {- dropPreVersioningAnnotations[0] = true;+ S3Object nullVersionToArchive = null;+ S3Object previous = resolveObjectForAccount(bucketOwnerAccount, latestKey).orElse(null);+ if (previous != null) {+ S3Object prev = previous;+ if (prev.getVersionId() == null && !prev.isDeleteMarker()) {+ // Preserve the current null body before the canonical file is replaced. Its+ // metadata is copied and published only after the replacement body succeeds,+ // so a failed PUT cannot demote or duplicate the still-current null version.+ nullVersionToArchive = copyObject(prev);}- });+ }// Apply lock fields from request or bucket defaultapplyObjectLock(object, bucket,@@ -988,26 +989,48 @@ public class S3Service implements Resettable, ResourceProvider {// versioned write fails, the canonical file - which unlocked GETs already associate// with the still-unpublished previous generation - is never touched, so a concurrent// GET can't observe corrupted "latest" bytes paired with the old metadata.- if (body instanceof AssembledBody assembled) {- Path versionedPath = resolveVersionedPath(bucketOwnerAccount, bucketName, key, versionId);- moveIntoPlace(assembled.file(), versionedPath);- linkIntoPlace(versionedPath, resolveObjectPath(bucketOwnerAccount, bucketName, key));- } else {- writeVersionedFile(bucketOwnerAccount, bucketName, key, versionId, data);- writeFile(bucketOwnerAccount, bucketName, key, data);- }- // Deferred pre-versioning annotation cleanup: only after the replacement body is on- // disk, so a failed write keeps the old body and its annotations together.- if (dropPreVersioningAnnotations[0]) {- deleteAllAnnotationsFor(annotationParentKey(bucketName, key, null));+ S3Object priorNullHistory = nullVersionToArchive != null+ ? resolveObjectForAccount(bucketOwnerAccount, versionedKey(bucketName, key, "null"))+ .map(S3Service::copyObject).orElse(null)+ : null;+ boolean archived = false;+ try {+ if (nullVersionToArchive != null) {+ archiveCurrentFile(bucketOwnerAccount, bucketName, key);+ archived = true;+ }+ if (body instanceof AssembledBody assembled) {+ Path versionedPath = resolveVersionedPath(bucketOwnerAccount, bucketName, key, versionId);+ moveIntoPlace(assembled.file(), versionedPath);+ linkIntoPlace(versionedPath, resolveObjectPath(bucketOwnerAccount, bucketName, key));+ } else {+ writeVersionedFile(bucketOwnerAccount, bucketName, key, versionId, data);+ writeFile(bucketOwnerAccount, bucketName, key, data);+ }+ if (previous != null && previous.getVersionId() != null) {+ previous.setLatest(false);+ putObjectForAccount(bucketOwnerAccount,+ versionedKey(bucketName, key, previous.getVersionId()), previous);+ }+ if (nullVersionToArchive != null) {+ nullVersionToArchive.setLatest(false);+ putObjectForAccount(bucketOwnerAccount,+ versionedKey(bucketName, key, "null"), nullVersionToArchive);+ }+ // Release the cached payload before publishing: once objectStore.put makes this+ // instance visible to other threads, a concurrent getObject can hold a reference to+ // it (copyObject reads getData() without any lock) and race this null-out otherwise.+ object.setData(null);+ // Store versioned copy and update latest pointer+ putObjectForAccount(bucketOwnerAccount, versionedKey(bucketName, key, versionId), object);+ putObjectForAccount(bucketOwnerAccount, latestKey, object);+ } catch (RuntimeException e) {+ if (archived) {+ restoreNullAfterFailedWrite(bucketOwnerAccount, bucketName, key,+ previous, priorNullHistory, versionId, e);+ }+ throw e;}- // Release the cached payload before publishing: once objectStore.put makes this- // instance visible to other threads, a concurrent getObject can hold a reference to- // it (copyObject reads getData() without any lock) and race this null-out otherwise.- object.setData(null);- // Store versioned copy and update latest pointer- putObjectForAccount(bucketOwnerAccount, versionedKey(bucketName, key, versionId), object);- putObjectForAccount(bucketOwnerAccount, latestKey, object);LOG.debugv("Put versioned object: {0}/{1} v={2} ({3} bytes)", bucketName, key, versionId, body.size());} else {S3Object prev = resolveObjectForAccount(@@ -1017,6 +1040,8 @@ public class S3Service implements Resettable, ResourceProvider {checkLockProtection(prev, false);}+ boolean hasArchivedNull = checkArchivedNullLock(bucketOwnerAccount, bucketName, key, false);+// Apply lock fields from request or bucket defaultapplyObjectLock(object, bucket,effectiveOptions.getObjectLockMode(),@@ -1035,20 +1060,24 @@ public class S3Service implements Resettable, ResourceProvider {} else {writeFile(bucketOwnerAccount, bucketName, key, data);}- // An overwrite replaces the object's annotations (AWS drops them on overwrite).- // The cleanup runs only after the body write succeeds, so a failed PUT keeps the- // old body together with its annotations; and before the new metadata is published,- // so the replacement never appears annotated.- deleteAllAnnotationsFor(annotationParentKey(bucketName, key, null));// Release the cached payload before publishing - see the comment in the versioned// branch above; the same race applies here.object.setData(null);- putObjectForAccount(bucketOwnerAccount, objectKey(bucketName, key), object);+ try {+ putObjectForAccount(bucketOwnerAccount, objectKey(bucketName, key), object);+ } catch (RuntimeException e) {+ if (hasArchivedNull && prev != null) {+ restoreCurrentAfterFailedPublication(bucketOwnerAccount, bucketName, key, prev, e);+ }+ throw e;+ }+ // Retain archived null history and annotations until publication succeeds.+ // Annotation readers share this bucket monitor, so they cannot observe the+ // replacement with the old annotations while cleanup is pending.+ deleteArchivedNullVersion(bucketOwnerAccount, bucketName, key, false);+ deleteAllAnnotationsFor(annotationParentKey(bucketName, key, null));LOG.debugv("Put object: {0}/{1} ({2} bytes)", bucketName, key, body.size());}- // Still under the bucket lock, so the sequencers of concurrent writes to one key follow- // the order they were stored in, whenever their notifications are built.- object.setEventSequencer(nextEventSequencer());return object;}@@ -1697,8 +1726,8 @@ public class S3Service implements Resettable, ResourceProvider {if (ownershipIsEnforced(bucket)) {return false;}- return objectStore.get(versionStoreKey(bucketName, key, versionId))- .filter(object -> isNamedVersion(object, versionId))+ return findStoredVersion(bucketName, key, versionId)+ .map(StoredVersion::object).filter(object -> !object.isDeleteMarker()).map(S3Object::getAcl).map(S3AclPublicAccessEvaluator::aclAllowsPublicRead)@@ -1756,12 +1785,7 @@ public class S3Service implements Resettable, ResourceProvider {return false;}if (versionId == null && bucket.get().value().isVersioningEnabled()) {- boolean currentIsVersioned = resolveObjectForAccount(bucket.get().account(), objectKey(bucketName, key))- .map(current -> current.getVersionId() != null)- .orElse(true);- if (currentIsVersioned) {- return false;- }+ return false;}return isGovernanceRetentionActive(bucketName, key, versionId);}@@ -1806,7 +1830,16 @@ public class S3Service implements Resettable, ResourceProvider {}public S3Object getObject(String bucketName, String key, String versionId) {- if (versionId != null && !"null".equals(versionId)) {+ if ("null".equals(versionId)) {+ Snapshot<byte[]> snapshot = readNullVersionSnapshot(bucketName, key,+ location -> location.archived()+ ? readVersionedFile(location.accountId(), bucketName, key, "null")+ : readFile(location.accountId(), bucketName, key),+ data -> { });+ snapshot.object().setData(snapshot.body());+ return snapshot.object();+ }+ if (versionId != null) {// An explicit version's file is immutable once written (see storeObjectInternal) and// never reused by a later PUT, so this pairing can never race a concurrent overwrite.String bucketOwnerAccount = resolveBucketEntry(bucketName)@@ -1848,12 +1881,82 @@ public class S3Service implements Resettable, ResourceProvider {private record Snapshot<B>(S3Object object, B body) { }+ private record NullVersionLocation(String accountId, String storeKey, boolean archived) { }++ /**+ * Reads the named null version while keeping its metadata and body location together across a+ * concurrent overwrite. A null version starts at the plain key, then moves to a reserved+ * history key when a versioned write replaces it. The generation alone cannot detect that move+ * because the logical version keeps its token, so the recheck compares both the token and the+ * store key.+ */+ private <B> Snapshot<B> readNullVersionSnapshot(String bucketName, String key,+ Function<NullVersionLocation, B> readBody,+ Consumer<B> discard) {+ AccountAwareStorageBackend.OwnedEntry<Bucket> ownedBucket = resolveBucketEntry(bucketName)+ .orElseThrow(() -> new AwsException("NoSuchBucket",+ "The specified bucket does not exist.", 404));+ Bucket bucket = ownedBucket.value();+ String accountId = ownedBucket.account();+ for (int attempt = 0; attempt < 10_000; attempt++) {+ StoredVersion stored = findStoredVersionForAccount(accountId, bucketName, key, "null")+ .orElseThrow(() -> new AwsException(+ "NoSuchVersion", "The specified version does not exist.", 404));+ if (stored.object().isDeleteMarker()) {+ throw new AwsException("NoSuchKey", "The specified key does not exist.", 404);+ }+ S3Object object = copyObject(stored.object());+ boolean archived = stored.storeKey().equals(versionedKey(bucketName, key, "null"));+ NullVersionLocation location = new NullVersionLocation(accountId, stored.storeKey(), archived);+ if (!archived && bucket.getVersioningStatus() != null) {+ synchronized (bucket) {+ StoredVersion current = findStoredVersionForAccount(accountId, bucketName, key, "null")+ .orElse(null);+ if (current != null && current.storeKey().equals(location.storeKey())+ && Objects.equals(current.object().getDataGeneration(), object.getDataGeneration())) {+ // Pin canonical null bytes while archival and promotion cannot move them.+ return new Snapshot<>(object, readBody.apply(location));+ }+ }+ continue;+ }+ B body;+ try {+ body = readBody.apply(location);+ } catch (UncheckedIOException e) {+ synchronized (bucket) {+ StoredVersion current = findStoredVersionForAccount(accountId, bucketName, key, "null")+ .orElse(null);+ if (current != null && current.storeKey().equals(location.storeKey())+ && Objects.equals(current.object().getDataGeneration(), object.getDataGeneration())) {+ throw e;+ }+ }+ continue;+ }+ synchronized (bucket) {+ StoredVersion current = findStoredVersionForAccount(accountId, bucketName, key, "null")+ .orElse(null);+ if (current != null && current.storeKey().equals(location.storeKey())+ && Objects.equals(current.object().getDataGeneration(), object.getDataGeneration())+ && (archived || bucket.getVersioningStatus() == null)) {+ return new Snapshot<>(object, body);+ }+ }+ discard.accept(body);+ }+ throw new IllegalStateException(+ "getObject retry limit exceeded for " + bucketName + "/" + key+ + " version null - the object is either under sustained concurrent overwrite or the "+ + "metadata/data resolution paths disagree about where the null version lives");+ }+/*** The optimistic read described on {@link #getLatestObject}, for any body type: {@code readBody}* gets the bucket owner's account and {@code discard} releases a body read during a race. It reads- * the object at the plain key, which is the current version when {@code versionId} is null and- * must be the null version when it is {@code "null"}: a write landing mid-read retries the read,- * which then finds the null version gone.+ * the object at the plain key, which is the current version when {@code versionId} is null.+ * Named null-version reads use {@link #readNullVersionSnapshot} because that version can move+ * from the plain key to archived history without changing its data-generation token.*/private <B> Snapshot<B> readLatestSnapshot(String bucketName, String key, String versionId,Function<String, B> readBody, Consumer<B> discard) {@@ -1868,11 +1971,37 @@ public class S3Service implements Resettable, ResourceProvider {// with a clear error instead of spinning forever re-reading the file and exhausting the heap.for (int attempt = 0; attempt < 10_000; attempt++) {S3Object obj = getObjectMetadata(bucketName, key, versionId);- B body = readBody.apply(bucketOwnerAccount);+ if (obj.getVersionId() == null && bucket.getVersioningStatus() != null) {+ synchronized (bucket) {+ S3Object current = resolveObjectForAccount(bucketOwnerAccount, storeKey).orElse(null);+ if (current != null && !current.isDeleteMarker() && current.getVersionId() == null+ && Objects.equals(current.getDataGeneration(), obj.getDataGeneration())) {+ // Once versioning is set, read or pin canonical null bytes while the+ // bucket monitor keeps archival and promotion from moving the body.+ return new Snapshot<>(obj, readBody.apply(bucketOwnerAccount));+ }+ }+ continue;+ }+ B body;+ try {+ body = readBody.apply(bucketOwnerAccount);+ } catch (UncheckedIOException e) {+ synchronized (bucket) {+ S3Object current = resolveObjectForAccount(bucketOwnerAccount, storeKey).orElse(null);+ if (current != null && !current.isDeleteMarker()+ && Objects.equals(current.getDataGeneration(), obj.getDataGeneration())+ && (obj.getVersionId() != null || bucket.getVersioningStatus() == null)) {+ throw e;+ }+ }+ continue;+ }synchronized (bucket) {S3Object current = resolveObjectForAccount(bucketOwnerAccount, storeKey).orElse(null);if (current != null && !current.isDeleteMarker()- && Objects.equals(current.getDataGeneration(), obj.getDataGeneration())) {+ && Objects.equals(current.getDataGeneration(), obj.getDataGeneration())+ && (obj.getVersionId() != null || bucket.getVersioningStatus() == null)) {return new Snapshot<>(obj, body);}}@@ -1904,7 +2033,15 @@ public class S3Service implements Resettable, ResourceProvider {S3Object obj = getObject(bucketName, key, versionId);return new ObjectRead(obj, new ByteArrayInputStream(obj.getData()));}- if (versionId == null || "null".equals(versionId)) {+ if ("null".equals(versionId)) {+ Snapshot<FileChannel> snapshot = readNullVersionSnapshot(bucketName, key,+ location -> openForRead(location.archived()+ ? resolveVersionedPathForRead(location.accountId(), bucketName, key, "null")+ : resolveObjectPathForRead(location.accountId(), bucketName, key)),+ S3Service::closeQuietly);+ return objectRead(snapshot.object(), snapshot.body());+ }+ if (versionId == null) {Snapshot<FileChannel> snapshot = readLatestSnapshot(bucketName, key, versionId,account -> openForRead(resolveObjectPathForRead(account, bucketName, key)),S3Service::closeQuietly);@@ -1978,13 +2115,16 @@ public class S3Service implements Resettable, ResourceProvider {}public InputStream openObjectStream(String bucketName, String key, String versionId) {+ if ("null".equals(versionId)) {+ return openObject(bucketName, key, versionId).body();+ }getObjectMetadata(bucketName, key, versionId);String bucketOwnerAccount = resolveBucketEntry(bucketName).orElseThrow(() -> new AwsException("NoSuchBucket","The specified bucket does not exist.", 404)).account();if (inMemory) {- byte[] data = versionId != null && !"null".equals(versionId)+ byte[] data = versionId != null? memoryDataStore.get(physicalVersionedKey(bucketOwnerAccount, bucketName, key, versionId)): memoryDataStore.get(physicalKey(bucketOwnerAccount, bucketName, key));if (data == null) {@@ -1993,7 +2133,7 @@ public class S3Service implements Resettable, ResourceProvider {return new ByteArrayInputStream(data);}try {- Path path = versionId != null && !"null".equals(versionId)+ Path path = versionId != null? resolveVersionedPathForRead(bucketOwnerAccount, bucketName, key, versionId): resolveObjectPathForRead(bucketOwnerAccount, bucketName, key);return Files.newInputStream(path);@@ -2062,26 +2202,54 @@ public class S3Service implements Resettable, ResourceProvider {/** The stored version {@code versionId} names, the current one when it is null, delete markers included. */private Optional<S3Object> findStoredObject(AccountAwareStorageBackend.OwnedEntry<Bucket> ownedBucket,String bucketName, String key, String versionId) {- return resolveObjectForAccount(ownedBucket.account(), versionStoreKey(bucketName, key, versionId))- .filter(existing -> isNamedVersion(existing, versionId));+ return findStoredVersionForAccount(ownedBucket.account(), bucketName, key, versionId)+ .map(StoredVersion::object);}/**- * Where the version {@code versionId} names is stored: the current version (null) and the null- * version ({@code "null"}) at the plain object key, where floci keeps the null version until a- * versioned write replaces it, and any other version at its versioned key.+ * One resolved metadata entry and the store key that supplied it. The store key matters for+ * named null-version body reads: that version can move from the plain key to its reserved+ * history key without changing its data-generation token.*/- private String versionStoreKey(String bucketName, String key, String versionId) {- return versionId == null || "null".equals(versionId)- ? objectKey(bucketName, key)- : versionedKey(bucketName, key, versionId);+ private record StoredVersion(String storeKey, S3Object object) { }++ private Optional<StoredVersion> findStoredVersion(String bucketName, String key, String versionId) {+ return findStoredVersion(bucketName, key, versionId, this::resolveObject);+ }++ private Optional<StoredVersion> findStoredVersionForAccount(+ String accountId, String bucketName, String key, String versionId) {+ return findStoredVersion(bucketName, key, versionId,+ storeKey -> resolveObjectForAccount(accountId, storeKey));+ }++ private Optional<StoredVersion> findStoredVersion(String bucketName, String key, String versionId,+ Function<String, Optional<S3Object>> lookup) {+ String latestKey = objectKey(bucketName, key);+ if (versionId == null) {+ return lookup.apply(latestKey).map(object -> new StoredVersion(latestKey, object));+ }+ if ("null".equals(versionId)) {+ Optional<StoredVersion> currentNull = lookup.apply(latestKey)+ .filter(object -> key.equals(object.getKey()))+ .filter(object -> isNamedVersion(object, versionId))+ .map(object -> new StoredVersion(latestKey, object));+ if (currentNull.isPresent()) {+ return currentNull;+ }+ }+ String historyKey = versionedKey(bucketName, key, versionId);+ return lookup.apply(historyKey)+ .filter(object -> !"null".equals(versionId) || key.equals(object.getKey()))+ .filter(object -> isNamedVersion(object, versionId))+ .map(object -> new StoredVersion(historyKey, object));}/**- * Whether {@code object}, found at {@link #versionStoreKey}, is the version {@code versionId}+ * Whether {@code object} is the version {@code versionId}* names. The literal {@code "null"} names only the null version, an object without a version id:- * a current version that has one is not it, and floci keeps no null version beside one, so such- * a request names a version that does not exist.+ * a current version that has one is not it, so resolution must continue to the null-version+ * history entry.*/private static boolean isNamedVersion(S3Object object, String versionId) {return !"null".equals(versionId) || object.getVersionId() == null;@@ -2214,14 +2382,13 @@ public class S3Service implements Resettable, ResourceProvider {private S3Object deleteObjectLocked(Bucket bucket, String bucketName, String key,String versionId, boolean bypassGovernance, String removalEvent) {if (bucket.isVersioningEnabled() && versionId == null) {- // Check lock on current latest before placing a delete marker- objectStore.get(objectKey(bucketName, key)).ifPresent(prev -> {- if (!prev.isDeleteMarker() && prev.getVersionId() == null) {- checkLockProtection(prev, bypassGovernance);- }- });-- // Create a delete marker instead of actually deleting+ // A delete marker preserves the previous version, including its lock settings.+ S3Object previous = objectStore.get(objectKey(bucketName, key)).orElse(null);+ boolean preservesNull = previous != null && !previous.isDeleteMarker()+ && previous.getVersionId() == null;+ S3Object priorNullHistory = preservesNull+ ? objectStore.get(versionedKey(bucketName, key, "null")).map(S3Service::copyObject).orElse(null)+ : null;S3Object deleteMarker = new S3Object(bucketName, key, new byte[0], null);String markerId = UUID.randomUUID().toString();deleteMarker.setVersionId(markerId);@@ -2229,72 +2396,93 @@ public class S3Service implements Resettable, ResourceProvider {deleteMarker.setLatest(true);deleteMarker.setEventSequencer(nextEventSequencer());- // Mark previous latest as not latest- objectStore.get(objectKey(bucketName, key)).ifPresent(prev -> {- if (prev.getVersionId() != null) {- prev.setLatest(false);- objectStore.put(versionedKey(bucketName, key, prev.getVersionId()), prev);- } else {- // The marker replaces a pre-versioning object: no versioned entry ever- // existed, so its annotations become unreachable and are removed here- // (they are permanent, as on AWS).- deleteAllAnnotationsFor(annotationParentKey(bucketName, key, null));+ boolean archived = false;+ try {+ if (preservesNull) {+ archiveCurrentFile(ownerId(), bucketName, key);+ archived = true;+ S3Object archivedNull = copyObject(previous);+ archivedNull.setLatest(false);+ objectStore.put(versionedKey(bucketName, key, "null"), archivedNull);+ } else if (previous != null && previous.getVersionId() != null) {+ previous.setLatest(false);+ objectStore.put(versionedKey(bucketName, key, previous.getVersionId()), previous);}- });-- objectStore.put(versionedKey(bucketName, key, markerId), deleteMarker);- objectStore.put(objectKey(bucketName, key), deleteMarker);+ objectStore.put(versionedKey(bucketName, key, markerId), deleteMarker);+ objectStore.put(objectKey(bucketName, key), deleteMarker);+ } catch (RuntimeException e) {+ if (archived) {+ restoreNullAfterFailedWrite(ownerId(), bucketName, key,+ previous, priorNullHistory, markerId, e);+ }+ throw e;+ }+ deleteFile(bucketName, key);LOG.debugv("Created delete marker: {0}/{1} v={2}", bucketName, key, markerId);fireNotifications(bucketName, key, removalEvent + ":DeleteMarkerCreated", deleteMarker);return deleteMarker;- } else if ("null".equals(versionId)) {- // A null version is stored at the plain object key until a versioned write replaces it.- // Treat the literal request value as that null version, not as a versioned key named- // "null". A delete marker is not the null version and must remain untouched.- S3Object existing = objectStore.get(objectKey(bucketName, key)).orElse(null);- if (existing == null || existing.isDeleteMarker() || existing.getVersionId() != null) {- return null;- }- checkLockProtection(existing, bypassGovernance);- objectStore.delete(objectKey(bucketName, key));- deleteFile(bucketName, key);- deleteAllAnnotationsFor(annotationParentKey(bucketName, key, null));- LOG.debugv("Permanently deleted null version: {0}/{1}", bucketName, key);- fireNotifications(bucketName, key, removalEvent + ":Delete", null);- return existing;} else if (versionId != null) {- // Get the specific version before permanent deletion- S3Object toDelete = objectStore.get(versionedKey(bucketName, key, versionId)).orElse(null);+ String historyKey = versionedKey(bucketName, key, versionId);+ S3Object toDelete = objectStore.get(historyKey)+ .filter(existing -> !"null".equals(versionId) || key.equals(existing.getKey()))+ .filter(existing -> isNamedVersion(existing, versionId))+ .orElse(null);+ if (toDelete == null && "null".equals(versionId)) {+ // Before the null version is archived it exists only at the plain latest key.+ S3Object existing = objectStore.get(objectKey(bucketName, key))+ .filter(candidate -> key.equals(candidate.getKey()))+ .filter(candidate -> !candidate.isDeleteMarker())+ .filter(candidate -> candidate.getVersionId() == null)+ .orElse(null);+ if (existing == null) {+ return null;+ }+ checkLockProtection(existing, bypassGovernance);+ objectStore.delete(objectKey(bucketName, key));+ deleteFile(bucketName, key);+ deleteAllAnnotationsFor(annotationParentKey(bucketName, key, null));+ LOG.debugv("Permanently deleted null version: {0}/{1}", bucketName, key);+ fireNotifications(bucketName, key, removalEvent + ":Delete", existing);+ return existing;+ }if (toDelete != null && !toDelete.isDeleteMarker()) {checkLockProtection(toDelete, bypassGovernance);}// Permanently delete a specific version (metadata + file data)- objectStore.delete(versionedKey(bucketName, key, versionId));+ objectStore.delete(historyKey);deleteVersionedFile(bucketName, key, versionId);- deleteAllAnnotationsFor(annotationParentKey(bucketName, key, versionId));+ deleteAllAnnotationsFor(annotationParentKey(+ bucketName, key, "null".equals(versionId) ? null : versionId));LOG.debugv("Permanently deleted version: {0}/{1} v={2}", bucketName, key, versionId);// Promote the next most-recent version when the deleted one was the latestString latestKey = objectKey(bucketName, key);objectStore.get(latestKey).ifPresent(latest -> {- if (versionId.equals(latest.getVersionId())) {+ if (versionId.equals(storedVersionId(latest))) {String vPrefix = versionedKey(bucketName, key, "");- List<S3Object> remaining = objectStore.scan(k -> k.startsWith(vPrefix));+ List<S3Object> remaining = objectStore.scan(k -> k.startsWith(vPrefix))+ .stream()+ .filter(candidate -> key.equals(candidate.getKey()))+ .collect(Collectors.toCollection(ArrayList::new));+ objectStore.get(versionedKey(bucketName, key, "null")).ifPresent(remaining::add);if (remaining.isEmpty()) {objectStore.delete(latestKey);deleteFile(bucketName, key);} else {- S3Object newLatest = remaining.stream()+ S3Object newLatest = copyObject(remaining.stream().max(WRITE_ORDER)- .orElseThrow();+ .orElseThrow());+ if (newLatest.getVersionId() == null) {+ newLatest.setDataGeneration(UUID.randomUUID().toString());+ }// The promoted version's body becomes current before its metadata is// published, the order storeObjectInternal writes in, so a concurrent GET// never pairs the promoted version with the deleted version's bytes.// Delete markers have no versioned file.if (!newLatest.isDeleteMarker()) {- promoteVersionedFile(bucketName, key, newLatest.getVersionId());+ promoteVersionedFile(bucketName, key, storedVersionId(newLatest));}newLatest.setLatest(true);- objectStore.put(versionedKey(bucketName, key, newLatest.getVersionId()), newLatest);+ objectStore.put(versionedKey(bucketName, key, storedVersionId(newLatest)), newLatest);objectStore.put(latestKey, newLatest);if (newLatest.isDeleteMarker()) {deleteFile(bucketName, key);@@ -2313,6 +2501,8 @@ public class S3Service implements Resettable, ResourceProvider {checkLockProtection(existing, bypassGovernance);}// Non-versioned delete+ checkArchivedNullLock(ownerId(), bucketName, key, bypassGovernance);+ deleteArchivedNullVersion(ownerId(), bucketName, key, true);objectStore.delete(objectKey(bucketName, key));deleteFile(bucketName, key);deleteAllAnnotationsFor(annotationParentKey(bucketName, key, null));@@ -2330,8 +2520,8 @@ public class S3Service implements Resettable, ResourceProvider {*/public boolean isGovernanceRetentionActive(String bucketName, String key, String versionId) {String bucketOwnerAccount = getBucketOwnerAccountId(bucketName);- S3Object object = resolveObjectForAccount(bucketOwnerAccount, versionStoreKey(bucketName, key, versionId))- .filter(existing -> isNamedVersion(existing, versionId))+ S3Object object = findStoredVersionForAccount(bucketOwnerAccount, bucketName, key, versionId)+ .map(StoredVersion::object).orElse(null);return object != null&& !object.isDeleteMarker()@@ -2361,7 +2551,9 @@ public class S3Service implements Resettable, ResourceProvider {String fullPrefix = prefix != null ? keyPrefix + prefix : keyPrefix;// Filter out versioned entries (contain #v#) and delete markers- Predicate<String> keyFilter = key -> key.startsWith(fullPrefix) && !key.contains("#v#");+ Predicate<String> keyFilter = key -> key.startsWith(fullPrefix)+ && !key.contains("#v#")+ && !key.startsWith(nullVersionPrefix(bucketName));List<S3Object> storedObjects = scanObjectsForAccount(bucketOwnerAccount, keyFilter);List<S3Object> allObjects = storedObjects.stream()@@ -2615,7 +2807,15 @@ public class S3Service implements Resettable, ResourceProvider {if (inMemory) {return new CopySource(getObject(bucketName, key, versionId), null);}- if (versionId == null || "null".equals(versionId)) {+ if ("null".equals(versionId)) {+ Snapshot<Path> snapshot = readNullVersionSnapshot(bucketName, key,+ location -> pinFile(location.archived()+ ? resolveVersionedPathForRead(location.accountId(), bucketName, key, "null")+ : resolveObjectPathForRead(location.accountId(), bucketName, key)),+ pinned -> deleteQuietly(pinned, "pinned null-version copy source of a read that raced an overwrite"));+ return checkedCopySource(snapshot.object(), snapshot.body());+ }+ if (versionId == null) {Snapshot<Path> snapshot = readLatestSnapshot(bucketName, key, versionId,account -> pinFile(resolveObjectPathForRead(account, bucketName, key)),pinned -> deleteQuietly(pinned, "pinned copy source of a read that raced an overwrite"));@@ -2810,6 +3010,8 @@ public class S3Service implements Resettable, ResourceProvider {public record ListVersionsResult(List<S3Object> versions, List<String> commonPrefixes, boolean isTruncated,String nextKeyMarker, String nextVersionIdMarker) {}+ private record ListedVersionIdentity(String key, String versionId) { }+public ListVersionsResult listObjectVersions(String bucketName, String prefix, int maxKeys, String keyMarker) {return listObjectVersions(bucketName, prefix, null, maxKeys, keyMarker, null);}@@ -2835,7 +3037,13 @@ public class S3Service implements Resettable, ResourceProvider {// Scan for versioned entries (contain #v#)List<S3Object> versions = new ArrayList<>(objectStore.scan(key ->- key.startsWith(fullPrefix) && key.contains("#v#")));+ key.startsWith(fullPrefix)+ && key.contains("#v#")+ && !key.startsWith(nullVersionPrefix(bucketName))));+ objectStore.scan(key -> key.startsWith(nullVersionPrefix(bucketName)))+ .stream()+ .filter(object -> prefix == null || object.getKey().startsWith(prefix))+ .forEach(versions::add);// Also include non-versioned objects (no #v# in storage key, versionId == null).// These are objects uploaded when versioning was disabled or before versioning was enabled.@@ -2844,8 +3052,22 @@ public class S3Service implements Resettable, ResourceProvider {objectStore.scan(key -> key.startsWith(fullPrefix) && !key.contains("#v#")).stream().filter(obj -> obj.getVersionId() == null)+ // A promoted null version is indexed under both the plain latest key and its+ // reserved history key. That history entry already appears in the scan above, so+ // adding the plain alias would list it twice.+ .filter(obj -> objectStore.get(versionedKey(bucketName, obj.getKey(), "null"))+ .filter(history -> obj.getKey().equals(history.getKey()))+ .filter(history -> history.getVersionId() == null)+ .isEmpty()).forEach(versions::add);+ Map<ListedVersionIdentity, S3Object> distinctVersions = new LinkedHashMap<>();+ for (S3Object version : versions) {+ distinctVersions.putIfAbsent(+ new ListedVersionIdentity(version.getKey(), storedVersionId(version)), version);+ }+ versions = new ArrayList<>(distinctVersions.values());+List<String> commonPrefixes = List.of();if (delimiter != null && !delimiter.isEmpty()) {Set<String> prefixSet = new LinkedHashSet<>();@@ -3096,8 +3318,8 @@ public class S3Service implements Resettable, ResourceProvider {AccountAwareStorageBackend.OwnedEntry<Bucket> ownedBucket = resolveBucketEntry(bucketName).orElseThrow(() -> new AwsException("NoSuchBucket","The specified bucket does not exist.", 404));- S3Object object = resolveObjectForAccount(ownedBucket.account(), versionStoreKey(bucketName, key, versionId))- .filter(existing -> isNamedVersion(existing, versionId))+ S3Object object = findStoredVersionForAccount(ownedBucket.account(), bucketName, key, versionId)+ .map(StoredVersion::object).orElseThrow(() -> versionId != null? new AwsException("NoSuchVersion", "The specified version does not exist.", 404): new AwsException("NoSuchKey", "The specified key does not exist.", 404));@@ -3350,8 +3572,8 @@ public class S3Service implements Resettable, ResourceProvider {* {@link #isNamedVersion}); a delete-marker latest reads as absent.*/private S3Object resolveParentObject(String bucketName, String key, String versionId) {- S3Object object = resolveObject(versionStoreKey(bucketName, key, versionId))- .filter(existing -> isNamedVersion(existing, versionId))+ S3Object object = findStoredVersion(bucketName, key, versionId)+ .map(StoredVersion::object).orElseThrow(() -> new AwsException("NoSuchKey","The specified key does not exist.", 404));if (object.isDeleteMarker()) {@@ -4124,53 +4346,57 @@ public class S3Service implements Resettable, ResourceProvider {public void putObjectRetention(String bucketName, String key, String versionId,String mode, Instant retainUntil, boolean bypassGovernance) {- AccountAwareStorageBackend.OwnedEntry<S3Object> ownedObject =- getLockableObjectEntry(bucketName, key, versionId);- S3Object obj = ownedObject.value();-- // A mode without a date, or a mode S3 does not know, is malformed, as S3 was recorded- // answering; a date without a mode is refused the same way, since GetObjectRetention reads a- // retention by its mode. A retention naming neither removes it.- if ((mode == null) != (retainUntil == null)- || (mode != null && !"GOVERNANCE".equals(mode) && !"COMPLIANCE".equals(mode))) {- throw new AwsException("MalformedXML",- "The XML you provided was not well-formed or did not validate against our published schema", 400);- }+ Bucket bucket = resolveBucket(bucketName)+ .orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404));+ synchronized (bucket) {+ AccountAwareStorageBackend.OwnedEntry<S3Object> ownedObject =+ getLockableObjectEntry(bucketName, key, versionId);+ S3Object obj = ownedObject.value();- boolean activeComplianceRetention = "COMPLIANCE".equals(obj.getObjectLockMode())- && obj.getRetainUntilDate() != null- && Instant.now().isBefore(obj.getRetainUntilDate());+ // A mode without a date, or a mode S3 does not know, is malformed, as S3 was recorded+ // answering; a date without a mode is refused the same way, since GetObjectRetention reads a+ // retention by its mode. A retention naming neither removes it.+ if ((mode == null) != (retainUntil == null)+ || (mode != null && !"GOVERNANCE".equals(mode) && !"COMPLIANCE".equals(mode))) {+ throw new AwsException("MalformedXML",+ "The XML you provided was not well-formed or did not validate against our published schema", 400);+ }- // Active COMPLIANCE mode cannot be changed or removed, even when the- // retention date is unchanged or extended.- if (activeComplianceRetention && !"COMPLIANCE".equals(mode)) {- throw new AwsException("AccessDenied",- "COMPLIANCE retention mode cannot be changed", 403);- }+ boolean activeComplianceRetention = "COMPLIANCE".equals(obj.getObjectLockMode())+ && obj.getRetainUntilDate() != null+ && Instant.now().isBefore(obj.getRetainUntilDate());- // Active COMPLIANCE mode: retainUntil cannot be shortened.- if (activeComplianceRetention- && retainUntil != null- && retainUntil.isBefore(obj.getRetainUntilDate())) {- throw new AwsException("AccessDenied",- "COMPLIANCE retention period cannot be shortened", 403);- }+ // Active COMPLIANCE mode cannot be changed or removed, even when the+ // retention date is unchanged or extended.+ if (activeComplianceRetention && !"COMPLIANCE".equals(mode)) {+ throw new AwsException("AccessDenied",+ "COMPLIANCE retention mode cannot be changed", 403);+ }- // Check bypass permission for existing governance lock when shortening/removing- if ("GOVERNANCE".equals(obj.getObjectLockMode())- && obj.getRetainUntilDate() != null- && Instant.now().isBefore(obj.getRetainUntilDate())- && !bypassGovernance) {- if (retainUntil == null || retainUntil.isBefore(obj.getRetainUntilDate())) {+ // Active COMPLIANCE mode: retainUntil cannot be shortened.+ if (activeComplianceRetention+ && retainUntil != null+ && retainUntil.isBefore(obj.getRetainUntilDate())) {throw new AwsException("AccessDenied",- "Object is protected by GOVERNANCE retention", 403);+ "COMPLIANCE retention period cannot be shortened", 403);}- }- obj.setObjectLockMode(mode);- obj.setRetainUntilDate(retainUntil);- putObjectMetadataForAccount(ownedObject.account(), bucketName, key, obj);- LOG.debugv("Set retention on {0}/{1}: mode={2}, until={3}", bucketName, key, mode, retainUntil);+ // Check bypass permission for existing governance lock when shortening/removing+ if ("GOVERNANCE".equals(obj.getObjectLockMode())+ && obj.getRetainUntilDate() != null+ && Instant.now().isBefore(obj.getRetainUntilDate())+ && !bypassGovernance) {+ if (retainUntil == null || retainUntil.isBefore(obj.getRetainUntilDate())) {+ throw new AwsException("AccessDenied",+ "Object is protected by GOVERNANCE retention", 403);+ }+ }++ obj.setObjectLockMode(mode);+ obj.setRetainUntilDate(retainUntil);+ putObjectMetadataForAccount(ownedObject.account(), bucketName, key, obj);+ LOG.debugv("Set retention on {0}/{1}: mode={2}, until={3}", bucketName, key, mode, retainUntil);+ }}public S3Object getObjectRetention(String bucketName, String key, String versionId) {@@ -4188,12 +4414,16 @@ public class S3Service implements Resettable, ResourceProvider {}public void putObjectLegalHold(String bucketName, String key, String versionId, String status) {- AccountAwareStorageBackend.OwnedEntry<S3Object> ownedObject =- getLockableObjectEntry(bucketName, key, versionId);- S3Object obj = ownedObject.value();- obj.setLegalHoldStatus(status);- putObjectMetadataForAccount(ownedObject.account(), bucketName, key, obj);- LOG.debugv("Set legal hold on {0}/{1}: {2}", bucketName, key, status);+ Bucket bucket = resolveBucket(bucketName)+ .orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404));+ synchronized (bucket) {+ AccountAwareStorageBackend.OwnedEntry<S3Object> ownedObject =+ getLockableObjectEntry(bucketName, key, versionId);+ S3Object obj = ownedObject.value();+ obj.setLegalHoldStatus(status);+ putObjectMetadataForAccount(ownedObject.account(), bucketName, key, obj);+ LOG.debugv("Set legal hold on {0}/{1}: {2}", bucketName, key, status);+ }}public S3Object getObjectLegalHold(String bucketName, String key, String versionId) {@@ -4282,6 +4512,7 @@ public class S3Service implements Resettable, ResourceProvider {public MultipartUpload initiateMultipartUpload(String bucket, String key, String contentType,Map<String, String> metadata, String checksumType,PutObjectOptions objectOptions) {+ requirePublicObjectKey(key);String storageClass = objectOptions.getStorageClass();String contentDisposition = objectOptions.getContentDisposition();String serverSideEncryption = objectOptions.getServerSideEncryption();@@ -5365,8 +5596,8 @@ public class S3Service implements Resettable, ResourceProvider {/*** The versions of one key in the order they were written: by last-modified time, then by the* sequencer each write takes under the bucket lock, so versions stored within one millisecond- * keep their order. The sequencer is not persisted, so across a restart such versions fall back- * to version id order, which is at least the same on every read.+ * keep their order, including after persisted metadata is reloaded. Older stored entries that+ * predate the sequencer fall back to version id order, which is at least stable across reads.*/private static final Comparator<S3Object> WRITE_ORDER = Comparator.comparing(S3Object::getLastModified)@@ -5561,7 +5792,7 @@ public class S3Service implements Resettable, ResourceProvider {}List<S3Object> versions = new ArrayList<>();for (S3Object version : snapshot.versions()) {- objectStore.get(versionedKey(bucketName, key, version.getVersionId())).ifPresent(versions::add);+ objectStore.get(versionedKey(bucketName, key, storedVersionId(version))).ifPresent(versions::add);}return new KeyHistory(current, versions);}@@ -5601,7 +5832,7 @@ public class S3Service implements Resettable, ResourceProvider {if (successorCreated != null && !lockedAgainstDeletion(version)&& lifecycle.expiresNoncurrentVersion(key, version.getTags(), version.getSize(),successorCreated, newer, now)) {- deletions.add(version.getVersionId());+ deletions.add(storedVersionId(version));}successorCreated = version.getLastModified();}@@ -5709,18 +5940,20 @@ public class S3Service implements Resettable, ResourceProvider {public void putObjectAcl(String bucketName, String key, String versionId, String bodyAcl, String cannedAcl,String grantRead, String grantWrite, String grantFullControl,String grantReadAcp, String grantWriteAcp) {- AccountAwareStorageBackend.OwnedEntry<S3Object> ownedObject =- getStoredObjectEntry(bucketName, key, versionId);- S3Object obj = ownedObject.value();Bucket bucket = resolveBucket(bucketName).orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404));- rejectAclWhenOwnershipEnforced(bucket);- String resolvedAcl = resolveObjectAclXml(ownedObject.account(), cannedAcl, grantRead,- grantWrite, grantFullControl, grantReadAcp, grantWriteAcp);- String newAcl = resolvedAcl != null ? resolvedAcl : (bodyAcl.isBlank() ? null : bodyAcl);- rejectPublicAclWhenBlocked(bucketName, newAcl);- obj.setAcl(newAcl);- putObjectMetadataForAccount(ownedObject.account(), bucketName, key, obj);+ synchronized (bucket) {+ AccountAwareStorageBackend.OwnedEntry<S3Object> ownedObject =+ getStoredObjectEntry(bucketName, key, versionId);+ S3Object obj = ownedObject.value();+ rejectAclWhenOwnershipEnforced(bucket);+ String resolvedAcl = resolveObjectAclXml(ownedObject.account(), cannedAcl, grantRead,+ grantWrite, grantFullControl, grantReadAcp, grantWriteAcp);+ String newAcl = resolvedAcl != null ? resolvedAcl : (bodyAcl.isBlank() ? null : bodyAcl);+ rejectPublicAclWhenBlocked(bucketName, newAcl);+ obj.setAcl(newAcl);+ putObjectMetadataForAccount(ownedObject.account(), bucketName, key, obj);+ }}private static void rejectAclWhenOwnershipEnforced(Bucket bucket) {@@ -6812,6 +7045,7 @@ public class S3Service implements Resettable, ResourceProvider {copy.setLegalHoldStatus(source.getLegalHoldStatus());copy.setAcl(source.getAcl());copy.setDataGeneration(source.getDataGeneration());+ copy.setEventSequencer(source.getEventSequencer());return copy;}@@ -6989,6 +7223,16 @@ public class S3Service implements Resettable, ResourceProvider {objectStore.put(storeKey, object);}+ private void deleteObjectForAccount(String accountId, String storeKey) {+ if (objectStore instanceof AccountAwareStorageBackend<?> aware) {+ @SuppressWarnings("unchecked")+ AccountAwareStorageBackend<S3Object> typed = (AccountAwareStorageBackend<S3Object>) aware;+ typed.deleteForAccount(accountId, storeKey);+ return;+ }+ objectStore.delete(storeKey);+ }+/*** Persists a metadata change to one stored object. The current version of a versioned object* is indexed under both its version key and the latest key, so both entries are written: the@@ -7006,10 +7250,20 @@ public class S3Service implements Resettable, ResourceProvider {String versionId = object.getVersionId();S3Object latest = resolveObjectForAccount(accountId, latestKey).orElse(null);boolean isCurrent = latest == object- || (versionId != null && latest != null && !latest.isDeleteMarker()- && versionId.equals(latest.getVersionId()));- if (versionId != null) {- putObjectForAccount(accountId, versionedKey(bucketName, key, versionId), object);+ || (latest != null && !latest.isDeleteMarker()+ && Objects.equals(versionId, latest.getVersionId())+ && Objects.equals(object.getDataGeneration(), latest.getDataGeneration()));+ String historyKey = versionedKey(bucketName, key, storedVersionId(object));+ boolean hasHistoryEntry = versionId != null+ || resolveObjectForAccount(accountId, historyKey)+ .filter(existing -> key.equals(existing.getKey()))+ .filter(existing -> existing.getVersionId() == null)+ .filter(existing -> Objects.equals(+ existing.getDataGeneration(), object.getDataGeneration()))+ .isPresent();+ if (hasHistoryEntry) {+ object.setLatest(isCurrent);+ putObjectForAccount(accountId, historyKey, object);}if (isCurrent) {putObjectForAccount(accountId, latestKey, object);@@ -7022,13 +7276,37 @@ public class S3Service implements Resettable, ResourceProvider {}private String objectKey(String bucketName, String key) {+ requirePublicObjectKey(key);return bucketName + "/" + key;}+ // S3 object keys can contain "#v#", so the legacy version-key suffix can collide with a+ // plain object. Null-version history is new and needs no migration; keep it in a namespace+ // that no path-safe bucket/object key can reach instead of widening that collision surface.+ private static final String NULL_VERSION_STORAGE_PREFIX = "\0null-version/";++ private static void requirePublicObjectKey(String key) {+ if (key.startsWith(NULL_VERSION_STORAGE_PREFIX)) {+ throw new AwsException("InvalidArgument", "The specified object key is not valid.", 400);+ }+ }++ private String nullVersionPrefix(String bucketName) {+ return bucketName + "/" + NULL_VERSION_STORAGE_PREFIX;+ }+private String versionedKey(String bucketName, String key, String versionId) {+ requirePublicObjectKey(key);+ if ("null".equals(versionId)) {+ return nullVersionPrefix(bucketName) + key;+ }return bucketName + "/" + key + "#v#" + versionId;}+ private static String storedVersionId(S3Object object) {+ return object.getVersionId() != null ? object.getVersionId() : "null";+ }+private static final String DATA_SUFFIX = ".s3data";// A 12-digit bucket name is valid on S3 and would otherwise collide with an account ID,@@ -7235,6 +7513,99 @@ public class S3Service implements Resettable, ResourceProvider {}}+ /** Preserves the canonical body under the literal null-version history path. */+ private void archiveCurrentFile(String accountId, String bucketName, String key) {+ if (inMemory) {+ byte[] data = memoryDataStore.get(physicalKey(accountId, bucketName, key));+ if (data == null) {+ throw new IllegalStateException("S3 object data is missing for " + bucketName + "/" + key);+ }+ memoryDataStore.put(physicalVersionedKey(accountId, bucketName, key, "null"), data);+ return;+ }+ linkIntoPlace(resolveObjectPathForRead(accountId, bucketName, key),+ resolveVersionedPath(accountId, bucketName, key, "null"));+ }++ private void restoreNullAfterFailedWrite(String accountId, String bucketName, String key,+ S3Object previous, S3Object priorHistory,+ String failedVersionId, RuntimeException failure) {+ try {+ if (inMemory) {+ memoryDataStore.put(physicalKey(accountId, bucketName, key),+ memoryDataStore.get(physicalVersionedKey(accountId, bucketName, key, "null")));+ } else {+ linkIntoPlace(resolveVersionedPath(accountId, bucketName, key, "null"),+ resolveObjectPath(accountId, bucketName, key));+ }+ S3Object restored = copyObject(previous);+ restored.setDataGeneration(UUID.randomUUID().toString());+ putObjectForAccount(accountId, objectKey(bucketName, key), restored);+ deleteObjectForAccount(accountId, versionedKey(bucketName, key, failedVersionId));+ if (priorHistory != null) {+ priorHistory.setDataGeneration(restored.getDataGeneration());+ putObjectForAccount(accountId, versionedKey(bucketName, key, "null"), priorHistory);+ } else {+ deleteObjectForAccount(accountId, versionedKey(bucketName, key, "null"));+ deleteVersionedFile(accountId, bucketName, key, "null");+ }+ deleteVersionedFile(accountId, bucketName, key, failedVersionId);+ } catch (RuntimeException recoveryFailure) {+ failure.addSuppressed(recoveryFailure);+ LOG.errorv(recoveryFailure, "Failed to restore null version after failed write: {0}/{1}", bucketName, key);+ }+ }++ private void restoreCurrentAfterFailedPublication(String accountId, String bucketName, String key,+ S3Object previous, RuntimeException failure) {+ try {+ if (previous.isDeleteMarker()) {+ deleteFile(accountId, bucketName, key);+ } else if (inMemory) {+ byte[] original = memoryDataStore.get(+ physicalVersionedKey(accountId, bucketName, key, storedVersionId(previous)));+ memoryDataStore.put(physicalKey(accountId, bucketName, key), Objects.requireNonNull(original));+ } else {+ linkIntoPlace(resolveVersionedPathForRead(accountId, bucketName, key, storedVersionId(previous)),+ resolveObjectPath(accountId, bucketName, key));+ }+ S3Object restored = copyObject(previous);+ restored.setDataGeneration(UUID.randomUUID().toString());+ putObjectForAccount(accountId, versionedKey(bucketName, key, storedVersionId(restored)), restored);+ putObjectForAccount(accountId, objectKey(bucketName, key), restored);+ } catch (RuntimeException recoveryFailure) {+ failure.addSuppressed(recoveryFailure);+ LOG.errorv(recoveryFailure, "Failed to restore current version after failed publication: {0}/{1}",+ bucketName, key);+ }+ }++ private boolean checkArchivedNullLock(String accountId, String bucketName, String key,+ boolean bypassGovernance) {+ Optional<S3Object> archived = resolveObjectForAccount(accountId, versionedKey(bucketName, key, "null"))+ .filter(object -> key.equals(object.getKey()))+ .filter(object -> object.getVersionId() == null && !object.isDeleteMarker());+ archived.ifPresent(object -> checkLockProtection(object, bypassGovernance));+ return archived.isPresent();+ }++ private void deleteArchivedNullVersion(+ String accountId, String bucketName, String key, boolean deleteAnnotations) {+ String storeKey = versionedKey(bucketName, key, "null");+ boolean archived = resolveObjectForAccount(accountId, storeKey)+ .filter(object -> key.equals(object.getKey()))+ .filter(object -> object.getVersionId() == null)+ .isPresent();+ if (!archived) {+ return;+ }+ deleteObjectForAccount(accountId, storeKey);+ deleteVersionedFile(accountId, bucketName, key, "null");+ if (deleteAnnotations) {+ deleteAllAnnotationsFor(annotationParentKey(bucketName, key, null));+ }+ }+private byte[] readVersionedFile(String accountId, String bucketName, String key, String versionId) {if (inMemory) {return memoryDataStore.get(physicalVersionedKey(accountId, bucketName, key, versionId));@@ -7379,11 +7750,15 @@ public class S3Service implements Resettable, ResourceProvider {}private void deleteFile(String bucketName, String key) {+ deleteFile(ownerId(), bucketName, key);+ }++ private void deleteFile(String accountId, String bucketName, String key) {if (inMemory) {- memoryDataStore.remove(physicalKey(bucketName, key));+ memoryDataStore.remove(physicalKey(accountId, bucketName, key));return;}- Path filePath = resolveObjectPath(bucketName, key);+ Path filePath = resolveObjectPath(accountId, bucketName, key);ReentrantLock lock = diskFileLock(filePath);lock.lock();try {@@ -7396,11 +7771,15 @@ public class S3Service implements Resettable, ResourceProvider {}private void deleteVersionedFile(String bucketName, String key, String versionId) {+ deleteVersionedFile(ownerId(), bucketName, key, versionId);+ }++ private void deleteVersionedFile(String accountId, String bucketName, String key, String versionId) {if (inMemory) {- memoryDataStore.remove(physicalVersionedKey(bucketName, key, versionId));+ memoryDataStore.remove(physicalVersionedKey(accountId, bucketName, key, versionId));return;}- Path filePath = resolveVersionedPath(bucketName, key, versionId);+ Path filePath = resolveVersionedPath(accountId, bucketName, key, versionId);ReentrantLock lock = diskFileLock(filePath);lock.lock();try {diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/model/S3Object.java b/src/main/java/io/github/hectorvent/floci/services/s3/model/S3Object.javaindex 8ebefd6417..ead3881717 100644--- a/src/main/java/io/github/hectorvent/floci/services/s3/model/S3Object.java+++ b/src/main/java/io/github/hectorvent/floci/services/s3/model/S3Object.java@@ -54,8 +54,8 @@ public class S3Object {private String dataGeneration;// Internal-only s3.object.sequencer of the write that stored this instance, assigned under the- // bucket lock so concurrent writes to one key get values in the order they were stored.- @JsonIgnore+ // bucket lock so concurrent writes to one key get values in the order they were stored. Unlike+ // dataGeneration, this is persisted because version promotion needs the order after restart.private String eventSequencer;public S3Object() {diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3LifecycleServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3LifecycleServiceTest.javaindex 21a7cadaa4..1125135d08 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3LifecycleServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3LifecycleServiceTest.java@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.s3;import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.ObjectMapper;+import io.github.hectorvent.floci.core.common.AwsException;import io.github.hectorvent.floci.core.common.RegionResolver;import io.github.hectorvent.floci.core.storage.InMemoryStorage;import io.github.hectorvent.floci.services.lambda.model.InvocationType;@@ -126,6 +127,24 @@ class S3LifecycleServiceTest {assertThat(versionIds("a.txt"), contains(v3.getVersionId(), v2.getVersionId()));}+ @Test+ void noncurrentExpirationDeletesAnArchivedNullVersionWithoutChangingTheCurrentVersion() {+ put("a.txt");+ s3Service.putBucketVersioning(BUCKET, "Enabled");+ S3Object current = put("a.txt");+ lifecycle(rule("Enabled", "<Filter></Filter>",+ "<NoncurrentVersionExpiration><NoncurrentDays>1</NoncurrentDays>"+ + "</NoncurrentVersionExpiration>"));++ s3Service.applyLifecycleExpiration(Instant.now().plus(Duration.ofDays(3)));++ assertThat(versionIds("a.txt"), contains(current.getVersionId()));+ assertEquals("data", new String(s3Service.getObject(BUCKET, "a.txt").getData(), StandardCharsets.UTF_8));+ AwsException missing = assertThrows(AwsException.class,+ () -> s3Service.getObject(BUCKET, "a.txt", "null"));+ assertEquals("NoSuchVersion", missing.getErrorCode());+ }+@Testvoid aNoncurrentVersionUnderALegalHoldIsKept() {s3Service.putBucketVersioning(BUCKET, "Enabled");diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3NullVersionDiskTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3NullVersionDiskTest.javaindex eaa6c332c3..d362652f74 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3NullVersionDiskTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3NullVersionDiskTest.java@@ -3,20 +3,25 @@ package io.github.hectorvent.floci.services.s3;import io.github.hectorvent.floci.core.common.AwsException;import io.github.hectorvent.floci.core.storage.InMemoryStorage;import io.github.hectorvent.floci.services.s3.model.CopyObjectOptions;+import io.github.hectorvent.floci.services.s3.model.S3Object;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;import org.junit.jupiter.api.function.Executable;import org.junit.jupiter.api.io.TempDir;import java.io.IOException;+import java.io.InputStream;import java.nio.charset.StandardCharsets;+import java.nio.file.Files;import java.nio.file.Path;import java.util.List;import java.util.Map;+import java.util.Optional;import static org.junit.jupiter.api.Assertions.assertArrayEquals;import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertThrows;+import static org.junit.jupiter.api.Assertions.assertTrue;/*** A request naming {@code versionId=null} in disk-storage mode, where the null version's bytes are@@ -44,6 +49,9 @@ class S3NullVersionDiskTest {try (S3Service.ObjectRead read = s3Service.openObject("bucket", "old.txt", "null")) {assertArrayEquals(bytes("null version"), read.body().readAllBytes());}+ try (InputStream stream = s3Service.openObjectStream("bucket", "old.txt", "null")) {+ assertArrayEquals(bytes("null version"), stream.readAllBytes());+ }s3Service.copyObject("bucket", "old.txt", "bucket", "copy.txt", "null", new CopyObjectOptions());@@ -61,6 +69,86 @@ class S3NullVersionDiskTest {}}+ @Test+ void anArchivedNullVersionUsesTheVersionedFileAndCanBePromoted() throws IOException {+ String currentVersionId = s3Service.putObject(+ "bucket", "old.txt", bytes("versioned replacement"), "text/plain", Map.of()).getVersionId();+ Path archived = tempDir.resolve("s3").resolve(".accounts").resolve("000000000000")+ .resolve(".versions").resolve("bucket").resolve("old.txt").resolve("null.s3data");++ assertTrue(Files.exists(archived));+ assertArrayEquals(bytes("null version"), Files.readAllBytes(archived));+ try (S3Service.ObjectRead read = s3Service.openObject("bucket", "old.txt", "null")) {+ assertArrayEquals(bytes("null version"), read.body().readAllBytes());+ }+ s3Service.copyObject("bucket", "old.txt", "bucket", "archived-copy.txt", "null",+ new CopyObjectOptions());+ assertArrayEquals(bytes("null version"), s3Service.getObject("bucket", "archived-copy.txt").getData());++ s3Service.deleteObject("bucket", "old.txt", currentVersionId);++ assertArrayEquals(bytes("null version"), s3Service.getObject("bucket", "old.txt").getData());+ assertEquals(1, s3Service.listObjectVersions("bucket", "old.txt", 1000, null).versions().size());+ s3Service.deleteObject("bucket", "old.txt", "null");+ AwsException missing = assertThrows(AwsException.class,+ () -> s3Service.getObject("bucket", "old.txt"));+ assertEquals("NoSuchKey", missing.getErrorCode());+ }++ @Test+ void archivedNullReadsRetryWhenASuspendedPutReplacesTheHistoryFile() throws IOException {+ ArchivedNullReplacementStore objectStore = new ArchivedNullReplacementStore();+ S3Service service = new S3Service(+ new InMemoryStorage<>(), objectStore, tempDir.resolve("racing-s3"), false);+ service.createBucket("racing-bucket", "us-east-1");+ List<String> keys = List.of("get.txt", "stream.txt", "copy.txt");+ for (String key : keys) {+ service.putObject("racing-bucket", key, bytes("original"), "text/plain", Map.of());+ }+ service.putBucketVersioning("racing-bucket", "Enabled");+ for (String key : keys) {+ service.putObject("racing-bucket", key, bytes("numbered"), "text/plain", Map.of());+ }+ service.putBucketVersioning("racing-bucket", "Suspended");++ objectStore.arm(service, "get.txt");+ assertArrayEquals(bytes("replacement"),+ service.getObject("racing-bucket", "get.txt", "null").getData());++ objectStore.arm(service, "stream.txt");+ try (S3Service.ObjectRead read = service.openObject("racing-bucket", "stream.txt", "null")) {+ assertArrayEquals(bytes("replacement"), read.body().readAllBytes());+ }++ objectStore.arm(service, "copy.txt");+ service.copyObject("racing-bucket", "copy.txt", "racing-bucket", "copied.txt", "null",+ new CopyObjectOptions());+ assertArrayEquals(bytes("replacement"), service.getObject("racing-bucket", "copied.txt").getData());+ }++ private static final class ArchivedNullReplacementStore extends InMemoryStorage<String, S3Object> {+ private S3Service service;+ private String armedKey;++ private void arm(S3Service service, String key) {+ this.service = service;+ armedKey = key;+ }++ @Override+ public Optional<S3Object> get(String key) {+ Optional<S3Object> result = super.get(key);+ String target = "racing-bucket/\0null-version/" + armedKey;+ if (armedKey != null && target.equals(key)) {+ String replacing = armedKey;+ armedKey = null;+ service.putObject("racing-bucket", replacing,+ bytes("replacement"), "text/plain", Map.of());+ }+ return result;+ }+ }+private static byte[] bytes(String text) {return text.getBytes(StandardCharsets.UTF_8);}diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3NullVersionLockPromotionTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3NullVersionLockPromotionTest.javanew file mode 100644index 0000000000..c8bfb13210--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3NullVersionLockPromotionTest.java@@ -0,0 +1,109 @@+package io.github.hectorvent.floci.services.s3;++import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.core.storage.InMemoryStorage;+import io.github.hectorvent.floci.services.s3.model.Bucket;+import io.github.hectorvent.floci.services.s3.model.S3Object;+import org.junit.jupiter.api.io.TempDir;+import org.junit.jupiter.params.ParameterizedTest;+import org.junit.jupiter.params.provider.ValueSource;++import java.nio.charset.StandardCharsets;+import java.nio.file.Path;+import java.time.Instant;+import java.util.Map;+import java.util.Optional;+import java.util.concurrent.CountDownLatch;+import java.util.concurrent.ExecutionException;+import java.util.concurrent.FutureTask;+import java.util.concurrent.TimeUnit;+import java.util.concurrent.TimeoutException;++import static org.junit.jupiter.api.Assertions.*;++class S3NullVersionLockPromotionTest {++ @TempDir+ Path tempDir;++ @ParameterizedTest+ @ValueSource(strings = {"legal-hold", "retention", "acl"})+ void promotionCannotDropAnAcknowledgedMetadataUpdate(String operation) throws Exception {+ InMemoryStorage<String, Bucket> buckets = new InMemoryStorage<>();+ PromotionStore objects = new PromotionStore();+ S3Service service = new S3Service(buckets, objects, tempDir, true);+ service.createBucket("bucket", "us-east-1");+ service.putBucketOwnershipControls("bucket",+ "<OwnershipControls><Rule><ObjectOwnership>BucketOwnerPreferred</ObjectOwnership></Rule></OwnershipControls>");+ service.putObject("bucket", "key", "original".getBytes(StandardCharsets.UTF_8), "text/plain", Map.of());+ service.setBucketObjectLockEnabled("bucket");+ service.putBucketVersioning("bucket", "Enabled");+ String numbered = service.putObject("bucket", "key", "numbered".getBytes(StandardCharsets.UTF_8),+ "text/plain", Map.of()).getVersionId();+ objects.arm(buckets.get("bucket").orElseThrow(), () -> service.deleteObject("bucket", "key", numbered));++ switch (operation) {+ case "legal-hold" -> service.putObjectLegalHold("bucket", "key", "null", "ON");+ case "retention" -> service.putObjectRetention("bucket", "key", "null", "COMPLIANCE",+ Instant.now().plusSeconds(3600), false);+ case "acl" -> service.putObjectAcl("bucket", "key", "null", "", "public-read",+ null, null, null, null, null);+ default -> throw new IllegalArgumentException(operation);+ }+ objects.promotion.get(5, TimeUnit.SECONDS);++ assertNull(service.getObject("bucket", "key").getVersionId());+ if ("acl".equals(operation)) {+ assertTrue(service.getObjectAcl("bucket", "key", "null").contains("AllUsers"));+ } else {+ if ("legal-hold".equals(operation)) {+ assertEquals("ON", service.getObjectLegalHold("bucket", "key", "null").getLegalHoldStatus());+ } else {+ assertEquals("COMPLIANCE", service.getObjectRetention("bucket", "key", "null").getObjectLockMode());+ }+ assertEquals("AccessDenied", assertThrows(AwsException.class,+ () -> service.deleteObject("bucket", "key", "null", true)).getErrorCode());+ }+ }++ private static final class PromotionStore extends InMemoryStorage<String, S3Object> {+ private Bucket bucket;+ private Runnable promote;+ private FutureTask<Void> promotion;++ private void arm(Bucket bucket, Runnable promote) {+ this.bucket = bucket;+ this.promote = promote;+ }++ @Override+ public Optional<S3Object> get(String key) {+ Optional<S3Object> object = super.get(key);+ if (promote != null && "bucket/\0null-version/key".equals(key)) {+ Runnable action = promote;+ promote = null;+ CountDownLatch started = new CountDownLatch(1);+ promotion = new FutureTask<>(() -> {+ started.countDown();+ action.run();+ return null;+ });+ Thread.ofPlatform().start(promotion);+ try {+ assertTrue(started.await(5, TimeUnit.SECONDS));+ if (!Thread.holdsLock(bucket)) {+ // Expose a stale target if resolution is outside the monitor. With the+ // monitor held, promotion follows the acknowledged metadata update.+ promotion.get(5, TimeUnit.SECONDS);+ }+ } catch (InterruptedException e) {+ Thread.currentThread().interrupt();+ throw new IllegalStateException(e);+ } catch (ExecutionException | TimeoutException e) {+ throw new IllegalStateException(e);+ }+ }+ return object;+ }+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3NullVersionReviewTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3NullVersionReviewTest.javanew file mode 100644index 0000000000..e23cec6cf8--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3NullVersionReviewTest.java@@ -0,0 +1,559 @@+package io.github.hectorvent.floci.services.s3;++import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.core.storage.InMemoryStorage;+import io.github.hectorvent.floci.services.s3.model.Bucket;+import io.github.hectorvent.floci.services.s3.model.CopyObjectOptions;+import io.github.hectorvent.floci.services.s3.model.S3Object;+import org.junit.jupiter.api.Test;+import org.junit.jupiter.api.io.TempDir;+import org.junit.jupiter.params.ParameterizedTest;+import org.junit.jupiter.params.provider.ValueSource;++import java.io.ByteArrayInputStream;+import java.io.IOException;+import java.io.UncheckedIOException;+import java.lang.reflect.Field;+import java.lang.reflect.Method;+import java.nio.charset.StandardCharsets;+import java.nio.file.Files;+import java.nio.file.Path;+import java.time.Instant;+import java.util.Arrays;+import java.util.List;+import java.util.Map;+import java.util.Optional;+import java.util.concurrent.atomic.AtomicInteger;+import java.util.concurrent.atomic.AtomicReference;+import java.util.function.Consumer;+import java.util.function.Function;+import java.util.stream.Stream;++import static org.junit.jupiter.api.Assertions.*;++class S3NullVersionReviewTest {++ @TempDir+ Path tempDir;++ @ParameterizedTest+ @ValueSource(booleans = {true, false})+ void rollbackThenPutListsTheReplacementNull(boolean memory) {+ S3Service service = rolledBackService(memory);+ S3Object replacement = service.putObject("bucket", "key", bytes("replacement"), "text/plain", Map.of());+ assertArrayEquals(bytes("replacement"), service.getObject("bucket", "key", "null").getData());+ List<S3Object> versions = service.listObjectVersions("bucket", "key", 100, null).versions();+ assertEquals(1, versions.size());+ assertEquals(replacement.getETag(), versions.getFirst().getETag(),+ "listing must describe the retrievable null version");+ }++ @ParameterizedTest+ @ValueSource(booleans = {true, false})+ void rollbackThenDeleteDoesNotResurrectNull(boolean memory) {+ S3Service service = rolledBackService(memory);+ service.deleteObject("bucket", "key");+ assertAll(+ () -> assertTrue(service.listObjectVersions("bucket", "key", 100, null).versions().isEmpty(),+ "plain DELETE must remove the only null version"),+ () -> assertFalse(service.objectExists("bucket", "key")),+ () -> assertEquals("NoSuchVersion", assertThrows(AwsException.class,+ () -> service.getObject("bucket", "key", "null")).getErrorCode()));+ }++ @ParameterizedTest+ @ValueSource(booleans = {true, false})+ void rollbackCannotEraseAnnotationsOfLockedArchivedNull(boolean memory) {+ S3Service service = new S3Service(new InMemoryStorage<>(), new InMemoryStorage<>(), tempDir, memory);+ service.createBucket("bucket", "us-east-1");+ service.putObject("bucket", "key", bytes("original"), "text/plain", Map.of());+ service.putObjectAnnotation("bucket", "key", "note", null, bytes("annotation"), null, null);+ service.setBucketObjectLockEnabled("bucket");+ service.putObjectLegalHold("bucket", "key", "null", "ON");+ String configuration = service.snapshotBucketConfiguration("bucket");+ service.putBucketVersioning("bucket", "Enabled");+ service.putObject("bucket", "key", bytes("numbered"), "text/plain", Map.of());+ service.restoreBucketConfiguration("bucket", configuration);++ assertAll(+ () -> assertEquals("AccessDenied", assertThrows(AwsException.class,+ () -> service.putObject("bucket", "key", bytes("replacement"), "text/plain", Map.of())).getErrorCode()),+ () -> assertEquals("AccessDenied", assertThrows(AwsException.class,+ () -> service.deleteObject("bucket", "key")).getErrorCode()),+ () -> assertArrayEquals(bytes("annotation"), service.readObjectAnnotationPayload(+ service.getObjectAnnotation("bucket", "key", "note", "null"))),+ () -> assertArrayEquals(bytes("numbered"), service.getObject("bucket", "key").getData()));+ }++ private S3Service rolledBackService(boolean memory) {+ S3Service service = new S3Service(new InMemoryStorage<>(), new InMemoryStorage<>(), tempDir, memory);+ service.createBucket("bucket", "us-east-1");+ service.putObject("bucket", "key", bytes("original"), "text/plain", Map.of());+ String configuration = service.snapshotBucketConfiguration("bucket");+ service.putBucketVersioning("bucket", "Enabled");+ String version = service.putObject("bucket", "key", bytes("numbered"), "text/plain", Map.of()).getVersionId();+ service.deleteObject("bucket", "key", version);+ service.restoreBucketConfiguration("bucket", configuration);+ return service;+ }+++ @ParameterizedTest+ @ValueSource(booleans = {true, false})+ void neverVersionedBodyReadsDoNotHoldTheBucketMonitor(boolean explicitNull) throws Exception {+ InMemoryStorage<String, Bucket> buckets = new InMemoryStorage<>();+ S3Service service = new S3Service(buckets, new InMemoryStorage<>(), tempDir, true);+ service.createBucket("bucket", "us-east-1");+ service.putObject("bucket", "key", bytes("original"), "text/plain", Map.of());+ Bucket bucket = buckets.get("bucket").orElseThrow();+ AtomicInteger reads = new AtomicInteger();++ readSnapshot(service, explicitNull, ignored -> {+ assertFalse(Thread.holdsLock(bucket), "body reads must allow independent writes in never-versioned buckets");+ reads.incrementAndGet();+ return "body";+ });++ assertEquals(1, reads.get());+ }++ @ParameterizedTest+ @ValueSource(booleans = {true, false})+ void aReadSpanningFirstEnableAndRollbackRetriesThePromotedNull(boolean explicitNull) throws Exception {+ S3Service service = service(true, new InMemoryStorage<>());+ service.putObject("bucket", "key", bytes("original"), "text/plain", Map.of());+ String snapshot = service.snapshotBucketConfiguration("bucket");+ AtomicInteger reads = new AtomicInteger();++ Object result = readSnapshot(service, explicitNull, ignored -> {+ if (reads.getAndIncrement() == 0) {+ service.putBucketVersioning("bucket", "Enabled");+ String version = service.putObject("bucket", "key", bytes("replacement"), "text/plain", Map.of())+ .getVersionId();+ service.deleteObject("bucket", "key", version);+ service.restoreBucketConfiguration("bucket", snapshot);+ return "replacement";+ }+ return "original";+ });++ Method body = result.getClass().getDeclaredMethod("body");+ body.setAccessible(true);+ assertEquals("original", body.invoke(result));+ assertEquals(2, reads.get());+ }++ @ParameterizedTest+ @ValueSource(booleans = {true, false})+ void suspendedWritesAndDeletesPreserveLockedArchivedNulls(boolean memory) {+ for (String lock : List.of("ON", "COMPLIANCE", "GOVERNANCE")) {+ S3Service service = service(memory, new InMemoryStorage<>());+ service.putObject("bucket", "key", bytes("original"), "text/plain", Map.of());+ service.putBucketVersioning("bucket", "Enabled");+ service.putObject("bucket", "key", bytes("numbered"), "text/plain", Map.of());+ String source = service.putObject("bucket", "source", bytes("copy source"), "text/plain", Map.of())+ .getVersionId();+ service.setBucketObjectLockEnabled("bucket");+ if ("ON".equals(lock)) {+ service.putObjectLegalHold("bucket", "key", "null", "ON");+ } else {+ service.putObjectRetention("bucket", "key", "null", lock, Instant.now().plusSeconds(3600), false);+ }+ service.putBucketVersioning("bucket", "Suspended");++ assertDenied(() -> service.putObject("bucket", "key", bytes("replacement"), "text/plain", Map.of()));+ assertPreservedBodies(service);+ assertDenied(() -> service.copyObject("bucket", "source", "bucket", "key", source, new CopyObjectOptions()));+ assertPreservedBodies(service);+ assertDenied(() -> service.deleteObject("bucket", "key", null, false));+ assertPreservedBodies(service);+ if ("GOVERNANCE".equals(lock)) {+ service.deleteObject("bucket", "key", null, true);+ AwsException missing = assertThrows(AwsException.class,+ () -> service.getObject("bucket", "key", "null"));+ assertEquals("NoSuchVersion", missing.getErrorCode());+ } else {+ assertDenied(() -> service.deleteObject("bucket", "key", null, true));+ assertPreservedBodies(service);+ }+ }+ }++ private void assertPreservedBodies(S3Service service) {+ assertArrayEquals(bytes("numbered"), service.getObject("bucket", "key").getData());+ assertArrayEquals(bytes("original"), service.getObject("bucket", "key", "null").getData());+ }++ @ParameterizedTest+ @ValueSource(booleans = {true, false})+ void reservedKeysCannotReadOrMutateArchivedMetadata(boolean memory) throws IOException {+ S3Service service = service(memory, new InMemoryStorage<>());+ service.putObject("bucket", "key", bytes("original"), "text/plain", Map.of());+ service.putBucketVersioning("bucket", "Enabled");+ service.putObject("bucket", "key", bytes("numbered"), "text/plain", Map.of());+ String reserved = "\0null-version/key";+ List<Runnable> requests = List.of(+ () -> service.putObject("bucket", reserved, bytes("bad"), "text/plain", Map.of()),+ () -> service.getObject("bucket", reserved),+ () -> service.getObject("bucket", reserved, "null"),+ () -> service.headObject("bucket", reserved),+ () -> service.deleteObject("bucket", reserved),+ () -> service.deleteObject("bucket", reserved, "null"),+ () -> service.putObjectTagging("bucket", reserved, Map.of("bad", "tag")),+ () -> service.copyObject("bucket", "key", "bucket", reserved),+ () -> service.initiateMultipartUpload("bucket", reserved, "text/plain"),+ () -> service.putObjectFromStream("bucket", reserved, new ByteArrayInputStream(bytes("bad")),+ "text/plain", Map.of()));+ for (Runnable request : requests) {+ AwsException error = assertThrows(AwsException.class, request::run);+ assertEquals("InvalidArgument", error.getErrorCode());+ }+ assertArrayEquals(bytes("original"), service.getObject("bucket", "key", "null").getData());+ assertEquals(2, service.listObjectVersions("bucket", null, 1000, null).versions().size());+ }++ @ParameterizedTest+ @ValueSource(booleans = {true, false})+ void readsSpanningFailedPublicationRetryTheRestoredCurrentVersion(boolean explicitNull) throws Exception {+ PublicationFailureStore objects = new PublicationFailureStore();+ S3Service service = service(true, objects);+ service.putObject("bucket", "key", bytes("original"), "text/plain", Map.of());+ String snapshot = service.snapshotBucketConfiguration("bucket");+ service.putBucketVersioning("bucket", "Enabled");+ String numbered = service.putObject("bucket", "key", bytes("numbered"), "text/plain", Map.of())+ .getVersionId();+ service.deleteObject("bucket", "key", numbered);+ service.restoreBucketConfiguration("bucket", snapshot);+ AtomicInteger reads = new AtomicInteger();+ AtomicReference<String> observed = new AtomicReference<>();+ objects.remaining = 1;+ objects.afterPut = true;+ objects.beforeFailure = () -> observed.set(+ new String(service.getObject("bucket", "key").getData(), StandardCharsets.UTF_8));++ Object result = readSnapshot(service, explicitNull, ignored -> {+ if (reads.getAndIncrement() == 0) {+ assertThrows(IllegalStateException.class, () -> service.putObject(+ "bucket", "key", bytes("replacement"), "text/plain", Map.of()));+ return observed.get();+ }+ return new String(service.getObject("bucket", "key").getData(), StandardCharsets.UTF_8);+ });++ Method body = result.getClass().getDeclaredMethod("body");+ body.setAccessible(true);+ assertEquals("replacement", observed.get());+ assertEquals("original", body.invoke(result));+ assertEquals(2, reads.get());+ }++ @ParameterizedTest+ @ValueSource(booleans = {true, false})+ void failedNonEnabledPublicationRetainsArchivedNullHistory(boolean memory) throws Exception {+ for (String current : List.of("numbered", "promoted", "marker")) {+ for (boolean rollback : List.of(true, false)) {+ for (boolean afterPut : List.of(false, true)) {+ PublicationFailureStore objects = new PublicationFailureStore();+ S3Service service = service(memory, objects);+ S3Object original = service.putObject("bucket", "key", bytes("original"),+ "text/plain", Map.of("original", "metadata"));+ service.putObjectAnnotation("bucket", "key", "note", null, bytes("annotation"), null, null);+ service.putObjectTagging("bucket", "key", Map.of("original", "tag"));+ String snapshot = service.snapshotBucketConfiguration("bucket");+ service.putBucketVersioning("bucket", "Enabled");+ String numbered = service.putObject("bucket", "key", bytes("numbered"), "text/plain", Map.of())+ .getVersionId();+ if ("promoted".equals(current)) {+ service.deleteObject("bucket", "key", numbered);+ } else if ("marker".equals(current)) {+ service.deleteObject("bucket", "key");+ }+ if (rollback) {+ service.restoreBucketConfiguration("bucket", snapshot);+ } else {+ service.putBucketVersioning("bucket", "Suspended");+ }+ objects.remaining = 1;+ objects.afterPut = afterPut;++ assertThrows(IllegalStateException.class, () -> service.putObject(+ "bucket", "key", bytes("replacement"), "text/plain", Map.of()));++ S3Object retained = service.getObject("bucket", "key", "null");+ assertArrayEquals(bytes("original"), retained.getData());+ assertEquals(original.getETag(), retained.getETag());+ assertEquals(Map.of("original", "metadata"), retained.getMetadata());+ assertEquals(Map.of("original", "tag"), service.getObjectTagging("bucket", "key", "null"));+ assertArrayEquals(bytes("annotation"), service.readObjectAnnotationPayload(+ service.getObjectAnnotation("bucket", "key", "note", "null")));+ if ("marker".equals(current)) {+ assertEquals("NoSuchKey", assertThrows(AwsException.class,+ () -> service.getObject("bucket", "key")).getErrorCode());+ if (memory) {+ assertFalse(memoryBodies(service).values().stream()+ .anyMatch(body -> body instanceof byte[] payload+ && Arrays.equals(payload, bytes("replacement"))),+ "failed marker replacement must not retain canonical bytes");+ } else {+ assertFalse(Files.exists(tempDir.resolve("s3/.accounts/000000000000/bucket/key.s3data")),+ "failed marker replacement must remove the canonical body");+ }+ } else {+ String previous = "promoted".equals(current) ? "original" : "numbered";+ assertArrayEquals(bytes(previous), service.getObject("bucket", "key").getData());+ }++ service.putObject("bucket", "key", bytes("retry"), "text/plain", Map.of());+ assertArrayEquals(bytes("retry"), service.getObject("bucket", "key", "null").getData());+ assertFalse(objects.get("bucket/\0null-version/key").isPresent());+ assertEquals("NoSuchAnnotation", assertThrows(AwsException.class,+ () -> service.getObjectAnnotation("bucket", "key", "note", "null")).getErrorCode());+ }+ }+ }+ }++ @ParameterizedTest+ @ValueSource(booleans = {true, false})+ void failedNullReplacementRestoresBodyMetadataAndLockAtEachPublication(boolean memory) throws Exception {+ for (boolean marker : List.of(false, true)) {+ for (boolean afterPut : List.of(false, true)) {+ for (int failAt = 1; failAt <= 3; failAt++) {+ PublicationFailureStore objects = new PublicationFailureStore();+ S3Service service = service(memory, objects);+ service.putObject("bucket", "key", bytes("original"), "text/plain", Map.of("original", "metadata"));+ service.putObjectAnnotation("bucket", "key", "note", null, bytes("annotation"), null, null);+ service.setBucketObjectLockEnabled("bucket");+ service.putObjectLegalHold("bucket", "key", "null", "ON");+ service.putObjectTagging("bucket", "key", Map.of("original", "tag"));+ service.putBucketVersioning("bucket", "Enabled");+ objects.remaining = failAt;+ objects.afterPut = afterPut;++ assertThrows(IllegalStateException.class, () -> {+ if (marker) {+ service.deleteObject("bucket", "key");+ } else {+ service.putObject("bucket", "key", bytes("replacement"), "text/plain", Map.of());+ }+ });++ S3Object original = service.getObject("bucket", "key");+ assertArrayEquals(bytes("original"), original.getData());+ assertEquals(Map.of("original", "metadata"), original.getMetadata());+ assertEquals("ON", original.getLegalHoldStatus());+ assertArrayEquals(bytes("annotation"), service.readObjectAnnotationPayload(+ service.getObjectAnnotation("bucket", "key", "note", "null")));+ assertEquals(Map.of("original", "tag"), service.getObjectTagging("bucket", "key", "null"));+ assertEquals(1, service.listObjectVersions("bucket", null, 1000, null).versions().size());+ assertFalse(objects.get("bucket/\0null-version/key").isPresent());+ if (!memory) {+ try (Stream<Path> files = Files.walk(tempDir.resolve("s3"))) {+ assertEquals(1, files.filter(Files::isRegularFile)+ .filter(file -> file.toString().endsWith(".s3data")).count());+ }+ }+ if (memory) {+ assertEquals(1, memoryBodies(service).size());+ }+ service.putObjectLegalHold("bucket", "key", "null", "OFF");+ service.deleteObject("bucket", "key", "null");+ if (!memory) {+ try (Stream<Path> files = Files.walk(tempDir.resolve("s3"))) {+ assertEquals(0, files.filter(Files::isRegularFile).count());+ }+ } else {+ assertEquals(0, memoryBodies(service).size());+ }+ }+ }+ }+ }++ @Test+ void failedStreamBodyWriteRemovesUnpublishedNullLink() throws IOException {+ MissingStagedBodyStore objects = new MissingStagedBodyStore(tempDir.resolve("s3"));+ S3Service service = service(false, objects);+ service.putObject("bucket", "key", bytes("original"), "text/plain", Map.of());+ service.putBucketVersioning("bucket", "Enabled");+ objects.armed = true;++ assertThrows(UncheckedIOException.class, () -> service.putObjectFromStream("bucket", "key",+ new ByteArrayInputStream(bytes("replacement")), "text/plain", Map.of()));++ assertArrayEquals(bytes("original"), service.getObject("bucket", "key").getData());+ service.deleteObject("bucket", "key", "null");+ try (Stream<Path> files = Files.walk(tempDir.resolve("s3"))) {+ assertEquals(0, files.filter(Files::isRegularFile).count());+ }+ service.putObject("bucket", "key", bytes("retry"), "text/plain", Map.of());+ assertArrayEquals(bytes("retry"), service.getObject("bucket", "key").getData());+ }++ @Test+ void deletingNullHistoryUnderAMarkerRemovesAllBodyFiles() throws IOException {+ S3Service service = service(false, new InMemoryStorage<>());+ service.putObject("bucket", "key", bytes("original"), "text/plain", Map.of());+ service.putBucketVersioning("bucket", "Enabled");+ String marker = service.deleteObject("bucket", "key").getVersionId();+ Path canonical = tempDir.resolve("s3/.accounts/000000000000/bucket/key.s3data");+ assertFalse(Files.exists(canonical));+ assertArrayEquals(bytes("original"), service.getObject("bucket", "key", "null").getData());++ service.deleteObject("bucket", "key", "null");++ assertTrue(service.listObjectVersions("bucket", "key", 1000, null).versions().stream()+ .anyMatch(version -> marker.equals(version.getVersionId()) && version.isDeleteMarker()));+ try (Stream<Path> files = Files.walk(tempDir.resolve("s3"))) {+ assertEquals(0, files.filter(Files::isRegularFile).count());+ }+ }++ @ParameterizedTest+ @ValueSource(strings = {"get", "open", "copy"})+ void readsRetryWhenADeleteMarkerUnlinksTheCanonicalBody(String operation) {+ DeleteDuringReadStore objects = new DeleteDuringReadStore();+ S3Service service = service(false, objects);+ service.putBucketVersioning("bucket", "Enabled");+ service.putObject("bucket", "key", bytes("numbered"), "text/plain", Map.of());+ objects.service = service;+ objects.remaining = "copy".equals(operation) ? 2 : 1;++ AwsException missing = assertThrows(AwsException.class, () -> {+ switch (operation) {+ case "get" -> service.getObject("bucket", "key");+ case "open" -> {+ try (S3Service.ObjectRead ignored = service.openObject("bucket", "key", null)) {+ fail("the concurrent marker must make the key absent");+ }+ }+ case "copy" -> service.copyObject("bucket", "key", "bucket", "destination");+ default -> throw new IllegalArgumentException(operation);+ }+ });+ assertEquals("NoSuchKey", missing.getErrorCode());+ assertEquals(0, objects.remaining);+ }++ @Test+ void unchangedMissingCanonicalBodyRemainsAStorageError() throws IOException {+ S3Service service = service(false, new InMemoryStorage<>());+ service.putBucketVersioning("bucket", "Enabled");+ service.putObject("bucket", "key", bytes("numbered"), "text/plain", Map.of());+ Files.delete(tempDir.resolve("s3/.accounts/000000000000/bucket/key.s3data"));+ assertThrows(UncheckedIOException.class, () -> service.getObject("bucket", "key"));+ assertThrows(UncheckedIOException.class, () -> service.openObject("bucket", "key", null));+ assertThrows(UncheckedIOException.class, () -> service.copyObject("bucket", "key", "bucket", "destination"));+ }++ @Test+ void enabledMarkersNeverOverrideGovernanceButSuspendedArchivedBypassFailsClosed() {+ S3Service service = service(true, new InMemoryStorage<>());+ service.putObject("bucket", "key", bytes("original"), "text/plain", Map.of());+ service.setBucketObjectLockEnabled("bucket");+ service.putObjectRetention("bucket", "key", "null", "GOVERNANCE", Instant.now().plusSeconds(3600), false);+ service.putBucketVersioning("bucket", "Enabled");+ assertFalse(service.deleteOverridesGovernanceRetention("bucket", "key", null));+ service.putObject("bucket", "key", bytes("numbered"), "text/plain", Map.of());+ service.putBucketVersioning("bucket", "Suspended");+ boolean overrides = service.deleteOverridesGovernanceRetention("bucket", "key", null);+ assertFalse(overrides);+ for (boolean s3Enforced : List.of(false, true)) {+ boolean bypass = S3Controller.effectiveBypass(true, overrides, s3Enforced, false);+ assertFalse(bypass);+ assertDenied(() -> service.deleteObject("bucket", "key", null, bypass));+ }+ }++ private static Map<?, ?> memoryBodies(S3Service service) throws Exception {+ Field bodies = S3Service.class.getDeclaredField("memoryDataStore");+ bodies.setAccessible(true);+ return (Map<?, ?>) bodies.get(service);+ }++ private static final class DeleteDuringReadStore extends InMemoryStorage<String, S3Object> {+ private S3Service service;+ private int remaining;++ @Override+ public Optional<S3Object> get(String key) {+ Optional<S3Object> object = super.get(key);+ if ("bucket/key".equals(key) && remaining > 0 && --remaining == 0) {+ service.deleteObject("bucket", "key");+ }+ return object;+ }+ }++ private S3Service service(boolean memory, InMemoryStorage<String, S3Object> objects) {+ S3Service service = new S3Service(new InMemoryStorage<>(), objects, tempDir.resolve("s3"), memory);+ service.createBucket("bucket", "us-east-1");+ return service;+ }++ private static Object readSnapshot(S3Service service, boolean explicitNull, Function<Object, String> reader)+ throws Exception {+ Method method = explicitNull+ ? S3Service.class.getDeclaredMethod("readNullVersionSnapshot", String.class, String.class,+ Function.class, Consumer.class)+ : S3Service.class.getDeclaredMethod("readLatestSnapshot", String.class, String.class, String.class,+ Function.class, Consumer.class);+ method.setAccessible(true);+ Consumer<String> discard = ignored -> { };+ return explicitNull ? method.invoke(service, "bucket", "key", reader, discard)+ : method.invoke(service, "bucket", "key", null, reader, discard);+ }++ private static void assertDenied(Runnable request) {+ assertEquals("AccessDenied", assertThrows(AwsException.class, request::run).getErrorCode());+ }++ private static byte[] bytes(String text) {+ return text.getBytes(StandardCharsets.UTF_8);+ }++ private static final class PublicationFailureStore extends InMemoryStorage<String, S3Object> {+ private int remaining;+ private boolean afterPut;+ private Runnable beforeFailure;++ @Override+ public void put(String key, S3Object value) {+ boolean fail = remaining > 0 && --remaining == 0;+ if (fail && !afterPut) {+ throw new IllegalStateException("injected metadata publication failure");+ }+ super.put(key, value);+ if (fail) {+ if (beforeFailure != null) {+ beforeFailure.run();+ }+ throw new IllegalStateException("injected failure after metadata publication");+ }+ }+ }++ private static final class MissingStagedBodyStore extends InMemoryStorage<String, S3Object> {+ private final Path root;+ private boolean armed;++ private MissingStagedBodyStore(Path root) {+ this.root = root;+ }++ @Override+ public Optional<S3Object> get(String key) {+ if (armed && "bucket/key".equals(key)) {+ armed = false;+ try (Stream<Path> staged = Files.walk(root.resolve(".multipart"))) {+ for (Path file : staged.filter(Files::isRegularFile).toList()) {+ Files.delete(file);+ }+ } catch (IOException e) {+ throw new UncheckedIOException(e);+ }+ }+ return super.get(key);+ }+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3ObjectIndexJournalTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3ObjectIndexJournalTest.javaindex cacc96f1cc..9d0274cbae 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3ObjectIndexJournalTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3ObjectIndexJournalTest.java@@ -5,6 +5,7 @@ import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.core.common.RegionResolver;import io.github.hectorvent.floci.core.common.ServiceConfigAccess;import io.github.hectorvent.floci.core.storage.StorageFactory;+import io.github.hectorvent.floci.services.s3.model.S3Object;import org.junit.jupiter.api.Test;import org.junit.jupiter.api.io.TempDir;@@ -13,6 +14,8 @@ import java.nio.charset.StandardCharsets;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.StandardCopyOption;+import java.time.Instant;+import java.util.List;import java.util.Map;import java.util.Optional;import java.util.stream.Stream;@@ -135,6 +138,35 @@ class S3ObjectIndexJournalTest {}}+ @Test+ void aTagUpdateOnAnArchivedNullVersionIsReplayedFromTheJournal() throws IOException {+ Path directory = root.resolve("live");+ Path afterCrash = root.resolve("after-crash");+ StorageFactory first = newFactory(directory);+ try {+ S3Service s3 = newService(first, directory);+ s3.createBucket(VERSIONED_BUCKET, REGION);+ s3.putObject(VERSIONED_BUCKET, key(0), body(0), "text/plain", Map.of());+ s3.putBucketVersioning(VERSIONED_BUCKET, "Enabled");+ s3.putObject(VERSIONED_BUCKET, key(0), body(1), "text/plain", Map.of());+ s3.putObjectTagging(VERSIONED_BUCKET, key(0), "null", Map.of("stage", "archived"));+ copyAsLeftByAKill(directory, afterCrash);+ } finally {+ first.shutdownAll();+ }++ StorageFactory second = newFactory(afterCrash);+ try {+ S3Service reopened = newService(second, afterCrash);+ assertEquals(Map.of("stage", "archived"),+ reopened.getObjectTagging(VERSIONED_BUCKET, key(0), "null"));+ assertArrayEquals(body(1), reopened.getObject(VERSIONED_BUCKET, key(0)).getData());+ assertArrayEquals(body(0), reopened.getObject(VERSIONED_BUCKET, key(0), "null").getData());+ } finally {+ second.shutdownAll();+ }+ }+@Testvoid aLegalHoldSetByVersionIdOnTheCurrentVersionIsReplayedUnderTheLatestKeyToo() throws IOException {Path directory = root.resolve("live");@@ -161,6 +193,54 @@ class S3ObjectIndexJournalTest {}}+ @Test+ void archivedNullPromotionOrderSurvivesJournalReplay() throws IOException {+ Path directory = root.resolve("live");+ Path afterCrash = root.resolve("after-crash");+ StorageFactory first = newFactory(directory);+ try {+ S3Service s3 = newService(first, directory);+ s3.createBucket(VERSIONED_BUCKET, REGION);+ s3.putBucketVersioning(VERSIONED_BUCKET, "Enabled");+ s3.putObject(VERSIONED_BUCKET, key(0),+ "older numbered".getBytes(StandardCharsets.UTF_8), "text/plain", Map.of());+ s3.putBucketVersioning(VERSIONED_BUCKET, "Suspended");+ s3.putObject(VERSIONED_BUCKET, key(0),+ "newer null".getBytes(StandardCharsets.UTF_8), "text/plain", Map.of());+ copyAsLeftByAKill(directory, afterCrash);+ } finally {+ first.shutdownAll();+ }++ StorageFactory second = newFactory(afterCrash);+ try {+ S3Service reopened = newService(second, afterCrash);+ List<S3Object> restored = reopened.listObjectVersions(+ VERSIONED_BUCKET, key(0), 100, null).versions();+ S3Object older = restored.stream()+ .filter(version -> version.getVersionId() != null)+ .findFirst()+ .orElseThrow();+ S3Object newerNull = restored.stream()+ .filter(version -> version.getVersionId() == null)+ .findFirst()+ .orElseThrow();+ assertTrue(older.getEventSequencer() != null && newerNull.getEventSequencer() != null,+ "initial PUT records must persist their write order before archival");+ Instant tied = Instant.parse("2026-10-21T00:00:00Z");+ older.setLastModified(tied);+ newerNull.setLastModified(tied);+ reopened.putBucketVersioning(VERSIONED_BUCKET, "Enabled");+ String currentVersionId = reopened.putObject(VERSIONED_BUCKET, key(0),+ "current".getBytes(StandardCharsets.UTF_8), "text/plain", Map.of()).getVersionId();+ reopened.deleteObject(VERSIONED_BUCKET, key(0), currentVersionId);+ assertArrayEquals("newer null".getBytes(StandardCharsets.UTF_8),+ reopened.getObject(VERSIONED_BUCKET, key(0)).getData());+ } finally {+ second.shutdownAll();+ }+ }+/*** The storage directory as a process killed now would leave it, with no shutdown compaction.* Replaying a copy keeps the restarted factory from sharing the journal with the live writer.diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3ObjectLockVersionedWriteTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3ObjectLockVersionedWriteTest.javaindex 7cc57847c5..82da9fe55c 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3ObjectLockVersionedWriteTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3ObjectLockVersionedWriteTest.java@@ -125,17 +125,27 @@ class S3ObjectLockVersionedWriteTest {}@Test- void writesOverLockedPreVersioningObjectAreStillDenied() {- s3Service.createBucket("pre-versioning-bucket", "us-east-1");- s3Service.setBucketObjectLockEnabled("pre-versioning-bucket");- s3Service.putObject("pre-versioning-bucket", "held.txt", BODY, "text/plain", null,- new PutObjectOptions().withLegalHoldStatus("ON"));- s3Service.putBucketVersioning("pre-versioning-bucket", "Enabled");-- assertAccessDenied(() -> s3Service.putObject("pre-versioning-bucket", "held.txt", BODY, "text/plain", null));- assertAccessDenied(() -> s3Service.copyObject(BUCKET, "source.txt", "pre-versioning-bucket", "held.txt"));- assertAccessDenied(() -> s3Service.deleteObject("pre-versioning-bucket", "held.txt", null, false));- assertEquals("ON", s3Service.getObject("pre-versioning-bucket", "held.txt").getLegalHoldStatus());+ void writesOverLockedPreVersioningObjectsPreserveTheNullVersion() {+ String bucket = "pre-versioning-bucket";+ s3Service.createBucket(bucket, "us-east-1");+ s3Service.setBucketObjectLockEnabled(bucket);+ for (String key : List.of("put", "copy", "marker", "multipart")) {+ s3Service.putObject(bucket, key, BODY, "text/plain", null,+ new PutObjectOptions().withLegalHoldStatus("ON"));+ }+ s3Service.putBucketVersioning(bucket, "Enabled");++ assertNotNull(s3Service.putObject(bucket, "put", BODY, "text/plain", null).getVersionId());+ assertNotNull(s3Service.copyObject(BUCKET, "source.txt", bucket, "copy").getVersionId());+ assertTrue(s3Service.deleteObject(bucket, "marker", null, false).isDeleteMarker());+ MultipartUpload upload = s3Service.initiateMultipartUpload(bucket, "multipart", "text/plain");+ s3Service.uploadPart(bucket, "multipart", upload.getUploadId(), 1, BODY);+ assertNotNull(s3Service.completeMultipartUpload(bucket, "multipart", upload.getUploadId(),+ List.of(1), null, null).getVersionId());+ for (String key : List.of("put", "copy", "marker", "multipart")) {+ assertEquals("ON", s3Service.getObject(bucket, key, "null").getLegalHoldStatus());+ assertAccessDenied(() -> s3Service.deleteObject(bucket, key, "null", true));+ }}private S3Object putLegalHold(String key) {diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3ServiceAnnotationsTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3ServiceAnnotationsTest.javaindex fb267b23dd..39731cda2e 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3ServiceAnnotationsTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3ServiceAnnotationsTest.java@@ -468,15 +468,26 @@ class S3ServiceAnnotationsTest {}@Test- void deleteMarkerOnPreVersioningObjectRemovesItsAnnotations() throws Exception {+ void deleteMarkerOnPreVersioningObjectPreservesItsAnnotationsUntilPermanentDeletion() throws Exception {putAnnotation("pre-versioning", "payload");s3Service.putBucketVersioning(BUCKET, "Enabled");- s3Service.deleteObject(BUCKET, "docs/readme.txt");+ S3Object marker = s3Service.deleteObject(BUCKET, "docs/readme.txt");- // The annotations are gone, including the payload files on disk.+ ObjectAnnotation annotation = s3Service.getObjectAnnotation(+ BUCKET, "docs/readme.txt", "pre-versioning", "null");+ assertEquals("payload", new String(+ s3Service.readObjectAnnotationPayload(annotation), StandardCharsets.UTF_8));try (Stream<Path> stream = Files.walk(tempDir.resolve("s3").resolve(".accounts").resolve("000000000000").resolve(".annotations"))) {- List<java.nio.file.Path> files = stream.filter(Files::isRegularFile).toList();+ List<Path> files = stream.filter(Files::isRegularFile).toList();+ assertEquals(1, files.size());+ }++ s3Service.deleteObject(BUCKET, "docs/readme.txt", marker.getVersionId());+ s3Service.deleteObject(BUCKET, "docs/readme.txt", "null");+ try (Stream<Path> stream = Files.walk(tempDir.resolve("s3").resolve(".accounts")+ .resolve("000000000000").resolve(".annotations"))) {+ List<Path> files = stream.filter(Files::isRegularFile).toList();assertTrue(files.isEmpty(), "annotation payload files should be removed, found: " + files);}}@@ -589,4 +600,4 @@ class S3ServiceAnnotationsTest {AwsException exception = assertThrows(AwsException.class, action::run);assertEquals(errorCode, exception.getErrorCode());}-}\ No newline at end of file+}diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3VersioningIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3VersioningIntegrationTest.javaindex 9fd4a15e05..c7fd8c4953 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3VersioningIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3VersioningIntegrationTest.java@@ -8,6 +8,7 @@ import org.junit.jupiter.api.Test;import org.junit.jupiter.api.TestMethodOrder;import java.util.List;+import java.util.regex.Pattern;import static io.restassured.RestAssured.given;import static org.hamcrest.Matchers.*;@@ -650,12 +651,15 @@ class S3VersioningIntegrationTest {String legalHold = "<LegalHold><Status>OFF</Status></LegalHold>";String old = "/" + bucket + "/old";+ given().body("replacement").when().put(old).then().statusCode(200);given().queryParam("versionId", "null").body(retention).when().put(old + "?retention").then().statusCode(200);given().queryParam("versionId", "null").when().get(old + "?retention").then().statusCode(200).body(containsString("<Mode>GOVERNANCE</Mode>"));given().queryParam("versionId", "null").body(legalHold).when().put(old + "?legal-hold").then().statusCode(200);given().queryParam("versionId", "null").when().get(old + "?legal-hold").then().statusCode(200).body(containsString("<Status>OFF</Status>"));+ given().queryParam("versionId", "null").when().delete(old)+ .then().statusCode(403).body(containsString("<Code>AccessDenied</Code>"));String added = "/" + bucket + "/new";Matcher<String> noSuchVersion = containsString("<Code>NoSuchVersion</Code>");@@ -672,13 +676,118 @@ class S3VersioningIntegrationTest {.when().delete(old).then().statusCode(204);}+ @Test+ @Order(26)+ void aVersionedOverwritePreservesAndPromotesTheNullVersion() {+ String bucket = "versioned-archived-null-test";+ String key = "/" + bucket + "/key";+ given().when().put("/" + bucket).then().statusCode(200);+ given().body("null body").when().put(key).then().statusCode(200);+ given().queryParam("annotationName", "labels").body("kept")+ .when().put(key + "?annotation").then().statusCode(200);+ given().body("<VersioningConfiguration><Status>Enabled</Status></VersioningConfiguration>")+ .when().put("/" + bucket + "?versioning").then().statusCode(200);+ String currentVersion = given().body("versioned body").when().put(key)+ .then().statusCode(200).extract().header("x-amz-version-id");++ String versions = given().when().get("/" + bucket + "?versions&prefix=key")+ .then().statusCode(200).extract().asString();+ assertEquals(1, occurrences(versions, "<VersionId>null</VersionId>"));+ assertEquals(1, occurrences(versions, "<VersionId>" + currentVersion + "</VersionId>"));+ given().queryParam("versionId", "null").when().get(key)+ .then().statusCode(200).body(equalTo("null body"));+ given().queryParam("versionId", "null").queryParam("annotationName", "labels")+ .when().get(key + "?annotation").then().statusCode(200).body(equalTo("kept"));+ given().header("x-amz-copy-source", "/" + bucket + "/key?versionId=null")+ .when().put("/" + bucket + "/copy").then().statusCode(200);+ given().when().get("/" + bucket + "/copy").then().statusCode(200).body(equalTo("null body"));++ given().when().delete(key + "?versionId=" + currentVersion).then().statusCode(204);+ given().when().get(key).then().statusCode(200).body(equalTo("null body"));+ versions = given().when().get("/" + bucket + "?versions&prefix=key")+ .then().statusCode(200).extract().asString();+ assertEquals(1, occurrences(versions, "<VersionId>null</VersionId>"));+ assertEquals(1, occurrences(versions, "<IsLatest>true</IsLatest>"));++ given().when().delete(key + "?versionId=null").then().statusCode(204);+ given().when().get(key).then().statusCode(404);+ given().queryParam("versionId", "null").queryParam("annotationName", "labels")+ .when().get(key + "?annotation").then().statusCode(404);+ }++ @Test+ @Order(27)+ void aDeleteMarkerPreservesTheCurrentNullVersion() {+ String bucket = "versioned-null-marker-test";+ String key = "/" + bucket + "/key";+ given().when().put("/" + bucket).then().statusCode(200);+ given().body("null body").when().put(key).then().statusCode(200);+ given().queryParam("annotationName", "labels").body("kept")+ .when().put(key + "?annotation").then().statusCode(200);+ given().body("<VersioningConfiguration><Status>Enabled</Status></VersioningConfiguration>")+ .when().put("/" + bucket + "?versioning").then().statusCode(200);++ String markerId = given().when().delete(key)+ .then().statusCode(204).extract().header("x-amz-version-id");+ given().when().get(key).then().statusCode(404);+ given().queryParam("versionId", "null").when().get(key)+ .then().statusCode(200).body(equalTo("null body"));+ given().queryParam("versionId", "null").queryParam("annotationName", "labels")+ .when().get(key + "?annotation").then().statusCode(200).body(equalTo("kept"));++ given().when().delete(key + "?versionId=" + markerId).then().statusCode(204);+ given().when().get(key).then().statusCode(200).body(equalTo("null body"));+ String versions = given().when().get("/" + bucket + "?versions&prefix=key")+ .then().statusCode(200).extract().asString();+ assertEquals(1, occurrences(versions, "<VersionId>null</VersionId>"));+ }+private static String tagging(String value) {return "<Tagging><TagSet><Tag><Key>version</Key><Value>" + value + "</Value></Tag></TagSet></Tagging>";}+ private static int occurrences(String value, String needle) {+ return value.split(Pattern.quote(needle), -1).length - 1;+ }+private static Matcher<String> deleteMarkerRefused(String method) {return allOf(containsString("<Code>MethodNotAllowed</Code>"),containsString("<Method>" + method + "</Method>"),containsString("<ResourceType>DeleteMarker</ResourceType>"));}+ @Test+ @Order(28)+ void versionedWritesAndMarkersPreserveComplianceLockedNullVersions() {+ String bucket = "versioned-locked-null-preservation";+ given().when().put("/" + bucket).then().statusCode(200);+ for (String key : List.of("put", "copy", "marker")) {+ given().body("original").when().put("/" + bucket + "/" + key).then().statusCode(200);+ }+ given().body("<VersioningConfiguration><Status>Enabled</Status></VersioningConfiguration>")+ .when().put("/" + bucket + "?versioning").then().statusCode(200);+ given().body("<ObjectLockConfiguration><ObjectLockEnabled>Enabled</ObjectLockEnabled></ObjectLockConfiguration>")+ .when().put("/" + bucket + "?object-lock").then().statusCode(200);+ for (String key : List.of("put", "copy", "marker")) {+ given().queryParam("versionId", "null")+ .body("<Retention><Mode>COMPLIANCE</Mode><RetainUntilDate>2031-01-01T00:00:00Z</RetainUntilDate></Retention>")+ .when().put("/" + bucket + "/" + key + "?retention").then().statusCode(200);+ }++ given().body("replacement").when().put("/" + bucket + "/put").then().statusCode(200);+ given().header("x-amz-copy-source", "/" + bucket + "/put")+ .when().put("/" + bucket + "/copy").then().statusCode(200);+ given().when().delete("/" + bucket + "/marker").then().statusCode(204)+ .header("x-amz-delete-marker", "true");++ for (String key : List.of("put", "copy", "marker")) {+ String path = "/" + bucket + "/" + key;+ given().queryParam("versionId", "null").when().get(path)+ .then().statusCode(200).body(equalTo("original"));+ given().queryParam("versionId", "null").when().get(path + "?retention")+ .then().statusCode(200).body(containsString("<Mode>COMPLIANCE</Mode>"));+ given().queryParam("versionId", "null").header("x-amz-bypass-governance-retention", "true")+ .when().delete(path).then().statusCode(403).body(containsString("<Code>AccessDenied</Code>"));+ }+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3VersioningServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3VersioningServiceTest.javaindex 39e655aac7..4ba3021458 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3VersioningServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3VersioningServiceTest.java@@ -13,9 +13,11 @@ import java.io.InputStream;import java.nio.charset.StandardCharsets;import java.nio.file.Files;import java.nio.file.Path;+import java.time.Instant;import java.util.Arrays;import java.util.List;import java.util.Map;+import java.util.Optional;import static org.junit.jupiter.api.Assertions.*;@@ -45,6 +47,216 @@ class S3VersioningServiceTest {assertEquals("Suspended", s3Service.getBucketVersioning("versioned-bucket"));}+ @Test+ void suspendedPutReplacesAnArchivedNullVersionWithoutLeavingADuplicate() {+ s3Service.putObject("versioned-bucket", "key",+ "original null".getBytes(StandardCharsets.UTF_8), "text/plain", null);+ s3Service.putBucketVersioning("versioned-bucket", "Enabled");+ s3Service.putObject("versioned-bucket", "key",+ "versioned".getBytes(StandardCharsets.UTF_8), "text/plain", null);+ s3Service.putBucketVersioning("versioned-bucket", "Suspended");++ S3Object replacement = s3Service.putObject("versioned-bucket", "key",+ "replacement null".getBytes(StandardCharsets.UTF_8), "text/plain", null);++ List<S3Object> versions = s3Service.listObjectVersions("versioned-bucket", "key", 100, null).versions();+ assertEquals(1, versions.stream().filter(version -> version.getVersionId() == null).count());+ S3Object listedNull = versions.stream().filter(version -> version.getVersionId() == null).findFirst().orElseThrow();+ assertTrue(listedNull.isLatest());+ assertEquals(replacement.getETag(), listedNull.getETag());+ assertEquals("replacement null", new String(+ s3Service.getObject("versioned-bucket", "key", "null").getData(), StandardCharsets.UTF_8));+ }++ @Test+ void suspendedDeleteRemovesAnArchivedNullVersion() {+ s3Service.putObject("versioned-bucket", "key",+ "original null".getBytes(StandardCharsets.UTF_8), "text/plain", null);+ s3Service.putBucketVersioning("versioned-bucket", "Enabled");+ s3Service.putObject("versioned-bucket", "key",+ "versioned".getBytes(StandardCharsets.UTF_8), "text/plain", null);+ s3Service.putBucketVersioning("versioned-bucket", "Suspended");++ s3Service.deleteObject("versioned-bucket", "key");++ AwsException missing = assertThrows(AwsException.class,+ () -> s3Service.getObject("versioned-bucket", "key", "null"));+ assertEquals("NoSuchVersion", missing.getErrorCode());+ }++ @Test+ void deletingAnAbsentNullVersionDoesNotDeleteAKeyThatLooksLikeItsHistoryEntry() {+ s3Service.putObject("versioned-bucket", "key#v#null",+ "tricky".getBytes(StandardCharsets.UTF_8), "text/plain", null);++ assertNull(s3Service.deleteObject("versioned-bucket", "key", "null"));+ assertEquals("tricky", new String(+ s3Service.getObject("versioned-bucket", "key#v#null").getData(), StandardCharsets.UTF_8));+ }++ @Test+ void archivingANullVersionDoesNotOverwriteAKeyThatLooksLikeItsLegacyHistoryEntry() {+ s3Service.putObject("versioned-bucket", "key",+ "null version".getBytes(StandardCharsets.UTF_8), "text/plain", null);+ s3Service.putObject("versioned-bucket", "key#v#null",+ "tricky".getBytes(StandardCharsets.UTF_8), "text/plain", null);+ s3Service.putBucketVersioning("versioned-bucket", "Enabled");++ S3Object replacement = s3Service.putObject("versioned-bucket", "key",+ "replacement".getBytes(StandardCharsets.UTF_8), "text/plain", null);++ assertEquals("tricky", new String(+ s3Service.getObject("versioned-bucket", "key#v#null").getData(), StandardCharsets.UTF_8));+ assertEquals("null version", new String(+ s3Service.getObject("versioned-bucket", "key", "null").getData(), StandardCharsets.UTF_8));++ s3Service.deleteObject("versioned-bucket", "key", replacement.getVersionId());++ assertEquals("null version", new String(+ s3Service.getObject("versioned-bucket", "key").getData(), StandardCharsets.UTF_8));+ assertEquals("null version", new String(+ s3Service.getObject("versioned-bucket", "key", "null").getData(), StandardCharsets.UTF_8));+ assertEquals("tricky", new String(+ s3Service.getObject("versioned-bucket", "key#v#null").getData(), StandardCharsets.UTF_8));+ }++ @Test+ void archivedNullVersionIsNotListedAsAnOrdinaryObject() {+ s3Service.putObject("versioned-bucket", "key",+ "null version".getBytes(StandardCharsets.UTF_8), "text/plain", null);+ s3Service.putBucketVersioning("versioned-bucket", "Enabled");+ s3Service.putObject("versioned-bucket", "key",+ "replacement".getBytes(StandardCharsets.UTF_8), "text/plain", null);++ List<S3Object> objects = s3Service.listObjects("versioned-bucket", null, null, 100);++ assertEquals(1, objects.size());+ assertEquals("key", objects.get(0).getKey());+ assertNotNull(objects.get(0).getVersionId());+ }++ @Test+ void archivedNullVersionWithLegacyMarkerInKeyIsListedOnce() {+ String key = "key#v#part";+ s3Service.putObject("versioned-bucket", key,+ "null version".getBytes(StandardCharsets.UTF_8), "text/plain", null);+ s3Service.putBucketVersioning("versioned-bucket", "Enabled");+ S3Object current = s3Service.putObject("versioned-bucket", key,+ "replacement".getBytes(StandardCharsets.UTF_8), "text/plain", null);++ List<S3Object> versions = s3Service.listObjectVersions(+ "versioned-bucket", key, 100, null).versions();++ assertEquals(1, versions.stream().filter(version -> version.getVersionId() == null).count());++ s3Service.deleteObject("versioned-bucket", key, current.getVersionId());++ versions = s3Service.listObjectVersions("versioned-bucket", key, 100, null).versions();+ assertEquals(1, versions.size());+ assertNull(versions.getFirst().getVersionId());+ assertTrue(versions.getFirst().isLatest());+ }++ @Test+ void staleMetadataUpdateDoesNotOverwriteAReplacementArchivedNullVersion() {+ MetadataReplacementStore objectStore = new MetadataReplacementStore();+ S3Service service = new S3Service(new InMemoryStorage<>(), objectStore, tempDir, true);+ service.createBucket("versioned-bucket", "us-east-1");+ service.putObject("versioned-bucket", "key",+ "old".getBytes(StandardCharsets.UTF_8), "text/plain", null);+ service.putBucketVersioning("versioned-bucket", "Enabled");+ service.putObject("versioned-bucket", "key",+ "numbered".getBytes(StandardCharsets.UTF_8), "text/plain", null);+ objectStore.arm(service);++ service.putObjectTagging("versioned-bucket", "key", "null", Map.of("stale", "tag"));++ S3Object nullVersion = service.getObject("versioned-bucket", "key", "null");+ assertEquals("replacement", new String(nullVersion.getData(), StandardCharsets.UTF_8));+ assertEquals("replacement".length(), nullVersion.getSize());+ assertTrue(service.getObjectTagging("versioned-bucket", "key", "null").isEmpty());+ }++ @Test+ void currentNullSnapshotSurvivesArchiveAndPromotionDuringTheRead() {+ SnapshotAbaStore objectStore = new SnapshotAbaStore();+ S3Service service = new S3Service(new InMemoryStorage<>(), objectStore, tempDir, true);+ service.createBucket("versioned-bucket", "us-east-1");+ service.putObject("versioned-bucket", "key",+ "original".getBytes(StandardCharsets.UTF_8), "text/plain", null);+ service.putBucketVersioning("versioned-bucket", "Enabled");+ objectStore.arm(service);++ S3Object nullVersion = service.getObject("versioned-bucket", "key", "null");++ assertEquals("original", new String(nullVersion.getData(), StandardCharsets.UTF_8));+ assertEquals("original", new String(+ service.getObject("versioned-bucket", "key").getData(), StandardCharsets.UTF_8));+ }++ @Test+ void latestNullSnapshotSurvivesArchiveAndPromotionDuringTheRead() {+ SnapshotAbaStore objectStore = new SnapshotAbaStore();+ S3Service service = new S3Service(new InMemoryStorage<>(), objectStore, tempDir, true);+ service.createBucket("versioned-bucket", "us-east-1");+ service.putObject("versioned-bucket", "key",+ "original".getBytes(StandardCharsets.UTF_8), "text/plain", null);+ service.putBucketVersioning("versioned-bucket", "Enabled");+ objectStore.arm(service);++ S3Object latest = service.getObject("versioned-bucket", "key");++ assertEquals("original", new String(latest.getData(), StandardCharsets.UTF_8));+ assertEquals("original", new String(+ service.getObject("versioned-bucket", "key").getData(), StandardCharsets.UTF_8));+ }++ @Test+ void delayedMetadataUpdateDemotesTheNullHistoryEntryItTargets() {+ MetadataArchiveStore objectStore = new MetadataArchiveStore();+ S3Service service = new S3Service(new InMemoryStorage<>(), objectStore, tempDir, true);+ service.createBucket("versioned-bucket", "us-east-1");+ service.putObject("versioned-bucket", "key",+ "original".getBytes(StandardCharsets.UTF_8), "text/plain", null);+ service.putBucketVersioning("versioned-bucket", "Enabled");+ objectStore.arm(service);++ service.putObjectTagging("versioned-bucket", "key", "null", Map.of("kept", "tag"));++ List<S3Object> versions = service.listObjectVersions("versioned-bucket", "key", 100, null).versions();+ assertEquals(1, versions.stream().filter(S3Object::isLatest).count());+ assertFalse(versions.stream()+ .filter(version -> version.getVersionId() == null)+ .findFirst()+ .orElseThrow()+ .isLatest());+ assertEquals(Map.of("kept", "tag"), service.getObjectTagging("versioned-bucket", "key", "null"));+ }++ @Test+ void archivedNullKeepsItsSequencerForSameMillisecondPromotionOrder() {+ InMemoryStorage<String, S3Object> objectStore = new InMemoryStorage<>();+ S3Service service = new S3Service(new InMemoryStorage<>(), objectStore, tempDir, true);+ service.createBucket("versioned-bucket", "us-east-1");+ service.putBucketVersioning("versioned-bucket", "Enabled");+ S3Object older = service.putObject("versioned-bucket", "key",+ "older numbered".getBytes(StandardCharsets.UTF_8), "text/plain", null);+ service.putBucketVersioning("versioned-bucket", "Suspended");+ service.putObject("versioned-bucket", "key",+ "newer null".getBytes(StandardCharsets.UTF_8), "text/plain", null);+ Instant tied = Instant.parse("2026-10-21T00:00:00Z");+ objectStore.get("versioned-bucket/key#v#" + older.getVersionId()).orElseThrow().setLastModified(tied);+ objectStore.get("versioned-bucket/key").orElseThrow().setLastModified(tied);+ service.putBucketVersioning("versioned-bucket", "Enabled");+ S3Object current = service.putObject("versioned-bucket", "key",+ "current".getBytes(StandardCharsets.UTF_8), "text/plain", null);++ service.deleteObject("versioned-bucket", "key", current.getVersionId());++ assertEquals("newer null", new String(+ service.getObject("versioned-bucket", "key").getData(), StandardCharsets.UTF_8));+ }+@Testvoid versioningNotEnabledByDefault() {assertNull(s3Service.getBucketVersioning("versioned-bucket"));@@ -526,4 +738,80 @@ class S3VersioningServiceTest {assertDoesNotThrow(() -> s3Service.deleteBucket("versioned-bucket"));}+ private static final class MetadataReplacementStore extends InMemoryStorage<String, S3Object> {+ private S3Service service;+ private boolean armed;++ private void arm(S3Service service) {+ this.service = service;+ armed = true;+ }++ @Override+ public Optional<S3Object> get(String key) {+ Optional<S3Object> result = super.get(key);+ if (armed && key.indexOf('\0') >= 0) {+ armed = false;+ service.putBucketVersioning("versioned-bucket", "Suspended");+ service.putObject("versioned-bucket", "key",+ "replacement".getBytes(StandardCharsets.UTF_8), "text/plain", null);+ service.putBucketVersioning("versioned-bucket", "Enabled");+ service.putObject("versioned-bucket", "key",+ "latest".getBytes(StandardCharsets.UTF_8), "text/plain", null);+ }+ return result;+ }+ }++ private static final class MetadataArchiveStore extends InMemoryStorage<String, S3Object> {+ private S3Service service;+ private boolean armed;++ private void arm(S3Service service) {+ this.service = service;+ armed = true;+ }++ @Override+ public Optional<S3Object> get(String key) {+ Optional<S3Object> result = super.get(key);+ if (armed && "versioned-bucket/key".equals(key)) {+ armed = false;+ service.putObject("versioned-bucket", "key",+ "replacement".getBytes(StandardCharsets.UTF_8), "text/plain", null);+ }+ return result;+ }+ }++ private static final class SnapshotAbaStore extends InMemoryStorage<String, S3Object> {+ private S3Service service;+ private int stage = -1;+ private String replacementVersionId;++ private void arm(S3Service service) {+ this.service = service;+ stage = 0;+ }++ @Override+ public Optional<S3Object> get(String key) {+ if (!"versioned-bucket/key".equals(key) || stage < 0 || stage > 1) {+ return super.get(key);+ }+ if (stage == 0) {+ Optional<S3Object> original = super.get(key);+ stage = -1;+ replacementVersionId = service.putObject("versioned-bucket", "key",+ "replacement".getBytes(StandardCharsets.UTF_8), "text/plain", null).getVersionId();+ stage = 1;+ return original;+ }+ stage = -1;+ service.deleteObject("versioned-bucket", "key", replacementVersionId);+ stage = 2;+ return super.get(key);+ }+ }+}