Light, fluffy, and always free - The AWS Local Emulator alternative
fix(cognito): keep tag operations and DescribeUserPoolDomain in the pool's region (#5133)
Hector Ventura899211e49822
diff --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 ee3f8ec9e5..2f162518fc 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@@ -46,6 +46,16 @@ public class CognitoJsonHandler {if (region != null && request.hasNonNull("UserPoolId") && !"UpdateAuthEventFeedback".equals(action)) {service.requireUserPoolInRegion(request.path("UserPoolId").asText(), region);}+ // The tag operations name the pool by ARN, and a domain is looked up by its name.+ if (region != null) {+ switch (action) {+ case "TagResource", "UntagResource", "ListTagsForResource" ->+ service.requireUserPoolArnInRegion(request.path("ResourceArn").asText(), region);+ case "DescribeUserPoolDomain" ->+ service.requireUserPoolDomainInRegion(request.path("Domain").asText(), region);+ default -> { }+ }+ }return switch (action) {case "CreateUserPool" -> handleCreateUserPool(request, region);case "DescribeUserPool" -> handleDescribeUserPool(request);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 7fdb3ff7c9..1b10d799d5 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@@ -765,6 +765,27 @@ public class CognitoService implements ResourceProvider {});}+ /** {@link #requireUserPoolInRegion} for an operation that names the pool by its ARN, as tagging does. */+ public void requireUserPoolArnInRegion(String resourceArn, String region) {+ requireUserPoolInRegion(extractUserPoolIdFromArn(resourceArn), region);+ }++ /**+ * Refuses a domain whose user pool lives in another region, as the same not found an unknown+ * domain gets: domains are stored by name alone, but a domain belongs to its pool's region.+ */+ public void requireUserPoolDomainInRegion(String domain, String region) {+ if (domain == null || domain.isBlank()) {+ return;+ }+ domainStore.get(domain).map(UserPoolDomain::getUserPoolId).flatMap(poolStore::get).ifPresent(pool -> {+ String poolRegion = poolRegion(pool);+ if (poolRegion != null && !poolRegion.equals(region)) {+ throw new AwsException("ResourceNotFoundException", "Domain does not exist", 400);+ }+ });+ }+/** Pools are stored by id for every region; the region comes from the pool ARN, else the id prefix. */private static String poolRegion(UserPool pool) {if (pool.getArn() != null && AwsArnUtils.isArn(pool.getArn())) {@@ -798,9 +819,14 @@ public class CognitoService implements ResourceProvider {return Set.of(new SupportedResourceType("cognito-idp:userpool", "cognito-idp", true));}+ /** The ARN must name the pool itself: the same id under another region or account is another pool. */private UserPool describeUserPoolByArn(String resourceArn) {String poolId = extractUserPoolIdFromArn(resourceArn);- return describeUserPool(poolId);+ UserPool pool = describeUserPool(poolId);+ if (pool.getArn() != null && !pool.getArn().equals(resourceArn)) {+ throw userPoolNotFound(poolId);+ }+ return pool;}public void tagResource(String resourceArn, Map<String, String> tags) {@@ -1658,7 +1684,7 @@ public class CognitoService implements ResourceProvider {throw new AwsException("InvalidParameterException", "Domain is required", 400);}return domainStore.get(domain)- .orElseThrow(() -> new AwsException("ResourceNotFoundException", "Domain does not exist", 404));+ .orElseThrow(() -> new AwsException("ResourceNotFoundException", "Domain does not exist", 400));}/**@@ -1713,7 +1739,7 @@ public class CognitoService implements ResourceProvider {describeUserPool(userPoolId);UserPoolDomain userPoolDomain = describeUserPoolDomain(domain);if (!userPoolDomain.getUserPoolId().equals(userPoolId)) {- throw new AwsException("ResourceNotFoundException", "Domain does not exist", 404);+ throw new AwsException("ResourceNotFoundException", "Domain does not exist", 400);}String previousCertificateArn = userPoolDomain.getCertificateArn();String certificateArn = previousCertificateArn;@@ -1758,7 +1784,7 @@ public class CognitoService implements ResourceProvider {public void deleteUserPoolDomain(String domain, String userPoolId) {UserPoolDomain userPoolDomain = describeUserPoolDomain(domain);if (!userPoolDomain.getUserPoolId().equals(userPoolId)) {- throw new AwsException("ResourceNotFoundException", "Domain does not exist", 404);+ throw new AwsException("ResourceNotFoundException", "Domain does not exist", 400);}domainStore.delete(domain);if (userPoolDomain.isCustomDomain()) {diff --git a/src/test/java/io/github/hectorvent/floci/core/common/RegionScopedListsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/RegionScopedListsIntegrationTest.javaindex 21dd3c1dee..c7921232c3 100644--- a/src/test/java/io/github/hectorvent/floci/core/common/RegionScopedListsIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/core/common/RegionScopedListsIntegrationTest.java@@ -2,9 +2,11 @@ package io.github.hectorvent.floci.core.common;import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;import io.quarkus.test.junit.QuarkusTest;+import io.restassured.path.json.JsonPath;import org.junit.jupiter.api.BeforeAll;import org.junit.jupiter.api.Test;+import java.util.List;import java.util.UUID;import static io.restassured.RestAssured.given;@@ -216,4 +218,81 @@ class RegionScopedListsIntegrationTest {.contentType(COGNITO_JSON).body("{\"UserPoolId\": \"" + poolId + "\"}").when().post("/").then().statusCode(200);}++ @Test+ void cognitoTagsAndDomainsStayInTheirPoolsRegion() {+ String suffix = UUID.randomUUID().toString().substring(0, 8);+ JsonPath pool = given().header("Authorization", auth(HOME, "cognito-idp"))+ .header("X-Amz-Target", "AWSCognitoIdentityProviderService.CreateUserPool")+ .contentType(COGNITO_JSON)+ .body("{\"PoolName\": \"region-tags-" + suffix + "\", \"UserPoolTags\": {\"team\": \"home\"}}")+ .when().post("/").then().statusCode(200)+ .extract().jsonPath();+ String poolId = pool.getString("UserPool.Id");+ String poolArn = pool.getString("UserPool.Arn");+ String domain = "region-domain-" + suffix;+ given().header("Authorization", auth(HOME, "cognito-idp"))+ .header("X-Amz-Target", "AWSCognitoIdentityProviderService.CreateUserPoolDomain")+ .contentType(COGNITO_JSON)+ .body("{\"UserPoolId\": \"" + poolId + "\", \"Domain\": \"" + domain + "\"}")+ .when().post("/").then().statusCode(200);++ // Another region cannot read or change the pool's tags, or describe its domain.+ given().header("Authorization", auth(OTHER, "cognito-idp"))+ .header("X-Amz-Target", "AWSCognitoIdentityProviderService.TagResource")+ .contentType(COGNITO_JSON)+ .body("{\"ResourceArn\": \"" + poolArn + "\", \"Tags\": {\"team\": \"other\"}}")+ .when().post("/").then().statusCode(400)+ .body("__type", endsWith("ResourceNotFoundException"));+ given().header("Authorization", auth(OTHER, "cognito-idp"))+ .header("X-Amz-Target", "AWSCognitoIdentityProviderService.UntagResource")+ .contentType(COGNITO_JSON)+ .body("{\"ResourceArn\": \"" + poolArn + "\", \"TagKeys\": [\"team\"]}")+ .when().post("/").then().statusCode(400)+ .body("__type", endsWith("ResourceNotFoundException"));+ given().header("Authorization", auth(OTHER, "cognito-idp"))+ .header("X-Amz-Target", "AWSCognitoIdentityProviderService.ListTagsForResource")+ .contentType(COGNITO_JSON).body("{\"ResourceArn\": \"" + poolArn + "\"}")+ .when().post("/").then().statusCode(400)+ .body("__type", endsWith("ResourceNotFoundException"));+ given().header("Authorization", auth(OTHER, "cognito-idp"))+ .header("X-Amz-Target", "AWSCognitoIdentityProviderService.DescribeUserPoolDomain")+ .contentType(COGNITO_JSON).body("{\"Domain\": \"" + domain + "\"}")+ .when().post("/").then().statusCode(400)+ .body("__type", endsWith("ResourceNotFoundException"));++ // An ARN naming the pool's id under another region or account names another pool.+ String[] arn = poolArn.split(":", 6);+ String otherRegionArn = String.join(":", arn[0], arn[1], arn[2], OTHER, arn[4], arn[5]);+ String otherAccountArn = String.join(":", arn[0], arn[1], arn[2], arn[3], "111122223333", arn[5]);+ for (String foreignArn : List.of(otherRegionArn, otherAccountArn)) {+ given().header("Authorization", auth(HOME, "cognito-idp"))+ .header("X-Amz-Target", "AWSCognitoIdentityProviderService.TagResource")+ .contentType(COGNITO_JSON)+ .body("{\"ResourceArn\": \"" + foreignArn + "\", \"Tags\": {\"team\": \"other\"}}")+ .when().post("/").then().statusCode(400)+ .body("__type", endsWith("ResourceNotFoundException"));+ }++ given().header("Authorization", auth(HOME, "cognito-idp"))+ .header("X-Amz-Target", "AWSCognitoIdentityProviderService.ListTagsForResource")+ .contentType(COGNITO_JSON).body("{\"ResourceArn\": \"" + poolArn + "\"}")+ .when().post("/").then().statusCode(200)+ .body("Tags.team", equalTo("home"));+ given().header("Authorization", auth(HOME, "cognito-idp"))+ .header("X-Amz-Target", "AWSCognitoIdentityProviderService.DescribeUserPoolDomain")+ .contentType(COGNITO_JSON).body("{\"Domain\": \"" + domain + "\"}")+ .when().post("/").then().statusCode(200)+ .body("DomainDescription.UserPoolId", equalTo(poolId));++ given().header("Authorization", auth(HOME, "cognito-idp"))+ .header("X-Amz-Target", "AWSCognitoIdentityProviderService.DeleteUserPoolDomain")+ .contentType(COGNITO_JSON)+ .body("{\"UserPoolId\": \"" + poolId + "\", \"Domain\": \"" + domain + "\"}")+ .when().post("/").then().statusCode(200);+ given().header("Authorization", auth(HOME, "cognito-idp"))+ .header("X-Amz-Target", "AWSCognitoIdentityProviderService.DeleteUserPool")+ .contentType(COGNITO_JSON).body("{\"UserPoolId\": \"" + poolId + "\"}")+ .when().post("/").then().statusCode(200);+ }}diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CognitoCfnIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CognitoCfnIntegrationTest.javaindex a757c784fc..f20d27f9e8 100644--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CognitoCfnIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CognitoCfnIntegrationTest.java@@ -513,7 +513,7 @@ class CognitoCfnIntegrationTest {private static void assertDomainIsGone(String domain) {cognitoAction("DescribeUserPoolDomain", "{\"Domain\": \"" + domain + "\"}").then()- .statusCode(404)+ .statusCode(400).body("__type", equalTo("ResourceNotFoundException"));}diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CognitoCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CognitoCfnProvisionerTest.javaindex cf15c28847..890b50a24b 100644--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CognitoCfnProvisionerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CognitoCfnProvisionerTest.java@@ -294,7 +294,7 @@ class CognitoCfnProvisionerTest {@Testvoid updateWhosePriorDomainIsGoneCreatesItAgain() {when(cognito.describeUserPoolDomain(DOMAIN))- .thenThrow(new AwsException("ResourceNotFoundException", "Domain does not exist", 404));+ .thenThrow(new AwsException("ResourceNotFoundException", "Domain does not exist", 400));when(cognito.createUserPoolDomain(DOMAIN, POOL_ID, Map.of("CertificateArn", CERTIFICATE_ARN), 2)).thenReturn(domain(DOMAIN, POOL_ID, CLOUDFRONT));StackResource r = resource(DOMAIN, Map.of("UserPoolId", POOL_ID, "CloudFrontDistribution", CLOUDFRONT));@@ -311,7 +311,7 @@ class CognitoCfnProvisionerTest {when(cognito.describeUserPoolDomain("old.example.com")).thenReturn(domain("old.example.com", POOL_ID, "dold.cloudfront.net"));when(cognito.createUserPoolDomain(any(), any(), any(), any())).thenReturn(domain(DOMAIN, POOL_ID, CLOUDFRONT));- doThrow(new AwsException("ResourceNotFoundException", "Domain does not exist", 404))+ doThrow(new AwsException("ResourceNotFoundException", "Domain does not exist", 400)).when(cognito).deleteUserPoolDomain("old.example.com", POOL_ID);StackResource r = resource("old.example.com", Map.of("UserPoolId", POOL_ID));@@ -329,7 +329,7 @@ class CognitoCfnProvisionerTest {@Testvoid deleteToleratesAnAlreadyDeletedDomain() {- doThrow(new AwsException("ResourceNotFoundException", "Domain does not exist", 404))+ doThrow(new AwsException("ResourceNotFoundException", "Domain does not exist", 400)).when(cognito).deleteUserPoolDomain(DOMAIN, POOL_ID);assertDoesNotThrow(() -> provisioner.delete(resource(DOMAIN, Map.of("UserPoolId", POOL_ID)), REGION));@@ -356,7 +356,7 @@ class CognitoCfnProvisionerTest {@Testvoid deleteWithoutARecordedUserPoolIdOfAMissingDomainIsANoOp() {when(cognito.describeUserPoolDomain(DOMAIN))- .thenThrow(new AwsException("ResourceNotFoundException", "Domain does not exist", 404));+ .thenThrow(new AwsException("ResourceNotFoundException", "Domain does not exist", 400));assertDoesNotThrow(() -> provisioner.delete(resource(DOMAIN, Map.of()), REGION));verify(cognito, never()).deleteUserPoolDomain(any(), any());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 788a99a7a0..a30204bd25 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@@ -125,7 +125,7 @@ class CognitoUserPoolDomainIntegrationTest {}""".formatted(prefixDomain)).then()- .statusCode(404)+ .statusCode(400).body("__type", equalTo("ResourceNotFoundException"));}@@ -292,7 +292,7 @@ class CognitoUserPoolDomainIntegrationTest {}""".formatted(customDomain)).then()- .statusCode(404)+ .statusCode(400).body("__type", equalTo("ResourceNotFoundException"));}