fix(iam): page ListRoles and ListPolicies through MaxItems and Marker (#5515)

Both actions returned every match in one response with IsTruncated false.
They now use the existing paginate helper, so an omitted MaxItems returns
100 items, a MaxItems outside 1 to 1000 is a ValidationError, and Marker
appears only on a truncated page. PathPrefix and Scope filter first.

Roles and customer managed policies are listed in name order so a marker
keeps pointing at the same place between calls.
This commit is contained in:
lex00 authored and GitHub committed 2026-10-12 15:40:59 -04:00
1 parent c5feab635a
commit 72a4cc5515
6 files changed
+355 -18

No files matched your search

@@ -57,7 +57,7 @@ public class IamQueryHandler {
private static final Logger LOG = Logger.getLogger(IamQueryHandler.class);
/** {@code MaxItems} on the last-accessed readers: "defaults to 100", valid range 1 to 1000. */
/** {@code MaxItems} on the readers that page through {@link #paginate}: "defaults to 100", range 1 to 1000. */
private static final int DEFAULT_MAX_ITEMS = 100;
private static final int MAX_MAX_ITEMS = 1000;
/** {@code sortKeyType} on {@code GetOrganizationsAccessReport}. */
@@ -1351,12 +1351,15 @@ public class IamQueryHandler {
}
private Response handleListRoles(MultivaluedMap<String, String> params) {
List<IamRole> roleList = iamService.listRoles(getParam(params, "PathPrefix"));
Page<IamRole> page = paginate(iamService.listRoles(getParam(params, "PathPrefix")), params);
XmlBuilder xml = new XmlBuilder().start("Roles");
for (IamRole r : roleList) {
for (IamRole r : page.items()) {
xml.start("member").raw(roleXml(r, false)).end("member");
}
xml.end("Roles").elem("IsTruncated", false);
xml.end("Roles").elem("IsTruncated", page.truncated());
if (page.marker() != null) {
xml.elem("Marker", page.marker());
}
return Response.ok(AwsQueryResponse.envelope("ListRoles", AwsNamespaces.IAM, xml.build())).build();
}
@@ -1417,13 +1420,16 @@ public class IamQueryHandler {
}
private Response handleListPolicies(MultivaluedMap<String, String> params) {
List<IamPolicy> policyList = iamService.listPolicies(
getParam(params, "Scope"), getParam(params, "PathPrefix"));
Page<IamPolicy> page = paginate(iamService.listPolicies(
getParam(params, "Scope"), getParam(params, "PathPrefix")), params);
XmlBuilder xml = new XmlBuilder().start("Policies");
for (IamPolicy p : policyList) {
for (IamPolicy p : page.items()) {
xml.start("member").raw(policyXml(p, false)).end("member");
}
xml.end("Policies").elem("IsTruncated", false);
xml.end("Policies").elem("IsTruncated", page.truncated());
if (page.marker() != null) {
xml.elem("Marker", page.marker());
}
return Response.ok(AwsQueryResponse.envelope("ListPolicies", AwsNamespaces.IAM, xml.build())).build();
}
@@ -2418,7 +2424,7 @@ public class IamQueryHandler {
// rather than answered emptily just because the marker happens to sit past the end.
int from = markerIndex(params);
// An omitted MaxItems is not "no limit": the API reference states the count "defaults to
// 100" on both readers, so a report wider than that is truncated with a Marker exactly as
// 100" on each reader paged here, so a list wider than that is truncated with a Marker as
// it would be against AWS. Operations that model no MaxItems at all pass through whole.
int limit = maxItemsModeled ? DEFAULT_MAX_ITEMS : items.size();
if (maxItemsModeled) {
@@ -1319,6 +1319,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
String prefix = pathPrefix != null ? pathPrefix : "/";
return roles.scan(k -> true).stream()
.filter(r -> r.getPath().startsWith(prefix))
.sorted(Comparator.comparing(IamRole::getRoleName))
.toList();
}
@@ -1561,6 +1562,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
policies.scan(k -> true).stream()
.filter(p -> !AwsManagedPolicies.isManagedPolicyArn(p.getArn()))
.filter(p -> p.getPath().startsWith(prefix))
.sorted(Comparator.comparing(IamPolicy::getPolicyName))
.forEach(result::add);
}
if (includeAws) {