mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cognito): keep tag operations and DescribeUserPoolDomain in the pool's region (#5133)
* fix(cognito): keep tag operations and DescribeUserPoolDomain in the pool's region The region guard for user pools checked only requests naming a UserPoolId. TagResource, UntagResource and ListTagsForResource name the pool by ARN, and DescribeUserPoolDomain looks a domain up by its name in a store keyed by name alone, so another region could still read or change a pool's tags and describe its domain. The handler now applies the same guard to the pool an ARN names, and refuses a domain whose pool lives in another region with the not found an unknown domain gets. * fix(cognito): answer a missing user pool domain with 400 and match the tagged pool's full ARN ResourceNotFoundException carries no HTTP status of its own in the cognito-idp model, so it goes out as the awsJson 400; the domain operations sent 404. The tag operations now also refuse an ARN that names the pool's id under another region or account, and the region test covers UntagResource.
This commit is contained in:
1 parent
8fee66ae61
commit
899211e498
6 files changed
+126
-11
No files matched your search
+79
@@ -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);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -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"));
|
||||
}
|
||||
|
||||
|
||||
+4
-4
@@ -294,7 +294,7 @@ class CognitoCfnProvisionerTest {
|
||||
@Test
|
||||
void 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 {
|
||||
|
||||
@Test
|
||||
void 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 {
|
||||
@Test
|
||||
void 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());
|
||||
|
||||
+2
-2
@@ -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"));
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user