diff --git a/src/main/java/io/floci/aws/services/cloudfront/CloudFrontController.java b/src/main/java/io/floci/aws/services/cloudfront/CloudFrontController.java index c8b7e0d19d..cf86633f70 100644 --- a/src/main/java/io/floci/aws/services/cloudfront/CloudFrontController.java +++ b/src/main/java/io/floci/aws/services/cloudfront/CloudFrontController.java @@ -13,6 +13,7 @@ import org.jboss.logging.Logger; import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; + import java.net.URI; import java.nio.charset.StandardCharsets; import java.util.ArrayList; @@ -1900,7 +1901,7 @@ public class CloudFrontController { .elem("HttpVersion", cfg.getHttpVersion() != null ? cfg.getHttpVersion() : "http2") .elem("PriceClass", cfg.getPriceClass() != null ? cfg.getPriceClass() : "PriceClass_All") .elem("IsIPV6Enabled", cfg.isIPV6Enabled()) - .elem("WebAclId", cfg.getWebAclId() != null ? cfg.getWebAclId() : ""); + .elem("WebACLId", cfg.getWebAclId() != null ? cfg.getWebAclId() : ""); List origins = cfg.getOrigins(); xml.raw(xmlQuantityItems("Origins", "Origin", origins != null ? origins.size() : 0, @@ -2367,6 +2368,7 @@ public class CloudFrontController { } private String xmlDistributionSummary(Distribution d) { + DistributionConfig cfg = d.getConfig(); XmlBuilder xml = new XmlBuilder() .start("DistributionSummary") .elem("Id", d.getId()) @@ -2383,9 +2385,9 @@ public class CloudFrontController { .elem("PriceClass", d.getConfig() != null && d.getConfig().getPriceClass() != null ? d.getConfig().getPriceClass() : "PriceClass_All") .elem("IsIPV6Enabled", d.getConfig() != null && d.getConfig().isIPV6Enabled()) - .elem("WebAclId", ""); + .elem("WebACLId", cfg != null && cfg.getWebAclId() != null + ? cfg.getWebAclId() : ""); - DistributionConfig cfg = d.getConfig(); List origins = cfg != null ? cfg.getOrigins() : null; xml.raw(xmlQuantityItems("Origins", "Origin", origins != null ? origins.size() : 0, @@ -2665,7 +2667,8 @@ public class CloudFrontController { values.getOrDefault("PriceClass", "PriceClass_All")); cfg.setIPV6Enabled("true".equalsIgnoreCase( values.getOrDefault("IsIPV6Enabled", "true"))); - cfg.setWebAclId(values.get("WebAclId")); + cfg.setWebAclId(values.containsKey("WebACLId") + ? values.get("WebACLId") : values.get("WebAclId")); cfg.setContinuousDeploymentPolicyId( values.get("ContinuousDeploymentPolicyId")); cfg.setStaging("true".equalsIgnoreCase( @@ -2744,6 +2747,7 @@ public class CloudFrontController { "HttpVersion", "PriceClass", "IsIPV6Enabled", + "WebACLId", "WebAclId", "ContinuousDeploymentPolicyId", "Staging" -> true; diff --git a/src/test/java/io/floci/aws/services/cloudfront/CloudFrontDistributionConfigMembersTest.java b/src/test/java/io/floci/aws/services/cloudfront/CloudFrontDistributionConfigMembersTest.java index 48c5f54a17..4de6e1c7b4 100644 --- a/src/test/java/io/floci/aws/services/cloudfront/CloudFrontDistributionConfigMembersTest.java +++ b/src/test/java/io/floci/aws/services/cloudfront/CloudFrontDistributionConfigMembersTest.java @@ -12,6 +12,7 @@ import org.junit.jupiter.api.Test; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.UUID; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.equalTo; @@ -36,6 +37,8 @@ class CloudFrontDistributionConfigMembersTest { "//*[local-name()='Logging']/*[local-name()='Bucket']"; private static final String LOGGING_PREFIX = "//*[local-name()='Logging']/*[local-name()='Prefix']"; + private static final String WEB_ACL_ID = + "//*[local-name()='WebACLId']"; private final CloudFrontService cloudFrontService; @@ -141,10 +144,10 @@ class CloudFrontDistributionConfigMembersTest { /** * A DistributionConfig request body, which is what an SDK or the Terraform provider - * actually sends. {@code loggingBlock} goes in verbatim so a caller that omits Logging - * and a caller that supplies it share one shape. + * actually sends. {@code additionalMembers} goes in verbatim so optional config + * members can be tested through the same request shape. */ - private static String distributionConfigBody(String originId, String loggingBlock) { + private static String distributionConfigBody(String originId, String additionalMembers) { return """ %s @@ -161,7 +164,7 @@ class CloudFrontDistributionConfigMembersTest { allow-all - """.formatted(originId, loggingBlock, originId, originId); + """.formatted(originId, additionalMembers, originId, originId); } /** POSTs a config body and returns the id of the distribution it created. */ @@ -248,4 +251,121 @@ class CloudFrontDistributionConfigMembersTest { .body(hasXPath(LOGGING_BUCKET, equalTo("update-logs.s3.amazonaws.com"))) .body(hasXPath(LOGGING_PREFIX, equalTo("update/"))); } + + @Test + void webAclIdRoundTripsThroughCreateUpdateAndDistributionReads() { + String originId = "origin-web-acl-" + UUID.randomUUID(); + String firstAcl = "arn:aws:wafv2:us-east-1:000000000000:global/webacl/edge/first"; + String secondAcl = "arn:aws:wafv2:us-east-1:000000000000:global/webacl/edge/second"; + String created = given() + .contentType(ContentType.XML) + .body(distributionConfigBody(originId, + "" + firstAcl + "")) + .when() + .post(DISTRIBUTIONS) + .then() + .statusCode(201) + .body(hasXPath(WEB_ACL_ID, equalTo(firstAcl))) + .extract().asString(); + String id = XmlParser.extractFirst(created, "Id", null); + String summaryAcl = "//*[local-name()='DistributionSummary']" + + "[*[local-name()='Id' and text()='" + id + "']]" + + "/*[local-name()='WebACLId']"; + + given().when().get(API + id + "/config").then() + .statusCode(200) + .body(hasXPath(WEB_ACL_ID, equalTo(firstAcl))); + given().when().get(API + id).then() + .statusCode(200) + .body(hasXPath(WEB_ACL_ID, equalTo(firstAcl))); + given().when().get(DISTRIBUTIONS).then() + .statusCode(200) + .body(hasXPath(summaryAcl, equalTo(firstAcl))); + + String etag = given().when().get(API + id + "/config").then() + .statusCode(200).extract().header("ETag"); + given().contentType(ContentType.XML).header("If-Match", etag) + .body(distributionConfigBody(originId, + "" + secondAcl + "")) + .when() + .put(API + id + "/config") + .then() + .statusCode(200) + .body(hasXPath(WEB_ACL_ID, equalTo(secondAcl))); + given().when().get(DISTRIBUTIONS).then() + .statusCode(200) + .body(hasXPath(summaryAcl, equalTo(secondAcl))); + + etag = given().when().get(API + id + "/config").then() + .statusCode(200).extract().header("ETag"); + given().contentType(ContentType.XML).header("If-Match", etag) + .body(distributionConfigBody(originId, "")) + .when() + .put(API + id + "/config") + .then() + .statusCode(200) + .body(hasXPath(WEB_ACL_ID, equalTo(""))); + given().when().get(API + id + "/config").then() + .statusCode(200) + .body(hasXPath(WEB_ACL_ID, equalTo(""))); + given().when().get(API + id).then() + .statusCode(200) + .body(hasXPath(WEB_ACL_ID, equalTo(""))); + given().when().get(DISTRIBUTIONS).then() + .statusCode(200) + .body(hasXPath(summaryAcl, equalTo(""))); + } + + @Test + void webAclIdDefaultsToEmptyWhenOmitted() { + String id = postDistribution(distributionConfigBody( + "origin-no-web-acl-" + UUID.randomUUID(), "")); + String summaryAcl = "//*[local-name()='DistributionSummary']" + + "[*[local-name()='Id' and text()='" + id + "']]" + + "/*[local-name()='WebACLId']"; + + given().when().get(API + id + "/config").then() + .statusCode(200) + .body(hasXPath(WEB_ACL_ID, equalTo(""))); + given().when().get(DISTRIBUTIONS).then() + .statusCode(200) + .body(hasXPath(summaryAcl, equalTo(""))); + } + + @Test + void legacyWebAclIdUpdateRetainsAssociationAndCanonicalValueTakesPrecedence() { + String originId = "origin-legacy-web-acl-" + UUID.randomUUID(); + String firstAcl = "arn:aws:wafv2:us-east-1:000000000000:global/webacl/edge/first"; + String legacyAcl = "arn:aws:wafv2:us-east-1:000000000000:global/webacl/edge/legacy"; + String id = postDistribution(distributionConfigBody(originId, + "" + firstAcl + "")); + String etag = given().when().get(API + id + "/config").then() + .statusCode(200).extract().header("ETag"); + + given().contentType(ContentType.XML).header("If-Match", etag) + .body(distributionConfigBody(originId, + "" + legacyAcl + "")) + .when() + .put(API + id + "/config") + .then() + .statusCode(200) + .body(hasXPath(WEB_ACL_ID, equalTo(legacyAcl))); + given().when().get(API + id + "/config").then() + .statusCode(200) + .body(hasXPath(WEB_ACL_ID, equalTo(legacyAcl))); + + etag = given().when().get(API + id + "/config").then() + .statusCode(200).extract().header("ETag"); + given().contentType(ContentType.XML).header("If-Match", etag) + .body(distributionConfigBody(originId, + "" + legacyAcl + "")) + .when() + .put(API + id + "/config") + .then() + .statusCode(200) + .body(hasXPath(WEB_ACL_ID, equalTo(""))); + given().when().get(API + id + "/config").then() + .statusCode(200) + .body(hasXPath(WEB_ACL_ID, equalTo(""))); + } }