diff --git a/.github/workflows/api-review-gate-fork.yaml b/.github/workflows/api-review-gate-fork.yaml index 2e1884e56..3c0b22d6b 100644 --- a/.github/workflows/api-review-gate-fork.yaml +++ b/.github/workflows/api-review-gate-fork.yaml @@ -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 diff --git a/.github/workflows/build-arm64-nightly.yaml b/.github/workflows/build-arm64-nightly.yaml index c9b4bd2fe..9a224d8c2 100644 --- a/.github/workflows/build-arm64-nightly.yaml +++ b/.github/workflows/build-arm64-nightly.yaml @@ -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. diff --git a/.github/workflows/build-main.yaml b/.github/workflows/build-main.yaml index 885d58a09..66eb25a12 100644 --- a/.github/workflows/build-main.yaml +++ b/.github/workflows/build-main.yaml @@ -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 diff --git a/.github/workflows/build-release.yaml b/.github/workflows/build-release.yaml index 24025b81d..037b6f0b5 100644 --- a/.github/workflows/build-release.yaml +++ b/.github/workflows/build-release.yaml @@ -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 diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index b8790956c..787cab735 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -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 diff --git a/.github/workflows/e2e-isp-hosted.yaml b/.github/workflows/e2e-isp-hosted.yaml index bd34ddbb7..fdb20b7d1 100644 --- a/.github/workflows/e2e-isp-hosted.yaml +++ b/.github/workflows/e2e-isp-hosted.yaml @@ -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. diff --git a/.github/workflows/e2e-proxmox.yaml b/.github/workflows/e2e-proxmox.yaml index 2834b5591..a797ad7bc 100644 --- a/.github/workflows/e2e-proxmox.yaml +++ b/.github/workflows/e2e-proxmox.yaml @@ -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: diff --git a/.github/workflows/issue-triage.yaml b/.github/workflows/issue-triage.yaml index 87e0806cd..7d3805722 100644 --- a/.github/workflows/issue-triage.yaml +++ b/.github/workflows/issue-triage.yaml @@ -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. diff --git a/.github/workflows/labels.yaml b/.github/workflows/labels.yaml index f5789f5f2..441d693f0 100644 --- a/.github/workflows/labels.yaml +++ b/.github/workflows/labels.yaml @@ -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 diff --git a/.github/workflows/nightly.yaml b/.github/workflows/nightly.yaml index f80ef9cf1..25fdd61e2 100644 --- a/.github/workflows/nightly.yaml +++ b/.github/workflows/nightly.yaml @@ -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 diff --git a/.github/workflows/retention.yaml b/.github/workflows/retention.yaml index 9b6a50565..a84f02dd9 100644 --- a/.github/workflows/retention.yaml +++ b/.github/workflows/retention.yaml @@ -57,6 +57,7 @@ concurrency: jobs: prune: + if: github.repository == 'cozystack/cozystack' name: Prune old GHCR temporary artifacts runs-on: ubuntu-latest permissions: diff --git a/.github/workflows/scorecard.yml b/.github/workflows/scorecard.yml index 055e6c7f1..023893e59 100644 --- a/.github/workflows/scorecard.yml +++ b/.github/workflows/scorecard.yml @@ -12,6 +12,7 @@ permissions: read-all jobs: analysis: + if: github.repository == 'cozystack/cozystack' name: Scorecard analysis runs-on: ubuntu-latest permissions: diff --git a/.github/workflows/stale.yaml b/.github/workflows/stale.yaml index c2e7b4bfd..658b2ce12 100644 --- a/.github/workflows/stale.yaml +++ b/.github/workflows/stale.yaml @@ -16,6 +16,7 @@ concurrency: jobs: stale: + if: github.repository == 'cozystack/cozystack' runs-on: ubuntu-latest permissions: issues: write diff --git a/.github/workflows/tags.yaml b/.github/workflows/tags.yaml index e108e6765..60407da77 100644 --- a/.github/workflows/tags.yaml +++ b/.github/workflows/tags.yaml @@ -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: diff --git a/.github/workflows/ui-test.yaml b/.github/workflows/ui-test.yaml index 12620fddf..f2c58f030 100644 --- a/.github/workflows/ui-test.yaml +++ b/.github/workflows/ui-test.yaml @@ -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: diff --git a/.github/workflows/update-releasenotes.yaml b/.github/workflows/update-releasenotes.yaml index 7669a3d60..583550315 100644 --- a/.github/workflows/update-releasenotes.yaml +++ b/.github/workflows/update-releasenotes.yaml @@ -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 diff --git a/.github/workflows/zizmor.yml b/.github/workflows/zizmor.yml index 6d0ee9b48..581e644ce 100644 --- a/.github/workflows/zizmor.yml +++ b/.github/workflows/zizmor.yml @@ -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: diff --git a/docs/security/incident-response.md b/docs/security/incident-response.md index 156ce4f2f..9d1a1d99e 100644 --- a/docs/security/incident-response.md +++ b/docs/security/incident-response.md @@ -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 diff --git a/hack/stitch-multiarch_test.bats b/hack/stitch-multiarch_test.bats index 2d1232810..3adcebb9b 100644 --- a/hack/stitch-multiarch_test.bats +++ b/hack/stitch-multiarch_test.bats @@ -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' diff --git a/hack/workflow-repo-guard.bats b/hack/workflow-repo-guard.bats new file mode 100644 index 000000000..edfa4f5e4 --- /dev/null +++ b/hack/workflow-repo-guard.bats @@ -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" ] +}