Last active
February 19, 2022 01:45
-
-
Save shollingsworth/6f613f7af93d7c830c5e687eac54dbd1 to your computer and use it in GitHub Desktop.
Dump the details of an AWS role
This file contains hidden or 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
| #!/usr/bin/env python3 | |
| # -*- coding: utf-8 -*- | |
| """get role details.""" | |
| import argparse | |
| import json | |
| import boto3 | |
| def json_normalize(obj): | |
| """Return JSON dump with non-json objects represented as strings.""" | |
| try: | |
| json.dumps(obj) | |
| return obj | |
| except Exception: # pylint: disable=broad-except | |
| return str(obj) | |
| def get_role_details(_args): | |
| """Get role details.""" | |
| if _args.profile: | |
| session = boto3.Session(profile_name=_args.profile) | |
| else: | |
| session = boto3.Session() | |
| client = session.client("iam") | |
| pager = client.get_paginator("get_account_authorization_details") | |
| for page in pager.paginate(): | |
| for i in page["RoleDetailList"]: | |
| if i["RoleName"] == _args.rolename: # type: ignore | |
| return i | |
| return None | |
| def main(): | |
| """Run main function.""" | |
| parser = argparse.ArgumentParser( | |
| formatter_class=argparse.RawDescriptionHelpFormatter, | |
| description=__doc__, | |
| ) | |
| parser.add_argument( | |
| "rolename", | |
| type=str, | |
| ) | |
| parser.add_argument( | |
| "--profile", | |
| type=str, | |
| default=None, | |
| ) | |
| args = parser.parse_args() | |
| sub = get_role_details(args) | |
| print( | |
| json.dumps( | |
| sub, | |
| indent=4, | |
| separators=(",", " : "), | |
| default=json_normalize, | |
| ) | |
| ) | |
| if __name__ == "__main__": | |
| main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment