Skip to content

Instantly share code, notes, and snippets.

@pacohope
Last active November 1, 2017 17:01
Show Gist options
  • Save pacohope/7e956a2ec3671fbd460803fd40efc38e to your computer and use it in GitHub Desktop.
Save pacohope/7e956a2ec3671fbd460803fd40efc38e to your computer and use it in GitHub Desktop.
Policy to Role listing. Given a CloudFormation in JSON that has ManagedPolicy objects connected to Roles, print a list of the policies and the roles that are attached.
def mapper(f):
if type == "array" then map(f)
elif type == "object" then
. as $in
| reduce keys[] as $key
({};
[$in[$key] | f ] as $value
| if $value | length == 0 then . else . + {($key): $value[0]}
end)
else .
end;
.Resources |= mapper(select(.Type=="AWS::IAM::ManagedPolicy")) |
.Resources |
keys[] as $k |
{"\(.[$k] | .Properties.Roles[]?.Ref?)":"\($k)"}
@pacohope
Copy link
Author

pacohope commented Nov 1, 2017

Example execution: jq -c -f policy-role.jq IAMPolicies.json

Example output:

{"AdminAccountManagementPolicy":"AccountAdminRole"}
{"AdminAccountManagementPolicy":"AccountAndBillingAdminRole"}
{"AdminBillingandCostManagementPolicy":"BillingAdminRole"}
{"AdminBillingandCostManagementPolicy":"AccountAndBillingAdminRole"}
{"BaselineDenyPolicy":"AWSReadOnlyRole"}
{"BaselineDenyPolicy":"AccountAndBillingAdminRole"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment