/get_policies.py Secret
Last active
July 13, 2020 20:12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
profile_name = 'dev' | |
role_name = 'test-role' | |
session = boto3.Session(profile_name=profile_name) | |
iam = session.client('iam') | |
def get_policies_for_role(rolename): | |
all_policies = {} | |
# two possible types of policies - inline and managed | |
inline_policies = iam.list_role_policies(RoleName=rolename)['PolicyNames'] | |
attached_policies = iam.list_attached_role_policies(RoleName=rolename)['AttachedPolicies'] | |
# Get each inline policy | |
for policy in inline_policies: | |
all_policies[policy] = iam.get_role_policy( | |
RoleName=rolename, | |
PolicyName=policy)['PolicyDocument'] | |
# Get the default VersionId, then use that to retrieve that version of the policy. | |
for policy in attached_policies: | |
policy_version = iam.get_policy( | |
PolicyArn=policy['PolicyArn'] | |
)['Policy']['DefaultVersionId'] | |
all_policies[policy['PolicyArn']] = iam.get_policy_version( | |
PolicyArn=policy['PolicyArn'], | |
VersionId=policy_version | |
)['PolicyVersion']['Document'] | |
return all_policies |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment