diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontController.java b/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontController.java index c8429513aa..1745d67a45 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontController.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontController.java @@ -1972,12 +1972,7 @@ public class CloudFrontController { xml.raw(xmlTrustedKeyGroups( dcb.isTrustedKeyGroupsEnabled(), dcb.getTrustedKeyGroups())); - List allowed = dcb.getAllowedMethods(); - if (allowed == null || allowed.isEmpty()) { - allowed = List.of("GET", "HEAD"); - } - xml.raw(xmlQuantityItems("AllowedMethods", "Method", allowed.size(), - allowed.stream().map(m -> "" + XmlBuilder.escape(m) + "").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 allowed = cb.getAllowedMethods(); - if (allowed == null || allowed.isEmpty()) { - allowed = List.of("GET", "HEAD"); - } - xml.raw(xmlQuantityItems("AllowedMethods", "Method", allowed.size(), - allowed.stream().map(m -> "" + XmlBuilder.escape(m) + "").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 allowedMethods, List cachedMethods) { + List 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 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 allowedMethods = new ArrayList<>(); + List cachedMethods = new ArrayList<>(); List 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 allowedMethods = new ArrayList<>(); + List cachedMethods = new ArrayList<>(); List 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); } diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontControllerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontControllerTest.java index 1ec5f1c02b..d17619f326 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontControllerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontControllerTest.java @@ -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( + """ + 2 + GETHEAD""", + """ + 3 + GETHEADOPTIONS + 2 + GETHEAD + """); + + ArgumentCaptor 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(""); + int end = xml.indexOf("") + "".length(); + String block = xml.substring(start, end); + + int cachedStart = block.indexOf(""); + 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);