Light, fluffy, and always free - The AWS Local Emulator alternative
fix(iam): match IAM's constraint-violation message shape (#4613)
Priamo Germosen240fd865f09a
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 4f46d3b64c..21c525394d 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@@ -1663,11 +1663,7 @@ 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);- }+ checkListLength(namespaces.size(), "serviceNamespaces", MAX_SERVICE_NAMESPACES);List<GrantingPolicy> candidates = policiesForIdentity(accountId, arn);// The response list carries one entry per requested namespace, so that is what a Marker// walks through.@@ -1737,11 +1733,13 @@ public class IamQueryHandler {throw new AwsException("InvalidInput","The value " + raw + " at 'maxItems' is not a number.", 400);}- 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 and less than or equal to "- + MAX_MAX_ITEMS, 400);+ if (limit < 1) {+ throw validationError(raw, "maxItems",+ "Member must have value greater than or equal to 1");+ }+ if (limit > MAX_MAX_ITEMS) {+ throw validationError(raw, "maxItems",+ "Member must have value less than or equal to " + MAX_MAX_ITEMS);}}}@@ -2196,14 +2194,14 @@ public class IamQueryHandler {String at = "tags." + (i + 1) + ".member";String keyViolation = tagKeyViolation(key);if (keyViolation != null) {- throw tagValidationError(key, at + ".key", keyViolation);+ throw validationError(key, at + ".key", keyViolation);}if (value.codePointCount(0, value.length()) > MAX_TAG_VALUE_LENGTH) {- throw tagValidationError(value, at + ".value",+ throw validationError(value, at + ".value","Member must have length less than or equal to " + MAX_TAG_VALUE_LENGTH);}if (!TAG_VALUE_PATTERN.matcher(value).matches()) {- throw tagValidationError(value, at + ".value",+ throw validationError(value, at + ".value","Member must satisfy regular expression pattern: " + TAG_VALUE_PATTERN.pattern());}if (caseInsensitiveKeys) {@@ -2225,10 +2223,18 @@ public class IamQueryHandler {}private static void checkListLength(int members, String param) {- if (members > MAX_TAG_LIST_MEMBERS) {+ checkListLength(members, param, MAX_TAG_LIST_MEMBERS);+ }++ /**+ * IAM's constraint-violation shape for a list member. Unlike the scalar form it does not quote+ * the value, matching how AWS reports an over-long list rather than echoing every member back.+ */+ private static void checkListLength(int members, String param, int limit) {+ if (members > limit) {throw new AwsException("ValidationError","1 validation error detected: Value at '" + param + "' failed to satisfy constraint: "- + "Member must have length less than or equal to " + MAX_TAG_LIST_MEMBERS, 400);+ + "Member must have length less than or equal to " + limit, 400);}}@@ -2246,7 +2252,12 @@ public class IamQueryHandler {return null;}- private static AwsException tagValidationError(String value, String at, String constraint) {+ /**+ * IAM's constraint-violation shape for a scalar member, which leads with the error count and+ * quotes the offending value. Only the bound actually broken is named, as AWS does: a value+ * outside a range reports the side it fell outside, not the whole range.+ */+ private static AwsException validationError(String value, String at, String constraint) {return new AwsException("ValidationError","1 validation error detected: Value '" + value + "' at '" + at + "' failed to satisfy constraint: "+ constraint, 400);@@ -2273,7 +2284,7 @@ public class IamQueryHandler {for (String key : keys) {String violation = tagKeyViolation(key);if (violation != null) {- throw tagValidationError(keys.toString(), "tagKeys",+ throw validationError(keys.toString(), "tagKeys","Member must satisfy constraint: [" + violation + "]");}}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 0aa666d95d..a98334bcff 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@@ -801,7 +801,10 @@ class ServiceLastAccessedIntegrationTest {.formParam("JobId", jobId).formParam("Marker", "999").formParam("MaxItems", "0").when().post("/").then().statusCode(400)- .body(containsString("ValidationError"));+ .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"));}@Test@@ -1006,7 +1009,13 @@ class ServiceLastAccessedIntegrationTest {.formParam("JobId", generateJobFor(arn)).formParam("MaxItems", "1001").when().post("/").then().statusCode(400)- .body(containsString("ValidationError"));+ .body("ErrorResponse.Error.Code", equalTo("ValidationError"))+ // IAM leads with the count, quotes the offending value, and names only the bound that+ // broke: an over-large MaxItems says nothing about the minimum. Asserted on the parsed+ // element because the raw body escapes the quotes as '.+ .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"));}/** serviceNamespaceListType allows at most 200 members. */@@ -1018,9 +1027,16 @@ class ServiceLastAccessedIntegrationTest {for (int i = 1; i <= 201; i++) {request.formParam("ServiceNamespaces.member." + i, "svc" + i);}- request.when().post("/").then()+ String body = request.when().post("/").then().statusCode(400)- .body(containsString("ValidationError"));+ .body("ErrorResponse.Error.Code", equalTo("ValidationError"))+ .body("ErrorResponse.Error.Message", equalTo(+ "1 validation error detected: Value at 'serviceNamespaces' failed to satisfy "+ + "constraint: Member must have length less than or equal to 200"))+ .extract().asString();++ // The list form does not quote the value, so a 201-member request must not echo them back.+ assertTrue(!body.contains("svc201"), "expected the members not to be echoed, in: " + body);}/** An empty report is a valid answer, not a request that fails its own page-size check. */