mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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:
1 parent
c5feab635a
commit
72a4cc5515
6 files changed
+355
-18
No files matched your search
+246
@@ -0,0 +1,246 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.path.xml.XmlPath;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* ListRoles and ListPolicies page through {@code MaxItems} and {@code Marker}. Both requests model
|
||||
* {@code MaxItems} with a range of 1 to 1000 and document that an omitted value "defaults to 100".
|
||||
*
|
||||
* <p>IAM state is shared across the suite, so each test scopes its listing to a path of its own
|
||||
* and removes what it created.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class IamListPaginationIntegrationTest {
|
||||
|
||||
private static final String IAM_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260227/us-east-1/iam/aws4_request";
|
||||
private static final String TRUST_POLICY = """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",\
|
||||
"Principal":{"Service":"lambda.amazonaws.com"},"Action":"sts:AssumeRole"}]}""";
|
||||
private static final String POLICY_DOCUMENT = """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"s3:GetObject",\
|
||||
"Resource":"*"}]}""";
|
||||
|
||||
private static RequestSpecification iam(String action) {
|
||||
return given().header("Authorization", IAM_AUTH).formParam("Action", action);
|
||||
}
|
||||
|
||||
private static String suffix() {
|
||||
return Long.toString(System.nanoTime(), 36);
|
||||
}
|
||||
|
||||
private record Page(List<String> names, boolean truncated, String marker) {}
|
||||
|
||||
private static Page page(String action, String listElement, String nameElement,
|
||||
RequestSpecification request) {
|
||||
String result = action + "Response." + action + "Result.";
|
||||
XmlPath xml = request.when().post("/").then().statusCode(200).extract().xmlPath();
|
||||
String marker = xml.getString(result + "Marker");
|
||||
return new Page(xml.getList(result + listElement + ".member." + nameElement, String.class),
|
||||
Boolean.parseBoolean(xml.getString(result + "IsTruncated")),
|
||||
marker == null || marker.isEmpty() ? null : marker);
|
||||
}
|
||||
|
||||
private static Page rolePage(String pathPrefix, String maxItems, String marker) {
|
||||
RequestSpecification request = iam("ListRoles").formParam("PathPrefix", pathPrefix);
|
||||
if (maxItems != null) {
|
||||
request.formParam("MaxItems", maxItems);
|
||||
}
|
||||
if (marker != null) {
|
||||
request.formParam("Marker", marker);
|
||||
}
|
||||
return page("ListRoles", "Roles", "RoleName", request);
|
||||
}
|
||||
|
||||
private static Page policyPage(String scope, String pathPrefix, String maxItems, String marker) {
|
||||
RequestSpecification request = iam("ListPolicies").formParam("Scope", scope);
|
||||
if (pathPrefix != null) {
|
||||
request.formParam("PathPrefix", pathPrefix);
|
||||
}
|
||||
if (maxItems != null) {
|
||||
request.formParam("MaxItems", maxItems);
|
||||
}
|
||||
if (marker != null) {
|
||||
request.formParam("Marker", marker);
|
||||
}
|
||||
return page("ListPolicies", "Policies", "Arn", request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Follows {@code Marker} until {@code IsTruncated} is false, checking on every page that a
|
||||
* marker is present exactly when the page is truncated.
|
||||
*/
|
||||
private static List<Page> walk(Function<String, Page> next) {
|
||||
List<Page> pages = new ArrayList<>();
|
||||
String marker = null;
|
||||
do {
|
||||
Page current = next.apply(marker);
|
||||
if (current.truncated()) {
|
||||
assertNotNull(current.marker(), "a truncated page carries a Marker");
|
||||
} else {
|
||||
assertNull(current.marker(), "the last page carries no Marker");
|
||||
}
|
||||
pages.add(current);
|
||||
marker = current.marker();
|
||||
} while (marker != null);
|
||||
return pages;
|
||||
}
|
||||
|
||||
private static List<String> flatten(List<Page> pages) {
|
||||
List<String> all = new ArrayList<>();
|
||||
pages.forEach(p -> all.addAll(p.names()));
|
||||
return all;
|
||||
}
|
||||
|
||||
private static void createRole(String name, String path) {
|
||||
iam("CreateRole").formParam("RoleName", name).formParam("Path", path)
|
||||
.formParam("AssumeRolePolicyDocument", TRUST_POLICY)
|
||||
.when().post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
private static void deleteRole(String name) {
|
||||
iam("DeleteRole").formParam("RoleName", name).when().post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
private static String createPolicy(String name, String path) {
|
||||
return iam("CreatePolicy").formParam("PolicyName", name).formParam("Path", path)
|
||||
.formParam("PolicyDocument", POLICY_DOCUMENT)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.extract().path("CreatePolicyResponse.CreatePolicyResult.Policy.Arn");
|
||||
}
|
||||
|
||||
private static void deletePolicy(String arn) {
|
||||
iam("DeletePolicy").formParam("PolicyArn", arn).when().post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void listRolesPagesThroughThePathPrefixInNameOrder() {
|
||||
String tag = suffix();
|
||||
String path = "/pg-roles-" + tag + "/";
|
||||
List<String> expected = new ArrayList<>();
|
||||
for (int i = 0; i < 5; i++) {
|
||||
expected.add("pg-role-" + tag + "-" + i);
|
||||
}
|
||||
String outside = "pg-role-" + tag + "-outside";
|
||||
expected.forEach(name -> createRole(name, path));
|
||||
createRole(outside, "/pg-roles-other-" + tag + "/");
|
||||
try {
|
||||
List<Page> pages = walk(marker -> rolePage(path, "2", marker));
|
||||
|
||||
assertEquals(List.of(2, 2, 1), pages.stream().map(p -> p.names().size()).toList());
|
||||
assertEquals(List.of(true, true, false), pages.stream().map(Page::truncated).toList());
|
||||
assertEquals(expected, flatten(pages), "every role once, in name order, nothing outside the prefix");
|
||||
} finally {
|
||||
expected.forEach(IamListPaginationIntegrationTest::deleteRole);
|
||||
deleteRole(outside);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void listRolesDefaultsToOneHundredItems() {
|
||||
String tag = suffix();
|
||||
String path = "/pg-roles-default-" + tag + "/";
|
||||
List<String> names = new ArrayList<>();
|
||||
for (int i = 0; i < 101; i++) {
|
||||
names.add(String.format("pg-default-%s-%03d", tag, i));
|
||||
}
|
||||
names.forEach(name -> createRole(name, path));
|
||||
try {
|
||||
Page first = rolePage(path, null, null);
|
||||
assertEquals(100, first.names().size());
|
||||
assertTrue(first.truncated());
|
||||
assertNotNull(first.marker());
|
||||
|
||||
Page second = rolePage(path, null, first.marker());
|
||||
assertEquals(List.of(names.get(100)), second.names());
|
||||
assertFalse(second.truncated());
|
||||
assertNull(second.marker());
|
||||
} finally {
|
||||
names.forEach(IamListPaginationIntegrationTest::deleteRole);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPoliciesPagesThroughScopeAndPathPrefix() {
|
||||
String tag = suffix();
|
||||
String path = "/pg-policies-" + tag + "/";
|
||||
List<String> expected = new ArrayList<>();
|
||||
for (int i = 0; i < 5; i++) {
|
||||
expected.add(createPolicy("pg-policy-" + tag + "-" + i, path));
|
||||
}
|
||||
String outside = createPolicy("pg-policy-" + tag + "-outside", "/pg-policies-other-" + tag + "/");
|
||||
try {
|
||||
List<Page> pages = walk(marker -> policyPage("Local", path, "2", marker));
|
||||
|
||||
assertEquals(List.of(2, 2, 1), pages.stream().map(p -> p.names().size()).toList());
|
||||
assertEquals(List.of(true, true, false), pages.stream().map(Page::truncated).toList());
|
||||
assertEquals(expected, flatten(pages), "every policy once, in name order, nothing outside the prefix");
|
||||
} finally {
|
||||
expected.forEach(IamListPaginationIntegrationTest::deletePolicy);
|
||||
deletePolicy(outside);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPoliciesDefaultsToOneHundredAndWalksTheWholeCatalog() {
|
||||
Page first = policyPage("AWS", null, null, null);
|
||||
assertEquals(100, first.names().size(), "an omitted MaxItems defaults to 100");
|
||||
assertTrue(first.truncated());
|
||||
|
||||
List<String> byDefault = flatten(walk(marker -> policyPage("AWS", null, null, marker)));
|
||||
List<String> byLargest = flatten(walk(marker -> policyPage("AWS", null, "1000", marker)));
|
||||
|
||||
assertTrue(byDefault.size() > 100, "the AWS managed catalog is wider than one default page");
|
||||
assertEquals(byDefault.size(), new HashSet<>(byDefault).size(), "no policy repeats across pages");
|
||||
assertEquals(byLargest, byDefault, "the page size changes where pages split, not what they hold");
|
||||
}
|
||||
|
||||
@Test
|
||||
void listRolesAndListPoliciesRejectMaxItemsOutsideOneToOneThousand() {
|
||||
for (String action : List.of("ListRoles", "ListPolicies")) {
|
||||
iam(action).formParam("MaxItems", "0")
|
||||
.when().post("/").then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
|
||||
.body("ErrorResponse.Error.Message", equalTo(
|
||||
"1 validation error detected: Value '0' at 'maxItems' failed to satisfy "
|
||||
+ "constraint: Member must have value greater than or equal to 1"));
|
||||
|
||||
iam(action).formParam("MaxItems", "1001")
|
||||
.when().post("/").then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
|
||||
.body("ErrorResponse.Error.Message", equalTo(
|
||||
"1 validation error detected: Value '1001' at 'maxItems' failed to satisfy "
|
||||
+ "constraint: Member must have value less than or equal to 1000"));
|
||||
|
||||
iam(action).formParam("MaxItems", "1000").when().post("/").then().statusCode(200);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void listRolesAndListPoliciesRejectAMarkerTheyDidNotIssue() {
|
||||
for (String action : List.of("ListRoles", "ListPolicies")) {
|
||||
iam(action).formParam("Marker", "not-a-marker")
|
||||
.when().post("/").then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidInput"));
|
||||
}
|
||||
}
|
||||
}
|
||||
+19
-4
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.XmlParser;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.path.xml.XmlPath;
|
||||
import io.restassured.response.Response;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -81,10 +82,24 @@ class IamManagedPolicyAttachmentCountIntegrationTest {
|
||||
iam(accountId, "GetPolicy", Map.of("PolicyArn", POLICY_ARN)).then().statusCode(200)
|
||||
.body("GetPolicyResponse.GetPolicyResult.Policy.AttachmentCount",
|
||||
equalTo(Integer.toString(expectedCount)));
|
||||
iam(accountId, "ListPolicies", Map.of("Scope", "AWS")).then().statusCode(200)
|
||||
.body("ListPoliciesResponse.ListPoliciesResult.Policies.member.find { it.Arn == '"
|
||||
+ POLICY_ARN + "' }.AttachmentCount",
|
||||
equalTo(Integer.toString(expectedCount)));
|
||||
assertEquals(Integer.toString(expectedCount), listedAttachmentCount(accountId));
|
||||
}
|
||||
|
||||
/** ListPolicies pages the AWS managed catalog, so this follows the Marker to the policy. */
|
||||
private String listedAttachmentCount(String accountId) {
|
||||
String result = "ListPoliciesResponse.ListPoliciesResult.";
|
||||
Map<String, String> parameters = Map.of("Scope", "AWS", "MaxItems", "1000");
|
||||
while (true) {
|
||||
XmlPath page = iam(accountId, "ListPolicies", parameters).then().statusCode(200)
|
||||
.extract().xmlPath();
|
||||
String count = page.getString(result + "Policies.member.find { it.Arn == '"
|
||||
+ POLICY_ARN + "' }.AttachmentCount");
|
||||
if (!count.isEmpty()) {
|
||||
return count;
|
||||
}
|
||||
assertEquals("true", page.getString(result + "IsTruncated"), POLICY_ARN + " was never listed");
|
||||
parameters = Map.of("Scope", "AWS", "MaxItems", "1000", "Marker", page.getString(result + "Marker"));
|
||||
}
|
||||
}
|
||||
|
||||
private void changeAttachment(String accountId, String action, String entityType, String name) {
|
||||
|
||||
Reference in new issue
Block a user