Skip to content

Commit 6d53d0d

Browse files
authored
Add authorization environments (#2843)
### Issues: Addresses V2002959392 ### Description of changes: Implements the guidance provided by internal tooling by using a GitHub "deployment" environment for manually approving workflow runs on GitHub workflows that use `pull_request_target` in order to get elevated credentials in our AWS CodeBuild-managed GitHub Actions runner environment. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and the ISC license.
1 parent a747549 commit 6d53d0d

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0 OR ISC
3+
4+
name: 'check-authorization'
5+
description: 'A helper action to determine the authorization level of a pull request for running CI'
6+
outputs:
7+
approval-env:
8+
description: 'The target environment to use for the workflow'
9+
value: ${{ steps.collab-check.outputs.result }}
10+
runs:
11+
using: 'composite'
12+
steps:
13+
- name: Collaborator Check
14+
uses: actions/github-script@v8
15+
id: collab-check
16+
with:
17+
result-encoding: string
18+
script: |
19+
try {
20+
const permissionResponse = await github.rest.repos.getCollaboratorPermissionLevel({
21+
owner: context.repo.owner,
22+
repo: context.repo.repo,
23+
username: context.payload.pull_request.user.login,
24+
});
25+
const permission = permissionResponse.data.permission;
26+
const hasWriteAccess = ['write', 'admin'].includes(permission);
27+
if (!hasWriteAccess) {
28+
console.log(`User ${context.payload.pull_request.user.login} does not have write access to the repository (permission: ${permission})`);
29+
return "manual-approval"
30+
} else {
31+
console.log(`Verifed ${context.payload.pull_request.user.login} has write access. Auto Approving PR Checks.`)
32+
return "auto-approve"
33+
}
34+
} catch (error) {
35+
console.log(`${context.payload.pull_request.user.login} does not have write access. Requiring Manual Approval to run PR Checks.`)
36+
return "manual-approval"
37+
}

.github/workflows/image-build-android.yml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,21 @@ permissions:
2929
contents: read
3030

3131
jobs:
32+
authorization-check:
33+
outputs:
34+
approval-env: ${{ steps.authz.outputs.approval-env }}
35+
runs-on:
36+
- codebuild-aws-lc-ci-github-actions-${{ github.run_id }}-${{ github.run_attempt }}
37+
image:linux-5.0
38+
instance-size:small
39+
steps:
40+
- uses: actions/checkout@v5
41+
- uses: ./.github/actions/check-authorization
42+
id: authz
43+
3244
build:
45+
needs: [authorization-check]
46+
environment: ${{ needs.authorization-check.outputs.approval-env }}
3347
runs-on:
3448
codebuild-aws-lc-ci-github-actions-${{ github.run_id }}-${{ github.run_attempt }}
3549
image:linux-5.0
@@ -39,7 +53,7 @@ jobs:
3953
steps:
4054
- uses: actions/checkout@v5
4155
with:
42-
ref: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.ref || github.ref }}
56+
ref: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.sha || github.ref }}
4357
- name: Query Environment
4458
id: env
4559
run: |
@@ -74,11 +88,13 @@ jobs:
7488
7589
push:
7690
if: ${{ github.event_name != 'pull_request_target' }}
91+
environment: ${{ needs.authorization-check.outputs.approval-env }}
7792
runs-on:
7893
codebuild-aws-lc-ci-github-actions-${{ github.run_id }}-${{ github.run_attempt }}
7994
image:linux-5.0
8095
instance-size:small
8196
needs:
97+
- authorization-check
8298
- build
8399
outputs:
84100
android: ${{ steps.images.outputs.android }}

0 commit comments

Comments
 (0)