Repository navigation
511 lines (488 loc) · 21.3 KB
/
Copy pathtesting.yaml
File metadata and controls
511 lines (488 loc) · 21.3 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
---
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: 2026 The Linux Foundation
# Exercises the reusable workflows in this repository against three of the
# largest downstream Python consumers, and an in-repository fixture, in
# parallel, to prove the reusable workflows function end-to-end:
# * dependamerge (standard single-arch) -> build-test.yaml
# * lftools-uv (standard single-arch) -> build-test.yaml
# * python-nss-ng (multi-arch C build) -> build-test-multiarch.yaml
# * tests/fixtures/grype-gate (fixture) -> build-test.yaml
# * tests/fixtures/mixed-markers (fixture) -> build-test.yaml
#
# The reusable workflows are referenced with the self-repository form
# ('$/'), which resolves this repository at the commit already
# running, so the workflows under test are always the ones this
# branch defines. The standard targets are
# checked out at their default/HEAD branch; the python-nss-ng leg is
# pinned to the v1.3.6 release commit (see ref: below).
#
# The dependamerge and python-nss-ng legs run the dependencies-changed
# Grype gate. Each builds another repository, which the
# repository-metadata job cannot compare against this pull request, so
# the verdict is unresolved and the gate stays on. The fixture leg
# builds this pull request itself, so its verdict resolves, and it
# watches only the docker preset, which nothing in the fixture matches:
# the gate must drop. grype-gate-assert proves both from the run, and
# grype-gate-wiring reads the workflow files for the wiring no run can
# show.
name: 'Testing 🧪'
# yamllint disable-line rule:truthy
on:
# No workflow_dispatch: a manual run happens on the default branch,
# which would hand this job a cache token with write access to the
# default-branch scope while it builds and tests third-party
# consumer repositories. Untrusted code from those repositories
# could then poison caches that later runs restore (CWE-349).
# Pull request runs write only to their own cache scope.
pull_request:
branches: ['main']
concurrency:
group: "${{ github.workflow }}-${{ github.ref }}"
cancel-in-progress: true
permissions: {}
jobs:
# Standard single-arch consumers, fanned out in parallel via a matrix.
# Each leg passes its own artifact_suffix: both call build-test.yaml
# in the same run, and unsuffixed artefact names would collide, so
# the leg whose upload the other hid would fail its Grype download.
standard:
name: 'Standard (${{ matrix.name }})'
strategy:
fail-fast: false
matrix:
include:
# Another repository at its default branch, so the change
# cannot be resolved and the gate must stay on: this
# exercises the fail-closed path through the SBOM artefact
- name: 'dependamerge'
repository: lfreleng-actions/dependamerge
grype_gate_when: 'dependencies-changed'
# Today's behaviour: detection never runs, no gate step
- name: 'lftools-uv'
repository: lfreleng-actions/lftools-uv
grype_gate_when: 'always'
permissions:
contents: read
pull-requests: read # Reusable workflow reads PR metadata
uses: $/.github/workflows/build-test.yaml
with:
repository: ${{ matrix.repository }}
artifact_suffix: '-${{ matrix.name }}'
grype_gate_when: ${{ matrix.grype_gate_when }}
# Exercise the allow-list inputs without granting anything new.
# A same-organisation list may follow its default branch, and
# 'lfreleng-actions//' resolves to the organisation list itself,
# which de-duplication absorbs. The organisation list already
# carries github.com:443, so the action reports it as redundant.
harden_runner_supplemental_allowlist: 'lfreleng-actions//'
harden_runner_supplemental_unpinned: true
harden_runner_extra_endpoints: 'github.com:443'
# Multi-arch C consumer (runs in parallel with the standard matrix).
#
# python-nss-ng provides the committed native setup script
# (.github/scripts/setup-nss.sh) consumed via the setup_script input.
# Pinned to the v1.3.6 release commit of the upstream repository.
multiarch:
name: 'Multi-Arch'
permissions:
contents: read
pull-requests: read # Reusable workflow reads PR metadata
uses: $/.github/workflows/build-test-multiarch.yaml
with:
repository: lfreleng-actions/python-nss-ng
ref: 74011d9ba5167d00daa1cbc1910a77b54b895254 # v1.3.6
make: true
make_args: 'deps-nss env-github-actions'
setup_script: '.github/scripts/setup-nss.sh'
# A fixture at a pinned commit of another repository: unresolved,
# so each SBOM leg's in-leg Grype step keeps the gate on
grype_gate_when: 'dependencies-changed'
# The resolved path: a project in this repository, so the
# repository-metadata job checks out this pull request and compares
# it with its parent. Only the docker preset is watched, and no
# Dockerfile sits in or above the fixture, so the verdict is a
# deterministic 'false' whatever else the pull request changes. The
# steps that add nothing to the gate are off.
fixture:
name: 'Fixture (grype-gate)'
permissions:
contents: read
pull-requests: read # Reusable workflow reads PR metadata
uses: $/.github/workflows/build-test.yaml
with:
path_prefix: 'tests/fixtures/grype-gate'
artifact_suffix: '-grype-gate-fixture'
tests_enabled: false
audit_enabled: false
cbom_enabled: false
grype_gate_when: 'dependencies-changed'
grype_dependency_ecosystems: 'docker'
# A whole-workflow build of a Python project beside a package.json.
# The build, audit and metadata jobs must all resolve the same project.
mixed-markers:
name: 'Fixture (mixed-markers)'
permissions:
contents: read
pull-requests: read # Reusable workflow reads PR metadata
uses: $/.github/workflows/build-test.yaml
with:
path_prefix: 'tests/fixtures/mixed-markers'
artifact_suffix: '-mixed-markers-fixture'
tests_enabled: false
audit_enabled: true
sbom_enabled: false
grype_enabled: false
cbom_enabled: false
# The dependencies-changed gate trusts the verdict the
# repository-metadata job computes and hands grype-scan-action as
# dependencies-changed. The action's own suite proves the verdict;
# tests/test-grype-gate-inputs.sh proves the wiring around it in all
# four workflows: the pinned call and its inputs, the checkout depth
# it needs (actions/checkout fetches one commit by default, and the
# comparison needs the parent), the job output guarded by the job's
# result, the policy and detection inputs with their defaults and
# pass-throughs, the validate-only call in gerrit-validate, which
# must share the scan's pin and mirror its inputs, and the
# ecosystem validation step, which it extracts and drives against
# every value it must reject.
# tests/test-grype-sbom-binding.sh proves the scan reads its own
# call's artefact. Both scripts run as pre-commit hooks. The job also
# runs tests/test-python-metadata.sh, which proves every workflow
# takes its Python versions from one pinned python-metadata job.
grype-gate-wiring:
name: 'Test: Grype gate wiring'
runs-on: ubuntu-latest
permissions:
contents: read
timeout-minutes: 5
steps:
- name: 'Load egress allow-list'
# yamllint disable-line rule:line-length
uses: lfreleng-actions/harden-runner-block-action@65ff87957e1cac2de3203a85dbb07c31810321cb # v0.16.4
- name: 'Harden runner'
# yamllint disable-line rule:line-length
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
with:
egress-policy: 'block'
allowed-endpoints: >
${{ env.CONNECTION_ALLOW_LIST }}
- name: 'Checkout repository'
# yamllint disable-line rule:line-length
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: 'Assert Grype gate wiring and input validation'
shell: bash
run: |
# Assert Grype gate wiring and input validation
if ! bash tests/test-grype-gate-inputs.sh; then
echo "Grype gate wiring incorrect ❌" >> "$GITHUB_STEP_SUMMARY"
exit 1
fi
echo "Grype gate wiring correct ✅" >> "$GITHUB_STEP_SUMMARY"
# The scan has to read its own call's SBOM: by the upload's ID,
# or in the leg that wrote it. A download by name would hand a
# call that shares a name with a later one the other call's
# SBOM. The standard legs pass distinct suffixes, so a run
# cannot show this; the script reads it from the workflow files.
- name: 'Assert the Grype scan is bound to the SBOM upload'
shell: bash
run: |
# Assert the Grype scan is bound to the SBOM upload
if ! bash tests/test-grype-sbom-binding.sh; then
echo "Grype scan not bound to its SBOM ❌" >> "$GITHUB_STEP_SUMMARY"
exit 1
fi
echo "Grype scan bound to its SBOM ✅" >> "$GITHUB_STEP_SUMMARY"
# The standard and multi-arch legs must agree on the Python
# versions a project supports, so all four workflows resolve them
# through the same pinned build-metadata-action call, and nothing
# reads them from python-build-action's embedded copy.
- name: 'Assert the Python versions come from python-metadata'
shell: bash
run: |
# Assert the Python versions come from python-metadata
if ! bash tests/test-python-metadata.sh; then
echo "Python version source incorrect ❌" >> "$GITHUB_STEP_SUMMARY"
exit 1
fi
echo "Python version source correct ✅" >> "$GITHUB_STEP_SUMMARY"
# The workflows declare project_type: python, so a Python project
# beside a higher-ranked marker file (here a package.json) still
# reads as itself. This job runs their metadata step on such a
# fixture; tests/test-python-metadata.sh holds the step to the
# workflows' pin and with: block, bar path_prefix. The
# mixed-markers fixture leg below runs the whole workflow and proves
# python-build-action makes the same declared-type call.
python-metadata-fixture:
name: 'Test: Declared Python project type'
runs-on: ubuntu-latest
permissions:
contents: read
timeout-minutes: 5
steps:
- name: 'Load egress allow-list'
# yamllint disable-line rule:line-length
uses: lfreleng-actions/harden-runner-block-action@65ff87957e1cac2de3203a85dbb07c31810321cb # v0.16.4
- name: 'Harden runner'
# yamllint disable-line rule:line-length
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
with:
egress-policy: 'block'
allowed-endpoints: >
${{ env.CONNECTION_ALLOW_LIST }}
- name: 'Checkout repository'
# yamllint disable-line rule:line-length
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: 'Extract Python project metadata'
id: metadata
# yamllint disable-line rule:line-length
uses: lfreleng-actions/build-metadata-action@689e13653176908922b12f85cb93b061c467ba6f # v0.13.2
with:
path_prefix: 'tests/fixtures/mixed-markers'
project_type: python
output_format: summary
include_environment: true
use_version_extract: true
verbose: true
- name: 'Assert the fixture reads as its Python project'
shell: bash
env:
MATRIX_JSON: ${{ steps.metadata.outputs.python_matrix_json }}
PROJECT_NAME: ${{ steps.metadata.outputs.project_name }}
EXPECTED_NAME: 'mixed-markers-fixture'
run: |
# Assert the fixture reads as its Python project
set -euo pipefail
status=0
if ! printf '%s' "${MATRIX_JSON}" | jq -e \
'."python-version" | type == "array" and length > 0' \
> /dev/null 2>&1; then
echo "::error::No usable Python version matrix: '${MATRIX_JSON}'"
status=1
fi
if [ "${PROJECT_NAME}" != "${EXPECTED_NAME}" ]; then
echo "::error::project_name is '${PROJECT_NAME}'," \
"expected '${EXPECTED_NAME}'"
status=1
fi
if [ "${status}" -ne 0 ]; then
echo "Declared Python project type ignored ❌" \
>> "$GITHUB_STEP_SUMMARY"
exit 1
fi
echo "Declared Python project type honoured ✅" \
>> "$GITHUB_STEP_SUMMARY"
mixed-markers-assert:
name: 'Test: Mixed markers build artefact name'
runs-on: ubuntu-latest
needs: ['mixed-markers']
if: ${{ !cancelled() }}
permissions:
actions: read # List this run's artefacts
timeout-minutes: 10
steps:
- name: 'Load egress allow-list'
# yamllint disable-line rule:line-length
uses: lfreleng-actions/harden-runner-block-action@65ff87957e1cac2de3203a85dbb07c31810321cb # v0.16.4
- name: 'Harden runner'
# yamllint disable-line rule:line-length
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
with:
egress-policy: 'block'
allowed-endpoints: >
${{ env.CONNECTION_ALLOW_LIST }}
- name: 'Define gh_api retry helper'
shell: bash
run: |
# Define gh_api retry helper
set -euo pipefail
cat > "${RUNNER_TEMP}/gh-api.sh" << 'EOF'
gh_api() {
local attempt delay out
for attempt in 1 2 3; do
if out="$(gh api "$@")"; then
printf '%s\n' "${out}"
return 0
fi
if [ "${attempt}" -eq 3 ]; then
return 1
fi
delay="$((attempt * 5))"
echo "::warning::gh api failed, attempt ${attempt} of 3;" \
"retrying in ${delay}s" >&2
sleep "${delay}"
done
}
EOF
- name: 'Assert the build artefact name'
shell: bash
env:
GH_TOKEN: ${{ github.token }}
REPOSITORY: ${{ github.repository }}
RUN_ID: ${{ github.run_id }}
LEG_RESULT: ${{ needs.mixed-markers.result }}
run: |
# Assert the build artefact name
set -euo pipefail
status=0
if [ "${LEG_RESULT}" != 'success' ]; then
echo "::error::Mixed-markers leg ${LEG_RESULT}, expected success"
status=1
fi
# shellcheck source=/dev/null
. "${RUNNER_TEMP}/gh-api.sh"
api="repos/${REPOSITORY}/actions/runs/${RUN_ID}"
names="$(gh_api --paginate "${api}/artifacts" \
--jq '.artifacts[].name')"
if ! grep -Fxq 'mixed-markers-fixture' <<< "${names}"; then
echo "::error::No artifact named mixed-markers-fixture"
status=1
fi
if grep -Fxq 'mixed-markers-node-side' <<< "${names}"; then
echo "::error::Unexpected artifact named mixed-markers-node-side"
status=1
fi
if [ "${status}" -eq 0 ]; then
echo "Mixed-markers build artefact name is correct ✅" \
>> "$GITHUB_STEP_SUMMARY"
fi
exit "${status}"
# Proves the gate verdicts from the run itself, not just that the job
# graph went green. Each consumer dependencies-changed leg builds
# another repository, which the repository-metadata job cannot
# compare against this pull request: the verdict is empty and the
# Grype step keeps the gate on. The fixture leg resolves to 'false'
# and the Grype step drops the gate. Runs whatever the legs'
# verdicts, so a CVE in a consumer cannot hide these checks.
grype-gate-assert:
name: 'Test: Grype gate follows the dependency verdict'
runs-on: ubuntu-latest
needs: ['standard', 'multiarch', 'fixture']
if: ${{ !cancelled() }}
permissions:
actions: read # list this run's jobs and download its artefacts
timeout-minutes: 10
steps:
- name: 'Load egress allow-list'
# yamllint disable-line rule:line-length
uses: lfreleng-actions/harden-runner-block-action@65ff87957e1cac2de3203a85dbb07c31810321cb # v0.16.4
- name: 'Harden runner'
# yamllint disable-line rule:line-length
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
with:
egress-policy: 'block'
allowed-endpoints: >
${{ env.CONNECTION_ALLOW_LIST }}
# The step below sources this helper for its read-only listing,
# so one transient 5xx cannot fail the job. It buffers each
# attempt and prints only a complete one, so a page that failed
# mid-pagination is never half-printed; after the third failure
# it returns non-zero, having shown gh's own error each time.
- name: 'Define gh_api retry helper'
shell: bash
run: |
# Define gh_api retry helper
set -euo pipefail
cat > "${RUNNER_TEMP}/gh-api.sh" << 'EOF'
gh_api() {
local attempt delay out
for attempt in 1 2 3; do
if out="$(gh api "$@")"; then
printf '%s\n' "${out}"
return 0
fi
if [ "${attempt}" -eq 3 ]; then
return 1
fi
delay="$((attempt * 5))"
echo "::warning::gh api failed, attempt ${attempt} of 3;" \
"retrying in ${delay}s" >&2
sleep "${delay}"
done
}
EOF
# A called workflow's step outputs never reach the caller, so the
# Grype step's gate-active output is read from the step name that
# carries it ('Grype gate active: true'), which the jobs listing
# shows. The always leg must have skipped that step: its gate is
# on by construction, and the summary would say nothing new. A
# skipped step's name may stay unrendered, so only its conclusion
# is checked.
- name: 'Assert the gate follows the dependency verdict'
shell: bash
env:
GH_TOKEN: ${{ github.token }}
REPOSITORY: ${{ github.repository }}
RUN_ID: ${{ github.run_id }}
# Keep in step with the standard matrix names and the
# multiarch runners default. The in-leg Grype step of a
# multi-arch SBOM job runs in that job.
ARMED: |
Standard (dependamerge) / Grype Audit SBOM
Multi-Arch / SBOM x64
Multi-Arch / SBOM arm64
ALWAYS: |
Standard (lftools-uv) / Grype Audit SBOM
RESOLVED: |
Fixture (grype-gate) / Grype Audit SBOM
run: |
# Assert the gate follows the dependency verdict
set -euo pipefail
status=0
fail() { echo "::error::$*"; status=1; }
# shellcheck source=/dev/null
. "${RUNNER_TEMP}/gh-api.sh"
api="repos/${REPOSITORY}/actions/runs/${RUN_ID}"
# One row per job: its name, verdict, and the name and verdict
# of its 'Grype gate active' step, or 'absent'. Small pages:
# each job carries its steps, and a page of 100 outruns the
# API gateway timeout and answers 502
jobs="$(gh_api --paginate "${api}/jobs?filter=latest&per_page=30" \
--jq '.jobs[] | [.name, (.conclusion // .status),
([.steps[]? | select(.name | startswith("Grype gate active"))
| .name] | first // "absent"),
([.steps[]? | select(.name | startswith("Grype gate active"))
| (.conclusion // .status)] | first // "absent")]
| @tsv')"
# $1 job name, $2 expected step name or '*', $3 expected step
# conclusion
check() {
local row result name conclusion
row="$(awk -F'\t' -v n="$1" '$1 == n' <<< "${jobs}")"
result="$(cut -f2 <<< "${row}")"
name="$(cut -f3 <<< "${row}")"
conclusion="$(cut -f4 <<< "${row}")"
case "${result}" in
success|failure) ;;
'') fail "$1: no such job in this run"; return ;;
*) fail "$1: ${result//$'\n'/ }, expected it to run"; return ;;
esac
if [ "${conclusion}" != "$3" ]; then
fail "$1: gate step '${name}' ${conclusion//$'\n'/ }," \
"expected $3"
elif [ "$2" != '*' ] && [ "${name}" != "$2" ]; then
fail "$1: gate step named '${name//$'\n'/ }', expected '$2'"
else
echo "$1: ${result}; gate step '${name}' ${conclusion} ✅"
fi
}
while read -r job; do
if [ -n "${job}" ]; then
check "${job}" 'Grype gate active: true' success
fi
done <<< "${ARMED}"
while read -r job; do
if [ -n "${job}" ]; then
check "${job}" '*' skipped
fi
done <<< "${ALWAYS}"
while read -r job; do
if [ -n "${job}" ]; then
check "${job}" 'Grype gate active: false' success
fi
done <<< "${RESOLVED}"
exit "${status}"