fix(cloudfront): preserve policy configurations (#4009)

* fix(cloudfront): preserve policy configurations

* fix(cloudfront): align policy config edge cases
This commit is contained in:
XuJian authored and GitHub committed 2026-09-20 16:33:18 -04:00
1 parent b791e04566
commit eb18b4ab1f
4 files changed
+613 -16

No files matched your search

@@ -1,6 +1,8 @@
package io.github.hectorvent.floci.services.cloudfront;
import io.github.hectorvent.floci.core.common.XmlParser;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.Response;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
@@ -208,4 +210,241 @@ class CloudFrontManagementProtocolTest {
"//*[local-name()='Code']/text()",
equalTo("InvalidArgument")));
}
@Test
void policyCreationRejectsMissingConfigurationRoot() {
given()
.contentType("application/xml")
.body("<UnexpectedConfig><Name>missing-cache-root</Name></UnexpectedConfig>")
.when()
.post(API + "cache-policy")
.then()
.statusCode(400)
.body(hasXPath("//*[local-name()='Code']/text()", equalTo("InvalidArgument")));
given()
.contentType("application/xml")
.body("<UnexpectedConfig><Name>missing-origin-root</Name></UnexpectedConfig>")
.when()
.post(API + "origin-request-policy")
.then()
.statusCode(400)
.body(hasXPath("//*[local-name()='Code']/text()", equalTo("InvalidArgument")));
}
@Test
void cachePolicyReadFillsAwsTtlDefaults() {
String name = "default-ttl-cache-policy-" + UUID.randomUUID();
String id = null;
String etag = null;
try {
Response created = given()
.contentType("application/xml")
.body("""
<CachePolicyConfig xmlns="http://cloudfront.amazonaws.com/doc/2020-05-31/">
<Name>%s</Name>
<MinTTL>10</MinTTL>
<ParametersInCacheKeyAndForwardedToOrigin>
<EnableAcceptEncodingGzip>false</EnableAcceptEncodingGzip>
<EnableAcceptEncodingBrotli>false</EnableAcceptEncodingBrotli>
<HeadersConfig><HeaderBehavior>none</HeaderBehavior></HeadersConfig>
<CookiesConfig><CookieBehavior>none</CookieBehavior></CookiesConfig>
<QueryStringsConfig>
<QueryStringBehavior>none</QueryStringBehavior>
</QueryStringsConfig>
</ParametersInCacheKeyAndForwardedToOrigin>
</CachePolicyConfig>
""".formatted(name))
.when()
.post(API + "cache-policy");
created.then()
.statusCode(201)
.body(hasXPath("//*[local-name()='DefaultTTL']/text()", equalTo("86400")))
.body(hasXPath("//*[local-name()='MaxTTL']/text()", equalTo("31536000")));
id = XmlParser.extractFirst(created.asString(), "Id", null);
etag = created.header("ETag");
given()
.when()
.get(API + "cache-policy/" + id + "/config")
.then()
.statusCode(200)
.body(hasXPath("//*[local-name()='DefaultTTL']/text()", equalTo("86400")))
.body(hasXPath("//*[local-name()='MaxTTL']/text()", equalTo("31536000")));
} finally {
if (id != null && etag != null) {
given().header("If-Match", etag)
.when().delete(API + "cache-policy/" + id)
.then().statusCode(204);
}
}
}
@Test
void cachePolicyPreservesItsCompleteConfigAcrossManagementOperations() {
String name = "complete-cache-policy-" + UUID.randomUUID();
String id = null;
String etag = null;
try {
Response created = given()
.contentType("application/xml")
.body(cachePolicyBody(name, "created", "3600", "session"))
.when()
.post(API + "cache-policy");
created.then()
.statusCode(201)
.body(hasXPath("//*[local-name()='MinTTL']/text()", equalTo("10")))
.body(hasXPath("//*[local-name()='DefaultTTL']/text()", equalTo("3600")))
.body(hasXPath("//*[local-name()='MaxTTL']/text()", equalTo("86400")))
.body(hasXPath("//*[local-name()='EnableAcceptEncodingGzip']/text()", equalTo("true")))
.body(hasXPath("//*[local-name()='EnableAcceptEncodingBrotli']/text()", equalTo("true")))
.body(hasXPath("//*[local-name()='Cookies']/*[local-name()='Quantity']/text()",
equalTo("2")))
.body(hasXPath("//*[local-name()='Cookies']//*[local-name()='Name' and text()='session']"));
id = XmlParser.extractFirst(created.asString(), "Id", null);
etag = created.header("ETag");
given()
.when()
.get(API + "cache-policy/" + id + "/config")
.then()
.statusCode(200)
.header("ETag", equalTo(etag))
.body(hasXPath("//*[local-name()='DefaultTTL']/text()", equalTo("3600")))
.body(hasXPath(
"//*[local-name()='Headers']//*[local-name()='Name' and text()='Accept-Language']"))
.body(hasXPath(
"//*[local-name()='QueryStrings']//*[local-name()='Name' and text()='page']"));
given()
.when()
.get(API + "cache-policy")
.then()
.statusCode(200)
.body(hasXPath("//*[local-name()='CachePolicy'][*[local-name()='Id' and text()='" + id
+ "']]//*[local-name()='DefaultTTL']/text()", equalTo("3600")));
Response updated = given()
.contentType("application/xml")
.header("If-Match", etag)
.body(cachePolicyBody(name, "updated", "7200", "updated-session"))
.when()
.put(API + "cache-policy/" + id);
updated.then()
.statusCode(200)
.header("ETag", not(equalTo(etag)))
.body(hasXPath("//*[local-name()='Comment']/text()", equalTo("updated")))
.body(hasXPath("//*[local-name()='DefaultTTL']/text()", equalTo("7200")))
.body(hasXPath(
"//*[local-name()='Cookies']//*[local-name()='Name' and text()='updated-session']"));
etag = updated.header("ETag");
} finally {
if (id != null && etag != null) {
given().header("If-Match", etag)
.when().delete(API + "cache-policy/" + id)
.then().statusCode(204);
}
}
}
@Test
void originRequestPolicyPreservesItsCompleteConfigAcrossManagementOperations() {
String name = "complete-origin-policy-" + UUID.randomUUID();
String id = null;
String etag = null;
try {
Response created = given()
.contentType("application/xml")
.body(originRequestPolicyBody(name, "created", "Origin"))
.when()
.post(API + "origin-request-policy");
created.then()
.statusCode(201)
.body(hasXPath("//*[local-name()='HeaderBehavior']/text()", equalTo("whitelist")))
.body(hasXPath("//*[local-name()='Headers']//*[local-name()='Name' and text()='Origin']"))
.body(hasXPath("//*[local-name()='Cookies']//*[local-name()='Name' and text()='session']"))
.body(hasXPath(
"//*[local-name()='QueryStrings']//*[local-name()='Name' and text()='page']"));
id = XmlParser.extractFirst(created.asString(), "Id", null);
etag = created.header("ETag");
given()
.when()
.get(API + "origin-request-policy/" + id + "/config")
.then()
.statusCode(200)
.header("ETag", equalTo(etag))
.body(hasXPath("//*[local-name()='HeaderBehavior']/text()", equalTo("whitelist")))
.body(hasXPath("//*[local-name()='Headers']/*[local-name()='Quantity']/text()",
equalTo("1")));
given()
.when()
.get(API + "origin-request-policy")
.then()
.statusCode(200)
.body(hasXPath("//*[local-name()='OriginRequestPolicy'][*[local-name()='Id' and text()='" + id
+ "']]//*[local-name()='QueryStringBehavior']/text()", equalTo("whitelist")));
Response updated = given()
.contentType("application/xml")
.header("If-Match", etag)
.body(originRequestPolicyBody(name, "updated", "CloudFront-Viewer-Country"))
.when()
.put(API + "origin-request-policy/" + id);
updated.then()
.statusCode(200)
.header("ETag", not(equalTo(etag)))
.body(hasXPath("//*[local-name()='Comment']/text()", equalTo("updated")))
.body(hasXPath(
"//*[local-name()='Headers']//*[local-name()='Name'"
+ " and text()='CloudFront-Viewer-Country']"));
etag = updated.header("ETag");
} finally {
if (id != null && etag != null) {
given().header("If-Match", etag)
.when().delete(API + "origin-request-policy/" + id)
.then().statusCode(204);
}
}
}
private static String cachePolicyBody(String name, String comment, String defaultTtl, String cookie) {
return """
<CachePolicyConfig xmlns="http://cloudfront.amazonaws.com/doc/2020-05-31/">
<Name>%s</Name><Comment>%s</Comment>
<MinTTL>10</MinTTL><DefaultTTL>%s</DefaultTTL><MaxTTL>86400</MaxTTL>
<ParametersInCacheKeyAndForwardedToOrigin>
<EnableAcceptEncodingGzip>true</EnableAcceptEncodingGzip>
<EnableAcceptEncodingBrotli>true</EnableAcceptEncodingBrotli>
<HeadersConfig><HeaderBehavior>whitelist</HeaderBehavior>
<Headers><Quantity>1</Quantity><Items><Name>Accept-Language</Name></Items></Headers>
</HeadersConfig>
<CookiesConfig><CookieBehavior>whitelist</CookieBehavior>
<Cookies><Quantity>2</Quantity><Items><Name>%s</Name><Name>locale</Name></Items></Cookies>
</CookiesConfig>
<QueryStringsConfig><QueryStringBehavior>whitelist</QueryStringBehavior>
<QueryStrings><Quantity>1</Quantity><Items><Name>page</Name></Items></QueryStrings>
</QueryStringsConfig>
</ParametersInCacheKeyAndForwardedToOrigin>
</CachePolicyConfig>
""".formatted(name, comment, defaultTtl, cookie);
}
private static String originRequestPolicyBody(String name, String comment, String header) {
return """
<OriginRequestPolicyConfig xmlns="http://cloudfront.amazonaws.com/doc/2020-05-31/">
<Name>%s</Name><Comment>%s</Comment>
<HeadersConfig><HeaderBehavior>whitelist</HeaderBehavior>
<Headers><Quantity>1</Quantity><Items><Name>%s</Name></Items></Headers>
</HeadersConfig>
<CookiesConfig><CookieBehavior>whitelist</CookieBehavior>
<Cookies><Quantity>1</Quantity><Items><Name>session</Name></Items></Cookies>
</CookiesConfig>
<QueryStringsConfig><QueryStringBehavior>whitelist</QueryStringBehavior>
<QueryStrings><Quantity>1</Quantity><Items><Name>page</Name></Items></QueryStrings>
</QueryStringsConfig>
</OriginRequestPolicyConfig>
""".formatted(name, comment, header);
}
}
@@ -0,0 +1,163 @@
package io.github.hectorvent.floci.services.cloudfront;
import io.github.hectorvent.floci.core.common.XmlBuilder;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class CloudFrontPolicyConfigCodecTest {
@Test
void cachePolicyConfigRoundTripsAllTtlsAndCacheKeyMembers() {
String body = """
<CachePolicyConfig>
<Name>complete-cache-policy</Name>
<Comment>all fields</Comment>
<MinTTL>10</MinTTL>
<DefaultTTL>3600</DefaultTTL>
<MaxTTL>86400</MaxTTL>
<ParametersInCacheKeyAndForwardedToOrigin>
<EnableAcceptEncodingGzip>true</EnableAcceptEncodingGzip>
<EnableAcceptEncodingBrotli>true</EnableAcceptEncodingBrotli>
<HeadersConfig>
<HeaderBehavior>whitelist</HeaderBehavior>
<Headers><Quantity>2</Quantity><Items>
<Name>Accept-Language</Name><Name>CloudFront-Viewer-Country</Name>
</Items></Headers>
</HeadersConfig>
<CookiesConfig>
<CookieBehavior>whitelist</CookieBehavior>
<Cookies><Quantity>2</Quantity><Items>
<Name>session</Name><Name>locale</Name>
</Items></Cookies>
</CookiesConfig>
<QueryStringsConfig>
<QueryStringBehavior>whitelist</QueryStringBehavior>
<QueryStrings><Quantity>2</Quantity><Items>
<Name>page</Name><Name>sort</Name>
</Items></QueryStrings>
</QueryStringsConfig>
</ParametersInCacheKeyAndForwardedToOrigin>
</CachePolicyConfig>
""";
Map<String, Object> config = CloudFrontPolicyConfigCodec.parseCachePolicy(body);
assertEquals(Map.of(
"MinTTL", "10",
"DefaultTTL", "3600",
"MaxTTL", "86400",
"ParametersInCacheKeyAndForwardedToOrigin", Map.of(
"EnableAcceptEncodingGzip", "true",
"EnableAcceptEncodingBrotli", "true",
"HeadersConfig", Map.of(
"HeaderBehavior", "whitelist",
"Headers", List.of("Accept-Language", "CloudFront-Viewer-Country")),
"CookiesConfig", Map.of(
"CookieBehavior", "whitelist",
"Cookies", List.of("session", "locale")),
"QueryStringsConfig", Map.of(
"QueryStringBehavior", "whitelist",
"QueryStrings", List.of("page", "sort")))),
config);
XmlBuilder xml = new XmlBuilder().start("CachePolicyConfig");
CloudFrontPolicyConfigCodec.serializeCachePolicy(xml, config);
Map<String, Object> reparsed = CloudFrontPolicyConfigCodec.parseCachePolicy(
xml.end("CachePolicyConfig").build());
assertEquals(config, reparsed);
}
@Test
void originRequestPolicyConfigRoundTripsAllForwardedMembers() {
String body = """
<OriginRequestPolicyConfig>
<Name>complete-origin-policy</Name>
<HeadersConfig>
<HeaderBehavior>whitelist</HeaderBehavior>
<Headers><Quantity>1</Quantity><Items><Name>Origin</Name></Items></Headers>
</HeadersConfig>
<CookiesConfig>
<CookieBehavior>whitelist</CookieBehavior>
<Cookies><Quantity>1</Quantity><Items><Name>session</Name></Items></Cookies>
</CookiesConfig>
<QueryStringsConfig>
<QueryStringBehavior>whitelist</QueryStringBehavior>
<QueryStrings><Quantity>1</Quantity><Items><Name>page</Name></Items></QueryStrings>
</QueryStringsConfig>
</OriginRequestPolicyConfig>
""";
Map<String, Object> config = CloudFrontPolicyConfigCodec.parseOriginRequestPolicy(body);
assertEquals(Map.of(
"HeadersConfig", Map.of(
"HeaderBehavior", "whitelist", "Headers", List.of("Origin")),
"CookiesConfig", Map.of(
"CookieBehavior", "whitelist", "Cookies", List.of("session")),
"QueryStringsConfig", Map.of(
"QueryStringBehavior", "whitelist", "QueryStrings", List.of("page"))),
config);
XmlBuilder xml = new XmlBuilder().start("OriginRequestPolicyConfig");
CloudFrontPolicyConfigCodec.serializeOriginRequestPolicy(xml, config);
Map<String, Object> reparsed = CloudFrontPolicyConfigCodec.parseOriginRequestPolicy(
xml.end("OriginRequestPolicyConfig").build());
assertEquals(config, reparsed);
}
@Test
void cachePolicyConfigAppliesAwsTtlDefaults() {
Map<String, Object> standard = CloudFrontPolicyConfigCodec.parseCachePolicy("""
<CachePolicyConfig><MinTTL>10</MinTTL></CachePolicyConfig>
""");
assertEquals("86400", standard.get("DefaultTTL"));
assertEquals("31536000", standard.get("MaxTTL"));
Map<String, Object> raisedMinimum = CloudFrontPolicyConfigCodec.parseCachePolicy("""
<CachePolicyConfig><MinTTL>40000000</MinTTL></CachePolicyConfig>
""");
assertEquals("40000000", raisedMinimum.get("DefaultTTL"));
assertEquals("40000000", raisedMinimum.get("MaxTTL"));
Map<String, Object> raisedDefault = CloudFrontPolicyConfigCodec.parseCachePolicy("""
<CachePolicyConfig><MinTTL>0</MinTTL><DefaultTTL>40000000</DefaultTTL></CachePolicyConfig>
""");
assertEquals("40000000", raisedDefault.get("MaxTTL"));
}
@Test
void selectionParsingAcceptsOnlyNamesAndPreservesEmptyShapes() {
String body = """
<OriginRequestPolicyConfig>
<HeadersConfig><HeaderBehavior>none</HeaderBehavior></HeadersConfig>
<CookiesConfig><CookieBehavior>whitelist</CookieBehavior>
<Cookies><Quantity>0</Quantity></Cookies>
</CookiesConfig>
<QueryStringsConfig><QueryStringBehavior>whitelist</QueryStringBehavior>
<QueryStrings><Quantity>1</Quantity><Items>
<Ignored>not-a-name</Ignored><Name>page</Name>
</Items></QueryStrings>
</QueryStringsConfig>
</OriginRequestPolicyConfig>
""";
Map<String, Object> config = CloudFrontPolicyConfigCodec.parseOriginRequestPolicy(body);
assertEquals(Map.of("HeaderBehavior", "none"), config.get("HeadersConfig"));
assertEquals(Map.of("CookieBehavior", "whitelist", "Cookies", List.of()),
config.get("CookiesConfig"));
assertEquals(Map.of("QueryStringBehavior", "whitelist", "QueryStrings", List.of("page")),
config.get("QueryStringsConfig"));
XmlBuilder xml = new XmlBuilder().start("OriginRequestPolicyConfig");
CloudFrontPolicyConfigCodec.serializeOriginRequestPolicy(xml, config);
String serialized = xml.end("OriginRequestPolicyConfig").build();
assertTrue(serialized.contains("<HeaderBehavior>none</HeaderBehavior>"));
assertFalse(serialized.contains("<Headers>"));
assertTrue(serialized.contains("<Cookies><Quantity>0</Quantity></Cookies>"));
assertFalse(serialized.contains("<Cookies><Quantity>0</Quantity><Items>"));
}
}