Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(cloudfront): echo TrustedSigners in cache behaviors so the AWS provider does not segfault (#3015)

Matan Baruch75e93e5e77be
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 e6e2450456..c8429513aa 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
@@ -1968,6 +1968,7 @@ public class CloudFrontController {
.elem("ResponseHeadersPolicyId", dcb.getResponseHeadersPolicyId())
.elem("Compress", dcb.isCompress());
+ xml.raw(xmlTrustedSigners());
xml.raw(xmlTrustedKeyGroups(
dcb.isTrustedKeyGroupsEnabled(), dcb.getTrustedKeyGroups()));
@@ -1997,6 +1998,7 @@ public class CloudFrontController {
.elem("ResponseHeadersPolicyId", cb.getResponseHeadersPolicyId())
.elem("Compress", cb.isCompress());
+ xml.raw(xmlTrustedSigners());
xml.raw(xmlTrustedKeyGroups(
cb.isTrustedKeyGroupsEnabled(), cb.getTrustedKeyGroups()));
@@ -2068,6 +2070,18 @@ public class CloudFrontController {
return xml.end("TrustedKeyGroups").build();
}
+ // AWS always echoes a (usually empty) TrustedSigners object in every cache behavior. Floci models
+ // only the modern TrustedKeyGroups, but the Terraform AWS provider reads TrustedSigners.Items with
+ // no nil guard, so an omitted object segfaults it on read-back. Emit the disabled form AWS returns.
+ private String xmlTrustedSigners() {
+ return new XmlBuilder()
+ .start("TrustedSigners")
+ .elem("Enabled", false)
+ .elem("Quantity", 0)
+ .end("TrustedSigners")
+ .build();
+ }
+
private String xmlActiveTrustedKeyGroups(DistributionConfig config) {
List<KeyGroup> groups = service.activeTrustedKeyGroups(config);
XmlBuilder xml = new XmlBuilder()
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 67290cd01f..1ec5f1c02b 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
@@ -158,6 +158,39 @@ class CloudFrontControllerTest {
}
}
+ @Test
+ void defaultCacheBehaviorEchoesTrustedSignersSoTheAwsProviderDoesNotSegfault() {
+ CloudFrontService service = mock(CloudFrontService.class);
+ CloudFrontController controller = new CloudFrontController(service);
+
+ String body = distributionConfigBody("");
+
+ ArgumentCaptor<Distribution> captor = ArgumentCaptor.forClass(Distribution.class);
+ when(service.createDistribution(captor.capture(), any())).thenAnswer(inv -> {
+ Distribution d = inv.getArgument(0);
+ d.setId("dist-ts");
+ d.setEtag("etag-ts");
+ return d;
+ });
+
+ try (Response created = controller.createDistribution(null, body)) {
+ assertEquals(201, created.getStatus());
+ }
+
+ when(service.getDistribution("dist-ts")).thenReturn(captor.getValue());
+
+ // The Terraform AWS provider reads DefaultCacheBehavior.TrustedSigners.Items with no nil
+ // guard, so an omitted object segfaults it on read-back. AWS always echoes the disabled form.
+ try (Response dist = controller.getDistribution("dist-ts")) {
+ String xml = (String) dist.getEntity();
+ int ts = xml.indexOf("<TrustedSigners>");
+ assertTrue(ts >= 0, "DefaultCacheBehavior must echo a TrustedSigners object");
+ String block = xml.substring(ts, xml.indexOf("</TrustedSigners>", ts));
+ assertTrue(block.contains("<Enabled>false</Enabled>"), "TrustedSigners.Enabled=false");
+ assertTrue(block.contains("<Quantity>0</Quantity>"), "TrustedSigners.Quantity=0");
+ }
+ }
+
@Test
void orderedCacheBehaviorRoundTripsLambdaFunctionAssociations() {
CloudFrontService service = mock(CloudFrontService.class);