From 09de44df0883e907f751afa67ad178c54c67c3e6 Mon Sep 17 00:00:00 2001 From: overtrue Date: Sat, 22 Aug 2026 20:43:36 +0800 Subject: [PATCH] ci(coverage): add security ratchet calibration --- .config/coverage-baselines.toml | 20 ++++ .config/make/tests.mak | 1 + .github/workflows/coverage.yml | 31 +++-- docs/testing/README.md | 15 ++- scripts/check_security_coverage.py | 174 +++++++++++++++++++++++++++++ scripts/coverage_per_crate.py | 33 +++--- 6 files changed, 246 insertions(+), 28 deletions(-) create mode 100644 .config/coverage-baselines.toml create mode 100644 scripts/check_security_coverage.py diff --git a/.config/coverage-baselines.toml b/.config/coverage-baselines.toml new file mode 100644 index 000000000..77409d9d2 --- /dev/null +++ b/.config/coverage-baselines.toml @@ -0,0 +1,20 @@ +# Report-only calibration baseline from https://github.com/rustfs/rustfs/actions/runs/29394996173. +# Update counts only with a linked coverage run and a reviewed explanation. +phase = "report-only" +allowed_drop_percentage_points = 1.0 + +[crates."crates/iam"] +covered = 5149 +count = 8131 + +[crates."crates/kms"] +covered = 2950 +count = 4200 + +[crates."crates/policy"] +covered = 4636 +count = 5464 + +[crates."crates/crypto"] +covered = 469 +count = 494 diff --git a/.config/make/tests.mak b/.config/make/tests.mak index 1dec7db3e..ab009713d 100644 --- a/.config/make/tests.mak +++ b/.config/make/tests.mak @@ -36,6 +36,7 @@ script-tests: ## Run shell script tests ./scripts/test_manual_transition_runbooks.sh ./scripts/check_embedded_secrets.sh --self-test python3 ./scripts/check_test_wiring.py --self-test + python3 ./scripts/check_security_coverage.py --self-test python3 ./scripts/s3-tests/test_report_compat.py bash -n ./scripts/validate_object_data_cache_cold_stampede.sh python3 ./scripts/check_object_data_cache_follower_samples.py --self-test diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index baa3c87a7..c869bfdf1 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -12,14 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -# Weekly workspace line-coverage baseline (backlog#1153 infra-5). +# Workspace line-coverage baseline and security-crate calibration +# (backlog#1153 infra-5/infra-6). # -# NON-BLOCKING by design: this workflow only runs on schedule and manual -# dispatch, so it never attaches a status to a PR and must never be made a -# required check. It exists to give coverage a visible baseline and trend -# (per-crate table in the job summary, lcov artifact kept 90 days) — the -# per-crate ratchet for the security-critical crates builds on it later -# (backlog#1153 infra-6, report-only first per the ci-11 ladder). +# NON-BLOCKING by design: the weekly job gives coverage a visible baseline and +# trend, while relevant pull requests run a report-only security-crate +# comparison. Neither job is a required check during calibration. # # Measurement scope matches the PR test gate (ci.yml "Run tests"): # `--workspace --exclude e2e_test` with the `ci` nextest profile. Doctests are @@ -31,6 +29,17 @@ name: coverage on: + pull_request: + branches: [main] + paths: + - "crates/iam/**" + - "crates/kms/**" + - "crates/policy/**" + - "crates/crypto/**" + - ".config/coverage-baselines.toml" + - "scripts/coverage_per_crate.py" + - "scripts/check_security_coverage.py" + - ".github/workflows/coverage.yml" workflow_dispatch: schedule: # 07:00 UTC Sunday — staggered clear of the other Sunday crons: ci (00:00), @@ -46,10 +55,10 @@ permissions: jobs: coverage: - name: Workspace coverage (weekly) + name: Workspace line coverage runs-on: sm-standard-4 # The instrumented build cannot reuse the regular CI cache (different - # RUSTFLAGS), so a cold week rebuilds the workspace before running the + # RUSTFLAGS), so a cold run rebuilds the workspace before running the # full suite; give it double the test job's 60-minute budget. timeout-minutes: 120 env: @@ -91,7 +100,9 @@ jobs: cargo llvm-cov report --json --output-path target/llvm-cov/coverage.json - name: Write per-crate summary - run: python3 scripts/coverage_per_crate.py target/llvm-cov/coverage.json >> "$GITHUB_STEP_SUMMARY" + run: | + python3 scripts/coverage_per_crate.py target/llvm-cov/coverage.json >> "$GITHUB_STEP_SUMMARY" + python3 scripts/check_security_coverage.py target/llvm-cov/coverage.json >> "$GITHUB_STEP_SUMMARY" - name: Upload coverage artifact if: always() diff --git a/docs/testing/README.md b/docs/testing/README.md index e38c0d1a2..702c2c4c9 100644 --- a/docs/testing/README.md +++ b/docs/testing/README.md @@ -158,10 +158,11 @@ added by backlog#1153 infra-4. ## Coverage -Line coverage is measured **weekly, not per-PR**, and is non-blocking: it -exists for visibility and trend, never as a required check. Per-crate ratchets -for the security-critical crates (iam / kms / policy / crypto) build on this -baseline later (backlog#1153 infra-6, report-only first). +Workspace line coverage is measured weekly. Pull requests that touch iam, kms, +policy, or crypto also run a non-required, report-only comparison against +`.config/coverage-baselines.toml`. During calibration, a regression is recorded +in the job summary without failing the job; missing or malformed coverage +evidence still fails closed (backlog#1153 infra-6). - **CI**: `.github/workflows/coverage.yml` runs every Sunday and on manual dispatch: `cargo llvm-cov nextest --workspace --exclude e2e_test` under the @@ -174,6 +175,12 @@ baseline later (backlog#1153 infra-6, report-only first). plus the full suite). It prints the same per-crate table via `scripts/coverage_per_crate.py` and writes `target/llvm-cov/lcov.info` and `coverage.json`. +- **Security-critical ratchet**: relevant pull requests compare iam / kms / + policy / crypto line coverage with the versioned baseline. Drops greater than + the configured one-percentage-point calibration threshold are marked + `REGRESSION (report-only)`. The weekly summary runs the same comparison so + calibration continues even when no relevant pull request is open. Baseline + changes require a linked coverage run and a reviewed explanation. - **Trend comparison**: each run's job summary is the weekly per-crate snapshot — open two runs from the Actions history (workflow "coverage") and compare their tables. For line-level diffs, download the two runs' diff --git a/scripts/check_security_coverage.py b/scripts/check_security_coverage.py new file mode 100644 index 000000000..633dde0e7 --- /dev/null +++ b/scripts/check_security_coverage.py @@ -0,0 +1,174 @@ +#!/usr/bin/env python3 +# Copyright 2024 RustFS Team +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Compare security-critical crate line coverage with the report-only baseline.""" + +import argparse +import json +import os +import sys +import tempfile +import tomllib +from pathlib import Path + +from coverage_per_crate import fmt_pct, load_coverage + + +def load_baselines(path: str) -> tuple[float, dict[str, tuple[int, int]]]: + with open(path, "rb") as fh: + config = tomllib.load(fh) + + if config.get("phase") != "report-only": + raise ValueError("coverage baseline phase must be report-only") + + allowed_drop = float(config["allowed_drop_percentage_points"]) + if allowed_drop < 0: + raise ValueError("allowed_drop_percentage_points must be non-negative") + + baselines: dict[str, tuple[int, int]] = {} + for crate, values in config["crates"].items(): + covered = int(values["covered"]) + count = int(values["count"]) + if covered < 0 or count <= 0 or covered > count: + raise ValueError(f"invalid baseline for {crate}: {covered}/{count}") + baselines[crate] = (covered, count) + if not baselines: + raise ValueError("coverage baseline has no crates") + return allowed_drop, baselines + + +def compare( + current: dict[str, list[int]], + baselines: dict[str, tuple[int, int]], + allowed_drop: float, +) -> list[tuple[str, int, int, int, int, float, bool]]: + rows = [] + for crate, (baseline_covered, baseline_count) in baselines.items(): + if crate not in current: + raise ValueError(f"coverage report is missing {crate}") + covered, count = current[crate] + if covered < 0 or count <= 0 or covered > count: + raise ValueError(f"invalid coverage for {crate}: {covered}/{count}") + current_pct = 100.0 * covered / count + baseline_pct = 100.0 * baseline_covered / baseline_count + delta = current_pct - baseline_pct + rows.append((crate, covered, count, baseline_covered, baseline_count, delta, delta < -allowed_drop)) + return rows + + +def print_report(rows: list[tuple[str, int, int, int, int, float, bool]], allowed_drop: float) -> None: + print("## Security-critical coverage ratchet (report-only)") + print() + print(f"Calibration threshold: a drop greater than {allowed_drop:.2f} percentage points is reported as a regression.") + print() + print("| Crate | Current | Baseline | Delta | Status |") + print("|---|---:|---:|---:|---|") + for crate, covered, count, baseline_covered, baseline_count, delta, regressed in rows: + status = "REGRESSION (report-only)" if regressed else "OK" + print( + f"| `{crate}` | {fmt_pct(covered, count)} ({covered}/{count}) " + f"| {fmt_pct(baseline_covered, baseline_count)} ({baseline_covered}/{baseline_count}) " + f"| {delta:+.2f} pp | {status} |" + ) + print() + print("This calibration phase records regressions without failing the job; malformed or incomplete evidence still fails closed.") + + +def self_test() -> None: + with tempfile.TemporaryDirectory() as tmp: + root = Path(tmp) + coverage = root / "coverage.json" + baseline = root / "baseline.toml" + coverage.write_text( + json.dumps( + { + "data": [ + { + "files": [ + { + "filename": str(root / "crates/iam/src/lib.rs"), + "summary": {"lines": {"covered": 80, "count": 100}}, + }, + { + "filename": str(root / "crates/kms/src/lib.rs"), + "summary": {"lines": {"covered": 90, "count": 100}}, + }, + ], + "totals": {"lines": {"covered": 170, "count": 200}}, + } + ] + } + ), + encoding="utf-8", + ) + baseline.write_text( + """phase = "report-only" +allowed_drop_percentage_points = 1.0 +[crates."crates/iam"] +covered = 90 +count = 100 +[crates."crates/kms"] +covered = 85 +count = 100 +""", + encoding="utf-8", + ) + current, _ = load_coverage(str(coverage), str(root)) + allowed_drop, baselines = load_baselines(str(baseline)) + rows = compare(current, baselines, allowed_drop) + assert [row[-1] for row in rows] == [True, False] + try: + compare({"crates/iam": current["crates/iam"]}, baselines, allowed_drop) + except ValueError as error: + assert str(error) == "coverage report is missing crates/kms" + else: + raise AssertionError("missing crate must fail closed") + try: + compare({**current, "crates/iam": [101, 100]}, baselines, allowed_drop) + except ValueError as error: + assert str(error) == "invalid coverage for crates/iam: 101/100" + else: + raise AssertionError("invalid coverage must fail closed") + + +def main() -> int: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("coverage_json", nargs="?") + parser.add_argument("--baseline", default=".config/coverage-baselines.toml") + parser.add_argument("--repo-root", default=os.getcwd()) + parser.add_argument("--self-test", action="store_true") + args = parser.parse_args() + + if args.self_test: + self_test() + print("security coverage self-test passed") + return 0 + if not args.coverage_json: + parser.error("coverage_json is required unless --self-test is used") + + try: + current, _ = load_coverage(args.coverage_json, os.path.abspath(args.repo_root)) + allowed_drop, baselines = load_baselines(args.baseline) + rows = compare(current, baselines, allowed_drop) + except (OSError, ValueError, KeyError, IndexError, json.JSONDecodeError, tomllib.TOMLDecodeError) as error: + print(f"error: {error}", file=sys.stderr) + return 1 + + print_report(rows, allowed_drop) + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/scripts/coverage_per_crate.py b/scripts/coverage_per_crate.py index 6c6622022..5b37d180a 100755 --- a/scripts/coverage_per_crate.py +++ b/scripts/coverage_per_crate.py @@ -47,23 +47,13 @@ def fmt_pct(covered: int, count: int) -> str: return f"{100.0 * covered / count:.2f}%" if count else "—" -def main() -> int: - if len(sys.argv) < 2 or len(sys.argv) > 3: - print(__doc__.strip(), file=sys.stderr) - return 2 - path = sys.argv[1] - root = os.path.abspath(sys.argv[2] if len(sys.argv) == 3 else os.getcwd()) - +def load_coverage(path: str, root: str) -> tuple[dict[str, list[int]], dict[str, int]]: with open(path, encoding="utf-8") as fh: export = json.load(fh) - try: - data = export["data"][0] - files = data["files"] - totals = data["totals"]["lines"] - except (KeyError, IndexError) as exc: - print(f"error: unexpected llvm-cov JSON shape ({exc})", file=sys.stderr) - return 1 + data = export["data"][0] + files = data["files"] + totals = data["totals"]["lines"] crates: dict[str, list[int]] = {} for f in files: @@ -71,6 +61,21 @@ def main() -> int: acc = crates.setdefault(crate_label(f["filename"], root), [0, 0]) acc[0] += lines["covered"] acc[1] += lines["count"] + return crates, totals + + +def main() -> int: + if len(sys.argv) < 2 or len(sys.argv) > 3: + print(__doc__.strip(), file=sys.stderr) + return 2 + path = sys.argv[1] + root = os.path.abspath(sys.argv[2] if len(sys.argv) == 3 else os.getcwd()) + + try: + crates, totals = load_coverage(path, root) + except (KeyError, IndexError) as exc: + print(f"error: unexpected llvm-cov JSON shape ({exc})", file=sys.stderr) + return 1 rows = sorted( crates.items(),