-
Notifications
You must be signed in to change notification settings - Fork 207
144 lines (131 loc) · 5.87 KB
/
Copy pathsecurity-review.yml
File metadata and controls
144 lines (131 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
name: security-review
on:
pull_request_target:
branches: ["*"]
types: [opened, synchronize, reopened, closed] # Note: closed is needed to update internal tracking state
push:
branches: [main]
concurrency:
group: >
${{ github.workflow }}-
${{ github.event_name == 'pull_request_target'
&& format('pr-{0}', github.event.pull_request.number)
|| format('{0}-{1}', github.ref_type || 'ref', github.ref_name || github.ref) }}
cancel-in-progress: true
permissions:
contents: read
jobs:
# no-op step so that we can enable security-review / report as a required PR check
register-check:
if: github.event_name == 'push'
runs-on: ubuntu-latest
permissions:
statuses: write
steps:
- name: Register check name on main
env:
GH_TOKEN: ${{ github.token }}
run: |
gh api "repos/${{ github.repository }}/statuses/${{ github.sha }}" \
-f state="success" \
-f context="security-review / report" \
-f description="No PR to review"
authorize:
if: github.event_name != 'push'
runs-on: ubuntu-latest
outputs:
approval-env: ${{ steps.authorization.outputs.approval-env }}
steps:
- uses: actions/checkout@v7
- name: Check authorization
id: authorization
uses: ./.github/actions/check-authorization
execute:
if: github.event_name != 'push'
needs: authorize
runs-on: ubuntu-latest
environment: ${{ needs.authorize.outputs.approval-env }}
permissions:
id-token: write
contents: read
statuses: write
pull-requests: write
steps:
- uses: actions/checkout@v7
with:
ref: ${{ github.event.pull_request.head.sha }}
# Safe: this job's fork checkout is gated behind the authorize
# deployment-environment approval (AWS-542), so fork code is only
# checked out after a maintainer approves the run. That approval gate
# is exactly the human-in-the-loop mitigation that
# actions/checkout@v7's new pull_request_target default requires; this
# opt-in re-asserts our existing protection, it does not weaken it.
allow-unsafe-pr-checkout: true
- name: Get AWS credentials
uses: aws-actions/configure-aws-credentials@v6
with:
role-to-assume: arn:aws:iam::620771051181:role/SecurityReview-GitHubOIDCRole
role-session-name: ${{ github.run_id }}-${{ github.run_attempt }}
aws-region: us-west-2
- name: Run review
id: codebuild
shell: bash
env:
PROJECT_NAME: SecurityReview-${{ github.event.repository.name }}
SOURCE_VERSION: "refs/pull/${{ github.event.pull_request.number }}/head^{${{ github.event.pull_request.head.sha }}}"
PR_TITLE: ${{ github.event.pull_request.title }}
PR_AUTHOR: ${{ github.event.pull_request.user.login }}
PR_STATE: ${{ github.event.pull_request.merged && 'merged' || github.event.pull_request.state }}
run: |
ENV_OVERRIDES=$(jq -nc \
--arg title "$PR_TITLE" --arg author "$PR_AUTHOR" --arg state "$PR_STATE" \
'[{name:"PR_TITLE",value:$title,type:"PLAINTEXT"},{name:"PR_AUTHOR",value:$author,type:"PLAINTEXT"},{name:"PR_STATE",value:$state,type:"PLAINTEXT"}]')
BUILD_ID=$(aws codebuild start-build \
--project-name "$PROJECT_NAME" \
--source-version "$SOURCE_VERSION" \
--environment-variables-override "$ENV_OVERRIDES" \
--query 'build.id' --output text)
while STATUS=$(aws codebuild batch-get-builds --ids "$BUILD_ID" \
--query 'builds[0].buildStatus' --output text); [[ "$STATUS" == "IN_PROGRESS" ]]; do
sleep 30
done
REVIEW_STATUS=$(aws codebuild batch-get-builds --ids "$BUILD_ID" \
--query 'builds[0].exportedEnvironmentVariables[?name==`REVIEW_STATUS`].value' --output text)
echo "review_status=$REVIEW_STATUS" >> "$GITHUB_OUTPUT"
- name: Update commit status
if: always() && github.event.action != 'closed'
env:
GH_TOKEN: ${{ github.token }}
REVIEW_STATUS: ${{ steps.codebuild.outputs.review_status }}
REPO_NAME: ${{ github.event.repository.name }}
PR_NUMBER: ${{ github.event.pull_request.number }}
PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
run: |
if [[ "$REVIEW_STATUS" == "PASS" ]]; then
STATE="success"
elif [[ "$REVIEW_STATUS" == "BLOCKING" ]]; then
STATE="failure"
else
STATE="error"
fi
ARGS=(-f state="$STATE" -f context="security-review / report")
if [[ "$STATE" != "error" ]]; then
ARGS+=(-f target_url="https://d225oy8drgbol2.cloudfront.net/${REPO_NAME}/findings.html?pr=${PR_NUMBER}")
else
ARGS+=(-f description="Review did not complete successfully")
fi
gh api "repos/${{ github.repository }}/statuses/${PR_HEAD_SHA}" "${ARGS[@]}"
- name: Post review comment
if: always() && github.event.action != 'closed' && (steps.codebuild.outputs.review_status == 'PASS' || steps.codebuild.outputs.review_status == 'BLOCKING')
env:
GH_TOKEN: ${{ github.token }}
REPO_NAME: ${{ github.event.repository.name }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
if ! gh api "repos/${{ github.repository }}/issues/${PR_NUMBER}/comments" \
--jq '.[].body' | grep -q "security-review-comment"; then
gh api "repos/${{ github.repository }}/issues/${PR_NUMBER}/comments" \
-f body="<!-- security-review-comment -->
🔒 **Security Review** — [View Report](https://d225oy8drgbol2.cloudfront.net/${REPO_NAME}/findings.html?pr=${PR_NUMBER})
Please review before merging."
fi