From f3a0595f502b6fa6947ce48a964753b4aaadf54e Mon Sep 17 00:00:00 2001 From: Jackson Price <59149601+jprice8@users.noreply.github.com> Date: Sun, 15 Nov 2026 16:39:09 -0600 Subject: [PATCH] fix(cloudfront): report stale issued store versions as failed preconditions (#8514) UpdateKeyValueStore and DeleteKeyValueStore answered every If-Match that did not match the store's control-plane ETag with InvalidIfMatchVersion (400). A stale version Floci issued, one that a comment update has since rotated, now fails with PreconditionFailed (412); a missing or malformed version keeps InvalidIfMatchVersion. A refused request leaves the store and its data plane unchanged, and the CloudFront overview and the key-value-store section of the docs describe the same statuses. Co-authored-by: Andre Kurait --- docs/services/cloudfront.md | 12 ++- .../CloudFrontKeyValueStoreService.java | 9 ++- ...ntKeyValueStoreEtagSdkIntegrationTest.java | 69 ++++++++++++++++ .../CloudFrontKeyValueStoreEtagTest.java | 81 +++++++++++++++++++ 4 files changed, 166 insertions(+), 5 deletions(-) create mode 100644 src/test/java/io/floci/aws/services/cloudfront/CloudFrontKeyValueStoreEtagSdkIntegrationTest.java create mode 100644 src/test/java/io/floci/aws/services/cloudfront/CloudFrontKeyValueStoreEtagTest.java diff --git a/docs/services/cloudfront.md b/docs/services/cloudfront.md index 75ad25f2c4..630088786f 100644 --- a/docs/services/cloudfront.md +++ b/docs/services/cloudfront.md @@ -169,8 +169,10 @@ the path: - `DeleteDistribution` returns `DistributionNotDisabled` (409) if `Enabled` is `true` in the config. - All mutating operations (`PUT`, `DELETE`) require an `If-Match` header containing the current `ETag`. Response headers policies, public keys, and key groups distinguish a missing header - (`InvalidIfMatchVersion`, 400) from a stale `ETag` (`PreconditionFailed`, 412). Other CloudFront - resources currently return `InvalidIfMatchVersion` (400) for either case. + (`InvalidIfMatchVersion`, 400) from a stale `ETag` (`PreconditionFailed`, 412). Key-value-store + control-plane updates and deletes also return `PreconditionFailed` (412) for stale UUID versions + issued by Floci; missing or malformed versions return `InvalidIfMatchVersion` (400). Other + CloudFront resources currently return `InvalidIfMatchVersion` (400) for either case. - All `GET` and `POST` (create) responses include an `ETag` response header. - List operations emit the payload root declared by the CloudFront REST XML model (for example, `ListDistributions` returns ``), with list contents represented by @@ -426,8 +428,10 @@ and described through the CloudFront control plane, and its keys are managed thr - Every key write needs the store's current data plane ETag in `If-Match`, and rotates it. `UpdateKeys` applies its puts and deletes together (at most 50 entries and 3 MB per request), and changes nothing when any entry is invalid, including a put without a `Value`. A stale or missing ETag on the - data plane is a `ValidationException` ("Pre-Condition failed during update of Key-Value-Store"), and on the - control plane an `InvalidIfMatchVersion`. + data plane is a `ValidationException` ("Pre-Condition failed during update of Key-Value-Store"). Control-plane + update and delete return `PreconditionFailed` (412) for stale UUID versions issued by Floci, and + `InvalidIfMatchVersion` (400) for missing or malformed versions. Rejected requests preserve the store. + ETags retain Floci's existing UUID representation; this does not define an AWS version format. - The control plane and the data plane keep independent ETags: a comment update rotates only the control plane ETag, and a key write only the data plane ETag. - An account can hold at most 200 stores (`EntityLimitExceeded`), and a store can be associated with at most 100 diff --git a/src/main/java/io/floci/aws/services/cloudfront/CloudFrontKeyValueStoreService.java b/src/main/java/io/floci/aws/services/cloudfront/CloudFrontKeyValueStoreService.java index dfb243ea25..84d63e168b 100644 --- a/src/main/java/io/floci/aws/services/cloudfront/CloudFrontKeyValueStoreService.java +++ b/src/main/java/io/floci/aws/services/cloudfront/CloudFrontKeyValueStoreService.java @@ -43,6 +43,8 @@ public class CloudFrontKeyValueStoreService implements StoredResourceArns { static final int MAX_STORES = 200; private static final Pattern STORE_NAME = Pattern.compile("[a-zA-Z0-9_-]{1,64}"); + private static final Pattern ISSUED_ETAG = Pattern.compile( + "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"); private static final int MAX_COMMENT_LENGTH = 128; private static final String STATUS_READY = "READY"; @@ -273,10 +275,15 @@ public class CloudFrontKeyValueStoreService implements StoredResourceArns { } private static void requireControlPlaneEtag(KeyValueStore kvs, String ifMatch) { - if (ifMatch == null || !kvs.getEtag().equals(ifMatch)) { + if (kvs.getEtag().equals(ifMatch)) { + return; + } + if (ifMatch == null || !ISSUED_ETAG.matcher(ifMatch).matches()) { throw new AwsException("InvalidIfMatchVersion", "The If-Match version is missing or not valid for the resource.", 400); } + throw new AwsException("PreconditionFailed", + "The precondition in the If-Match header did not match the current resource.", 412); } private static void requireDataPlaneEtag(KeyValueStore kvs, String ifMatch) { diff --git a/src/test/java/io/floci/aws/services/cloudfront/CloudFrontKeyValueStoreEtagSdkIntegrationTest.java b/src/test/java/io/floci/aws/services/cloudfront/CloudFrontKeyValueStoreEtagSdkIntegrationTest.java new file mode 100644 index 0000000000..b57244e840 --- /dev/null +++ b/src/test/java/io/floci/aws/services/cloudfront/CloudFrontKeyValueStoreEtagSdkIntegrationTest.java @@ -0,0 +1,69 @@ +package io.floci.aws.services.cloudfront; + +import io.floci.aws.testing.AwsTestClients; +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.cloudfront.CloudFrontClient; +import software.amazon.awssdk.services.cloudfront.model.CloudFrontException; +import software.amazon.awssdk.services.cloudfront.model.CreateKeyValueStoreResponse; +import software.amazon.awssdk.services.cloudfront.model.DescribeKeyValueStoreResponse; +import software.amazon.awssdk.services.cloudfront.model.UpdateKeyValueStoreResponse; + +import java.util.List; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** Native SDK XML/header handling and stale-version refusal; no data-plane or function execution claim. */ +@QuarkusTest +class CloudFrontKeyValueStoreEtagSdkIntegrationTest { + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void updateAndDeleteDistinguishStaleVersionsFromMalformedHeaders(boolean delete) { + try (CloudFrontClient client = AwsTestClients.create( + CloudFrontClient.builder(), "000000000000", Region.US_EAST_1)) { + String name = "etag-" + UUID.randomUUID().toString().substring(0, 8); + CreateKeyValueStoreResponse created = client.createKeyValueStore(r -> r.name(name).comment("before")); + try { + UpdateKeyValueStoreResponse updated = + client.updateKeyValueStore(r -> r.name(name).ifMatch(created.eTag()).comment("after")); + String current = updated.eTag(); + assertNotEquals(created.eTag(), current); + if (delete) { + assertError("PreconditionFailed", 412, + () -> client.deleteKeyValueStore(r -> r.name(name).ifMatch(created.eTag()))); + } else { + assertError("PreconditionFailed", 412, + () -> client.updateKeyValueStore(r -> r.name(name).ifMatch(created.eTag()).comment("lost"))); + } + DescribeKeyValueStoreResponse unchanged = client.describeKeyValueStore(r -> r.name(name)); + assertEquals("after", unchanged.keyValueStore().comment()); + assertEquals(current, unchanged.eTag()); + assertEquals(created.keyValueStore().arn(), unchanged.keyValueStore().arn()); + for (String malformed : List.of("stale", "")) { + assertError("InvalidIfMatchVersion", 400, + () -> client.updateKeyValueStore(r -> r.name(name).ifMatch(malformed).comment("lost"))); + assertError("InvalidIfMatchVersion", 400, + () -> client.deleteKeyValueStore(r -> r.name(name).ifMatch(malformed))); + } + DescribeKeyValueStoreResponse retained = client.describeKeyValueStore(r -> r.name(name)); + assertEquals("after", retained.keyValueStore().comment()); + assertEquals(current, retained.eTag()); + } finally { + String cleanupVersion = client.describeKeyValueStore(r -> r.name(name)).eTag(); + client.deleteKeyValueStore(r -> r.name(name).ifMatch(cleanupVersion)); + } + assertError("EntityNotFound", 404, () -> client.describeKeyValueStore(r -> r.name(name))); + } + } + + private static void assertError(String code, int status, Runnable action) { + CloudFrontException failure = assertThrows(CloudFrontException.class, action::run); + assertEquals(code, failure.awsErrorDetails().errorCode()); + assertEquals(status, failure.statusCode()); + } +} diff --git a/src/test/java/io/floci/aws/services/cloudfront/CloudFrontKeyValueStoreEtagTest.java b/src/test/java/io/floci/aws/services/cloudfront/CloudFrontKeyValueStoreEtagTest.java new file mode 100644 index 0000000000..5da51291b2 --- /dev/null +++ b/src/test/java/io/floci/aws/services/cloudfront/CloudFrontKeyValueStoreEtagTest.java @@ -0,0 +1,81 @@ +package io.floci.aws.services.cloudfront; + +import io.floci.aws.core.common.AwsException; +import io.floci.aws.core.common.RegionResolver; +import io.floci.aws.core.storage.AccountAwareStorageBackend; +import io.floci.aws.core.storage.StorageFactory; +import io.floci.aws.services.cloudfront.model.KeyValueStore; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class CloudFrontKeyValueStoreEtagTest { + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void staleIssuedVersionsFailPreconditionsWithoutChangingControlOrDataState(boolean delete) { + CloudFrontKeyValueStoreService service = service(); + KeyValueStore first = service.create("versions", "before"); + KeyValueStore data = service.putKey(first.getArn(), first.getDataEtag(), "key", "value"); + KeyValueStore current = service.update(first.getName(), first.getEtag(), "after"); + + if (delete) { + assertError("PreconditionFailed", 412, () -> service.delete("versions", first.getEtag())); + } else { + assertError("PreconditionFailed", 412, () -> service.update("versions", first.getEtag(), "lost")); + } + assertEquals("after", service.describe("versions").getComment()); + assertEquals(current.getEtag(), service.describe("versions").getEtag()); + assertEquals(data.getDataEtag(), service.describeByArn(first.getArn()).getDataEtag()); + assertEquals("value", service.getKey(first.getArn(), "key").value()); + service.delete("versions", current.getEtag()); + assertError("EntityNotFound", 404, () -> service.describe("versions")); + } + + @Test + void missingEmptyAndMalformedVersionsKeepTheInvalidIfMatchError() { + CloudFrontKeyValueStoreService service = service(); + KeyValueStore current = service.create("versions", "unchanged"); + assertError("InvalidIfMatchVersion", 400, () -> service.update("versions", null, "lost")); + assertError("InvalidIfMatchVersion", 400, () -> service.delete("versions", null)); + for (String malformed : List.of("", "stale", "not-a-version", " " + current.getEtag(), current.getEtag() + " ")) { + assertError("InvalidIfMatchVersion", 400, () -> service.update("versions", malformed, "lost")); + assertError("InvalidIfMatchVersion", 400, () -> service.delete("versions", malformed)); + } + assertEquals("unchanged", service.describe("versions").getComment()); + assertEquals(current.getEtag(), service.describe("versions").getEtag()); + } + + @Test + void matchingStoredVersionsRemainValidWithAnOpaqueFormat() { + CloudFrontKeyValueStoreService service = service(); + KeyValueStore existing = service.create("versions", "before"); + existing.setEtag("opaque-stored-version"); + KeyValueStore updated = service.update("versions", existing.getEtag(), "after"); + assertEquals("after", updated.getComment()); + updated.setEtag("another-opaque-version"); + service.delete("versions", updated.getEtag()); + assertError("EntityNotFound", 404, () -> service.describe("versions")); + } + + private static CloudFrontKeyValueStoreService service() { + StorageFactory factory = mock(StorageFactory.class); + when(factory.create(anyString(), anyString(), any())) + .thenAnswer(call -> AccountAwareStorageBackend.inMemory("000000000000")); + return new CloudFrontKeyValueStoreService(factory, new RegionResolver("us-east-1", "000000000000")); + } + + private static void assertError(String code, int status, Runnable action) { + AwsException failure = assertThrows(AwsException.class, action::run); + assertEquals(code, failure.getErrorCode()); + assertEquals(status, failure.getHttpStatus()); + } +}