diff --git a/docs/services/acm.md b/docs/services/acm.md index cdda33df06..5e279df653 100644 --- a/docs/services/acm.md +++ b/docs/services/acm.md @@ -20,6 +20,10 @@ | `RemoveTagsFromCertificate` | Remove tags from a certificate | | `GetAccountConfiguration` | Get account-level ACM settings | | `PutAccountConfiguration` | Update account-level ACM settings | +| `UpdateCertificateOptions` | - | +| `RevokeCertificate` | - | +| `RenewCertificate` | - | +| `ResendValidationEmail` | - | ## Emulation Behavior diff --git a/scripts/test-acm-integration b/scripts/test-acm-integration new file mode 100755 index 0000000000..0fb28fb29e --- /dev/null +++ b/scripts/test-acm-integration @@ -0,0 +1,4 @@ +#!/usr/bin/env bash +set -euo pipefail + +exec "$(dirname "$0")/test-focused" AcmIntegrationTest "$@" diff --git a/src/main/java/io/github/hectorvent/floci/services/acm/AcmJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/acm/AcmJsonHandler.java index e4d0fb43cd..3835071796 100644 --- a/src/main/java/io/github/hectorvent/floci/services/acm/AcmJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/acm/AcmJsonHandler.java @@ -49,12 +49,37 @@ public class AcmJsonHandler { case "RemoveTagsFromCertificate" -> handleRemoveTagsFromCertificate(request, region); case "GetAccountConfiguration" -> handleGetAccountConfiguration(request, region); case "PutAccountConfiguration" -> handlePutAccountConfiguration(request, region); + case "UpdateCertificateOptions" -> handleUpdateCertificateOptions(request, region); + case "RevokeCertificate" -> handleRevokeCertificate(request, region); + case "RenewCertificate" -> handleRenewCertificate(request, region); + case "ResendValidationEmail" -> handleResendValidationEmail(request, region); default -> Response.status(400) .entity(new AwsErrorResponse("UnsupportedOperation", "Operation " + action + " is not supported.")) .build(); }; } + private Response handleResendValidationEmail(JsonNode request, String region) { + String certificateArn = request.path("CertificateArn").asText(null); + String domain = request.path("Domain").asText(null); + String validationDomain = request.path("ValidationDomain").asText(null); + if (certificateArn == null || certificateArn.isBlank() || domain == null || domain.isBlank() || validationDomain == null || validationDomain.isBlank()) { + return Response.status(400).entity(new AwsErrorResponse("ValidationException", "Required parameter is missing")).build(); + } + Certificate cert = service.describeCertificate(certificateArn, region); + boolean domainMatchesCertificate = domain.equalsIgnoreCase(cert.getDomainName()) + || cert.getSubjectAlternativeNames().stream().anyMatch(domain::equalsIgnoreCase); + boolean validationDomainIsSuperdomain = domain.equalsIgnoreCase(validationDomain) + || domain.toLowerCase(java.util.Locale.ROOT).endsWith("." + validationDomain.toLowerCase(java.util.Locale.ROOT)); + if (!domainMatchesCertificate || !validationDomainIsSuperdomain) { + return Response.status(400) + .entity(new AwsErrorResponse("InvalidDomainValidationOptionsException", + "One or more values in the DomainValidationOption structure is incorrect.")) + .build(); + } + return Response.ok(objectMapper.createObjectNode()).build(); + } + private Response handleRequestCertificate(JsonNode request, String region) { String domainName = request.path("DomainName").asText(null); if (domainName == null || domainName.isBlank()) { @@ -227,6 +252,83 @@ public class AcmJsonHandler { return Response.ok(objectMapper.createObjectNode()).build(); } + private Response handleRevokeCertificate(JsonNode request, String region) { + String certificateArn = request.path("CertificateArn").asText(null); + if (certificateArn == null || certificateArn.isBlank()) { + return Response.status(400) + .entity(new AwsErrorResponse("ValidationException", "CertificateArn is required")) + .build(); + } + + String reasonStr = request.path("RevocationReason").asText(null); + if (reasonStr == null || reasonStr.isBlank()) { + return Response.status(400) + .entity(new AwsErrorResponse("ValidationException", "RevocationReason is required")) + .build(); + } + + RevocationReason reason; + try { + reason = RevocationReason.valueOf(reasonStr); + } catch (IllegalArgumentException e) { + return Response.status(400) + .entity(new AwsErrorResponse("ValidationException", "Invalid RevocationReason: " + reasonStr)) + .build(); + } + + Certificate cert = service.revokeCertificate(certificateArn, reason, region); + + ObjectNode response = objectMapper.createObjectNode(); + response.put("CertificateArn", cert.getArn()); + return Response.ok(response).build(); + } + + private Response handleRenewCertificate(JsonNode request, String region) { + String certificateArn = request.path("CertificateArn").asText(null); + if (certificateArn == null || certificateArn.isBlank()) { + return Response.status(400) + .entity(new AwsErrorResponse("ValidationException", "CertificateArn is required")) + .build(); + } + service.renewCertificate(certificateArn, region); + return Response.ok(objectMapper.createObjectNode()).build(); + } + + private Response handleUpdateCertificateOptions(JsonNode request, String region) { + String certificateArn = request.path("CertificateArn").asText(null); + if (certificateArn == null || certificateArn.isBlank()) { + return Response.status(400) + .entity(new AwsErrorResponse("ValidationException", "CertificateArn is required")) + .build(); + } + JsonNode optionsNode = request.path("Options"); + if (!optionsNode.isObject()) { + return Response.status(400) + .entity(new AwsErrorResponse("ValidationException", "Options is required")) + .build(); + } + service.updateCertificateOptions(certificateArn, parseUpdateOptions(optionsNode), region); + return Response.ok(objectMapper.createObjectNode()).build(); + } + + private CertificateOptions parseUpdateOptions(JsonNode optionsNode) { + return new CertificateOptions( + parseOptionValue(optionsNode, "CertificateTransparencyLoggingPreference"), + parseOptionValue(optionsNode, "Export")); + } + + private String parseOptionValue(JsonNode optionsNode, String field) { + if (!optionsNode.has(field)) { + return null; + } + String value = optionsNode.path(field).asText(); + if (!"ENABLED".equals(value) && !"DISABLED".equals(value)) { + throw new io.github.hectorvent.floci.core.common.AwsException( + "ValidationException", field + " must be ENABLED or DISABLED", 400); + } + return value; + } + // ============ Helper Methods ============ private ObjectNode buildCertificateDetail(Certificate cert) { @@ -329,6 +431,7 @@ public class AcmJsonHandler { ObjectNode opts = objectMapper.createObjectNode(); opts.put("CertificateTransparencyLoggingPreference", cert.getCertOptions().certificateTransparencyLoggingPreference()); + opts.put("Export", cert.getCertOptions().export()); node.set("Options", opts); } diff --git a/src/main/java/io/github/hectorvent/floci/services/acm/AcmService.java b/src/main/java/io/github/hectorvent/floci/services/acm/AcmService.java index 7f911e04a9..519487bc39 100644 --- a/src/main/java/io/github/hectorvent/floci/services/acm/AcmService.java +++ b/src/main/java/io/github/hectorvent/floci/services/acm/AcmService.java @@ -507,6 +507,47 @@ public class AcmService implements ResourceProvider { return Set.of(new SupportedResourceType("acm:certificate", "acm", true)); } + public Certificate revokeCertificate(String certificateArn, RevocationReason reason, String region) { + Certificate cert = getCertificateByArn(certificateArn, region); + boolean exportEnabled = cert.getCertOptions() != null && "ENABLED".equals(cert.getCertOptions().export()); + if (!exportEnabled) { + throw new AwsException("ValidationException", + "Certificate " + certificateArn + " cannot be revoked because it is not export-enabled.", 400); + } + cert.setStatus(CertificateStatus.REVOKED); + store.put(regionKey(region, cert.extractCertificateId()), cert); + return cert; + } + + public Certificate renewCertificate(String certificateArn, String region) { + Certificate cert = getCertificateByArn(certificateArn, region); + if (cert.getStatus() == CertificateStatus.PENDING_VALIDATION) { + throw new AwsException("RequestInProgressException", "Certificate is pending validation", 400); + } + if (cert.getType() != CertificateType.PRIVATE || cert.getStatus() != CertificateStatus.ISSUED) { + throw new AwsException("InvalidArnException", "Certificate is not a private issued certificate", 400); + } + // TODO: certificate/key reissuance material is not regenerated in this local emulator. + Instant now = Instant.now(); + cert.setIssuedAt(now); + cert.setNotBefore(now); + cert.setNotAfter(now.plusSeconds(365L * 24L * 60L * 60L)); + store.put(regionKey(region, cert.extractCertificateId()), cert); + return cert; + } + + public void updateCertificateOptions(String certificateArn, CertificateOptions options, String region) { + Certificate cert = getCertificateByArn(certificateArn, region); + CertificateOptions current = cert.getCertOptions() != null + ? cert.getCertOptions() : CertificateOptions.defaultOptions(); + cert.setCertOptions(new CertificateOptions( + options.certificateTransparencyLoggingPreference() != null + ? options.certificateTransparencyLoggingPreference() + : current.certificateTransparencyLoggingPreference(), + options.export() != null ? options.export() : current.export())); + store.put(regionKey(region, cert.extractCertificateId()), cert); + } + // ============ Helper Methods ============ private Certificate getCertificateByArn(String arn, String region) { diff --git a/src/main/java/io/github/hectorvent/floci/services/acm/model/RevocationReason.java b/src/main/java/io/github/hectorvent/floci/services/acm/model/RevocationReason.java new file mode 100644 index 0000000000..51de986a69 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/acm/model/RevocationReason.java @@ -0,0 +1,18 @@ +package io.github.hectorvent.floci.services.acm.model; + +import io.quarkus.runtime.annotations.RegisterForReflection; + +@RegisterForReflection +public enum RevocationReason { + UNSPECIFIED, + KEY_COMPROMISE, + CA_COMPROMISE, + AFFILIATION_CHANGED, + SUPERCEDED, + SUPERSEDED, + CESSATION_OF_OPERATION, + CERTIFICATE_HOLD, + REMOVE_FROM_CRL, + PRIVILEGE_WITHDRAWN, + A_A_COMPROMISE +} diff --git a/src/test/java/io/github/hectorvent/floci/services/acm/AcmIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/acm/AcmIntegrationTest.java index 818d6e9c87..8da953f962 100644 --- a/src/test/java/io/github/hectorvent/floci/services/acm/AcmIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/acm/AcmIntegrationTest.java @@ -537,6 +537,150 @@ class AcmIntegrationTest { .body("__type", equalTo("ResourceNotFoundException")); } + @Test + @Order(40) + void updateCertificateOptions() { + given() + .header("X-Amz-Target", "CertificateManager.UpdateCertificateOptions") + .contentType(ACM_CONTENT_TYPE) + .body(""" + { + "CertificateArn": "%s", + "Options": {"Export": "ENABLED"} + } + """.formatted(createdCertificateArn)) + .when() + .post("/") + .then() + .statusCode(200); + + given() + .header("X-Amz-Target", "CertificateManager.DescribeCertificate") + .contentType(ACM_CONTENT_TYPE) + .body(""" + {"CertificateArn": "%s"} + """.formatted(createdCertificateArn)) + .when() + .post("/") + .then() + .statusCode(200) + .body("Certificate.Options.Export", equalTo("ENABLED")); + } + + @Test + @Order(41) + void updateCertificateOptionsRequiresOptions() { + given() + .header("X-Amz-Target", "CertificateManager.UpdateCertificateOptions") + .contentType(ACM_CONTENT_TYPE) + .body(""" + {"CertificateArn": "%s"} + """.formatted(createdCertificateArn)) + .when() + .post("/") + .then() + .statusCode(400) + .body("__type", equalTo("ValidationException")); + } + + @Test + @Order(42) + void revokeCertificate() { + String certificateArn = given() + .header("X-Amz-Target", "CertificateManager.RequestCertificate") + .contentType(ACM_CONTENT_TYPE) + .body(""" + {"DomainName": "revocable.example.com"} + """) + .when() + .post("/") + .then() + .statusCode(200) + .extract().jsonPath().getString("CertificateArn"); + + given() + .header("X-Amz-Target", "CertificateManager.UpdateCertificateOptions") + .contentType(ACM_CONTENT_TYPE) + .body(""" + {"CertificateArn": "%s", "Options": {"Export": "ENABLED"}} + """.formatted(certificateArn)) + .when() + .post("/") + .then() + .statusCode(200); + + given() + .header("X-Amz-Target", "CertificateManager.RevokeCertificate") + .contentType(ACM_CONTENT_TYPE) + .body(""" + {"CertificateArn": "%s", "RevocationReason": "KEY_COMPROMISE"} + """.formatted(certificateArn)) + .when() + .post("/") + .then() + .statusCode(200) + .body("CertificateArn", equalTo(certificateArn)); + + given() + .header("X-Amz-Target", "CertificateManager.DescribeCertificate") + .contentType(ACM_CONTENT_TYPE) + .body(""" + {"CertificateArn": "%s"} + """.formatted(certificateArn)) + .when() + .post("/") + .then() + .statusCode(200) + .body("Certificate.Status", equalTo("REVOKED")); + } + + @Test + @Order(43) + void renewPrivateCertificate() { + String certificateArn = given() + .header("X-Amz-Target", "CertificateManager.RequestCertificate") + .contentType(ACM_CONTENT_TYPE) + .body(""" + { + "DomainName": "renewable-private.example.com", + "CertificateAuthorityArn": "arn:aws:acm-pca:us-east-1:000000000000:certificate-authority/test" + } + """) + .when() + .post("/") + .then() + .statusCode(200) + .extract().jsonPath().getString("CertificateArn"); + + given() + .header("X-Amz-Target", "CertificateManager.RenewCertificate") + .contentType(ACM_CONTENT_TYPE) + .body(""" + {"CertificateArn": "%s"} + """.formatted(certificateArn)) + .when() + .post("/") + .then() + .statusCode(200) + .body(equalTo("{}")); + } + + @Test + @Order(44) + void renewCertificateNotFound() { + given() + .header("X-Amz-Target", "CertificateManager.RenewCertificate") + .contentType(ACM_CONTENT_TYPE) + .body(""" + {"CertificateArn": "arn:aws:acm:us-east-1:123456789012:certificate/nonexistent"} + """) + .when() + .post("/") + .then() + .statusCode(404) + .body("__type", equalTo("ResourceNotFoundException")); + } + // ==================== Unsupported Operation ==================== @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/acm/AcmPaginationTest.java b/src/test/java/io/github/hectorvent/floci/services/acm/AcmPaginationTest.java index f8e70f4907..c5a8b5916d 100644 --- a/src/test/java/io/github/hectorvent/floci/services/acm/AcmPaginationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/acm/AcmPaginationTest.java @@ -140,8 +140,12 @@ class AcmPaginationTest { @Test @Order(5) void emptyListReturnsNoNextToken() { - // List with a filter that matches nothing - given() + // Filter by a status none of this class's own certificates ever have. Can't assert the + // whole result is empty -- other test classes in the shared full-suite JVM (e.g. + // AcmIntegrationTest) revoke certificates of their own, and that state persists across + // classes. What this test actually verifies -- filtering excludes non-matching + // certificates and a small result page carries no NextToken -- holds regardless. + Response response = given() .header("X-Amz-Target", "CertificateManager.ListCertificates") .contentType(ACM_CONTENT_TYPE) .body(""" @@ -153,8 +157,12 @@ class AcmPaginationTest { .post("/") .then() .statusCode(200) - .body("CertificateSummaryList", empty()) - .body("NextToken", nullValue()); + .extract().response(); + + List revokedArns = response.jsonPath().getList("CertificateSummaryList.CertificateArn"); + assertTrue(createdArns.stream().noneMatch(revokedArns::contains), + "none of this class's own (always-ISSUED) certificates should appear under a REVOKED filter"); + assertNull(response.jsonPath().get("NextToken")); } @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/acm/AcmResendValidationEmailIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/acm/AcmResendValidationEmailIntegrationTest.java new file mode 100644 index 0000000000..c4feee702b --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/acm/AcmResendValidationEmailIntegrationTest.java @@ -0,0 +1,110 @@ +package io.github.hectorvent.floci.services.acm; + +import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; + +@QuarkusTest +class AcmResendValidationEmailIntegrationTest { + + private static final String ACM_CONTENT_TYPE = "application/x-amz-json-1.1"; + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @Test + void testResendValidationEmailNotFound() { + String requestBody = "{\"CertificateArn\":\"arn:aws:acm:us-east-1:000000000000:certificate/nonexistent\",\"Domain\":\"example.com\",\"ValidationDomain\":\"example.com\"}"; + + given() + .header("X-Amz-Target", "CertificateManager.ResendValidationEmail") + .contentType(ACM_CONTENT_TYPE) + .body(requestBody) + .when() + .post("/") + .then() + .statusCode(404); + } + + @Test + void testResendValidationEmailRejectsUnrelatedDomain() { + String certificateArn = given() + .header("X-Amz-Target", "CertificateManager.RequestCertificate") + .contentType(ACM_CONTENT_TYPE) + .body("{\"DomainName\":\"resend-validation.example.com\"}") + .when() + .post("/") + .then() + .statusCode(200) + .extract().jsonPath().getString("CertificateArn"); + + String requestBody = "{\"CertificateArn\":\"" + certificateArn + + "\",\"Domain\":\"totally-unrelated.example.org\",\"ValidationDomain\":\"totally-unrelated.example.org\"}"; + + given() + .header("X-Amz-Target", "CertificateManager.ResendValidationEmail") + .contentType(ACM_CONTENT_TYPE) + .body(requestBody) + .when() + .post("/") + .then() + .statusCode(400) + .body("__type", org.hamcrest.Matchers.equalTo("InvalidDomainValidationOptionsException")); + } + + @Test + void testResendValidationEmailRejectsValidationDomainNotASuperdomain() { + String certificateArn = given() + .header("X-Amz-Target", "CertificateManager.RequestCertificate") + .contentType(ACM_CONTENT_TYPE) + .body("{\"DomainName\":\"site.subdomain.resend-superdomain.example.com\"}") + .when() + .post("/") + .then() + .statusCode(200) + .extract().jsonPath().getString("CertificateArn"); + + String requestBody = "{\"CertificateArn\":\"" + certificateArn + + "\",\"Domain\":\"site.subdomain.resend-superdomain.example.com\",\"ValidationDomain\":\"unrelated.example.net\"}"; + + given() + .header("X-Amz-Target", "CertificateManager.ResendValidationEmail") + .contentType(ACM_CONTENT_TYPE) + .body(requestBody) + .when() + .post("/") + .then() + .statusCode(400) + .body("__type", org.hamcrest.Matchers.equalTo("InvalidDomainValidationOptionsException")); + } + + @Test + void testResendValidationEmailAcceptsMatchingDomainAndSuperdomain() { + String certificateArn = given() + .header("X-Amz-Target", "CertificateManager.RequestCertificate") + .contentType(ACM_CONTENT_TYPE) + .body("{\"DomainName\":\"site.subdomain.resend-ok.example.com\"}") + .when() + .post("/") + .then() + .statusCode(200) + .extract().jsonPath().getString("CertificateArn"); + + String requestBody = "{\"CertificateArn\":\"" + certificateArn + + "\",\"Domain\":\"site.subdomain.resend-ok.example.com\",\"ValidationDomain\":\"subdomain.resend-ok.example.com\"}"; + + given() + .header("X-Amz-Target", "CertificateManager.ResendValidationEmail") + .contentType(ACM_CONTENT_TYPE) + .body(requestBody) + .when() + .post("/") + .then() + .statusCode(200); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/acm/AcmRevokeCertificateGuardTest.java b/src/test/java/io/github/hectorvent/floci/services/acm/AcmRevokeCertificateGuardTest.java new file mode 100644 index 0000000000..6cd51e7cc3 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/acm/AcmRevokeCertificateGuardTest.java @@ -0,0 +1,55 @@ +package io.github.hectorvent.floci.services.acm; + +import io.github.hectorvent.floci.services.acm.model.KeyAlgorithm; +import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; +import io.quarkus.test.junit.QuarkusTest; +import jakarta.inject.Inject; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; + +@QuarkusTest +class AcmRevokeCertificateGuardTest { + + private static final String ACM_CONTENT_TYPE = "application/x-amz-json-1.1"; + + @Inject + CertificateGenerator certificateGenerator; + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @Test + void revokeCertificateRejectsImportedCertificateWithoutExportEnabled() { + CertificateGenerator.GeneratedCertificate generated = certificateGenerator.generateCertificate( + "revoke-guard-imported.example.com", List.of(), KeyAlgorithm.RSA_2048); + String certJson = generated.certificatePem().replace("\r\n", "\n").replace("\n", "\\n"); + String keyJson = generated.privateKeyPem().replace("\r\n", "\n").replace("\n", "\\n"); + + String certificateArn = given() + .header("X-Amz-Target", "CertificateManager.ImportCertificate") + .contentType(ACM_CONTENT_TYPE) + .body("{\"Certificate\":\"" + certJson + "\",\"PrivateKey\":\"" + keyJson + "\"}") + .when() + .post("/") + .then() + .statusCode(200) + .extract().jsonPath().getString("CertificateArn"); + + given() + .header("X-Amz-Target", "CertificateManager.RevokeCertificate") + .contentType(ACM_CONTENT_TYPE) + .body("{\"CertificateArn\":\"" + certificateArn + "\",\"RevocationReason\":\"KEY_COMPROMISE\"}") + .when() + .post("/") + .then() + .statusCode(400) + .body("__type", equalTo("ValidationException")); + } +}