mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudfront): persist and echo LambdaFunctionAssociations and FunctionAssociations (#2915)
* fix(cloudfront): persist and echo LambdaFunctionAssociations and FunctionAssociations CreateDistribution accepted LambdaFunctionAssociations and then silently dropped them: the parser never read the element and the serializer hardcoded Quantity 0. A distribution meant to run a viewer-request Lambda@Edge function ended up with no function attached and no error. FunctionAssociations (CloudFront Functions) had the same bug, so it is fixed in the same change. - parseDefaultCacheBehavior + parseCacheBehaviors: read LambdaFunctionARN, EventType, IncludeBody for Lambda and FunctionARN, EventType for Functions into the existing model fields. - xmlDefaultCacheBehavior + xmlCacheBehavior: replace the two hardcoded blocks with xmlLambdaFunctionAssociations / xmlFunctionAssociations, still emitting <Quantity>0</Quantity> when empty. - Validate EventType: Lambda allows all four event types, Functions only viewer-request and viewer-response; an empty ARN or an invalid EventType returns 400 InvalidArgument. Closes #2835 * fix(cloudfront): validate association Quantity and brace association parsers Address review feedback on the LambdaFunctionAssociations / FunctionAssociations round-trip: - The parsers now read the declared <Quantity> for each association block. A malformed value returns 400 InvalidArgument; a value that differs from the number of items returns 400 InconsistentQuantities, matching the existing TrustedKeyGroups handling, instead of being silently recomputed on read-back. An omitted Quantity is still accepted. - Add braces to every conditional in the new association parsing paths, per the repository convention in AGENTS.md.
This commit is contained in:
1 parent
5c71713cad
commit
3d3ab0023f
2 files changed
+417
-4
No files matched your search
+155
@@ -6,11 +6,14 @@ import io.github.hectorvent.floci.services.cloudfront.model.ContinuousDeployment
|
||||
import io.github.hectorvent.floci.services.cloudfront.model.Distribution;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -104,6 +107,158 @@ class CloudFrontControllerTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void distributionConfigRoundTripsLambdaAndCloudFrontFunctionAssociations() {
|
||||
CloudFrontService service = mock(CloudFrontService.class);
|
||||
CloudFrontController controller = new CloudFrontController(service);
|
||||
|
||||
String body = distributionConfigBody("""
|
||||
<LambdaFunctionAssociations><Quantity>1</Quantity><Items><LambdaFunctionAssociation>
|
||||
<LambdaFunctionARN>arn:aws:lambda:us-east-1:000000000000:function:edge-fn:1</LambdaFunctionARN>
|
||||
<EventType>viewer-request</EventType>
|
||||
<IncludeBody>false</IncludeBody>
|
||||
</LambdaFunctionAssociation></Items></LambdaFunctionAssociations>
|
||||
<FunctionAssociations><Quantity>1</Quantity><Items><FunctionAssociation>
|
||||
<FunctionARN>arn:aws:cloudfront::000000000000:function/cf-fn</FunctionARN>
|
||||
<EventType>viewer-response</EventType>
|
||||
</FunctionAssociation></Items></FunctionAssociations>
|
||||
""");
|
||||
|
||||
ArgumentCaptor<Distribution> captor = ArgumentCaptor.forClass(Distribution.class);
|
||||
when(service.createDistribution(captor.capture(), any())).thenAnswer(inv -> {
|
||||
Distribution d = inv.getArgument(0);
|
||||
d.setId("dist-1");
|
||||
d.setEtag("etag-1");
|
||||
return d;
|
||||
});
|
||||
|
||||
try (Response created = controller.createDistribution(null, body)) {
|
||||
assertEquals(201, created.getStatus());
|
||||
}
|
||||
|
||||
when(service.getDistribution("dist-1")).thenReturn(captor.getValue());
|
||||
|
||||
try (Response cfg = controller.getDistributionConfig("dist-1")) {
|
||||
String xml = (String) cfg.getEntity();
|
||||
|
||||
List<Map<String, String>> lambda =
|
||||
XmlParser.extractGroups(xml, "LambdaFunctionAssociation");
|
||||
assertEquals(1, lambda.size());
|
||||
assertEquals("arn:aws:lambda:us-east-1:000000000000:function:edge-fn:1",
|
||||
lambda.get(0).get("LambdaFunctionARN"));
|
||||
assertEquals("viewer-request", lambda.get(0).get("EventType"));
|
||||
assertEquals("false", lambda.get(0).get("IncludeBody"));
|
||||
|
||||
List<Map<String, String>> fn =
|
||||
XmlParser.extractGroups(xml, "FunctionAssociation");
|
||||
assertEquals(1, fn.size());
|
||||
assertEquals("arn:aws:cloudfront::000000000000:function/cf-fn",
|
||||
fn.get(0).get("FunctionARN"));
|
||||
assertEquals("viewer-response", fn.get(0).get("EventType"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void orderedCacheBehaviorRoundTripsLambdaFunctionAssociations() {
|
||||
CloudFrontService service = mock(CloudFrontService.class);
|
||||
CloudFrontController controller = new CloudFrontController(service);
|
||||
|
||||
String body = distributionConfigBody("").replace("</DefaultCacheBehavior>", """
|
||||
</DefaultCacheBehavior>
|
||||
<CacheBehaviors><Quantity>1</Quantity><Items><CacheBehavior>
|
||||
<PathPattern>/api/*</PathPattern>
|
||||
<TargetOriginId>o1</TargetOriginId>
|
||||
<ViewerProtocolPolicy>https-only</ViewerProtocolPolicy>
|
||||
<AllowedMethods><Quantity>2</Quantity><Items>
|
||||
<Method>GET</Method><Method>HEAD</Method></Items></AllowedMethods>
|
||||
<LambdaFunctionAssociations><Quantity>1</Quantity><Items><LambdaFunctionAssociation>
|
||||
<LambdaFunctionARN>arn:aws:lambda:us-east-1:000000000000:function:api-fn:7</LambdaFunctionARN>
|
||||
<EventType>origin-request</EventType>
|
||||
<IncludeBody>true</IncludeBody>
|
||||
</LambdaFunctionAssociation></Items></LambdaFunctionAssociations>
|
||||
</CacheBehavior></Items></CacheBehaviors>
|
||||
""");
|
||||
|
||||
ArgumentCaptor<Distribution> captor = ArgumentCaptor.forClass(Distribution.class);
|
||||
when(service.createDistribution(captor.capture(), any())).thenAnswer(inv -> {
|
||||
Distribution d = inv.getArgument(0);
|
||||
d.setId("dist-2");
|
||||
d.setEtag("etag-2");
|
||||
return d;
|
||||
});
|
||||
try (Response created = controller.createDistribution(null, body)) {
|
||||
assertEquals(201, created.getStatus());
|
||||
}
|
||||
when(service.getDistribution("dist-2")).thenReturn(captor.getValue());
|
||||
|
||||
try (Response cfg = controller.getDistributionConfig("dist-2")) {
|
||||
String xml = (String) cfg.getEntity();
|
||||
List<Map<String, String>> lambda =
|
||||
XmlParser.extractGroups(xml, "LambdaFunctionAssociation");
|
||||
assertEquals(1, lambda.size());
|
||||
assertEquals("origin-request", lambda.get(0).get("EventType"));
|
||||
assertEquals("true", lambda.get(0).get("IncludeBody"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidLambdaFunctionAssociationEventTypeIsRejected() {
|
||||
CloudFrontService service = mock(CloudFrontService.class);
|
||||
CloudFrontController controller = new CloudFrontController(service);
|
||||
|
||||
String body = distributionConfigBody("""
|
||||
<LambdaFunctionAssociations><Quantity>1</Quantity><Items><LambdaFunctionAssociation>
|
||||
<LambdaFunctionARN>arn:aws:lambda:us-east-1:000000000000:function:edge-fn:1</LambdaFunctionARN>
|
||||
<EventType>not-an-event</EventType>
|
||||
</LambdaFunctionAssociation></Items></LambdaFunctionAssociations>
|
||||
""");
|
||||
|
||||
try (Response created = controller.createDistribution(null, body)) {
|
||||
assertEquals(400, created.getStatus());
|
||||
assertTrue(((String) created.getEntity()).contains("InvalidArgument"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void lambdaFunctionAssociationsQuantityMismatchIsRejected() {
|
||||
CloudFrontService service = mock(CloudFrontService.class);
|
||||
CloudFrontController controller = new CloudFrontController(service);
|
||||
|
||||
String body = distributionConfigBody("""
|
||||
<LambdaFunctionAssociations><Quantity>2</Quantity><Items><LambdaFunctionAssociation>
|
||||
<LambdaFunctionARN>arn:aws:lambda:us-east-1:000000000000:function:edge-fn:1</LambdaFunctionARN>
|
||||
<EventType>viewer-request</EventType>
|
||||
</LambdaFunctionAssociation></Items></LambdaFunctionAssociations>
|
||||
""");
|
||||
|
||||
try (Response created = controller.createDistribution(null, body)) {
|
||||
assertEquals(400, created.getStatus());
|
||||
assertTrue(((String) created.getEntity()).contains("InconsistentQuantities"));
|
||||
}
|
||||
}
|
||||
|
||||
private static String distributionConfigBody(String defaultCacheBehaviorExtra) {
|
||||
return """
|
||||
<DistributionConfig xmlns="http://cloudfront.amazonaws.com/doc/2020-05-31/">
|
||||
<CallerReference>ref-1</CallerReference>
|
||||
<Enabled>true</Enabled>
|
||||
<Comment>probe</Comment>
|
||||
<Origins><Quantity>1</Quantity><Items><Origin>
|
||||
<Id>o1</Id><DomainName>example.com</DomainName>
|
||||
<CustomOriginConfig><HTTPPort>80</HTTPPort><HTTPSPort>443</HTTPSPort>
|
||||
<OriginProtocolPolicy>https-only</OriginProtocolPolicy></CustomOriginConfig>
|
||||
</Origin></Items></Origins>
|
||||
<DefaultCacheBehavior>
|
||||
<TargetOriginId>o1</TargetOriginId>
|
||||
<ViewerProtocolPolicy>redirect-to-https</ViewerProtocolPolicy>
|
||||
<AllowedMethods><Quantity>2</Quantity><Items>
|
||||
<Method>GET</Method><Method>HEAD</Method></Items></AllowedMethods>
|
||||
%s
|
||||
</DefaultCacheBehavior>
|
||||
</DistributionConfig>
|
||||
""".formatted(defaultCacheBehaviorExtra);
|
||||
}
|
||||
|
||||
private static Distribution distribution(String id) {
|
||||
Distribution distribution = new Distribution();
|
||||
distribution.setId(id);
|
||||
|
||||
Reference in new issue
Block a user