fix(cloudfront): store and return a distribution's ConnectionMode (#8934)

* fix(cloudfront): store and return a distribution's ConnectionMode
* test(cloudfront): deploy the ConnectionMode stack with CfnRequests and check refusals store nothing
This commit is contained in:
Joaquín Gamboa authored and GitHub committed 2026-11-18 23:53:11 -04:00
1 parent 9a38bd0bd2
commit afd3107151
9 files changed
+234 -1

No files matched your search

@@ -6,6 +6,7 @@ import io.floci.aws.services.cloudfront.model.KeyGroup;
import io.floci.aws.services.cloudfront.model.OriginAccessControl;
import io.floci.aws.services.cloudfront.model.PublicKey;
import io.floci.aws.services.s3.S3Service;
import io.floci.aws.testing.CfnRequests;
import io.floci.aws.testing.CfnStackWaits;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
@@ -21,6 +22,7 @@ import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.xml.HasXPath.hasXPath;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
@@ -380,4 +382,44 @@ class CloudFormationCloudFrontDistributionIntegrationTest {
}
""".formatted(comment, alias, bucket);
}
@Test
void createStackCarriesTheConnectionModeOntoTheDistribution() {
String stackName = "cfn-cloudfront-tenant-only-" + Long.toString(System.nanoTime(), 36);
String template = """
{
"Resources": {
"Dist": {
"Type": "AWS::CloudFront::Distribution",
"Properties": {
"DistributionConfig": {
"Enabled": true,
"ConnectionMode": "tenant-only",
"Origins": [{
"Id": "origin",
"DomainName": "origin.example.com",
"CustomOriginConfig": {"OriginProtocolPolicy": "https-only"}
}],
"DefaultCacheBehavior": {
"TargetOriginId": "origin",
"ViewerProtocolPolicy": "redirect-to-https"
}
}
}
}
}
}
""";
CfnRequests.deploy("CreateStack", stackName, template, CFN_AUTH, "CREATE_COMPLETE");
String distributionId = CfnRequests.physicalId(stackName, "Dist", CFN_AUTH);
given()
.when()
.get("/2020-05-31/distribution/" + distributionId)
.then()
.statusCode(200)
.body(hasXPath("//*[local-name()='DistributionConfig']/*[local-name()='ConnectionMode']",
equalTo("tenant-only")));
}
}
@@ -212,6 +212,43 @@ class CloudFrontCfnProvisionerTest {
assertEquals(Integer.valueOf(120), origin.getCustomOriginConfig().get("OriginKeepaliveTimeout"));
}
@Test
void copiesTheConnectionModeTheTemplateDeclares() throws Exception {
when(cloudFront.createDistribution(any(), any())).thenReturn(distribution("E1DIST", "d111.cloudfront.net"));
StackResource r = resource(DISTRIBUTION);
provisioner.provision(r, json("""
{"DistributionConfig": {
"ConnectionMode": "tenant-only",
"Origins": [{"Id": "o1", "DomainName": "origin.example.com",
"CustomOriginConfig": {"OriginProtocolPolicy": "https-only"}}],
"DefaultCacheBehavior": {"TargetOriginId": "o1", "ViewerProtocolPolicy": "redirect-to-https"}
}}
"""), distributionCtx(null));
ArgumentCaptor<Distribution> sent = ArgumentCaptor.forClass(Distribution.class);
verify(cloudFront).createDistribution(sent.capture(), any());
assertEquals("tenant-only", sent.getValue().getConfig().getConnectionMode());
}
@Test
void leavesTheConnectionModeUnsetWhenTheTemplateOmitsIt() throws Exception {
when(cloudFront.createDistribution(any(), any())).thenReturn(distribution("E1DIST", "d111.cloudfront.net"));
StackResource r = resource(DISTRIBUTION);
provisioner.provision(r, json("""
{"DistributionConfig": {
"Origins": [{"Id": "o1", "DomainName": "origin.example.com",
"CustomOriginConfig": {"OriginProtocolPolicy": "https-only"}}],
"DefaultCacheBehavior": {"TargetOriginId": "o1", "ViewerProtocolPolicy": "redirect-to-https"}
}}
"""), distributionCtx(null));
ArgumentCaptor<Distribution> sent = ArgumentCaptor.forClass(Distribution.class);
verify(cloudFront).createDistribution(sent.capture(), any());
assertNull(sent.getValue().getConfig().getConnectionMode());
}
@Test
void aNonNumericOriginConnectionSettingFailsTheStackInsteadOfAServerError() {
StackResource r = resource(DISTRIBUTION);
@@ -40,6 +40,8 @@ class CloudFrontDistributionConfigMembersTest {
"//*[local-name()='Logging']/*[local-name()='Prefix']";
private static final String WEB_ACL_ID =
"//*[local-name()='WebACLId']";
private static final String CONNECTION_MODE =
"//*[local-name()='DistributionConfig']/*[local-name()='ConnectionMode']";
private final CloudFrontService cloudFrontService;
@@ -344,4 +346,74 @@ class CloudFrontDistributionConfigMembersTest {
.statusCode(200)
.body(hasXPath(WEB_ACL_ID, equalTo("")));
}
@Test
void connectionModeRoundTripsThroughCreateUpdateAndDistributionReads() {
String originId = "origin-connection-mode-" + UUID.randomUUID();
String created = given()
.contentType(ContentType.XML)
.body(distributionConfigBody(originId, "<ConnectionMode>tenant-only</ConnectionMode>"))
.when()
.post(DISTRIBUTIONS)
.then()
.statusCode(201)
.body(hasXPath(CONNECTION_MODE, equalTo("tenant-only")))
.extract().asString();
String id = XmlParser.extractFirst(created, "Id", null);
given().when().get(API + id + "/config").then()
.statusCode(200)
.body(hasXPath("//*[local-name()='ConnectionMode']", equalTo("tenant-only")));
given().when().get(API + id).then()
.statusCode(200)
.body(hasXPath(CONNECTION_MODE, equalTo("tenant-only")));
given().when().get(DISTRIBUTIONS).then()
.statusCode(200)
.body(hasXPath(summaryConnectionMode(id), equalTo("tenant-only")));
updateDistribution(id, distributionConfigBody(originId, "<ConnectionMode>tenant-only</ConnectionMode>"))
.statusCode(200)
.body(hasXPath(CONNECTION_MODE, equalTo("tenant-only")));
}
@Test
void connectionModeIsLeftOutWhenTheRequestOmitsIt() {
// No published sentence says which mode an omitted ConnectionMode reads back as, so
// the member is reported only when the caller set it.
String id = postDistribution(distributionConfigBody(
"origin-no-connection-mode-" + UUID.randomUUID(), ""));
given().when().get(API + id + "/config").then()
.statusCode(200)
.body(hasXPath("count(//*[local-name()='ConnectionMode'])", equalTo("0")));
given().when().get(DISTRIBUTIONS).then()
.statusCode(200)
.body(hasXPath("count(" + summaryConnectionMode(id) + ")", equalTo("0")));
}
@Test
void connectionModeOutsideItsEnumerationIsRefused() {
String originId = "origin-bad-connection-mode-" + UUID.randomUUID();
given()
.contentType(ContentType.XML)
.body(distributionConfigBody(originId, "<ConnectionMode>shared</ConnectionMode>"))
.when()
.post(DISTRIBUTIONS)
.then()
.statusCode(400)
.body(hasXPath("//*[local-name()='Code']", equalTo("InvalidArgument")))
.body(hasXPath("//*[local-name()='Message']",
equalTo("The ConnectionMode specified is not valid.")));
// The refused request created no distribution.
given().when().get(DISTRIBUTIONS).then()
.statusCode(200)
.body(hasXPath("count(//*[local-name()='Origin']/*[local-name()='Id' and text()='"
+ originId + "'])", equalTo("0")));
}
private static String summaryConnectionMode(String id) {
return "//*[local-name()='DistributionSummary']"
+ "[*[local-name()='Id' and text()='" + id + "']]"
+ "/*[local-name()='ConnectionMode']";
}
}
@@ -1843,6 +1843,38 @@ class CloudFrontServiceTest {
LinkedHashMap::new));
}
@Test
void createDistributionKeepsTheConnectionModeTheConfigNames() {
CloudFrontService service = serviceWithDomainSuffix("cloudfront.net");
Distribution tenantOnly = distribution(true, List.of());
tenantOnly.getConfig().setConnectionMode("tenant-only");
Distribution created = service.createDistribution(tenantOnly, Map.of());
assertEquals("tenant-only", service.getDistribution(created.getId()).getConfig().getConnectionMode());
}
@Test
void createAndUpdateDistributionRefuseAConnectionModeOutsideItsEnumeration() {
CloudFrontService service = serviceWithDomainSuffix("cloudfront.net");
Distribution created = service.createDistribution(distribution(true, List.of()), Map.of());
Distribution shared = distribution(true, List.of());
shared.getConfig().setConnectionMode("shared");
AwsException onCreate = assertAws("InvalidArgument",
() -> service.createDistribution(shared, Map.of()));
AwsException onUpdate = assertAws("InvalidArgument",
() -> service.updateDistribution(created.getId(), created.getEtag(), shared));
assertEquals("The ConnectionMode specified is not valid.", onCreate.getMessage());
assertEquals("The ConnectionMode specified is not valid.", onUpdate.getMessage());
// Neither refusal stored anything: no second distribution, and the first is as it was.
assertEquals(1, service.listDistributions(null, 100).size());
Distribution unchanged = service.getDistribution(created.getId());
assertNull(unchanged.getConfig().getConnectionMode());
assertEquals(created.getEtag(), unchanged.getEtag());
}
private static AwsException assertAws(
String code, Executable action) {
AwsException error = assertThrows(AwsException.class, action);