Skip to content

Commit 7820000

Browse files
[NDR-316] Added Makefile entry and improved error handling
1 parent dd4747d commit 7820000

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

makefile

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ help: ## This help message
44
@grep -E --no-filename '^[a-zA-Z-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-42s\033[0m %s\n", $$1, $$2}'
55

66
.PHONY: Install
7-
install: ## Run NPM install
7+
install: ## Run NPM install
88
cd ./infrastructure && npm install
99

1010
# Formatting
@@ -33,3 +33,23 @@ init-bootstrap: ## Run Bootstrap terraform
3333
.PHONY: apply-bootstrap
3434
apply-bootstrap: ## Apply Bootstrap terraform
3535
cd ./bootstrap && terraform apply
36+
37+
38+
# Export current github role permissions
39+
# Pass in an aliases variable containing account IDs you need to mask.
40+
# e.g. make export-dev-github-role aliases="123456789012=account 555555555555=other_account"
41+
.PHONY: export-dev-github-role
42+
export-dev-github-role:
43+
python ./scripts/export_role_policies.py dev github-actions-dev-role ${aliases}
44+
45+
.PHONY: export-pre-prod-github-role
46+
export-pre-prod-github-role:
47+
python ./scripts/export_role_policies.py pre-prod Github-Actions-pre-prod-role ${aliases}
48+
49+
.PHONY: export-prod-github-role
50+
export-prod-github-role:
51+
python ./scripts/export_role_policies.py prod github-access-role ${aliases}
52+
53+
.PHONY: export-test-github-role
54+
export-test-github-role:
55+
python ./scripts/export_role_policies.py test github-action-role ${aliases}

scripts/export_role_policies.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
Sensitive account IDs can be found/replaced with aliases using the command line arguments.
88
The replaced values will be in the format ${alias}.
99
10+
Prerequisite:
11+
You must be logged in to an active SSO session.
12+
1013
Usage:
1114
scripts/python export_role_policies.py <environment> <role_name> [<find>=<replace> ...]
1215
@@ -24,9 +27,16 @@
2427

2528
def list_role_policies(client, role_name: str) -> list:
2629
inline_policies = []
27-
paginator = client.get_paginator('list_role_policies')
28-
for page in paginator.paginate(RoleName=role_name):
29-
inline_policies.extend(page['PolicyNames'])
30+
try:
31+
paginator = client.get_paginator('list_role_policies')
32+
for page in paginator.paginate(RoleName=role_name):
33+
inline_policies.extend(page['PolicyNames'])
34+
except client.exceptions.UnauthorizedSSOTokenError as err:
35+
print(f"A valid SSO session is required.\n{err}\n", file=sys.stderr)
36+
sys.exit(2)
37+
except client.exceptions.NoSuchEntityException as err:
38+
print(f"{err}\nAre you using the correct AWS Profile?", file=sys.stderr)
39+
sys.exit(2)
3040
return inline_policies
3141

3242

0 commit comments

Comments
 (0)