mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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:
1 parent
e1c3242978
commit
f3a0595f50
4 files changed
+166
-5
No files matched your search
@@ -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 `<DistributionList>`), 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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
+69
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user