From 9d49e619c10bfb66e8940469866b9db22b01c52f Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Wed, 7 Oct 2026 08:34:02 -0500 Subject: [PATCH] fix(appsync): front custom domains with the partition's CloudFront zone (#5237) * fix(appsync): front custom domains with the partition's CloudFront zone CreateDomainName returned a random hosted zone id and an appsync-api regional host as appsyncDomainName. AppSync serves a custom domain through CloudFront: the CDK aliases appsyncDomainName into the partition's CloudFront hosted zone (aws-route53-targets AppSyncTarget), so a DNS alias built from Floci's answer pointed at a zone that does not exist. The domain now gets a CloudFront distribution name on the configured CloudFront suffix and the partition's CloudFront hosted zone (Z2FDTNDATAQYW2 in aws, Z3RFFRIM2A3IF5 in aws-cn), as an edge-optimized API Gateway domain does. * refactor(appsync): share the CloudFront front with edge-optimized API Gateway domains An AppSync custom domain and an edge-optimized API Gateway domain both get a CloudFront distribution name and the partition's CloudFront hosted zone, and the two services built that pair with the same copied statements. CloudFrontEdgeDomain now builds it once for both. RequestRegionArnIntegrationTest still expected appsyncDomainName to carry the request region; it now asserts the CloudFront form, while its domain ARN assertion keeps checking the region. --- .../core/common/CloudFrontEdgeDomain.java | 22 +++++++++++++++ .../apigateway/ApiGatewayService.java | 10 +++---- .../services/appsync/AppSyncService.java | 21 +++++++++++--- .../core/common/CloudFrontEdgeDomainTest.java | 28 +++++++++++++++++++ .../RequestRegionArnIntegrationTest.java | 4 +-- .../appsync/AppSyncIntegrationTest.java | 4 +-- .../services/appsync/AppSyncServiceTest.java | 26 +++++++++++++++-- 7 files changed, 99 insertions(+), 16 deletions(-) create mode 100644 src/main/java/io/github/hectorvent/floci/core/common/CloudFrontEdgeDomain.java create mode 100644 src/test/java/io/github/hectorvent/floci/core/common/CloudFrontEdgeDomainTest.java diff --git a/src/main/java/io/github/hectorvent/floci/core/common/CloudFrontEdgeDomain.java b/src/main/java/io/github/hectorvent/floci/core/common/CloudFrontEdgeDomain.java new file mode 100644 index 0000000000..6c3c7f1709 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/core/common/CloudFrontEdgeDomain.java @@ -0,0 +1,22 @@ +package io.github.hectorvent.floci.core.common; + +import java.util.UUID; + +/** + * The CloudFront distribution AWS puts in front of a custom domain, an edge-optimized API Gateway + * domain or an AppSync custom domain: a distribution domain name on the configured CloudFront + * suffix, and the CloudFront hosted zone of the region's partition, which a Route 53 alias record + * targets. The zone is null in a partition without CloudFront. + * + * @param domainName the distribution domain name, {@code d<13 hex>.} + * @param hostedZoneId the partition's CloudFront hosted zone, or null where there is none + */ +public record CloudFrontEdgeDomain(String domainName, String hostedZoneId) { + + public static CloudFrontEdgeDomain create(String region, String domainSuffix) { + String distributionId = "d" + UUID.randomUUID().toString().replace("-", "").substring(0, 13); + String hostedZoneId = AwsRegionFacts.cloudFrontHostedZoneId( + AwsPartitions.forRegionOrCommercial(region).id()).orElse(null); + return new CloudFrontEdgeDomain(distributionId + "." + domainSuffix, hostedZoneId); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java index 6adb3a240d..8d1b58bdaa 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 @@ -10,7 +10,7 @@ import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.AwsPartition; import io.github.hectorvent.floci.core.common.AwsPartitions; -import io.github.hectorvent.floci.core.common.AwsRegionFacts; +import io.github.hectorvent.floci.core.common.CloudFrontEdgeDomain; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.common.ReservedTags; import io.github.hectorvent.floci.core.resource.ExplorerResource; @@ -2082,11 +2082,9 @@ public class ApiGatewayService implements ResourceProvider { domain.setDistributionDomainName(null); domain.setDistributionHostedZoneId(null); } else if (domain.getDistributionDomainName() == null) { - domain.setDistributionDomainName( - "d" + UUID.randomUUID().toString().replace("-", "").substring(0, 13) + "." - + config.services().cloudfront().domainSuffix()); - domain.setDistributionHostedZoneId(AwsRegionFacts.cloudFrontHostedZoneId( - AwsPartitions.forRegionOrCommercial(region).id()).orElse(null)); + CloudFrontEdgeDomain edge = CloudFrontEdgeDomain.create(region, config.services().cloudfront().domainSuffix()); + domain.setDistributionDomainName(edge.domainName()); + domain.setDistributionHostedZoneId(edge.hostedZoneId()); } } diff --git a/src/main/java/io/github/hectorvent/floci/services/appsync/AppSyncService.java b/src/main/java/io/github/hectorvent/floci/services/appsync/AppSyncService.java index ab235d449f..ec7f9bfdc9 100644 --- a/src/main/java/io/github/hectorvent/floci/services/appsync/AppSyncService.java +++ b/src/main/java/io/github/hectorvent/floci/services/appsync/AppSyncService.java @@ -4,8 +4,8 @@ import io.github.hectorvent.floci.core.common.AwsArnUtils; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import io.github.hectorvent.floci.config.EmulatorConfig; -import io.github.hectorvent.floci.core.common.AwsEndpoints; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.CloudFrontEdgeDomain; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.common.RequestContext; import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; @@ -59,6 +59,7 @@ public class AppSyncService { private final ObjectMapper objectMapper; private final Clock clock; private final String baseUrl; + private final String cloudFrontDomainSuffix; private final LambdaAuthorizerCache lambdaAuthorizerCache; @Inject @@ -88,6 +89,7 @@ public class AppSyncService { this.objectMapper = objectMapper; this.clock = clock; this.baseUrl = trimTrailingSlash(config.effectiveBaseUrl()); + this.cloudFrontDomainSuffix = config.services().cloudfront().domainSuffix(); this.lambdaAuthorizerCache = lambdaAuthorizerCache; } @@ -735,9 +737,7 @@ public class AppSyncService { dn.setDomainName(domainName); dn.setDescription((String) request.get("description")); dn.setCertificateArn((String) request.get("certificateArn")); - String shortId = generateShortId(); - dn.setAppsyncDomainName(shortId + "." + AwsEndpoints.host("appsync-api", regionResolver.getRegion())); - dn.setHostedZoneId("Z" + generateShortId()); + applyCloudFrontFront(dn, regionResolver.getRegion()); dn.setDomainNameArn(regionResolver.buildArn("appsync", regionResolver.getRegion(), "domainnames/" + domainName)); @@ -752,6 +752,19 @@ public class AppSyncService { return dn; } + /** + * AppSync fronts a custom domain with a CloudFront distribution: the CDK aliases + * {@code appsyncDomainName} into the CloudFront hosted zone of the partition + * ({@code aws-route53-targets/lib/appsync-target.ts}), so the name is a distribution name and + * the zone is CloudFront's, never a zone of its own, the same front an edge-optimized API + * Gateway domain gets. + */ + private void applyCloudFrontFront(DomainName dn, String region) { + CloudFrontEdgeDomain edge = CloudFrontEdgeDomain.create(region, cloudFrontDomainSuffix); + dn.setAppsyncDomainName(edge.domainName()); + dn.setHostedZoneId(edge.hostedZoneId()); + } + public DomainName getDomainName(String domainName) { return domainStore.get(domainName) .orElseThrow(() -> new AwsException("NotFoundException", "Domain name not found: " + domainName, 404)); diff --git a/src/test/java/io/github/hectorvent/floci/core/common/CloudFrontEdgeDomainTest.java b/src/test/java/io/github/hectorvent/floci/core/common/CloudFrontEdgeDomainTest.java new file mode 100644 index 0000000000..fe278c4f2b --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/core/common/CloudFrontEdgeDomainTest.java @@ -0,0 +1,28 @@ +package io.github.hectorvent.floci.core.common; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** The CloudFront front an edge-optimized API Gateway domain and an AppSync custom domain share. */ +class CloudFrontEdgeDomainTest { + + @Test + void namesADistributionOnTheConfiguredSuffix() { + CloudFrontEdgeDomain edge = CloudFrontEdgeDomain.create("us-east-1", "cloudfront.net"); + + assertTrue(edge.domainName().matches("d[0-9a-f]{13}\\.cloudfront\\.net"), edge.domainName()); + assertNotEquals(edge.domainName(), CloudFrontEdgeDomain.create("us-east-1", "cloudfront.net").domainName()); + } + + @Test + void hostedZoneIsThePartitionsCloudFrontZone() { + assertEquals("Z2FDTNDATAQYW2", CloudFrontEdgeDomain.create("eu-west-1", "cloudfront.net").hostedZoneId()); + assertEquals("Z3RFFRIM2A3IF5", CloudFrontEdgeDomain.create("cn-north-1", "cloudfront.net").hostedZoneId()); + assertNull(CloudFrontEdgeDomain.create("us-gov-west-1", "cloudfront.net").hostedZoneId(), + "GovCloud has no CloudFront"); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/core/common/RequestRegionArnIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/RequestRegionArnIntegrationTest.java index 962bfae73b..4e34c4ead0 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/RequestRegionArnIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/RequestRegionArnIntegrationTest.java @@ -7,7 +7,7 @@ import java.nio.charset.StandardCharsets; import java.util.Base64; import static io.restassured.RestAssured.given; -import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.endsWith; import static org.hamcrest.Matchers.startsWith; /** @@ -117,7 +117,7 @@ class RequestRegionArnIntegrationTest { .then() .statusCode(200) .body("domainNameConfig.domainNameArn", startsWith("arn:aws:appsync:" + REGION + ":")) - .body("domainNameConfig.appsyncDomainName", containsString(REGION)); + .body("domainNameConfig.appsyncDomainName", endsWith(".cloudfront.net")); given().header("Authorization", auth("appsync")).delete("/v1/domainnames/" + domain) .then().statusCode(204); diff --git a/src/test/java/io/github/hectorvent/floci/services/appsync/AppSyncIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/appsync/AppSyncIntegrationTest.java index b48187d4b0..a15db838ff 100644 --- a/src/test/java/io/github/hectorvent/floci/services/appsync/AppSyncIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/appsync/AppSyncIntegrationTest.java @@ -2443,8 +2443,8 @@ class AppSyncIntegrationTest { .statusCode(200) .body("domainNameConfig.domainName", equalTo("api.example.com")) .body("domainNameConfig.description", equalTo("Test domain")) - .body("domainNameConfig.appsyncDomainName", containsString(".appsync-api.")) - .body("domainNameConfig.hostedZoneId", notNullValue()) + .body("domainNameConfig.appsyncDomainName", endsWith(".cloudfront.net")) + .body("domainNameConfig.hostedZoneId", equalTo("Z2FDTNDATAQYW2")) .extract().path("domainNameConfig.domainName"); } diff --git a/src/test/java/io/github/hectorvent/floci/services/appsync/AppSyncServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/appsync/AppSyncServiceTest.java index 0a78f4347c..6ca9eefc94 100644 --- a/src/test/java/io/github/hectorvent/floci/services/appsync/AppSyncServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/appsync/AppSyncServiceTest.java @@ -14,6 +14,7 @@ import io.github.hectorvent.floci.services.appsync.graphql.auth.LambdaAuthorizer import io.github.hectorvent.floci.services.appsync.graphql.auth.LambdaAuthorizerResult; import io.github.hectorvent.floci.services.appsync.model.ApiKey; import io.github.hectorvent.floci.services.appsync.model.AuthenticationType; +import io.github.hectorvent.floci.services.appsync.model.DomainName; import io.github.hectorvent.floci.services.appsync.model.FunctionConfiguration; import io.github.hectorvent.floci.services.appsync.model.GraphqlApi; import io.github.hectorvent.floci.services.appsync.model.SchemaCreationStatus; @@ -33,6 +34,7 @@ import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Answers.RETURNS_DEEP_STUBS; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.clearInvocations; import static org.mockito.Mockito.mock; @@ -47,12 +49,31 @@ class AppSyncServiceTest { private static final Instant NOW = Instant.parse("2026-01-01T00:00:00Z"); private AppSyncService service; private AccountAwareStorageBackend apiKeyStoreOverride; + private String region = "us-east-1"; @BeforeEach void setUp() { service = newService(Clock.fixed(NOW, ZoneOffset.UTC)); } + /** + * AppSync fronts a custom domain with CloudFront, so a DNS alias to {@code appsyncDomainName} + * lives in the partition's CloudFront hosted zone, as the CDK's AppSyncTarget builds it. A + * random zone id made every such alias point nowhere. + */ + @Test + void customDomainIsFrontedByTheCloudFrontZoneOfItsPartition() { + DomainName commercial = service.createDomainName(Map.of("domainName", "api.example.com")); + assertEquals("Z2FDTNDATAQYW2", commercial.getHostedZoneId()); + assertTrue(commercial.getAppsyncDomainName().matches("d[0-9a-f]{13}\\.cloudfront\\.net"), + commercial.getAppsyncDomainName()); + + region = "cn-north-1"; + DomainName china = newService(Clock.fixed(NOW, ZoneOffset.UTC)) + .createDomainName(Map.of("domainName", "api.example.cn")); + assertEquals("Z3RFFRIM2A3IF5", china.getHostedZoneId()); + } + @Test void updateFunctionAppliesANewName() { GraphqlApi api = service.createGraphqlApi( @@ -383,12 +404,13 @@ class AppSyncServiceTest { Instance requestContext = mock(Instance.class); schemaRegistry = mock(SchemaRegistry.class); lambdaAuthorizerCache = new LambdaAuthorizerCache(); - EmulatorConfig config = mock(EmulatorConfig.class); + EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS); when(config.effectiveBaseUrl()).thenReturn(baseUrl); + when(config.services().cloudfront().domainSuffix()).thenReturn("cloudfront.net"); return new AppSyncService( storageFactory, config, - new RegionResolver("us-east-1", "000000000000"), + new RegionResolver(region, "000000000000"), schemaRegistry, mock(SchemaCreationWorker.class), requestContext,