Light, fluffy, and always free - The AWS Local Emulator alternative
fix(apigateway): default a REST custom domain to EDGE when no endpoint type is given (#6885)
Allen Sanborn043664471bde
diff --git a/docs/services/api-gateway.md b/docs/services/api-gateway.mdindex e2c3020c4f..fb3d16f6e9 100644--- a/docs/services/api-gateway.md+++ b/docs/services/api-gateway.md@@ -211,7 +211,10 @@ A domain created with `CreateDomainName` is `AVAILABLE` at once. Its `regionalDoitself is routed through the domain's base path mappings to the mapped REST API stage. An`EDGE` domain also reports a `distributionDomainName` under `cloudfront.net` and the fixedCloudFront hosted zone `Z2FDTNDATAQYW2`; a `REGIONAL` domain reports neither, as on AWS. Moving a-domain between the two types with `UpdateDomainName` creates or drops the distribution.+domain between the two types with `UpdateDomainName` creates or drops the distribution. A+domain created with no endpoint type is `EDGE`, as on AWS; it is `REGIONAL` when the request+carries a regional certificate (`regionalCertificateArn` or `regionalCertificateName`) or the+partition has no edge-optimized endpoints.`GetDomainName` returns the `domainNameArn`, the `endpointConfiguration`, both certificate ARNsand the `tags`. Tags are managed through `TagResource`, `UntagResource` and `GetTags` on`arn:aws:apigateway:<region>::/domainnames/<domain>`. A second mapping on a base path thatdiff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.javaindex 54070aafa3..1d910dd8ee 100644--- a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java+++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java@@ -2232,6 +2232,17 @@ public class ApiGatewayService implements ResourceProvider {String domainName = (String) request.get("domainName");if (domainName == null) throw new AwsException("BadRequestException", "domainName is required", 400);String endpointType = endpointTypeOf(request);+ if (endpointType == null) {+ // EDGE is AWS's default for a REST domain: terraform-provider-aws v6.70.0+ // internal/service/apigateway/domain_name_test.go (acceptance test against AWS) creates one+ // with only certificate_body/certificate_name and asserts a CloudFront domain and zone.+ // A regional certificate => REGIONAL is NOT verified against AWS; it keeps Floci's previous+ // behaviour, as does REGIONAL in a partition without edge.+ boolean regionalCertificate = request.get("regionalCertificateArn") != null+ || request.get("regionalCertificateName") != null;+ boolean edgeAvailable = EDGE_OPTIMIZED_PARTITIONS.contains(AwsPartitions.forRegionOrCommercial(region).id());+ endpointType = !regionalCertificate && edgeAvailable ? "EDGE" : "REGIONAL";+ }if (!"REGIONAL".equals(endpointType) && !"EDGE".equals(endpointType)) {// Private custom domains are not emulated; anything else would be a domain that is// neither regional nor edge-optimized, which nothing here could route or describe.@@ -2309,8 +2320,7 @@ public class ApiGatewayService implements ResourceProvider {/*** Reads the endpoint type from either spelling: REST passes {@code endpointConfiguration.types},- * HTTP APIs pass a single {@code endpointType}. REGIONAL is the default an emulated domain gets,- * since nothing here fronts it with an edge distribution.+ * HTTP APIs pass a single {@code endpointType}. Null when the request names neither.*/private static String endpointTypeOf(Map<String, Object> request) {Object endpointType = request.get("endpointType");@@ -2322,7 +2332,7 @@ public class ApiGatewayService implements ResourceProvider {&& types.getFirst() instanceof String type && !type.isBlank()) {return type;}- return "REGIONAL";+ return null;}public CustomDomain getDomainName(String region, String domainName) {diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayDomainNameIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayDomainNameIntegrationTest.javaindex cea5763323..7ffcee18d9 100644--- a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayDomainNameIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayDomainNameIntegrationTest.java@@ -80,6 +80,37 @@ class ApiGatewayDomainNameIntegrationTest {deleteDomain(domain);}+ @Test+ void domainWithOnlyACertificateArnDefaultsToEdge() {+ String domain = "default-edge.apigw-domain-it.example.com";+ createDomain("""+ {"domainName":"%s","certificateArn":"%s"}+ """.formatted(domain, CERTIFICATE_ARN));++ given().when().get("/domainnames/" + domain).then()+ .statusCode(200)+ .body("endpointConfiguration.types[0]", equalTo("EDGE"))+ .body("distributionDomainName", endsWith(".cloudfront.net"))+ .body("distributionHostedZoneId", equalTo("Z2FDTNDATAQYW2"));++ deleteDomain(domain);+ }++ @Test+ void domainWithOnlyARegionalCertificateStaysRegional() {+ String domain = "default-regional.apigw-domain-it.example.com";+ createDomain("""+ {"domainName":"%s","regionalCertificateArn":"%s"}+ """.formatted(domain, CERTIFICATE_ARN));++ given().when().get("/domainnames/" + domain).then()+ .statusCode(200)+ .body("endpointConfiguration.types[0]", equalTo("REGIONAL"))+ .body("distributionDomainName", nullValue());++ deleteDomain(domain);+ }+@Testvoid endpointTypeCanBeMovedFromEdgeToRegional() {String domain = "endpoint-type.apigw-domain-it.example.com";diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayEdgeDomainPartitionTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayEdgeDomainPartitionTest.javaindex 068eee72ea..3754d9fee7 100644--- a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayEdgeDomainPartitionTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayEdgeDomainPartitionTest.java@@ -91,4 +91,13 @@ class ApiGatewayEdgeDomainPartitionTest {assertEquals("BadRequestException", error.getErrorCode());}++ /** Without a type the domain is edge-optimized only where the partition has edge. */+ @Test+ void aDomainWithoutATypeStaysRegionalWhereThePartitionHasNoCloudFront() {+ CustomDomain domain = service.createDomainName(GOV_REGION, Map.of("domainName", "default.example.com",+ "certificateArn", "arn:aws-us-gov:acm:us-gov-west-1:000000000000:certificate/d"));++ assertEquals("REGIONAL", domain.getEndpointConfigurationType());+ }}diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/LegacyBasePathMappingIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/LegacyBasePathMappingIntegrationTest.javaindex 8a88afa399..eee02b35d1 100644--- a/src/test/java/io/github/hectorvent/floci/services/apigateway/LegacyBasePathMappingIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/LegacyBasePathMappingIntegrationTest.java@@ -41,7 +41,8 @@ class LegacyBasePathMappingIntegrationTest {}private void seedLegacyState() {- service.createDomainName(REGION, Map.of("domainName", DOMAIN));+ // REGIONAL: the v2 mapping API this test reads through refuses an EDGE domain, as AWS does.+ service.createDomainName(REGION, Map.of("domainName", DOMAIN, "endpointType", "REGIONAL"));// The canonical root, as writes produce it today.service.createBasePathMapping(REGION, DOMAIN, Map.of("restApiId", "api-canonical", "stage", "prod"));// And one the old write path left under the empty key; its field still reads "(none)".