From 3641a074dc708606990b8c3e5fb4690cc0e813bc Mon Sep 17 00:00:00 2001 From: Priamo Germosen <6233424+pgermosen@users.noreply.github.com> Date: Tue, 15 Sep 2026 22:30:49 -0400 Subject: [PATCH] fix(cloudfront): dedupe CacheBehavior XML parsing (#3705) DefaultCacheBehavior and CacheBehavior each had their own ~250-line hand-written XML-parsing state machine for the same element shape, found via a CPD pass (#3652). Extracted the shared logic into one parseCacheBehaviorScope helper, used by both parsing call sites, with a small CacheBehaviorSettings interface so the helper can populate either model type. The duplication had already caused the exact bug it risked: the CacheBehavior copy never parsed RealtimeLogConfigArn, even though the field exists on both types in AWS's real CloudFront wire model and on the CacheBehavior model class itself. The response XML builders for both types also never emitted it. Fixed both and added a regression test. --- .../cloudfront/CloudFrontController.java | 816 ++++++------------ .../cloudfront/model/CacheBehavior.java | 2 +- .../model/CacheBehaviorSettings.java | 53 ++ .../model/DefaultCacheBehavior.java | 2 +- .../cloudfront/CloudFrontControllerTest.java | 39 + 5 files changed, 344 insertions(+), 568 deletions(-) create mode 100644 src/main/java/io/github/hectorvent/floci/services/cloudfront/model/CacheBehaviorSettings.java 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 4d6df061ba..b6cdf61f8c 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 @@ -12,6 +12,7 @@ import jakarta.ws.rs.core.Response; 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.util.ArrayList; @@ -2001,6 +2002,7 @@ public class CloudFrontController { .elem("ResponseHeadersPolicyId", dcb.getResponseHeadersPolicyId()) .elem("FieldLevelEncryptionId", dcb.getFieldLevelEncryptionId() != null ? dcb.getFieldLevelEncryptionId() : "") + .elem("RealtimeLogConfigArn", dcb.getRealtimeLogConfigArn()) .elem("Compress", dcb.isCompress()) .elem("SmoothStreaming", dcb.isSmoothStreaming()); @@ -2032,6 +2034,7 @@ public class CloudFrontController { .elem("ResponseHeadersPolicyId", cb.getResponseHeadersPolicyId()) .elem("FieldLevelEncryptionId", cb.getFieldLevelEncryptionId() != null ? cb.getFieldLevelEncryptionId() : "") + .elem("RealtimeLogConfigArn", cb.getRealtimeLogConfigArn()) .elem("Compress", cb.isCompress()) .elem("SmoothStreaming", cb.isSmoothStreaming()); @@ -3124,272 +3127,15 @@ public class CloudFrontController { } try { XMLStreamReader r = XmlParser.newStreamReader(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 inForwardedValues = false; - boolean inForwardedCookies = false; - boolean inForwardedCookieNames = false; - boolean inForwardedHeaders = false; - boolean inForwardedQueryStringCacheKeys = false; - boolean sawForwardedValues = false; - Boolean forwardedQueryString = null; - String forwardedCookiesForward = null; - List forwardedCookieNames = new ArrayList<>(); - List forwardedHeaders = new ArrayList<>(); - List forwardedQueryStringCacheKeys = new ArrayList<>(); - boolean inLambdaAssociations = false; - boolean inFunctionAssociations = false; - Map currentLambdaAssociation = null; - Map currentFunctionAssociation = null; - List> lambdaAssociations = new ArrayList<>(); - List> functionAssociations = new ArrayList<>(); - Integer lambdaAssociationsQuantity = null; - Integer functionAssociationsQuantity = null; - while (r.hasNext()) { int event = r.next(); - if (event == XMLStreamConstants.START_ELEMENT) { - String local = r.getLocalName(); - switch (local) { - case "DefaultCacheBehavior" -> inDcb = true; - case "LambdaFunctionAssociations" -> { - if (inDcb) { - inLambdaAssociations = true; - } - } - case "FunctionAssociations" -> { - if (inDcb) { - inFunctionAssociations = true; - } - } - case "LambdaFunctionAssociation" -> { - if (inLambdaAssociations) { - currentLambdaAssociation = new LinkedHashMap<>(); - } - } - case "FunctionAssociation" -> { - if (inFunctionAssociations) { - currentFunctionAssociation = new LinkedHashMap<>(); - } - } - case "LambdaFunctionARN" -> { - if (currentLambdaAssociation != null) { - currentLambdaAssociation.put("LambdaFunctionARN", r.getElementText()); - } - } - case "FunctionARN" -> { - if (currentFunctionAssociation != null) { - currentFunctionAssociation.put("FunctionARN", r.getElementText()); - } - } - case "IncludeBody" -> { - if (currentLambdaAssociation != null) { - currentLambdaAssociation.put( - "IncludeBody", "true".equalsIgnoreCase(r.getElementText())); - } - } - case "EventType" -> { - if (currentLambdaAssociation != null) { - currentLambdaAssociation.put("EventType", r.getElementText()); - } else if (currentFunctionAssociation != null) { - currentFunctionAssociation.put("EventType", r.getElementText()); - } - } - case "TrustedKeyGroups" -> { - if (inDcb) { - inTrustedKeyGroups = true; - sawTrustedKeyGroups = true; - } - } - case "Enabled" -> { - if (inTrustedKeyGroups) { - trustedKeyGroupsEnabled = - parseTrustedKeyGroupsEnabled(r.getElementText()); - } - } - case "Quantity" -> { - if (inTrustedKeyGroups) { - trustedKeyGroupsQuantity = - parseTrustedKeyGroupsQuantity(r.getElementText()); - } else if (inLambdaAssociations && currentLambdaAssociation == null) { - lambdaAssociationsQuantity = - parseAssociationsQuantity(r.getElementText()); - } else if (inFunctionAssociations && currentFunctionAssociation == null) { - functionAssociationsQuantity = - parseAssociationsQuantity(r.getElementText()); - } - } - case "KeyGroup" -> { - if (inTrustedKeyGroups) { - trustedKeyGroups.add(r.getElementText()); - } - } - case "AllowedMethods" -> { - if (inDcb) inAllowedMethods = true; - } - case "CachedMethods" -> { - if (inAllowedMethods) inCachedMethods = true; - } - case "ForwardedValues" -> { - if (inDcb) { - inForwardedValues = true; - sawForwardedValues = true; - } - } - case "Cookies" -> { - if (inForwardedValues) inForwardedCookies = true; - } - case "WhitelistedNames" -> { - if (inForwardedCookies) inForwardedCookieNames = true; - } - case "Headers" -> { - if (inForwardedValues) inForwardedHeaders = true; - } - case "QueryStringCacheKeys" -> { - if (inForwardedValues) inForwardedQueryStringCacheKeys = true; - } - case "Name" -> { - if (inForwardedCookieNames) { - forwardedCookieNames.add(r.getElementText()); - } else if (inForwardedHeaders) { - forwardedHeaders.add(r.getElementText()); - } else if (inForwardedQueryStringCacheKeys) { - forwardedQueryStringCacheKeys.add(r.getElementText()); - } - } - case "QueryString" -> { - if (inForwardedValues && !inForwardedCookies) { - forwardedQueryString = "true".equalsIgnoreCase(r.getElementText()); - } - } - case "Forward" -> { - if (inForwardedCookies) forwardedCookiesForward = r.getElementText(); - } - case "TargetOriginId" -> { - if (inDcb) dcb.setTargetOriginId(r.getElementText()); - } - case "ViewerProtocolPolicy" -> { - if (inDcb) dcb.setViewerProtocolPolicy(r.getElementText()); - } - case "CachePolicyId" -> { - if (inDcb) dcb.setCachePolicyId(r.getElementText()); - } - case "OriginRequestPolicyId" -> { - if (inDcb) dcb.setOriginRequestPolicyId(r.getElementText()); - } - case "ResponseHeadersPolicyId" -> { - if (inDcb) dcb.setResponseHeadersPolicyId(r.getElementText()); - } - case "FieldLevelEncryptionId" -> { - if (inDcb) dcb.setFieldLevelEncryptionId(r.getElementText()); - } - case "RealtimeLogConfigArn" -> { - if (inDcb) dcb.setRealtimeLogConfigArn(r.getElementText()); - } - case "Compress" -> { - if (inDcb) dcb.setCompress("true".equalsIgnoreCase(r.getElementText())); - } - case "SmoothStreaming" -> { - if (inDcb) dcb.setSmoothStreaming("true".equalsIgnoreCase(r.getElementText())); - } - case "MinTTL" -> { - if (inDcb) dcb.setMinTTL(parseLongOrZero(r.getElementText())); - } - case "DefaultTTL" -> { - if (inDcb) dcb.setDefaultTTL(parseLongOrZero(r.getElementText())); - } - case "MaxTTL" -> { - if (inDcb) dcb.setMaxTTL(parseLongOrZero(r.getElementText())); - } - case "Method" -> { - 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 "WhitelistedNames" -> inForwardedCookieNames = false; - case "Headers" -> inForwardedHeaders = false; - case "QueryStringCacheKeys" -> inForwardedQueryStringCacheKeys = false; - case "Cookies" -> inForwardedCookies = false; - case "ForwardedValues" -> inForwardedValues = false; - case "TrustedKeyGroups" -> inTrustedKeyGroups = false; - case "DefaultCacheBehavior" -> inDcb = false; - case "LambdaFunctionAssociations" -> inLambdaAssociations = false; - case "FunctionAssociations" -> inFunctionAssociations = false; - case "LambdaFunctionAssociation" -> { - if (currentLambdaAssociation != null) { - lambdaAssociations.add(currentLambdaAssociation); - currentLambdaAssociation = null; - } - } - case "FunctionAssociation" -> { - if (currentFunctionAssociation != null) { - functionAssociations.add(currentFunctionAssociation); - currentFunctionAssociation = null; - } - } - default -> { - } - } + if (event == XMLStreamConstants.START_ELEMENT + && "DefaultCacheBehavior".equals(r.getLocalName())) { + parseCacheBehaviorScope(r, "DefaultCacheBehavior", dcb); + break; } } r.close(); - if (!allowedMethods.isEmpty()) { - dcb.setAllowedMethods(allowedMethods); - } - if (!cachedMethods.isEmpty()) { - dcb.setCachedMethods(cachedMethods); - } - if (sawForwardedValues) { - dcb.setForwardedValues(forwardedValuesModel( - forwardedQueryString, - forwardedCookiesForward, - forwardedCookieNames, - forwardedHeaders, - forwardedQueryStringCacheKeys)); - } - if (!trustedKeyGroups.isEmpty()) { - dcb.setTrustedKeyGroups(trustedKeyGroups); - } - validateAssociationsQuantity(lambdaAssociationsQuantity, lambdaAssociations.size()); - validateAssociationsQuantity(functionAssociationsQuantity, functionAssociations.size()); - validateLambdaFunctionAssociations(lambdaAssociations); - validateFunctionAssociations(functionAssociations); - if (!lambdaAssociations.isEmpty()) { - dcb.setLambdaFunctionAssociations(lambdaAssociations); - } - if (!functionAssociations.isEmpty()) { - dcb.setFunctionAssociations(functionAssociations); - } - if (sawTrustedKeyGroups) { - if (trustedKeyGroupsEnabled == null) { - throw new AwsException( - "InvalidArgument", - "TrustedKeyGroups must include Enabled.", - 400); - } - validateTrustedKeyGroupsQuantity( - trustedKeyGroupsQuantity, trustedKeyGroups.size()); - validateEnabledTrustedKeyGroups( - trustedKeyGroupsEnabled, trustedKeyGroups.size()); - dcb.setTrustedKeyGroupsEnabled(trustedKeyGroupsEnabled); - } } catch (AwsException e) { throw e; } catch (Exception e) { @@ -3408,317 +3154,21 @@ public class CloudFrontController { try { XMLStreamReader r = XmlParser.newStreamReader(body); 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 inForwardedValues = false; - boolean inForwardedCookies = false; - boolean inForwardedCookieNames = false; - boolean inForwardedHeaders = false; - boolean inForwardedQueryStringCacheKeys = false; - boolean sawForwardedValues = false; - Boolean forwardedQueryString = null; - String forwardedCookiesForward = null; - List forwardedCookieNames = new ArrayList<>(); - List forwardedHeaders = new ArrayList<>(); - List forwardedQueryStringCacheKeys = new ArrayList<>(); - boolean inLambdaAssociations = false; - boolean inFunctionAssociations = false; - Map currentLambdaAssociation = null; - Map currentFunctionAssociation = null; - List> lambdaAssociations = new ArrayList<>(); - List> functionAssociations = new ArrayList<>(); - Integer lambdaAssociationsQuantity = null; - Integer functionAssociationsQuantity = null; while (r.hasNext()) { int event = r.next(); if (event == XMLStreamConstants.START_ELEMENT) { String local = r.getLocalName(); - switch (local) { - case "CacheBehaviors" -> inCacheBehaviors = true; - case "CacheBehavior" -> { - if (inCacheBehaviors) { - inCacheBehavior = true; - current = new CacheBehavior(); - sawTrustedKeyGroups = false; - trustedKeyGroupsEnabled = null; - trustedKeyGroupsQuantity = null; - allowedMethods = new ArrayList<>(); - cachedMethods = new ArrayList<>(); - trustedKeyGroups = new ArrayList<>(); - sawForwardedValues = false; - forwardedQueryString = null; - forwardedCookiesForward = null; - forwardedCookieNames = new ArrayList<>(); - forwardedHeaders = new ArrayList<>(); - forwardedQueryStringCacheKeys = new ArrayList<>(); - lambdaAssociations = new ArrayList<>(); - functionAssociations = new ArrayList<>(); - lambdaAssociationsQuantity = null; - functionAssociationsQuantity = null; - } - } - case "LambdaFunctionAssociations" -> { - if (inCacheBehavior) { - inLambdaAssociations = true; - } - } - case "FunctionAssociations" -> { - if (inCacheBehavior) { - inFunctionAssociations = true; - } - } - case "LambdaFunctionAssociation" -> { - if (inLambdaAssociations) { - currentLambdaAssociation = new LinkedHashMap<>(); - } - } - case "FunctionAssociation" -> { - if (inFunctionAssociations) { - currentFunctionAssociation = new LinkedHashMap<>(); - } - } - case "LambdaFunctionARN" -> { - if (currentLambdaAssociation != null) { - currentLambdaAssociation.put("LambdaFunctionARN", r.getElementText()); - } - } - case "FunctionARN" -> { - if (currentFunctionAssociation != null) { - currentFunctionAssociation.put("FunctionARN", r.getElementText()); - } - } - case "IncludeBody" -> { - if (currentLambdaAssociation != null) { - currentLambdaAssociation.put( - "IncludeBody", "true".equalsIgnoreCase(r.getElementText())); - } - } - case "EventType" -> { - if (currentLambdaAssociation != null) { - currentLambdaAssociation.put("EventType", r.getElementText()); - } else if (currentFunctionAssociation != null) { - currentFunctionAssociation.put("EventType", r.getElementText()); - } - } - case "TrustedKeyGroups" -> { - if (inCacheBehavior) { - inTrustedKeyGroups = true; - sawTrustedKeyGroups = true; - } - } - case "Enabled" -> { - if (inTrustedKeyGroups) { - trustedKeyGroupsEnabled = - parseTrustedKeyGroupsEnabled(r.getElementText()); - } - } - case "Quantity" -> { - if (inTrustedKeyGroups) { - trustedKeyGroupsQuantity = - parseTrustedKeyGroupsQuantity(r.getElementText()); - } else if (inLambdaAssociations && currentLambdaAssociation == null) { - lambdaAssociationsQuantity = - parseAssociationsQuantity(r.getElementText()); - } else if (inFunctionAssociations && currentFunctionAssociation == null) { - functionAssociationsQuantity = - parseAssociationsQuantity(r.getElementText()); - } - } - case "KeyGroup" -> { - if (inTrustedKeyGroups) { - trustedKeyGroups.add(r.getElementText()); - } - } - case "AllowedMethods" -> { - if (inCacheBehavior) inAllowedMethods = true; - } - case "CachedMethods" -> { - if (inAllowedMethods) inCachedMethods = true; - } - case "ForwardedValues" -> { - if (inCacheBehavior) { - inForwardedValues = true; - sawForwardedValues = true; - } - } - case "Cookies" -> { - if (inForwardedValues) inForwardedCookies = true; - } - case "WhitelistedNames" -> { - if (inForwardedCookies) inForwardedCookieNames = true; - } - case "Headers" -> { - if (inForwardedValues) inForwardedHeaders = true; - } - case "QueryStringCacheKeys" -> { - if (inForwardedValues) inForwardedQueryStringCacheKeys = true; - } - case "Name" -> { - if (inForwardedCookieNames) { - forwardedCookieNames.add(r.getElementText()); - } else if (inForwardedHeaders) { - forwardedHeaders.add(r.getElementText()); - } else if (inForwardedQueryStringCacheKeys) { - forwardedQueryStringCacheKeys.add(r.getElementText()); - } - } - case "QueryString" -> { - if (inForwardedValues && !inForwardedCookies) { - forwardedQueryString = "true".equalsIgnoreCase(r.getElementText()); - } - } - case "Forward" -> { - if (inForwardedCookies) forwardedCookiesForward = r.getElementText(); - } - case "PathPattern" -> { - if (inCacheBehavior && current != null) current.setPathPattern(r.getElementText()); - } - case "TargetOriginId" -> { - if (inCacheBehavior && current != null) current.setTargetOriginId(r.getElementText()); - } - case "ViewerProtocolPolicy" -> { - if (inCacheBehavior && current != null) current.setViewerProtocolPolicy(r.getElementText()); - } - case "CachePolicyId" -> { - if (inCacheBehavior && current != null) current.setCachePolicyId(r.getElementText()); - } - case "OriginRequestPolicyId" -> { - if (inCacheBehavior && current != null) - current.setOriginRequestPolicyId(r.getElementText()); - } - case "ResponseHeadersPolicyId" -> { - if (inCacheBehavior && current != null) - current.setResponseHeadersPolicyId(r.getElementText()); - } - case "FieldLevelEncryptionId" -> { - if (inCacheBehavior && current != null) - current.setFieldLevelEncryptionId(r.getElementText()); - } - case "Compress" -> { - if (inCacheBehavior && current != null) { - current.setCompress("true".equalsIgnoreCase(r.getElementText())); - } - } - case "SmoothStreaming" -> { - if (inCacheBehavior && current != null) { - current.setSmoothStreaming("true".equalsIgnoreCase(r.getElementText())); - } - } - case "MinTTL" -> { - if (inCacheBehavior && current != null) { - current.setMinTTL(parseLongOrZero(r.getElementText())); - } - } - case "DefaultTTL" -> { - if (inCacheBehavior && current != null) { - current.setDefaultTTL(parseLongOrZero(r.getElementText())); - } - } - case "MaxTTL" -> { - if (inCacheBehavior && current != null) { - current.setMaxTTL(parseLongOrZero(r.getElementText())); - } - } - case "Method" -> { - 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 "WhitelistedNames" -> inForwardedCookieNames = false; - case "Headers" -> inForwardedHeaders = false; - case "QueryStringCacheKeys" -> inForwardedQueryStringCacheKeys = false; - case "Cookies" -> inForwardedCookies = false; - case "ForwardedValues" -> inForwardedValues = false; - case "TrustedKeyGroups" -> inTrustedKeyGroups = false; - case "LambdaFunctionAssociations" -> inLambdaAssociations = false; - case "FunctionAssociations" -> inFunctionAssociations = false; - case "LambdaFunctionAssociation" -> { - if (currentLambdaAssociation != null) { - lambdaAssociations.add(currentLambdaAssociation); - currentLambdaAssociation = null; - } - } - case "FunctionAssociation" -> { - if (currentFunctionAssociation != null) { - functionAssociations.add(currentFunctionAssociation); - currentFunctionAssociation = null; - } - } - case "CacheBehavior" -> { - if (inCacheBehavior && current != null) { - if (!allowedMethods.isEmpty()) { - current.setAllowedMethods(allowedMethods); - } - if (!cachedMethods.isEmpty()) { - current.setCachedMethods(cachedMethods); - } - if (sawForwardedValues) { - current.setForwardedValues(forwardedValuesModel( - forwardedQueryString, - forwardedCookiesForward, - forwardedCookieNames, - forwardedHeaders, - forwardedQueryStringCacheKeys)); - } - if (!trustedKeyGroups.isEmpty()) { - current.setTrustedKeyGroups(trustedKeyGroups); - } - validateAssociationsQuantity( - lambdaAssociationsQuantity, lambdaAssociations.size()); - validateAssociationsQuantity( - functionAssociationsQuantity, functionAssociations.size()); - validateLambdaFunctionAssociations(lambdaAssociations); - validateFunctionAssociations(functionAssociations); - if (!lambdaAssociations.isEmpty()) { - current.setLambdaFunctionAssociations(lambdaAssociations); - } - if (!functionAssociations.isEmpty()) { - current.setFunctionAssociations(functionAssociations); - } - if (sawTrustedKeyGroups) { - if (trustedKeyGroupsEnabled == null) { - throw new AwsException( - "InvalidArgument", - "TrustedKeyGroups must include Enabled.", - 400); - } - validateTrustedKeyGroupsQuantity( - trustedKeyGroupsQuantity, - trustedKeyGroups.size()); - validateEnabledTrustedKeyGroups( - trustedKeyGroupsEnabled, - trustedKeyGroups.size()); - current.setTrustedKeyGroupsEnabled( - trustedKeyGroupsEnabled); - } - result.add(current); - } - inCacheBehavior = false; - current = null; - } - case "CacheBehaviors" -> inCacheBehaviors = false; - default -> { - } + if ("CacheBehaviors".equals(local)) { + inCacheBehaviors = true; + } else if ("CacheBehavior".equals(local) && inCacheBehaviors) { + CacheBehavior current = new CacheBehavior(); + parseCacheBehaviorScope(r, "CacheBehavior", current); + result.add(current); } + } else if (event == XMLStreamConstants.END_ELEMENT + && "CacheBehaviors".equals(r.getLocalName())) { + inCacheBehaviors = false; } } r.close(); @@ -3732,6 +3182,240 @@ public class CloudFrontController { return result; } + /** + * Parses one {@code DefaultCacheBehavior} or {@code CacheBehavior} element into + * {@code target}. The reader must have just produced the {@code START_ELEMENT} for + * {@code scopeElement}; this consumes everything up to and including its matching + * {@code END_ELEMENT}. + */ + private void parseCacheBehaviorScope( + XMLStreamReader r, String scopeElement, CacheBehaviorSettings target) + throws XMLStreamException { + 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 inForwardedValues = false; + boolean inForwardedCookies = false; + boolean inForwardedCookieNames = false; + boolean inForwardedHeaders = false; + boolean inForwardedQueryStringCacheKeys = false; + boolean sawForwardedValues = false; + Boolean forwardedQueryString = null; + String forwardedCookiesForward = null; + List forwardedCookieNames = new ArrayList<>(); + List forwardedHeaders = new ArrayList<>(); + List forwardedQueryStringCacheKeys = new ArrayList<>(); + boolean inLambdaAssociations = false; + boolean inFunctionAssociations = false; + Map currentLambdaAssociation = null; + Map currentFunctionAssociation = null; + List> lambdaAssociations = new ArrayList<>(); + List> functionAssociations = new ArrayList<>(); + Integer lambdaAssociationsQuantity = null; + Integer functionAssociationsQuantity = null; + + while (r.hasNext()) { + int event = r.next(); + if (event == XMLStreamConstants.START_ELEMENT) { + String local = r.getLocalName(); + switch (local) { + case "LambdaFunctionAssociations" -> inLambdaAssociations = true; + case "FunctionAssociations" -> inFunctionAssociations = true; + case "LambdaFunctionAssociation" -> { + if (inLambdaAssociations) { + currentLambdaAssociation = new LinkedHashMap<>(); + } + } + case "FunctionAssociation" -> { + if (inFunctionAssociations) { + currentFunctionAssociation = new LinkedHashMap<>(); + } + } + case "LambdaFunctionARN" -> { + if (currentLambdaAssociation != null) { + currentLambdaAssociation.put("LambdaFunctionARN", r.getElementText()); + } + } + case "FunctionARN" -> { + if (currentFunctionAssociation != null) { + currentFunctionAssociation.put("FunctionARN", r.getElementText()); + } + } + case "IncludeBody" -> { + if (currentLambdaAssociation != null) { + currentLambdaAssociation.put( + "IncludeBody", "true".equalsIgnoreCase(r.getElementText())); + } + } + case "EventType" -> { + if (currentLambdaAssociation != null) { + currentLambdaAssociation.put("EventType", r.getElementText()); + } else if (currentFunctionAssociation != null) { + currentFunctionAssociation.put("EventType", r.getElementText()); + } + } + case "TrustedKeyGroups" -> { + inTrustedKeyGroups = true; + sawTrustedKeyGroups = true; + } + case "Enabled" -> { + if (inTrustedKeyGroups) { + trustedKeyGroupsEnabled = parseTrustedKeyGroupsEnabled(r.getElementText()); + } + } + case "Quantity" -> { + if (inTrustedKeyGroups) { + trustedKeyGroupsQuantity = parseTrustedKeyGroupsQuantity(r.getElementText()); + } else if (inLambdaAssociations && currentLambdaAssociation == null) { + lambdaAssociationsQuantity = parseAssociationsQuantity(r.getElementText()); + } else if (inFunctionAssociations && currentFunctionAssociation == null) { + functionAssociationsQuantity = parseAssociationsQuantity(r.getElementText()); + } + } + case "KeyGroup" -> { + if (inTrustedKeyGroups) { + trustedKeyGroups.add(r.getElementText()); + } + } + case "AllowedMethods" -> inAllowedMethods = true; + case "CachedMethods" -> { + if (inAllowedMethods) inCachedMethods = true; + } + case "ForwardedValues" -> { + inForwardedValues = true; + sawForwardedValues = true; + } + case "Cookies" -> { + if (inForwardedValues) inForwardedCookies = true; + } + case "WhitelistedNames" -> { + if (inForwardedCookies) inForwardedCookieNames = true; + } + case "Headers" -> { + if (inForwardedValues) inForwardedHeaders = true; + } + case "QueryStringCacheKeys" -> { + if (inForwardedValues) inForwardedQueryStringCacheKeys = true; + } + case "Name" -> { + if (inForwardedCookieNames) { + forwardedCookieNames.add(r.getElementText()); + } else if (inForwardedHeaders) { + forwardedHeaders.add(r.getElementText()); + } else if (inForwardedQueryStringCacheKeys) { + forwardedQueryStringCacheKeys.add(r.getElementText()); + } + } + case "QueryString" -> { + if (inForwardedValues && !inForwardedCookies) { + forwardedQueryString = "true".equalsIgnoreCase(r.getElementText()); + } + } + case "Forward" -> { + if (inForwardedCookies) forwardedCookiesForward = r.getElementText(); + } + case "PathPattern" -> target.setPathPattern(r.getElementText()); + case "TargetOriginId" -> target.setTargetOriginId(r.getElementText()); + case "ViewerProtocolPolicy" -> target.setViewerProtocolPolicy(r.getElementText()); + case "CachePolicyId" -> target.setCachePolicyId(r.getElementText()); + case "OriginRequestPolicyId" -> target.setOriginRequestPolicyId(r.getElementText()); + case "ResponseHeadersPolicyId" -> target.setResponseHeadersPolicyId(r.getElementText()); + case "FieldLevelEncryptionId" -> target.setFieldLevelEncryptionId(r.getElementText()); + case "RealtimeLogConfigArn" -> target.setRealtimeLogConfigArn(r.getElementText()); + case "Compress" -> target.setCompress("true".equalsIgnoreCase(r.getElementText())); + case "SmoothStreaming" -> + target.setSmoothStreaming("true".equalsIgnoreCase(r.getElementText())); + case "MinTTL" -> target.setMinTTL(parseLongOrZero(r.getElementText())); + case "DefaultTTL" -> target.setDefaultTTL(parseLongOrZero(r.getElementText())); + case "MaxTTL" -> target.setMaxTTL(parseLongOrZero(r.getElementText())); + case "Method" -> { + if (inCachedMethods) { + cachedMethods.add(r.getElementText()); + } else if (inAllowedMethods) { + allowedMethods.add(r.getElementText()); + } + } + default -> { + } + } + } else if (event == XMLStreamConstants.END_ELEMENT) { + String local = r.getLocalName(); + if (scopeElement.equals(local)) { + break; + } + switch (local) { + case "CachedMethods" -> inCachedMethods = false; + case "AllowedMethods" -> inAllowedMethods = false; + case "WhitelistedNames" -> inForwardedCookieNames = false; + case "Headers" -> inForwardedHeaders = false; + case "QueryStringCacheKeys" -> inForwardedQueryStringCacheKeys = false; + case "Cookies" -> inForwardedCookies = false; + case "ForwardedValues" -> inForwardedValues = false; + case "TrustedKeyGroups" -> inTrustedKeyGroups = false; + case "LambdaFunctionAssociations" -> inLambdaAssociations = false; + case "FunctionAssociations" -> inFunctionAssociations = false; + case "LambdaFunctionAssociation" -> { + if (currentLambdaAssociation != null) { + lambdaAssociations.add(currentLambdaAssociation); + currentLambdaAssociation = null; + } + } + case "FunctionAssociation" -> { + if (currentFunctionAssociation != null) { + functionAssociations.add(currentFunctionAssociation); + currentFunctionAssociation = null; + } + } + default -> { + } + } + } + } + + if (!allowedMethods.isEmpty()) { + target.setAllowedMethods(allowedMethods); + } + if (!cachedMethods.isEmpty()) { + target.setCachedMethods(cachedMethods); + } + if (sawForwardedValues) { + target.setForwardedValues(forwardedValuesModel( + forwardedQueryString, + forwardedCookiesForward, + forwardedCookieNames, + forwardedHeaders, + forwardedQueryStringCacheKeys)); + } + if (!trustedKeyGroups.isEmpty()) { + target.setTrustedKeyGroups(trustedKeyGroups); + } + validateAssociationsQuantity(lambdaAssociationsQuantity, lambdaAssociations.size()); + validateAssociationsQuantity(functionAssociationsQuantity, functionAssociations.size()); + validateLambdaFunctionAssociations(lambdaAssociations); + validateFunctionAssociations(functionAssociations); + if (!lambdaAssociations.isEmpty()) { + target.setLambdaFunctionAssociations(lambdaAssociations); + } + if (!functionAssociations.isEmpty()) { + target.setFunctionAssociations(functionAssociations); + } + if (sawTrustedKeyGroups) { + if (trustedKeyGroupsEnabled == null) { + throw new AwsException( + "InvalidArgument", "TrustedKeyGroups must include Enabled.", 400); + } + validateTrustedKeyGroupsQuantity(trustedKeyGroupsQuantity, trustedKeyGroups.size()); + validateEnabledTrustedKeyGroups(trustedKeyGroupsEnabled, trustedKeyGroups.size()); + target.setTrustedKeyGroupsEnabled(trustedKeyGroupsEnabled); + } + } + private static boolean parseTrustedKeyGroupsEnabled(String value) { if (!"true".equalsIgnoreCase(value) && !"false".equalsIgnoreCase(value)) { diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudfront/model/CacheBehavior.java b/src/main/java/io/github/hectorvent/floci/services/cloudfront/model/CacheBehavior.java index 4834ab4ffc..5d15e30d43 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudfront/model/CacheBehavior.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudfront/model/CacheBehavior.java @@ -6,7 +6,7 @@ import java.util.List; import java.util.Map; @RegisterForReflection -public class CacheBehavior { +public class CacheBehavior implements CacheBehaviorSettings { private String pathPattern; private String targetOriginId; diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudfront/model/CacheBehaviorSettings.java b/src/main/java/io/github/hectorvent/floci/services/cloudfront/model/CacheBehaviorSettings.java new file mode 100644 index 0000000000..d2980140d4 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/cloudfront/model/CacheBehaviorSettings.java @@ -0,0 +1,53 @@ +package io.github.hectorvent.floci.services.cloudfront.model; + +import java.util.List; +import java.util.Map; + +/** + * The fields {@link CacheBehavior} and {@link DefaultCacheBehavior} share, letting XML-parsing + * code populate either one through a single code path. + */ +public interface CacheBehaviorSettings { + + void setTargetOriginId(String targetOriginId); + + void setViewerProtocolPolicy(String viewerProtocolPolicy); + + void setAllowedMethods(List allowedMethods); + + void setCachedMethods(List cachedMethods); + + void setCachePolicyId(String cachePolicyId); + + void setOriginRequestPolicyId(String originRequestPolicyId); + + void setResponseHeadersPolicyId(String responseHeadersPolicyId); + + void setFieldLevelEncryptionId(String fieldLevelEncryptionId); + + void setRealtimeLogConfigArn(String realtimeLogConfigArn); + + void setFunctionAssociations(List> functionAssociations); + + void setLambdaFunctionAssociations(List> lambdaFunctionAssociations); + + void setCompress(boolean compress); + + void setSmoothStreaming(boolean smoothStreaming); + + void setMinTTL(Long minTTL); + + void setDefaultTTL(Long defaultTTL); + + void setMaxTTL(Long maxTTL); + + void setForwardedValues(Map forwardedValues); + + void setTrustedKeyGroupsEnabled(boolean trustedKeyGroupsEnabled); + + void setTrustedKeyGroups(List trustedKeyGroups); + + default void setPathPattern(String pathPattern) { + // DefaultCacheBehavior has no PathPattern; only CacheBehavior overrides this. + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudfront/model/DefaultCacheBehavior.java b/src/main/java/io/github/hectorvent/floci/services/cloudfront/model/DefaultCacheBehavior.java index 8885fcd351..deaa8fa313 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudfront/model/DefaultCacheBehavior.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudfront/model/DefaultCacheBehavior.java @@ -6,7 +6,7 @@ import java.util.List; import java.util.Map; @RegisterForReflection -public class DefaultCacheBehavior { +public class DefaultCacheBehavior implements CacheBehaviorSettings { private String targetOriginId; private String viewerProtocolPolicy = "redirect-to-https"; 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 07a527ebc5..f35574f57e 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 @@ -285,6 +285,45 @@ class CloudFrontControllerTest { } } + @Test + void orderedCacheBehaviorRoundTripsRealtimeLogConfigArn() { + // A hand-written parser copy-pasted for CacheBehavior once silently dropped + // RealtimeLogConfigArn, since only the DefaultCacheBehavior copy captured it. + CloudFrontService service = mock(CloudFrontService.class); + CloudFrontController controller = new CloudFrontController(service); + + String body = distributionConfigBody("").replace("", """ + + 1 + /api/* + o1 + https-only + arn:aws:cloudfront::000000000000:realtime-log-config/rtlc-1 + + """); + + ArgumentCaptor captor = ArgumentCaptor.forClass(Distribution.class); + when(service.createDistribution(captor.capture(), any())).thenAnswer(inv -> { + Distribution d = inv.getArgument(0); + d.setId("dist-3"); + d.setEtag("etag-3"); + return d; + }); + try (Response created = controller.createDistribution(null, body)) { + assertEquals(201, created.getStatus()); + } + when(service.getDistribution("dist-3")).thenReturn(captor.getValue()); + + try (Response cfg = controller.getDistributionConfig("dist-3")) { + String xml = (String) cfg.getEntity(); + int start = xml.indexOf(""); + String cacheBehaviorsBlock = xml.substring(start); + assertEquals( + "arn:aws:cloudfront::000000000000:realtime-log-config/rtlc-1", + XmlParser.extractFirst(cacheBehaviorsBlock, "RealtimeLogConfigArn", null)); + } + } + @Test void invalidLambdaFunctionAssociationEventTypeIsRejected() { CloudFrontService service = mock(CloudFrontService.class);