feat(iam): list entities attached to managed policies (#1808)

This commit is contained in:
Alex Banna authored and GitHub committed 2026-08-01 16:36:11 -05:00
1 parent a8ed218c57
commit 78ba5b3457
6 files changed
+196 -1

No files matched your search

@@ -82,7 +82,7 @@ public class AwsQueryController {
"CreateGroup", "GetGroup", "DeleteGroup", "ListGroups",
"AddUserToGroup", "RemoveUserFromGroup", "ListGroupsForUser",
"CreateRole", "GetRole", "DeleteRole", "ListRoles", "UpdateRole",
"CreatePolicy", "GetPolicy", "DeletePolicy", "ListPolicies",
"CreatePolicy", "GetPolicy", "DeletePolicy", "ListPolicies", "ListEntitiesForPolicy",
"CreatePolicyVersion", "GetPolicyVersion", "DeletePolicyVersion",
"ListPolicyVersions", "SetDefaultPolicyVersion",
"AttachUserPolicy", "DetachUserPolicy", "ListAttachedUserPolicies",
@@ -92,6 +92,7 @@ public class IamQueryHandler {
case "GetPolicy" -> handleGetPolicy(params);
case "DeletePolicy" -> handleDeletePolicy(params);
case "ListPolicies" -> handleListPolicies(params);
case "ListEntitiesForPolicy" -> handleListEntitiesForPolicy(params);
case "CreatePolicyVersion" -> handleCreatePolicyVersion(params);
case "GetPolicyVersion" -> handleGetPolicyVersion(params);
case "DeletePolicyVersion" -> handleDeletePolicyVersion(params);
@@ -475,6 +476,27 @@ public class IamQueryHandler {
return Response.ok(AwsQueryResponse.envelope("ListPolicies", AwsNamespaces.IAM, xml.build())).build();
}
private Response handleListEntitiesForPolicy(MultivaluedMap<String, String> params) {
IamService.PolicyEntities entities = iamService.listEntitiesForPolicy(getParam(params, "PolicyArn"));
var xml = new XmlBuilder().start("PolicyGroups");
for (IamGroup group : entities.groups()) {
xml.start("member").elem("GroupName", group.getGroupName())
.elem("GroupId", group.getGroupId()).end("member");
}
xml.end("PolicyGroups").start("PolicyUsers");
for (IamUser user : entities.users()) {
xml.start("member").elem("UserName", user.getUserName())
.elem("UserId", user.getUserId()).end("member");
}
xml.end("PolicyUsers").start("PolicyRoles");
for (IamRole role : entities.roles()) {
xml.start("member").elem("RoleName", role.getRoleName())
.elem("RoleId", role.getRoleId()).end("member");
}
xml.end("PolicyRoles").elem("IsTruncated", false);
return Response.ok(AwsQueryResponse.envelope("ListEntitiesForPolicy", AwsNamespaces.IAM, xml.build())).build();
}
private Response handleCreatePolicyVersion(MultivaluedMap<String, String> params) {
String policyArn = getParam(params, "PolicyArn");
String document = getParam(params, "PolicyDocument");
@@ -497,6 +497,28 @@ public class IamService implements SessionAccountLookup {
LOG.infov("Deleted IAM policy: {0}", policyArn);
}
/** The roles, users and groups a managed policy is currently attached to. */
public record PolicyEntities(List<IamRole> roles, List<IamUser> users, List<IamGroup> groups) {}
/**
* Lists the entities (roles, users, groups) a managed policy is attached to — the read behind
* IAM's ListEntitiesForPolicy and what a caller must detach before {@link #deletePolicy} will
* succeed. Attachments are tracked on the principals, so this scans them for the given ARN.
*/
public PolicyEntities listEntitiesForPolicy(String policyArn) {
getPolicy(policyArn); // AWS raises NoSuchEntity for an unknown policy ARN; fail fast likewise.
List<IamRole> attachedRoles = roles.scan(k -> true).stream()
.filter(r -> r.getAttachedPolicyArns().contains(policyArn))
.toList();
List<IamUser> attachedUsers = users.scan(k -> true).stream()
.filter(u -> u.getAttachedPolicyArns().contains(policyArn))
.toList();
List<IamGroup> attachedGroups = groups.scan(k -> true).stream()
.filter(g -> g.getAttachedPolicyArns().contains(policyArn))
.toList();
return new PolicyEntities(attachedRoles, attachedUsers, attachedGroups);
}
public List<IamPolicy> listPolicies(String scope, String pathPrefix) {
if (scope != null && !scope.isBlank()
&& !"All".equalsIgnoreCase(scope)