mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(iam): add GetAccountAuthorizationDetails (#4208)
Returns every user, group and role in the account, plus every local (customer-managed) policy and every AWS-managed policy actually attached to or used as a permissions boundary by something in the account: the entity lists themselves, their inline and attached policies, group membership, instance profiles, permissions boundaries, and each managed policy's version list. AttachmentCount and PermissionsBoundaryUsageCount are computed by scanning the account's own users, groups and roles rather than read off IamPolicy.getAttachmentCount(): for an AWS-managed policy that field is a single object shared process-wide across every account (attachUserPolicy et al. mutate the same catalog entry every account resolves), so trusting it would leak one account's attachments into another's response. Filed separately as #4205, since fixing it properly means changing the attach/detach methods themselves, well beyond this action. Also fixes two related, smaller gaps found while building this: - CreateInstanceProfile silently dropped a Tags parameter at creation, unlike every other Create* action. - GetInstanceProfile, CreateInstanceProfile and ListInstanceProfilesForRole never serialized an instance profile's own tags into the response. ListInstanceProfiles keeps omitting them: its own operation documentation, like ListRoles's, says it returns a subset of attributes and does not return tags, even though the InstanceProfile shape itself carries no such exclusion note. Policy documents are returned as plain JSON, not URL-encoded as AWS documents them for this action: matches every other IAM action that returns a policy document, none of which URL-encode either. Filter, MaxItems and Marker are not honored; the response always includes everything.
This commit is contained in:
1 parent
d30a744b66
commit
1fa5534e6c
6 files changed
+621
-7
No files matched your search
@@ -133,6 +133,7 @@ public class IamQueryHandler {
|
||||
case "ListPolicies" -> handleListPolicies(params);
|
||||
case "ListEntitiesForPolicy" -> handleListEntitiesForPolicy(params);
|
||||
case "GetAccountSummary" -> handleGetAccountSummary(params);
|
||||
case "GetAccountAuthorizationDetails" -> handleGetAccountAuthorizationDetails(params);
|
||||
case "CreatePolicyVersion" -> handleCreatePolicyVersion(params);
|
||||
case "GetPolicyVersion" -> handleGetPolicyVersion(params);
|
||||
case "DeletePolicyVersion" -> handleDeletePolicyVersion(params);
|
||||
@@ -854,6 +855,137 @@ public class IamQueryHandler {
|
||||
return Response.ok(AwsQueryResponse.envelope("GetAccountSummary", AwsNamespaces.IAM, xml.build())).build();
|
||||
}
|
||||
|
||||
// Filter, MaxItems and Marker are not honored: every user, group, role and relevant policy
|
||||
// is always returned in one response, matching this handler's general convention elsewhere
|
||||
// (ListInstanceProfiles, SimulatePrincipalPolicy, ...) of not implementing pagination.
|
||||
private Response handleGetAccountAuthorizationDetails(MultivaluedMap<String, String> params) {
|
||||
IamService.AccountAuthorizationDetails details = iamService.getAccountAuthorizationDetails();
|
||||
|
||||
XmlBuilder xml = new XmlBuilder().start("UserDetailList");
|
||||
for (IamUser user : details.users()) {
|
||||
xml.start("member").raw(userDetailXml(user)).end("member");
|
||||
}
|
||||
xml.end("UserDetailList").start("GroupDetailList");
|
||||
for (IamGroup group : details.groups()) {
|
||||
xml.start("member").raw(groupDetailXml(group)).end("member");
|
||||
}
|
||||
xml.end("GroupDetailList").start("RoleDetailList");
|
||||
for (IamRole role : details.roles()) {
|
||||
xml.start("member").raw(roleDetailXml(role)).end("member");
|
||||
}
|
||||
xml.end("RoleDetailList").start("Policies");
|
||||
for (IamPolicy policy : details.policies()) {
|
||||
xml.start("member")
|
||||
.raw(managedPolicyDetailXml(policy, details.attachmentCounts(), details.permissionsBoundaryUsageCounts()))
|
||||
.end("member");
|
||||
}
|
||||
xml.end("Policies").elem("IsTruncated", false);
|
||||
return Response.ok(AwsQueryResponse.envelope("GetAccountAuthorizationDetails", AwsNamespaces.IAM, xml.build())).build();
|
||||
}
|
||||
|
||||
private String userDetailXml(IamUser u) {
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.raw(userXml(u, true))
|
||||
.raw(policyDetailListXml("UserPolicyList", u.getInlinePolicies()))
|
||||
.start("GroupList");
|
||||
for (String groupName : u.getGroupNames()) {
|
||||
xml.elem("member", groupName);
|
||||
}
|
||||
return xml.end("GroupList")
|
||||
.raw(attachedManagedPoliciesXml(iamService.listAttachedUserPolicies(u.getUserName(), null)))
|
||||
.raw(permissionsBoundaryXml(u.getPermissionsBoundaryArn()))
|
||||
.build();
|
||||
}
|
||||
|
||||
private String groupDetailXml(IamGroup g) {
|
||||
return new XmlBuilder()
|
||||
.raw(groupXml(g))
|
||||
.raw(policyDetailListXml("GroupPolicyList", g.getInlinePolicies()))
|
||||
.raw(attachedManagedPoliciesXml(iamService.listAttachedGroupPolicies(g.getGroupName(), null)))
|
||||
.build();
|
||||
}
|
||||
|
||||
// RoleDetail is not the same subset as ListRoles's Role type: it carries
|
||||
// AssumeRolePolicyDocument (which ListRoles also carries) but, per the wire model, never
|
||||
// MaxSessionDuration or Description, so this does not reuse roleXml's general fragment.
|
||||
private String roleDetailXml(IamRole r) {
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.elem("Path", r.getPath())
|
||||
.elem("RoleName", r.getRoleName())
|
||||
.elem("RoleId", r.getRoleId())
|
||||
.elem("Arn", r.getArn())
|
||||
.elem("CreateDate", isoDate(r.getCreateDate()))
|
||||
.elem("AssumeRolePolicyDocument", r.getAssumeRolePolicyDocument())
|
||||
.raw(tagsElement(r.getTags()))
|
||||
.start("InstanceProfileList");
|
||||
for (InstanceProfile profile : iamService.listInstanceProfilesForRole(r.getRoleName())) {
|
||||
xml.start("member").raw(instanceProfileXml(profile, true)).end("member");
|
||||
}
|
||||
return xml.end("InstanceProfileList")
|
||||
.raw(policyDetailListXml("RolePolicyList", r.getInlinePolicies()))
|
||||
.raw(attachedManagedPoliciesXml(iamService.listAttachedRolePolicies(r.getRoleName(), null)))
|
||||
.raw(permissionsBoundaryXml(r.getPermissionsBoundaryArn()))
|
||||
.build();
|
||||
}
|
||||
|
||||
// AttachmentCount and PermissionsBoundaryUsageCount come from the caller's account-scoped
|
||||
// scan (see IamService.getAccountAuthorizationDetails), not IamPolicy.getAttachmentCount():
|
||||
// for an AWS-managed policy that field is shared process-wide across every account.
|
||||
private String managedPolicyDetailXml(IamPolicy p, Map<String, Integer> attachmentCounts,
|
||||
Map<String, Integer> permissionsBoundaryUsageCounts) {
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.elem("PolicyName", p.getPolicyName())
|
||||
.elem("PolicyId", p.getPolicyId())
|
||||
.elem("Arn", p.getArn())
|
||||
.elem("Path", p.getPath())
|
||||
.elem("DefaultVersionId", p.getDefaultVersionId())
|
||||
.elem("AttachmentCount", (long) attachmentCounts.getOrDefault(p.getArn(), 0))
|
||||
.elem("PermissionsBoundaryUsageCount",
|
||||
(long) permissionsBoundaryUsageCounts.getOrDefault(p.getArn(), 0))
|
||||
.elem("IsAttachable", true)
|
||||
.elem("Description", p.getDescription())
|
||||
.elem("CreateDate", isoDate(p.getCreateDate()))
|
||||
.elem("UpdateDate", isoDate(p.getUpdateDate()))
|
||||
.start("PolicyVersionList");
|
||||
for (PolicyVersion version : p.getVersions().values()) {
|
||||
xml.start("member").raw(policyVersionXml(version)).end("member");
|
||||
}
|
||||
return xml.end("PolicyVersionList").build();
|
||||
}
|
||||
|
||||
private String policyDetailListXml(String wrapperName, Map<String, String> inlinePolicies) {
|
||||
XmlBuilder xml = new XmlBuilder().start(wrapperName);
|
||||
for (Map.Entry<String, String> entry : inlinePolicies.entrySet()) {
|
||||
xml.start("member")
|
||||
.elem("PolicyName", entry.getKey())
|
||||
.elem("PolicyDocument", entry.getValue())
|
||||
.end("member");
|
||||
}
|
||||
return xml.end(wrapperName).build();
|
||||
}
|
||||
|
||||
private String attachedManagedPoliciesXml(List<IamPolicy> attached) {
|
||||
XmlBuilder xml = new XmlBuilder().start("AttachedManagedPolicies");
|
||||
for (IamPolicy p : attached) {
|
||||
xml.start("member")
|
||||
.elem("PolicyName", p.getPolicyName())
|
||||
.elem("PolicyArn", p.getArn())
|
||||
.end("member");
|
||||
}
|
||||
return xml.end("AttachedManagedPolicies").build();
|
||||
}
|
||||
|
||||
private String permissionsBoundaryXml(String boundaryArn) {
|
||||
if (boundaryArn == null) {
|
||||
return "";
|
||||
}
|
||||
return new XmlBuilder().start("PermissionsBoundary")
|
||||
.elem("PermissionsBoundaryType", "Policy")
|
||||
.elem("PermissionsBoundaryArn", boundaryArn)
|
||||
.end("PermissionsBoundary")
|
||||
.build();
|
||||
}
|
||||
|
||||
private Response handleCreatePolicyVersion(MultivaluedMap<String, String> params) {
|
||||
String policyArn = getParam(params, "PolicyArn");
|
||||
String document = getParam(params, "PolicyDocument");
|
||||
@@ -1159,15 +1291,20 @@ public class IamQueryHandler {
|
||||
// =========================================================================
|
||||
|
||||
private Response handleCreateInstanceProfile(MultivaluedMap<String, String> params) {
|
||||
Map<String, String> tags = extractTags(params);
|
||||
InstanceProfile profile = iamService.createInstanceProfile(
|
||||
getParam(params, "InstanceProfileName"), getParam(params, "Path"));
|
||||
String result = new XmlBuilder().start("InstanceProfile").raw(instanceProfileXml(profile)).end("InstanceProfile").build();
|
||||
if (!tags.isEmpty()) {
|
||||
iamService.tagInstanceProfile(profile.getInstanceProfileName(), tags);
|
||||
profile = iamService.getInstanceProfile(profile.getInstanceProfileName());
|
||||
}
|
||||
String result = new XmlBuilder().start("InstanceProfile").raw(instanceProfileXml(profile, true)).end("InstanceProfile").build();
|
||||
return Response.ok(AwsQueryResponse.envelope("CreateInstanceProfile", AwsNamespaces.IAM, result)).build();
|
||||
}
|
||||
|
||||
private Response handleGetInstanceProfile(MultivaluedMap<String, String> params) {
|
||||
InstanceProfile profile = iamService.getInstanceProfile(getParam(params, "InstanceProfileName"));
|
||||
String result = new XmlBuilder().start("InstanceProfile").raw(instanceProfileXml(profile)).end("InstanceProfile").build();
|
||||
String result = new XmlBuilder().start("InstanceProfile").raw(instanceProfileXml(profile, true)).end("InstanceProfile").build();
|
||||
return Response.ok(AwsQueryResponse.envelope("GetInstanceProfile", AwsNamespaces.IAM, result)).build();
|
||||
}
|
||||
|
||||
@@ -1180,7 +1317,8 @@ public class IamQueryHandler {
|
||||
List<InstanceProfile> profiles = iamService.listInstanceProfiles(getParam(params, "PathPrefix"));
|
||||
var xml = new XmlBuilder().start("InstanceProfiles");
|
||||
for (InstanceProfile p : profiles) {
|
||||
xml.start("member").raw(instanceProfileXml(p)).end("member");
|
||||
// Documented listing subset: tags are omitted here, unlike GetInstanceProfile.
|
||||
xml.start("member").raw(instanceProfileXml(p, false)).end("member");
|
||||
}
|
||||
xml.end("InstanceProfiles").elem("IsTruncated", false);
|
||||
return Response.ok(AwsQueryResponse.envelope("ListInstanceProfiles", AwsNamespaces.IAM, xml.build())).build();
|
||||
@@ -1200,7 +1338,7 @@ public class IamQueryHandler {
|
||||
List<InstanceProfile> profiles = iamService.listInstanceProfilesForRole(getParam(params, "RoleName"));
|
||||
var xml = new XmlBuilder().start("InstanceProfiles");
|
||||
for (InstanceProfile p : profiles) {
|
||||
xml.start("member").raw(instanceProfileXml(p)).end("member");
|
||||
xml.start("member").raw(instanceProfileXml(p, true)).end("member");
|
||||
}
|
||||
xml.end("InstanceProfiles").elem("IsTruncated", false);
|
||||
return Response.ok(AwsQueryResponse.envelope("ListInstanceProfilesForRole", AwsNamespaces.IAM, xml.build())).build();
|
||||
@@ -1425,7 +1563,13 @@ public class IamQueryHandler {
|
||||
return xml.elem("CreateDate", isoDate(k.getCreateDate())).build();
|
||||
}
|
||||
|
||||
private String instanceProfileXml(InstanceProfile p) {
|
||||
// detailed is per-operation, not per-profile, mirroring roleXml/userXml/policyXml:
|
||||
// ListInstanceProfiles documents itself as a listing subset ("this operation does not
|
||||
// return tags, even though they are an attribute of the returned object"), the same note
|
||||
// ListRoles carries. GetInstanceProfile, CreateInstanceProfile, ListInstanceProfilesForRole
|
||||
// and the InstanceProfileList embedded in GetAccountAuthorizationDetails's RoleDetail carry
|
||||
// no such note, so they stay detailed.
|
||||
private String instanceProfileXml(InstanceProfile p, boolean detailed) {
|
||||
var xml = new XmlBuilder()
|
||||
.elem("InstanceProfileName", p.getInstanceProfileName())
|
||||
.elem("InstanceProfileId", p.getInstanceProfileId())
|
||||
@@ -1439,7 +1583,8 @@ public class IamQueryHandler {
|
||||
xml.start("member").raw(roleXml(role, false)).end("member");
|
||||
} catch (AwsException ignored) {}
|
||||
}
|
||||
return xml.end("Roles").build();
|
||||
xml.end("Roles");
|
||||
return xml.raw(detailed ? tagsElement(p.getTags()) : "").build();
|
||||
}
|
||||
|
||||
private String attachedPoliciesXml(List<IamPolicy> policyList) {
|
||||
|
||||
@@ -41,6 +41,7 @@ import java.util.Set;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
@@ -2794,4 +2795,81 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
public Map<String, String> listInstanceProfileTags(String instanceProfileName) {
|
||||
return getInstanceProfile(instanceProfileName).getTags();
|
||||
}
|
||||
|
||||
/**
|
||||
* Every user, group and role in the account, plus the policies relevant to them: every
|
||||
* local (customer-managed) policy, and every AWS-managed policy actually attached to or
|
||||
* used as a boundary by something in the account. Backs GetAccountAuthorizationDetails.
|
||||
*
|
||||
* <p>{@code attachmentCounts} and {@code permissionsBoundaryUsageCounts} are computed by
|
||||
* scanning this account's own users, groups and roles, not read off {@link
|
||||
* IamPolicy#getAttachmentCount()}. For an AWS-managed policy that field is shared process-wide
|
||||
* across every account (see {@link #awsManagedPolicies}), so trusting it here would leak one
|
||||
* account's attachments into another's response. A local policy's own counter is already
|
||||
* account-scoped and would agree with this scan; computing it uniformly for both avoids
|
||||
* special-casing and keeps this method independent of that stored field either way.
|
||||
*/
|
||||
public AccountAuthorizationDetails getAccountAuthorizationDetails() {
|
||||
List<IamUser> allUsers = listUsers(null);
|
||||
List<IamGroup> allGroups = listGroups(null);
|
||||
List<IamRole> allRoles = listRoles(null);
|
||||
|
||||
Map<String, Integer> attachmentCounts = new LinkedHashMap<>();
|
||||
Map<String, Integer> boundaryUsageCounts = new LinkedHashMap<>();
|
||||
Set<String> referencedAwsManagedArns = new LinkedHashSet<>();
|
||||
|
||||
for (IamUser user : allUsers) {
|
||||
tallyAttachments(user.getAttachedPolicyArns(), attachmentCounts, referencedAwsManagedArns);
|
||||
tallyBoundaryUsage(user.getPermissionsBoundaryArn(), boundaryUsageCounts, referencedAwsManagedArns);
|
||||
}
|
||||
for (IamGroup group : allGroups) {
|
||||
tallyAttachments(group.getAttachedPolicyArns(), attachmentCounts, referencedAwsManagedArns);
|
||||
}
|
||||
for (IamRole role : allRoles) {
|
||||
tallyAttachments(role.getAttachedPolicyArns(), attachmentCounts, referencedAwsManagedArns);
|
||||
tallyBoundaryUsage(role.getPermissionsBoundaryArn(), boundaryUsageCounts, referencedAwsManagedArns);
|
||||
}
|
||||
|
||||
List<IamPolicy> allPolicies = new ArrayList<>(listPolicies("Local", null));
|
||||
for (String arn : referencedAwsManagedArns) {
|
||||
allPolicies.add(getPolicy(arn));
|
||||
}
|
||||
|
||||
return new AccountAuthorizationDetails(
|
||||
allUsers, allGroups, allRoles, allPolicies, attachmentCounts, boundaryUsageCounts);
|
||||
}
|
||||
|
||||
private void tallyAttachments(List<String> attachedPolicyArns, Map<String, Integer> attachmentCounts,
|
||||
Set<String> referencedAwsManagedArns) {
|
||||
for (String arn : attachedPolicyArns) {
|
||||
attachmentCounts.merge(arn, 1, Integer::sum);
|
||||
if (arn.startsWith(AwsManagedPolicies.ARN_PREFIX)) {
|
||||
referencedAwsManagedArns.add(arn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void tallyBoundaryUsage(String permissionsBoundaryArn, Map<String, Integer> boundaryUsageCounts,
|
||||
Set<String> referencedAwsManagedArns) {
|
||||
if (permissionsBoundaryArn == null) {
|
||||
return;
|
||||
}
|
||||
boundaryUsageCounts.merge(permissionsBoundaryArn, 1, Integer::sum);
|
||||
if (permissionsBoundaryArn.startsWith(AwsManagedPolicies.ARN_PREFIX)) {
|
||||
referencedAwsManagedArns.add(permissionsBoundaryArn);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param attachmentCounts arn -> number of users, groups and roles it is attached to
|
||||
* @param permissionsBoundaryUsageCounts arn -> number of users and roles using it as a boundary
|
||||
*/
|
||||
public record AccountAuthorizationDetails(
|
||||
List<IamUser> users,
|
||||
List<IamGroup> groups,
|
||||
List<IamRole> roles,
|
||||
List<IamPolicy> policies,
|
||||
Map<String, Integer> attachmentCounts,
|
||||
Map<String, Integer> permissionsBoundaryUsageCounts) {
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user