Skip to content

Commit 7281902

Browse files
committed
chore(ci): refactor workflows to scope permissions (#1978)
1 parent 57cb2a0 commit 7281902

21 files changed

+390
-204
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const {
2+
PR_ACTION,
3+
PR_AUTHOR,
4+
PR_BODY,
5+
PR_NUMBER,
6+
IGNORE_AUTHORS,
7+
LABEL_BLOCK,
8+
LABEL_BLOCK_MISSING_LICENSE_AGREEMENT,
9+
} = require('./constants');
10+
11+
module.exports = async ({ github, context, core }) => {
12+
if (IGNORE_AUTHORS.includes(PR_AUTHOR)) {
13+
return core.notice('Author in IGNORE_AUTHORS list; skipping...');
14+
}
15+
16+
if (PR_ACTION != 'opened') {
17+
return core.notice(
18+
'Only newly open PRs are labelled to avoid spam; skipping'
19+
);
20+
}
21+
22+
const RELATED_ACK_SECTION_REGEX =
23+
/By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice./;
24+
25+
const isMatch = RELATED_ACK_SECTION_REGEX.exec(PR_BODY);
26+
27+
if (isMatch == null) {
28+
core.info(
29+
`No acknowledgement section found, maybe the author didn't use the template but there is one.`
30+
);
31+
32+
const msg =
33+
"No acknowledgement section found. Please make sure you used the template to open a PR and didn't remove the acknowledgment section. Check the template here: https://github.com/aws-powertools/powertools-lambda-python/blob/develop/.github/PULL_REQUEST_TEMPLATE.md#acknowledgment";
34+
35+
await Promise.allSettled([
36+
github.rest.issues.createComment({
37+
owner: context.repo.owner,
38+
repo: context.repo.repo,
39+
body: msg,
40+
issue_number: PR_NUMBER,
41+
}),
42+
github.rest.issues.addLabels({
43+
issue_number: PR_NUMBER,
44+
owner: context.repo.owner,
45+
repo: context.repo.repo,
46+
labels: [LABEL_BLOCK, LABEL_BLOCK_MISSING_LICENSE_AGREEMENT],
47+
}),
48+
]);
49+
}
50+
};

.github/workflows/closed-issues-message.yml

-23
This file was deleted.
+37-16
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
name: Dispatch analytics
22

3+
# PROCESS
4+
#
5+
# 1. Trade GitHub JWT token with AWS credentials for the analytics account
6+
# 2. Invoke a Lambda function dispatcher synchronously with the read-only scoped JWT token
7+
# 3. The dispatcher function will call GitHub APIs to read data from the last hour and aggregate for operational analytics
8+
9+
# USAGE
10+
#
11+
# NOTE: meant to use as a scheduled task only (or manually for debugging purposes).
12+
313
on:
414
workflow_dispatch:
515

616
schedule:
717
- cron: '0 * * * *'
818

919
permissions:
10-
id-token: write
11-
actions: read
12-
checks: read
1320
contents: read
14-
deployments: read
15-
issues: read
16-
discussions: read
17-
packages: read
18-
pages: read
19-
pull-requests: read
20-
repository-projects: read
21-
security-events: read
22-
statuses: read
2321

2422
jobs:
2523
dispatch_token:
@@ -28,6 +26,20 @@ jobs:
2826
group: analytics
2927
runs-on: ubuntu-latest
3028
environment: analytics
29+
permissions:
30+
id-token: write
31+
actions: read
32+
checks: read
33+
contents: read # previously we needed `write` to use GH_TOKEN in our dispatcher (Lambda)
34+
deployments: read
35+
issues: read
36+
discussions: read
37+
packages: read
38+
pages: read
39+
pull-requests: read
40+
repository-projects: read
41+
security-events: read
42+
statuses: read
3143
steps:
3244
- name: Configure AWS credentials
3345
uses: aws-actions/configure-aws-credentials@010d0da01d0b5a38af31e9c3470dbfdabdecca3a # v4.0.1
@@ -39,7 +51,16 @@ jobs:
3951
- name: Invoke Lambda function
4052
run: |
4153
payload=$(echo -n '{"githubToken": "${{ secrets.GITHUB_TOKEN }}"}' | base64)
42-
aws lambda invoke \
43-
--function-name ${{ secrets.AWS_ANALYTICS_DISPATCHER_ARN }} \
44-
--payload "$payload" response.json
45-
cat response.json
54+
response=$(aws lambda invoke \
55+
--function-name "${{ secrets.AWS_ANALYTICS_DISPATCHER_ARN }}" \
56+
--payload "$payload" \
57+
response.json \
58+
--query 'FunctionError' \
59+
--output text)
60+
61+
cat response.json ; echo # add newline at the end
62+
63+
if [ "$response" != "None" ]; then
64+
echo "Error invoking lambda function: $response. Aborting."
65+
exit 1
66+
fi

.github/workflows/label_pr_on_title.yml

+25-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
name: Label PR based on title
22

3+
# PROCESS
4+
#
5+
# 1. Fetch PR details previously saved from untrusted location
6+
# 2. Parse details for safety
7+
# 3. Label PR based on semantic title (e.g., area, change type)
8+
9+
# USAGE
10+
#
11+
# NOTE: meant to be used with ./.github/workflows/record_pr.yml
12+
#
13+
# Security Note:
14+
#
15+
# This workflow depends on "Record PR" workflow that runs in an untrusted location (forks) instead of `pull_request_target`.
16+
# This enforces zero trust where "Record PR" workflow always runs on fork with zero permissions on GH_TOKEN.
17+
# When "Record PR" completes, this workflow runs in our repository with the appropriate permissions and sanitize inputs.
18+
#
19+
# Coupled with "Approve GitHub Action to run on forks", we have confidence no privilege can be escalated,
20+
# since any malicious change would need to be approved, and upon social engineering, it'll have zero permissions.
21+
322
on:
423
workflow_run:
524
workflows: ["Record PR details"]
@@ -11,23 +30,23 @@ permissions:
1130

1231
jobs:
1332
get_pr_details:
33+
permissions:
34+
actions: read # download PR artifact
35+
contents: read # checkout code
1436
# Guardrails to only ever run if PR recording workflow was indeed
1537
# run in a PR event and ran successfully
1638
if: ${{ github.event.workflow_run.conclusion == 'success' }}
1739
uses: ./.github/workflows/reusable_export_pr_details.yml
1840
with:
1941
record_pr_workflow_id: ${{ github.event.workflow_run.id }}
2042
workflow_origin: ${{ github.event.repository.full_name }}
21-
permissions:
22-
contents: read
23-
pull-requests: read
2443
secrets:
2544
token: ${{ secrets.GITHUB_TOKEN }}
2645
label_pr:
27-
permissions:
28-
pull-requests: write
2946
needs: get_pr_details
3047
runs-on: ubuntu-latest
48+
permissions:
49+
pull-requests: write # label respective PR
3150
steps:
3251
- name: Checkout repository
3352
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
@@ -43,4 +62,4 @@ jobs:
4362
# and label PR based on semantic title accordingly
4463
script: |
4564
const script = require('.github/scripts/label_pr_based_on_title.js')
46-
await script({github, context, core})
65+
await script({github, context, core})

.github/workflows/measure-packages-size.yml

-45
This file was deleted.

.github/workflows/on-doc-v2-merge.yml

-25
This file was deleted.
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Closed Issue Message
2+
3+
# PROCESS
4+
#
5+
# 1. Comment on recently closed issues to warn future responses may not be looked after
6+
7+
# USAGE
8+
#
9+
# Always triggered upon issue closure
10+
11+
on:
12+
issues:
13+
types: [closed]
14+
15+
permissions:
16+
contents: read
17+
18+
jobs:
19+
auto_comment:
20+
runs-on: ubuntu-latest
21+
permissions:
22+
issues: write # comment on issues
23+
steps:
24+
- uses: aws-actions/closed-issue-message@36b7048ea77bb834d16e7a7c5b5471ac767a4ca1 # v1.0.0
25+
with:
26+
repo-token: "${{ secrets.GITHUB_TOKEN }}"
27+
message: |
28+
⚠️ **COMMENT VISIBILITY WARNING** ⚠️
29+
30+
This issue is now closed. Please be mindful that future comments are hard for our team to see.
31+
32+
If you need more assistance, please either tag a [team member](https://docs.powertools.aws.dev/lambda/typescript/latest/maintainers/#current-maintainers) or open a new issue that references this one.
33+
34+
If you wish to keep having a conversation with other community members under this issue feel free to do so.

.github/workflows/on_doc_merge.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ permissions:
1414
jobs:
1515
release-docs:
1616
permissions:
17-
contents: write
18-
pages: write
17+
actions: write
1918
id-token: write
2019
secrets: inherit
2120
uses: ./.github/workflows/reusable-publish-docs.yml

0 commit comments

Comments
 (0)