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.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-07 09:34:02 -04:00
1 parent 73133b472a
commit 9d49e619c1
7 files changed
+99 -16

No files matched your search

@@ -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");
}
}
@@ -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);
@@ -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");
}
@@ -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<ApiKey> 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> 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,