Skip to content

Commit e644bbc

Browse files
committed
Refactor onboarding workflow to improve security and clarity. Changed permissions to read for contents, updated steps to use generated GitHub App token for authentication, and enhanced variable handling for repository and branch detection.
1 parent 3c667be commit e644bbc

File tree

1 file changed

+39
-25
lines changed

1 file changed

+39
-25
lines changed

.github/workflows/onboard-new-repo.yml

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@ jobs:
1919
runs-on: ubuntu-latest
2020
environment: onboarding
2121
permissions:
22-
contents: write
23-
pull-requests: write
22+
contents: read
2423
steps:
2524
- name: Checkout scanner action repository
2625
uses: actions/checkout@v4
2726
with:
2827
path: scanner-repo
2928

30-
- name: Determine target repository
29+
- name: Parse target repository inputs
3130
id: target
3231
run: |
3332
validate_name() {
@@ -58,13 +57,33 @@ jobs:
5857
5958
validate_name "$ORG" "Organization" 39
6059
validate_name "$REPO_NAME" "Repository" 100
61-
REPO="$ORG/$REPO_NAME"
6260
63-
# Auto-detect default branch from the repository
61+
echo "organization=$ORG" >> "$GITHUB_OUTPUT"
62+
echo "repo_name=$REPO_NAME" >> "$GITHUB_OUTPUT"
63+
echo "repository=$ORG/$REPO_NAME" >> "$GITHUB_OUTPUT"
64+
shell: bash
65+
env:
66+
EVENT_NAME: ${{ github.event_name }}
67+
EVENT_ORG: ${{ github.event.client_payload.organization }}
68+
EVENT_REPO: ${{ github.event.client_payload.repository }}
69+
INPUT_ORG: ${{ inputs.organization }}
70+
INPUT_REPO: ${{ inputs.repository }}
71+
72+
- name: Generate GitHub App token
73+
id: app_token
74+
uses: actions/create-github-app-token@v2
75+
with:
76+
app-id: ${{ secrets.ONBOARDING_APP_ID }}
77+
private-key: ${{ secrets.ONBOARDING_APP_PRIVATE_KEY }}
78+
owner: ${{ steps.target.outputs.organization }}
79+
repositories: ${{ steps.target.outputs.repo_name }}
80+
81+
- name: Detect default branch
82+
id: detect_branch
83+
run: |
6484
echo "Detecting default branch for $REPO..."
6585
BASE_BRANCH=$(gh api "repos/$REPO" --jq '.default_branch' 2>/dev/null) || BASE_BRANCH=""
6686
67-
# If repo is empty or API call failed, default to 'main'
6887
if [ -z "$BASE_BRANCH" ] || [ "$BASE_BRANCH" = "null" ]; then
6988
echo "Repository is empty or default branch not found. Defaulting to 'main'"
7089
BASE_BRANCH="main"
@@ -75,16 +94,11 @@ jobs:
7594
exit 1
7695
fi
7796
78-
echo "repository=$REPO" >> "$GITHUB_OUTPUT"
7997
echo "base_branch=$BASE_BRANCH" >> "$GITHUB_OUTPUT"
8098
shell: bash
8199
env:
82-
GH_TOKEN: ${{ secrets.ONBOARDING_TOKEN }}
83-
EVENT_NAME: ${{ github.event_name }}
84-
EVENT_ORG: ${{ github.event.client_payload.organization }}
85-
EVENT_REPO: ${{ github.event.client_payload.repository }}
86-
INPUT_ORG: ${{ inputs.organization }}
87-
INPUT_REPO: ${{ inputs.repository }}
100+
GH_TOKEN: ${{ steps.app_token.outputs.token }}
101+
REPO: ${{ steps.target.outputs.repository }}
88102

89103
- name: Check for opt-out file
90104
id: check_opt_out
@@ -97,9 +111,9 @@ jobs:
97111
fi
98112
shell: bash
99113
env:
100-
GH_TOKEN: ${{ secrets.ONBOARDING_TOKEN }}
114+
GH_TOKEN: ${{ steps.app_token.outputs.token }}
101115
REPO: ${{ steps.target.outputs.repository }}
102-
BASE_BRANCH: ${{ steps.target.outputs.base_branch }}
116+
BASE_BRANCH: ${{ steps.detect_branch.outputs.base_branch }}
103117

104118
- name: Skip onboarding (repository opted out)
105119
if: steps.check_opt_out.outputs.opted_out == 'true'
@@ -133,28 +147,28 @@ jobs:
133147
echo "is_empty=$IS_EMPTY" >> "$GITHUB_OUTPUT"
134148
shell: bash
135149
env:
136-
GH_TOKEN: ${{ secrets.ONBOARDING_TOKEN }}
150+
GH_TOKEN: ${{ steps.app_token.outputs.token }}
137151
REPO: ${{ steps.target.outputs.repository }}
138152

139153
- name: Checkout target repository
140154
if: steps.check_opt_out.outputs.opted_out != 'true' && steps.check_empty.outputs.is_empty == 'false'
141155
uses: actions/checkout@v4
142156
with:
143157
repository: ${{ steps.target.outputs.repository }}
144-
token: ${{ secrets.ONBOARDING_TOKEN }}
158+
token: ${{ steps.app_token.outputs.token }}
145159
path: target-repo
146-
ref: ${{ steps.target.outputs.base_branch }}
160+
ref: ${{ steps.detect_branch.outputs.base_branch }}
147161

148162
- name: Initialize empty repository locally
149163
if: steps.check_opt_out.outputs.opted_out != 'true' && steps.check_empty.outputs.is_empty == 'true'
150164
run: |
151165
mkdir -p target-repo
152166
cd target-repo
153167
git init
154-
git remote add origin "https://x-access-token:${ONBOARDING_TOKEN}@github.com/${REPO}.git"
168+
git remote add origin "https://x-access-token:${APP_TOKEN}@github.com/${REPO}.git"
155169
shell: bash
156170
env:
157-
ONBOARDING_TOKEN: ${{ secrets.ONBOARDING_TOKEN }}
171+
APP_TOKEN: ${{ steps.app_token.outputs.token }}
158172
REPO: ${{ steps.target.outputs.repository }}
159173

160174
- name: Create branch and add SAST workflow
@@ -163,7 +177,7 @@ jobs:
163177
working-directory: target-repo
164178
env:
165179
IS_EMPTY: ${{ steps.check_empty.outputs.is_empty }}
166-
BASE_BRANCH: ${{ steps.target.outputs.base_branch }}
180+
BASE_BRANCH: ${{ steps.detect_branch.outputs.base_branch }}
167181
run: |
168182
git config user.name "MetaMask Security Bot"
169183
git config user.email "security-bot@metamask.io"
@@ -216,9 +230,9 @@ jobs:
216230
if: steps.check_opt_out.outputs.opted_out != 'true' && steps.check_empty.outputs.is_empty == 'false' && steps.create_branch.outputs.skipped != 'true'
217231
working-directory: target-repo
218232
env:
219-
GH_TOKEN: ${{ secrets.ONBOARDING_TOKEN }}
233+
GH_TOKEN: ${{ steps.app_token.outputs.token }}
220234
REPO_NAME: ${{ steps.target.outputs.repository }}
221-
BASE_BRANCH: ${{ steps.target.outputs.base_branch }}
235+
BASE_BRANCH: ${{ steps.detect_branch.outputs.base_branch }}
222236
run: |
223237
# Extract owner and repo name for URL construction
224238
OWNER=$(echo "$REPO_NAME" | cut -d'/' -f1)
@@ -240,7 +254,7 @@ jobs:
240254
if: steps.check_opt_out.outputs.opted_out != 'true' && steps.check_empty.outputs.is_empty == 'false' && steps.create_branch.outputs.skipped != 'true'
241255
working-directory: target-repo
242256
env:
243-
GH_TOKEN: ${{ secrets.ONBOARDING_TOKEN }}
257+
GH_TOKEN: ${{ steps.app_token.outputs.token }}
244258
run: |
245259
PR_URL=$(gh pr view security/add-sast-scanner --json url -q .url)
246260
echo "✅ Pull Request created: $PR_URL"
@@ -255,7 +269,7 @@ jobs:
255269
shell: bash
256270
env:
257271
REPO: ${{ steps.target.outputs.repository }}
258-
BASE_BRANCH: ${{ steps.target.outputs.base_branch }}
272+
BASE_BRANCH: ${{ steps.detect_branch.outputs.base_branch }}
259273

260274
- name: Post to Slack channel on failure
261275
if: ${{ failure() && env.SLACK_WEBHOOK_URL != '' }}

0 commit comments

Comments
 (0)