Skip to content

Commit 6b7528b

Browse files
authored
fix(reusable-zizmor): fix runs from forks (#970)
Forks can't get an ID token. We use this method to look up the version we're called at: get an OIDC ID token and then use the `job_workflow_ref` claim. We're currently hard failing when this doesn't work, which means we throw an error for runs from forks. It would be better to still succeed, and fall back to using the default config.
1 parent b42327d commit 6b7528b

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

.github/workflows/reusable-zizmor.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ show the current results.
1717

1818
## Examples
1919

20-
**Online Checks**
20+
### Online Checks
2121

2222
```yaml
2323
name: Zizmor GitHub Actions static analysis
@@ -39,13 +39,17 @@ jobs:
3939
actions: read
4040
contents: read
4141

42-
# used in the `job-workflow-ref` job to fetch an OIDC token, which allows
43-
# the run to determine its ref
42+
# used in the `job-workflow-ref` job to fetch an OIDC token, which
43+
# allows the run to determine its ref. That's used to find the default
44+
# configuration file. This doesn't work from forks. In that case,
45+
# Zizmor's default config behaviour will be used.
4446
id-token: write
4547

4648
# required to comment on pull requests with the results of the check
4749
pull-requests: write
48-
# required to upload the results to GitHub's code scanning service
50+
# required to upload the results to GitHub's code scanning service. This
51+
# doesn't work if the repo doesn't have Advanced Security enabled. In that
52+
# case we'll skip the upload.
4953
security-events: write
5054

5155
uses: grafana/shared-workflows/.github/workflows/reusable-zizmor.yml@<some sha>
@@ -54,7 +58,7 @@ jobs:
5458
fail-severity: any
5559
```
5660
57-
**Faster Offline Checks**
61+
### Faster Offline Checks
5862
5963
```yaml
6064
name: Zizmor GitHub Actions static analysis (online checks)
@@ -76,9 +80,17 @@ jobs:
7680
actions: read
7781
contents: read
7882

83+
# used in the `job-workflow-ref` job to fetch an OIDC token, which
84+
# allows the run to determine its ref. That's used to find the default
85+
# configuration file. This doesn't work from forks. In that case,
86+
# Zizmor's default config behaviour will be used.
87+
id-token: write
88+
7989
# required to comment on pull requests with the results of the check
8090
pull-requests: write
81-
# required to upload the results to GitHub's code scanning service
91+
# required to upload the results to GitHub's code scanning service. This
92+
# doesn't work if the repo doesn't have Advanced Security enabled. In that
93+
# case we'll skip the upload.
8294
security-events: write
8395

8496
uses: grafana/shared-workflows/.github/workflows/reusable-zizmor.yml@<some sha>

.github/workflows/reusable-zizmor.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ jobs:
9999
const { jwtVerify, createRemoteJWKSet } = require('jose');
100100
101101
async function retrieveIdToken(audience) {
102+
// Perform an explicit check to see if we can get the ID token or
103+
// not, so we can show a better error.
104+
const runtimeUrl = process.env['ACTIONS_ID_TOKEN_REQUEST_URL']
105+
if (!runtimeUrl) {
106+
throw new Error(
107+
"We're unable to look up the version of the Zizmor workflow being called, so we can't fetch the `grafana` default configuration. Zizmor's own default will be used. Is the `is-token: write` permission set? If so, is this a run from a fork? Unfortunately we're unable to do this lookup for pull requests from forks currently."
108+
)
109+
}
110+
102111
core.debug(`Attempting to retrieve ID token with audience: ${audience}...`);
103112
104113
const idToken = await core.getIDToken(audience);
@@ -199,7 +208,9 @@ jobs:
199208
core.setOutput('repo', repo);
200209
core.setOutput('sha', sha);
201210
} catch (error) {
202-
core.setFailed(`Script failed: ${error.message}`);
211+
// On errors, we log an error messge, but we don't fail. It's
212+
// better to run with the default config than not run at all.
213+
core.error(`Script failed: ${error.message}`);
203214
204215
if (error.stack) {
205216
console.error(`Stack trace: ${error.stack}`);

0 commit comments

Comments
 (0)