Check effective NetworkPolicy ingress to the region session port

Rendered policies must keep default-deny in every environment, and no ingress rule
that can select a region pod may admit the session port (no or empty ports, the
session name, 7400 or a spanning range) except the validated route policy. Route
Service and policy validators are shared by source and rendered checks. Self-test
grows to 44 cases, including the reviewer's all-ingress reproduction.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-02 19:06:49 -04:00
co-authored by Claude Opus 5.5
parent 8a1e5ef53d
commit cb31893a47
4 changed files with 201 additions and 53 deletions
+106 -53
View File
@@ -309,6 +309,57 @@ for path, doc in objects:
if volume.get('secret', {}).get('secretName') == 'evolution-dev-session':
check(volume['secret'].get('optional') is True and volume['secret'].get('defaultMode') == 0o440,
f'{path}: the dev session Secret is optional and 0440')
ROUTE_PORT = [{'name': 'session', 'port': 7400, 'targetPort': 'session', 'protocol': 'TCP'}]
REGION_SELECTOR = {'app.kubernetes.io/name': 'evo-region'}
def validate_route_service(where, doc):
"""The route Service's full reviewed shape; returns its source ranges."""
spec = doc['spec']
ranges = spec.get('loadBalancerSourceRanges', [])
check(spec.get('type') == 'LoadBalancer' and spec.get('externalTrafficPolicy') == 'Local'
and spec.get('allocateLoadBalancerNodePorts') is False,
f'{where}: the route is a LoadBalancer with Local traffic policy and no node ports')
check(spec.get('ports') == ROUTE_PORT, f'{where}: the route exposes exactly TCP 7400')
check(spec.get('selector') == REGION_SELECTOR, f'{where}: the route selects only the region pod')
annotations = doc['metadata'].get('annotations', {})
check(set(annotations) == {'metallb.io/loadBalancerIPs'},
f'{where}: only a pinned MetalLB address annotation (no PROXY protocol or sharing)')
vip = str(annotations.get('metallb.io/loadBalancerIPs', ''))
check(len(ranges) >= 1, f'{where}: loadBalancerSourceRanges must name the permitted CIDRs')
if not vip.startswith('REPLACE'):
try:
address = ipaddress.ip_address(vip)
check(internal(ipaddress.ip_network(f'{address}/{address.max_prefixlen}')),
f'{where}: the VIP must be an internal address (RFC 1918 or approved in ADR-0087)')
except ValueError:
check(False, f'{where}: the VIP must be one IP address')
for cidr in ranges:
if str(cidr).startswith('REPLACE'):
continue
try:
network = ipaddress.ip_network(cidr)
check(internal(network) and network.prefixlen >= (16 if network.version == 4 else 48),
f'{where}: {cidr} must be a private range no wider than /16 (IPv6 /48)')
except ValueError:
check(False, f'{where}: {cidr} is not a CIDR')
return ranges
def validate_route_policy(where, doc, ranges):
"""The route policy admits TCP 7400 to the region pod from exactly the Service ranges."""
spec = doc['spec']
rules = spec.get('ingress', [])
check(spec.get('podSelector') == {'matchLabels': REGION_SELECTOR}
and spec.get('policyTypes') == ['Ingress'] and len(rules) == 1
and rules[0].get('ports') == [{'protocol': 'TCP', 'port': 7400}],
f'{where}: the session policy admits only TCP 7400 to the region pod')
peers = rules[0].get('from', []) if rules else []
check(peers and all(set(peer) == {'ipBlock'} and set(peer['ipBlock']) == {'cidr'} for peer in peers)
and sorted(peer['ipBlock']['cidr'] for peer in peers) == sorted(ranges),
f'{where}: the session policy admits exactly the Service source ranges')
route_ranges = []
for path, doc in objects:
if doc['kind'] == 'Service':
@@ -321,35 +372,7 @@ for path, doc in objects:
check(spec.get('type', 'ClusterIP') == 'ClusterIP',
f'{path}: {spec.get("type")} Services are forbidden (ADR-0069)')
continue
route_ranges = spec.get('loadBalancerSourceRanges', [])
check(spec.get('type') == 'LoadBalancer' and spec.get('externalTrafficPolicy') == 'Local'
and spec.get('allocateLoadBalancerNodePorts') is False,
f'{path}: the route is a LoadBalancer with Local traffic policy and no node ports')
check(spec.get('ports') == [{'name': 'session', 'port': 7400, 'targetPort': 'session', 'protocol': 'TCP'}],
f'{path}: the route exposes exactly TCP 7400')
check(spec.get('selector') == {'app.kubernetes.io/name': 'evo-region'},
f'{path}: the route selects only the region pod')
annotations = doc['metadata'].get('annotations', {})
check(set(annotations) == {'metallb.io/loadBalancerIPs'},
f'{path}: only a pinned MetalLB address annotation (no PROXY protocol or sharing)')
vip = annotations.get('metallb.io/loadBalancerIPs', '')
check(len(route_ranges) >= 1, f'{path}: loadBalancerSourceRanges must name the permitted CIDRs')
if not vip.startswith('REPLACE'):
try:
address = ipaddress.ip_address(vip)
check(internal(ipaddress.ip_network(f'{address}/{address.max_prefixlen}')),
f'{path}: the VIP must be an internal address (RFC 1918 or approved in ADR-0087)')
except ValueError:
check(False, f'{path}: the VIP must be one IP address')
for cidr in route_ranges:
if cidr.startswith('REPLACE'):
continue
try:
network = ipaddress.ip_network(cidr)
check(internal(network) and network.prefixlen >= (16 if network.version == 4 else 48),
f'{path}: {cidr} must be a private range no wider than /16 (IPv6 /48)')
except ValueError:
check(False, f'{path}: {cidr} is not a CIDR')
route_ranges = validate_route_service(path, doc)
if doc['kind'] == 'Ingress':
text = yaml.safe_dump(doc)
check('7400' not in text and 'session' not in text,
@@ -357,16 +380,7 @@ for path, doc in objects:
policies = [(p, d) for p, d in objects if p.startswith(ROUTE) and d['kind'] == 'NetworkPolicy']
check(len(policies) == 1, f'{ROUTE}: exactly one session ingress policy')
for path, doc in policies:
spec = doc['spec']
rules = spec.get('ingress', [])
blocks = [peer.get('ipBlock', {}) for rule in rules for peer in rule.get('from', [])]
check(spec.get('podSelector') == {'matchLabels': {'app.kubernetes.io/name': 'evo-region'}}
and spec.get('policyTypes') == ['Ingress'] and len(rules) == 1
and rules[0].get('ports') == [{'protocol': 'TCP', 'port': 7400}],
f'{path}: the session policy admits only TCP 7400 to the region pod')
check(all(set(b) == {'cidr'} for b in blocks) and len(blocks) == len(rules[0].get('from', []))
and sorted(b.get('cidr') for b in blocks) == sorted(route_ranges),
f'{path}: the session policy admits exactly the Service source ranges')
validate_route_policy(path, doc, route_ranges)
for path, doc in objects:
if path.startswith(ROUTE):
check(doc.get('metadata', {}).get('namespace') in (None, 'evolution-dev'),
@@ -606,6 +620,43 @@ if binary:
check(result.returncode == 0, f'{overlay}: kustomize build failed: {result.stderr.strip()[:300]}')
if result.returncode == 0:
renders[overlay] = [d for d in yaml.safe_load_all(result.stdout) if isinstance(d, dict)]
REGION_POD_LABELS = [d['spec']['template']['metadata'].get('labels', {}) for docs in renders.values()
for d in docs if d['kind'] == 'Deployment' and d['metadata'].get('name') == 'evo-region']
REGION_POD_LABELS = REGION_POD_LABELS or [REGION_SELECTOR]
def selects_region(selector):
"""True when a pod selector can select a region pod (empty and expressions count)."""
selector = selector or {}
if not selector or selector.get('matchExpressions'):
return True
wanted = selector.get('matchLabels', {})
return any(all(labels.get(k) == v for k, v in wanted.items()) for labels in REGION_POD_LABELS)
def admits_session(rule):
"""True when an ingress rule admits TCP 7400 on the region pod: no or empty ports, a
TCP entry without a port, the 'session' name, 7400 itself or a range spanning it."""
ports = rule.get('ports')
if not ports:
return True
for port in ports:
if port.get('protocol', 'TCP') != 'TCP':
continue
number = port.get('port')
if number is None or number in (7400, 'session'):
return True
if isinstance(number, int) and isinstance(port.get('endPort'), int) and number <= 7400 <= port['endPort']:
return True
return False
for env in ('dev', 'stage', 'world0'):
for overlay in (f'deploy/kubernetes/platform/{env}', 'deploy/kubernetes/platform/cluster'):
if overlay in renders:
check(any(d['kind'] == 'NetworkPolicy' and d['metadata'].get('name') == 'aevolution-default-deny'
and d['metadata'].get('namespace') == f'evolution-{env}' for d in renders[overlay]),
f'{overlay} (rendered): evolution-{env} must keep its default-deny policy')
for overlay, docs in renders.items():
expected = EXPECTED_NAMESPACE.get(overlay)
route_services = [d for d in docs if d['kind'] == 'Service' and d['metadata']['name'] == 'evo-region-session']
@@ -642,12 +693,8 @@ for overlay, docs in renders.items():
session = any(p.get('port') == 7400 or p.get('targetPort') in (7400, 'session')
for p in spec.get('ports', []))
if name == 'evo-region-session':
check(namespace == 'evolution-dev' and spec.get('type') == 'LoadBalancer'
and spec.get('externalTrafficPolicy') == 'Local'
and spec.get('allocateLoadBalancerNodePorts') is False
and spec.get('ports') == [{'name': 'session', 'port': 7400, 'targetPort': 'session',
'protocol': 'TCP'}],
f'{where}: the route keeps its reviewed shape in evolution-dev')
check(namespace == 'evolution-dev', f'{where}: the route belongs to evolution-dev only')
validate_route_service(where, doc)
else:
check(spec.get('type', 'ClusterIP') == 'ClusterIP' and not session,
f'{where}: only the route may be a LoadBalancer or expose the session port')
@@ -655,14 +702,20 @@ for overlay, docs in renders.items():
check('7400' not in yaml.safe_dump(doc) and 'session' not in yaml.safe_dump(doc),
f'{where}: the dev session is never an Ingress')
if kind == 'NetworkPolicy':
for rule in doc['spec'].get('ingress', []):
if any(p.get('port') == 7400 for p in rule.get('ports', [])):
ranges = route_services[0]['spec'].get('loadBalancerSourceRanges', []) if route_services else []
blocks = [peer.get('ipBlock', {}) for peer in rule.get('from', [])]
check(name == 'evo-region-session-ingress' and route_active and blocks
and all(set(b) == {'cidr'} for b in blocks) and len(blocks) == len(rule.get('from', []))
and sorted(b['cidr'] for b in blocks) == sorted(ranges),
f'{where}: only the route policy admits TCP 7400, from exactly its source ranges')
spec = doc.get('spec', {})
if name == 'aevolution-default-deny':
check(spec.get('podSelector') == {} and sorted(spec.get('policyTypes', [])) == ['Egress', 'Ingress']
and not spec.get('ingress') and not spec.get('egress'),
f'{where}: default-deny must select every pod and allow nothing')
ranges = route_services[0]['spec'].get('loadBalancerSourceRanges', []) if route_services else []
if name == 'evo-region-session-ingress':
check(route_active, f'{where}: the session policy renders only with the route')
validate_route_policy(where, doc, ranges)
elif selects_region(spec.get('podSelector')):
for rule in spec.get('ingress', []):
check(not admits_session(rule),
f'{where}: an ingress rule admits the region session port '
'(no or empty ports, the session name, 7400 or a range spanning it)')
if kind == 'ResourceQuota':
hard = doc['spec'].get('hard', {})
check(hard.get('services.nodeports') == '0', f'{where}: node ports stay forbidden')