From 948dcadae82a6c86f67a14fcaf56e44c3a5c7633 Mon Sep 17 00:00:00 2001
From: Priamo Germosen <6233424+pgermosen@users.noreply.github.com>
Date: Tue, 17 Nov 2026 11:34:47 -0400
Subject: [PATCH] fix(cognito-idp): give resource-tag conditions the tags of
the user pool a request names (#8753)
The generated request resources give Cognito user pools'
userpool type the aws:ResourceTag/ key, but no service answered the
cognito-idp namespace's stored tags, so a condition on a user pool's
tags saw none. CognitoService now answers it: storedTags reads the pool
stored under its id in the request's account, where the handlers read,
in place, and matches the whole ARN, or the id for a pool stored before
pools recorded their ARN, as DescribeUserPool's lookup by ARN does. Tag
changes already store a new map under the pool's lock; the tags field is
now volatile.
---
docs/services/iam.md | 4 +-
.../aws/core/common/StoredResourceArns.java | 6 +--
.../aws/services/cognito/CognitoService.java | 38 ++++++++++++++++++-
.../aws/services/cognito/model/UserPool.java | 2 +-
.../services/cognito/CognitoServiceTest.java | 20 ++++++++++
...estResourceEnforcementIntegrationTest.java | 36 +++++++++++++++---
6 files changed, 94 insertions(+), 12 deletions(-)
diff --git a/docs/services/iam.md b/docs/services/iam.md
index bd77dffdbb..41e487bd69 100644
--- a/docs/services/iam.md
+++ b/docs/services/iam.md
@@ -3203,8 +3203,8 @@ floci populates:
permission sets, applications and trusted token issuers, OAM for its sinks and links, RAM for its resource shares,
Payment Cryptography for its keys, Resource Explorer for its indexes and views, Application Auto Scaling for its
scalable targets, ECS for its clusters, task definitions, services, capacity providers, tasks and container
- instances, ACM for its certificates, and Firehose for its delivery streams. Tag keys differing only in case are one
- condition key holding every value.
+ instances, ACM for its certificates, Firehose for its delivery streams, and Cognito for its user pools. Tag keys
+ differing only in case are one condition key holding every value.
- EC2 resource keys such as a network interface's `ec2:Vpc`, `ec2:Subnet`, `ec2:AvailabilityZone` and
`ec2:NetworkInterfaceID`: for every action on a resource EC2 holds, the members of the resource the
key names, an ARN for a key the Service Reference types as one. `resourceKeys` in
diff --git a/src/main/java/io/floci/aws/core/common/StoredResourceArns.java b/src/main/java/io/floci/aws/core/common/StoredResourceArns.java
index 6c261fc898..6649fc8c50 100644
--- a/src/main/java/io/floci/aws/core/common/StoredResourceArns.java
+++ b/src/main/java/io/floci/aws/core/common/StoredResourceArns.java
@@ -45,9 +45,9 @@ import java.util.Optional;
* key value store a name names and the tags of any resource it tags; ECS, for the revision a task definition
* reference resolves to, its default cluster and the tags of a cluster, a task definition, a service, a capacity
* provider, a task or a container instance; ACM, for the tags of a certificate; Firehose, for the tags of a delivery
- * stream); looked up lazily by {@code ResourceArnBuilder} and {@code IamConditionContextResolver} through
- * {@code Instance} for the namespace a request or an ARN is of, so IAM never depends on that
- * service directly.
+ * stream; Cognito, for the tags of a user pool); looked up lazily by {@code ResourceArnBuilder} and
+ * {@code IamConditionContextResolver} through {@code Instance} for the namespace a request or an
+ * ARN is of, so IAM never depends on that service directly.
*
* Each answer is a lookup of what the service holds, asked for by an entry of the generated table ({@code find},
* {@code through}, a default, {@code forwarded}, a type's {@code resourceTagKeys}): never a fact about what an operation acts on, which the table alone
diff --git a/src/main/java/io/floci/aws/services/cognito/CognitoService.java b/src/main/java/io/floci/aws/services/cognito/CognitoService.java
index 067eff4ae6..994a4d427e 100644
--- a/src/main/java/io/floci/aws/services/cognito/CognitoService.java
+++ b/src/main/java/io/floci/aws/services/cognito/CognitoService.java
@@ -22,6 +22,7 @@ import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.common.RequestScopes;
import io.floci.aws.core.common.ReservedTags;
import io.floci.aws.core.common.SmithyValidationErrors;
+import io.floci.aws.core.common.StoredResourceArns;
import io.floci.aws.core.resource.ExplorerResource;
import io.floci.aws.core.resource.ResourceProvider;
import io.floci.aws.core.resource.SupportedResourceType;
@@ -110,7 +111,7 @@ import java.util.regex.Pattern;
import static io.floci.aws.core.common.ReservedTags.rejectUnknownReservedTags;
@ApplicationScoped
-public class CognitoService implements ResourceProvider {
+public class CognitoService implements ResourceProvider, StoredResourceArns {
private static final int DEFAULT_REFRESH_TOKEN_VALIDITY_DAYS = 30;
private static final Duration MIN_ACCESS_AND_ID_TOKEN_VALIDITY = Duration.ofMinutes(5);
private static final Duration MAX_ACCESS_AND_ID_TOKEN_VALIDITY = Duration.ofDays(1);
@@ -1136,6 +1137,41 @@ public class CognitoService implements ResourceProvider {
return new HashMap<>(pool.getUserPoolTags() != null ? pool.getUserPoolTags() : Map.of());
}
+ @Override
+ public String namespace() {
+ return "cognito-idp";
+ }
+
+ /** A request names a user pool by the id Cognito minted, which its ARN ends in, so there is nothing to find. */
+ @Override
+ public Optional find(String type, List names, String region) {
+ return Optional.empty();
+ }
+
+ /**
+ * The tags of the user pool whose ARN is {@code arn}, in the request's account, where the handlers read: an ARN of
+ * another account names none. Read in place under the pool's id, a pool stored before storage was account-aware
+ * included, and matched on the whole ARN, or on the id for a pool stored before pools recorded their ARN, as
+ * {@link #describeUserPoolByArn} matches it. Tag calls store a new map under the pool's lock, so the read is of
+ * one whole set.
+ */
+ @Override
+ public Map storedTags(String arn) {
+ if (!AwsArnUtils.isArnFor(arn, "cognito-idp")) {
+ return Map.of();
+ }
+ AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
+ if (!parsed.accountId().equals(regionResolver.getAccountId()) || !parsed.resource().startsWith("userpool/")) {
+ return Map.of();
+ }
+ Map tags = AccountAwareStorageBackend.readInAccount(poolStore, parsed.accountId(),
+ parsed.resource().substring("userpool/".length()))
+ .filter(pool -> pool.getArn() == null || arn.equals(pool.getArn()))
+ .map(UserPool::getUserPoolTags)
+ .orElse(Map.of());
+ return new LinkedHashMap<>(tags);
+ }
+
private static String extractUserPoolIdFromArn(String resourceArn) {
if (resourceArn == null || resourceArn.isBlank()) {
throw new AwsException("InvalidParameterException", "ResourceArn is required", 400);
diff --git a/src/main/java/io/floci/aws/services/cognito/model/UserPool.java b/src/main/java/io/floci/aws/services/cognito/model/UserPool.java
index debfc40c88..a103f7ce97 100644
--- a/src/main/java/io/floci/aws/services/cognito/model/UserPool.java
+++ b/src/main/java/io/floci/aws/services/cognito/model/UserPool.java
@@ -45,7 +45,7 @@ public class UserPool {
private int estimatedNumberOfUsers = 0;
private Map emailConfiguration = new HashMap<>();
private Map smsConfiguration = new HashMap<>();
- private Map userPoolTags = new HashMap<>();
+ private volatile Map userPoolTags = new HashMap<>();
private Map adminCreateUserConfig = new HashMap<>();
private Map userPoolAddOns = new HashMap<>();
private Map usernameConfiguration = new HashMap<>();
diff --git a/src/test/java/io/floci/aws/services/cognito/CognitoServiceTest.java b/src/test/java/io/floci/aws/services/cognito/CognitoServiceTest.java
index f066a0b077..1c17bbf048 100644
--- a/src/test/java/io/floci/aws/services/cognito/CognitoServiceTest.java
+++ b/src/test/java/io/floci/aws/services/cognito/CognitoServiceTest.java
@@ -1869,6 +1869,26 @@ class CognitoServiceTest {
assertEquals(Map.of("env", "test", "team", "platform"), service.listTagsForResource(pool.getArn()));
}
+ /**
+ * IAM's tag lookup reads the tags of the user pool whose ARN it is given, as tag calls leave them; nothing for an
+ * ARN of another account or Region, or of no pool.
+ */
+ @Test
+ void storedTagsAreTheTagsOfTheUserPoolTheArnNames() {
+ UserPool pool = service.createUserPool(
+ Map.of("PoolName", "TaggedPool", "UserPoolTags", Map.of("env", "dev")),
+ "us-east-1"
+ );
+ service.tagResource(pool.getArn(), Map.of("team", "data"));
+ service.untagResource(pool.getArn(), List.of("env"));
+
+ assertEquals(Map.of("team", "data"), service.storedTags(pool.getArn()));
+ assertEquals(Map.of(), service.storedTags(pool.getArn().replace(":000000000000:", ":111122223333:")));
+ assertEquals(Map.of(), service.storedTags(pool.getArn().replace(":us-east-1:", ":eu-west-1:")));
+ assertEquals(Map.of(), service.storedTags(pool.getArn() + "x"));
+ assertEquals(Map.of(), service.storedTags("arn:aws:s3:::pool"));
+ }
+
@Test
void tagResourceRejectsReservedKey() {
UserPool pool = service.createUserPool(Map.of("PoolName", "TaggedPool"), "us-east-1");
diff --git a/src/test/java/io/floci/aws/services/iam/IamRequestResourceEnforcementIntegrationTest.java b/src/test/java/io/floci/aws/services/iam/IamRequestResourceEnforcementIntegrationTest.java
index 0aefc03476..e31fcba145 100644
--- a/src/test/java/io/floci/aws/services/iam/IamRequestResourceEnforcementIntegrationTest.java
+++ b/src/test/java/io/floci/aws/services/iam/IamRequestResourceEnforcementIntegrationTest.java
@@ -518,14 +518,10 @@ class IamRequestResourceEnforcementIntegrationTest {
@Test
void aUserPoolIsCheckedOnTheArnItsIdNames() {
- String id = admin("cognito-idp").header("X-Amz-Target", "AWSCognitoIdentityProviderService.CreateUserPool")
- .contentType("application/x-amz-json-1.1").body("{\"PoolName\":\"res-pool-" + suffix() + "\"}")
- .post("/").then().statusCode(200).extract().path("UserPool.Id");
+ String id = userPool(null);
String arn = admin("cognito-idp").header("X-Amz-Target", "AWSCognitoIdentityProviderService.DescribeUserPool")
.contentType("application/x-amz-json-1.1").body("{\"UserPoolId\":\"" + id + "\"}")
.post("/").then().statusCode(200).extract().path("UserPool.Arn");
- cleanup.register(() -> admin("cognito-idp").header("X-Amz-Target", "AWSCognitoIdentityProviderService.DeleteUserPool")
- .contentType("application/x-amz-json-1.1").body("{\"UserPoolId\":\"" + id + "\"}").post("/"));
assertEquals("arn:aws:cognito-idp:" + REGION + ":" + ACCOUNT + ":userpool/" + id, arn);
User user = user("res-pool");
@@ -534,6 +530,36 @@ class IamRequestResourceEnforcementIntegrationTest {
refusedJson(describeUserPool(user, id + "x"), "cognito-idp:DescribeUserPool");
}
+ /**
+ * An {@code aws:ResourceTag} condition reads the tags Cognito holds on the user pool a request names by its id, as
+ * a tag call leaves them, so an allow on one team's tag lets the caller describe that team's pool and not
+ * another's.
+ */
+ @Test
+ void aCognitoResourceTagConditionReadsTheTagsOfTheUserPool() {
+ String tagged = userPool("{\"team\":\"web\"}");
+ String other = userPool("{\"team\":\"web\"}");
+ adminJson("cognito-idp", "AWSCognitoIdentityProviderService.TagResource").body("{\"ResourceArn\":\""
+ + "arn:aws:cognito-idp:" + REGION + ":" + ACCOUNT + ":userpool/" + tagged
+ + "\",\"Tags\":{\"team\":\"data\"}}").post("/").then().statusCode(200);
+
+ User user = user("res-pool-tags");
+ user.policy("{\"Effect\":\"Allow\",\"Action\":\"cognito-idp:DescribeUserPool\",\"Resource\":\"*\","
+ + "\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"data\"}}}");
+ describeUserPool(user, tagged).statusCode(200);
+ refusedJson(describeUserPool(user, other), "cognito-idp:DescribeUserPool");
+ }
+
+ /** The id of a user pool an administrator creates, with the {@code tags} object if any, deleted afterwards. */
+ private String userPool(String tags) {
+ String id = adminJson("cognito-idp", "AWSCognitoIdentityProviderService.CreateUserPool")
+ .body("{\"PoolName\":\"res-pool-" + suffix() + "\"" + (tags == null ? "" : ",\"UserPoolTags\":" + tags)
+ + "}").post("/").then().statusCode(200).extract().path("UserPool.Id");
+ cleanup.register(() -> adminJson("cognito-idp", "AWSCognitoIdentityProviderService.DeleteUserPool")
+ .body("{\"UserPoolId\":\"" + id + "\"}").post("/"));
+ return id;
+ }
+
/**
* A key is named by its id or by an alias, and an alias names the key it points at, so a request naming a key by
* an alias is checked on the key the alias names, as the handler resolves it: a grant on that key allows it, a