fix(iam): resolve attached AWS-managed policies for any account (#1663)

* fix(iam): resolve attached AWS-managed policies for any account

AWS-managed policies (arn:aws:iam::aws:policy/...) are global and live in the
in-memory catalog, not the account-partitioned policy store; getPolicy already
serves them from the catalog. Several IAM read paths, however, resolved attached
policy ARNs straight from the account-scoped store (policies.get(arn) / the
flatMap variants), so a managed policy attached to a principal owned by a
non-default account was silently dropped.

Add a catalog-aware resolvePolicy(arn) helper modeled on getPolicy and route the
affected reads through it: listAttachedUser/Group/RolePolicies, the user/role
permissions-boundary document resolvers, and collectUser/RolePolicies (the
SimulatePrincipalPolicy / caller-context paths). Customer-policy-only logic and
attachment-count bookkeeping on detach are left untouched.

* test(iam): cover attached managed-policy resolution for a group in a non-default account

Adds the group analog of the existing user/role tests, exercising listAttachedGroupPolicies
(the resolvePolicy catalog fix) for a group owned by a non-default account, with a customer
policy attached to the same group as an account-scoping control.
This commit is contained in:
Alex Banna authored and GitHub committed 2026-07-03 00:46:15 -05:00
1 parent 188854f2df
commit 952f30293f
2 files changed
+160 -8

No files matched your search

@@ -453,6 +453,20 @@ public class IamService implements SessionAccountLookup {
"Policy " + policyArn + " does not exist.", 404));
}
/**
* Resolves a policy by ARN without throwing, mirroring {@link #getPolicy} so that
* AWS-managed policies (arn:aws:iam::aws:policy/...) are served from the global catalog
* rather than the account-partitioned store. Attached-policy read paths must use this:
* a managed policy attached to a principal owned by a non-default account is absent from
* that account's {@link #policies} partition and would otherwise be silently dropped.
*/
private Optional<IamPolicy> resolvePolicy(String arn) {
if (arn != null && arn.startsWith(AwsManagedPolicies.ARN_PREFIX)) {
return Optional.ofNullable(awsManagedPolicies.get(arn));
}
return policies.get(arn);
}
private void rejectIfAwsManaged(String policyArn) {
if (policyArn != null && policyArn.startsWith(AwsManagedPolicies.ARN_PREFIX)) {
throw new AwsException("AccessDenied",
@@ -629,7 +643,7 @@ public class IamService implements SessionAccountLookup {
public List<IamPolicy> listAttachedUserPolicies(String userName, String pathPrefix) {
return getUser(userName).getAttachedPolicyArns().stream()
.flatMap(arn -> policies.get(arn).stream())
.flatMap(arn -> resolvePolicy(arn).stream())
.filter(p -> pathPrefix == null || p.getPath().startsWith(pathPrefix))
.toList();
}
@@ -664,7 +678,7 @@ public class IamService implements SessionAccountLookup {
public List<IamPolicy> listAttachedGroupPolicies(String groupName, String pathPrefix) {
return getGroup(groupName).getAttachedPolicyArns().stream()
.flatMap(arn -> policies.get(arn).stream())
.flatMap(arn -> resolvePolicy(arn).stream())
.filter(p -> pathPrefix == null || p.getPath().startsWith(pathPrefix))
.toList();
}
@@ -699,7 +713,7 @@ public class IamService implements SessionAccountLookup {
public List<IamPolicy> listAttachedRolePolicies(String roleName, String pathPrefix) {
return getRole(roleName).getAttachedPolicyArns().stream()
.flatMap(arn -> policies.get(arn).stream())
.flatMap(arn -> resolvePolicy(arn).stream())
.filter(p -> pathPrefix == null || p.getPath().startsWith(pathPrefix))
.toList();
}
@@ -1156,7 +1170,7 @@ public class IamService implements SessionAccountLookup {
private String resolveUserBoundaryDocument(String userName) {
return users.get(userName)
.map(IamUser::getPermissionsBoundaryArn)
.flatMap(arn -> policies.get(arn))
.flatMap(this::resolvePolicy)
.map(IamPolicy::getDefaultDocument)
.orElse(null);
}
@@ -1168,7 +1182,7 @@ public class IamService implements SessionAccountLookup {
String roleName = roleArn.contains("/") ? roleArn.substring(roleArn.lastIndexOf('/') + 1) : roleArn;
return roles.get(roleName)
.map(IamRole::getPermissionsBoundaryArn)
.flatMap(arn -> policies.get(arn))
.flatMap(this::resolvePolicy)
.map(IamPolicy::getDefaultDocument)
.orElse(null);
}
@@ -1227,7 +1241,7 @@ public class IamService implements SessionAccountLookup {
// User attached managed policies
for (String arn : user.getAttachedPolicyArns()) {
Optional<IamPolicy> p = policies.get(arn);
Optional<IamPolicy> p = resolvePolicy(arn);
if (p.isPresent() && p.get().getDefaultDocument() != null) {
docs.add(p.get().getDefaultDocument());
}
@@ -1240,7 +1254,7 @@ public class IamService implements SessionAccountLookup {
IamGroup group = groupOpt.get();
docs.addAll(group.getInlinePolicies().values());
for (String arn : group.getAttachedPolicyArns()) {
Optional<IamPolicy> p = policies.get(arn);
Optional<IamPolicy> p = resolvePolicy(arn);
if (p.isPresent() && p.get().getDefaultDocument() != null) {
docs.add(p.get().getDefaultDocument());
}
@@ -1267,7 +1281,7 @@ public class IamService implements SessionAccountLookup {
// Role attached managed policies
for (String arn : role.getAttachedPolicyArns()) {
Optional<IamPolicy> p = policies.get(arn);
Optional<IamPolicy> p = resolvePolicy(arn);
if (p.isPresent() && p.get().getDefaultDocument() != null) {
docs.add(p.get().getDefaultDocument());
}