ADR-0087 records the maintainer's route (void.aeternalisgames.com:7400 via a dedicated internal MetalLB VIP to the evolution-dev region pod, TLS end to end, CIDR-limited) and operator-opened windows enforced by evo-server. The route is a kustomize component included nowhere; the policy check encodes its shape and refuses enabling until VIP/CIDR, the dev quota and --session-window exist, with a 20-case self-test in the CI deploy stage. The base listener stays loopback. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
149 lines
7.7 KiB
Python
149 lines
7.7 KiB
Python
#!/usr/bin/env python3
|
|
"""M9.8f fail-closed tests for deploy-policy-check.py (ADR-0087). Needs Python 3 and
|
|
PyYAML only. Copies the manifests and the tools the check runs into a temporary tree,
|
|
applies one unsafe change at a time and requires the check to fail with the expected
|
|
message; the unmodified copy must pass.
|
|
"""
|
|
import os
|
|
import re
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
|
|
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
|
|
ROUTE = 'deploy/kubernetes/region/dev-session-route/'
|
|
DEV = 'deploy/kubernetes/region/dev/kustomization.yaml'
|
|
REAL = [(ROUTE + 'service.yaml', 'REPLACE-dev-session-vip', '10.20.30.40'),
|
|
(ROUTE + 'service.yaml', 'REPLACE-dev-test-client-cidr', '10.20.0.0/24'),
|
|
(ROUTE + 'networkpolicy.yaml', 'REPLACE-dev-test-client-cidr', '10.20.0.0/24')]
|
|
|
|
|
|
def edit(tree, path, old, new, count=1):
|
|
full = os.path.join(tree, path)
|
|
with open(full, encoding='utf-8') as f:
|
|
text = f.read()
|
|
if old not in text:
|
|
raise SystemExit(f'selftest setup: {old!r} not in {path}')
|
|
with open(full, 'w', encoding='utf-8') as f:
|
|
f.write(text.replace(old, new, count))
|
|
|
|
|
|
def append(tree, path, text):
|
|
with open(os.path.join(tree, path), 'a', encoding='utf-8') as f:
|
|
f.write(text)
|
|
|
|
|
|
def write(tree, path, text):
|
|
full = os.path.join(tree, path)
|
|
os.makedirs(os.path.dirname(full), exist_ok=True)
|
|
with open(full, 'w', encoding='utf-8') as f:
|
|
f.write(text)
|
|
|
|
|
|
def enable(tree):
|
|
append(tree, DEV, 'components:\n - ../dev-session-route\n')
|
|
|
|
|
|
def resolve(tree):
|
|
for path, old, new in REAL:
|
|
edit(tree, path, old, new)
|
|
|
|
|
|
def quota_one(tree):
|
|
write(tree, 'deploy/kubernetes/platform/dev/quota-session.yaml',
|
|
'apiVersion: v1\nkind: ResourceQuota\nmetadata:\n name: aevolution-quota\n'
|
|
'spec:\n hard:\n services.loadbalancers: "1"\n services.nodeports: "0"\n')
|
|
|
|
|
|
CASES = [
|
|
('enabling with unresolved markers', lambda t: enable(t), 'unresolved REPLACE marker'),
|
|
('enabling before server-enforced windows', lambda t: (enable(t), resolve(t), quota_one(t)),
|
|
'server-enforced session windows'),
|
|
('enabling without the dev quota', lambda t: (enable(t), resolve(t)), 'exactly one LoadBalancer'),
|
|
('route included by stage', lambda t: append(t, 'deploy/kubernetes/region/stage/kustomization.yaml',
|
|
'components:\n - ../dev-session-route\n'),
|
|
'only region/dev may include'),
|
|
('stage quota allowing a LoadBalancer', lambda t: write(
|
|
t, 'deploy/kubernetes/platform/stage/quota-lb.yaml',
|
|
'apiVersion: v1\nkind: ResourceQuota\nmetadata:\n name: aevolution-quota\n'
|
|
'spec:\n hard:\n services.loadbalancers: "1"\n'), 'LoadBalancers only in evolution-dev'),
|
|
('world-open source range', lambda t: (edit(t, ROUTE + 'service.yaml', 'REPLACE-dev-test-client-cidr', '0.0.0.0/0'),
|
|
edit(t, ROUTE + 'networkpolicy.yaml', 'REPLACE-dev-test-client-cidr', '0.0.0.0/0')),
|
|
'private range'),
|
|
('public VIP', lambda t: edit(t, ROUTE + 'service.yaml', 'REPLACE-dev-session-vip', '8.8.8.8'), 'internal address'),
|
|
('policy CIDR differs from the Service', lambda t: edit(t, ROUTE + 'networkpolicy.yaml',
|
|
'REPLACE-dev-test-client-cidr', '10.99.0.0/24'),
|
|
'exactly the Service source ranges'),
|
|
('policy admits another port', lambda t: edit(t, ROUTE + 'networkpolicy.yaml', 'port: 7400', 'port: 7401'),
|
|
'only TCP 7400'),
|
|
('cluster traffic policy', lambda t: edit(t, ROUTE + 'service.yaml', 'externalTrafficPolicy: Local',
|
|
'externalTrafficPolicy: Cluster'), 'Local traffic policy'),
|
|
('node ports allocated', lambda t: edit(t, ROUTE + 'service.yaml', 'allocateLoadBalancerNodePorts: false',
|
|
'allocateLoadBalancerNodePorts: true'), 'no node ports'),
|
|
('PROXY protocol annotation', lambda t: edit(t, ROUTE + 'service.yaml', ' metallb.io/loadBalancerIPs:',
|
|
' example.io/proxy-protocol: "true"\n metallb.io/loadBalancerIPs:'),
|
|
'no PROXY protocol'),
|
|
('extra route port', lambda t: append(t, ROUTE + 'service.yaml',
|
|
' - {name: ops, port: 9100, targetPort: ops, protocol: TCP}\n'),
|
|
'exactly TCP 7400'),
|
|
('another LoadBalancer', lambda t: edit(t, 'deploy/kubernetes/region/base/service.yaml', 'type: ClusterIP',
|
|
'type: LoadBalancer'), 'Services are forbidden'),
|
|
('session port on another Service', lambda t: append(t, 'deploy/kubernetes/region/base/service.yaml',
|
|
' - {name: session, port: 7400, targetPort: 7400, protocol: TCP}\n'),
|
|
'only the dev session route Service'),
|
|
('session Ingress', lambda t: write(
|
|
t, 'deploy/kubernetes/region/dev/ingress.yaml',
|
|
'apiVersion: networking.k8s.io/v1\nkind: Ingress\nmetadata:\n name: session\nspec:\n rules:\n'
|
|
' - host: void.aeternalisgames.com\n http:\n paths:\n - path: /\n'
|
|
' pathType: Prefix\n backend:\n service:\n'
|
|
' name: evo-region-session\n port: {number: 7400}\n'), 'never an Ingress'),
|
|
('base listener on the pod interface', lambda t: edit(
|
|
t, 'deploy/kubernetes/region/base/deployment.yaml', 'value: "127.0.0.1:7400"', 'value: "0.0.0.0:7400"'),
|
|
'REGION_SESSION_LISTEN must be 127.0.0.1:7400'),
|
|
('hard-coded listener address', lambda t: edit(
|
|
t, 'deploy/kubernetes/region/base/deployment.yaml', '--session-listen "$REGION_SESSION_LISTEN"',
|
|
'--session-listen 0.0.0.0:7400'), 'only from REGION_SESSION_LISTEN'),
|
|
('route listener patch elsewhere', lambda t: edit(
|
|
t, 'deploy/kubernetes/region/dev/region-values.yaml', '- {name: REGION_ID, value: "1"}',
|
|
'- {name: REGION_ID, value: "1"}\n - {name: REGION_SESSION_LISTEN, value: "0.0.0.0:7400"}'),
|
|
'REGION_SESSION_LISTEN must be 127.0.0.1:7400'),
|
|
('route object in another namespace', lambda t: edit(
|
|
t, ROUTE + 'networkpolicy.yaml', ' name: evo-region-session-ingress',
|
|
' name: evo-region-session-ingress\n namespace: evolution-stage'), 'evolution-dev only'),
|
|
]
|
|
|
|
|
|
def copy_tree(target):
|
|
for directory in ('deploy', 'tools/ops', 'tools/backup'):
|
|
shutil.copytree(os.path.join(ROOT, directory), os.path.join(target, directory))
|
|
os.makedirs(os.path.join(target, 'tools', 'ci'))
|
|
shutil.copy(os.path.join(ROOT, 'tools', 'ci', 'deploy-policy-check.py'), os.path.join(target, 'tools', 'ci'))
|
|
|
|
|
|
def run(tree):
|
|
result = subprocess.run([sys.executable, os.path.join(tree, 'tools', 'ci', 'deploy-policy-check.py')],
|
|
capture_output=True, text=True)
|
|
return result.returncode, result.stdout + result.stderr
|
|
|
|
|
|
failures = []
|
|
with tempfile.TemporaryDirectory() as clean:
|
|
copy_tree(clean)
|
|
code, output = run(clean)
|
|
if code != 0:
|
|
failures.append(f'baseline copy must pass:\n{output}')
|
|
for name, mutate, expected in CASES:
|
|
with tempfile.TemporaryDirectory() as tree:
|
|
copy_tree(tree)
|
|
mutate(tree)
|
|
code, output = run(tree)
|
|
refused = code != 0 and re.search(re.escape(expected), output)
|
|
print(f'{"refused" if refused else "NOT REFUSED"}: {name}')
|
|
if not refused:
|
|
failures.append(f'{name}: expected failure mentioning {expected!r}, got rc={code}\n{output}')
|
|
for message in failures:
|
|
print('FAIL', message)
|
|
print(f'deploy policy selftest: {len(CASES)} cases, {len(failures)} failures')
|
|
sys.exit(1 if failures else 0)
|