feat(cognito): add TagResource, UntagResource, ListTagsForResource for user pools (#579)

* feat(cognito): add TagResource, UntagResource, ListTagsForResource for user pools

Dispatch the three Cognito IDP tag operations through CognitoJsonHandler and
resolve user pools by parsing the resource ARN (O(1) pool-store lookup, not a
full scan). Malformed ARNs now surface as InvalidParameterException rather
than a swallowed ResourceNotFoundException.

Reject reserved floci:* keys on standalone TagResource via
ReservedTags.rejectReservedTagsOnUpdate, matching the existing strip-on-write
behavior on CreateUserPool / UpdateUserPool. Mutations are synchronized on
the pool instance to match the ensureJwtSigningKeys pattern.

Covered by new unit tests (CognitoServiceTest, CognitoJsonHandlerTest), AWS
CLI bats (cognito.bats), and Java SDK compat tests (CognitoFeaturesTest).

* chore: retrigger CI
This commit is contained in:
TiM authored and GitHub committed 2026-04-24 01:56:03 -05:00
1 parent 2d1fe19d88
commit 273434893f
9 files changed
+305 -4

No files matched your search

@@ -266,7 +266,8 @@ public class AwsQueryController {
"InitiateAuth", "AdminInitiateAuth", "RespondToAuthChallenge",
"SignUp", "ConfirmSignUp", "ChangePassword", "ForgotPassword",
"ConfirmForgotPassword", "GetUser", "UpdateUserAttributes",
"CreateUserPool", "DescribeUserPool", "ListUserPools", "DeleteUserPool",
"CreateUserPool", "DescribeUserPool", "ListUserPools", "UpdateUserPool", "DeleteUserPool",
"TagResource", "UntagResource", "ListTagsForResource",
"CreateUserPoolClient", "DescribeUserPoolClient", "ListUserPoolClients", "DeleteUserPoolClient",
"CreateGroup", "GetGroup", "ListGroups", "DeleteGroup",
"AdminAddUserToGroup", "AdminRemoveUserFromGroup", "AdminListGroupsForUser"
@@ -38,6 +38,9 @@ public class CognitoJsonHandler {
case "DescribeUserPool" -> handleDescribeUserPool(request);
case "ListUserPools" -> handleListUserPools(request);
case "UpdateUserPool" -> handleUpdateUserPool(request, region);
case "TagResource" -> handleTagResource(request);
case "UntagResource" -> handleUntagResource(request);
case "ListTagsForResource" -> handleListTagsForResource(request);
case "GetUserPoolMfaConfig" -> handleGetUserPoolMfaConfig(request);
case "DeleteUserPool" -> handleDeleteUserPool(request);
case "CreateUserPoolClient" -> handleCreateUserPoolClient(request);
@@ -119,6 +122,24 @@ public class CognitoJsonHandler {
return Response.ok(response).build();
}
private Response handleTagResource(JsonNode request) {
@SuppressWarnings("unchecked")
Map<String, String> tags = objectMapper.convertValue(request.path("Tags"), Map.class);
service.tagResource(request.path("ResourceArn").asText(), tags);
return Response.ok(objectMapper.createObjectNode()).build();
}
private Response handleUntagResource(JsonNode request) {
service.untagResource(request.path("ResourceArn").asText(), readStringList(request.path("TagKeys")));
return Response.ok(objectMapper.createObjectNode()).build();
}
private Response handleListTagsForResource(JsonNode request) {
ObjectNode response = objectMapper.createObjectNode();
response.set("Tags", objectMapper.valueToTree(service.listTagsForResource(request.path("ResourceArn").asText())));
return Response.ok(response).build();
}
private Response handleGetUserPoolMfaConfig(JsonNode request) {
UserPool pool = service.describeUserPool(request.path("UserPoolId").asText());
ObjectNode response = objectMapper.createObjectNode();
@@ -150,6 +150,61 @@ public class CognitoService {
return poolStore.scan(k -> true);
}
private UserPool describeUserPoolByArn(String resourceArn) {
String poolId = extractUserPoolIdFromArn(resourceArn);
return describeUserPool(poolId);
}
public void tagResource(String resourceArn, Map<String, String> tags) {
if (tags == null || tags.isEmpty()) {
throw new AwsException("InvalidParameterException", "Tags are required", 400);
}
ReservedTags.rejectReservedTagsOnUpdate(tags);
UserPool pool = describeUserPoolByArn(resourceArn);
synchronized (pool) {
pool.setUserPoolTags(mergeUserPoolTags(pool.getUserPoolTags(), tags));
pool.setLastModifiedDate(System.currentTimeMillis() / 1000L);
poolStore.put(pool.getId(), pool);
}
}
public void untagResource(String resourceArn, List<String> tagKeys) {
if (tagKeys == null || tagKeys.isEmpty()) {
throw new AwsException("InvalidParameterException", "TagKeys are required", 400);
}
UserPool pool = describeUserPoolByArn(resourceArn);
synchronized (pool) {
pool.setUserPoolTags(removeUserPoolTags(pool.getUserPoolTags(), tagKeys));
pool.setLastModifiedDate(System.currentTimeMillis() / 1000L);
poolStore.put(pool.getId(), pool);
}
}
public Map<String, String> listTagsForResource(String resourceArn) {
UserPool pool = describeUserPoolByArn(resourceArn);
return new HashMap<>(pool.getUserPoolTags() != null ? pool.getUserPoolTags() : Map.of());
}
private static String extractUserPoolIdFromArn(String resourceArn) {
if (resourceArn == null || resourceArn.isBlank()) {
throw new AwsException("InvalidParameterException", "ResourceArn is required", 400);
}
// arn:aws:cognito-idp:<region>:<account>:userpool/<pool-id>
String[] parts = resourceArn.split(":", 6);
if (parts.length < 6 || !"cognito-idp".equals(parts[2])) {
throw new AwsException("InvalidParameterException", "Invalid resource ARN: " + resourceArn, 400);
}
String resource = parts[5];
if (!resource.startsWith("userpool/")) {
throw new AwsException("InvalidParameterException", "Invalid resource ARN: " + resourceArn, 400);
}
String poolId = resource.substring("userpool/".length());
if (poolId.isBlank()) {
throw new AwsException("InvalidParameterException", "Invalid resource ARN: " + resourceArn, 400);
}
return poolId;
}
public void deleteUserPool(String id) {
String prefix = id + "::";
groupStore.scan(k -> k.startsWith(prefix))
@@ -1463,6 +1518,18 @@ public class CognitoService {
+ UUID.randomUUID().toString().replace("-", "");
}
private Map<String, String> mergeUserPoolTags(Map<String, String> existingTags, Map<String, String> tagsToAdd) {
Map<String, String> merged = new HashMap<>(existingTags != null ? existingTags : Map.of());
merged.putAll(tagsToAdd);
return merged;
}
private Map<String, String> removeUserPoolTags(Map<String, String> existingTags, List<String> tagKeys) {
Map<String, String> updated = new HashMap<>(existingTags != null ? existingTags : Map.of());
tagKeys.forEach(updated::remove);
return updated;
}
private String trimTrailingSlash(String value) {
if (value.endsWith("/")) {
return value.substring(0, value.length() - 1);