Files
vpn-dns-gateway/delivery/ci/test_controls.py
T
vsun1 bf28adc7fb
vpn-dns-gateway/pipeline/head This commit looks good
vpn-dns-gateway/pipeline/pr-main This commit looks good
Prepare Assignment 8 secure delivery and rollback
Add the locked pricing service, trusted-agent pipeline, Ansible deployments, security gates, recovery runbooks, prerequisite evidence, and preparation manual.

Prepared with Codex assistance. Vincent explicitly authorized this local commit and will handle the push. Assessed releases and recovery exercises remain pending.
2026-11-14 17:09:25 -05:00

113 lines
4.8 KiB
Python

"""Regression checks for fail-closed release and scan boundaries."""
import importlib.util
import io
import json
import os
from pathlib import Path
import sys
import tempfile
import unittest
from unittest.mock import patch
import release
import security
spec = importlib.util.spec_from_file_location('run_ansible', Path(__file__).with_name('run-ansible.py'))
run_ansible = importlib.util.module_from_spec(spec)
spec.loader.exec_module(run_ansible)
class SecurityGateTests(unittest.TestCase):
def test_empty_dependency_audit_is_not_clean_evidence(self):
with self.assertRaises(ValueError):
security.check_report({"dependencies": []}, "dependencies")
def test_incomplete_or_unrecognized_scan_is_rejected(self):
for report in [{}, {"SchemaVersion": 2, "ArtifactName": "."},
{"SchemaVersion": 3, "ArtifactName": ".", "Results": []}]:
with self.subTest(report=report), self.assertRaises((ValueError, KeyError)):
security.check_report(report, "secrets")
def test_high_findings_and_secrets_block(self):
report = {"SchemaVersion": 2, "ArtifactName": "image", "Results": [{
"Vulnerabilities": [{"Severity": "LOW"}, {"Severity": "HIGH"}, {"Severity": "CRITICAL"}],
"Secrets": [{"Severity": "LOW", "Match": "test-data"}],
}]}
self.assertEqual(len(security.check_report(report, "image")), 2)
self.assertEqual(len(security.check_report(report, "secrets")), 1)
security.redact_secret_matches(report)
self.assertEqual(report["Results"][0]["Secrets"][0]["Match"], "[REDACTED]")
def test_image_without_analyzed_targets_is_rejected(self):
with self.assertRaises(ValueError):
security.check_report({"SchemaVersion": 2, "ArtifactName": "image", "Results": []}, "image")
class ReleaseBoundaryTests(unittest.TestCase):
def setUp(self):
self.record = {
"release_image": "git.yebilly.me/ssw590-group1/pricing@sha256:" + "a" * 64,
"expected_commit": "b" * 40, "configuration_commit": "c" * 40,
"expected_version": "v1", "expected_total": 30,
"proxy_network": "gitea_web", "target_env": "staging",
"public_url": "https://staging.yebilly.me",
}
@patch("release.subprocess.run")
def test_both_commits_must_be_reviewed_main_ancestors(self, run):
release.verify(self.record)
self.assertEqual([call.args[0][-2] for call in run.call_args_list], ["b" * 40, "c" * 40])
self.assertTrue(all(call.kwargs["check"] for call in run.call_args_list))
@patch("release.subprocess.run")
def test_cross_environment_url_is_rejected(self, run):
self.record["public_url"] = "https://app.yebilly.me"
with self.assertRaises(ValueError):
release.verify(self.record)
def test_mutable_tag_or_other_registry_is_rejected(self):
for image in ["git.yebilly.me/ssw590-group1/pricing:latest", "example.com/pricing@sha256:" + "a" * 64]:
self.record["release_image"] = image
with self.assertRaises(ValueError):
release.verify(self.record)
def test_unexpected_ansible_variables_are_rejected(self):
self.record["ansible_host"] = "unapproved-host"
with self.assertRaises(ValueError):
release.verify(self.record)
class UnchangedRerunTests(unittest.TestCase):
def check_recap(self, text, exit_code=0):
original = Path.cwd()
with tempfile.TemporaryDirectory() as directory:
try:
os.chdir(directory)
Path('release.json').write_text(json.dumps({'target_env': 'staging'}))
with patch('sys.argv', ['run-ansible.py', 'release.json', 'rerun', 'playbook.yml']), \
patch.object(run_ansible.subprocess, 'Popen') as launch, \
patch('sys.stdout', new_callable=io.StringIO), \
patch('sys.stderr', new_callable=io.StringIO):
launch.return_value.stdout = io.StringIO(text)
launch.return_value.wait.return_value = exit_code
return run_ansible.main()
finally:
os.chdir(original)
def test_unchanged_success_passes(self):
self.assertEqual(self.check_recap('startup : ok=9 changed=0 unreachable=0 failed=0 skipped=0\n'), 0)
def test_success_with_unnecessary_changes_blocks(self):
self.assertEqual(self.check_recap('startup : ok=9 changed=1 unreachable=0 failed=0 skipped=0\n'), 1)
def test_missing_recap_is_not_a_success(self):
self.assertEqual(self.check_recap('No deployment results\n'), 1)
def test_ansible_error_blocks_even_with_good_looking_recap(self):
self.assertEqual(self.check_recap('startup : ok=9 changed=0 unreachable=0 failed=0\n', 1), 1)
if __name__ == "__main__":
unittest.main()