fix(cloudfront): stop duplicating CachedMethods into AllowedMethods.Items (#3217)

DefaultCacheBehavior/CacheBehavior parsing tracked a single inAllowedMethods
flag across the whole <AllowedMethods> element, so <Method> entries nested
inside the sibling <CachedMethods> sub-object were folded into the parsed
AllowedMethods list too (and CachedMethods itself was silently dropped).
Since terraform-provider-aws's aws_cloudfront_distribution requires both
allowed_methods and cached_methods on every cache behavior, this produced a
permanent per-plan diff: AllowedMethods.Items came back duplicated
(["GET","HEAD","GET","HEAD","OPTIONS"]) and CachedMethods was missing
entirely.

Scope one of the two follow-up fidelity gaps flagged (but deliberately left
unfixed) by #3015, which fixed the TrustedSigners crash from #2767. The
other gap from that issue (ForwardedValues/MinTTL/DefaultTTL/MaxTTL dropped
on read-back for the legacy forwarded_values cache-behavior path) is a
separate, larger change, filed as #3208.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-07 20:28:44 -05:00
1 parent 3af6f47bf8
commit 9e9b0b6e91
2 files changed
+103 -14

No files matched your search

@@ -1972,12 +1972,7 @@ public class CloudFrontController {
xml.raw(xmlTrustedKeyGroups(
dcb.isTrustedKeyGroupsEnabled(), dcb.getTrustedKeyGroups()));
List<String> allowed = dcb.getAllowedMethods();
if (allowed == null || allowed.isEmpty()) {
allowed = List.of("GET", "HEAD");
}
xml.raw(xmlQuantityItems("AllowedMethods", "Method", allowed.size(),
allowed.stream().map(m -> "<Method>" + XmlBuilder.escape(m) + "</Method>").toList()));
xml.raw(xmlAllowedMethods(dcb.getAllowedMethods(), dcb.getCachedMethods()));
xml.raw(xmlFunctionAssociations(dcb.getFunctionAssociations()));
xml.raw(xmlLambdaFunctionAssociations(dcb.getLambdaFunctionAssociations()));
@@ -2002,12 +1997,7 @@ public class CloudFrontController {
xml.raw(xmlTrustedKeyGroups(
cb.isTrustedKeyGroupsEnabled(), cb.getTrustedKeyGroups()));
List<String> allowed = cb.getAllowedMethods();
if (allowed == null || allowed.isEmpty()) {
allowed = List.of("GET", "HEAD");
}
xml.raw(xmlQuantityItems("AllowedMethods", "Method", allowed.size(),
allowed.stream().map(m -> "<Method>" + XmlBuilder.escape(m) + "</Method>").toList()));
xml.raw(xmlAllowedMethods(cb.getAllowedMethods(), cb.getCachedMethods()));
xml.raw(xmlFunctionAssociations(cb.getFunctionAssociations()));
xml.raw(xmlLambdaFunctionAssociations(cb.getLambdaFunctionAssociations()));
@@ -2305,6 +2295,28 @@ public class CloudFrontController {
return value != null ? value.toString() : "";
}
// AllowedMethods.CachedMethods is a nested Quantity/Items pair, not a sibling of AllowedMethods
// itself. Terraform's aws_cloudfront_distribution requires both allowed_methods and
// cached_methods on every cache behavior, so omitting this sub-object is a permanent diff.
private String xmlAllowedMethods(List<String> allowedMethods, List<String> cachedMethods) {
List<String> allowed = allowedMethods == null || allowedMethods.isEmpty()
? List.of("GET", "HEAD") : allowedMethods;
XmlBuilder xml = new XmlBuilder().start("AllowedMethods").elem("Quantity", allowed.size());
xml.start("Items");
for (String method : allowed) {
xml.elem("Method", method);
}
xml.end("Items");
if (cachedMethods != null && !cachedMethods.isEmpty()) {
xml.start("CachedMethods").elem("Quantity", cachedMethods.size()).start("Items");
for (String method : cachedMethods) {
xml.elem("Method", method);
}
xml.end("Items").end("CachedMethods");
}
return xml.end("AllowedMethods").build();
}
private String xmlQuantityItems(String wrapper, String itemTag, int count, List<String> items) {
XmlBuilder xml = new XmlBuilder().start(wrapper).elem("Quantity", count);
if (count > 0 && items != null && !items.isEmpty()) {
@@ -2844,11 +2856,13 @@ public class CloudFrontController {
XMLStreamReader r = XML_FACTORY.createXMLStreamReader(new StringReader(body));
boolean inDcb = false;
boolean inAllowedMethods = false;
boolean inCachedMethods = false;
boolean inTrustedKeyGroups = false;
boolean sawTrustedKeyGroups = false;
Boolean trustedKeyGroupsEnabled = null;
Integer trustedKeyGroupsQuantity = null;
List<String> allowedMethods = new ArrayList<>();
List<String> cachedMethods = new ArrayList<>();
List<String> trustedKeyGroups = new ArrayList<>();
boolean inLambdaAssociations = false;
boolean inFunctionAssociations = false;
@@ -2940,6 +2954,9 @@ public class CloudFrontController {
case "AllowedMethods" -> {
if (inDcb) inAllowedMethods = true;
}
case "CachedMethods" -> {
if (inAllowedMethods) inCachedMethods = true;
}
case "TargetOriginId" -> {
if (inDcb) dcb.setTargetOriginId(r.getElementText());
}
@@ -2965,13 +2982,18 @@ public class CloudFrontController {
if (inDcb) dcb.setCompress("true".equalsIgnoreCase(r.getElementText()));
}
case "Method" -> {
if (inAllowedMethods) allowedMethods.add(r.getElementText());
if (inCachedMethods) {
cachedMethods.add(r.getElementText());
} else if (inAllowedMethods) {
allowedMethods.add(r.getElementText());
}
}
default -> {
}
}
} else if (event == XMLStreamConstants.END_ELEMENT) {
switch (r.getLocalName()) {
case "CachedMethods" -> inCachedMethods = false;
case "AllowedMethods" -> inAllowedMethods = false;
case "TrustedKeyGroups" -> inTrustedKeyGroups = false;
case "DefaultCacheBehavior" -> inDcb = false;
@@ -2998,6 +3020,9 @@ public class CloudFrontController {
if (!allowedMethods.isEmpty()) {
dcb.setAllowedMethods(allowedMethods);
}
if (!cachedMethods.isEmpty()) {
dcb.setCachedMethods(cachedMethods);
}
if (!trustedKeyGroups.isEmpty()) {
dcb.setTrustedKeyGroups(trustedKeyGroups);
}
@@ -3044,12 +3069,14 @@ public class CloudFrontController {
boolean inCacheBehaviors = false;
boolean inCacheBehavior = false;
boolean inAllowedMethods = false;
boolean inCachedMethods = false;
boolean inTrustedKeyGroups = false;
CacheBehavior current = null;
boolean sawTrustedKeyGroups = false;
Boolean trustedKeyGroupsEnabled = null;
Integer trustedKeyGroupsQuantity = null;
List<String> allowedMethods = new ArrayList<>();
List<String> cachedMethods = new ArrayList<>();
List<String> trustedKeyGroups = new ArrayList<>();
boolean inLambdaAssociations = false;
boolean inFunctionAssociations = false;
@@ -3074,6 +3101,7 @@ public class CloudFrontController {
trustedKeyGroupsEnabled = null;
trustedKeyGroupsQuantity = null;
allowedMethods = new ArrayList<>();
cachedMethods = new ArrayList<>();
trustedKeyGroups = new ArrayList<>();
lambdaAssociations = new ArrayList<>();
functionAssociations = new ArrayList<>();
@@ -3156,6 +3184,9 @@ public class CloudFrontController {
case "AllowedMethods" -> {
if (inCacheBehavior) inAllowedMethods = true;
}
case "CachedMethods" -> {
if (inAllowedMethods) inCachedMethods = true;
}
case "PathPattern" -> {
if (inCacheBehavior && current != null) current.setPathPattern(r.getElementText());
}
@@ -3182,13 +3213,18 @@ public class CloudFrontController {
}
}
case "Method" -> {
if (inAllowedMethods) allowedMethods.add(r.getElementText());
if (inCachedMethods) {
cachedMethods.add(r.getElementText());
} else if (inAllowedMethods) {
allowedMethods.add(r.getElementText());
}
}
default -> {
}
}
} else if (event == XMLStreamConstants.END_ELEMENT) {
switch (r.getLocalName()) {
case "CachedMethods" -> inCachedMethods = false;
case "AllowedMethods" -> inAllowedMethods = false;
case "TrustedKeyGroups" -> inTrustedKeyGroups = false;
case "LambdaFunctionAssociations" -> inLambdaAssociations = false;
@@ -3210,6 +3246,9 @@ public class CloudFrontController {
if (!allowedMethods.isEmpty()) {
current.setAllowedMethods(allowedMethods);
}
if (!cachedMethods.isEmpty()) {
current.setCachedMethods(cachedMethods);
}
if (!trustedKeyGroups.isEmpty()) {
current.setTrustedKeyGroups(trustedKeyGroups);
}
@@ -191,6 +191,56 @@ class CloudFrontControllerTest {
}
}
@Test
void defaultCacheBehaviorEchoesCachedMethodsWithoutDuplicatingAllowedMethods() {
CloudFrontService service = mock(CloudFrontService.class);
CloudFrontController controller = new CloudFrontController(service);
// AWS nests CachedMethods inside AllowedMethods. A parser that doesn't scope Method
// elements to the right block folds the CachedMethods items into AllowedMethods too,
// producing duplicates (["GET","HEAD","GET","HEAD","OPTIONS"]) and dropping CachedMethods.
String body = distributionConfigBody("").replace(
"""
<AllowedMethods><Quantity>2</Quantity><Items>
<Method>GET</Method><Method>HEAD</Method></Items></AllowedMethods>""",
"""
<AllowedMethods><Quantity>3</Quantity><Items>
<Method>GET</Method><Method>HEAD</Method><Method>OPTIONS</Method></Items>
<CachedMethods><Quantity>2</Quantity><Items>
<Method>GET</Method><Method>HEAD</Method></Items></CachedMethods>
</AllowedMethods>""");
ArgumentCaptor<Distribution> captor = ArgumentCaptor.forClass(Distribution.class);
when(service.createDistribution(captor.capture(), any())).thenAnswer(inv -> {
Distribution d = inv.getArgument(0);
d.setId("dist-cm");
d.setEtag("etag-cm");
return d;
});
try (Response created = controller.createDistribution(null, body)) {
assertEquals(201, created.getStatus());
}
when(service.getDistribution("dist-cm")).thenReturn(captor.getValue());
try (Response dist = controller.getDistribution("dist-cm")) {
String xml = (String) dist.getEntity();
int start = xml.indexOf("<AllowedMethods>");
int end = xml.indexOf("</AllowedMethods>") + "</AllowedMethods>".length();
String block = xml.substring(start, end);
int cachedStart = block.indexOf("<CachedMethods>");
assertTrue(cachedStart >= 0, "AllowedMethods must echo a nested CachedMethods object");
String outer = block.substring(0, cachedStart);
String cached = block.substring(cachedStart);
assertEquals(List.of("GET", "HEAD", "OPTIONS"), XmlParser.extractAll(outer, "Method"),
"AllowedMethods.Items must not include CachedMethods entries");
assertEquals(List.of("GET", "HEAD"), XmlParser.extractAll(cached, "Method"));
}
}
@Test
void orderedCacheBehaviorRoundTripsLambdaFunctionAssociations() {
CloudFrontService service = mock(CloudFrontService.class);