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 <AndreKurait@gmail.com>
This commit is contained in:
Jackson PriceandAndre Kurait authored and GitHub committed 2026-11-15 18:39:09 -04:00
1 parent e1c3242978
commit f3a0595f50
4 files changed
+166 -5

No files matched your search

@@ -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());
}
}
@@ -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());
}
}