|
| 1 | +name: Security Guardian |
| 2 | +on: |
| 3 | + pull_request: {} |
| 4 | + |
| 5 | + # Triggered from a separate job when a review is added |
| 6 | + workflow_run: |
| 7 | + workflows: [PR Linter Trigger] |
| 8 | + types: |
| 9 | + - completed |
| 10 | + |
| 11 | + # Trigger when a status is updated (CodeBuild leads to statuses) |
| 12 | + status: {} |
| 13 | + |
| 14 | + # Trigger when a check suite is completed (GitHub actions and CodeCov create checks) |
| 15 | + check_suite: |
| 16 | + types: [completed] |
| 17 | + |
| 18 | +jobs: |
| 19 | + download-if-workflow-run: |
| 20 | + runs-on: ubuntu-latest |
| 21 | + outputs: |
| 22 | + pr_number: ${{ steps.pr_output.outputs.pr_number }} |
| 23 | + pr_sha: ${{ steps.pr_output.outputs.pr_sha }} |
| 24 | + # if conditions on all individual steps because subsequent jobs depend on this job |
| 25 | + # and we cannot skip it entirely |
| 26 | + steps: |
| 27 | + - name: 'Download workflow_run artifact' |
| 28 | + if: github.event_name == 'workflow_run' |
| 29 | + uses: dawidd6/action-download-artifact@v9 |
| 30 | + with: |
| 31 | + run_id: ${{ github.event.workflow_run.id }} |
| 32 | + name: pr_info |
| 33 | + path: pr/ |
| 34 | + search_artifacts: true |
| 35 | + |
| 36 | + - name: 'Determine PR info' |
| 37 | + # PR info comes from the artifact if downloaded, or GitHub context if not. |
| 38 | + if: github.event_name == 'workflow_run' |
| 39 | + id: 'pr_output' |
| 40 | + run: | |
| 41 | + if [[ ! -f pr/pr_number ]]; then |
| 42 | + echo "${{ github.event.pull_request.number }}" > pr/pr_number |
| 43 | + fi |
| 44 | + if [[ ! -f pr/pr_sha ]]; then |
| 45 | + echo "${{ github.event.pull_request.head.sha }}" > pr/pr_sha |
| 46 | + fi |
| 47 | + cat pr/* |
| 48 | + echo "pr_number=$(cat pr/pr_number)" >> "$GITHUB_OUTPUT" |
| 49 | + echo "pr_sha=$(cat pr/pr_sha)" >> "$GITHUB_OUTPUT" |
| 50 | +
|
| 51 | + run-security-guardian: |
| 52 | + # Necessary to have sufficient permissions to write to the PR |
| 53 | + permissions: |
| 54 | + contents: read |
| 55 | + pull-requests: write |
| 56 | + statuses: read |
| 57 | + issues: read |
| 58 | + checks: read |
| 59 | + runs-on: ubuntu-latest |
| 60 | + needs: download-if-workflow-run |
| 61 | + steps: |
| 62 | + - name: Checkout |
| 63 | + uses: actions/checkout@v4 |
| 64 | + |
| 65 | + - name: Get list of changed .template.json files |
| 66 | + id: filter_files |
| 67 | + run: | |
| 68 | + echo "Getting changed CloudFormation templates..." |
| 69 | + mkdir -p changed_templates |
| 70 | +
|
| 71 | + git fetch origin main --depth=1 |
| 72 | +
|
| 73 | + base_sha="${{ github.event.pull_request.base.sha }}" |
| 74 | + head_sha="${{ github.event.pull_request.head.sha }}" |
| 75 | + if [[ -z "$base_sha" ]]; then base_sha=$(git merge-base origin/main HEAD); fi |
| 76 | + if [[ -z "$head_sha" ]]; then head_sha=HEAD; fi |
| 77 | +
|
| 78 | + git diff --name-status "$base_sha" "$head_sha" \ |
| 79 | + | grep -E '^(A|M)\s+.*\.template\.json$' \ |
| 80 | + | awk '{print $2}' > changed_files.txt || true |
| 81 | +
|
| 82 | + while IFS= read -r file; do |
| 83 | + if [ -f "$file" ]; then |
| 84 | + safe_name=$(echo "$file" | sed 's|/|_|g') |
| 85 | + cp "$file" "changed_templates/$safe_name" |
| 86 | + else |
| 87 | + echo "::warning::Changed file not found in workspace: $file" |
| 88 | + fi |
| 89 | + done < changed_files.txt |
| 90 | +
|
| 91 | + if [ -s changed_files.txt ]; then |
| 92 | + echo "files_changed=true" >> $GITHUB_OUTPUT |
| 93 | + else |
| 94 | + echo "files_changed=false" >> $GITHUB_OUTPUT |
| 95 | + fi |
| 96 | + |
| 97 | + - name: Install cfn-guard |
| 98 | + if: steps.filter_files.outputs.files_changed == 'true' |
| 99 | + run: | |
| 100 | + mkdir -p $HOME/.local/bin |
| 101 | + curl -L -o cfn-guard.tar.gz https://github.com/aws-cloudformation/cloudformation-guard/releases/latest/download/cfn-guard-v3-x86_64-ubuntu-latest.tar.gz |
| 102 | + tar -xzf cfn-guard.tar.gz |
| 103 | + mv cfn-guard-v3-*/cfn-guard $HOME/.local/bin/cfn-guard |
| 104 | + chmod +x $HOME/.local/bin/cfn-guard |
| 105 | + echo "$HOME/.local/bin" >> $GITHUB_PATH |
| 106 | + |
| 107 | + - name: Install & Build security-guardian |
| 108 | + if: steps.filter_files.outputs.files_changed == 'true' |
| 109 | + run: yarn install --frozen-lockfile && cd tools/@aws-cdk/security-guardian && yarn build |
| 110 | + |
| 111 | + - name: Run cfn-guard if templates changed |
| 112 | + if: steps.filter_files.outputs.files_changed == 'true' |
| 113 | + uses: ./tools/@aws-cdk/security-guardian |
| 114 | + with: |
| 115 | + data_directory: './changed_templates' |
| 116 | + rule_set_path: './tools/@aws-cdk/security-guardian/rules/trust_scope_rules.guard' |
| 117 | + show_summary: 'fail' |
| 118 | + output_format: 'single-line-summary' |
0 commit comments