diff --git a/docs/services/iam.md b/docs/services/iam.md index 106e69ce4e..446f1ebc89 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -505,10 +505,15 @@ validator's one sentence for both cases, `Syntax errors in policy.`, and to the language defines: a `Version` outside `2012-10-17` and `2008-10-17` answers that same sentence, a statement whose `Effect` is not literally `Allow` or `Deny` answers `The policy failed legacy parsing`, and a resource named neither by ARN nor by `*` answers `Resource must be in ARN -format or "*".` A managed policy, on `CreatePolicy` and `CreatePolicyVersion`, is held to those and +format or "*".` A value that is no string names the text of that value, as the `Version` and the +`Effect` already do, so `"Resource": 123` answers that sentence exactly as `"Resource": "123"` +does; an element carrying null names nothing, which is the element's own case rather than a value +to read. A managed policy, on `CreatePolicy` and `CreatePolicyVersion`, is held to those and to its statements' own elements besides, `Policy statement must contain actions.` and `Policy statement must contain resources.`, where a statement that is no object at all is the syntax case -rather than a missing element. +rather than a missing element. One clause the two validators do not word alike: an `Effect` the +language does not define answers `The policy failed legacy parsing` on an inline write and +`Syntax errors in policy.` on a managed one, so the sentence follows the validator that read it. A document carrying `Statement` with nothing in it is still a policy, one that grants nothing, and a member the request leaves out is still answered as absent by the constraints ahead of the operation. diff --git a/src/main/java/io/floci/aws/services/iam/IamPolicyDocuments.java b/src/main/java/io/floci/aws/services/iam/IamPolicyDocuments.java index 200bb19354..c4bc80e0b6 100644 --- a/src/main/java/io/floci/aws/services/iam/IamPolicyDocuments.java +++ b/src/main/java/io/floci/aws/services/iam/IamPolicyDocuments.java @@ -72,7 +72,12 @@ final class IamPolicyDocuments { private static final String STATEMENT_WITHOUT_RESOURCES = "Policy statement must contain resources."; - /** What a statement declaring an effect the language does not define is refused with. */ + /** + * What an INLINE write refuses a statement declaring an effect the language does not define + * with. The managed validator words that same clause {@link #SYNTAX_ERRORS_IN_POLICY}, which is + * the one clause of an identity-based document the two validators do not word alike, so each + * passes the sentence its own callers are owed. + */ private static final String POLICY_PARSE_FAILED = "The policy failed legacy parsing"; private static final String ANY_RESOURCE = "*"; @@ -121,7 +126,7 @@ final class IamPolicyDocuments { return; } checkIdentityGrammar(document, false); - checkIdentityValues(document); + checkIdentityValues(document, POLICY_PARSE_FAILED); } /** @@ -134,7 +139,7 @@ final class IamPolicyDocuments { return; } checkIdentityGrammar(document, true); - checkIdentityValues(document); + checkIdentityValues(document, SYNTAX_ERRORS_IN_POLICY); } /** @@ -168,7 +173,7 @@ final class IamPolicyDocuments { * document the grammar accepted, so a text that is no policy is answered as one rather than * through whichever value it happens to carry. */ - private static void checkIdentityValues(String document) { + private static void checkIdentityValues(String document, String unreadableEffect) { JsonNode root = parse(document); if (root == null || !root.isObject()) { return; @@ -180,7 +185,7 @@ final class IamPolicyDocuments { for (JsonNode statement : statements(root)) { JsonNode effect = statement.get("Effect"); if (effect != null && !EFFECTS.contains(effect.asText())) { - throw malformed(POLICY_PARSE_FAILED); + throw malformed(unreadableEffect); } for (String resource : resourceValues(statement)) { if (!resource.startsWith(ARN_PREFIX) && !ANY_RESOURCE.equals(resource)) { @@ -261,7 +266,14 @@ final class IamPolicyDocuments { return false; } - /** Every value a statement's resource elements name, in the order the document carries them. */ + /** + * Every value a statement's resource elements name, in the order the document carries them. A + * value that is no string names the text of that value, which is how the {@code Version} and + * the {@code Effect} above are already read: a number or a boolean is held to naming a resource + * rather than passed over, so {@code 123} and {@code "123"} are refused alike. A null names + * nothing, as {@link #namesSomething} reads it, and a list or an object is no value of this + * element at all. + */ private static List resourceValues(JsonNode statement) { List values = new ArrayList<>(); for (String spelling : new String[] {"Resource", "NotResource"}) { @@ -270,7 +282,7 @@ final class IamPolicyDocuments { continue; } for (JsonNode value : element.isArray() ? element : List.of(element)) { - if (value.isTextual()) { + if (value.isValueNode() && !value.isNull()) { values.add(value.asText()); } } diff --git a/src/test/java/io/floci/aws/services/iam/IamPolicyDocumentWriteIntegrationTest.java b/src/test/java/io/floci/aws/services/iam/IamPolicyDocumentWriteIntegrationTest.java index 4ca4c0102e..177d8a7a5c 100644 --- a/src/test/java/io/floci/aws/services/iam/IamPolicyDocumentWriteIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/iam/IamPolicyDocumentWriteIntegrationTest.java @@ -90,6 +90,25 @@ class IamPolicyDocumentWriteIntegrationTest { return arn; } + /** + * A {@code PutUserPolicy} of {@code document} on a holder these cases share, which is all the + * inline cases differ in: the write is the same one every time. + */ + private RequestSpecification inlinePolicyWrite(String userName, String policyName, + String document) { + return iam("PutUserPolicy") + .formParam("UserName", userName) + .formParam("PolicyName", policyName) + .formParam("PolicyDocument", document); + } + + /** A {@code CreatePolicy} of {@code document} under a name no other case holds. */ + private RequestSpecification managedPolicyWrite(String policyNamePrefix, String document) { + return iam("CreatePolicy") + .formParam("PolicyName", unique(policyNamePrefix)) + .formParam("PolicyDocument", document); + } + /** * A role's trust policy is read by a validator of IAM's own, which words a document that does * not parse as a JSON object apart from one that parses and describes no statement. @@ -180,21 +199,16 @@ class IamPolicyDocumentWriteIntegrationTest { */ @Test void aManagedPolicysStatementsAreHeldToTheElementsTheGrammarRequires() { - expectMalformed(iam("CreatePolicy") - .formParam("PolicyName", unique("write-managed-actions")) - .formParam("PolicyDocument", - "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\"," - + "\"Resource\":\"*\"}]}"), + expectMalformed(managedPolicyWrite("write-managed-actions", + "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\"," + + "\"Resource\":\"*\"}]}"), "Policy statement must contain actions."); - expectMalformed(iam("CreatePolicy") - .formParam("PolicyName", unique("write-managed-resources")) - .formParam("PolicyDocument", - "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\"," - + "\"Action\":\"s3:GetObject\"}]}"), + expectMalformed(managedPolicyWrite("write-managed-resources", + "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\"," + + "\"Action\":\"s3:GetObject\"}]}"), "Policy statement must contain resources."); - expectMalformed(iam("CreatePolicy") - .formParam("PolicyName", unique("write-managed-statement")) - .formParam("PolicyDocument", "{\"Statement\":\"allow everything\"}"), + expectMalformed(managedPolicyWrite("write-managed-statement", + "{\"Statement\":\"allow everything\"}"), "Syntax errors in policy."); } @@ -223,35 +237,23 @@ class IamPolicyDocumentWriteIntegrationTest { void theValuesAnIdentityPolicyDeclaresAreReadAgainstTheLanguage() { String userName = createUser("write-values"); - expectMalformed(iam("PutUserPolicy") - .formParam("UserName", userName) - .formParam("PolicyName", "write-values") - .formParam("PolicyDocument", - "{\"Version\":\"2019-01-01\",\"Statement\":[{\"Effect\":\"Allow\"," - + "\"Action\":\"s3:GetObject\",\"Resource\":\"*\"}]}"), + expectMalformed(inlinePolicyWrite(userName, "write-values", + "{\"Version\":\"2019-01-01\",\"Statement\":[{\"Effect\":\"Allow\"," + + "\"Action\":\"s3:GetObject\",\"Resource\":\"*\"}]}"), "Syntax errors in policy."); - expectMalformed(iam("PutUserPolicy") - .formParam("UserName", userName) - .formParam("PolicyName", "write-values") - .formParam("PolicyDocument", - "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"allow\"," - + "\"Action\":\"s3:GetObject\",\"Resource\":\"*\"}]}"), + expectMalformed(inlinePolicyWrite(userName, "write-values", + "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"allow\"," + + "\"Action\":\"s3:GetObject\",\"Resource\":\"*\"}]}"), "The policy failed legacy parsing"); - expectMalformed(iam("PutUserPolicy") - .formParam("UserName", userName) - .formParam("PolicyName", "write-values") - .formParam("PolicyDocument", - "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\"," - + "\"Action\":\"s3:GetObject\",\"Resource\":\"my-bucket\"}]}"), + expectMalformed(inlinePolicyWrite(userName, "write-values", + "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\"," + + "\"Action\":\"s3:GetObject\",\"Resource\":\"my-bucket\"}]}"), "Resource my-bucket must be in ARN format or \"*\"."); // The version the language also defines, and a resource named by ARN, are both taken. - iam("PutUserPolicy") - .formParam("UserName", userName) - .formParam("PolicyName", "write-values") - .formParam("PolicyDocument", - "{\"Version\":\"2008-10-17\",\"Statement\":[{\"Effect\":\"Deny\"," - + "\"Action\":\"s3:GetObject\",\"Resource\":\"arn:aws:s3:::bucket/*\"}]}") + inlinePolicyWrite(userName, "write-values", + "{\"Version\":\"2008-10-17\",\"Statement\":[{\"Effect\":\"Deny\"," + + "\"Action\":\"s3:GetObject\",\"Resource\":\"arn:aws:s3:::bucket/*\"}]}") .when().post("/").then().statusCode(200); cleanup.register(() -> iam("DeleteUserPolicy").formParam("UserName", userName) .formParam("PolicyName", "write-values").when().post("/").then().statusCode(200)); @@ -318,4 +320,67 @@ class IamPolicyDocumentWriteIntegrationTest { .body(CODE, equalTo(MALFORMED)) .body(MESSAGE, equalTo("Syntax errors in policy.")); } + + /** + * A resource value that is no string names the text of that value, so it is held to the same + * rule as the string spelling it: {@code "Resource": 123} and {@code "Resource": "123"} are + * one case, where the first used to be passed over and stored. The {@code Version} and the + * {@code Effect} are already read that way, and both validators word this clause alike. + */ + @Test + void aResourceValueThatIsNoStringIsHeldToTheArnRuleTheStringSpellingIs() { + String userName = createUser("write-nonstring"); + + expectMalformed(inlinePolicyWrite(userName, "write-nonstring", + "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\"," + + "\"Action\":\"s3:GetObject\",\"Resource\":123}]}"), + "Resource 123 must be in ARN format or \"*\"."); + expectMalformed(inlinePolicyWrite(userName, "write-nonstring", + "{\"Statement\":[{\"Effect\":\"Allow\",\"Action\":\"s3:GetObject\"," + + "\"Resource\":[\"arn:aws:s3:::reports/*\",7]}]}"), + "Resource 7 must be in ARN format or \"*\"."); + expectMalformed(inlinePolicyWrite(userName, "write-nonstring", + "{\"Statement\":[{\"Effect\":\"Allow\",\"Action\":\"s3:GetObject\"," + + "\"NotResource\":true}]}"), + "Resource true must be in ARN format or \"*\"."); + expectMalformed(managedPolicyWrite("write-nonstring-managed", + "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\"," + + "\"Action\":\"s3:GetObject\",\"Resource\":123}]}"), + "Resource 123 must be in ARN format or \"*\"."); + + // A null element still names nothing rather than a value to check, which is the element's + // own sentence and not this one. + expectMalformed(managedPolicyWrite("write-null-resource", + "{\"Statement\":[{\"Effect\":\"Allow\",\"Action\":\"s3:GetObject\"," + + "\"Resource\":null}]}"), + "Policy statement must contain resources."); + } + + /** + * The one clause the two identity validators do not word alike: an {@code Effect} the language + * does not define answers the inline validator's sentence on an inline write and the managed + * one's on a managed write, whichever way the value misses, spelling or type. + */ + @Test + void anEffectTheLanguageDoesNotDefineIsWordedByTheValidatorThatReadsIt() { + String userName = createUser("write-effect-wording"); + String arn = createManagedPolicy("write-effect-wording"); + String lowercase = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"allow\"," + + "\"Action\":\"s3:GetObject\",\"Resource\":\"*\"}]}"; + String undefined = "{\"Statement\":[{\"Effect\":\"Permit\",\"Action\":\"s3:GetObject\"," + + "\"Resource\":\"*\"}]}"; + String noString = "{\"Statement\":[{\"Effect\":true,\"Action\":\"s3:GetObject\"," + + "\"Resource\":\"*\"}]}"; + + for (String document : new String[] {lowercase, undefined, noString}) { + expectMalformed(managedPolicyWrite("write-effect-managed", document), + "Syntax errors in policy."); + expectMalformed(iam("CreatePolicyVersion") + .formParam("PolicyArn", arn) + .formParam("PolicyDocument", document), + "Syntax errors in policy."); + expectMalformed(inlinePolicyWrite(userName, "write-effect-wording", document), + "The policy failed legacy parsing"); + } + } }