Light, fluffy, and always free - The AWS Local Emulator alternative
fix(iam): apply the documented MaxItems and ServiceNamespaces limits (#4543)
Priamo Germosen0f79cba0d9fb
diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/IamQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/iam/IamQueryHandler.javaindex cb6260b3cc..c88b52185b 100644--- a/src/main/java/io/github/hectorvent/floci/services/iam/IamQueryHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/iam/IamQueryHandler.java@@ -45,6 +45,12 @@ import java.util.stream.Stream;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. */+ private static final int DEFAULT_MAX_ITEMS = 100;+ private static final int MAX_MAX_ITEMS = 1000;+ /** {@code serviceNamespaceListType}: minimum 1 item, maximum 200. */+ private static final int MAX_SERVICE_NAMESPACES = 200;private static final int MAX_TAG_LIST_MEMBERS = 50;private static final int MAX_TAG_KEY_LENGTH = 128;private static final int MAX_TAG_VALUE_LENGTH = 256;@@ -1658,6 +1664,11 @@ public class IamQueryHandler {throw new AwsException("InvalidInput","The request must include at least one service namespace.", 400);}+ if (namespaces.size() > MAX_SERVICE_NAMESPACES) {+ throw new AwsException("ValidationError",+ "Value at 'serviceNamespaces' failed to satisfy constraint: Member must have "+ + "length less than or equal to " + MAX_SERVICE_NAMESPACES, 400);+ }List<GrantingPolicy> candidates = policiesForIdentity(accountId, arn);// The response list carries one entry per requested namespace, so that is what a Marker// walks through.@@ -1714,9 +1725,10 @@ public class IamQueryHandler {// Every input is validated before the bounds check, so a malformed request is rejected// rather than answered emptily just because the marker happens to sit past the end.int from = markerIndex(params);- // Validated only when actually supplied: an absent MaxItems means "no limit", which is not- // the same as a limit of zero, and an empty report would otherwise fail its own check.- int limit = items.size();+ // 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+ // 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) {String raw = params.getFirst("MaxItems");if (raw != null && !raw.isBlank()) {@@ -1726,10 +1738,11 @@ public class IamQueryHandler {throw new AwsException("InvalidInput","The value " + raw + " at 'maxItems' is not a number.", 400);}- if (limit < 1) {+ if (limit < 1 || limit > MAX_MAX_ITEMS) {throw new AwsException("ValidationError",- "Value at 'maxItems' failed to satisfy constraint: "- + "Member must have value greater than or equal to 1", 400);+ "Value at 'maxItems' failed to satisfy constraint: Member must have "+ + "value greater than or equal to 1 and less than or equal to "+ + MAX_MAX_ITEMS, 400);}}}diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/ServiceLastAccessedIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/ServiceLastAccessedIntegrationTest.javaindex 4de6426402..07703f74f3 100644--- a/src/test/java/io/github/hectorvent/floci/services/iam/ServiceLastAccessedIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/iam/ServiceLastAccessedIntegrationTest.java@@ -761,9 +761,14 @@ class ServiceLastAccessedIntegrationTest {equalTo("COMPLETED"));}- /** A huge MaxItems must page, not overflow into a negative index. */+ /**+ * The largest MaxItems the API allows, combined with a Marker, must page rather than overflow+ * into a negative index. The documented ceiling of 1000 now bounds this, so a value large+ * enough to overflow is rejected before it reaches the arithmetic; the guard stays because the+ * ceiling and the offset are enforced in different places.+ */@Test- void getServiceLastAccessedDetailsSurvivesAnEnormousMaxItems() {+ void getServiceLastAccessedDetailsSurvivesTheLargestAllowedMaxItems() {String tag = suffix();String userName = "laa-overflow-" + tag;String arn = createUser(userName);@@ -776,12 +781,151 @@ class ServiceLastAccessedIntegrationTest {iam("GetServiceLastAccessedDetails").formParam("JobId", generateJobFor(arn)).formParam("Marker", "1")- .formParam("MaxItems", Integer.toString(Integer.MAX_VALUE))+ .formParam("MaxItems", "1000").when().post("/").then().statusCode(200).body(containsString("<ServiceNamespace>s3</ServiceNamespace>"));}+ /**+ * The API reference states the item count "defaults to 100" when MaxItems is omitted, so a+ * report wider than that comes back truncated with a Marker rather than whole.+ */+ @Test+ void getServiceLastAccessedDetailsDefaultsToAHundredItems() {+ String tag = suffix();+ String userName = "laa-default-page-" + tag;+ String arn = createUser(userName);+ StringBuilder actions = new StringBuilder();+ for (int i = 1; i <= 120; i++) {+ actions.append(actions.isEmpty() ? "" : ",").append("\"svc").append(i).append(":Do\"");+ }+ iam("PutUserPolicy").formParam("UserName", userName)+ .formParam("PolicyName", "laa-default-page-policy-" + tag)+ .formParam("PolicyDocument", "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","+ + "\"Action\":[" + actions + "],\"Resource\":\"*\"}]}")+ .when().post("/").then().statusCode(200);++ String jobId = generateJobFor(arn);+ String first = iam("GetServiceLastAccessedDetails").formParam("JobId", jobId)+ .when().post("/").then()+ .statusCode(200)+ .body("GetServiceLastAccessedDetailsResponse.GetServiceLastAccessedDetailsResult.IsTruncated",+ equalTo("true"))+ .extract().asString();++ assertEquals(100, first.split("<ServiceNamespace>", -1).length - 1,+ "expected the documented default page of 100 services");++ // A truncated page the caller cannot continue from is useless, so the Marker has to be+ // there and has to actually fetch the rest.+ String marker = iam("GetServiceLastAccessedDetails").formParam("JobId", jobId)+ .when().post("/").then().statusCode(200)+ .extract().path("GetServiceLastAccessedDetailsResponse."+ + "GetServiceLastAccessedDetailsResult.Marker");+ assertTrue(marker != null && !marker.isBlank(),+ "a truncated response must carry a Marker, got: " + marker);++ String second = iam("GetServiceLastAccessedDetails")+ .formParam("JobId", jobId).formParam("Marker", marker)+ .when().post("/").then()+ .statusCode(200)+ .body("GetServiceLastAccessedDetailsResponse.GetServiceLastAccessedDetailsResult.IsTruncated",+ equalTo("false"))+ .extract().asString();++ assertEquals(20, second.split("<ServiceNamespace>", -1).length - 1,+ "expected the remaining 20 services on the continued page");+ }++ /**+ * The page bounds are shared by both readers, so the entities reader is held to the same+ * documented default rather than only the service reader being covered.+ */+ @Test+ void getServiceLastAccessedDetailsWithEntitiesDefaultsToAHundredItems() {+ String tag = suffix();+ String groupName = "laa-ent-page-" + tag;+ iam("CreateGroup").formParam("GroupName", groupName)+ .when().post("/").then().statusCode(200);+ iam("PutGroupPolicy").formParam("GroupName", groupName)+ .formParam("PolicyName", "laa-ent-page-policy-" + tag)+ .formParam("PolicyDocument", S3_POLICY)+ .when().post("/").then().statusCode(200);+ for (int i = 1; i <= 120; i++) {+ String member = "laa-ent-page-u" + i + "-" + tag;+ createUser(member);+ iam("AddUserToGroup").formParam("GroupName", groupName).formParam("UserName", member)+ .when().post("/").then().statusCode(200);+ }++ String jobId = generateJobFor("arn:aws:iam::000000000000:group/" + groupName);+ String first = iam("GetServiceLastAccessedDetailsWithEntities")+ .formParam("JobId", jobId).formParam("ServiceNamespace", "s3")+ .when().post("/").then()+ .statusCode(200)+ .body("GetServiceLastAccessedDetailsWithEntitiesResponse."+ + "GetServiceLastAccessedDetailsWithEntitiesResult.IsTruncated", equalTo("true"))+ .extract().asString();++ assertEquals(100, first.split("<EntityInfo>", -1).length - 1,+ "expected the documented default page of 100 entities");++ String marker = iam("GetServiceLastAccessedDetailsWithEntities")+ .formParam("JobId", jobId).formParam("ServiceNamespace", "s3")+ .when().post("/").then().statusCode(200)+ .extract().path("GetServiceLastAccessedDetailsWithEntitiesResponse."+ + "GetServiceLastAccessedDetailsWithEntitiesResult.Marker");++ iam("GetServiceLastAccessedDetailsWithEntities")+ .formParam("JobId", jobId).formParam("ServiceNamespace", "s3")+ .formParam("Marker", marker)+ .when().post("/").then()+ .statusCode(200)+ .body("GetServiceLastAccessedDetailsWithEntitiesResponse."+ + "GetServiceLastAccessedDetailsWithEntitiesResult.IsTruncated", equalTo("false"));+ }++ /** The 1000 ceiling is shared by both readers too. */+ @Test+ void getServiceLastAccessedDetailsWithEntitiesRejectsMaxItemsAboveTheCeiling() {+ String arn = createUser("laa-ent-ceiling-" + suffix());++ iam("GetServiceLastAccessedDetailsWithEntities")+ .formParam("JobId", generateJobFor(arn))+ .formParam("ServiceNamespace", "s3")+ .formParam("MaxItems", "1001")+ .when().post("/").then()+ .statusCode(400)+ .body(containsString("ValidationError"));+ }++ /** MaxItems is documented as 1 to 1000, so the ceiling is enforced as well as the floor. */+ @Test+ void getServiceLastAccessedDetailsRejectsMaxItemsAboveTheDocumentedCeiling() {+ String arn = createUser("laa-maxitems-ceiling-" + suffix());++ iam("GetServiceLastAccessedDetails")+ .formParam("JobId", generateJobFor(arn)).formParam("MaxItems", "1001")+ .when().post("/").then()+ .statusCode(400)+ .body(containsString("ValidationError"));+ }++ /** serviceNamespaceListType allows at most 200 members. */+ @Test+ void listPoliciesGrantingServiceAccessRejectsMoreThanTwoHundredNamespaces() {+ String arn = createUser("laa-ns-limit-" + suffix());++ RequestSpecification request = iam("ListPoliciesGrantingServiceAccess").formParam("Arn", arn);+ for (int i = 1; i <= 201; i++) {+ request.formParam("ServiceNamespaces.member." + i, "svc" + i);+ }+ request.when().post("/").then()+ .statusCode(400)+ .body(containsString("ValidationError"));+ }+/** An empty report is a valid answer, not a request that fails its own page-size check. */@Testvoid getServiceLastAccessedDetailsReturnsAnEmptyReportWithoutMaxItems() {