Files
nvke/hack/test_name_check.py
Alex Pelavin b594eaf4b7 ci: deprecate GitHub releases for subcomponents (#2112)
* ci: stop creating GitHub releases and remove the changelog

Publishing to GHCR, NVCR, S3 and the apt intake is unchanged. release.yml
now only builds nvkectl for the cli publishers. The agent, NKTP and os-image
release-page jobs, their checksums and attestations, verify-release-assets,
malware-backfill.yml, CHANGELOG.md and cliff.toml are removed, and the docs
no longer point at release downloads.

Signed-off-by: Alex Pelavin <apelavin@nvidia.com>

* ci: remove what the GitHub release removal orphaned

ORAS_SHA256_AMD64 was read only by malware-backfill.yml, and os-image's
agent_version step output only fed the release body. Two comments still
described release-page behavior.

Signed-off-by: Alex Pelavin <apelavin@nvidia.com>

* ci: rename release.yml to tag-publish.yml

It no longer creates releases. It gates every tag on the license checks,
builds nvkectl for the cli publishers and records which workflow
publishes each component tag.

Signed-off-by: Alex Pelavin <apelavin@nvidia.com>

* ci: drop the CHANGELOG.md exclusion from removed-settings-check

Signed-off-by: Alex Pelavin <apelavin@nvidia.com>

#2114 added the check while CHANGELOG.md still existed. It no longer does.

* docs: correct publication destinations, fork-tag behavior and stale download notes

- os-image tags put only the qcow2s on NVCR; the SBOMs, checksums and the
  checker go to S3.
- A fork tag publishes nothing: every publisher checks the repository ID.
- The custom-base-os cloud-init example does not install nvkectl.
- SECURITY.md and plan-installation.md still described GitHub release
  checksums and release pages.

Signed-off-by: Alex Pelavin <apelavin@nvidia.com>

---------

Signed-off-by: Alex Pelavin <apelavin@nvidia.com>
2026-11-05 09:02:56 -08:00

130 lines
6.5 KiB
Python

# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
"""Exercise the banned-name scan against isolated repository fixtures."""
import os
from pathlib import Path
import shutil
import subprocess
import tempfile
import unittest
SCRIPT = Path(__file__).with_name("name-check.py")
# A hook can export GIT_DIR and friends, which would point git at the outer
# repository instead of the fixture.
ENV = {k: v for k, v in os.environ.items() if not k.startswith("GIT_")}
CLEAN = "linked symlinked chunked blanket Winkernel encryptionKey beginKeywords nats-io/nkeys h1:9Q3nKEdv4abcdef+/=\n"
class NameCheckTest(unittest.TestCase):
def run_check(self, files, allowlist="", mode="fail", extra=()):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
subprocess.run(["git", "init", "--quiet", str(root)], check=True, capture_output=True, env=ENV)
(root / "hack").mkdir()
shutil.copyfile(SCRIPT, root / "hack" / SCRIPT.name)
(root / "hack" / "name-check-allowlist.txt").write_text(allowlist, encoding="utf-8")
for name, content in files.items():
path = root / name
path.parent.mkdir(parents=True, exist_ok=True)
if isinstance(content, bytes):
path.write_bytes(content)
else:
path.write_text(content, encoding="utf-8")
subprocess.run(["git", "-C", str(root), "add", "-A"], check=True, capture_output=True, env=ENV)
return subprocess.run(
["python3", "-B", "hack/name-check.py", "--mode", mode, *extra],
cwd=root, capture_output=True, text=True, check=False, env=ENV,
)
def test_product_token_fails(self):
result = self.run_check({"fixture.txt": "apt-get install nke-agent\n"})
self.assertEqual(result.returncode, 1, result.stdout + result.stderr)
self.assertIn("fixture.txt:1: nke-agent", result.stdout)
self.assertIn("1 hits outside the allowlist", result.stderr)
def test_english_words_camel_case_nkeys_and_base64_pass(self):
result = self.run_check({"fixture.txt": CLEAN})
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertIn("name-check: clean", result.stdout)
def test_product_shapes_are_kept(self):
for token in ("NKECluster", "nkectl", "Nkectl", "nke.nvidia.com/reboot", "/etc/nke", "NKE_AGENT", "nkeRBACPrefix", "urn:nvidia:nke:controller"):
with self.subTest(token=token):
result = self.run_check({"fixture.txt": f"x {token} y\n"})
self.assertEqual(result.returncode, 1, result.stdout + result.stderr)
self.assertIn(token, result.stdout)
def test_report_mode_exits_zero_with_hits(self):
result = self.run_check({"fixture.txt": "nke-agent nke-agent\nnkectl\n"}, mode="report")
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertIn("hits: 3 in 1 files", result.stdout)
self.assertIn("distinct tokens: 2", result.stdout)
self.assertIn("2 nke-agent", result.stdout)
self.assertIn("report only, exit 0", result.stdout)
def test_allowlisted_token_passes(self):
result = self.run_check({"fixture.txt": 'magic := "NKEAuth"\n'}, allowlist="token:^NKEAuth$\n")
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertIn("1 allowed by the allowlist", result.stdout)
def test_allowlisted_path_passes_but_other_hits_still_fail(self):
files = {"fixtures/a.txt": "nke-agent\n", "fixtures/nke-b.txt": "clean\n"}
result = self.run_check(files, allowlist="path:fixtures/*\n")
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
files["other.txt"] = "nke-agent\n"
result = self.run_check(files, allowlist="path:fixtures/*\n")
self.assertEqual(result.returncode, 1, result.stdout + result.stderr)
self.assertIn("other.txt:1: nke-agent", result.stdout)
self.assertNotIn("fixtures/", result.stdout.split("hits:", 1)[1].split("\n", 1)[1])
def test_dead_allowlist_entry_fails_in_both_modes(self):
for mode in ("fail", "report"):
with self.subTest(mode=mode):
result = self.run_check({"fixture.txt": "clean\n"}, allowlist="token:^never-nke-matches$\n", mode=mode)
self.assertEqual(result.returncode, 1, result.stdout + result.stderr)
self.assertIn("DEAD", result.stderr)
self.assertIn("token:^never-nke-matches$", result.stderr)
def test_malformed_allowlist_is_a_usage_error(self):
result = self.run_check({"fixture.txt": "clean\n"}, allowlist="nke-agent\n")
self.assertEqual(result.returncode, 2, result.stdout + result.stderr)
self.assertIn("expected 'path:<glob>' or 'token:<regex>'", result.stderr)
def test_excluded_paths_are_not_scanned(self):
files = {
"go.sum": "github.com/x/nke-agent v1 h1:abc=\n",
"vendor/x.txt": "nke-agent\n",
"a/vendor/y.txt": "nke-agent\n",
"a/generated/z.txt": "nke-agent\n",
"poetry.lock": "nke-agent\n",
}
result = self.run_check(files)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
# The five fixtures plus the check's own script and allowlist, which the
# fixture repository tracks too.
self.assertIn(f"{len(files) + 2} excluded by path", result.stdout)
def test_path_carrying_the_name_is_a_hit(self):
result = self.run_check({"nke-thing/readme.txt": "clean\n"})
self.assertEqual(result.returncode, 1, result.stdout + result.stderr)
self.assertIn("nke-thing/readme.txt (path): nke-thing", result.stdout)
self.assertIn("1 paths carry the name", result.stdout)
def test_binary_files_are_skipped(self):
result = self.run_check({"blob.bin": b"\x00\x01nke-agent\x00"})
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertIn("1 binary skipped", result.stdout)
def test_name_is_a_parameter(self):
result = self.run_check({"fixture.txt": "foo-bar nke-agent\n"}, extra=("--name", "foo"))
self.assertEqual(result.returncode, 1, result.stdout + result.stderr)
self.assertIn("fixture.txt:1: foo-bar", result.stdout)
self.assertNotIn("nke-agent", result.stdout)
if __name__ == "__main__":
unittest.main()