-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
82 additions
and
11 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
local-app/python-tools/cross-organization/check_iam_roles.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import boto3 | ||
| import json | ||
|
|
||
| # --- VERSIONING --- | ||
| __version__ = "1.0.0" | ||
|
|
||
| def account_task(account_session, account_id, account_name, region): | ||
| """ | ||
| Scans IAM roles to list metadata, policies, boundaries, and tags. | ||
| IAM is global; this runs once per account. | ||
| """ | ||
| results = {"alias": "N/A", "data": {}} | ||
| try: | ||
| iam = account_session.client('iam') | ||
| results["alias"] = iam.list_account_aliases().get('AccountAliases', ["N/A"])[0] | ||
|
|
||
| roles_paginator = iam.get_paginator('list_roles') | ||
|
|
||
| for page in roles_paginator.paginate(): | ||
| for role in page['Roles']: | ||
| role_name = role['RoleName'] | ||
|
|
||
| # 1. Attached (Managed) Policies | ||
| attached_policies = [] | ||
| attached_paginator = iam.get_paginator('list_attached_role_policies') | ||
| for attached_page in attached_paginator.paginate(RoleName=role_name): | ||
| for policy in attached_page['AttachedPolicies']: | ||
| attached_policies.append(policy['PolicyName']) | ||
|
|
||
| # 2. Inline Policies | ||
| inline_policies = [] | ||
| inline_paginator = iam.get_paginator('list_role_policies') | ||
| for inline_page in inline_paginator.paginate(RoleName=role_name): | ||
| inline_policies.extend(inline_page['PolicyNames']) | ||
|
|
||
| # 3. Permissions Boundary | ||
| boundary = role.get('PermissionsBoundary', {}) | ||
| boundary_name = boundary.get('PermissionsBoundaryArn', 'N/A').split('/')[-1] if boundary else 'N/A' | ||
|
|
||
| # 4. Tags | ||
| # Boto3's list_roles returns tags in the main response structure | ||
| tags = {t['Key']: t['Value'] for t in role.get('Tags', [])} | ||
|
|
||
| results["data"][role_name] = { | ||
| "resource": role['Arn'], | ||
| "role_name": role_name, | ||
| "path": role['Path'], | ||
| "attached_policies": attached_policies, | ||
| "inline_policies": inline_policies, | ||
| "permissions_boundary": boundary_name, | ||
| "tags": tags | ||
| } | ||
|
|
||
| results["data"]["account_summary"] = {"_summary": f"ROLES:{len(results['data'])}"} | ||
|
|
||
| except Exception as e: | ||
| results["error"] = str(e) | ||
|
|
||
| return results |
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