import io import json import os from contextlib import ExitStack, redirect_stdout from pathlib import Path import stat import tempfile from types import SimpleNamespace import unittest from unittest import mock import build_artifact from test_bootstrap import RUNNER class EnrollmentV2Tests(unittest.TestCase): def setUp(self): temp = tempfile.TemporaryDirectory() self.addCleanup(temp.cleanup) self.root = Path(temp.name).resolve() self.app = self.root / "ci" self.pending = self.root / "enrollment.pending.json" self.token = "fixture-not-a-real-token-1234567890" self.pending.write_text(json.dumps({ "repository": "SILVER/NODEDC_DEVICE_CORE", "token": self.token, })) self.pending.chmod(0o666) # Simulate NAS permissions hidden by SMB's 0700 view. self.stack = ExitStack() self.addCleanup(self.stack.close) self.stack.enter_context(mock.patch.object(RUNNER, "GITEA_CI_ROOT", self.app)) self.stack.enter_context(mock.patch.object(RUNNER, "GITEA_CI_ENROLLMENT", self.pending)) self.stack.enter_context(mock.patch.object(RUNNER.pwd, "getpwnam", return_value=SimpleNamespace(pw_uid=os.getuid()))) def emulate_root_ownership(self): fstat = os.fstat def root_stat(fd): original = fstat(fd) fields = {key: getattr(original, key) for key in dir(original) if key.startswith("st_")} fields["st_uid"] = 0 return SimpleNamespace(**fields) self.stack.enter_context(mock.patch.object(RUNNER.os, "fstat", side_effect=root_stat)) chown = self.stack.enter_context(mock.patch.object(RUNNER.os, "chown")) fchown = self.stack.enter_context(mock.patch.object(RUNNER.os, "fchown")) return chown, fchown def test_v1_private_guard_still_rejects_shared_staging(self): with self.assertRaisesRegex(RUNNER.DeployError, "private regular"): RUNNER.read_gitea_ci_enrollment() def test_v2_preflight_reports_metadata_without_mutation_or_secret(self): before = self.pending.stat() result = RUNNER.preflight_gitea_ci_v2_enrollment() self.assertIn("mode=0666", result) self.assertNotIn(self.token, result) self.assertEqual(self.pending.stat(), before) self.assertFalse(self.app.exists()) def test_v2_import_is_private_atomic_and_retryable_without_staging(self): chown, fchown = self.emulate_root_ownership() output = io.StringIO() with redirect_stdout(output): self.assertEqual(RUNNER.import_gitea_ci_v2_enrollment(), self.token) private = self.app / "secrets" / "registration-token" self.assertEqual(private.read_text(), self.token + "\n") self.assertEqual(stat.S_IMODE(private.stat().st_mode), 0o600) self.assertEqual(stat.S_IMODE(private.parent.stat().st_mode), 0o700) self.assertEqual(private.stat().st_nlink, 1) self.assertFalse(self.pending.exists()) self.assertEqual(list(private.parent.iterdir()), [private]) chown.assert_called_with(str(private.parent), 0, 0) self.assertEqual(fchown.call_args.args[1:], (0, 0)) self.assertNotIn(self.token, output.getvalue()) original_inode = private.stat().st_ino self.assertEqual(RUNNER.preflight_gitea_ci_v2_enrollment(), "reuse-root-private-runtime") with redirect_stdout(output): RUNNER.import_gitea_ci_v2_enrollment() self.assertEqual(private.stat().st_ino, original_inode) def test_durability_failure_keeps_staging_for_recovery(self): self.emulate_root_ownership() # First fsync persists file bytes; the second publishes its directory entry. with mock.patch.object(RUNNER.os, "fsync", side_effect=[None, OSError("disk")]): with self.assertRaises(OSError): RUNNER.import_gitea_ci_v2_enrollment() self.assertTrue(self.pending.exists()) self.assertEqual((self.app / "secrets/registration-token").read_text(), self.token + "\n") with redirect_stdout(io.StringIO()): RUNNER.import_gitea_ci_v2_enrollment() self.assertFalse(self.pending.exists()) def test_different_private_identity_is_never_overwritten(self): self.emulate_root_ownership() private = self.app / "secrets/registration-token" private.parent.mkdir(parents=True) private.write_text("different-fixture-token-1234567890\n") private.chmod(0o600) with self.assertRaisesRegex(RUNNER.DeployError, "differs"): RUNNER.import_gitea_ci_v2_enrollment() self.assertTrue(self.pending.exists()) self.assertEqual(private.read_text(), "different-fixture-token-1234567890\n") def test_wrong_operator_owner_rejected(self): with mock.patch.object(RUNNER.pwd, "getpwnam", return_value=SimpleNamespace(pw_uid=os.getuid() + 1)): with self.assertRaisesRegex(RUNNER.DeployError, "metadata rejected"): RUNNER.read_gitea_ci_v2_input() def test_symlink_and_hardlink_rejected(self): actual = self.root / "actual" self.pending.rename(actual) self.pending.symlink_to(actual) with self.assertRaises(RUNNER.DeployError): RUNNER.read_gitea_ci_v2_input() self.pending.unlink() os.link(actual, self.pending) with self.assertRaisesRegex(RUNNER.DeployError, "nlink=2"): RUNNER.read_gitea_ci_v2_input() def test_special_and_oversized_files_rejected_without_blocking(self): self.pending.unlink() os.mkfifo(self.pending) with self.assertRaises(RUNNER.DeployError): RUNNER.read_gitea_ci_v2_input() self.pending.unlink() self.pending.write_text("x" * 2049) with self.assertRaisesRegex(RUNNER.DeployError, "metadata rejected"): RUNNER.read_gitea_ci_v2_input() def test_runtime_copy_requires_root_and_exact_private_mode(self): self.emulate_root_ownership() private = self.app / "secrets/registration-token" private.parent.mkdir(parents=True) private.write_text(self.token) private.chmod(0o644) with self.assertRaisesRegex(RUNNER.DeployError, "runtime=True;mode=0644"): RUNNER.read_gitea_ci_v2_input(runtime=True) def test_wrong_repository_and_malformed_json_never_echo_secret(self): for value in [self.token + "!", json.dumps({"repository": "other", "token": self.token})]: self.pending.write_text(value) with self.assertRaises(RUNNER.DeployError) as error: RUNNER.read_gitea_ci_v2_input() self.assertNotIn(self.token, str(error.exception)) def test_v2_artifact_has_own_identity_and_no_secret_paths(self): patch = "gitea-ci-bootstrap-v2-test-001" data = build_artifact.bundle(patch) self.assertEqual(data, build_artifact.bundle(patch)) artifact = self.root / "v2.tgz" artifact.write_bytes(data) extracted = self.root / "extracted" extracted.mkdir() manifest, entries, payload = RUNNER.load_artifact(artifact, extracted) self.assertEqual(tuple(entries), RUNNER.GITEA_CI_V2_ENTRIES) self.assertEqual(RUNNER.component_services("gitea-ci", entries), ("build-runner",)) with self.assertRaisesRegex(RUNNER.DeployError, "release identity"): RUNNER.validate_gitea_ci_payload(payload, entries, patch.replace("v2", "v1")) with self.assertRaisesRegex(RUNNER.DeployError, "entry set"): RUNNER.validate_gitea_ci_payload(payload, list(entries) + [RUNNER.GITEA_CI_ENTRIES[2]], patch) if __name__ == "__main__": unittest.main()