Light, fluffy, and always free - The AWS Local Emulator alternative
fix(cognito): include domains in user pool responses (#5448)
Som Samantray34c11b898190
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CognitoFeaturesTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CognitoFeaturesTest.javaindex 2546f018a4..0c14a8eddf 100644--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CognitoFeaturesTest.java+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CognitoFeaturesTest.java@@ -454,6 +454,21 @@ class CognitoFeaturesTest {assertThat(sub.mutable()).isFalse();}+ @Test+ @Order(56)+ void describeUserPoolReturnsItsPrefixDomain() {+ String domain = "compat-" + UUID.randomUUID().toString().replace("-", "").substring(0, 20);+ cognito.createUserPoolDomain(b -> b.domain(domain).userPoolId(poolId));++ try {+ UserPoolType userPool = cognito.describeUserPool(b -> b.userPoolId(poolId)).userPool();+ assertThat(userPool.domain()).isEqualTo(domain);+ assertThat(userPool.customDomain()).isNull();+ } finally {+ cognito.deleteUserPoolDomain(b -> b.domain(domain).userPoolId(poolId));+ }+ }+// ── AdminRespondToAuthChallenge ─────────────────────────────────────────@Testdiff --git a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoJsonHandler.javaindex be9d7d04a1..7fb6470683 100644--- a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoJsonHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoJsonHandler.java@@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode;import com.fasterxml.jackson.databind.node.ObjectNode;import io.github.hectorvent.floci.core.common.AwsErrorResponse;import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.services.cognito.CognitoService.UserPoolDomainNames;import io.github.hectorvent.floci.services.cognito.model.CognitoGroup;import io.github.hectorvent.floci.services.cognito.model.CognitoUser;import io.github.hectorvent.floci.services.cognito.model.IdentityProvider;@@ -1132,6 +1133,14 @@ public class CognitoJsonHandler {node.put("CreationDate", (double) p.getCreationDate());node.put("LastModifiedDate", (double) p.getLastModifiedDate());+ UserPoolDomainNames domains = service.findDomainNamesForPool(p.getId());+ if (domains.domain() != null) {+ node.put("Domain", domains.domain());+ }+ if (domains.customDomain() != null) {+ node.put("CustomDomain", domains.customDomain());+ }+node.set("Policies", objectMapper.valueToTree(p.getPolicies() != null ? p.getPolicies() : new HashMap<>()));node.put("DeletionProtection", p.getDeletionProtection() != null ? p.getDeletionProtection() : "INACTIVE");node.set("LambdaConfig", objectMapper.valueToTree(p.getLambdaConfig() != null ? p.getLambdaConfig() : new HashMap<>()));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.javaindex 99765466af..649b714fe9 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@@ -1729,6 +1729,32 @@ public class CognitoService implements ResourceProvider {.findFirst();}+ public UserPoolDomainNames findDomainNamesForPool(String poolId) {+ String domain = null;+ String customDomain = null;+ List<UserPoolDomain> domains;+ if (domainStore instanceof AccountAwareStorageBackend<UserPoolDomain> aware) {+ domains = new ArrayList<>(aware.scanForAccount(regionResolver.getAccountId(), key -> true));+ domains.addAll(aware.scanUnscopedLegacy(+ userPoolDomain -> regionResolver.getAccountId().equals(userPoolDomain.getAwsAccountId())));+ } else {+ domains = domainStore.scan(key -> true);+ }+ for (UserPoolDomain userPoolDomain : domains) {+ if (!poolId.equals(userPoolDomain.getUserPoolId())) {+ continue;+ }+ if (userPoolDomain.isCustomDomain() && customDomain == null) {+ customDomain = userPoolDomain.getDomain();+ } else if (!userPoolDomain.isCustomDomain() && domain == null) {+ domain = userPoolDomain.getDomain();+ }+ }+ return new UserPoolDomainNames(domain, customDomain);+ }++ public record UserPoolDomainNames(String domain, String customDomain) { }+private List<UserPoolDomain> allDomains() {return domainStore instanceof AccountAwareStorageBackend<UserPoolDomain> aware? aware.scanAllAccounts()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.javaindex f9f64d3e2d..3637abef14 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@@ -8,6 +8,7 @@ import io.github.hectorvent.floci.config.TlsCertificateManager;import io.github.hectorvent.floci.core.common.AwsException;import io.github.hectorvent.floci.core.common.RegionResolver;import io.github.hectorvent.floci.core.common.ReservedTags;+import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;import io.github.hectorvent.floci.core.storage.InMemoryStorage;import io.github.hectorvent.floci.services.acm.AcmService;import io.github.hectorvent.floci.services.acm.model.Certificate;@@ -5821,6 +5822,33 @@ class CognitoServiceTest {return pool;}+ @Test+ void findDomainNamesForPoolIncludesOwnedLegacyDomainRecords() {+ InMemoryStorage<String, UserPoolDomain> legacyStore = new InMemoryStorage<>();+ UserPoolDomain legacyDomain = new UserPoolDomain();+ legacyDomain.setDomain("legacy-prefix");+ legacyDomain.setUserPoolId("pool-id");+ legacyDomain.setAwsAccountId("000000000000");+ legacyStore.put("legacy-prefix", legacyDomain);+ UserPoolDomain otherAccountDomain = new UserPoolDomain();+ otherAccountDomain.setDomain("other-prefix");+ otherAccountDomain.setUserPoolId("pool-id");+ otherAccountDomain.setAwsAccountId("111111111111");+ legacyStore.put("other-prefix", otherAccountDomain);++ CognitoService legacyService = new CognitoService(+ new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),+ new AccountAwareStorageBackend<>(legacyStore, null, "000000000000"), new InMemoryStorage<>(),+ new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),+ "http://localhost:4566", "cloudfront.net", regionResolver, null, acmService,+ null, null, null);++ CognitoService.UserPoolDomainNames domainNames = legacyService.findDomainNamesForPool("pool-id");++ assertEquals("legacy-prefix", domainNames.domain());+ assertNull(domainNames.customDomain());+ }+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.";diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoUserPoolDomainIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoUserPoolDomainIntegrationTest.javaindex a30204bd25..7750e7fba5 100644--- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoUserPoolDomainIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoUserPoolDomainIntegrationTest.java@@ -3,6 +3,8 @@ package io.github.hectorvent.floci.services.cognito;import com.fasterxml.jackson.databind.JsonNode;import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;import io.quarkus.test.junit.QuarkusTest;+import io.quarkus.test.junit.QuarkusTestProfile;+import io.quarkus.test.junit.TestProfile;import org.junit.jupiter.api.BeforeAll;import org.junit.jupiter.api.MethodOrderer;import org.junit.jupiter.api.Order;@@ -13,6 +15,7 @@ import java.util.UUID;import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoAction;import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoJson;+import static io.restassured.RestAssured.given;import static org.hamcrest.Matchers.equalTo;import static org.hamcrest.Matchers.startsWith;import static org.junit.jupiter.api.Assertions.assertEquals;@@ -24,9 +27,13 @@ import static org.junit.jupiter.api.Assertions.assertNull;* the certificate's InUseBy bookkeeping in ACM.*/@QuarkusTest+@TestProfile(CognitoUserPoolDomainIntegrationTest.DomainResetIsolationProfile.class)@TestMethodOrder(MethodOrderer.OrderAnnotation.class)class CognitoUserPoolDomainIntegrationTest {+ /** HTTP state reset clears every service, so isolate this suite from other Quarkus tests. */+ public static class DomainResetIsolationProfile implements QuarkusTestProfile {}+private static String poolId;private static String prefixDomain;private static String customDomain;@@ -49,6 +56,8 @@ class CognitoUserPoolDomainIntegrationTest {}""");poolId = poolResponse.path("UserPool").path("Id").asText();+ assertNull(poolResponse.path("UserPool").get("Domain"));+ assertNull(poolResponse.path("UserPool").get("CustomDomain"));prefixDomain = "floci-test-" + UUID.randomUUID().toString().substring(0, 8);customDomain = "auth-" + UUID.randomUUID().toString().substring(0, 8) + ".example.com";CERTIFICATE_ARN = requestCertificate(customDomain);@@ -101,11 +110,19 @@ class CognitoUserPoolDomainIntegrationTest {assertEquals(poolId, description.path("UserPoolId").asText());assertEquals("ACTIVE", description.path("Status").asText());assertNull(description.get("CustomDomainConfig"));++ JsonNode userPool = cognitoJson("DescribeUserPool", """+ {+ "UserPoolId": "%s"+ }+ """.formatted(poolId)).path("UserPool");+ assertEquals(prefixDomain, userPool.path("Domain").asText());+ assertNull(userPool.get("CustomDomain"));}@Test@Order(4)- void deletePrefixDomainSucceeds() {+ void deletePrefixDomainSucceeds() throws Exception {cognitoAction("DeleteUserPoolDomain", """{"Domain": "%s",@@ -114,6 +131,13 @@ class CognitoUserPoolDomainIntegrationTest {""".formatted(prefixDomain, poolId)).then().statusCode(200);++ JsonNode userPool = cognitoJson("DescribeUserPool", """+ {+ "UserPoolId": "%s"+ }+ """.formatted(poolId)).path("UserPool");+ assertNull(userPool.get("Domain"));}@Test@@ -161,6 +185,33 @@ class CognitoUserPoolDomainIntegrationTest {assertEquals(CERTIFICATE_ARN, description.path("CustomDomainConfig").path("CertificateArn").asText());assertEquals("ACTIVE", description.path("Status").asText());org.junit.jupiter.api.Assertions.assertNotNull(description.path("CloudFrontDistribution").asText(null));++ String secondPrefixDomain = prefixDomain + "-again";+ cognitoJson("CreateUserPoolDomain", """+ {+ "Domain": "%s",+ "UserPoolId": "%s"+ }+ """.formatted(secondPrefixDomain, poolId));+ try {+ JsonNode userPool = cognitoJson("DescribeUserPool", """+ {+ "UserPoolId": "%s"+ }+ """.formatted(poolId)).path("UserPool");+ assertEquals(secondPrefixDomain, userPool.path("Domain").asText());+ assertEquals(customDomain, userPool.path("CustomDomain").asText());++ } finally {+ cognitoAction("DeleteUserPoolDomain", """+ {+ "Domain": "%s",+ "UserPoolId": "%s"+ }+ """.formatted(secondPrefixDomain, poolId))+ .then()+ .statusCode(200);+ }}@Test@@ -353,4 +404,100 @@ class CognitoUserPoolDomainIntegrationTest {.then().statusCode(200);}++ @Test+ @Order(18)+ void samePoolIdInDifferentAccountsReturnsOnlyThatAccountsDomain() throws Exception {+ String accountB = "111122223333";+ String sharedPoolId = "us-east-1_shared" + UUID.randomUUID().toString().substring(0, 8);+ String defaultDomain = "floci-default-" + UUID.randomUUID().toString().substring(0, 8);+ String accountBDomain = "floci-accountb-" + UUID.randomUUID().toString().substring(0, 8);+ cognitoJson("CreateUserPool", """+ {+ "PoolName": "DefaultAccountSharedPool",+ "UserPoolTags": { "floci:override-id": "%s" }+ }+ """.formatted(sharedPoolId));+ CognitoCustomDomainFixtures.awsJsonAs(accountB, CognitoCustomDomainFixtures.COGNITO, "CreateUserPool", """+ {+ "PoolName": "AccountBSharedPool",+ "UserPoolTags": { "floci:override-id": "%s" }+ }+ """.formatted(sharedPoolId));++ try {+ cognitoJson("CreateUserPoolDomain", """+ { "Domain": "%s", "UserPoolId": "%s" }+ """.formatted(defaultDomain, sharedPoolId));+ CognitoCustomDomainFixtures.awsJsonAs(accountB, CognitoCustomDomainFixtures.COGNITO,+ "CreateUserPoolDomain", """+ { "Domain": "%s", "UserPoolId": "%s" }+ """.formatted(accountBDomain, sharedPoolId));++ JsonNode defaultPool = cognitoJson("DescribeUserPool", """+ { "UserPoolId": "%s" }+ """.formatted(sharedPoolId)).path("UserPool");+ JsonNode accountBPool = CognitoCustomDomainFixtures.awsJsonAs(accountB,+ CognitoCustomDomainFixtures.COGNITO, "DescribeUserPool", """+ { "UserPoolId": "%s" }+ """.formatted(sharedPoolId)).path("UserPool");+ assertEquals(defaultDomain, defaultPool.path("Domain").asText());+ assertEquals(accountBDomain, accountBPool.path("Domain").asText());+ } finally {+ CognitoCustomDomainFixtures.awsActionAs(accountB, CognitoCustomDomainFixtures.COGNITO,+ "DeleteUserPoolDomain", """+ { "Domain": "%s", "UserPoolId": "%s" }+ """.formatted(accountBDomain, sharedPoolId)).then().statusCode(200);+ cognitoAction("DeleteUserPoolDomain", """+ { "Domain": "%s", "UserPoolId": "%s" }+ """.formatted(defaultDomain, sharedPoolId)).then().statusCode(200);+ CognitoCustomDomainFixtures.awsActionAs(accountB, CognitoCustomDomainFixtures.COGNITO,+ "DeleteUserPool", """+ { "UserPoolId": "%s" }+ """.formatted(sharedPoolId)).then().statusCode(200);+ cognitoAction("DeleteUserPool", """+ { "UserPoolId": "%s" }+ """.formatted(sharedPoolId)).then().statusCode(200);+ }+ }++ @Test+ @Order(19)+ void recreatedPoolDoesNotInheritDomainAfterStateReset() throws Exception {+ String pinnedPoolId = "us-east-1_reset" + UUID.randomUUID().toString().substring(0, 8);+ String resetDomain = "floci-reset-" + UUID.randomUUID().toString().substring(0, 8);++ try {+ cognitoJson("CreateUserPool", """+ {+ "PoolName": "DomainResetPool",+ "UserPoolTags": { "floci:override-id": "%s" }+ }+ """.formatted(pinnedPoolId));+ cognitoJson("CreateUserPoolDomain", """+ {+ "Domain": "%s",+ "UserPoolId": "%s"+ }+ """.formatted(resetDomain, pinnedPoolId));++ assertEquals(resetDomain, cognitoJson("DescribeUserPool", """+ { "UserPoolId": "%s" }+ """.formatted(pinnedPoolId)).path("UserPool").path("Domain").asText());++ given().when().post("/_floci/state/reset").then().statusCode(200);++ JsonNode recreatedPool = cognitoJson("CreateUserPool", """+ {+ "PoolName": "DomainResetPoolAgain",+ "UserPoolTags": { "floci:override-id": "%s" }+ }+ """.formatted(pinnedPoolId)).path("UserPool");++ assertNull(recreatedPool.get("Domain"), "a reset pool must not inherit its old domain");+ assertNull(recreatedPool.get("CustomDomain"));+ } finally {+ given().when().post("/_floci/state/reset").then().statusCode(200);+ }+ }}