From 86f8434ef6e72fd77ccbe8f6af4513313bd47c28 Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Wed, 4 Nov 2026 03:17:53 -0600 Subject: [PATCH] fix(cognito): name a custom domain's CloudFront distribution like the others (#7486) CreateUserPoolDomain built its CloudFrontDistribution by hand from 14 hex characters with no leading d, unlike the names an edge-optimized API Gateway domain and an AppSync custom domain get. Use CloudFrontEdgeDomain.create, so the name is d plus 13 hex on the partition's suffix. The name is stored, so existing domains keep theirs. Signed-off-by: Hector Ventura --- .../floci/services/cognito/CognitoService.java | 9 ++------- .../cognito/CognitoCustomDomainPartitionTest.java | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoService.java b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoService.java index f013283efe..7d0d18f3b5 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoService.java +++ b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoService.java @@ -1865,7 +1865,8 @@ public class CognitoService implements ResourceProvider { userPoolDomain.setCertificateArn(certificateArn); Object securityPolicy = customDomainConfig.get("SecurityPolicy"); userPoolDomain.setSecurityPolicy(securityPolicy != null ? securityPolicy.toString() : "TLS_V1_2_2021"); - userPoolDomain.setCloudFrontDistribution(generateCloudFrontDomain()); + userPoolDomain.setCloudFrontDistribution( + CloudFrontEdgeDomain.create(regionResolver.getPartition(), cloudFrontDomainSuffix).domainName()); } // Check and write as one step: two accounts racing for one name write to different @@ -2106,12 +2107,6 @@ public class CognitoService implements ResourceProvider { return AwsArnUtils.Arn.global(partition, "cloudfront", userPoolDomain.getAwsAccountId(), "distribution/" + id).toString(); } - /** A distribution name on the configured CloudFront suffix, or the request partition's when none is. */ - private String generateCloudFrontDomain() { - return UUID.randomUUID().toString().replace("-", "").substring(0, 14) + "." - + CloudFrontEdgeDomain.domainSuffix(cloudFrontDomainSuffix, regionResolver.getPartition()); - } - private String generateDomainVersion() { return DateTimeFormatter.ofPattern("yyyyMMddHHmmss") .withZone(ZoneOffset.UTC) diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoCustomDomainPartitionTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoCustomDomainPartitionTest.java index 05a00b3ef9..4b7611ef61 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoCustomDomainPartitionTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoCustomDomainPartitionTest.java @@ -14,6 +14,7 @@ import java.util.Map; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.matchesPattern; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -103,6 +104,7 @@ class CognitoCustomDomainPartitionTest { UserPoolDomain created = service.createUserPoolDomain(CUSTOM_DOMAIN, poolId, CUSTOM, null); assertTrue(created.isCustomDomain()); + assertThat(created.getCloudFrontDistribution(), matchesPattern("d[0-9a-f]{13}\\.cloudfront\\.net")); Map replacement = Map.of("CertificateArn", "arn:aws:acm:us-east-1:" + ACCOUNT + ":certificate/def"); @@ -110,6 +112,18 @@ class CognitoCustomDomainPartitionTest { assertEquals(replacement.get("CertificateArn"), updated.getCertificateArn()); } + @Test + void aCustomDomainInChinaIsServedFromTheChinaCloudFrontSuffix() { + CognitoService service = serviceIn("cn-north-1"); + String poolId = createPool(service, "cn-north-1"); + Map certificate = + Map.of("CertificateArn", "arn:aws-cn:acm:us-east-1:" + ACCOUNT + ":certificate/abc"); + + UserPoolDomain created = service.createUserPoolDomain(CUSTOM_DOMAIN, poolId, certificate, null); + + assertThat(created.getCloudFrontDistribution(), matchesPattern("d[0-9a-f]{13}\\.cloudfront\\.cn")); + } + @ParameterizedTest @ValueSource(strings = { "arn:aws-cn:acm:us-east-1:000000000000:certificate/abc",