Light, fluffy, and always free - The AWS Local Emulator alternative
fix(iam): separate a constraint failure from an invalid value in PutAccountProperties (#5240)
Priamo Germosen5fc8b0418c4d
diff --git a/docs/services/iam.md b/docs/services/iam.mdindex f970206d0e..80f80f7913 100644--- a/docs/services/iam.md+++ b/docs/services/iam.md@@ -549,6 +549,15 @@ states directly, so a request mixing namespaces is rejected whole rather than paManager is the only namespace AWS names, and any well-formed key is accepted rather than only thatone, since an allowlist of one would refuse keys AWS takes.+Those two rules are not the same kind of failure, and they answer differently. The length and the+pattern are constraints the API Reference publishes on the parameter, and it defines+`ValidationError` as the common error for input that "doesn't meet the required format or+constraints", so a key that is too long or outside the pattern, and a value outside 1 to 1024, are+`ValidationError` with status 400. The `Namespace/PropertyName` rule cannot be expressed in the+published pattern, which admits a key with no slash, with several, and with a trailing one, so it is+a rule applied once the format is already satisfied: that, and a request mixing namespaces, are+`InvalidInput` with status 400, the "invalid or out-of-range value" the operation declares.+**Enabling federation twice is an error, and so is disabling it twice**, which is the asymmetryworth noticing: `EnableOutboundWebIdentityFederation` answers `FeatureEnabled` with status 409,while `DisableOutboundWebIdentityFederation` answers `FeatureDisabled` with status **404**. AWSdiff --git a/src/main/java/io/github/hectorvent/floci/services/iam/IamService.java b/src/main/java/io/github/hectorvent/floci/services/iam/IamService.javaindex 2b0d29f10c..f117f4db20 100644--- a/src/main/java/io/github/hectorvent/floci/services/iam/IamService.java+++ b/src/main/java/io/github/hectorvent/floci/services/iam/IamService.java@@ -2341,11 +2341,20 @@ public class IamService implements SessionAccountLookup, ResourceProvider {properties.size(), namespace);}- /** The namespace half of the key, having checked the whole key against the model. */+ /**+ * The namespace half of the key, having checked the whole key against the model.+ *+ * <p>The two rejections here are not the same kind of error. The length and the pattern are+ * published constraints on the parameter, and the API Reference defines {@code ValidationError}+ * as the common error for input that "doesn't meet the required format or constraints". The+ * {@code Namespace/PropertyName} rule is not expressible in the published pattern, which admits+ * no slash, several, and a trailing one, so it is a rule the service applies after the format+ * is already satisfied: that is {@code InvalidInput}, "an invalid or out-of-range value".+ */private String validateAccountPropertyKey(String key) {if (key == null || key.isEmpty() || key.length() > MAX_ACCOUNT_PROPERTY_KEY_LENGTH|| !ACCOUNT_PROPERTY_KEY_PATTERN.matcher(key).matches()) {- throw new AwsException("InvalidInput",+ throw new AwsException("ValidationError","Value '" + key + "' at 'properties' failed to satisfy constraint: Map keys "+ "must satisfy constraint: [Member must have length less than or "+ "equal to " + MAX_ACCOUNT_PROPERTY_KEY_LENGTH + ", Member must "@@ -2355,9 +2364,9 @@ public class IamService implements SessionAccountLookup, ResourceProvider {int separator = key.indexOf('/');if (separator <= 0 || separator != key.lastIndexOf('/') || separator == key.length() - 1) {throw new AwsException("InvalidInput",- "Value '" + key + "' at 'properties' failed to satisfy constraint: a key is "- + "Namespace/PropertyName, with exactly one forward slash and neither "- + "a leading nor a trailing one.", 400);+ "The property key '" + key + "' is not Namespace/PropertyName: a key has "+ + "exactly one forward slash, and neither a leading nor a trailing "+ + "one.", 400);}return key.substring(0, separator);}@@ -2365,7 +2374,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {private void validateAccountPropertyValue(String key, String value) {if (value == null || value.isEmpty()|| value.length() > MAX_ACCOUNT_PROPERTY_VALUE_LENGTH) {- throw new AwsException("InvalidInput",+ throw new AwsException("ValidationError","Value at 'properties." + key + "' failed to satisfy constraint: Member must "+ "have length between 1 and " + MAX_ACCOUNT_PROPERTY_VALUE_LENGTH,400);diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/AccountPropertiesIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/AccountPropertiesIntegrationTest.javaindex 8e2dfda14e..45ab1e117e 100644--- a/src/test/java/io/github/hectorvent/floci/services/iam/AccountPropertiesIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/iam/AccountPropertiesIntegrationTest.java@@ -75,16 +75,47 @@ class AccountPropertiesIntegrationTest {.body(not(containsString(second + "/Beta")));}- /** The key is Namespace/PropertyName: exactly one slash, not at either end. */+ /**+ * A key can be wrong in two ways that AWS reports differently, so they get a test each rather+ * than one list asserting the same code for both.+ *+ * <p>These keys violate the published pattern {@code ^[A-Za-z][A-Za-z0-9/_-]*$}, either by+ * starting with something other than a letter or by carrying a character outside the class.+ * That is a format failure, which the API Reference gives to the common+ * {@code ValidationError}.+ */+ @Test+ void aKeyOutsideThePublishedPatternIsAValidationError() {+ for (String key : List.of("/LeadingSlash", "1StartsWithADigit", "Has Space/Prop",+ "Ns/Prop!")) {+ iam("PutAccountProperties")+ .formParam("Properties.entry.1.key", key)+ .formParam("Properties.entry.1.value", "x")+ .when().post("/").then().statusCode(400)+ .body(containsString("ValidationError"))+ .body(not(containsString("InvalidInput")))+ .body(containsString("failed to satisfy constraint"));+ }+ }++ /**+ * These three satisfy the published pattern, which admits no slash, several, and a trailing+ * one, and fail only the Namespace/PropertyName rule. The service applies that after the+ * format is already good, so it is {@code InvalidInput} rather than a constraint failure.+ */@Test- void aMalformedKeyIsRejected() {- for (String key : List.of("NoSlash", "/LeadingSlash", "Trailing/", "Two/Slashes/Here",- "1StartsWithADigit", "Has Space/Prop", "Ns/Prop!")) {+ void aKeyThatIsNotNamespaceSlashPropertyIsInvalidInput() {+ for (String key : List.of("NoSlash", "Trailing/", "Two/Slashes/Here")) {iam("PutAccountProperties").formParam("Properties.entry.1.key", key).formParam("Properties.entry.1.value", "x").when().post("/").then().statusCode(400)- .body(containsString("InvalidInput"));+ .body(containsString("InvalidInput"))+ .body(not(containsString("ValidationError")))+ // The wording matters as much as the code here: carrying the constraint phrasing+ // on an InvalidInput is the mismatch this split exists to remove.+ .body(containsString("is not Namespace/PropertyName"))+ .body(not(containsString("failed to satisfy constraint")));}}@@ -95,13 +126,13 @@ class AccountPropertiesIntegrationTest {iam("PutAccountProperties").formParam("Properties.entry.1.key", ns + "/" + "P".repeat(60)).formParam("Properties.entry.1.value", "x")- .when().post("/").then().statusCode(400).body(containsString("InvalidInput"));+ .when().post("/").then().statusCode(400).body(containsString("ValidationError"));// valueLength is 1 to 1024.iam("PutAccountProperties").formParam("Properties.entry.1.key", ns + "/TooLongValue").formParam("Properties.entry.1.value", "v".repeat(1025))- .when().post("/").then().statusCode(400).body(containsString("InvalidInput"));+ .when().post("/").then().statusCode(400).body(containsString("ValidationError"));// And the top of the range is accepted, so the bound is not off by one.iam("PutAccountProperties")