Files
aevolution/tools/ci/deploy-policy-check.py
T
dingoandClaude Opus 5.5 df0c4f4efb Add M8.2 environment isolation and World0 protection
Per-environment platform layers (dev, stage, world0) with restricted pod
security, quotas, limit ranges, default-deny networking and token-less
service accounts, applied by an administrator rather than Argo. A native
ValidatingAdmissionPolicy denies deleting World0 data, identity and Argo
objects to every requester without the break-glass group. Stage and
World0 Argo projects are scoped to one namespace, World0 blocks
automated sync, and a cleanup tool refuses World0. An offline policy
check runs as a new CI suite; a live verification script uses only
can-i, reads and server dry runs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 12:43:58 -04:00

169 lines
9.3 KiB
Python

#!/usr/bin/env python3
"""M8.2 offline deployment-policy check (ADR-0069). Needs Python 3 and PyYAML only.
Parses every manifest under deploy/ (including .yaml.example files) and fails if an
environment-isolation or World0-protection invariant is broken:
- no Secret objects in Git;
- every ServiceAccount disables token automounting;
- each platform overlay: labelled restricted namespace, quota, limit range,
default-deny and DNS-only policies, three service accounts; World0 alone is
labelled protected;
- the World0 admission policy denies what ADR-0069 lists, fails closed and is
bound with Deny;
- every AEvolution AppProject targets exactly its own namespace with no cluster
kinds; World0 blocks automated sync; no Application auto-syncs or carries the
cascade-delete finalizer, and each stays in its own environment;
- the live Argo path (deploy/kubernetes/dev) holds no Namespace, Secret, quota or
RBAC objects;
- the cleanup tool refuses World0 before invoking kubectl.
"""
import os
import shutil
import subprocess
import sys
import tempfile
import yaml
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
failures = []
def check(condition, message):
if not condition:
failures.append(message)
def documents(path):
with open(path, encoding='utf-8') as f:
return [d for d in yaml.safe_load_all(f) if isinstance(d, dict)]
manifests = {}
for base, _, files in os.walk(os.path.join(ROOT, 'deploy')):
for name in files:
if name.endswith(('.yaml', '.yml', '.yaml.example')):
path = os.path.join(base, name)
manifests[os.path.relpath(path, ROOT).replace(os.sep, '/')] = documents(path)
objects = [(p, d) for p, ds in manifests.items() for d in ds
if 'kind' in d and d['kind'] != 'Kustomization']
# Secrets and service-account tokens.
for path, doc in objects:
check(doc['kind'] != 'Secret', f'{path}: Secret objects never belong in Git')
if doc['kind'] == 'ServiceAccount':
check(doc.get('automountServiceAccountToken') is False,
f'{path}: ServiceAccount {doc["metadata"]["name"]} must not automount a token')
# Platform overlays.
platform = 'deploy/kubernetes/platform'
base = {d['kind'] + '/' + d['metadata']['name']: d
for p, d in objects if p.startswith(platform + '/base/')}
for key in ('ResourceQuota/aevolution-quota', 'LimitRange/aevolution-defaults',
'NetworkPolicy/aevolution-default-deny', 'NetworkPolicy/aevolution-allow-dns',
'ServiceAccount/aevolution-migrator', 'ServiceAccount/aevolution-region',
'ServiceAccount/aevolution-backup'):
check(key in base, f'{platform}/base: missing {key}')
deny = base.get('NetworkPolicy/aevolution-default-deny', {}).get('spec', {})
check(deny.get('podSelector') == {} and sorted(deny.get('policyTypes', [])) == ['Egress', 'Ingress']
and not deny.get('ingress') and not deny.get('egress'),
'default-deny must select every pod and allow nothing')
dns = base.get('NetworkPolicy/aevolution-allow-dns', {}).get('spec', {})
dns_ports = [p.get('port') for rule in dns.get('egress', []) for p in rule.get('ports', [])]
check(dns.get('policyTypes') == ['Egress'] and set(dns_ports) == {53} and len(dns.get('egress', [])) == 1,
'allow-dns must allow only egress to port 53')
quota = base.get('ResourceQuota/aevolution-quota', {}).get('spec', {}).get('hard', {})
check(quota.get('services.loadbalancers') == '0' and quota.get('services.nodeports') == '0',
'quota must forbid LoadBalancer and NodePort services')
for env in ('dev', 'stage', 'world0'):
k = f'{platform}/{env}/kustomization.yaml'
ns_path = f'{platform}/{env}/namespace.yaml'
check(k in manifests and ns_path in manifests, f'{platform}/{env}: overlay missing')
if k not in manifests or ns_path not in manifests:
continue
kustomization = manifests[k][0]
check(kustomization.get('namespace') == f'evolution-{env}' and '../base' in kustomization.get('resources', []),
f'{k}: must set namespace evolution-{env} and include ../base')
labels = manifests[ns_path][0]['metadata'].get('labels', {})
check(manifests[ns_path][0]['metadata']['name'] == f'evolution-{env}', f'{ns_path}: wrong name')
check(labels.get('aevolution.io/environment') == env, f'{ns_path}: environment label')
check(labels.get('pod-security.kubernetes.io/enforce') == 'restricted', f'{ns_path}: restricted pod security')
check((labels.get('aevolution.io/protected') == 'true') == (env == 'world0'),
f'{ns_path}: only world0 is protected')
# World0 admission policy.
policy = [d for p, d in objects if d['kind'] == 'ValidatingAdmissionPolicy'
and d['metadata']['name'] == 'aevolution-world0-protection']
binding = [d for p, d in objects if d['kind'] == 'ValidatingAdmissionPolicyBinding'
and d['metadata']['name'] == 'aevolution-world0-protection']
check(len(policy) == 1 and len(binding) == 1, 'world0 admission policy and binding must exist once')
if policy and binding:
spec = policy[0]['spec']
check(spec.get('failurePolicy') == 'Fail', 'world0 policy must fail closed')
check(binding[0]['spec'].get('validationActions') == ['Deny'] and 'paramRef' not in binding[0]['spec']
and 'matchResources' not in binding[0]['spec'], 'world0 binding must Deny everywhere')
covered = {(r, op) for rule in spec['matchConstraints']['resourceRules']
for r in rule['resources'] for op in rule['operations']}
for resource in ('namespaces', 'persistentvolumes', 'persistentvolumeclaims', 'secrets',
'serviceaccounts', 'applications', 'appprojects'):
check((resource, 'DELETE') in covered, f'world0 policy must cover DELETE {resource}')
for resource in ('namespaces', 'persistentvolumes'):
check((resource, 'UPDATE') in covered, f'world0 policy must cover UPDATE {resource}')
text = yaml.safe_dump(spec)
for needle in ("aevolution:world0-break-glass", "evolution-world0", "aevolution-world0",
"aevolution.io/protected", "Retain"):
check(needle in text, f'world0 policy must reference {needle}')
# Argo CD projects and applications.
for path, doc in objects:
name = doc['metadata'].get('name', '')
if doc['kind'] == 'AppProject' and name.startswith('aevolution-'):
env = name[len('aevolution-'):]
spec = doc['spec']
namespaces = [d.get('namespace') for d in spec.get('destinations', [])]
check(namespaces == [f'evolution-{env}'], f'{path}: {name} must target only evolution-{env}')
check(spec.get('clusterResourceWhitelist') == [], f'{path}: {name} must allow no cluster kinds')
kinds = {w.get('kind') for w in spec.get('namespaceResourceWhitelist', [])}
check(not kinds & {'Secret', 'Namespace', 'ResourceQuota', 'LimitRange', 'Role', 'RoleBinding', '*'},
f'{path}: {name} whitelists a forbidden kind')
if env == 'world0':
windows = spec.get('syncWindows', [])
check(any(w.get('kind') == 'deny' and w.get('manualSync') is True and w.get('duration') == '24h'
and w.get('schedule') == '* * * * *' for w in windows),
f'{path}: world0 must deny automated sync at all times')
check('ServiceAccount' not in kinds, f'{path}: world0 service accounts are platform-managed')
if doc['kind'] == 'Application':
spec = doc['spec']
project = spec.get('project', '')
destination = spec.get('destination', {}).get('namespace', '')
check('automated' not in (spec.get('syncPolicy') or {}), f'{path}: {name} must not auto-sync')
check('resources-finalizer.argocd.argoproj.io' not in doc['metadata'].get('finalizers', []),
f'{path}: {name} must not cascade-delete')
if project.startswith('aevolution-'):
check(destination == 'evolution-' + project[len('aevolution-'):],
f'{path}: {name} leaves its project environment')
check(destination != 'evolution-world0' or name == 'aevolution-world0',
f'{path}: only aevolution-world0 may target evolution-world0')
# The live Argo path.
for path, doc in objects:
if path.startswith('deploy/kubernetes/dev/'):
check(doc['kind'] not in {'Namespace', 'Secret', 'ResourceQuota', 'LimitRange', 'Role',
'RoleBinding', 'ClusterRole', 'ClusterRoleBinding'},
f'{path}: {doc["kind"]} must not live in the Argo workload path')
# The cleanup tool refuses World0 before kubectl runs (no kubectl on PATH here).
with tempfile.TemporaryDirectory() as empty:
env = dict(os.environ, PATH=empty + os.pathsep + os.path.dirname(sys.executable))
for bad in ('world0', 'evolution-world0', 'World0', 'prod', ''):
result = subprocess.run([shutil.which('bash') or '/bin/bash', os.path.join(ROOT, 'tools/ops/cleanup-environment.sh'), bad],
capture_output=True, text=True, env=env)
check(result.returncode == 2 and ('refused' in result.stderr or 'usage' in result.stderr)
and 'kubectl' not in result.stderr,
f'cleanup must refuse {bad!r} before kubectl (rc={result.returncode})')
for message in failures:
print('FAIL', message)
print(f'deploy policy: {len(objects)} objects in {len(manifests)} files, {len(failures)} failures')
sys.exit(1 if failures else 0)