fix(cognito-identity): give resource-tag conditions the tags of the identity pool a request names (#8765)

The generated request resources give Cognito identity pools'
identitypool type the aws:ResourceTag/ key, but no service answered the
cognito-identity namespace's stored tags, so a condition on an identity
pool's tags saw none. CognitoIdentityService now answers it: storedTags
reads the pool stored under the Region the ARN names and its id, in the
request's account, where the tag handlers read, in place, and matched on
the whole ARN. TagResource and UntagResource changed the pool's map in
place with no lock; they now give it a new map one call at a time, and
the tags field is volatile. UpdateIdentityPool, which sets the tags
too, replaces the pool under the same lock.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-17 14:48:33 -04:00
1 parent f64dea0ba5
commit 45aeeb0652
6 files changed
+165 -21

No files matched your search

@@ -0,0 +1,62 @@
package io.floci.aws.services.cognitoidentity;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.storage.AccountAwareStorageBackend;
import io.floci.aws.core.storage.InMemoryStorage;
import io.floci.aws.services.cognitoidentity.model.IdentityPool;
import io.floci.aws.testing.InMemoryStorageFactory;
import org.junit.jupiter.api.Test;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
class CognitoIdentityServiceTest {
private static final String ACCOUNT = "000000000000";
private static final String REGION = "us-east-1";
private final InMemoryStorage<String, IdentityPool> raw = new InMemoryStorage<>();
private final CognitoIdentityService service = new CognitoIdentityService(
new InMemoryStorageFactory(ACCOUNT).withStore("cognitoidentity-pools.json",
new AccountAwareStorageBackend<>(raw, null, ACCOUNT)),
new RegionResolver(REGION, ACCOUNT));
/**
* IAM's tag lookup reads the tags of the identity pool whose ARN it is given, as tag calls leave them, a tag call
* giving the pool a new map rather than changing the one a reader holds, and those of a pool stored before storage
* was account-aware, in place, without migrating it; nothing for an ARN of another account or Region, or of no
* pool.
*/
@Test
void storedTagsAreTheTagsOfTheIdentityPoolTheArnNamesAndTaggingPutsANewMap() {
IdentityPool spec = new IdentityPool();
spec.setIdentityPoolName("tagged");
spec.setIdentityPoolTags(new LinkedHashMap<>(Map.of("env", "dev")));
String arn = service.createIdentityPool(spec, REGION).getArn();
Map<String, String> held = service.listTagsForResource(arn, REGION);
service.tagResource(arn, Map.of("team", "data"), REGION);
service.untagResource(arn, List.of("env"), REGION);
String legacyId = REGION + ":0b6b2c1e-1f6e-4d7a-9a51-3c1f7f0e2d4a";
String legacyArn = "arn:aws:cognito-identity:" + REGION + ":" + ACCOUNT + ":identitypool/" + legacyId;
IdentityPool legacy = new IdentityPool();
legacy.setIdentityPoolId(legacyId);
legacy.setIdentityPoolName("legacy");
legacy.setArn(legacyArn);
legacy.setIdentityPoolTags(Map.of("tier", "web"));
raw.put(REGION + "::" + legacyId, legacy);
assertEquals(Map.of("env", "dev"), held);
assertEquals(Map.of("team", "data"), service.storedTags(arn));
assertEquals(Map.of("tier", "web"), service.storedTags(legacyArn));
assertTrue(raw.get(REGION + "::" + legacyId).isPresent());
assertEquals(Map.of(), service.storedTags(arn.replace(":" + ACCOUNT + ":", ":111122223333:")));
assertEquals(Map.of(), service.storedTags(arn.replace(":" + REGION + ":" + ACCOUNT, ":eu-west-1:" + ACCOUNT)));
assertEquals(Map.of(), service.storedTags(arn + "0"));
assertEquals(Map.of(), service.storedTags("arn:aws:cognito-idp:" + REGION + ":" + ACCOUNT + ":userpool/x"));
}
}
@@ -859,14 +859,9 @@ class IamRequestResourceEnforcementIntegrationTest {
/** An identity pool is checked on the ARN its id gives, the one Cognito tags it under. */
@Test
void anIdentityPoolIsCheckedOnTheArnItsIdGives() {
String id = adminJson("cognito-identity", "AWSCognitoIdentityService.CreateIdentityPool")
.body("{\"IdentityPoolName\":\"res pool " + suffix() + "\",\"AllowUnauthenticatedIdentities\":false}")
.post("/").then().statusCode(200).extract().path("IdentityPoolId");
cleanup.register(() -> adminJson("cognito-identity", "AWSCognitoIdentityService.DeleteIdentityPool")
.body("{\"IdentityPoolId\":\"" + id + "\"}").post("/"));
String arn = "arn:aws:cognito-identity:" + REGION + ":" + ACCOUNT + ":identitypool/" + id;
adminJson("cognito-identity", "AWSCognitoIdentityService.TagResource")
.body("{\"ResourceArn\":\"" + arn + "\",\"Tags\":{\"a\":\"b\"}}").post("/").then().statusCode(200);
String id = identityPool(null);
String arn = identityPoolArn(id);
tagIdentityPool(arn, "{\"a\":\"b\"}");
User user = user("res-identity-pool");
user.policy(allowOn("cognito-identity:DescribeIdentityPool", arn));
@@ -874,6 +869,44 @@ class IamRequestResourceEnforcementIntegrationTest {
refusedJson(describeIdentityPool(user, REGION + ":" + UUID.randomUUID()), "cognito-identity:DescribeIdentityPool");
}
/**
* An {@code aws:ResourceTag} condition reads the tags Cognito holds on the identity pool a request names, as
* TagResource leaves them, so an allow on one team's tag lets the caller describe that team's pool and not
* another's.
*/
@Test
void aCognitoIdentityResourceTagConditionReadsTheTagsOfTheIdentityPool() {
String tagged = identityPool("{\"team\":\"web\"}");
String other = identityPool("{\"team\":\"web\"}");
tagIdentityPool(identityPoolArn(tagged), "{\"team\":\"data\"}");
User user = user("res-identity-pool-tags");
user.policy("{\"Effect\":\"Allow\",\"Action\":\"cognito-identity:DescribeIdentityPool\",\"Resource\":\"*\","
+ "\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"data\"}}}");
describeIdentityPool(user, tagged).statusCode(200);
refusedJson(describeIdentityPool(user, other), "cognito-identity:DescribeIdentityPool");
}
/** The id of an identity pool an administrator creates, with the {@code tags} object if any, deleted afterwards. */
private String identityPool(String tags) {
String id = adminJson("cognito-identity", "AWSCognitoIdentityService.CreateIdentityPool")
.body("{\"IdentityPoolName\":\"res pool " + suffix() + "\",\"AllowUnauthenticatedIdentities\":false"
+ (tags == null ? "" : ",\"IdentityPoolTags\":" + tags) + "}")
.post("/").then().statusCode(200).extract().path("IdentityPoolId");
cleanup.register(() -> adminJson("cognito-identity", "AWSCognitoIdentityService.DeleteIdentityPool")
.body("{\"IdentityPoolId\":\"" + id + "\"}").post("/"));
return id;
}
private static String identityPoolArn(String id) {
return "arn:aws:cognito-identity:" + REGION + ":" + ACCOUNT + ":identitypool/" + id;
}
private static void tagIdentityPool(String arn, String tags) {
adminJson("cognito-identity", "AWSCognitoIdentityService.TagResource")
.body("{\"ResourceArn\":\"" + arn + "\",\"Tags\":" + tags + "}").post("/").then().statusCode(200);
}
@Test
void aCloudMapNamespaceAndServiceAreCheckedOnTheArnsTheirIdsGive() {
String namespaceId = cloudMapNamespace("res-ns-" + suffix());