Skip to content

Commit 73009bb

Browse files
Merge pull request #3655 from nhsuk/make_policy_validation_fail_properly
Make github policy matching check fail on missmatch
2 parents 2f0747e + 505d2be commit 73009bb

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

.github/workflows/deploy-infrastructure.yml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ jobs:
5252
runs-on: ubuntu-latest
5353
permissions:
5454
id-token: write
55-
outputs:
56-
policy-mismatch: ${{ steps.compare-permissions.outputs.policy_mismatch }}
5755
steps:
5856
- name: Checkout code
5957
uses: actions/checkout@v4
@@ -67,13 +65,15 @@ jobs:
6765
- name: Compare permissions
6866
id: compare-permissions
6967
run: |
70-
../scripts/validate-github-actions-policy.sh arn:aws:iam::${{ env.aws_account_id }}:policy/DeployMavisResources ../resources/github_actions_policy.json
68+
source ../scripts/validate-github-actions-policy.sh
69+
validate_policies arn:aws:iam::${{ env.aws_account_id }}:policy/DeployMavisResources ../resources/github_actions_policy.json
70+
exit $?
7171
7272
update-permissions:
7373
name: Update permissions
7474
runs-on: ubuntu-latest
7575
needs: validate-permissions
76-
if: (inputs.environment == 'production' || inputs.environment == 'preview') && needs.validate-permissions.outputs.policy-mismatch == 'true'
76+
if: always() && (inputs.environment == 'production' || inputs.environment == 'preview') && needs.validate-permissions.result == 'failure'
7777
environment: ${{ inputs.environment }}
7878
permissions:
7979
id-token: write
@@ -95,7 +95,13 @@ jobs:
9595
name: Terraform plan
9696
runs-on: ubuntu-latest
9797
needs: [validate-permissions, update-permissions]
98-
if: always() && (needs.validate-permissions.outputs.policy-mismatch != 'true' || needs.update-permissions.result == 'success')
98+
if: >
99+
always() &&
100+
(
101+
(inputs.environment != 'production' && inputs.environment != 'preview') ||
102+
needs.validate-permissions.result == 'success' ||
103+
needs.update-permissions.result == 'success'
104+
)
99105
permissions:
100106
id-token: write
101107
steps:
Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
#!/usr/bin/env bash
22

3-
if [ "$#" -ne 2 ]; then
4-
echo "Usage: $0 <policy-arn> <policy-file>"
5-
exit 1
6-
fi
3+
function validate_policies() {
4+
if [ "$#" -ne 2 ]; then
5+
echo "Usage: $0 <policy-arn> <policy-file>"
6+
exit 1
7+
fi
78

8-
POLICY_ARN=$1
9-
POLICY_FILE=$2
9+
POLICY_ARN=$1
10+
POLICY_FILE=$2
1011

11-
VERSION_ID=$(aws iam get-policy --policy-arn "$POLICY_ARN" --query 'Policy.DefaultVersionId' --output text)
12-
aws iam get-policy-version --policy-arn "$POLICY_ARN" --version-id "$VERSION_ID" --query 'PolicyVersion.Document' --output json > deployed_policy.json
12+
VERSION_ID=$(aws iam get-policy --policy-arn "$POLICY_ARN" --query 'Policy.DefaultVersionId' --output text)
13+
aws iam get-policy-version --policy-arn "$POLICY_ARN" --version-id "$VERSION_ID" --query 'PolicyVersion.Document' --output json > deployed_policy.json
1314

14-
jq -S . deployed_policy.json > deployed_policy_sorted.json
15-
jq -S . "$POLICY_FILE" > github_actions_policy_sorted.json
15+
jq -S . deployed_policy.json > deployed_policy_sorted.json
16+
jq -S . "$POLICY_FILE" > github_actions_policy_sorted.json
1617

17-
POLICY_DIFF=$(diff --unified deployed_policy_sorted.json github_actions_policy_sorted.json)
18-
if [ -n "$POLICY_DIFF" ]; then
19-
echo "Policy mismatch detected: $POLICY_DIFF"
20-
echo "policy_mismatch=true" >> "$GITHUB_OUTPUT"
21-
else
22-
echo "No policy mismatch detected"
23-
fi
18+
POLICY_DIFF=$(diff --unified deployed_policy_sorted.json github_actions_policy_sorted.json)
19+
if [ -n "$POLICY_DIFF" ]; then
20+
echo "Policy mismatch detected: $POLICY_DIFF"
21+
return 1
22+
else
23+
echo "No policy mismatch detected"
24+
return 0
25+
fi
26+
}

0 commit comments

Comments
 (0)