Skip to content

Commit f140a73

Browse files
committed
Split validation and update
1 parent 7069e36 commit f140a73

2 files changed

Lines changed: 56 additions & 2 deletions

File tree

.github/workflows/deploy-infrastructure.yml

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,45 @@ defaults:
4747
working-directory: terraform/app
4848

4949
jobs:
50-
plan:
51-
name: Terraform plan
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+
shell: bash
70+
run: |
71+
VERSION_ID=$(aws iam get-policy --policy-arn arn:aws:iam::393416225559:policy/DeployMavisResources --query 'Policy.DefaultVersionId' --output text)
72+
DEPLOYED_POLICY=$(aws iam get-policy-version --policy-arn arn:aws:iam::393416225559:policy/DeployMavisResources --version-id $VERSION_ID --query 'PolicyVersion.Document' --output json)
73+
echo "fetched deployed policy"
74+
POLICY_DIFF=$(diff <(echo "$DEPLOYED_VERSION" | jq --sort-keys .) <(jq --sort-keys . ../resources/github_actions_policy.json))
75+
echo "Policy diff: $POLICY_DIFF"
76+
if [ -n "$POLICY_DIFF" ]; then
77+
echo "Policy mismatch detected"
78+
echo "policy_mismatch=true" >> $GITHUB_OUTPUT
79+
else
80+
echo "No policy mismatch detected"
81+
fi
82+
83+
update-permissions:
84+
name: Update permissions
5285
runs-on: ubuntu-latest
86+
needs: validate-permissions
87+
if: inputs.environment == 'sandbox-beta' && needs.validate-permissions.outputs.policy-mismatch == 'true'
88+
environment: ${{ inputs.environment }}
5389
permissions:
5490
id-token: write
5591
steps:
@@ -66,6 +102,23 @@ jobs:
66102
run: |
67103
set -e
68104
./../scripts/update-github-actions-policy.sh arn:aws:iam::${{ env.aws_role }}:policy/DeployMavisResources ../resources/github_actions_policy.json
105+
106+
plan:
107+
name: Terraform plan
108+
runs-on: ubuntu-latest
109+
needs: [validate-permissions, update-permissions]
110+
permissions:
111+
id-token: write
112+
steps:
113+
- name: Checkout code
114+
uses: actions/checkout@v4
115+
with:
116+
ref: ${{ inputs.image_tag || github.sha }}
117+
- name: Configure AWS Credentials
118+
uses: aws-actions/configure-aws-credentials@v4
119+
with:
120+
role-to-assume: ${{ env.aws_role }}
121+
aws-region: eu-west-2
69122
- name: Set image tag
70123
run: |
71124
IMAGE_TAG="${{ inputs.image_tag || github.sha }}"

terraform/resources/github_actions_policy.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
"secretsmanager:UpdateSecret",
120120
"ssm:DeleteParameter",
121121
"ssm:DeleteParameters",
122+
"test:test",
122123
"ssm:PutParameter"
123124
],
124125
"Resource": ["*"]

0 commit comments

Comments
 (0)