generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 20
204 lines (201 loc) · 10.4 KB
/
awsnuke.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
---
name: awsnuke
on:
push:
branches:
- main
paths:
- '.github/workflows/awsnuke.yml'
pull_request:
branches:
- main
types: [opened, edited, reopened, synchronize]
paths:
- '.github/workflows/awsnuke.yml'
schedule:
# * is a special character in YAML so you have to quote this string
# trigger every sunday at 12:00am
- cron: '0 12 * * 0'
workflow_dispatch:
env:
AWS_REGION: "eu-west-2"
ENVIRONMENT_MANAGEMENT: ${{ secrets.MODERNISATION_PLATFORM_ENVIRONMENTS }}
TF_IN_AUTOMATION: true
NUKE_ACCOUNTS: ${{ secrets.MODERNISATION_PLATFORM_AUTONUKE }}
NUKE_BLOCKLIST_ACCOUNTS: ${{ secrets.MODERNISATION_PLATFORM_AUTONUKE_BLOCKLIST }}
permissions: {}
defaults:
run:
shell: bash
jobs:
setup-prerequisites:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
account_blocklist_str : ${{ steps.set-blocklist-string.outputs.account_blocklist_str }}
steps:
- id: set-matrix
name: Set Up Matrix
run: echo "matrix=$(jq -c '.| map(select(. !="testing-test"))|sort' <<< $NUKE_ACCOUNTS)" >> $GITHUB_OUTPUT
- id: set-blocklist-string
name: Set Up Blocklist
run: |
account_blocklist_str=""
while read -r account_alias _; do
account_number=$(jq -r -e --arg account_name "$account_alias" '.account_ids[$account_name]' <<< $ENVIRONMENT_MANAGEMENT)
account_blocklist_str+=" - \"${account_number}\" # ${account_alias}"
account_blocklist_str+=$'\n'
done <<< "$(jq -c -r '.[]' <<< $NUKE_BLOCKLIST_ACCOUNTS)" # Taking this approach as piping (|) to a read opens a subshell and passes the variables to it by value
# Encoding and passing blocklist string as an output
echo "account_blocklist_str=$(echo -n "${account_blocklist_str}" | base64 -w 0)" >> $GITHUB_OUTPUT
nuke:
strategy:
fail-fast: false
matrix:
nuke_accts: ${{ fromJSON(needs.setup-prerequisites.outputs.matrix) }}
name: Sandbox Nuke
permissions:
id-token: write # This is required for requesting the JWT
contents: read # This is required for actions/checkout
runs-on: ubuntu-latest
needs: setup-prerequisites
steps:
- name: Checkout Repository
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: Set Account Number
run: echo "ACCOUNT_NUMBER=$(jq -r -e --arg account_name "${ACCOUNT_NAME}" '.account_ids[$account_name]' <<< $ENVIRONMENT_MANAGEMENT)" >> $GITHUB_ENV
- name: configure aws credentials
uses: aws-actions/configure-aws-credentials@010d0da01d0b5a38af31e9c3470dbfdabdecca3a # v4.0.1
with:
role-to-assume: "arn:aws:iam::${{ env.ACCOUNT_NUMBER }}:role/github-actions"
role-session-name: githubactionsrolesession
aws-region: ${{ env.AWS_REGION }}
- name: Setup Nuke Account String
run: |
accounts_str=''
accounts_str+=" \"${ACCOUNT_NUMBER}\": # ${ACCOUNT_NAME}"
accounts_str+=$'\n'
accounts_str+=" presets:"
accounts_str+=$'\n'
accounts_str+=" - \"common\""
accounts_str+=$'\n'
echo "$accounts_str"
echo "ACCOUNTS_STR<<EOF" >> $GITHUB_ENV
echo "$accounts_str" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
- name: Setup Nuke Config
run: |
export accounts_str="$ACCOUNTS_STR"
export account_blocklist_str=$(echo "${{ needs.setup-prerequisites.outputs.account_blocklist_str }}" | base64 --decode)
cat scripts/nuke-config-template.txt | envsubst > nuke-config.yml
- name: Install AWS Nuke
run: |
echo "BEGIN: Install AWS Nuke"
mkdir -p $HOME/bin
wget -c "https://github.com/rebuy-de/aws-nuke/releases/download/${AWS_NUKE_VERSION}/aws-nuke-${AWS_NUKE_VERSION}-linux-amd64.tar.gz" -O - | tar -xz -C $HOME/bin
mv "$HOME/bin/aws-nuke-${AWS_NUKE_VERSION}-linux-amd64" $HOME/bin/aws-nuke
chmod +x $HOME/bin/aws-nuke
echo "END: Install AWS Nuke"
env:
AWS_NUKE_VERSION: v2.19.0
- name: Nuke Plan (Dry Run)
run: |
aws sts assume-role --role-arn "arn:aws:iam::${ACCOUNT_NUMBER}:role/MemberInfrastructureAccess" --role-session-name githubactionsgotestrolesession > creds
$(echo "export AWS_ACCESS_KEY_ID=$(echo $(cat creds) | sed -n 's/.*"AccessKeyId": "\([^"]*\)".*/\1/p')")
$(echo "export AWS_SECRET_ACCESS_KEY=$(echo $(cat creds) | sed -n 's/.*"SecretAccessKey": "\([^"]*\)".*/\1/p')")
$(echo "export AWS_SESSION_TOKEN=$(echo $(cat creds) | sed -n 's/.*"SessionToken": "\([^"]*\)".*/\1/p')")
grep "MemberInfrastructureAccess" <<< $(aws sts get-caller-identity) || { echo 'Failed to assume role' ; exit 1; }
$HOME/bin/aws-nuke --access-key-id "$AWS_ACCESS_KEY_ID" \
--secret-access-key "$AWS_SECRET_ACCESS_KEY" \
--session-token "$AWS_SESSION_TOKEN" \
--config nuke-config.yml \
--force
- name: Nuke Apply (-no-dry-run)
if: ${{ github.event_name == 'schedule' || (github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/main') }}
run: |
# The environment gets reset between steps, so the role needs to be re-assumed.
aws sts assume-role --role-arn "arn:aws:iam::${ACCOUNT_NUMBER}:role/MemberInfrastructureAccess" --role-session-name githubactionsgotestrolesession > creds
$(echo "export AWS_ACCESS_KEY_ID=$(echo $(cat creds) | sed -n 's/.*"AccessKeyId": "\([^"]*\)".*/\1/p')")
$(echo "export AWS_SECRET_ACCESS_KEY=$(echo $(cat creds) | sed -n 's/.*"SecretAccessKey": "\([^"]*\)".*/\1/p')")
$(echo "export AWS_SESSION_TOKEN=$(echo $(cat creds) | sed -n 's/.*"SessionToken": "\([^"]*\)".*/\1/p')")
grep "MemberInfrastructureAccess" <<< $(aws sts get-caller-identity) || { echo 'Failed to assume role' ; exit 1; }
$HOME/bin/aws-nuke --access-key-id "$AWS_ACCESS_KEY_ID" \
--secret-access-key "$AWS_SECRET_ACCESS_KEY" \
--session-token "$AWS_SESSION_TOKEN" \
--config nuke-config.yml \
--force \
--no-dry-run
env:
ACCOUNT_NAME: ${{ matrix.nuke_accts }}
# testing-test account is not set up with OIDC because it is used to terratest OIDC-related resources. Therefore it's handled as a special case
# using testing-ci user static credentials.
nuke-testing-test:
name: Sandbox Nuke (testing-test)
permissions:
contents: read # This is required for actions/checkout
runs-on: ubuntu-latest
needs: setup-prerequisites
steps:
- name: Checkout Repository
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: Set Account Number
run: echo "ACCOUNT_NUMBER=$(jq -r -e --arg account_name "${ACCOUNT_NAME}" '.account_ids[$account_name]' <<< $ENVIRONMENT_MANAGEMENT)" >> $GITHUB_ENV
- name: Setup Nuke Account String
run: |
accounts_str=''
accounts_str+=" \"${ACCOUNT_NUMBER}\": # ${ACCOUNT_NAME}"
accounts_str+=$'\n'
accounts_str+=" presets:"
accounts_str+=$'\n'
accounts_str+=" - \"common\""
accounts_str+=$'\n'
echo "$accounts_str"
echo "ACCOUNTS_STR<<EOF" >> $GITHUB_ENV
echo "$accounts_str" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
- name: Setup Nuke Config
run: |
export accounts_str="$ACCOUNTS_STR"
export account_blocklist_str=$(echo "${{ needs.setup-prerequisites.outputs.account_blocklist_str }}" | base64 --decode)
cat scripts/nuke-config-template.txt | envsubst > nuke-config.yml
- name: Install AWS Nuke
run: |
echo "BEGIN: Install AWS Nuke"
mkdir -p $HOME/bin
wget -c "https://github.com/rebuy-de/aws-nuke/releases/download/${AWS_NUKE_VERSION}/aws-nuke-${AWS_NUKE_VERSION}-linux-amd64.tar.gz" -O - | tar -xz -C $HOME/bin
mv "$HOME/bin/aws-nuke-${AWS_NUKE_VERSION}-linux-amd64" $HOME/bin/aws-nuke
chmod +x $HOME/bin/aws-nuke
echo "END: Install AWS Nuke"
env:
AWS_NUKE_VERSION: v2.19.0
- name: Nuke Plan (Dry Run)
run: |
aws sts assume-role --role-arn "arn:aws:iam::${ACCOUNT_NUMBER}:role/MemberInfrastructureAccess" --role-session-name githubactionsgotestrolesession > creds
$(echo "export AWS_ACCESS_KEY_ID=$(echo $(cat creds) | sed -n 's/.*"AccessKeyId": "\([^"]*\)".*/\1/p')")
$(echo "export AWS_SECRET_ACCESS_KEY=$(echo $(cat creds) | sed -n 's/.*"SecretAccessKey": "\([^"]*\)".*/\1/p')")
$(echo "export AWS_SESSION_TOKEN=$(echo $(cat creds) | sed -n 's/.*"SessionToken": "\([^"]*\)".*/\1/p')")
grep "MemberInfrastructureAccess" <<< $(aws sts get-caller-identity) || { echo 'Failed to assume role' ; exit 1; }
$HOME/bin/aws-nuke --access-key-id "$AWS_ACCESS_KEY_ID" \
--secret-access-key "$AWS_SECRET_ACCESS_KEY" \
--session-token "$AWS_SESSION_TOKEN" \
--config nuke-config.yml \
--force
- name: Nuke Apply (-no-dry-run)
if: ${{ github.event_name == 'schedule' || (github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/main') }}
run: |
# The environment gets reset between steps, so the AWS credentials get overwritten and the role needs to be re-assumed.
aws sts assume-role --role-arn "arn:aws:iam::${ACCOUNT_NUMBER}:role/MemberInfrastructureAccess" --role-session-name githubactionsgotestrolesession > creds
$(echo "export AWS_ACCESS_KEY_ID=$(echo $(cat creds) | sed -n 's/.*"AccessKeyId": "\([^"]*\)".*/\1/p')")
$(echo "export AWS_SECRET_ACCESS_KEY=$(echo $(cat creds) | sed -n 's/.*"SecretAccessKey": "\([^"]*\)".*/\1/p')")
$(echo "export AWS_SESSION_TOKEN=$(echo $(cat creds) | sed -n 's/.*"SessionToken": "\([^"]*\)".*/\1/p')")
grep "MemberInfrastructureAccess" <<< $(aws sts get-caller-identity) || { echo 'Failed to assume role' ; exit 1; }
$HOME/bin/aws-nuke --access-key-id "$AWS_ACCESS_KEY_ID" \
--secret-access-key "$AWS_SECRET_ACCESS_KEY" \
--session-token "$AWS_SESSION_TOKEN" \
--config nuke-config.yml \
--force \
--no-dry-run
env:
AWS_ACCESS_KEY_ID: ${{ secrets.TESTING_AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.TESTING_AWS_SECRET_ACCESS_KEY }}
ACCOUNT_NAME: "testing-test"