fix(cloudfront): include terraform-required distribution fields (#1940)

Real CloudFront always returns DistributionConfig.OriginGroups and
DistributionConfig.Restrictions on Create/Get/ListDistributions. Floci
omitted both, so the Terraform AWS provider dereferenced a nil
OriginGroups.Quantity and panicked while flattening the distribution.

Emit an empty OriginGroups block plus a Restrictions/GeoRestriction block
(defaulting to RestrictionType "none") in the distribution config body and
in distribution summaries, and parse an incoming GeoRestriction so
submitted restriction types and locations are echoed back.

Closes #1930
This commit is contained in:
Ben Younes authored and GitHub committed 2026-08-21 10:33:04 -04:00
1 parent 04d120fdd5
commit 8ac23deb06
2 files changed
+209

No files matched your search

@@ -29,6 +29,16 @@ public class CloudFrontController {
private static final String NS = AwsNamespaces.CLOUDFRONT;
private static final String XML = "application/xml";
private static final String GEO_RESTRICTION = "GeoRestriction";
private static final String ORIGIN_GROUPS = "OriginGroups";
private static final String ITEMS = "Items";
private static final String LOCATION = "Location";
private static final String LOCATIONS = "Locations";
private static final String QUANTITY = "Quantity";
private static final String RESTRICTION_TYPE = "RestrictionType";
private static final String RESTRICTIONS = "Restrictions";
private static final String DEFAULT_GEO_RESTRICTION_TYPE = "none";
private static final int EMPTY_QUANTITY = 0;
private static final XMLInputFactory XML_FACTORY;
@@ -1760,6 +1770,7 @@ public class CloudFrontController {
List<Origin> origins = cfg.getOrigins();
xml.raw(xmlQuantityItems("Origins", "Origin", origins != null ? origins.size() : 0,
origins != null ? origins.stream().map(this::xmlOrigin).toList() : List.of()));
xml.raw(xmlEmptyOriginGroups());
if (cfg.getDefaultCacheBehavior() != null) {
xml.raw(xmlDefaultCacheBehavior(cfg.getDefaultCacheBehavior()));
@@ -1815,10 +1826,69 @@ public class CloudFrontController {
xml.end("Aliases");
xml.raw(xmlViewerCertificate(cfg.getViewerCertificate()));
xml.raw(xmlRestrictions(cfg.getGeoRestriction()));
return xml.build();
}
private String xmlEmptyOriginGroups() {
// Presence-only OriginGroups is intentional for Terraform compatibility; round-tripping groups is deferred.
return new XmlBuilder()
.start(ORIGIN_GROUPS)
.elem(QUANTITY, EMPTY_QUANTITY)
.end(ORIGIN_GROUPS)
.build();
}
private String xmlRestrictions(Map<String, Object> geoRestriction) {
String restrictionType = DEFAULT_GEO_RESTRICTION_TYPE;
int quantity = EMPTY_QUANTITY;
List<String> locations = List.of();
if (geoRestriction != null) {
restrictionType = String.valueOf(
geoRestriction.getOrDefault(RESTRICTION_TYPE, DEFAULT_GEO_RESTRICTION_TYPE));
locations = stringList(geoRestriction.get(LOCATIONS));
quantity = parseInt(geoRestriction.get(QUANTITY), EMPTY_QUANTITY);
if (quantity == EMPTY_QUANTITY && !locations.isEmpty()) {
quantity = locations.size();
}
}
XmlBuilder xml = new XmlBuilder()
.start(RESTRICTIONS)
.start(GEO_RESTRICTION)
.elem(RESTRICTION_TYPE, restrictionType)
.elem(QUANTITY, quantity);
if (!locations.isEmpty()) {
xml.start(ITEMS);
for (String location : locations) {
xml.elem(LOCATION, location);
}
xml.end(ITEMS);
}
return xml.end(GEO_RESTRICTION)
.end(RESTRICTIONS)
.build();
}
private List<String> stringList(Object value) {
if (value instanceof List<?> list) {
return list.stream().map(String::valueOf).toList();
}
return List.of();
}
private int parseInt(Object value, int defaultValue) {
if (value == null) {
return defaultValue;
}
try {
return Integer.parseInt(value.toString());
} catch (NumberFormatException e) {
return defaultValue;
}
}
private String xmlOrigin(Origin o) {
XmlBuilder xml = new XmlBuilder()
.start("Origin")
@@ -2013,6 +2083,7 @@ public class CloudFrontController {
origins != null ? origins.size() : 0,
origins != null ? origins.stream().map(this::xmlOrigin).toList()
: List.of()));
xml.raw(xmlEmptyOriginGroups());
List<String> aliases = cfg != null ? cfg.getAliases() : null;
int aliasCount = aliases != null ? aliases.size() : 0;
@@ -2027,6 +2098,7 @@ public class CloudFrontController {
xml.end("Aliases");
xml.raw(xmlViewerCertificate(cfg != null ? cfg.getViewerCertificate() : null));
xml.raw(xmlRestrictions(cfg != null ? cfg.getGeoRestriction() : null));
xml.end("DistributionSummary");
return xml.build();
@@ -2245,6 +2317,7 @@ public class CloudFrontController {
cfg.setAliases(parseAliases(body));
cfg.setViewerCertificate(parseViewerCertificate(body));
cfg.setCustomErrorResponses(parseCustomErrorResponses(body));
cfg.setGeoRestriction(parseGeoRestriction(body));
return cfg;
}
@@ -2378,6 +2451,19 @@ public class CloudFrontController {
return result;
}
private Map<String, Object> parseGeoRestriction(String body) {
List<Map<String, String>> groups = XmlParser.extractGroups(body, GEO_RESTRICTION);
if (groups.isEmpty()) {
return Map.of();
}
Map<String, Object> geoRestriction = new LinkedHashMap<>(groups.getFirst());
List<String> locations = XmlParser.extractAll(body, LOCATION);
if (!locations.isEmpty()) {
geoRestriction.put(LOCATIONS, locations);
}
return geoRestriction;
}
private List<Origin> parseOrigins(String body) {
List<Origin> result = new ArrayList<>();
if (body == null || body.isEmpty()) {