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.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-17 11:34:47 -04:00
1 parent 751aedde94
commit 948dcadae8
6 files changed
+94 -12

No files matched your search

+2 -2
View File
@@ -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
@@ -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<StoredResourceArns>} for the namespace a request or an ARN is of, so IAM never depends on that
* service directly.</p>
* stream; Cognito, for the tags of a user pool); looked up lazily by {@code ResourceArnBuilder} and
* {@code IamConditionContextResolver} through {@code Instance<StoredResourceArns>} for the namespace a request or an
* ARN is of, so IAM never depends on that service directly.</p>
*
* <p>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
@@ -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<String> find(String type, List<String> 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<String, String> 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<String, String> 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);
@@ -45,7 +45,7 @@ public class UserPool {
private int estimatedNumberOfUsers = 0;
private Map<String, Object> emailConfiguration = new HashMap<>();
private Map<String, Object> smsConfiguration = new HashMap<>();
private Map<String, String> userPoolTags = new HashMap<>();
private volatile Map<String, String> userPoolTags = new HashMap<>();
private Map<String, Object> adminCreateUserConfig = new HashMap<>();
private Map<String, Object> userPoolAddOns = new HashMap<>();
private Map<String, Object> usernameConfiguration = new HashMap<>();
@@ -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");
@@ -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