Skip to content

Commit b1f9030

Browse files
Make github policy matching check fail on missmatch
- Ensure relevant jobs run in relevant environments afterwards - Use result of job instead of custom outputs for clarity
1 parent 3fb1667 commit b1f9030

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-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: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
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+
exit 1
5+
if [ "$#" -ne 2 ]; then
6+
echo "Usage: $0 <policy-arn> <policy-file>"
7+
exit 1
8+
fi
79

8-
POLICY_ARN=$1
9-
POLICY_FILE=$2
10+
POLICY_ARN=$1
11+
POLICY_FILE=$2
1012

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
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
1315

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

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
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+
return 1
23+
else
24+
echo "No policy mismatch detected"
25+
retrun 0
26+
fi
27+
}

0 commit comments

Comments
 (0)