com.amazonaws.auth.policy.Policy.fromJson() does not parse Statements with no list correctly
If you have a policy where there is only one statement not contained in a list [] then com.amazonaws.auth.policy.Policy.fromJson() does not parse the statement correctly into a Policy object. Stock policies that can't be parsed.
policyarn:aws:iam::aws:policy/AWSCertificateManagerReadOnly policyarn:aws:iam::aws:policy/AWSElementalMediaPackageFullAccess policyarn:aws:iam::aws:policy/AWSElementalMediaPackageReadOnly policyarn:aws:iam::aws:policy/service-role/AWSIoTRuleActions policyarn:aws:iam::aws:policy/service-role/AWSIoTOTAUpdate { "Version": "2012-10-17", "Statement": { "Effect": "Allow", "Action": [ "iot:CreateJob", "signer:DescribeSigningJob" ], "Resource": "*" } }
Can you provide a sample code to show the behavior? Please include the observed behavior and the expected behavior. Which SDK version are you using?
using aws-java-sdk-bundle-1.11.317.jar
Example.
PolicyVersion defaultPolicy = iamClient.getPolicyVersion(new GetPolicyVersionRequest().withPolicyArn("policyarn:aws:iam::aws:policy/AWSCertificateManagerReadOnly").withVersionId(policy.getDefaultVersionId())).getPolicyVersion(); Policy p = com.amazonaws.auth.policy.Policy.fromJson(java.net.URLDecoder.decode(defaultPolicy.getDocument(), "UTF-8")); for(Statement statement: policy.getStatements()){ //null pointer will be thrown due to bad parsing into policy object }
Sorry, I'm having trouble reproducing this issue on 1.11.319.
Does this code cause an exception on your machine?
String policy = "{\n" +
" \"Version\": \"2012-10-17\",\n" +
" \"Statement\": {\n" +
" \"Effect\": \"Allow\",\n" +
" \"Action\": [\n" +
" \"acm:DescribeCertificate\",\n" +
" \"acm:ListCertificates\",\n" +
" \"acm:GetCertificate\",\n" +
" \"acm:ListTagsForCertificate\"\n" +
" ],\n" +
" \"Resource\": \"*\"\n" +
" }\n" +
"}";
Policy.fromJson(policy);
If so, can you check which version of Jackson you're using? If not, do you think you could modify the test to fail?
Any update?
Hey Matthew,
I have faced the same issue with Jackson 2.8.10. Running the code you provided above I get an invalid Policy in the result:
{"id":null,"version":"2012-10-17","statements":[{"id":"1","effect":"Deny","principals":[],"actions":[],"resources":null,"conditions":[]},{"id":"2","effect":"Deny","principals":[],"actions":[],"resources":null,"conditions":[]},{"id":"3","effect":"Deny","principals":[],"actions":[],"resources":null,"conditions":[]}]}
The problem is in JsonPolicyReader class, line 92:
statementNodes = policyNode.get(JsonDocumentFields.STATEMENT);
if (isNotNull(statementNodes)) {
for (JsonNode node : statementNodes) {
statements.add(statementOf(node));
}
}
If statement element is an Json object, not an Array, then method statementOf() receives invalid input.
Thanks! I've been able to reproduce this. Investigating now...
We've made the change to distinguish between statements as objects and statements as arrays. This change will go out with our next release. Thanks for the report!
@millems We are using aws-java-sdk-bundle-1.11.354.jar, is this issue fixed in 1.11.354 release.
Hi, @asmita-shinde-sp This issue should be fixed in release 11.342 and releases after.
https://github.com/aws/aws-sdk-java/commit/3591899f6a7dcbfe9d36ea33c9d28f70c6e18ac1#diff-23e9f73892a493683085563598a5a5d7
Would this prevent you from correctly parsing a policy object on return?
AWS expects the output of a policy object to look like this
{
"principalId": "user",
"policyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": "execute-api:Invoke",
"Effect": "Deny",
"Resource": "arn:aws:execute-api:us-west-2:123456789012:ymy8tbxw7b/*/GET/"
}
]
}
}
instead my object is returned like this
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "execute-api:Invoke",
"Effect": "Deny",
"Resource": "arn:aws:execute-api:us-west-2:123456789012:ymy8tbxw7b/*/GET/"
}
]
}
my generatePolicy return is formatted exactly like the desired object, when I test my custom authorizer over API Gateway -> Authorizers -> test , i even get the correct policy object BEFORE body parse, after body parse is when i lose my outer object with PrincipalId and PolicyDocument attributes