|
| 1 | +name: security-review |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + branches: ["*"] |
| 6 | +concurrency: |
| 7 | + group: ${{ github.workflow }}-${{ github.ref_name }} |
| 8 | + cancel-in-progress: true |
| 9 | +permissions: |
| 10 | + contents: read |
| 11 | + |
| 12 | +jobs: |
| 13 | + authorize: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + outputs: |
| 16 | + approval-env: ${{ steps.authorization.outputs.approval-env }} |
| 17 | + steps: |
| 18 | + - uses: actions/checkout@v4 |
| 19 | + - name: Check authorization |
| 20 | + id: authorization |
| 21 | + uses: ./.github/actions/check-authorization |
| 22 | + |
| 23 | + execute: |
| 24 | + needs: authorize |
| 25 | + runs-on: ubuntu-latest |
| 26 | + environment: ${{ needs.authorize.outputs.approval-env }} |
| 27 | + permissions: |
| 28 | + id-token: write |
| 29 | + contents: read |
| 30 | + statuses: write |
| 31 | + steps: |
| 32 | + - uses: actions/checkout@v4 |
| 33 | + with: |
| 34 | + ref: ${{ github.event.pull_request.head.sha }} |
| 35 | + |
| 36 | + - name: Get AWS credentials |
| 37 | + uses: aws-actions/configure-aws-credentials@v4 |
| 38 | + with: |
| 39 | + role-to-assume: arn:aws:iam::547182295936:role/SecurityReview-GitHubOIDCRole #TODO: migrate to production account |
| 40 | + role-session-name: ${{ github.run_id }}-${{ github.run_attempt }} |
| 41 | + aws-region: us-west-2 |
| 42 | + |
| 43 | + - name: Start CodeBuild and wait for completion |
| 44 | + id: codebuild |
| 45 | + shell: bash |
| 46 | + env: |
| 47 | + PROJECT_NAME: SecurityReview-${{ github.event.repository.name }} |
| 48 | + SOURCE_VERSION: "pr/${{ github.event.pull_request.number }}" |
| 49 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 50 | + run: | |
| 51 | + # Start the build |
| 52 | + BUILD_ID=$(aws codebuild start-build \ |
| 53 | + --project-name "${PROJECT_NAME}" \ |
| 54 | + --source-version "${SOURCE_VERSION}" \ |
| 55 | + --environment-variables-override "name=PR_NUMBER,value=${PR_NUMBER},type=PLAINTEXT" \ |
| 56 | + --query 'build.id' --output text) |
| 57 | +
|
| 58 | + # Wait for completion |
| 59 | + while STATUS=$(aws codebuild batch-get-builds --ids "${BUILD_ID}" --query 'builds[0].buildStatus' --output text); [[ "$STATUS" == "IN_PROGRESS" ]]; do |
| 60 | + sleep 30 |
| 61 | + done |
| 62 | +
|
| 63 | + if [[ "$STATUS" != "SUCCEEDED" ]]; then |
| 64 | + echo "blocking=skip" >> "$GITHUB_OUTPUT" |
| 65 | + exit 1 |
| 66 | + fi |
| 67 | +
|
| 68 | + REVIEW_STATUS=$(aws codebuild batch-get-builds --ids "${BUILD_ID}" --query 'builds[0].exportedEnvironmentVariables[?name==`REVIEW_STATUS`].value' --output text) |
| 69 | + echo "blocking=$([[ "$REVIEW_STATUS" == "FAIL" ]] && echo true || echo false)" >> "$GITHUB_OUTPUT" |
| 70 | +
|
| 71 | + - name: Update commit status |
| 72 | + if: always() && steps.codebuild.outputs.blocking != 'skip' |
| 73 | + shell: bash |
| 74 | + env: |
| 75 | + GH_TOKEN: ${{ github.token }} |
| 76 | + STATUS_URL: ${{ github.api_url }}/repos/${{ github.repository }}/statuses/${{ github.event.pull_request.head.sha }} |
| 77 | + REPORT_URL: https://d28bfvmis1skm5.cloudfront.net/${{ github.event.repository.name }}/pr-${{ github.event.pull_request.number }}/${{ github.event.pull_request.head.sha }}.html |
| 78 | + BLOCKING: ${{ steps.codebuild.outputs.blocking }} |
| 79 | + run: | |
| 80 | + STATE=$([[ "$BLOCKING" == "true" ]] && echo "failure" || echo "success") |
| 81 | + curl -sS -X POST \ |
| 82 | + -H "Authorization: token ${GH_TOKEN}" \ |
| 83 | + "${STATUS_URL}" \ |
| 84 | + -d "{\"state\":\"${STATE}\",\"context\":\"security-review / report\",\"target_url\":\"${REPORT_URL}\"}" |
0 commit comments