mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-30 04:06:34 +09:00
ci(workflows): run push and schedule workflows only in cozystack/cozystack (#5186)
## What this PR does GitHub runs no CI in a security advisory's temporary private fork, and merging from the advisory skips branch protection, so an embargoed fix currently reaches `main` with nothing having run on it. This PR prepares the repository for verifying such a fix in a separate private copy of the repository (an embargo mirror), and documents that process. - Every job of a workflow that a push or a schedule starts now runs only in `cozystack/cozystack`. A mirror receives all workflow files on each sync, and without the guard it would build, publish, prune and cut releases from that copy on every sync and cron tick. Workflows that also run on `pull_request` keep running on pull requests, which is what the mirror is for. Every job carries the guard rather than only the root jobs, because a job whose condition calls a status function such as `!cancelled()` runs even when the job it needs was skipped. - `hack/workflow-repo-guard.bats` holds every current and future push or schedule workflow to that guard, accepting it only as the leading conjunct so nothing ORed after it can reopen the job. - `docs/security/incident-response.md` describes the verification flow: sync the mirror on demand, open the fix PR inside it, record the green run in the advisory, then merge from the advisory. It also records why the mirror must not hold the public registry credentials: pull request CI pushes every image and the packages artifact (the full chart tree) to a registry that is anonymously readable, tag listing included, so a run there would publish the fix before the release. Behaviour in `cozystack/cozystack` is unchanged: the guard is true there, and each existing condition is kept intact inside the new conjunction. Contributor forks with Actions enabled stop running these workflows too. Out of scope, as a follow-up: letting pull request CI push to a private registry and pull from it in the E2E cluster, so the mirror can run the E2E suite and not only planning, pre-commit and the unit tests. Verification: - `hack/workflow-repo-guard.bats` was red before the workflow changes (30 unguarded jobs) and is green after; removing the guard from `tags.yaml` `rc-e2e`, dropping the pull request clause from `zizmor.yml`, or adding an unguarded scheduled workflow each turns it red. - Every changed workflow is identical to `main` once job `if` fields are removed (`yq 'del(.jobs[].if)'`). - `zizmor --offline` reports no findings; `actionlint` reports the same findings on the changed files as on `main`. - `hack/stitch-multiarch_test.bats` pinned the exact `if` of two `tags.yaml` jobs and is updated to the guarded form. ### Screenshots Not a UI change. ### Downstream repositories - [x] No downstream repository is affected by this change - [ ] [cozystack/website](https://github.com/cozystack/website) - follow-up: - [ ] [cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack) - follow-up: - [ ] [cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack) - follow-up: - [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up: - [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up: - [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) - follow-up: - [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) - follow-up: - [ ] [cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server) - follow-up: - [ ] [cozystack/external-apps-example](https://github.com/cozystack/external-apps-example) - follow-up: - [ ] [cozystack/examples](https://github.com/cozystack/examples) - follow-up: - [ ] [cozystack/community](https://github.com/cozystack/community) - follow-up: The trigger map entry that touches this diff is `cozystack/ccp` for `tags.yaml`: its `package-bump` skill describes the release pipeline of `cozystack/cozystack`, which this PR does not change. ### Release note ```release-note ci(workflows): push- and schedule-triggered workflows run only in cozystack/cozystack, so a private mirror used to verify embargoed security fixes does not build, publish or release from its copy. ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **CI** * Scheduled, release, and maintenance workflows now run only in the canonical repository. Pull-request checks continue to run where configured. * Build, validation, and security workflows now follow repository-specific run conditions. * **Documentation** * Updated incident-response guidance with steps for verifying fixes in a private advisory mirror, including CI limitations, check requirements, and credential restrictions. * **Tests** * Added workflow checks to verify repository guards and event-specific conditions. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
20 files changed
+247
-18
No files matched your search
@@ -82,9 +82,9 @@ jobs:
|
||||
# of the gate, which can only withhold a re-evaluation: a push then leaves no
|
||||
# status at all, and a withdrawn approval is caught by the sweep.
|
||||
if: >-
|
||||
${{ github.event_name == 'workflow_run'
|
||||
${{ github.repository == 'cozystack/cozystack' && (github.event_name == 'workflow_run'
|
||||
&& github.event.workflow_run.conclusion != 'cancelled'
|
||||
&& github.event.workflow_run.conclusion != 'skipped' }}
|
||||
&& github.event.workflow_run.conclusion != 'skipped') }}
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
pull-requests: read
|
||||
@@ -157,7 +157,7 @@ jobs:
|
||||
detect:
|
||||
name: Detect sizeable API changes
|
||||
needs: [resolve]
|
||||
if: ${{ needs.resolve.outputs.should_run == 'true' }}
|
||||
if: ${{ github.repository == 'cozystack/cozystack' && (needs.resolve.outputs.should_run == 'true') }}
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: read
|
||||
@@ -236,9 +236,9 @@ jobs:
|
||||
name: Report fork API review gate status
|
||||
needs: [resolve, detect]
|
||||
if: >-
|
||||
${{ !cancelled()
|
||||
${{ github.repository == 'cozystack/cozystack' && (!cancelled()
|
||||
&& (github.event_name != 'workflow_run'
|
||||
|| needs.resolve.outputs.should_run == 'true') }}
|
||||
|| needs.resolve.outputs.should_run == 'true')) }}
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
@@ -24,6 +24,7 @@ permissions:
|
||||
|
||||
jobs:
|
||||
build-arm64:
|
||||
if: github.repository == 'cozystack/cozystack'
|
||||
name: Build main arm64 images
|
||||
runs-on: oracle-vm-24cpu-96gb-arm64
|
||||
# Starts from the rc leg's budget; re-measure after the first runs.
|
||||
|
||||
@@ -38,6 +38,7 @@ permissions:
|
||||
|
||||
jobs:
|
||||
warm-cache:
|
||||
if: github.repository == 'cozystack/cozystack'
|
||||
name: Warm build cache
|
||||
# Runs on the ephemeral pool, not the dedicated self-hosted runner, so the
|
||||
# build path no longer depends on that host (#2937 decommission). Serial
|
||||
|
||||
@@ -60,6 +60,7 @@ permissions:
|
||||
|
||||
jobs:
|
||||
build-line:
|
||||
if: github.repository == 'cozystack/cozystack'
|
||||
name: Build ${{ github.ref_name }} images and packages artifact
|
||||
runs-on: oracle-vm-24cpu-96gb-x86-64
|
||||
timeout-minutes: 120
|
||||
|
||||
@@ -21,6 +21,7 @@ concurrency:
|
||||
|
||||
jobs:
|
||||
analyze:
|
||||
if: (github.event_name == 'pull_request' || github.repository == 'cozystack/cozystack')
|
||||
name: Analyze (${{ matrix.language }})
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 120
|
||||
|
||||
@@ -38,6 +38,7 @@ permissions:
|
||||
|
||||
jobs:
|
||||
e2e:
|
||||
if: github.repository == 'cozystack/cozystack'
|
||||
name: E2E (isp-hosted on kind)
|
||||
# The whole platform lands on one kind node with nothing pre-pulled. The
|
||||
# smaller 4-vCPU class was not tried; sizing down is left to a measured run.
|
||||
|
||||
@@ -38,7 +38,7 @@ jobs:
|
||||
# cancels it, so the schedule fires only where the stand is declared;
|
||||
# manual dispatch always runs. The runner must be v2.327.1 or newer, the
|
||||
# minimum the checkout README names for its node24 runtime.
|
||||
if: github.event_name == 'workflow_dispatch' || vars.PROXMOX_NIGHTLY == 'true'
|
||||
if: ${{ github.repository == 'cozystack/cozystack' && (github.event_name == 'workflow_dispatch' || vars.PROXMOX_NIGHTLY == 'true') }}
|
||||
runs-on: [self-hosted, proxmox]
|
||||
timeout-minutes: 90
|
||||
env:
|
||||
|
||||
@@ -55,6 +55,7 @@ concurrency:
|
||||
|
||||
jobs:
|
||||
triage:
|
||||
if: github.repository == 'cozystack/cozystack'
|
||||
runs-on: ubuntu-latest
|
||||
# A run that reaches the write cap paces roughly seven minutes of work, so
|
||||
# anything near this ceiling is a call that hung rather than a long sweep.
|
||||
|
||||
@@ -23,6 +23,7 @@ concurrency:
|
||||
|
||||
jobs:
|
||||
validate:
|
||||
if: (github.event_name == 'pull_request' || github.repository == 'cozystack/cozystack')
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: read
|
||||
@@ -73,7 +74,7 @@ jobs:
|
||||
|
||||
sync:
|
||||
needs: validate
|
||||
if: github.event_name != 'pull_request'
|
||||
if: ${{ (github.event_name == 'pull_request' || github.repository == 'cozystack/cozystack') && (github.event_name != 'pull_request') }}
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
@@ -41,7 +41,7 @@ jobs:
|
||||
# packages artifact + installer chart on GHCR. No rebuild.
|
||||
mirror:
|
||||
name: Mirror images to GHCR
|
||||
if: vars.NIGHTLY_ENABLED == 'true' || github.event_name == 'workflow_dispatch'
|
||||
if: ${{ github.repository == 'cozystack/cozystack' && (vars.NIGHTLY_ENABLED == 'true' || github.event_name == 'workflow_dispatch') }}
|
||||
runs-on: oracle-vm-4cpu-16gb-x86-64
|
||||
timeout-minutes: 60
|
||||
permissions:
|
||||
@@ -188,6 +188,7 @@ jobs:
|
||||
# ── Assemble the Talos nocloud disk from the upstream imager (no cozystack
|
||||
# image dependency) and publish it as a GHCR OCI artifact.
|
||||
build-disk:
|
||||
if: github.repository == 'cozystack/cozystack'
|
||||
name: Build & publish nocloud disk
|
||||
needs: [mirror]
|
||||
runs-on: oracle-vm-4cpu-16gb-x86-64
|
||||
@@ -239,6 +240,7 @@ jobs:
|
||||
# upstream Talos image, so the nocloud disk built above is published but
|
||||
# not exercised here, and e2e does not wait for it.
|
||||
e2e:
|
||||
if: github.repository == 'cozystack/cozystack'
|
||||
name: E2E (full suite)
|
||||
needs: [mirror]
|
||||
# Same sandbox as the other e2e lanes: three nodes at 8 vCPU / 24 GiB each
|
||||
|
||||
@@ -57,6 +57,7 @@ concurrency:
|
||||
|
||||
jobs:
|
||||
prune:
|
||||
if: github.repository == 'cozystack/cozystack'
|
||||
name: Prune old GHCR temporary artifacts
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
|
||||
@@ -12,6 +12,7 @@ permissions: read-all
|
||||
|
||||
jobs:
|
||||
analysis:
|
||||
if: github.repository == 'cozystack/cozystack'
|
||||
name: Scorecard analysis
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
|
||||
@@ -16,6 +16,7 @@ concurrency:
|
||||
|
||||
jobs:
|
||||
stale:
|
||||
if: github.repository == 'cozystack/cozystack'
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
issues: write
|
||||
|
||||
@@ -28,7 +28,7 @@ jobs:
|
||||
# e2e sandbox is amd64 only, so none of them has an arm64 half here.
|
||||
build-arm64:
|
||||
name: Build arm64 images
|
||||
if: contains(github.ref_name, '-')
|
||||
if: ${{ github.repository == 'cozystack/cozystack' && (contains(github.ref_name, '-')) }}
|
||||
runs-on: oracle-vm-24cpu-96gb-arm64
|
||||
# Starts from build-amd64's budget; re-measure after the first runs.
|
||||
timeout-minutes: 120
|
||||
@@ -140,6 +140,7 @@ jobs:
|
||||
# It runs for every tag, since the release-exists no-op and the rejection of
|
||||
# a hand-pushed stable tag live here.
|
||||
build-amd64:
|
||||
if: github.repository == 'cozystack/cozystack'
|
||||
name: Build amd64 images and assets
|
||||
# Ephemeral pool, not the dedicated self-hosted runner (#2937 decommission).
|
||||
# Serial `make build` + `make assets` is CPU-bound, so use the large shape —
|
||||
@@ -388,7 +389,7 @@ jobs:
|
||||
# build-arm64 is skipped only for a stable tag, which never builds. On a
|
||||
# pre-release, either half failing or being cancelled leaves this job
|
||||
# skipped and the run red: an rc ships multi-arch or not at all.
|
||||
if: ${{ !cancelled() && needs.build-amd64.result == 'success' && (needs.build-arm64.result == 'success' || needs.build-arm64.result == 'skipped') }}
|
||||
if: ${{ github.repository == 'cozystack/cozystack' && (!cancelled() && needs.build-amd64.result == 'success' && (needs.build-arm64.result == 'success' || needs.build-arm64.result == 'skipped')) }}
|
||||
runs-on: oracle-vm-24cpu-96gb-x86-64
|
||||
# The stitch reads and writes every first-party image once, then the
|
||||
# packages artifact, the chart and the assets are pushed; start there and
|
||||
@@ -643,7 +644,7 @@ jobs:
|
||||
# !cancelled(): every job below build-arm64 carries a status function,
|
||||
# since a stable tag skips build-arm64. This one only runs for an rc,
|
||||
# where build-arm64 always runs, so here it just follows that rule.
|
||||
if: ${{ !cancelled() && needs.prepare-release.result == 'success' && contains(github.ref_name, '-rc.') }}
|
||||
if: ${{ github.repository == 'cozystack/cozystack' && (!cancelled() && needs.prepare-release.result == 'success' && contains(github.ref_name, '-rc.')) }}
|
||||
# A caller's permissions are the ceiling for every job in the called
|
||||
# workflow, and GitHub validates that ceiling STATICALLY, when it creates the
|
||||
# run — before this job's `if:` is evaluated, let alone before a step runs.
|
||||
@@ -700,7 +701,7 @@ jobs:
|
||||
# finalized at the stable release, so the changelog is generated then.
|
||||
# !cancelled(): this job runs on stable tags only, which skip build-arm64,
|
||||
# and a skipped ancestor makes the implicit success() false.
|
||||
if: ${{ !cancelled() && needs.prepare-release.result == 'success' && !contains(github.ref_name, '-') }}
|
||||
if: ${{ github.repository == 'cozystack/cozystack' && (!cancelled() && needs.prepare-release.result == 'success' && !contains(github.ref_name, '-')) }}
|
||||
# Non-blocking: this is a best-effort backfill of an already-published
|
||||
# release, never a gate on one.
|
||||
continue-on-error: true
|
||||
@@ -959,7 +960,7 @@ jobs:
|
||||
# promoted stable.
|
||||
# Stable tags only — a prerelease (ref_name contains '-') would otherwise open
|
||||
# an "update managed apps for vX.Y.Z-rc.N" website PR on every rc.
|
||||
if: ${{ !cancelled() && needs.prepare-release.result == 'success' && !contains(github.ref_name, '-') }}
|
||||
if: ${{ github.repository == 'cozystack/cozystack' && (!cancelled() && needs.prepare-release.result == 'success' && !contains(github.ref_name, '-')) }}
|
||||
permissions:
|
||||
contents: read
|
||||
steps:
|
||||
|
||||
@@ -24,6 +24,7 @@ permissions:
|
||||
|
||||
jobs:
|
||||
test:
|
||||
if: (github.event_name == 'pull_request' || github.repository == 'cozystack/cozystack')
|
||||
name: Typecheck and test
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
|
||||
@@ -30,6 +30,7 @@ permissions:
|
||||
|
||||
jobs:
|
||||
update-releasenotes:
|
||||
if: github.repository == 'cozystack/cozystack'
|
||||
name: Update Release Notes
|
||||
# GitHub API calls only — no docker, no repo toolchain: GitHub-hosted.
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
@@ -32,6 +32,7 @@ concurrency:
|
||||
|
||||
jobs:
|
||||
zizmor:
|
||||
if: (github.event_name == 'pull_request' || github.repository == 'cozystack/cozystack')
|
||||
name: Audit workflows
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
|
||||
@@ -37,9 +37,7 @@ One person may hold more than one role on a small incident.
|
||||
configuration change, a policy, or disabling an affected feature), it is
|
||||
documented and shared with the reporter and, if warranted, with affected
|
||||
adopters under embargo.
|
||||
4. **Private fix development.** The fix is developed under a **GitHub Security
|
||||
Advisory** (private fork) so that code and discussion stay confidential until
|
||||
release.
|
||||
4. **Private fix development.** The fix is developed under a **GitHub Security Advisory** (private fork) so that code and discussion stay confidential until release. CI does not run there, so the fix is verified in the embargo mirror described under [Verifying a private fix](#verifying-a-private-fix) before it is merged from the advisory.
|
||||
5. **Coordinated release.** The fix is shipped through the normal patch-release and backport process: the backport batch carries it onto the supported lines, the two latest minor releases, newest first, by the rules in [`docs/release/backports.md`](../release/backports.md), which a maintainer can override for a single fix. See the *Supported Versions* table in `SECURITY.md`.
|
||||
6. **Disclosure.** A GHSA — and, when applicable, a CVE — is published. Public
|
||||
disclosure follows the outbound channels: GitHub Releases and release notes,
|
||||
@@ -55,6 +53,21 @@ One person may hold more than one role on a small incident.
|
||||
root cause, and any follow-up hardening, and open tracking issues for
|
||||
preventative work.
|
||||
|
||||
## Verifying a private fix
|
||||
|
||||
GitHub runs no CI in an advisory's temporary private fork: status checks do not run on its pull requests and no integration can reach it. Merging from the advisory also skips branch protection, so the required checks on `main` are not asked for either. The fix is therefore verified in a separate private repository, the embargo mirror, and the run there stands in for the checks the merge skips.
|
||||
|
||||
1. **Sync the mirror** right before use, not continuously: push the current `main` and every affected `release-X.Y` branch from `cozystack/cozystack` to the mirror, so the fix is tested on the same base it will be merged into. Push branches by name and never tags: a pushed tag runs `tags.yaml` as the tagged commit carries it, and a tag cut before the guard below existed carries none, so the mirror would build a release.
|
||||
2. **Open the pull request inside the mirror** from the same fix branch as the advisory's pull request, and wait for its CI.
|
||||
3. **Record the run** in the advisory: a link to it, the `main` commit the mirror was synced to, and the head commit of the mirror's pull request, which must be the head commit of the advisory's pull request. CI checks out the pull request merged into its base rather than the head itself, and the merge from the advisory creates yet another commit on `main`, so the two pull request heads are what to compare. The run as a whole is red by design for any code change, because the mirror holds no registry credentials: `Finalize`, and `Build` or `Build Talos` when they are selected, fail at the registry login and E2E never starts. What must be green is `Plan build` and `Unit & controller tests` in the `Pull Request` workflow, and `pre-commit` and `Commit trailers` in `Pre-Commit Checks`.
|
||||
4. **Merge from the advisory.** First compare the recorded `main` commit with the current `main`. If `main` has moved, sync and run again before merging, then merge without waiting: a change elsewhere on `main` can break the fix without causing a merge conflict, so a run on an older base proves nothing about the newer one. The merge lands on whatever `main` is at that moment, so the CI that runs on `main` after the merge stays the final check.
|
||||
|
||||
Every job of a workflow that a push or a schedule starts is guarded to run only in `cozystack/cozystack`, so syncing `main` to the mirror does not build, publish, prune or release from it; `hack/workflow-repo-guard.bats` holds every such workflow to that guard. A push runs the workflows of the branch that was pushed, though, and the guard is on `main` only: a release line carries the workflows it was cut with, so pushing `release-X.Y` to the mirror starts that line's `build-release.yaml` there until the guard reaches the line. With registry credentials kept out of the mirror, as below, that build cannot publish anything and fails at its registry login, but it still occupies a runner.
|
||||
|
||||
Pull request workflows still run, which is what the mirror is for. CodeQL needs reading with care: code scanning on a private repository needs a GitHub Code Security license, so without one it fails in the mirror for a reason unrelated to the fix. It is a required check on `main`, but merging from the advisory skips branch protection, so a red CodeQL in the mirror does not block the merge; it runs on `main` after the merge.
|
||||
|
||||
The mirror must not be given the credentials of the public image registry. Pull request CI pushes every image it builds and the packages artifact, which carries the full chart tree, to that registry, and the registry is anonymously readable, tag listing included, so a run there would publish the fix before the release. Until pull request CI can push to a private registry instead, the mirror verifies only the jobs step 3 lists, and the E2E suite runs after the merge.
|
||||
|
||||
## Severity and timelines
|
||||
|
||||
Severity classification (CVSS v3.1) and the target remediation and disclosure
|
||||
|
||||
@@ -376,10 +376,10 @@ _make_world() {
|
||||
wf=.github/workflows/tags.yaml
|
||||
[ "$(yq -r '.jobs.build-amd64.needs // "none"' "$wf")" = none ]
|
||||
[ "$(yq -r '.jobs.build-arm64.needs // "none"' "$wf")" = none ]
|
||||
[ "$(yq -r '.jobs.build-arm64.if' "$wf")" = "contains(github.ref_name, '-')" ]
|
||||
[ "$(yq -r '.jobs.build-arm64.if' "$wf")" = "\${{ github.repository == 'cozystack/cozystack' && (contains(github.ref_name, '-')) }}" ]
|
||||
# The amd64 job runs for every tag: it holds the release-exists no-op and the
|
||||
# hand-pushed stable tag rejection.
|
||||
[ "$(yq -r '.jobs.build-amd64.if // "none"' "$wf")" = none ]
|
||||
[ "$(yq -r '.jobs.build-amd64.if // "none"' "$wf")" = "github.repository == 'cozystack/cozystack'" ]
|
||||
amd_steps=$(yq -r '.jobs.build-amd64.steps[].name' "$wf")
|
||||
echo "$amd_steps" | grep -qx 'Check if release already exists'
|
||||
echo "$amd_steps" | grep -qx 'Reject hand-pushed stable tags'
|
||||
|
||||
@@ -0,0 +1,200 @@
|
||||
#!/usr/bin/env bats
|
||||
|
||||
# Every job of a workflow that a push or a schedule can start runs only in
|
||||
# cozystack/cozystack. A private copy of the repository (the embargo mirror
|
||||
# docs/security/incident-response.md describes) receives these files with every
|
||||
# sync, and without the guard each sync and each cron tick there would build,
|
||||
# publish, prune and release with that copy's secrets. Pull request events stay
|
||||
# allowed, because checking a pull request is what such a copy exists for.
|
||||
#
|
||||
# Every job is pinned rather than only the roots: a job whose `if` calls a
|
||||
# status function such as `!cancelled()` runs even when the job it needs was
|
||||
# skipped, so guarding the roots alone does not stop it.
|
||||
|
||||
load test_helper
|
||||
|
||||
ROOT="$(cd "$(dirname "${BATS_TEST_FILENAME:-$0}")/.." && pwd)"
|
||||
WORKFLOWS="$ROOT/.github/workflows"
|
||||
|
||||
GUARD="github.repository == 'cozystack/cozystack'"
|
||||
PR_GUARD="(github.event_name == 'pull_request' || $GUARD)"
|
||||
|
||||
# `on` may be a single event name, a list of names or a map keyed by name.
|
||||
events() {
|
||||
yq '.on | [(select(tag == "!!str")), (select(tag == "!!seq") | .[]), (select(tag == "!!map") | keys | .[])] | .[]' "$1"
|
||||
}
|
||||
|
||||
# A workflow yq cannot parse fails the audit: dropping it would leave its jobs
|
||||
# unchecked while every other workflow passes.
|
||||
guarded_workflows() {
|
||||
local f names rc=0
|
||||
for f in "$1"/*.yml "$1"/*.yaml; do
|
||||
[ -e "$f" ] || continue
|
||||
if ! names="$(events "$f")"; then
|
||||
echo "cannot parse $f" >&2
|
||||
rc=1
|
||||
continue
|
||||
fi
|
||||
if grep -qxE 'push|schedule' <<<"$names"; then
|
||||
printf '%s\n' "$f"
|
||||
fi
|
||||
done
|
||||
return "$rc"
|
||||
}
|
||||
|
||||
# `${{ }}` and line breaks do not change what an `if` means, so both are
|
||||
# dropped before the condition is compared with the accepted forms.
|
||||
normalize() {
|
||||
tr '\n' ' ' | sed -E \
|
||||
-e 's/^[[:space:]]*\$\{\{[[:space:]]*//' \
|
||||
-e 's/[[:space:]]*\}\}[[:space:]]*$//' \
|
||||
-e 's/[[:space:]]+/ /g' \
|
||||
-e 's/^ //' -e 's/ $//'
|
||||
}
|
||||
|
||||
# True when the whole string is one parenthesized group: the parenthesis that
|
||||
# opens it closes on the last character. Parentheses inside single-quoted
|
||||
# literals do not count.
|
||||
one_group() {
|
||||
local s="$1" depth=0 quoted=0 i c
|
||||
[ "${s:0:1}" = "(" ] || return 1
|
||||
for ((i = 0; i < ${#s}; i++)); do
|
||||
c="${s:i:1}"
|
||||
if [ "$c" = "'" ]; then
|
||||
quoted=$((1 - quoted))
|
||||
elif [ "$quoted" -eq 0 ] && [ "$c" = "(" ]; then
|
||||
depth=$((depth + 1))
|
||||
elif [ "$quoted" -eq 0 ] && [ "$c" = ")" ]; then
|
||||
depth=$((depth - 1))
|
||||
[ "$depth" -eq 0 ] && [ "$i" -ne $((${#s} - 1)) ] && return 1
|
||||
fi
|
||||
done
|
||||
[ "$depth" -eq 0 ] && [ "$quoted" -eq 0 ]
|
||||
}
|
||||
|
||||
# The guard must be the leading conjunct, alone or ANDed with the job's own
|
||||
# condition as one parenthesized group. `&&` binds tighter than `||`, so a
|
||||
# tail like `(a) || (b)` would let `b` reopen the job.
|
||||
is_guarded() {
|
||||
local cond="$1" guard="$2"
|
||||
[ "$cond" = "$guard" ] && return 0
|
||||
case "$cond" in
|
||||
"$guard && "*) one_group "${cond#"$guard && "}" ;;
|
||||
*) return 1 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
unguarded_jobs() {
|
||||
local f="$1" guard="$GUARD" job cond
|
||||
if events "$f" | grep -qx pull_request; then
|
||||
guard="$PR_GUARD"
|
||||
fi
|
||||
while IFS= read -r job; do
|
||||
cond="$(yq ".jobs[\"$job\"].if // \"\"" "$f" | normalize)"
|
||||
is_guarded "$cond" "$guard" || printf '%s:%s\n' "$(basename "$f")" "$job"
|
||||
done < <(yq '.jobs | keys | .[]' "$f")
|
||||
}
|
||||
|
||||
@test "workflows a push or a schedule starts are discovered" {
|
||||
run guarded_workflows "$WORKFLOWS"
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"/build-main.yaml"* ]]
|
||||
[[ "$output" == *"/nightly.yaml"* ]]
|
||||
[[ "$output" == *"/tags.yaml"* ]]
|
||||
}
|
||||
|
||||
@test "every job of a push or schedule workflow runs only in cozystack/cozystack" {
|
||||
local f list missing
|
||||
list="$(guarded_workflows "$WORKFLOWS")"
|
||||
missing="$(while IFS= read -r f; do unguarded_jobs "$f"; done <<<"$list")"
|
||||
if [ -n "$missing" ]; then
|
||||
echo "jobs without the repository guard:" >&2
|
||||
echo "$missing" >&2
|
||||
false
|
||||
fi
|
||||
}
|
||||
|
||||
@test "guard check accepts the canonical forms" {
|
||||
is_guarded "$GUARD" "$GUARD"
|
||||
is_guarded "$GUARD && (contains(github.ref_name, '-'))" "$GUARD"
|
||||
is_guarded "$PR_GUARD" "$PR_GUARD"
|
||||
is_guarded "$PR_GUARD && (github.event_name != 'pull_request')" "$PR_GUARD"
|
||||
[ "$(printf '%s\n' "\${{ $GUARD &&" " (!cancelled()) }}" | normalize)" = "$GUARD && (!cancelled())" ]
|
||||
}
|
||||
|
||||
@test "guard check rejects a missing, trailing or reopened guard" {
|
||||
if is_guarded "" "$GUARD"; then false; fi
|
||||
if is_guarded "contains(github.ref_name, '-')" "$GUARD"; then false; fi
|
||||
if is_guarded "contains(github.ref_name, '-') && $GUARD" "$GUARD"; then false; fi
|
||||
if is_guarded "$GUARD || true" "$GUARD"; then false; fi
|
||||
if is_guarded "github.event_name == 'pull_request'" "$PR_GUARD"; then false; fi
|
||||
if is_guarded "$GUARD" "$PR_GUARD"; then false; fi
|
||||
if is_guarded "$GUARD && (true) || (github.event_name == 'push')" "$GUARD"; then false; fi
|
||||
if is_guarded "$PR_GUARD && (true) || (github.event_name == 'push')" "$PR_GUARD"; then false; fi
|
||||
if is_guarded "$GUARD && (')' == ')') || (true)" "$GUARD"; then false; fi
|
||||
is_guarded "$GUARD && (contains(github.ref_name, ')'))" "$GUARD"
|
||||
}
|
||||
|
||||
@test "an on given as a string or a list is read as event names" {
|
||||
local dir
|
||||
dir="$(mktemp -d)"
|
||||
printf 'on: push\njobs: {}\n' > "$dir/string.yaml"
|
||||
printf 'on: [pull_request, schedule]\njobs: {}\n' > "$dir/list.yaml"
|
||||
printf 'on: [pull_request]\njobs: {}\n' > "$dir/pr-only.yaml"
|
||||
run guarded_workflows "$dir"
|
||||
rm -rf "$dir"
|
||||
[ "$status" -eq 0 ]
|
||||
[ "$output" = "$(printf '%s\n' "$dir/list.yaml" "$dir/string.yaml")" ]
|
||||
}
|
||||
|
||||
@test "an unparseable workflow fails the audit instead of dropping out of it" {
|
||||
local dir
|
||||
dir="$(mktemp -d)"
|
||||
printf 'on: [push\njobs: {}\n' > "$dir/broken.yaml"
|
||||
printf 'on: push\njobs: {}\n' > "$dir/fine.yaml"
|
||||
run guarded_workflows "$dir"
|
||||
rm -rf "$dir"
|
||||
[ "$status" -ne 0 ]
|
||||
[[ "$output" == *"broken.yaml"* ]]
|
||||
}
|
||||
|
||||
@test "a list on that names pull_request requires the pull request form" {
|
||||
local dir
|
||||
dir="$(mktemp -d)"
|
||||
cat > "$dir/both.yaml" <<'EOF'
|
||||
on: [push, pull_request]
|
||||
jobs:
|
||||
plain:
|
||||
if: github.repository == 'cozystack/cozystack'
|
||||
runs-on: ubuntu-latest
|
||||
steps: [{run: "true"}]
|
||||
EOF
|
||||
run unguarded_jobs "$dir/both.yaml"
|
||||
rm -rf "$dir"
|
||||
[ "$status" -eq 0 ]
|
||||
[ "$output" = "both.yaml:plain" ]
|
||||
}
|
||||
|
||||
@test "an unguarded job in a scheduled workflow is reported" {
|
||||
local dir
|
||||
dir="$(mktemp -d)"
|
||||
cat > "$dir/cron.yaml" <<'EOF'
|
||||
on:
|
||||
schedule:
|
||||
- cron: "0 0 * * *"
|
||||
jobs:
|
||||
guarded:
|
||||
if: github.repository == 'cozystack/cozystack'
|
||||
runs-on: ubuntu-latest
|
||||
steps: [{run: "true"}]
|
||||
open:
|
||||
needs: guarded
|
||||
if: ${{ !cancelled() }}
|
||||
runs-on: ubuntu-latest
|
||||
steps: [{run: "true"}]
|
||||
EOF
|
||||
run unguarded_jobs "$dir/cron.yaml"
|
||||
rm -rf "$dir"
|
||||
[ "$status" -eq 0 ]
|
||||
[ "$output" = "cron.yaml:open" ]
|
||||
}
|
||||
Reference in new issue
Block a user