Skip to content

Commit de1d72b

Browse files
committed
Add check-for-go-fips-test pre-commit hook
Implements GitHub issue requirements: - Detects packages using go-fips in environment or pipeline - Verifies they have corresponding go-fips test (uses: test/go-fips-check) - Reports failures for packages missing the required test
1 parent 70fee29 commit de1d72b

5 files changed

Lines changed: 97 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__pycache__/

.pre-commit-hooks.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,13 @@
2020
- manual
2121
types:
2222
- yaml
23+
- id: check-for-go-fips-test
24+
name: check for go-fips test
25+
description: Check that packages using go-fips have corresponding go-fips tests
26+
entry: check-for-go-fips-test
27+
language: python
28+
stages:
29+
- pre-commit
30+
- manual
31+
types:
32+
- yaml

example.pre-commit-config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ repos:
1818
(?x)^(
1919
[^/]+\.ya?ml # matches .yaml or .yml files at the top level only
2020
)$
21+
- id: check-for-go-fips-test
22+
files: '^[^.][^/]*\.yaml$' # matches non-hidden .yaml files at the top level only
2123
- repo: https://github.com/chainguard-dev/yam
2224
rev: 768695300c5f663012a77911eb4920c12e5ed2e5 # frozen: v0.2.26
2325
hooks:
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
from __future__ import annotations
2+
3+
import argparse
4+
import sys
5+
from collections.abc import Sequence
6+
from typing import Any
7+
8+
import ruamel.yaml
9+
10+
yaml = ruamel.yaml.YAML(typ="safe")
11+
12+
13+
def uses_go_fips(melange_cfg: dict[str, Any]) -> bool:
14+
"""Check if package uses go-fips."""
15+
# Check environment packages
16+
env_packages = melange_cfg.get("environment", {}).get("contents", {}).get("packages", [])
17+
if "go-fips" in env_packages:
18+
return True
19+
20+
# Check pipeline steps for go/build with go-package: go-fips
21+
pipelines = melange_cfg.get("pipeline", [])
22+
for step in pipelines:
23+
if step.get("uses") == "go/build":
24+
if step.get("with", {}).get("go-package") == "go-fips":
25+
return True
26+
27+
# Check subpackage pipelines
28+
for subpkg in melange_cfg.get("subpackages", []):
29+
subpkg_pipelines = subpkg.get("pipeline", [])
30+
for step in subpkg_pipelines:
31+
if step.get("uses") == "go/build":
32+
if step.get("with", {}).get("go-package") == "go-fips":
33+
return True
34+
35+
return False
36+
37+
38+
def has_go_fips_test(melange_cfg: dict[str, Any]) -> bool:
39+
"""Check if package has go-fips test."""
40+
test_section = melange_cfg.get("test", {})
41+
test_pipelines = test_section.get("pipeline", [])
42+
43+
for step in test_pipelines:
44+
if step.get("uses") == "test/go-fips-check":
45+
return True
46+
47+
return False
48+
49+
50+
def main(argv: Sequence[str] | None = None) -> int:
51+
parser = argparse.ArgumentParser(
52+
description="Check that packages using go-fips have corresponding go-fips tests"
53+
)
54+
parser.add_argument("filenames", nargs="*", help="Filenames to check")
55+
args = parser.parse_args(argv)
56+
57+
retval = 0
58+
59+
for filename in args.filenames:
60+
try:
61+
with open(filename) as f:
62+
melange_cfg = yaml.load(f)
63+
except Exception as e:
64+
print(f"Error loading {filename}: {e}")
65+
retval = 1
66+
continue
67+
68+
if not melange_cfg:
69+
continue
70+
71+
if uses_go_fips(melange_cfg):
72+
if not has_go_fips_test(melange_cfg):
73+
print(
74+
f"{filename}: Package uses go-fips but does not have "
75+
"a corresponding go-fips test (add '- uses: test/go-fips-check' to test pipeline)"
76+
)
77+
retval = 1
78+
79+
return retval
80+
81+
82+
if __name__ == "__main__":
83+
sys.exit(main())

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ python_requires = >=3.9
2222
[options.entry_points]
2323
console_scripts =
2424
shellcheck-run-steps = pre_commit_hooks.shellcheck_run_steps:main
25+
check-for-go-fips-test = pre_commit_hooks.check_for_go_fips_test:main
2526

2627
[bdist_wheel]
2728
universal = True

0 commit comments

Comments
 (0)