Skip to content

Commit 1b0fe5e

Browse files
authored
Merge pull request #3604 from nhsuk/policy_version_sync
Policy version sync
2 parents 2e55d56 + 39235bc commit 1b0fe5e

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

.github/workflows/deploy-infrastructure.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,63 @@ env:
3939
aws_role: ${{ inputs.environment == 'production'
4040
&& 'arn:aws:iam::820242920762:role/GithubDeployMavisAndInfrastructure'
4141
|| 'arn:aws:iam::393416225559:role/GithubDeployMavisAndInfrastructure' }}
42+
aws_account_id: ${{ inputs.environment == 'production'
43+
&& '820242920762' || '393416225559' }}
4244

4345
defaults:
4446
run:
4547
working-directory: terraform/app
4648

4749
jobs:
50+
validate-permissions:
51+
name: Validate permissions
52+
runs-on: ubuntu-latest
53+
permissions:
54+
id-token: write
55+
outputs:
56+
policy-mismatch: ${{ steps.compare-permissions.outputs.policy_mismatch }}
57+
steps:
58+
- name: Checkout code
59+
uses: actions/checkout@v4
60+
with:
61+
ref: ${{ inputs.image_tag || github.sha }}
62+
- name: Configure AWS Credentials
63+
uses: aws-actions/configure-aws-credentials@v4
64+
with:
65+
role-to-assume: ${{ env.aws_role }}
66+
aws-region: eu-west-2
67+
- name: Compare permissions
68+
id: compare-permissions
69+
run: |
70+
../scripts/validate-github-actions-policy.sh arn:aws:iam::${{ env.aws_account_id }}:policy/DeployMavisResources ../resources/github_actions_policy.json
71+
72+
update-permissions:
73+
name: Update permissions
74+
runs-on: ubuntu-latest
75+
needs: validate-permissions
76+
if: inputs.environment == 'production' && needs.validate-permissions.outputs.policy-mismatch == 'true'
77+
environment: ${{ inputs.environment }}
78+
permissions:
79+
id-token: write
80+
steps:
81+
- name: Checkout code
82+
uses: actions/checkout@v4
83+
with:
84+
ref: ${{ inputs.image_tag || github.sha }}
85+
- name: Configure AWS Credentials
86+
uses: aws-actions/configure-aws-credentials@v4
87+
with:
88+
role-to-assume: ${{ env.aws_role }}
89+
aws-region: eu-west-2
90+
- name: Update IAM policy
91+
run: |
92+
../scripts/update-github-actions-policy.sh arn:aws:iam::${{ env.aws_account_id }}:policy/DeployMavisResources ../resources/github_actions_policy.json
93+
4894
plan:
4995
name: Terraform plan
5096
runs-on: ubuntu-latest
97+
needs: [validate-permissions, update-permissions]
98+
if: always() && (needs.validate-permissions.outputs.policy-mismatch != 'true' || needs.update-permissions.result == 'success')
5199
permissions:
52100
id-token: write
53101
steps:
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
if [ "$#" -ne 2 ]; then
6+
echo "Usage: $0 <policy-arn> <policy-file>"
7+
exit 1
8+
fi
9+
10+
POLICY_ARN=$1
11+
POLICY_FILE=$2
12+
13+
# Get existing policy versions
14+
EXISTING_VERSIONS=$(aws iam list-policy-versions --policy-arn "$POLICY_ARN" --query 'Versions[].VersionId' --output text)
15+
16+
# If there are 5 or more versions, delete the oldest one
17+
if [ "$(echo "$EXISTING_VERSIONS" | wc -w)" -ge 5 ]; then
18+
OLDEST_VERSION=$(echo "$EXISTING_VERSIONS" | awk '{print $NF}')
19+
echo "Deleting oldest version: $OLDEST_VERSION"
20+
aws iam delete-policy-version --policy-arn "$POLICY_ARN" --version-id "$OLDEST_VERSION"
21+
else
22+
echo "No need to delete any policy versions."
23+
fi
24+
25+
# Create a new version of the policy
26+
aws iam create-policy-version --policy-arn "$POLICY_ARN" --policy-document "file://$POLICY_FILE" --set-as-default
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
if [ "$#" -ne 2 ]; then
6+
echo "Usage: $0 <policy-arn> <policy-file>"
7+
exit 1
8+
fi
9+
10+
POLICY_ARN=$1
11+
POLICY_FILE=$2
12+
13+
VERSION_ID=$(aws iam get-policy --policy-arn "$POLICY_ARN" --query 'Policy.DefaultVersionId' --output text)
14+
aws iam get-policy-version --policy-arn "$POLICY_ARN" --version-id "$VERSION_ID" --query 'PolicyVersion.Document' --output json > deployed_policy.json
15+
16+
jq -S . deployed_policy.json > deployed_policy_sorted.json
17+
jq -S . "$POLICY_FILE" > github_actions_policy_sorted.json
18+
19+
POLICY_DIFF=$(diff --unified deployed_policy_sorted.json github_actions_policy_sorted.json)
20+
if [ -n "$POLICY_DIFF" ]; then
21+
echo "Policy mismatch detected: $POLICY_DIFF"
22+
echo "policy_mismatch=true" >> "$GITHUB_OUTPUT"
23+
else
24+
echo "No policy mismatch detected"
25+
fi

0 commit comments

Comments
 (0)