diff --git a/docs/configuration/partitions.md b/docs/configuration/partitions.md index d062c394c0..b84d54d878 100644 --- a/docs/configuration/partitions.md +++ b/docs/configuration/partitions.md @@ -130,6 +130,11 @@ back to `aws` for a label no partition publishes. [GovCloud](https://docs.aws.amazon.com/govcloud-us/latest/UserGuide/govcloud-lambda.html) and [China](https://docs.amazonaws.cn/en_us/aws/latest/userguide/lambda.html) guides list function URLs as not available. + A Cognito user pool custom domain (`CustomDomainConfig`) is refused in GovCloud and EUSC + (`InvalidParameterException`), where the + [GovCloud](https://docs.aws.amazon.com/govcloud-us/latest/UserGuide/govcloud-cog.html) and + [EUSC](https://docs.aws.eu/esc/latest/userguide/cognito.html) guides list custom domains as not + available; prefix domains work there. - **VPC endpoint service names**: interface endpoints (as `DescribeVpcEndpointServices` lists them) are `com.amazonaws..` everywhere, except the (region, service) pairs the CDK lists for China, ISO and EUSC, which reverse the DNS suffix (`cn.com.amazonaws.cn-north-1.lambda`). diff --git a/docs/services/cognito.md b/docs/services/cognito.md index 7d66f551bc..32a84773f8 100644 --- a/docs/services/cognito.md +++ b/docs/services/cognito.md @@ -188,6 +188,14 @@ certificate as soon as it is created, so `https://` verifies without a r [TLS](../configuration/tls.md) for the accepted suffixes. A prefix domain is served under `amazoncognito.com` on AWS, not by Floci, and is left alone. +Custom domains are not available in AWS GovCloud (US) +([GovCloud guide](https://docs.aws.amazon.com/govcloud-us/latest/UserGuide/govcloud-cog.html)) or in +the AWS European Sovereign Cloud +([EUSC guide](https://docs.aws.eu/esc/latest/userguide/cognito.html)), so `CreateUserPoolDomain` and +`UpdateUserPoolDomain` refuse a `CustomDomainConfig` in a GovCloud or EUSC region with +`InvalidParameterException`. Prefix domains work there. See +[AWS Partitions](../configuration/partitions.md). + ### Log Delivery | Action | Description | 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 1482ee78c5..d1c7e13556 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 @@ -9,6 +9,8 @@ import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.config.TlsCertificateManager; 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.AwsRegions; import io.github.hectorvent.floci.core.common.Base64Url; import io.github.hectorvent.floci.core.common.Digests; @@ -1683,6 +1685,8 @@ public class CognitoService implements ResourceProvider { private static final String CERTIFICATE_REGION = "us-east-1"; // partition-literal: CloudFront certificates live in us-east-1 by AWS's rule private static final String CERTIFICATE_NOT_USABLE = "The specified SSL certificate doesn't exist, " + "isn't in us-east-1 region, isn't valid, or doesn't include a valid certificate chain."; // partition-literal: AWS's message text + /** The partitions where AWS documents user pool custom domains as not available. */ + private static final Set CUSTOM_DOMAIN_UNAVAILABLE_PARTITIONS = Set.of("aws-us-gov", "aws-eusc"); /** * Creates either an Amazon Cognito prefix domain ({@code customDomainConfig == null}) @@ -1710,6 +1714,7 @@ public class CognitoService implements ResourceProvider { userPoolDomain.setS3Bucket("aws-cognito-prod-" + regionResolver.getRegion() + "-assets"); if (customDomainConfig != null) { + requireCustomDomainsAvailable(regionResolver.getRegion()); String certificateArn = (String) customDomainConfig.get("CertificateArn"); if (certificateArn == null || certificateArn.isBlank()) { throw new AwsException("InvalidParameterException", @@ -1835,6 +1840,7 @@ public class CognitoService implements ResourceProvider { String previousCertificateArn = userPoolDomain.getCertificateArn(); String certificateArn = previousCertificateArn; if (customDomainConfig != null) { + requireCustomDomainsAvailable(regionResolver.getRegion()); if (!userPoolDomain.isCustomDomain()) { throw new AwsException("InvalidParameterException", "CustomDomainConfig cannot be set on an Amazon Cognito prefix domain", 400); @@ -1900,6 +1906,21 @@ public class CognitoService implements ResourceProvider { } } + /** + * User pool custom domains are not available in AWS GovCloud (US) or in the AWS European + * Sovereign Cloud: both guides say "Custom domains for user pools are not available" + * (https://docs.aws.amazon.com/govcloud-us/latest/UserGuide/govcloud-cog.html, + * https://docs.aws.eu/esc/latest/userguide/cognito.html). Prefix domains are. No source shows + * AWS's message for the refusal, so the wording is Floci's own. + */ + private static void requireCustomDomainsAvailable(String region) { + AwsPartition partition = AwsPartitions.forRegionOrCommercial(region); + if (CUSTOM_DOMAIN_UNAVAILABLE_PARTITIONS.contains(partition.id())) { + throw new AwsException("InvalidParameterException", + "Custom domains for user pools are not available in partition " + partition.id() + ".", 400); + } + } + /** * AWS accepts only an issued ACM certificate from us-east-1 behind a custom domain, and answers * anything else with this one message. diff --git a/src/test/java/io/github/hectorvent/floci/core/common/PartitionRegionGatesIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/PartitionRegionGatesIntegrationTest.java index c8a5d32519..c50d407389 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/PartitionRegionGatesIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/PartitionRegionGatesIntegrationTest.java @@ -181,6 +181,57 @@ class PartitionRegionGatesIntegrationTest { .body("FunctionUrl", containsString(".lambda-url.us-east-1.")); } + private static Response cognito(String region, String action, String body) { + return given() + .header("Authorization", PartitionMatrix.sigV4Auth(region, "cognito-idp")) + .header("X-Amz-Target", "AWSCognitoIdentityProviderService." + action) + .contentType(JSON_1_1) + .body(body) + .when().post("/"); + } + + /** + * GovCloud and the European Sovereign Cloud have user pools but no custom domains for them, so a + * CustomDomainConfig is refused there for what it asks, before its certificate is looked at; a + * prefix domain is not affected. + */ + @Test + void userPoolCustomDomainsAreRejectedInGovCloudAndEusc() { + for (String region : List.of("us-gov-west-1", "eusc-de-east-1")) { + assertCustomDomainsRejected(region); + } + } + + private void assertCustomDomainsRejected(String region) { + String partition = AwsRegions.partitionFor(region); + String certificateArn = "arn:" + partition + ":acm:" + region + ":000000000000:certificate/custom"; + String suffix = Long.toString(System.nanoTime(), 36); + String poolId = cognito(region, "CreateUserPool", "{\"PoolName\":\"domain-gate-" + suffix + "\"}") + .then().statusCode(200) + .extract().jsonPath().getString("UserPool.Id"); + cleanup.register(() -> cognito(region, "DeleteUserPool", "{\"UserPoolId\":\"" + poolId + "\"}")); + + cognito(region, "CreateUserPoolDomain", "{\"UserPoolId\":\"" + poolId + "\"," + + "\"Domain\":\"auth-" + suffix + ".example.com\",\"CustomDomainConfig\":{\"CertificateArn\":" + + "\"" + certificateArn + "\"}}") + .then().statusCode(400) + .body("__type", containsString("InvalidParameterException")) + .body("message", containsString("not available in partition " + partition)); + + String prefix = "gate-" + suffix; + cognito(region, "CreateUserPoolDomain", + "{\"UserPoolId\":\"" + poolId + "\",\"Domain\":\"" + prefix + "\"}") + .then().statusCode(200); + cleanup.register(() -> cognito(region, "DeleteUserPoolDomain", + "{\"UserPoolId\":\"" + poolId + "\",\"Domain\":\"" + prefix + "\"}")); + + cognito(region, "UpdateUserPoolDomain", "{\"UserPoolId\":\"" + poolId + "\"," + + "\"Domain\":\"" + prefix + "\",\"CustomDomainConfig\":{\"CertificateArn\":" + + "\"" + certificateArn + "\"}}") + .then().statusCode(400) + .body("message", containsString("not available in partition " + partition)); + } + @Test void cloudFrontScopeLivesInThePartitionsImplicitGlobalRegion() { String name = "cn-" + Long.toString(System.nanoTime(), 36); diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoCertificateStubs.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoCertificateStubs.java new file mode 100644 index 0000000000..ff3c9e0b17 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoCertificateStubs.java @@ -0,0 +1,34 @@ +package io.github.hectorvent.floci.services.cognito; + +import io.github.hectorvent.floci.services.acm.AcmService; +import io.github.hectorvent.floci.services.acm.model.Certificate; +import io.github.hectorvent.floci.services.acm.model.CertificateStatus; + +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; + +/** + * The ACM answers the user pool domain unit tests share: a custom domain needs an issued certificate + * in us-east-1, so a mocked {@link AcmService} has to describe one before a domain can be created. + */ +final class CognitoCertificateStubs { + + private static final String CERTIFICATE_REGION = "us-east-1"; + + private CognitoCertificateStubs() { + } + + /** Every certificate asked for in us-east-1 exists and is issued, unless a test says otherwise. */ + static void everyCertificateIsIssued(AcmService acmService) { + when(acmService.describeCertificate(anyString(), eq(CERTIFICATE_REGION))) + .thenAnswer(invocation -> issuedCertificate(invocation.getArgument(0))); + } + + static Certificate issuedCertificate(String arn) { + Certificate certificate = new Certificate(); + certificate.setArn(arn); + certificate.setStatus(CertificateStatus.ISSUED); + return certificate; + } +} 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 new file mode 100644 index 0000000000..74d0611cf5 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoCustomDomainPartitionTest.java @@ -0,0 +1,112 @@ +package io.github.hectorvent.floci.services.cognito; + +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.AwsRegions; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.storage.InMemoryStorage; +import io.github.hectorvent.floci.services.acm.AcmService; +import io.github.hectorvent.floci.services.cognito.model.UserPoolDomain; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.Map; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verifyNoInteractions; + +/** + * User pool custom domains are not available in AWS GovCloud (US) or in the AWS European Sovereign + * Cloud, so one cannot be created or configured there, while a prefix domain works in every + * partition that has user pools. + */ +class CognitoCustomDomainPartitionTest { + + private static final String ACCOUNT = "000000000000"; + private static final String CUSTOM_DOMAIN = "auth.example.com"; + private static final Map CUSTOM = + Map.of("CertificateArn", "arn:aws:acm:us-east-1:" + ACCOUNT + ":certificate/abc"); + + private final AcmService acmService = mock(AcmService.class); + + private CognitoService serviceIn(String region) { + CognitoCertificateStubs.everyCertificateIsIssued(acmService); + return new CognitoService( + new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), + new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), + "http://localhost:4566", new RegionResolver(region, ACCOUNT), null, acmService, null, null, null); + } + + private static String createPool(CognitoService service, String region) { + return service.createUserPool(Map.of("PoolName", "domain-pool"), region).getId(); + } + + private static void assertRefused(AwsException error, String region) { + assertEquals("InvalidParameterException", error.getErrorCode()); + assertEquals(400, error.getHttpStatus()); + assertThat(error.getMessage(), + containsString("not available in partition " + AwsRegions.partitionFor(region))); + } + + @ParameterizedTest + @ValueSource(strings = {"us-gov-west-1", "us-gov-east-1", "eusc-de-east-1"}) + void aCustomDomainIsRefusedInGovCloudAndEusc(String region) { + CognitoService service = serviceIn(region); + String poolId = createPool(service, region); + + AwsException error = assertThrows(AwsException.class, + () -> service.createUserPoolDomain(CUSTOM_DOMAIN, poolId, CUSTOM, null)); + + assertRefused(error, region); + assertTrue(service.findCustomDomain(CUSTOM_DOMAIN).isEmpty()); + // Refused before the certificate is looked at. + verifyNoInteractions(acmService); + } + + @ParameterizedTest + @ValueSource(strings = {"us-gov-west-1", "us-gov-east-1", "eusc-de-east-1"}) + void aPrefixDomainCannotBeGivenACustomDomainConfigInGovCloudOrEusc(String region) { + CognitoService service = serviceIn(region); + String poolId = createPool(service, region); + service.createUserPoolDomain("only-prefix", poolId, null, null); + + AwsException error = assertThrows(AwsException.class, + () -> service.updateUserPoolDomain("only-prefix", poolId, CUSTOM, null)); + + assertRefused(error, region); + verifyNoInteractions(acmService); + } + + @ParameterizedTest + @ValueSource(strings = {"us-east-1", "us-gov-west-1", "us-gov-east-1", "eusc-de-east-1"}) + void aPrefixDomainIsAcceptedAndUpdatedInEveryPartition(String region) { + CognitoService service = serviceIn(region); + String poolId = createPool(service, region); + + UserPoolDomain created = service.createUserPoolDomain("my-prefix", poolId, null, 1); + assertFalse(created.isCustomDomain()); + + UserPoolDomain updated = service.updateUserPoolDomain("my-prefix", poolId, null, 2); + assertEquals(2, updated.getManagedLoginVersion()); + } + + @Test + void aCustomDomainIsAcceptedInTheCommercialPartition() { + CognitoService service = serviceIn("us-east-1"); + String poolId = createPool(service, "us-east-1"); + + UserPoolDomain created = service.createUserPoolDomain(CUSTOM_DOMAIN, poolId, CUSTOM, null); + assertTrue(created.isCustomDomain()); + + Map replacement = + Map.of("CertificateArn", "arn:aws:acm:us-east-1:" + ACCOUNT + ":certificate/def"); + UserPoolDomain updated = service.updateUserPoolDomain(CUSTOM_DOMAIN, poolId, replacement, null); + assertEquals(replacement.get("CertificateArn"), updated.getCertificateArn()); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoDomainTlsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoDomainTlsServiceTest.java index 25272d05ea..322b0330e1 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoDomainTlsServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoDomainTlsServiceTest.java @@ -49,13 +49,7 @@ class CognitoDomainTlsServiceTest { @BeforeEach void setUp() { - // Every certificate exists and is issued unless a test says otherwise. - when(acmService.describeCertificate(anyString(), eq(REGION))).thenAnswer(invocation -> { - Certificate certificate = new Certificate(); - certificate.setArn(invocation.getArgument(0)); - certificate.setStatus(CertificateStatus.ISSUED); - return certificate; - }); + CognitoCertificateStubs.everyCertificateIsIssued(acmService); service = new CognitoService( new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoServiceTest.java index ab4bf2bc23..1d9605e4d4 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoServiceTest.java @@ -101,9 +101,7 @@ class CognitoServiceTest { revokedTokenStore = new InMemoryStorage<>(); regionResolver = new RegionResolver("us-east-1", "000000000000"); acmService = mock(AcmService.class); - // Every certificate exists and is issued unless a test says otherwise. - when(acmService.describeCertificate(anyString(), eq("us-east-1"))) - .thenAnswer(inv -> issuedCertificate(inv.getArgument(0))); + CognitoCertificateStubs.everyCertificateIsIssued(acmService); service = new CognitoService( poolStore, new InMemoryStorage<>(), @@ -6172,13 +6170,6 @@ class CognitoServiceTest { private static final String AWS_CERTIFICATE_MESSAGE = "The specified SSL certificate doesn't exist, " + "isn't in us-east-1 region, isn't valid, or doesn't include a valid certificate chain."; - private static Certificate issuedCertificate(String arn) { - Certificate certificate = new Certificate(); - certificate.setArn(arn); - certificate.setStatus(CertificateStatus.ISSUED); - return certificate; - } - /** The ARN a domain registers on its certificate: the CloudFront distribution serving it. */ private String consumerArn(String domain) { String distribution = service.describeUserPoolDomain(domain).getCloudFrontDistribution(); @@ -6217,7 +6208,7 @@ class CognitoServiceTest { @Test void createUserPoolDomainRejectsACertificateThatIsNotIssued() { UserPool pool = service.createUserPool(Map.of("PoolName", "DomainPool"), "us-east-1"); - Certificate pending = issuedCertificate(CERTIFICATE_ARN); + Certificate pending = CognitoCertificateStubs.issuedCertificate(CERTIFICATE_ARN); pending.setStatus(CertificateStatus.PENDING_VALIDATION); when(acmService.describeCertificate(CERTIFICATE_ARN, "us-east-1")).thenReturn(pending);