ci(workflows): run push and schedule workflows only in cozystack/cozystack

A private copy of the repository, used to verify an embargoed security
fix because GitHub runs no CI in an advisory's temporary fork, receives
every workflow file with each sync. Without a repository guard, each
sync and each cron tick there would build, publish, prune and cut
releases with that copy's credentials. Pull request events stay allowed
in the workflows that also check pull requests, since checking the fix
is what the copy is for.

Every job carries the guard rather than only the root jobs, because a
job whose condition calls a status function runs even when the job it
needs was skipped. A contract test holds new push and schedule
workflows to the same rule. It reads 'on' in every form GitHub accepts,
requires whatever follows the guard to be one parenthesized group,
because && binds tighter than || and a trailing '|| (...)' would reopen
the job, and fails on a workflow it cannot parse instead of leaving
that workflow unchecked.

Signed-off-by: Alexey Artamonov <aleksei.artamonov@aenix.io>
Assisted-by: LLM
This commit is contained in:
Alexey Artamonov committed 2026-11-25 15:49:33 +03:00
1 parent 86ad3870d1
commit d6a4835ea5
19 files changed
+231 -15

No files matched your search

+5 -5
View File
@@ -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.
+1
View File
@@ -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
+1
View File
@@ -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
+1
View File
@@ -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
+1
View File
@@ -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.
+1 -1
View File
@@ -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:
+1
View File
@@ -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.
+2 -1
View File
@@ -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
+3 -1
View File
@@ -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
+1
View File
@@ -57,6 +57,7 @@ concurrency:
jobs:
prune:
if: github.repository == 'cozystack/cozystack'
name: Prune old GHCR temporary artifacts
runs-on: ubuntu-latest
permissions:
+1
View File
@@ -12,6 +12,7 @@ permissions: read-all
jobs:
analysis:
if: github.repository == 'cozystack/cozystack'
name: Scorecard analysis
runs-on: ubuntu-latest
permissions:
+1
View File
@@ -16,6 +16,7 @@ concurrency:
jobs:
stale:
if: github.repository == 'cozystack/cozystack'
runs-on: ubuntu-latest
permissions:
issues: write
+6 -5
View File
@@ -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:
+1
View File
@@ -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
+1
View File
@@ -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:
+2 -2
View File
@@ -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'
+200
View File
@@ -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" ]
}