From 3cf24c452066aca0a38655773cedb94fef605cae Mon Sep 17 00:00:00 2001 From: EurusDevSec Date: Tue, 24 Nov 2026 22:06:02 +0700 Subject: [PATCH] ci(p5): run kyverno test in static-gates with a self-test (DRF-67) New job `kyverno` runs gitops/tests/kyverno_test.sh: the L1 cases in gitops/tests/kyverno/ inside the pinned kyverno-cli v1.20.0 image (by digest, no network, repo read-only). It passes only on "N passed and 0 failed" with N >= 1, then flips one expectation in a copy and requires that run to fail. Local: 14/14, self-test 11 passed 3 failed. SPEC-P5 P5-T2.2 note and the Kyverno runbook appendix updated. Co-Authored-By: Claude Opus 5.5 --- .agent/specs/SPEC-P5_TENANT_GUARDRAILS.md | 3 +- .github/workflows/static-gates.yml | 15 +++++++- docs/runbooks/P5_L2_KIND_KYVERNO.md | 16 ++++++++ gitops/tests/kyverno_test.sh | 47 +++++++++++++++++++++++ 4 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 gitops/tests/kyverno_test.sh diff --git a/.agent/specs/SPEC-P5_TENANT_GUARDRAILS.md b/.agent/specs/SPEC-P5_TENANT_GUARDRAILS.md index f72238a..b311216 100644 --- a/.agent/specs/SPEC-P5_TENANT_GUARDRAILS.md +++ b/.agent/specs/SPEC-P5_TENANT_GUARDRAILS.md @@ -310,7 +310,8 @@ Evidence: `docs/evidence/p5/P5-T2.1_L2_KIND_BASELINE_.md`. - [x] Webhook scope and `failurePolicy` per D8; verified in the rendered `ValidatingWebhookConfiguration`, not only in values. (24/11: `gitops/platform/apps/45-kyverno.yaml`, `47-kyverno-policies.yaml`, - `gitops/platform/policies/`; `kyverno test` 14/14; `check.sh` rule 17 with + `gitops/platform/policies/`; `kyverno test` 14/14, in CI since 24/11 (job + `kyverno` of `static-gates`, `gitops/tests/kyverno_test.sh`); `check.sh` rule 17 with guards; on kind the rendered webhook `vpol.validate.kyverno.svc-fail` is `Fail` and selects only namespaces with the tenant label. Findings F1–F6 in §10.) diff --git a/.github/workflows/static-gates.yml b/.github/workflows/static-gates.yml index 17b2190..7e97807 100644 --- a/.github/workflows/static-gates.yml +++ b/.github/workflows/static-gates.yml @@ -1,5 +1,6 @@ # Static gates on pull requests (SPEC-P4 P4-T3.2 B, D12; ver3 §9.1 step 2). -# gitops/check.sh with its guard tests, and the Terraform static gate +# gitops/check.sh with its guard tests, kyverno test for the Kyverno policies, +# and the Terraform static gate # (fmt, validate, mocked contract tests, plan_assert guards, pins, leak scan). # No cloud or cluster credential: no secret is referenced, the token is read-only, # Terraform runs with -backend=false and mock providers only. @@ -47,6 +48,18 @@ jobs: - name: Guard tests (each rule must be able to fail) run: bash gitops/tests/check_guards.sh + kyverno: + # L1 for the Kyverno policies (SPEC-P5 P5-T2.2 A): kyverno test in the pinned + # kyverno-cli image by digest, no network in the container, plus a self-test. + runs-on: ubuntu-24.04 + timeout-minutes: 10 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - name: kyverno test (kyverno-cli v1.20.0 by digest) + run: bash gitops/tests/kyverno_test.sh + terraform: runs-on: ubuntu-24.04 timeout-minutes: 15 diff --git a/docs/runbooks/P5_L2_KIND_KYVERNO.md b/docs/runbooks/P5_L2_KIND_KYVERNO.md index 8594005..04b715a 100644 --- a/docs/runbooks/P5_L2_KIND_KYVERNO.md +++ b/docs/runbooks/P5_L2_KIND_KYVERNO.md @@ -230,3 +230,19 @@ kind delete cluster --name zdr-p5-kyv; rm -rf "$HOME/zdr-p5-kyv"; unset K POD LA | 4 bỏ bước scale thì sao | | | | Sau lab, trả lời một câu: **vì sao `noreq` không bị Kyverno chặn, và khi nào `zdr-require-resources` mới có tác dụng?** + +## Phụ lục: L1 `kyverno test` (không cần cluster) + +Đây là kiểm tra tĩnh, chạy trước lab: mỗi luật có một Pod phải qua và một Pod phải bị chặn +(`gitops/tests/kyverno/`). Từ 24/11, CI `static-gates` (job `kyverno`) chạy lệnh này trên mọi PR đụng `gitops/`. +Ví von: diễn tập bảo vệ bằng hàng mẫu trước khi mở cửa toà nhà. + +Chạy lại trên máy (cần Docker đang chạy, tải image `kyverno-cli` v1.20.0 theo digest, không cần AWS): + +```bash +bash gitops/tests/kyverno_test.sh +``` + +Phải thấy: `Test Summary: 14 tests passed and 0 tests failed`, rồi dòng self-test +`… tests failed` khác 0 (một kỳ vọng bị cố ý lật trong bản sao), cuối cùng `PASS kyverno test and self-test`. +Không có gì để dọn: container tự xoá (`--rm`), thư mục tạm tự xoá khi script thoát. diff --git a/gitops/tests/kyverno_test.sh b/gitops/tests/kyverno_test.sh new file mode 100644 index 0000000..58290f2 --- /dev/null +++ b/gitops/tests/kyverno_test.sh @@ -0,0 +1,47 @@ +#!/usr/bin/env bash +# L1 gate (SPEC-P5 P5-T2.2 A, L1 in §8): run the kyverno test cases in +# gitops/tests/kyverno/ with the pinned kyverno-cli v1.20.0 image, the same +# digest the chart uses (docs/evidence/p5/P5-T1_PINS_20261122.md). +# Then a self-test: one expected result is flipped in a copy, and the run must +# fail, so a CLI that passes everything cannot turn this gate green. +# Needs Docker. No network inside the container, the repo is mounted read-only. +# Usage: bash gitops/tests/kyverno_test.sh +set -euo pipefail + +IMG="reg.kyverno.io/kyverno/kyverno-cli@sha256:bd2b8b7c9d550ecf04e0bacaf351adce2f82ce67fd3f291075edcee7f8ab4ab4" +cd "$(dirname "$0")/.." # gitops/ + +# run DIR: prints the CLI summary, returns 0 only on "N passed and 0 failed", N >= 1. +run() { + local src out rc summary + src="$(cd "$1" && { pwd -W 2>/dev/null || pwd; })" # Git Bash needs the Windows path for -v + out="$(MSYS_NO_PATHCONV=1 docker run --rm --network none -v "$src:/g:ro" -w /g/tests/kyverno "$IMG" test . 2>&1)" && rc=0 || rc=$? + summary="$(printf '%s\n' "$out" | grep -E '^Test Summary: ' | tail -n 1 || true)" + echo "exit $rc: ${summary:-no summary}" + if [[ $rc -eq 0 && "$summary" =~ ^Test\ Summary:\ [1-9][0-9]*\ tests\ passed\ and\ 0\ tests\ failed$ ]]; then + return 0 + fi + printf '%s\n' "$out" | tail -n 40 + return 1 +} + +echo "== kyverno test (gitops/tests/kyverno)" +run . || { echo "FAIL kyverno test"; exit 1; } + +echo "== self-test: one flipped expectation must fail" +tmp="$(mktemp -d)" +trap 'rm -rf "$tmp"' EXIT +mkdir -p "$tmp/platform" "$tmp/tests" +cp -R platform/policies "$tmp/platform/" +cp -R tests/kyverno "$tmp/tests/" +f="$tmp/tests/kyverno/kyverno-test.yaml" +n="$(grep -c '^ result: pass$' "$f" || true)" +[[ "$n" -ge 1 ]] || { echo "FAIL self-test: no 'result: pass' to flip"; exit 1; } +sed -i '0,/^ result: pass$/s// result: fail/' "$f" +r=0; s="$(run "$tmp")" || r=$? +echo "${s%%$'\n'*}" +if [[ $r -eq 0 ]]; then + echo "FAIL self-test: flipped expectation still passed" + exit 1 +fi +echo "PASS kyverno test and self-test"