diff --git a/docs/services/cloudfront.md b/docs/services/cloudfront.md index 10417473ba..530873fff3 100644 --- a/docs/services/cloudfront.md +++ b/docs/services/cloudfront.md @@ -319,7 +319,11 @@ PUT, PATCH and DELETE). policies attached to a cache behavior cannot be deleted. - `ViewerCertificate.IAMCertificateId` must be the `ServerCertificateId` of an uploaded IAM server certificate: distribution create and update reject an unknown one with `InvalidViewerCertificate`, - and `DeleteServerCertificate` returns `DeleteConflict` while a distribution references it. + and `DeleteServerCertificate` returns `DeleteConflict` while a distribution references it. A blank + value names no certificate, which is what a distribution on the CloudFront domain name sends. The + `/cloudfront` upload path AWS documents for a certificate meant for CloudFront is not enforced, so + a certificate uploaded anywhere serves a distribution: a deliberate divergence, with both sources + and what would move it under Server Certificates in [IAM](iam.md). - `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. diff --git a/docs/services/iam.md b/docs/services/iam.md index 44223264df..2ddd8ce276 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -1238,6 +1238,40 @@ them, would buy nothing. The delete reads a snapshot of each load balancer's lis the live list, so an unguarded write cannot end its walk in `ConcurrentModificationException` either, which no mapper covers and which would surface as an unmapped 500. +What a distribution's `IAMCertificateId` is held to is the id and nothing else, and two questions +sat open on that member. The first is the path the certificate was uploaded under, and AWS +publishes a rule for it in two places. `UploadServerCertificate`'s `Path` carries the note that "If +you are uploading a server certificate specifically for use with Amazon CloudFront distributions, +you must specify a path using the path parameter. The path must begin with /cloudfront and must +include a trailing slash (for example, /cloudfront/test/)", and CloudFront's Developer Guide states +it flatly in the procedure for uploading a third-party certificate to IAM: the `--path` value "must +start with /cloudfront/" and "The path must end with a /". So the obligation on a caller is +documented, and this emulator does not hold callers to it. A certificate uploaded anywhere is +resolved by id and serves a distribution. + +That is a deliberate divergence, not a conclusion that AWS accepts any path, and nothing in hand +says how AWS answers a distribution naming a certificate stored elsewhere. What is published is the +obligation on the upload rather than a refusal on the write, and the one source that enumerates the +write's checks does not reach the path: the Developer Guide's `InvalidViewerCertificate` page lists +what CloudFront verifies when an alternate domain name is added, in the order it verifies them, and +names a distribution with no certificate attached, a chain longer than five, chain dates that are +not current, a certificate no trusted authority signed, a malformed certificate, an internal error +and a name the certificate does not cover. The certificate's path is none of them. Holding a +distribution to the path would therefore refuse a write on a rule published for the upload, which +is why the id alone decides here until something records otherwise. A capture of a write refused +for a certificate outside `/cloudfront` moves the reader, this paragraph and the test that pins it, +and the code would be `InvalidViewerCertificate`, 400 and sender fault, the one this service +already answers for an id nothing matches. + +The second question is a blank value, and AWS settles that one outright. `IAMCertificateId` is +declared with a minimum length of 0, so an empty string breaks no bound the member carries, and the +`ViewerCertificate` type's own entry tells a caller on the CloudFront domain name to set +`CloudFrontDefaultCertificate` to true and leave "all other fields empty". An element left empty is +what that instruction produces on this REST XML protocol, so a blank names no certificate: it is +not resolved, it is not reported to IAM as a reference, and it does not make the write wait on the +certificate lock. Reading it as an id instead would refuse with `InvalidViewerCertificate` the very +request the reference asks for. + ### SSH Public Keys | Action | Description | 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 ed7d403e97..4e03a8cdbb 100644 --- a/src/main/java/io/floci/aws/services/cloudfront/CloudFrontService.java +++ b/src/main/java/io/floci/aws/services/cloudfront/CloudFrontService.java @@ -298,6 +298,13 @@ public class CloudFrontService implements ServerCertificateReferenceProvider { return certificateArn == null || certificateArn.isBlank() ? null : certificateArn; } + /** + * The IAM certificate a distribution names, or null when the member is absent or blank. A + * blank is what a distribution on the CloudFront domain name sends, so it names no certificate + * rather than one that cannot be found. The {@code /cloudfront} upload path AWS documents for a + * certificate meant for CloudFront is deliberately not enforced here. Both are set out under + * Server Certificates in {@code docs/services/iam.md}. + */ private static String iamCertificateId(DistributionConfig config) { if (config == null || config.getViewerCertificate() == null) { return null; 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 7409066167..cfec8ffbc6 100644 --- a/src/test/java/io/floci/aws/services/cloudfront/CloudFrontViewerCertificateTest.java +++ b/src/test/java/io/floci/aws/services/cloudfront/CloudFrontViewerCertificateTest.java @@ -13,6 +13,7 @@ import io.floci.aws.services.iam.IamService; import io.floci.aws.services.iam.model.ServerCertificate; import io.quarkus.test.junit.QuarkusTest; import io.restassured.http.ContentType; +import io.restassured.response.ValidatableResponse; import org.junit.jupiter.api.Test; import java.util.LinkedHashMap; @@ -55,12 +56,24 @@ class CloudFrontViewerCertificateTest { } private ServerCertificate uploadCertificate(String name) { + return uploadCertificate(name, "/cloudfront/"); + } + + private ServerCertificate uploadCertificate(String name, String path) { CertificateGenerator.GeneratedCertificate pair = GENERATOR.generateSelfSignedCertificate( "cf.certs.test.local", List.of(), KeyAlgorithm.EC_prime256v1); - return iamService.uploadServerCertificate(name, "/cloudfront/", pair.certificatePem(), + return iamService.uploadServerCertificate(name, path, pair.certificatePem(), pair.privateKeyPem(), null, Map.of()); } + private ValidatableResponse deleteServerCertificate(String name) { + return given().header("Authorization", IAM_AUTH) + .contentType(ContentType.URLENC) + .formParam("Action", "DeleteServerCertificate") + .formParam("ServerCertificateName", name) + .when().post("/").then(); + } + private Certificate importAcmCertificate() { CertificateGenerator.GeneratedCertificate pair = GENERATOR.generateSelfSignedCertificate( "cf.certs.test.local", List.of(), KeyAlgorithm.EC_prime256v1); @@ -118,22 +131,14 @@ class CloudFrontViewerCertificateTest { Distribution created = cloudFrontService.createDistribution( distributionWithCertificate(certificate.getServerCertificateId()), Map.of()); - given().header("Authorization", IAM_AUTH) - .contentType(ContentType.URLENC) - .formParam("Action", "DeleteServerCertificate") - .formParam("ServerCertificateName", name) - .when().post("/").then() + deleteServerCertificate(name) .statusCode(409) .body("ErrorResponse.Error.Code", equalTo("DeleteConflict")) .body("ErrorResponse.Error.Message", containsString(created.getId())); cloudFrontService.removeDistribution(created.getId()); - given().header("Authorization", IAM_AUTH) - .contentType(ContentType.URLENC) - .formParam("Action", "DeleteServerCertificate") - .formParam("ServerCertificateName", name) - .when().post("/").then().statusCode(200); + deleteServerCertificate(name).statusCode(200); } @Test @@ -192,4 +197,87 @@ class CloudFrontViewerCertificateTest { cloudFrontService.removeDistribution(created.getId()); acmService.deleteCertificate(newArn, ACM_REGION); } + + /** + * A distribution on the CloudFront domain name is told to set + * {@code CloudFrontDefaultCertificate} and leave the other fields empty, and an element left + * empty reaches this protocol as a blank value. So a blank {@code IAMCertificateId} names no + * certificate: the create is accepted rather than refused for an id nothing matches, and IAM + * is told of no reference it would refuse a delete for. + */ + @Test + void aBlankIamCertificateIdNamesNoCertificate() { + String body = """ + + + blank-iam-cert-%s + + 1 + + + blank-cert-origin + example.com + + 80 + 443 + http-only + + + + + + blank-cert-origin + allow-all + + + true + + + blank-viewer-certificate + false + + """.formatted(suffix()); + + String location = given() + .contentType(ContentType.XML) + .body(body) + .when() + .post("/2020-05-31/distribution") + .then() + .statusCode(201) + .extract().header("Location"); + String id = location.substring(location.lastIndexOf('/') + 1); + + assertEquals(List.of(), cloudFrontService.serverCertificateReferences().stream() + .filter(reference -> reference.referrer().contains(id)) + .toList()); + + cloudFrontService.removeDistribution(id); + } + + /** + * The path a certificate was uploaded under does not decide whether a distribution can serve it + * here, the id does. AWS documents {@code /cloudfront} for a certificate meant for CloudFront, + * on IAM's upload and in CloudFront's own upload procedure, so what this pins is a deliberate + * divergence rather than a recorded AWS answer: nothing published says the distribution write + * refuses one stored elsewhere. Why it is left this way is under Server Certificates in + * {@code docs/services/iam.md}. + */ + @Test + void aCertificateOutsideTheCloudFrontPathStillServesADistribution() { + String name = "cf-cert-root-path-" + suffix(); + ServerCertificate certificate = uploadCertificate(name, "/"); + + Distribution created = cloudFrontService.createDistribution( + distributionWithCertificate(certificate.getServerCertificateId()), Map.of()); + + deleteServerCertificate(name) + .statusCode(409) + .body("ErrorResponse.Error.Code", equalTo("DeleteConflict")) + .body("ErrorResponse.Error.Message", containsString(created.getId())); + + cloudFrontService.removeDistribution(created.getId()); + + deleteServerCertificate(name).statusCode(200); + } }