mirror of
https://github.com/floci-io/floci.git
synced 2026-12-02 01:43:02 -05:00
fix(cloudfront): carry origin connection settings through CloudFormation (#5992)
* fix(cloudfront): honor origin connection timeouts and attempts, answer 504 CloudFront ignored a custom origin's ConnectionTimeout, OriginReadTimeout and ConnectionAttempts and answered 502 for any origin failure. Apply the origin's connect and read timeouts to the origin request, retry a timed-out GET/HEAD up to ConnectionAttempts, and answer 504 with the AWS message when the last attempt times out. A refused connection or an unknown host still answers 502. Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com> * fix(cloudfront): retry a connect timeout for every origin method Review follow-ups on the origin timeout work: - Connection attempts count for every method, not only GET and HEAD: a connect timeout or a refused connection is retried up to the configured ConnectionAttempts whatever the method is. Apache HttpClient 5's ConnectTimeoutException extends SocketTimeoutException, so it is caught first to keep the split: a response timeout is still retried only for an idempotent method and still answers 504, while a refused connection propagates unchanged as a bad gateway. - The origin read and connection timeout bounds moved into CloudFrontOriginTimeouts, shared by CloudFrontController, CloudFrontServingController and CloudFrontOriginHttpClient instead of being repeated in each. - The slow-origin tests hold the origin open on a CountDownLatch with millisecond client timeouts rather than sleeping out a fixed delay, so an attempt ends by the client's own timeout and the suite finishes in seconds. Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com> * fix(cloudfront): carry origin connection settings through CloudFormation Follow-up to #5802: the serving layer now applies Origin.ConnectionAttempts, Origin.ConnectionTimeout and CustomOriginConfig.OriginReadTimeout, but cfnOrigins never copied them, so a template that set them was dropped and every distribution a stack provisioned ran on the AWS defaults. - cfnOrigins copies ConnectionAttempts and ConnectionTimeout from the origin and OriginReadTimeout from CustomOriginConfig. A non-integer origin setting in a template is now a ValidationError instead of the server error an uncaught NumberFormatException produced. - CreateDistribution and UpdateDistribution range-check ConnectionAttempts (1-3) and ConnectionTimeout (1-10 seconds) the way OriginReadTimeout and OriginKeepaliveTimeout already are. AWS publishes no dedicated error code for either, so both fail as InvalidArgument, and a non-numeric value is a 400 instead of being silently ignored. - The serving clamp reads MIN_CONNECTION_ATTEMPTS from CloudFrontOriginTimeouts instead of a literal. Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com> * fix(cloudfront): enforce origin connection setting ranges in the service Move the ConnectionAttempts, ConnectionTimeout, OriginReadTimeout and OriginKeepaliveTimeout range checks out of the XML parser into CloudFrontService.createDistribution/updateDistribution, next to the custom-header and OAC checks, so the CloudFormation path shares one copy. A template with an out-of-range value now fails the stack with the same InvalidArgument/InvalidOriginReadTimeout/ InvalidOriginKeepaliveTimeout error the API path returns, instead of falling back to the model defaults. The parser keeps rejecting non-numeric values; the cross-field ResponseCompletionTimeout check stays where it was. Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com> --------- Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
1 parent
935ebee2c4
commit
9a7c62b8f1
9 files changed
+365
-73
No files matched your search
+68
@@ -264,6 +264,74 @@ class CloudFormationCloudFrontDistributionIntegrationTest {
|
||||
distribution.getConfig().getCacheBehaviors().getFirst().getTrustedKeyGroups());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createStackWithAnOutOfRangeOriginConnectionSettingFailsTheStack() {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String stackName = "cfn-cloudfront-bad-attempts-" + suffix;
|
||||
|
||||
String template = """
|
||||
{
|
||||
"Resources": {
|
||||
"Dist": {
|
||||
"Type": "AWS::CloudFront::Distribution",
|
||||
"Properties": {
|
||||
"DistributionConfig": {
|
||||
"Enabled": true,
|
||||
"Origins": [{
|
||||
"Id": "o1",
|
||||
"DomainName": "origin.example.com",
|
||||
"ConnectionAttempts": 4,
|
||||
"CustomOriginConfig": {"OriginProtocolPolicy": "https-only"}
|
||||
}],
|
||||
"DefaultCacheBehavior": {
|
||||
"TargetOriginId": "o1",
|
||||
"ViewerProtocolPolicy": "allow-all"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "CreateStack")
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("TemplateBody", template)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
// On AWS the stack fails because CloudFront rejects the out-of-range value; the
|
||||
// service-level check makes the CloudFormation path share that one copy.
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DescribeStacks")
|
||||
.formParam("StackName", stackName)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<StackStatus>ROLLBACK_COMPLETE</StackStatus>"))
|
||||
.body(containsString("failed to create: [Dist]"));
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DescribeStackEvents")
|
||||
.formParam("StackName", stackName)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString(
|
||||
"The parameter ConnectionAttempts must be between 1 and 3."));
|
||||
}
|
||||
|
||||
private KeyGroup createKeyGroup(String name, String publicKeyId) {
|
||||
KeyGroup keyGroup = new KeyGroup();
|
||||
keyGroup.setName(name);
|
||||
|
||||
+44
@@ -169,9 +169,53 @@ class CloudFrontCfnProvisionerTest {
|
||||
assertEquals("b.s3.amazonaws.com", origin.getDomainName());
|
||||
// No CustomOriginConfig means an S3 origin.
|
||||
assertNull(origin.getCustomOriginConfig());
|
||||
// Settings the template never mentions stay on the model's own AWS defaults.
|
||||
assertEquals(3, origin.getConnectionAttempts());
|
||||
assertEquals(10, origin.getConnectionTimeout());
|
||||
assertEquals("redirect-to-https", config.getDefaultCacheBehavior().getViewerProtocolPolicy());
|
||||
}
|
||||
|
||||
@Test
|
||||
void copiesTheOriginConnectionSettingsTheTemplateDeclares() 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",
|
||||
"ConnectionAttempts": 2, "ConnectionTimeout": 5,
|
||||
"CustomOriginConfig": {"OriginProtocolPolicy": "https-only",
|
||||
"OriginReadTimeout": 45}}],
|
||||
"DefaultCacheBehavior": {"TargetOriginId": "o1", "ViewerProtocolPolicy": "redirect-to-https"}
|
||||
}}
|
||||
"""), distributionCtx(null));
|
||||
|
||||
ArgumentCaptor<Distribution> sent = ArgumentCaptor.forClass(Distribution.class);
|
||||
verify(cloudFront).createDistribution(sent.capture(), any());
|
||||
Origin origin = sent.getValue().getConfig().getOrigins().getFirst();
|
||||
assertEquals(2, origin.getConnectionAttempts());
|
||||
assertEquals(5, origin.getConnectionTimeout());
|
||||
// OriginReadTimeout lives on CustomOriginConfig, where the serving layer reads it.
|
||||
assertEquals(Integer.valueOf(45), origin.getCustomOriginConfig().get("OriginReadTimeout"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aNonNumericOriginConnectionSettingFailsTheStackInsteadOfAServerError() {
|
||||
StackResource r = resource(DISTRIBUTION);
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r, json("""
|
||||
{"DistributionConfig": {
|
||||
"Origins": [{"Id": "o1", "DomainName": "origin.example.com", "ConnectionTimeout": "lots"}],
|
||||
"DefaultCacheBehavior": {"TargetOriginId": "o1", "ViewerProtocolPolicy": "redirect-to-https"}
|
||||
}}
|
||||
"""), distributionCtx(null)));
|
||||
|
||||
assertEquals("ValidationError", e.getErrorCode());
|
||||
assertEquals("AWS::CloudFront::Distribution origin ConnectionTimeout must be an integer, but was 'lots'",
|
||||
e.getMessage());
|
||||
verify(cloudFront, never()).createDistribution(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updatesTheDistributionInPlaceUnderThePriorId() throws Exception {
|
||||
// provision() re-runs on every UpdateStack. The prior id comes from the context, not from
|
||||
|
||||
+16
-32
@@ -444,38 +444,6 @@ class CloudFrontControllerTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void originReadTimeoutAboveTheAwsMaximumIsRejected() {
|
||||
CloudFrontService service = mock(CloudFrontService.class);
|
||||
CloudFrontController controller = new CloudFrontController(service);
|
||||
|
||||
String body = distributionConfigBody("").replace(
|
||||
"<OriginProtocolPolicy>https-only</OriginProtocolPolicy></CustomOriginConfig>",
|
||||
"<OriginProtocolPolicy>https-only</OriginProtocolPolicy>"
|
||||
+ "<OriginReadTimeout>121</OriginReadTimeout></CustomOriginConfig>");
|
||||
|
||||
try (Response created = controller.createDistribution(null, body)) {
|
||||
assertEquals(400, created.getStatus());
|
||||
assertTrue(((String) created.getEntity()).contains("InvalidOriginReadTimeout"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void originKeepaliveTimeoutOfZeroIsRejected() {
|
||||
CloudFrontService service = mock(CloudFrontService.class);
|
||||
CloudFrontController controller = new CloudFrontController(service);
|
||||
|
||||
String body = distributionConfigBody("").replace(
|
||||
"<OriginProtocolPolicy>https-only</OriginProtocolPolicy></CustomOriginConfig>",
|
||||
"<OriginProtocolPolicy>https-only</OriginProtocolPolicy>"
|
||||
+ "<OriginKeepaliveTimeout>0</OriginKeepaliveTimeout></CustomOriginConfig>");
|
||||
|
||||
try (Response created = controller.createDistribution(null, body)) {
|
||||
assertEquals(400, created.getStatus());
|
||||
assertTrue(((String) created.getEntity()).contains("InvalidOriginKeepaliveTimeout"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void nonNumericOriginReadTimeoutIsRejected() {
|
||||
CloudFrontService service = mock(CloudFrontService.class);
|
||||
@@ -492,6 +460,22 @@ class CloudFrontControllerTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void nonNumericConnectionTimeoutIsRejected() {
|
||||
CloudFrontService service = mock(CloudFrontService.class);
|
||||
CloudFrontController controller = new CloudFrontController(service);
|
||||
|
||||
String body = distributionConfigBody("").replace(
|
||||
"<OriginProtocolPolicy>https-only</OriginProtocolPolicy></CustomOriginConfig>",
|
||||
"<OriginProtocolPolicy>https-only</OriginProtocolPolicy></CustomOriginConfig>"
|
||||
+ "<ConnectionTimeout>fast</ConnectionTimeout>");
|
||||
|
||||
try (Response created = controller.createDistribution(null, body)) {
|
||||
assertEquals(400, created.getStatus());
|
||||
assertTrue(((String) created.getEntity()).contains("InvalidArgument"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void responseCompletionTimeoutBelowOriginReadTimeoutIsRejected() {
|
||||
CloudFrontService service = mock(CloudFrontService.class);
|
||||
|
||||
+125
@@ -33,6 +33,7 @@ import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
@@ -710,6 +711,111 @@ class CloudFrontServiceTest {
|
||||
.getOriginAccessControlId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDistributionRejectsConnectionAttemptsOutsideTheAwsRange() {
|
||||
CloudFrontService service = serviceWithDomainSuffix("cloudfront.net");
|
||||
|
||||
AwsException above = assertAws("InvalidArgument",
|
||||
() -> service.createDistribution(distributionWithCustomOrigin(origin -> {
|
||||
origin.setConnectionAttempts(4);
|
||||
}), Map.of()));
|
||||
assertEquals("The parameter ConnectionAttempts must be between 1 and 3.", above.getMessage());
|
||||
assertEquals(400, above.getHttpStatus());
|
||||
|
||||
assertAws("InvalidArgument",
|
||||
() -> service.createDistribution(distributionWithCustomOrigin(origin -> {
|
||||
origin.setConnectionAttempts(0);
|
||||
}), Map.of()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDistributionRejectsConnectionTimeoutOutsideTheAwsRange() {
|
||||
CloudFrontService service = serviceWithDomainSuffix("cloudfront.net");
|
||||
|
||||
AwsException above = assertAws("InvalidArgument",
|
||||
() -> service.createDistribution(distributionWithCustomOrigin(origin -> {
|
||||
origin.setConnectionTimeout(11);
|
||||
}), Map.of()));
|
||||
assertEquals("The parameter ConnectionTimeout must be between 1 and 10 seconds.", above.getMessage());
|
||||
assertEquals(400, above.getHttpStatus());
|
||||
|
||||
AwsException below = assertAws("InvalidArgument",
|
||||
() -> service.createDistribution(distributionWithCustomOrigin(origin -> {
|
||||
origin.setConnectionTimeout(0);
|
||||
}), Map.of()));
|
||||
assertEquals("The parameter ConnectionTimeout must be between 1 and 10 seconds.", below.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDistributionRejectsOriginReadTimeoutOutsideTheAwsRange() {
|
||||
CloudFrontService service = serviceWithDomainSuffix("cloudfront.net");
|
||||
|
||||
AwsException error = assertAws("InvalidOriginReadTimeout",
|
||||
() -> service.createDistribution(distributionWithCustomOrigin(origin -> {
|
||||
origin.getCustomOriginConfig().put("OriginReadTimeout", 121);
|
||||
}), Map.of()));
|
||||
assertEquals("The parameter OriginReadTimeout must be between 1 and 120 seconds.", error.getMessage());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
|
||||
assertAws("InvalidOriginReadTimeout",
|
||||
() -> service.createDistribution(distributionWithCustomOrigin(origin -> {
|
||||
origin.getCustomOriginConfig().put("OriginReadTimeout", 0);
|
||||
}), Map.of()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDistributionRejectsOriginKeepaliveTimeoutOutsideTheAwsRange() {
|
||||
CloudFrontService service = serviceWithDomainSuffix("cloudfront.net");
|
||||
|
||||
AwsException below = assertAws("InvalidOriginKeepaliveTimeout",
|
||||
() -> service.createDistribution(distributionWithCustomOrigin(origin -> {
|
||||
origin.getCustomOriginConfig().put("OriginKeepaliveTimeout", 0);
|
||||
}), Map.of()));
|
||||
assertEquals("The parameter OriginKeepaliveTimeout must be between 1 and 300 seconds.", below.getMessage());
|
||||
assertEquals(400, below.getHttpStatus());
|
||||
|
||||
assertAws("InvalidOriginKeepaliveTimeout",
|
||||
() -> service.createDistribution(distributionWithCustomOrigin(origin -> {
|
||||
origin.getCustomOriginConfig().put("OriginKeepaliveTimeout", 301);
|
||||
}), Map.of()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateDistributionRejectsOriginSettingsOutsideTheAwsRange() {
|
||||
CloudFrontService service = serviceWithDomainSuffix("cloudfront.net");
|
||||
Distribution existing = service.createDistribution(
|
||||
distributionWithCustomOrigin(origin -> { }), Map.of());
|
||||
|
||||
AwsException error = assertAws("InvalidArgument", () -> service.updateDistribution(
|
||||
existing.getId(), existing.getEtag(),
|
||||
distributionWithCustomOrigin(origin -> origin.setConnectionAttempts(4))));
|
||||
assertEquals("The parameter ConnectionAttempts must be between 1 and 3.", error.getMessage());
|
||||
|
||||
assertAws("InvalidOriginReadTimeout", () -> service.updateDistribution(
|
||||
existing.getId(), existing.getEtag(),
|
||||
distributionWithCustomOrigin(origin ->
|
||||
origin.getCustomOriginConfig().put("OriginReadTimeout", 121))));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDistributionAcceptsOriginSettingsWithinTheAwsRange() {
|
||||
CloudFrontService service = serviceWithDomainSuffix("cloudfront.net");
|
||||
|
||||
Distribution created = assertDoesNotThrow(() -> service.createDistribution(
|
||||
distributionWithCustomOrigin(origin -> {
|
||||
origin.setConnectionAttempts(2);
|
||||
origin.setConnectionTimeout(5);
|
||||
origin.getCustomOriginConfig().put("OriginReadTimeout", 45);
|
||||
origin.getCustomOriginConfig().put("OriginKeepaliveTimeout", 300);
|
||||
}), Map.of()));
|
||||
|
||||
Origin origin = created.getConfig().getOrigins().getFirst();
|
||||
assertEquals(2, origin.getConnectionAttempts());
|
||||
assertEquals(5, origin.getConnectionTimeout());
|
||||
assertEquals(45, origin.getCustomOriginConfig().get("OriginReadTimeout"));
|
||||
assertEquals(300, origin.getCustomOriginConfig().get("OriginKeepaliveTimeout"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anOriginAccessControlInUseKeepsHowItSigns() {
|
||||
CloudFrontService service = serviceWithDomainSuffix("cloudfront.net");
|
||||
@@ -1495,6 +1601,25 @@ class CloudFrontServiceTest {
|
||||
return distribution;
|
||||
}
|
||||
|
||||
/** A distribution with one custom (HTTP/HTTPS) origin, customized through {@code originCustomizer}. */
|
||||
private static Distribution distributionWithCustomOrigin(Consumer<Origin> originCustomizer) {
|
||||
Origin origin = new Origin();
|
||||
origin.setId("o");
|
||||
origin.setDomainName("example.com");
|
||||
Map<String, Object> customOriginConfig = new LinkedHashMap<>();
|
||||
customOriginConfig.put("HTTPPort", "80");
|
||||
customOriginConfig.put("HTTPSPort", "443");
|
||||
customOriginConfig.put("OriginProtocolPolicy", "https-only");
|
||||
origin.setCustomOriginConfig(customOriginConfig);
|
||||
originCustomizer.accept(origin);
|
||||
DefaultCacheBehavior behavior = new DefaultCacheBehavior();
|
||||
behavior.setTargetOriginId("o");
|
||||
Distribution distribution = distribution(false, List.of());
|
||||
distribution.getConfig().setOrigins(List.of(origin));
|
||||
distribution.getConfig().setDefaultCacheBehavior(behavior);
|
||||
return distribution;
|
||||
}
|
||||
|
||||
private static void assertOriginAccessControlRefused(CloudFrontService service, String domainName, String protocol,
|
||||
String behavior, String originType, String reason) {
|
||||
Distribution distribution = distributionWithOrigin(domainName, oac(service, protocol, behavior, originType), null);
|
||||
|
||||
Reference in new issue
Block a user