Light, fluffy, and always free - The AWS Local Emulator alternative
fix(acm): reject RequestCertificate key algorithms real ACM does not support (#3044)
BingEdwarddb6c41945939
diff --git a/docs/services/acm.md b/docs/services/acm.mdindex 944bf81c38..091447ce5b 100644--- a/docs/services/acm.md+++ b/docs/services/acm.md@@ -32,7 +32,7 @@- **Real Cryptography:** Certificates are generated with real RSA/EC keys and valid X.509 structure- **One Local CA:** Every issued certificate (`AMAZON_ISSUED` and `PRIVATE`) is signed by Floci's local root CA, and `GetCertificate` returns that CA as `CertificateChain`. Trust it once (`GET /_floci/ca.pem`, see [TLS](../configuration/tls.md)) and both Floci's HTTPS endpoint and every ACM certificate validate. `DescribeCertificate` reports `Issuer` as the CA's name, `CN=Floci Local CA`, where AWS reports `Amazon`. `ImportCertificate` keeps the chain you upload.- **CA on First Use:** The CA is created under `{persistent-path}/tls/` the first time a certificate is issued, also with TLS off and in `memory` storage mode, so that directory must be writable. A certificate keeps the chain it was issued with; after a CA regeneration, delete and re-request it.-- **Key Algorithms:** Supports `RSA_2048`, `RSA_3072`, `RSA_4096`, `EC_prime256v1`, `EC_secp384r1`, `EC_secp521r1`+- **Key Algorithms:** `RequestCertificate` accepts `RSA_2048`, `EC_prime256v1`, and `EC_secp384r1` and rejects the rest with a `ValidationException`, matching real ACM; the wider list (`RSA_1024`, `RSA_3072`, `RSA_4096`, `EC_secp521r1`) remains valid for `ImportCertificate`- **Certificate Types:** `AMAZON_ISSUED` (default) and `PRIVATE` (when `CertificateAuthorityArn` is provided)- **Export:** Only `PRIVATE` type certificates can be exported with their private key- **In use:** `DescribeCertificate` lists under `InUseBy` the CloudFront distribution of each Cognito custom domain that uses the certificate, and `DeleteCertificate` refuses with `ResourceInUseException` while that list is not empty. Other consumers, such as load balancers and API Gateway domain names, are not tracked yet.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.javaindex e68a74751e..ba20c2c5c8 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@@ -46,6 +46,9 @@ public class AcmService implements ResourceProvider {private static final int MAX_TAG_VALUE_LENGTH = 256;private static final int MAX_SANS = 100;private static final int MAX_DOMAIN_LENGTH = 253;+ /** Key algorithms real ACM accepts for RequestCertificate; the wider enum stays valid for ImportCertificate. */+ private static final Set<KeyAlgorithm> REQUESTABLE_KEY_ALGORITHMS =+ EnumSet.of(KeyAlgorithm.RSA_2048, KeyAlgorithm.EC_prime256v1, KeyAlgorithm.EC_secp384r1);private final StorageBackend<String, Certificate> store;private final CertificateGenerator certificateGenerator;private final FlociCertificateAuthority certificateAuthority;@@ -113,6 +116,7 @@ public class AcmService implements ResourceProvider {}KeyAlgorithm alg = keyAlgorithm != null ? keyAlgorithm : KeyAlgorithm.RSA_2048;+ validateRequestableKeyAlgorithm(alg, region);// Check idempotency with parameter validationif (idempotencyToken != null && !idempotencyToken.isEmpty()) {@@ -670,6 +674,16 @@ public class AcmService implements ResourceProvider {}}+ private void validateRequestableKeyAlgorithm(KeyAlgorithm alg, String region) {+ if (REQUESTABLE_KEY_ALGORITHMS.contains(alg)) {+ return;+ }+ // Real ACM quirk: the RSA_4096 message carries the account id where the region goes.+ String location = alg == KeyAlgorithm.RSA_4096 ? regionResolver.getAccountId() : region;+ throw new AwsException("ValidationException",+ "Encryption Algorithm " + alg.name() + " is not supported in " + location + " region", 400);+ }+private void validateSans(List<String> sans) {if (sans != null && sans.size() > MAX_SANS) {throw new AwsException("ValidationException",diff --git a/src/test/java/io/github/hectorvent/floci/services/acm/AcmEdgeCaseTest.java b/src/test/java/io/github/hectorvent/floci/services/acm/AcmEdgeCaseTest.javaindex 49ceec4996..07fe8cdb82 100644--- a/src/test/java/io/github/hectorvent/floci/services/acm/AcmEdgeCaseTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/acm/AcmEdgeCaseTest.java@@ -398,8 +398,8 @@ class AcmEdgeCaseTest {// ==================== Key Algorithm Tests ====================@Test- void allKeyAlgorithmsSupported() {- String[] algorithms = {"RSA_2048", "RSA_3072", "RSA_4096", "EC_prime256v1", "EC_secp384r1", "EC_secp521r1"};+ void requestableKeyAlgorithmsAccepted() {+ String[] algorithms = {"RSA_2048", "EC_prime256v1", "EC_secp384r1"};for (String algo : algorithms) {given()@@ -418,4 +418,33 @@ class AcmEdgeCaseTest {.body("CertificateArn", startsWith("arn:aws:acm:"));}}++ @Test+ void unsupportedKeyAlgorithmsRejected() {+ // Real ACM rejects these on RequestCertificate. The RSA_4096 message carries the+ // account id where the region goes, an AWS quirk replicated verbatim.+ Map<String, String> expected = Map.of(+ "RSA_1024", "Encryption Algorithm RSA_1024 is not supported in us-east-1 region",+ "RSA_3072", "Encryption Algorithm RSA_3072 is not supported in us-east-1 region",+ "RSA_4096", "Encryption Algorithm RSA_4096 is not supported in 000000000000 region",+ "EC_secp521r1", "Encryption Algorithm EC_secp521r1 is not supported in us-east-1 region");++ for (Map.Entry<String, String> entry : expected.entrySet()) {+ given()+ .header("X-Amz-Target", "CertificateManager.RequestCertificate")+ .contentType(ACM_CONTENT_TYPE)+ .body("""+ {+ "DomainName": "%s.example.com",+ "KeyAlgorithm": "%s"+ }+ """.formatted(entry.getKey().toLowerCase().replace("_", "-"), entry.getKey()))+ .when()+ .post("/")+ .then()+ .statusCode(400)+ .body("__type", equalTo("ValidationException"))+ .body("message", equalTo(entry.getValue()));+ }+ }}diff --git a/src/test/java/io/github/hectorvent/floci/services/acm/AcmServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/acm/AcmServiceTest.javaindex aea7e1aeaf..c7f2f74fff 100644--- a/src/test/java/io/github/hectorvent/floci/services/acm/AcmServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/acm/AcmServiceTest.java@@ -79,7 +79,7 @@ class AcmServiceTest {}@ParameterizedTest- @EnumSource(KeyAlgorithm.class)+ @EnumSource(value = KeyAlgorithm.class, names = {"RSA_2048", "EC_prime256v1", "EC_secp384r1"})void issuesALeafOfTheRequestedAlgorithmSignedByTheLocalCa(KeyAlgorithm algorithm) throws Exception {Certificate cert = service.requestCertificate("api.example.test", List.of("*.example.test"),ValidationMethod.DNS, null, algorithm, null, null, Map.of(), REGION);