diff --git a/docs/services/cloudfront.md b/docs/services/cloudfront.md index 530873fff3..5626e0935a 100644 --- a/docs/services/cloudfront.md +++ b/docs/services/cloudfront.md @@ -327,6 +327,48 @@ PUT, PATCH and DELETE). - `ViewerCertificate.ACMCertificateArn` must name an existing ACM certificate: distribution create and update reject an unknown one with `InvalidViewerCertificate`. ACM lists the distribution's ARN under `InUseBy`, and `DeleteCertificate` returns `ResourceInUseException` while a distribution references it. +- A `ViewerCertificate` that names a certificate is held to the two rules its own entry states. + The location is `ACMCertificateArn` or `IAMCertificateId` and "not both", and a certificate named + in either one "must also specify values for MinimumProtocolVersion and SSLSupportMethod". Both + refusals are `InvalidViewerCertificate` 400, which is Floci's reading rather than a published + answer: `InvalidArgument` and `InvalidMinimumProtocolVersion` are modelled on the distribution + writes as well, and no sentence says which one a caller gets, so both cases read as the + certificate the request carries not being valid. The Developer Guide page that enumerates what + `InvalidViewerCertificate` answers reaches neither case either: it names the certificate's own + trust, its chain, its format and the names it covers. A member carried blank counts as no value, the + way a blank location member names no certificate, and a distribution on the CloudFront domain + name needs neither member. The rules are read before the certificate is looked up, so a request + that breaks one is refused for the rule and not for a certificate nothing matches. +- The viewer certificate's two protocol members are held to the values they admit and to the one + pair they cannot carry together. A `MinimumProtocolVersion` outside `SSLv3`, `TLSv1`, + `TLSv1_2016`, `TLSv1.1_2016`, `TLSv1.2_2018`, `TLSv1.2_2019`, `TLSv1.2_2021`, `TLSv1.2_2025` and + `TLSv1.3_2025` is `InvalidMinimumProtocolVersion` 400, which is published rather than read: that + code's own sentence names this case. An `SSLSupportMethod` outside `sni-only`, `vip` and + `static-ip` has no code of its own and is `InvalidArgument` 400, what this service already + answers a member outside its shape with. `SSLv3` alongside `sni-only` is + `InvalidProtocolSettings` 400 on `CreateDistribution`, `CreateDistributionWithTags` and + `CopyDistribution`, whose sentence names that pair and nothing else and which is the same rule as + the floor the member states for SNI, "you must specify TLSv1 or higher". `UpdateDistribution` has + never declared that code and has declared `InvalidMinimumProtocolVersion` since 2015, so the + update answers the pair with the code it declares, which is the one a caller's SDK can map, and + keeps the pair's published sentence, so the two causes the update answers under one code stay + apart. The pair is + read for a distribution on a certificate of its own alone, since on the CloudFront certificate + the policy is replaced and the value that would make the pair is discarded. Both enumerations are + read either way. +- A distribution on the CloudFront domain name sets `CloudFrontDefaultCertificate` and is told to + "leave all other fields empty". The member is declared `boolean`, so it is held to the four forms + XML gives a boolean, `true`, `false`, `1` and `0`, and anything else is `InvalidArgument` 400. + That refusal is Floci's reading: CloudFront models no error for a malformed document, and reading + the member for the word `true` alone would let a request saying `yes` dodge every rule below. A + certificate named beside it, in `ACMCertificateArn` or `IAMCertificateId`, contradicts the + request's own claim and is `InvalidViewerCertificate` 400, Floci's reading again. A + `MinimumProtocolVersion` beside it is not refused: the member's entry says CloudFront + "automatically sets the security policy to TLSv1 regardless of the value that you set here", so + the policy a request asks for there is replaced and the config reads back `TLSv1`. A distribution + created with no `ViewerCertificate` at all reads the same way, the default certificate on `TLSv1`. + The `us-east-1` restriction AWS states for ACM certificates is not enforced: a certificate is + resolved from whatever region its ARN names, as long as it exists. - Up to 20 custom response headers policies can be created, and one policy can be associated with up to 100 distributions. - The five AWS managed response headers policy IDs are available and can be selected with diff --git a/src/main/java/io/floci/aws/services/cloudfront/CloudFrontController.java b/src/main/java/io/floci/aws/services/cloudfront/CloudFrontController.java index e98f1a00b1..8418ddaf19 100644 --- a/src/main/java/io/floci/aws/services/cloudfront/CloudFrontController.java +++ b/src/main/java/io/floci/aws/services/cloudfront/CloudFrontController.java @@ -2253,6 +2253,14 @@ public class CloudFrontController { return xml.end("ActiveTrustedKeyGroups").build(); } + /** + * A config carrying no viewer certificate reads as the certificate CloudFront serves its own + * domain name with, and the security policy that comes with it is + * {@code TLSv1}: {@code MinimumProtocolVersion}'s entry says CloudFront "automatically sets the + * security policy to TLSv1 regardless of the value that you set here" when + * {@code CloudFrontDefaultCertificate} is true, and {@link CloudFrontService} stores that same + * value for a request that sets the member itself. + */ private String xmlViewerCertificate(Map vc) { XmlBuilder xml = new XmlBuilder().start("ViewerCertificate"); if (vc != null && !vc.isEmpty()) { @@ -2261,7 +2269,8 @@ public class CloudFrontController { } } else { xml.elem("CloudFrontDefaultCertificate", "true") - .elem("MinimumProtocolVersion", "TLSv1.2_2021"); + .elem("MinimumProtocolVersion", + CloudFrontService.DEFAULT_CERTIFICATE_PROTOCOL_VERSION); } xml.end("ViewerCertificate"); return xml.build(); diff --git a/src/main/java/io/floci/aws/services/cloudfront/CloudFrontService.java b/src/main/java/io/floci/aws/services/cloudfront/CloudFrontService.java index 4e03a8cdbb..2970cdc078 100644 --- a/src/main/java/io/floci/aws/services/cloudfront/CloudFrontService.java +++ b/src/main/java/io/floci/aws/services/cloudfront/CloudFrontService.java @@ -75,6 +75,30 @@ public class CloudFrontService implements ServerCertificateReferenceProvider { private static final List OAC_SIGNING_BEHAVIORS = List.of("always-amz-auth", "never", "always", "no-override"); private static final Set FUNCTION_RUNTIMES = Set.of("cloudfront-js-1.0", "cloudfront-js-2.0"); + /** + * The security policies {@code MinimumProtocolVersion} admits, the enumeration its shape + * declares. {@code SSLv3} is the only one below {@code TLSv1}, which is why the floor the + * member states for {@code sni-only} and the pair {@code InvalidProtocolSettings} names are + * the same rule. + */ + private static final Set MINIMUM_PROTOCOL_VERSIONS = Set.of("SSLv3", "TLSv1", + "TLSv1_2016", "TLSv1.1_2016", "TLSv1.2_2018", "TLSv1.2_2019", "TLSv1.2_2021", + "TLSv1.2_2025", "TLSv1.3_2025"); + /** The values {@code SSLSupportMethod} admits, the enumeration its shape declares. */ + private static final Set SSL_SUPPORT_METHODS = Set.of("sni-only", "vip", "static-ip"); + /** + * The forms a member declared {@code boolean} can carry, XML giving such a value as + * {@code true}, {@code false}, {@code 1} or {@code 0}. + */ + private static final Map XML_BOOLEANS = + Map.of("true", true, "1", true, "false", false, "0", false); + /** + * The security policy a distribution on the CloudFront domain name ends up on, whatever it + * asked for: {@code MinimumProtocolVersion}'s own entry says that when + * {@code CloudFrontDefaultCertificate} is true CloudFront "automatically sets the security + * policy to TLSv1 regardless of the value that you set here". + */ + static final String DEFAULT_CERTIFICATE_PROTOCOL_VERSION = "TLSv1"; private static final int MAX_ORIGIN_CUSTOM_HEADERS = 30; private static final int MAX_CUSTOM_HEADER_NAME_LENGTH = 256; private static final int MAX_CUSTOM_HEADER_VALUE_LENGTH = 1_783; @@ -213,6 +237,8 @@ public class CloudFrontService implements ServerCertificateReferenceProvider { validateResponseHeadersPolicyReferences(dist.getConfig(), null); validateTrustedKeyGroups(dist.getConfig()); validateOriginAccessControls(dist.getConfig()); + validateViewerCertificateSettings(dist.getConfig(), DistributionWrite.CREATE); + applyDefaultCertificateProtocolVersion(dist.getConfig()); return whileCertificatesHeld(dist.getConfig(), () -> { validateViewerCertificate(dist.getConfig()); String id = generateDistributionId(); @@ -248,6 +274,8 @@ public class CloudFrontService implements ServerCertificateReferenceProvider { validateResponseHeadersPolicyReferences(updated.getConfig(), id); validateTrustedKeyGroups(updated.getConfig()); validateOriginAccessControls(updated.getConfig()); + validateViewerCertificateSettings(updated.getConfig(), DistributionWrite.UPDATE); + applyDefaultCertificateProtocolVersion(updated.getConfig()); return whileCertificatesHeld(updated.getConfig(), () -> { validateViewerCertificate(updated.getConfig()); updated.setId(id); @@ -291,11 +319,19 @@ public class CloudFrontService implements ServerCertificateReferenceProvider { } private static String acmCertificateArn(DistributionConfig config) { + return viewerCertificateMember(config, "ACMCertificateArn"); + } + + /** + * A member of the viewer certificate, or null when it is absent or blank. An element left empty + * reaches this protocol as a blank value, so a member carried blank carries no value. + */ + private static String viewerCertificateMember(DistributionConfig config, String member) { if (config == null || config.getViewerCertificate() == null) { return null; } - String certificateArn = config.getViewerCertificate().get("ACMCertificateArn"); - return certificateArn == null || certificateArn.isBlank() ? null : certificateArn; + String value = config.getViewerCertificate().get(member); + return value == null || value.isBlank() ? null : value; } /** @@ -306,11 +342,155 @@ public class CloudFrontService implements ServerCertificateReferenceProvider { * Server Certificates in {@code docs/services/iam.md}. */ private static String iamCertificateId(DistributionConfig config) { - if (config == null || config.getViewerCertificate() == null) { - return null; + return viewerCertificateMember(config, "IAMCertificateId"); + } + + /** + * Whether the request claims the certificate CloudFront serves its own domain name with. The + * member is declared {@code boolean} in the model and reaches this protocol as text, so it is + * held to the four forms XML gives a boolean, {@code true}, {@code false}, {@code 1} and + * {@code 0}, rather than read for the word {@code true} alone. Anything else answers the + * {@code InvalidArgument} this service answers a member outside its shape with, which is + * Floci's reading: CloudFront models no error for a malformed document, and taking the value + * for false instead would let a request dodge every rule this member governs, the refusals + * beside a named certificate and the security policy it is overridden to alike. An absent + * member, or one carried blank, leaves the distribution on a certificate of its own. + */ + private static boolean cloudFrontDefaultCertificate(DistributionConfig config) { + String claimed = viewerCertificateMember(config, "CloudFrontDefaultCertificate"); + if (claimed == null) { + return false; } - String certificateId = config.getViewerCertificate().get("IAMCertificateId"); - return certificateId == null || certificateId.isBlank() ? null : certificateId; + Boolean parsed = XML_BOOLEANS.get(claimed.strip().toLowerCase(Locale.ROOT)); + if (parsed == null) { + throw new AwsException("InvalidArgument", + "The CloudFrontDefaultCertificate specified is not valid.", 400); + } + return parsed; + } + + /** + * Which distribution write is being validated. The two differ in one answer, the code the + * {@code SSLv3} with {@code sni-only} pair is refused with, which is the code the operation + * declares: {@code CreateDistribution}, {@code CreateDistributionWithTags} and + * {@code CopyDistribution} declare {@code InvalidProtocolSettings}, where + * {@code UpdateDistribution} has never declared it in any version of this API and has declared + * {@code InvalidMinimumProtocolVersion} since 2015. Where no sentence settles which a caller + * gets, the declared code is what a typed SDK can map: an undeclared one reaches such a caller + * as a generic error, its deserializer carrying no case for it. + */ + private enum DistributionWrite { + CREATE("InvalidProtocolSettings"), + UPDATE("InvalidMinimumProtocolVersion"); + + private final String protocolPairCode; + + DistributionWrite(String protocolPairCode) { + this.protocolPairCode = protocolPairCode; + } + } + + /** + * The two enumerations the viewer certificate declares, and the one pair it admits apart but + * not together. A {@code MinimumProtocolVersion} outside its set is published: + * {@code InvalidMinimumProtocolVersion}, "The minimum protocol version specified is not + * valid.", is modelled on both distribution writes and its own sentence names this case. An + * {@code SSLSupportMethod} outside its set has no code of its own, so it reads here as the + * {@code InvalidArgument} this service already answers a member outside its shape with. + * {@code SSLv3} alongside {@code sni-only} is published as well, by + * {@code InvalidProtocolSettings}, whose sentence names that pair and nothing else, and it is + * the same rule as the floor {@code MinimumProtocolVersion} states for SNI, "you must specify + * TLSv1 or higher". That code is declared on the two creates and the copy and never on the + * update, so the code this refusal answers is the write's own, as {@link DistributionWrite} + * sets out. The sentence stays the pair's published one either way, which keeps the two causes + * the update answers under one code apart. The pair is read for a distribution on a certificate + * of its own alone: on the CloudFront certificate the security policy is replaced with + * {@code TLSv1} "regardless of the value that you set here", so the value that would make the + * pair is discarded and refusing the request for it would contradict the override. Both + * enumerations are read either way, a member outside the set its shape declares being outside + * its shape whatever the distribution is on. + */ + private static void validateViewerCertificateProtocols(DistributionConfig config, + boolean defaultCertificate, + DistributionWrite write) { + String protocolVersion = viewerCertificateMember(config, "MinimumProtocolVersion"); + if (protocolVersion != null && !MINIMUM_PROTOCOL_VERSIONS.contains(protocolVersion)) { + throw new AwsException("InvalidMinimumProtocolVersion", + "The minimum protocol version specified is not valid.", 400); + } + String sslSupportMethod = viewerCertificateMember(config, "SSLSupportMethod"); + if (sslSupportMethod != null && !SSL_SUPPORT_METHODS.contains(sslSupportMethod)) { + throw new AwsException("InvalidArgument", + "The SSLSupportMethod specified is not valid.", 400); + } + if (!defaultCertificate && "SSLv3".equals(protocolVersion) + && "sni-only".equals(sslSupportMethod)) { + throw new AwsException(write.protocolPairCode, + "You cannot specify SSLv3 as the minimum protocol version if you only want to " + + "support only clients that support Server Name Indication (SNI).", 400); + } + } + + /** + * The rules the viewer certificate's own entry states about the certificate it names, and + * nothing else here holds a caller to. Its location is set "in one of the following fields + * (not both)", {@code ACMCertificateArn} or {@code IAMCertificateId}; a certificate named in + * either one "must also specify values for MinimumProtocolVersion and SSLSupportMethod"; and a + * request that sets {@code CloudFrontDefaultCertificate} is told to "leave all other fields + * empty", so a certificate named beside it contradicts the request's own claim. None of the + * three refusals has a published code: {@code InvalidArgument} and + * {@code InvalidMinimumProtocolVersion} are modelled on the distribution writes as well and no + * sentence says which a caller gets, and the Developer Guide page enumerating what + * {@code InvalidViewerCertificate} answers reaches none of these cases. So all three read here + * as the certificate the request carries not being valid, which is what + * {@code InvalidViewerCertificate} is documented for. The readings are set out in + * {@code docs/services/cloudfront.md}. The members are held to their shape first, the claim + * itself and then the protocol members, and all of it runs before the certificate is looked + * up, so a request breaking a rule is refused for the rule rather than for a certificate + * nothing matches. + */ + private static void validateViewerCertificateSettings(DistributionConfig config, + DistributionWrite write) { + boolean defaultCertificate = cloudFrontDefaultCertificate(config); + validateViewerCertificateProtocols(config, defaultCertificate, write); + String certificateArn = acmCertificateArn(config); + String certificateId = iamCertificateId(config); + if (certificateArn != null && certificateId != null) { + throw new AwsException("InvalidViewerCertificate", + "You must specify a value for ACMCertificateArn or IAMCertificateId, not both.", 400); + } + if (certificateArn == null && certificateId == null) { + return; + } + if (defaultCertificate) { + throw new AwsException("InvalidViewerCertificate", + "You cannot specify ACMCertificateArn or IAMCertificateId when " + + "CloudFrontDefaultCertificate is true.", 400); + } + if (viewerCertificateMember(config, "MinimumProtocolVersion") == null + || viewerCertificateMember(config, "SSLSupportMethod") == null) { + throw new AwsException("InvalidViewerCertificate", + "You must specify values for MinimumProtocolVersion and SSLSupportMethod.", 400); + } + } + + /** + * The one rule in this group that replaces a value instead of refusing the request. + * {@code MinimumProtocolVersion}'s entry says that when {@code CloudFrontDefaultCertificate} is + * true CloudFront "automatically sets the security policy to TLSv1 regardless of the value that + * you set here", so the policy a request asks for there is overwritten and the config reads + * back the one the distribution is really on. The parsed map is replaced rather than written + * to, a caller inside this JVM being free to hand in one that cannot be. + */ + private static void applyDefaultCertificateProtocolVersion(DistributionConfig config) { + if (config == null || !cloudFrontDefaultCertificate(config) + || DEFAULT_CERTIFICATE_PROTOCOL_VERSION.equals( + config.getViewerCertificate().get("MinimumProtocolVersion"))) { + return; + } + Map viewerCertificate = new LinkedHashMap<>(config.getViewerCertificate()); + viewerCertificate.put("MinimumProtocolVersion", DEFAULT_CERTIFICATE_PROTOCOL_VERSION); + config.setViewerCertificate(viewerCertificate); } private void validateViewerCertificate(DistributionConfig config) { diff --git a/src/test/java/io/floci/aws/services/cloudfront/CloudFrontCertificateDeleteRaceTest.java b/src/test/java/io/floci/aws/services/cloudfront/CloudFrontCertificateDeleteRaceTest.java index 8143c3b25f..402664c075 100644 --- a/src/test/java/io/floci/aws/services/cloudfront/CloudFrontCertificateDeleteRaceTest.java +++ b/src/test/java/io/floci/aws/services/cloudfront/CloudFrontCertificateDeleteRaceTest.java @@ -82,6 +82,7 @@ class CloudFrontCertificateDeleteRaceTest { Map viewerCertificate = new LinkedHashMap<>(); viewerCertificate.put("IAMCertificateId", certificateId); viewerCertificate.put("SSLSupportMethod", "sni-only"); + viewerCertificate.put("MinimumProtocolVersion", "TLSv1.2_2021"); config.setViewerCertificate(viewerCertificate); Distribution distribution = new Distribution(); diff --git a/src/test/java/io/floci/aws/services/cloudfront/CloudFrontViewerCertificateTest.java b/src/test/java/io/floci/aws/services/cloudfront/CloudFrontViewerCertificateTest.java index cfec8ffbc6..0a0449b75d 100644 --- a/src/test/java/io/floci/aws/services/cloudfront/CloudFrontViewerCertificateTest.java +++ b/src/test/java/io/floci/aws/services/cloudfront/CloudFrontViewerCertificateTest.java @@ -15,6 +15,8 @@ import io.quarkus.test.junit.QuarkusTest; import io.restassured.http.ContentType; import io.restassured.response.ValidatableResponse; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import java.util.LinkedHashMap; import java.util.List; @@ -86,6 +88,35 @@ class CloudFrontViewerCertificateTest { } private Distribution distributionWithViewerCertificate(String member, String certificate) { + return distributionWithViewerCertificate(completeViewerCertificate(member, certificate)); + } + + /** + * A certificate named in the location member handed in, carrying the two members AWS requires + * alongside one: a certificate named without them is refused. + */ + private static Map completeViewerCertificate(String member, String certificate) { + Map viewerCertificate = new LinkedHashMap<>(); + viewerCertificate.put(member, certificate); + viewerCertificate.put("SSLSupportMethod", "sni-only"); + viewerCertificate.put("MinimumProtocolVersion", "TLSv1.2_2021"); + return viewerCertificate; + } + + /** + * A viewer certificate for a distribution on the CloudFront domain name, asking for the + * security policy handed in, or for none when that is null. + */ + private static Map defaultViewerCertificate(String protocolVersion) { + Map viewerCertificate = new LinkedHashMap<>(); + viewerCertificate.put("CloudFrontDefaultCertificate", "true"); + if (protocolVersion != null) { + viewerCertificate.put("MinimumProtocolVersion", protocolVersion); + } + return viewerCertificate; + } + + private Distribution distributionWithViewerCertificate(Map viewerCertificate) { Origin origin = new Origin(); origin.setId("cert-origin"); origin.setDomainName("example.com"); @@ -104,9 +135,6 @@ class CloudFrontViewerCertificateTest { config.setComment("viewer-certificate-test"); config.setOrigins(List.of(origin)); config.setDefaultCacheBehavior(behavior); - Map viewerCertificate = new LinkedHashMap<>(); - viewerCertificate.put(member, certificate); - viewerCertificate.put("SSLSupportMethod", "sni-only"); config.setViewerCertificate(viewerCertificate); Distribution distribution = new Distribution(); @@ -280,4 +308,593 @@ class CloudFrontViewerCertificateTest { deleteServerCertificate(name).statusCode(200); } + + /** + * A certificate named in the viewer certificate "must also specify values for + * MinimumProtocolVersion and SSLSupportMethod". The id here matches nothing, so the message + * says which refusal the request got: the rule is read before the certificate is looked up. + */ + @ParameterizedTest + @ValueSource(strings = {"MinimumProtocolVersion", "SSLSupportMethod"}) + void createDistributionRefusesACertificateWithoutTheMembersItRequires(String missing) { + Map viewerCertificate = + completeViewerCertificate("IAMCertificateId", "ASCAMISSING"); + viewerCertificate.remove(missing); + + AwsException rejected = assertThrows(AwsException.class, () -> cloudFrontService + .createDistribution(distributionWithViewerCertificate(viewerCertificate), Map.of())); + + assertEquals("InvalidViewerCertificate", rejected.getErrorCode()); + assertEquals(400, rejected.getHttpStatus()); + assertEquals("You must specify values for MinimumProtocolVersion and SSLSupportMethod.", + rejected.getMessage()); + } + + /** + * An element left empty reaches this protocol as a blank value, and a blank is no value. The + * certificate exists here, so what refuses the request is the rule rather than the lookup. + */ + @ParameterizedTest + @ValueSource(strings = {"MinimumProtocolVersion", "SSLSupportMethod"}) + void aBlankMemberIsNoValueAlongsideACertificate(String blanked) { + String certificateArn = importAcmCertificate().getArn(); + Map viewerCertificate = + completeViewerCertificate("ACMCertificateArn", certificateArn); + viewerCertificate.put(blanked, ""); + + AwsException rejected = assertThrows(AwsException.class, () -> cloudFrontService + .createDistribution(distributionWithViewerCertificate(viewerCertificate), Map.of())); + + assertEquals("InvalidViewerCertificate", rejected.getErrorCode()); + assertEquals("You must specify values for MinimumProtocolVersion and SSLSupportMethod.", + rejected.getMessage()); + + acmService.deleteCertificate(certificateArn, ACM_REGION); + } + + /** + * The certificate's location is one field and not both, so a request naming an ACM certificate + * and an IAM certificate is refused although each of them exists. + */ + @Test + void createDistributionRefusesBothCertificateLocationsAtOnce() { + String name = "cf-cert-both-" + suffix(); + ServerCertificate certificate = uploadCertificate(name); + String certificateArn = importAcmCertificate().getArn(); + Map viewerCertificate = + completeViewerCertificate("ACMCertificateArn", certificateArn); + viewerCertificate.put("IAMCertificateId", certificate.getServerCertificateId()); + + AwsException rejected = assertThrows(AwsException.class, () -> cloudFrontService + .createDistribution(distributionWithViewerCertificate(viewerCertificate), Map.of())); + + assertEquals("InvalidViewerCertificate", rejected.getErrorCode()); + assertEquals(400, rejected.getHttpStatus()); + assertEquals("You must specify a value for ACMCertificateArn or IAMCertificateId, not both.", + rejected.getMessage()); + + deleteServerCertificate(name).statusCode(200); + acmService.deleteCertificate(certificateArn, ACM_REGION); + } + + /** Both rules are read on the update too, and a refused update leaves the distribution as it was. */ + @Test + void updateDistributionRefusesACertificateWithoutTheMembersItRequires() { + String certificateArn = importAcmCertificate().getArn(); + Distribution created = cloudFrontService.createDistribution( + distributionWithViewerCertificate("ACMCertificateArn", certificateArn), Map.of()); + Map viewerCertificate = + completeViewerCertificate("ACMCertificateArn", certificateArn); + viewerCertificate.remove("SSLSupportMethod"); + + AwsException rejected = assertThrows(AwsException.class, + () -> cloudFrontService.updateDistribution(created.getId(), created.getEtag(), + distributionWithViewerCertificate(viewerCertificate))); + + assertEquals("InvalidViewerCertificate", rejected.getErrorCode()); + assertEquals("sni-only", cloudFrontService.getDistribution(created.getId()) + .getConfig().getViewerCertificate().get("SSLSupportMethod")); + + cloudFrontService.removeDistribution(created.getId()); + acmService.deleteCertificate(certificateArn, ACM_REGION); + } + + @Test + void updateDistributionRefusesBothCertificateLocationsAtOnce() { + String name = "cf-cert-update-both-" + suffix(); + ServerCertificate certificate = uploadCertificate(name); + Distribution created = cloudFrontService.createDistribution( + distributionWithCertificate(certificate.getServerCertificateId()), Map.of()); + String certificateArn = importAcmCertificate().getArn(); + Map viewerCertificate = + completeViewerCertificate("IAMCertificateId", certificate.getServerCertificateId()); + viewerCertificate.put("ACMCertificateArn", certificateArn); + + AwsException rejected = assertThrows(AwsException.class, + () -> cloudFrontService.updateDistribution(created.getId(), created.getEtag(), + distributionWithViewerCertificate(viewerCertificate))); + + assertEquals("InvalidViewerCertificate", rejected.getErrorCode()); + assertEquals("You must specify a value for ACMCertificateArn or IAMCertificateId, not both.", + rejected.getMessage()); + + cloudFrontService.removeDistribution(created.getId()); + deleteServerCertificate(name).statusCode(200); + acmService.deleteCertificate(certificateArn, ACM_REGION); + } + + /** The refusal reaches the wire as the REST XML error a distribution write answers with. */ + @Test + void theRefusalReachesTheWireAsAnInvalidViewerCertificate() { + String body = """ + + + incomplete-viewer-cert-%s + + 1 + + + incomplete-cert-origin + example.com + + 80 + 443 + http-only + + + + + + incomplete-cert-origin + allow-all + + + arn:aws:acm:us-east-1:000000000000:certificate/%s + sni-only + + incomplete-viewer-certificate + false + + """.formatted(suffix(), UUID.randomUUID()); + + given() + .contentType(ContentType.XML) + .body(body) + .when() + .post("/2020-05-31/distribution") + .then() + .statusCode(400) + .body("ErrorResponse.Error.Code", equalTo("InvalidViewerCertificate")) + .body("ErrorResponse.Error.Message", + equalTo("You must specify values for MinimumProtocolVersion and SSLSupportMethod.")); + } + + /** + * A distribution on the CloudFront domain name sets {@code CloudFrontDefaultCertificate} and + * leaves the other fields empty, so it names no certificate and neither rule reaches it. + */ + @Test + void aDistributionOnTheCloudFrontDomainNameNeedsNeitherMember() { + Distribution created = cloudFrontService.createDistribution( + distributionWithViewerCertificate(defaultViewerCertificate(null)), Map.of()); + + assertEquals("true", cloudFrontService.getDistribution(created.getId()) + .getConfig().getViewerCertificate().get("CloudFrontDefaultCertificate")); + + cloudFrontService.removeDistribution(created.getId()); + } + + /** + * {@code MinimumProtocolVersion} admits an enumeration, and a value outside it is what + * {@code InvalidMinimumProtocolVersion} is documented for. The certificate named here matches + * nothing, so what refuses the request is the member's shape rather than the lookup. + */ + @Test + void createDistributionRefusesASecurityPolicyOutsideTheEnumeration() { + Map viewerCertificate = + completeViewerCertificate("IAMCertificateId", "ASCAMISSING"); + viewerCertificate.put("MinimumProtocolVersion", "TLSv1.2_2020"); + + AwsException rejected = assertThrows(AwsException.class, () -> cloudFrontService + .createDistribution(distributionWithViewerCertificate(viewerCertificate), Map.of())); + + assertEquals("InvalidMinimumProtocolVersion", rejected.getErrorCode()); + assertEquals(400, rejected.getHttpStatus()); + assertEquals("The minimum protocol version specified is not valid.", rejected.getMessage()); + } + + /** + * Every security policy the member admits is taken, {@code vip} keeping {@code SSLv3} clear of + * the one pair the two members cannot carry together. + */ + @ParameterizedTest + @ValueSource(strings = {"SSLv3", "TLSv1", "TLSv1_2016", "TLSv1.1_2016", "TLSv1.2_2018", + "TLSv1.2_2019", "TLSv1.2_2021", "TLSv1.2_2025", "TLSv1.3_2025"}) + void everySecurityPolicyTheEnumerationAdmitsIsTaken(String protocolVersion) { + String certificateArn = importAcmCertificate().getArn(); + Map viewerCertificate = + completeViewerCertificate("ACMCertificateArn", certificateArn); + viewerCertificate.put("MinimumProtocolVersion", protocolVersion); + viewerCertificate.put("SSLSupportMethod", "vip"); + + Distribution created = cloudFrontService.createDistribution( + distributionWithViewerCertificate(viewerCertificate), Map.of()); + + assertEquals(protocolVersion, cloudFrontService.getDistribution(created.getId()) + .getConfig().getViewerCertificate().get("MinimumProtocolVersion")); + + cloudFrontService.removeDistribution(created.getId()); + acmService.deleteCertificate(certificateArn, ACM_REGION); + } + + /** + * {@code SSLSupportMethod} admits an enumeration too, and a value outside it has no code of its + * own: it reads as the {@code InvalidArgument} this service answers a member outside its shape + * with. + */ + @Test + void createDistributionRefusesAViewerSupportMethodOutsideTheEnumeration() { + Map viewerCertificate = + completeViewerCertificate("IAMCertificateId", "ASCAMISSING"); + viewerCertificate.put("SSLSupportMethod", "sni"); + + AwsException rejected = assertThrows(AwsException.class, () -> cloudFrontService + .createDistribution(distributionWithViewerCertificate(viewerCertificate), Map.of())); + + assertEquals("InvalidArgument", rejected.getErrorCode()); + assertEquals(400, rejected.getHttpStatus()); + assertEquals("The SSLSupportMethod specified is not valid.", rejected.getMessage()); + } + + /** And every value that member admits is taken, {@code static-ip} included. */ + @ParameterizedTest + @ValueSource(strings = {"sni-only", "vip", "static-ip"}) + void everyViewerSupportMethodTheEnumerationAdmitsIsTaken(String sslSupportMethod) { + String certificateArn = importAcmCertificate().getArn(); + Map viewerCertificate = + completeViewerCertificate("ACMCertificateArn", certificateArn); + viewerCertificate.put("SSLSupportMethod", sslSupportMethod); + + Distribution created = cloudFrontService.createDistribution( + distributionWithViewerCertificate(viewerCertificate), Map.of()); + + assertEquals(sslSupportMethod, cloudFrontService.getDistribution(created.getId()) + .getConfig().getViewerCertificate().get("SSLSupportMethod")); + + cloudFrontService.removeDistribution(created.getId()); + acmService.deleteCertificate(certificateArn, ACM_REGION); + } + + /** + * SNI carries a floor, "you must specify TLSv1 or higher", and {@code SSLv3} is the only value + * below it, which is the pair {@code InvalidProtocolSettings} names. + */ + @Test + void createDistributionRefusesSslv3AlongsideSniOnly() { + Map viewerCertificate = + completeViewerCertificate("IAMCertificateId", "ASCAMISSING"); + viewerCertificate.put("MinimumProtocolVersion", "SSLv3"); + + AwsException rejected = assertThrows(AwsException.class, () -> cloudFrontService + .createDistribution(distributionWithViewerCertificate(viewerCertificate), Map.of())); + + assertEquals("InvalidProtocolSettings", rejected.getErrorCode()); + assertEquals(400, rejected.getHttpStatus()); + assertEquals("You cannot specify SSLv3 as the minimum protocol version if you only want to " + + "support only clients that support Server Name Indication (SNI).", + rejected.getMessage()); + } + + /** + * The same pair on the update, which has never declared {@code InvalidProtocolSettings}: it + * answers the {@code InvalidMinimumProtocolVersion} it does declare, carrying the pair's own + * published sentence, and the refused update leaves the distribution on the policy it had. + */ + @Test + void updateDistributionRefusesSslv3AlongsideSniOnly() { + String certificateArn = importAcmCertificate().getArn(); + Distribution created = cloudFrontService.createDistribution( + distributionWithViewerCertificate("ACMCertificateArn", certificateArn), Map.of()); + Map viewerCertificate = + completeViewerCertificate("ACMCertificateArn", certificateArn); + viewerCertificate.put("MinimumProtocolVersion", "SSLv3"); + + AwsException rejected = assertThrows(AwsException.class, + () -> cloudFrontService.updateDistribution(created.getId(), created.getEtag(), + distributionWithViewerCertificate(viewerCertificate))); + + assertEquals("InvalidMinimumProtocolVersion", rejected.getErrorCode()); + assertEquals(400, rejected.getHttpStatus()); + assertEquals("You cannot specify SSLv3 as the minimum protocol version if you only want to " + + "support only clients that support Server Name Indication (SNI).", + rejected.getMessage()); + assertEquals("TLSv1.2_2021", cloudFrontService.getDistribution(created.getId()) + .getConfig().getViewerCertificate().get("MinimumProtocolVersion")); + + cloudFrontService.removeDistribution(created.getId()); + acmService.deleteCertificate(certificateArn, ACM_REGION); + } + + /** + * The update answers both protocol refusals with the one code it declares, and the sentences + * keep them apart: {@code InvalidMinimumProtocolVersion}'s own for a value outside the + * enumeration, and the pair's published one for {@code SSLv3} alongside {@code sni-only}. + */ + @Test + void theUpdateAnswersBothProtocolRefusalsWithTheCodeItDeclares() { + String certificateArn = importAcmCertificate().getArn(); + Distribution created = cloudFrontService.createDistribution( + distributionWithViewerCertificate("ACMCertificateArn", certificateArn), Map.of()); + Map outsideTheSet = + completeViewerCertificate("ACMCertificateArn", certificateArn); + outsideTheSet.put("MinimumProtocolVersion", "TLSv1.4"); + + AwsException outside = assertThrows(AwsException.class, + () -> cloudFrontService.updateDistribution(created.getId(), created.getEtag(), + distributionWithViewerCertificate(outsideTheSet))); + + assertEquals("InvalidMinimumProtocolVersion", outside.getErrorCode()); + assertEquals("The minimum protocol version specified is not valid.", outside.getMessage()); + + Map pair = completeViewerCertificate("ACMCertificateArn", certificateArn); + pair.put("MinimumProtocolVersion", "SSLv3"); + + AwsException refused = assertThrows(AwsException.class, + () -> cloudFrontService.updateDistribution(created.getId(), created.getEtag(), + distributionWithViewerCertificate(pair))); + + assertEquals(outside.getErrorCode(), refused.getErrorCode()); + assertEquals("You cannot specify SSLv3 as the minimum protocol version if you only want to " + + "support only clients that support Server Name Indication (SNI).", + refused.getMessage()); + + cloudFrontService.removeDistribution(created.getId()); + acmService.deleteCertificate(certificateArn, ACM_REGION); + } + + /** + * A request that sets {@code CloudFrontDefaultCertificate} is told to leave the other fields + * empty, so a certificate named beside it contradicts the request's own claim. Neither value + * here is looked up: the rule is read first. + */ + @ParameterizedTest + @ValueSource(strings = {"ACMCertificateArn", "IAMCertificateId"}) + void createDistributionRefusesACertificateAlongsideTheDefaultCertificate(String member) { + Map viewerCertificate = completeViewerCertificate(member, + "ACMCertificateArn".equals(member) + ? "arn:aws:acm:us-east-1:000000000000:certificate/" + UUID.randomUUID() + : "ASCAMISSING"); + viewerCertificate.put("CloudFrontDefaultCertificate", "true"); + + AwsException rejected = assertThrows(AwsException.class, () -> cloudFrontService + .createDistribution(distributionWithViewerCertificate(viewerCertificate), Map.of())); + + assertEquals("InvalidViewerCertificate", rejected.getErrorCode()); + assertEquals(400, rejected.getHttpStatus()); + assertEquals("You cannot specify ACMCertificateArn or IAMCertificateId when " + + "CloudFrontDefaultCertificate is true.", rejected.getMessage()); + } + + /** The contradiction is read on the update too, which keeps the certificate it already had. */ + @Test + void updateDistributionRefusesACertificateAlongsideTheDefaultCertificate() { + String certificateArn = importAcmCertificate().getArn(); + Distribution created = cloudFrontService.createDistribution( + distributionWithViewerCertificate("ACMCertificateArn", certificateArn), Map.of()); + Map viewerCertificate = + completeViewerCertificate("ACMCertificateArn", certificateArn); + viewerCertificate.put("CloudFrontDefaultCertificate", "true"); + + AwsException rejected = assertThrows(AwsException.class, + () -> cloudFrontService.updateDistribution(created.getId(), created.getEtag(), + distributionWithViewerCertificate(viewerCertificate))); + + assertEquals("InvalidViewerCertificate", rejected.getErrorCode()); + assertEquals(certificateArn, cloudFrontService.getDistribution(created.getId()) + .getConfig().getViewerCertificate().get("ACMCertificateArn")); + + cloudFrontService.removeDistribution(created.getId()); + acmService.deleteCertificate(certificateArn, ACM_REGION); + } + + /** + * The pair is not read for a distribution on the CloudFront certificate: the policy it asks + * for is replaced with {@code TLSv1} "regardless of the value that you set here", so the value + * that would make the pair is discarded and refusing the request for it would contradict the + * override. The distribution is taken and reads back the policy it is really on. + */ + @Test + void sslv3WithSniOnlyIsTakenOnTheDefaultCertificateWhosePolicyIsReplaced() { + Map viewerCertificate = defaultViewerCertificate("SSLv3"); + viewerCertificate.put("SSLSupportMethod", "sni-only"); + + Distribution created = cloudFrontService.createDistribution( + distributionWithViewerCertificate(viewerCertificate), Map.of()); + + assertEquals("TLSv1", cloudFrontService.getDistribution(created.getId()) + .getConfig().getViewerCertificate().get("MinimumProtocolVersion")); + + Distribution updated = cloudFrontService.updateDistribution(created.getId(), + created.getEtag(), distributionWithViewerCertificate(viewerCertificate)); + + assertEquals("TLSv1", updated.getConfig().getViewerCertificate().get("MinimumProtocolVersion")); + + cloudFrontService.removeDistribution(created.getId()); + } + + /** + * The claim itself is a member declared {@code boolean}, so text that is no boolean at all is + * refused rather than taken for false. Read for the word {@code true} alone, a request saying + * {@code yes} would dodge every rule the member governs. + */ + @ParameterizedTest + @ValueSource(strings = {"yes", "no", "TRUE1", "2", "t", "on", "null"}) + void createDistributionRefusesADefaultCertificateClaimThatIsNoBoolean(String claimed) { + Map viewerCertificate = new LinkedHashMap<>(); + viewerCertificate.put("CloudFrontDefaultCertificate", claimed); + + AwsException rejected = assertThrows(AwsException.class, () -> cloudFrontService + .createDistribution(distributionWithViewerCertificate(viewerCertificate), Map.of())); + + assertEquals("InvalidArgument", rejected.getErrorCode()); + assertEquals(400, rejected.getHttpStatus()); + assertEquals("The CloudFrontDefaultCertificate specified is not valid.", + rejected.getMessage()); + } + + /** + * A claim that is no boolean is refused even beside a certificate of the request's own, where + * taking it for false would have stored the distribution on that certificate instead. + */ + @Test + void createDistributionRefusesADefaultCertificateClaimThatIsNoBooleanBesideACertificate() { + String certificateArn = importAcmCertificate().getArn(); + Map viewerCertificate = + completeViewerCertificate("ACMCertificateArn", certificateArn); + viewerCertificate.put("CloudFrontDefaultCertificate", "yes"); + + AwsException rejected = assertThrows(AwsException.class, () -> cloudFrontService + .createDistribution(distributionWithViewerCertificate(viewerCertificate), Map.of())); + + assertEquals("InvalidArgument", rejected.getErrorCode()); + + acmService.deleteCertificate(certificateArn, ACM_REGION); + } + + /** The update reads the claim's shape too, and keeps the certificate it already had. */ + @Test + void updateDistributionRefusesADefaultCertificateClaimThatIsNoBoolean() { + String certificateArn = importAcmCertificate().getArn(); + Distribution created = cloudFrontService.createDistribution( + distributionWithViewerCertificate("ACMCertificateArn", certificateArn), Map.of()); + Map viewerCertificate = new LinkedHashMap<>(); + viewerCertificate.put("CloudFrontDefaultCertificate", "yes"); + + AwsException rejected = assertThrows(AwsException.class, + () -> cloudFrontService.updateDistribution(created.getId(), created.getEtag(), + distributionWithViewerCertificate(viewerCertificate))); + + assertEquals("InvalidArgument", rejected.getErrorCode()); + assertEquals(certificateArn, cloudFrontService.getDistribution(created.getId()) + .getConfig().getViewerCertificate().get("ACMCertificateArn")); + + cloudFrontService.removeDistribution(created.getId()); + acmService.deleteCertificate(certificateArn, ACM_REGION); + } + + /** + * Every form XML gives a boolean is taken, and the two that mean true put the distribution on + * the CloudFront certificate, where the security policy is the one that comes with it. + */ + @ParameterizedTest + @ValueSource(strings = {"true", "TRUE", "True", "1"}) + void everyBooleanFormThatMeansTrueClaimsTheDefaultCertificate(String claimed) { + Map viewerCertificate = new LinkedHashMap<>(); + viewerCertificate.put("CloudFrontDefaultCertificate", claimed); + viewerCertificate.put("MinimumProtocolVersion", "TLSv1.2_2021"); + + Distribution created = cloudFrontService.createDistribution( + distributionWithViewerCertificate(viewerCertificate), Map.of()); + + assertEquals("TLSv1", cloudFrontService.getDistribution(created.getId()) + .getConfig().getViewerCertificate().get("MinimumProtocolVersion")); + + cloudFrontService.removeDistribution(created.getId()); + } + + /** + * The forms that mean false leave the distribution on a certificate of its own, so the members + * that come with one are required and the security policy asked for is kept. + */ + @ParameterizedTest + @ValueSource(strings = {"false", "FALSE", "0"}) + void everyBooleanFormThatMeansFalseLeavesTheDistributionOnItsOwnCertificate(String claimed) { + String certificateArn = importAcmCertificate().getArn(); + Map viewerCertificate = + completeViewerCertificate("ACMCertificateArn", certificateArn); + viewerCertificate.put("CloudFrontDefaultCertificate", claimed); + + Distribution created = cloudFrontService.createDistribution( + distributionWithViewerCertificate(viewerCertificate), Map.of()); + + assertEquals("TLSv1.2_2021", cloudFrontService.getDistribution(created.getId()) + .getConfig().getViewerCertificate().get("MinimumProtocolVersion")); + + cloudFrontService.removeDistribution(created.getId()); + acmService.deleteCertificate(certificateArn, ACM_REGION); + } + + /** + * The security policy a distribution on the CloudFront domain name asks for is replaced rather + * than refused: CloudFront "automatically sets the security policy to TLSv1 regardless of the + * value that you set here". The map the caller handed in is left as it was. + */ + @Test + void theDefaultCertificateCarriesTlsv1WhateverTheRequestAsksFor() { + Map asked = defaultViewerCertificate("TLSv1.2_2021"); + + Distribution created = cloudFrontService.createDistribution( + distributionWithViewerCertificate(asked), Map.of()); + + assertEquals("TLSv1", cloudFrontService.getDistribution(created.getId()) + .getConfig().getViewerCertificate().get("MinimumProtocolVersion")); + assertEquals("TLSv1.2_2021", asked.get("MinimumProtocolVersion")); + + Distribution updated = cloudFrontService.updateDistribution(created.getId(), + created.getEtag(), + distributionWithViewerCertificate(defaultViewerCertificate("TLSv1.3_2025"))); + + assertEquals("TLSv1", updated.getConfig().getViewerCertificate().get("MinimumProtocolVersion")); + + cloudFrontService.removeDistribution(created.getId()); + } + + /** + * A request carrying no viewer certificate at all reads back the same way on the wire: the + * CloudFront default certificate, on the security policy that comes with it. + */ + @Test + void aDistributionWithNoViewerCertificateReadsAsTheDefaultCertificateOnTlsv1() { + String body = """ + + + no-viewer-cert-%s + + 1 + + + no-cert-origin + example.com + + 80 + 443 + http-only + + + + + + no-cert-origin + allow-all + + no-viewer-certificate + false + + """.formatted(suffix()); + + String location = given() + .contentType(ContentType.XML) + .body(body) + .when() + .post("/2020-05-31/distribution") + .then() + .statusCode(201) + .body("Distribution.DistributionConfig.ViewerCertificate.CloudFrontDefaultCertificate", + equalTo("true")) + .body("Distribution.DistributionConfig.ViewerCertificate.MinimumProtocolVersion", + equalTo("TLSv1")) + .extract().header("Location"); + + cloudFrontService.removeDistribution(location.substring(location.lastIndexOf('/') + 1)); + } }