-
Notifications
You must be signed in to change notification settings - Fork 4
VED-1223: Update permissions to auto-ops role so the pipeline can apply terraform changes at account level #1384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
0561063
283a3a1
af6e116
2b6d8b2
5e2a874
c2d0559
2ce0cdc
85b12bc
d93ba1b
90f4aec
7ed155f
a1fe5ea
60f978a
9860152
a23387e
1abcf47
4caeddb
4db413a
c61a3c4
31c25f1
aa15e8b
34d9440
99d20c0
33b2bed
3dfb8a4
c37f9f9
264bee5
6ccbb2d
48f3cad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| name: Account Terraform | ||
|
|
||
| on: | ||
| workflow_call: | ||
| inputs: | ||
| base_sha: | ||
| required: true | ||
| type: string | ||
| head_sha: | ||
| required: true | ||
| type: string | ||
| environment: | ||
| required: true | ||
| type: string | ||
| artifact_name: | ||
| required: true | ||
| type: string | ||
|
|
||
| concurrency: | ||
| group: account-terraform-${{ github.repository }}-${{ inputs.environment }} | ||
| cancel-in-progress: false | ||
|
|
||
| jobs: | ||
| detect-account-infra-changes: | ||
| runs-on: ubuntu-latest | ||
| outputs: | ||
| account_infra_changed: ${{ steps.diff.outputs.account_infra_changed }} | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@0c366fd6a839edf440554fa01a7085ccba70ac98 | ||
| with: | ||
| fetch-depth: 0 | ||
|
|
||
| - name: Detect account terraform changes | ||
| id: diff | ||
| run: | | ||
| base_sha="${{ inputs.base_sha }}" | ||
Check failureCode scanning / SonarCloud GitHub Actions should not be vulnerable to script injections High
inputs.base\_sha is vulnerable to script injection: values of inputs are provided by whoever triggers the workflow. Change this workflow to not use user-controlled data directly in a run block, for example by assigning this expression to an environment variable. See more on SonarQube Cloud
Check failure on line 37 in .github/workflows/account-terraform.yml
|
||
| if [ -z "$base_sha" ] || [ "$base_sha" = "0000000000000000000000000000000000000000" ]; then | ||
| base_sha=$(git rev-parse HEAD~1) | ||
| fi | ||
|
|
||
| changed_files=$(git diff --name-only "$base_sha" "${{ inputs.head_sha }}") | ||
Check failureCode scanning / SonarCloud GitHub Actions should not be vulnerable to script injections High
inputs.head\_sha is vulnerable to script injection: values of inputs are provided by whoever triggers the workflow. Change this workflow to not use user-controlled data directly in a run block, for example by assigning this expression to an environment variable. See more on SonarQube Cloud
Check failure on line 42 in .github/workflows/account-terraform.yml
|
||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| if echo "$changed_files" | grep -q '^infrastructure/account/'; then | ||
| echo "account_infra_changed=true" >> "$GITHUB_OUTPUT" | ||
| else | ||
| echo "account_infra_changed=false" >> "$GITHUB_OUTPUT" | ||
| fi | ||
|
|
||
| account-terraform-plan: | ||
| permissions: | ||
| id-token: write | ||
| contents: read | ||
| needs: [detect-account-infra-changes] | ||
| if: ${{ needs.detect-account-infra-changes.outputs.account_infra_changed == 'true' }} | ||
| runs-on: ubuntu-latest | ||
| environment: | ||
| name: ${{ inputs.environment }} | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@0c366fd6a839edf440554fa01a7085ccba70ac98 | ||
|
|
||
| - name: Connect to AWS | ||
| uses: aws-actions/configure-aws-credentials@8df5847569e6427dd6c4fb1cf565c83acfa8afa7 | ||
| with: | ||
| aws-region: eu-west-2 | ||
| role-to-assume: arn:aws:iam::${{ vars.AWS_ACCOUNT_ID }}:role/auto-ops | ||
| role-session-name: github-actions | ||
|
|
||
| - uses: hashicorp/setup-terraform@5e8dbf3c6d9deaf4193ca7a8fb23f2ac83bb6c85 | ||
| with: | ||
| terraform_version: "1.12.2" | ||
|
|
||
|
avshetty1980 marked this conversation as resolved.
|
||
| - name: Resolve account terraform state bucket | ||
| id: account-state-bucket | ||
| env: | ||
| CONFIGURED_ACCOUNT_TERRAFORM_STATE_BUCKET: ${{ vars.ACCOUNT_TERRAFORM_STATE_BUCKET }} | ||
| run: | | ||
| bucket_name="$(bash ./utilities/scripts/resolve_account_terraform_state_bucket.sh)" | ||
| if [ -z "$bucket_name" ]; then | ||
| echo "Resolved empty account terraform state bucket." >&2 | ||
| exit 1 | ||
| fi | ||
| echo "bucket_name=$bucket_name" >> "$GITHUB_OUTPUT" | ||
|
|
||
| - name: Terraform Init (account) | ||
| working-directory: infrastructure/account | ||
| run: make init ENVIRONMENT=${{ inputs.environment }} BUCKET_NAME=${{ steps.account-state-bucket.outputs.bucket_name }} | ||
Check failureCode scanning / SonarCloud GitHub Actions should not be vulnerable to script injections High
inputs.environment is vulnerable to script injection: values of inputs are provided by whoever triggers the workflow. Change this workflow to not use user-controlled data directly in a run block, for example by assigning this expression to an environment variable. See more on SonarQube Cloud
Check failure on line 87 in .github/workflows/account-terraform.yml
|
||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
|
|
||
| - name: Terraform Plan (account) | ||
| # Ignore cancellations to prevent Terraform from being killed while it holds a state lock | ||
| # A stuck process can still be killed with the force-cancel API operation | ||
| if: ${{ !failure() }} | ||
| working-directory: infrastructure/account | ||
| run: make plan-ci ENVIRONMENT=${{ inputs.environment }} | ||
Check failureCode scanning / SonarCloud GitHub Actions should not be vulnerable to script injections High
inputs.environment is vulnerable to script injection: values of inputs are provided by whoever triggers the workflow. Change this workflow to not use user-controlled data directly in a run block, for example by assigning this expression to an environment variable. See more on SonarQube Cloud
Check failure on line 94 in .github/workflows/account-terraform.yml
|
||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
|
|
||
| - name: Save Account Terraform Plan | ||
| uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f | ||
| with: | ||
| name: ${{ inputs.artifact_name }} | ||
| path: infrastructure/account/tfplan | ||
|
|
||
| account-terraform-manual-approval: | ||
| needs: [account-terraform-plan] | ||
| if: ${{ !cancelled() && needs.account-terraform-plan.result == 'success' }} | ||
| runs-on: ubuntu-latest | ||
| environment: | ||
| name: account-level-infra-approval | ||
| steps: | ||
| - name: Await manual approval | ||
| run: echo "Waiting for account-level infrastructure approval." | ||
|
|
||
| account-terraform-apply: | ||
| permissions: | ||
| id-token: write | ||
| contents: read | ||
| needs: [account-terraform-manual-approval] | ||
| if: ${{ !cancelled() && needs.account-terraform-manual-approval.result == 'success' }} | ||
| runs-on: ubuntu-latest | ||
| environment: | ||
| name: ${{ inputs.environment }} | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@0c366fd6a839edf440554fa01a7085ccba70ac98 | ||
|
|
||
|
Thomas-Boyle marked this conversation as resolved.
|
||
| - name: Connect to AWS | ||
| uses: aws-actions/configure-aws-credentials@8df5847569e6427dd6c4fb1cf565c83acfa8afa7 | ||
| with: | ||
| aws-region: eu-west-2 | ||
| role-to-assume: arn:aws:iam::${{ vars.AWS_ACCOUNT_ID }}:role/auto-ops | ||
| role-session-name: github-actions | ||
|
|
||
|
avshetty1980 marked this conversation as resolved.
|
||
| - uses: hashicorp/setup-terraform@5e8dbf3c6d9deaf4193ca7a8fb23f2ac83bb6c85 | ||
| with: | ||
| terraform_version: "1.12.2" | ||
|
|
||
| - name: Resolve account terraform state bucket | ||
| id: account-state-bucket | ||
| env: | ||
| CONFIGURED_ACCOUNT_TERRAFORM_STATE_BUCKET: ${{ vars.ACCOUNT_TERRAFORM_STATE_BUCKET }} | ||
| run: | | ||
| bucket_name="$(bash ./utilities/scripts/resolve_account_terraform_state_bucket.sh)" | ||
| if [ -z "$bucket_name" ]; then | ||
| echo "Resolved empty account terraform state bucket." >&2 | ||
| exit 1 | ||
| fi | ||
| echo "bucket_name=$bucket_name" >> "$GITHUB_OUTPUT" | ||
|
|
||
| - name: Retrieve Account Terraform Plan | ||
| uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c | ||
| with: | ||
| name: ${{ inputs.artifact_name }} | ||
| path: infrastructure/account | ||
|
|
||
| - name: Terraform Init (account) | ||
| working-directory: infrastructure/account | ||
| run: make init ENVIRONMENT=${{ inputs.environment }} BUCKET_NAME=${{ steps.account-state-bucket.outputs.bucket_name }} | ||
Check failureCode scanning / SonarCloud GitHub Actions should not be vulnerable to script injections High
inputs.environment is vulnerable to script injection: values of inputs are provided by whoever triggers the workflow. Change this workflow to not use user-controlled data directly in a run block, for example by assigning this expression to an environment variable. See more on SonarQube Cloud
Check failure on line 156 in .github/workflows/account-terraform.yml
|
||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
|
|
||
| - name: Terraform Apply (account) | ||
| # Ignore cancellations to prevent Terraform from being killed while it holds a state lock | ||
| # A stuck process can still be killed with the force-cancel API operation | ||
| if: ${{ !failure() }} | ||
| working-directory: infrastructure/account | ||
| run: make apply-ci ENVIRONMENT=${{ inputs.environment }} | ||
Check failureCode scanning / SonarCloud GitHub Actions should not be vulnerable to script injections High
inputs.environment is vulnerable to script injection: values of inputs are provided by whoever triggers the workflow. Change this workflow to not use user-controlled data directly in a run block, for example by assigning this expression to an environment variable. See more on SonarQube Cloud
Check failure on line 163 in .github/workflows/account-terraform.yml
|
||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
|
|
||
| account-terraform-not-required: | ||
| needs: [detect-account-infra-changes] | ||
| if: ${{ needs.detect-account-infra-changes.outputs.account_infra_changed != 'true' }} | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Skip account terraform | ||
| run: echo "No account-level infrastructure changes detected." | ||
|
|
||
| account-terraform-ready: | ||
| needs: | ||
| - account-terraform-plan | ||
| - account-terraform-manual-approval | ||
| - account-terraform-apply | ||
| - account-terraform-not-required | ||
| if: ${{ always() && !contains(needs.*.result, 'failure') && !contains(needs.*.result, 'cancelled') }} | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Account terraform stage complete | ||
| run: echo "Account terraform stage complete." | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| #!/bin/bash | ||
|
avshetty1980 marked this conversation as resolved.
|
||
|
|
||
| set -o nounset errexit pipefail | ||
|
|
||
| configured_bucket="$(printf '%s' "${CONFIGURED_ACCOUNT_TERRAFORM_STATE_BUCKET:-}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')" | ||
|
|
||
| if [ -n "$configured_bucket" ]; then | ||
| printf '%s\n' "$configured_bucket" | ||
| exit 0 | ||
| fi | ||
|
|
||
| mapfile -t buckets < <( | ||
| aws s3api list-buckets --query 'Buckets[].Name' --output text | | ||
| tr '\t' '\n' | | ||
| grep -E '^immunisation-dev-terraform-state(-files)?$' | ||
| ) | ||
|
|
||
| if [ "${#buckets[@]}" -ne 1 ]; then | ||
| echo "Expected exactly 1 dev account terraform state bucket, found ${#buckets[@]}." >&2 | ||
| echo "Set repo/environment variable ACCOUNT_TERRAFORM_STATE_BUCKET to remove ambiguity." >&2 | ||
| printf '%s\n' "${buckets[@]}" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| printf '%s\n' "${buckets[0]}" | ||
Uh oh!
There was an error while loading. Please reload this page.