diff --git a/docs/adr/0088-rendered-deployment-policy.md b/docs/adr/0088-rendered-deployment-policy.md index 2159887..9048a95 100644 --- a/docs/adr/0088-rendered-deployment-policy.md +++ b/docs/adr/0088-rendered-deployment-policy.md @@ -16,6 +16,13 @@ and a session port while the check passed. session/input/listener environment, the single listener argument and container ports; Service types and the session port; route Service shape; NetworkPolicies admitting the session port; quotas; Ingresses; and the absence of Secrets. Source-file checks stay. +- NetworkPolicies (review of 8a1e5ef): every rendered environment keeps its default-deny + (all pods, both directions, no rules). Any rendered ingress rule whose selector can + select a region pod (empty or expression selectors count) must not admit the session + port: omitted or empty ports, a TCP entry without a port, the name `session`, 7400 or a + range spanning it all count, whatever the sources. Only the route policy may, with its + full reviewed shape. The route Service and policy use the same validators for source + and rendered objects (VIP, ranges, annotations, selector). - Kustomizations may use only the keys in use today (resources, components, patches, images, labels, common labels/annotations, namespace, ConfigMap generators); generators/transformers plugins, Helm inflation and Secret generators are refused. diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index cb3f783..7dbe69a 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -3962,3 +3962,23 @@ ops-port failures in both region/dev and dev; the check fails without kustomize kustomize v5.4.3; ops-lib and ci-path fixtures pass. Not run: the ci-deploy Docker stage (no Docker here; its commands were run directly, including prepare-kustomize.sh), any cluster dry-run or live action. + + +## 2026-10-02 — M9.8f review of 8a1e5ef (.cache/reviews/M9.8f-8a1e5ef-review.md) + +P2: rendered NetworkPolicies are now checked by effect: the default-deny contract in +every environment namespace, and every ingress rule able to select a region pod +(empty/expression selectors included) is refused if it admits the session port (no or +empty ports, port-less TCP entries, the `session` name, 7400, ranges spanning it), except +the fully validated route policy. Activation follow-up done now: the route Service and +policy validators (VIP, source ranges, annotations, selector, shape) are shared by the +source and rendered checks. + +Executed (WSL, kustomize v5.8.1): policy check 0 failures (126 rendered objects, existing +ops/metrics scrape rule accepted); self-test 44/44 refused, adding the reviewer's +all-ingress default-deny patch, region policies without ports, with empty ports, by +the session name, by a 7000-8000 range, an every-pod policy on 7400, an expression +selector, a port-less entry, and rendered route patches of the selector, VIP and source +range; the reviewer's m98f-8a1e5ef-repro.py now exits 1 with default-deny and +session-port failures in platform/dev and platform/cluster. No cluster action. M9.8f +remains open. diff --git a/tools/ci/deploy-policy-check.py b/tools/ci/deploy-policy-check.py index 658974f..64007da 100644 --- a/tools/ci/deploy-policy-check.py +++ b/tools/ci/deploy-policy-check.py @@ -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') diff --git a/tools/ci/deploy-policy-selftest.py b/tools/ci/deploy-policy-selftest.py index 6375939..485b224 100644 --- a/tools/ci/deploy-policy-selftest.py +++ b/tools/ci/deploy-policy-selftest.py @@ -63,6 +63,30 @@ def env_patch(name, value): f' - {{name: {name}, value: "{value}"}}\n') +def add_resource(tree, kustomization, name, text): + """Adds a manifest file to a kustomization's resources.""" + import yaml + directory = os.path.dirname(kustomization) + write(tree, os.path.join(directory, name), text) + full = os.path.join(tree, kustomization) + with open(full, encoding='utf-8') as f: + data = yaml.safe_load(f) + data.setdefault('resources', []).append(name) + with open(full, 'w', encoding='utf-8') as f: + yaml.safe_dump(data, f, sort_keys=False) + + +def region_policy(selector, rule): + return ('apiVersion: networking.k8s.io/v1\nkind: NetworkPolicy\nmetadata:\n name: widened\nspec:\n' + f' podSelector: {selector}\n policyTypes: [Ingress]\n ingress:\n - {rule}\n') + + +def route_on(tree): + enable(tree) + resolve(tree) + quota_one(tree) + + def enable(tree): append(tree, DEV, 'components:\n - ../dev-session-route\n') @@ -118,6 +142,50 @@ CASES = [ t, 'deploy/kubernetes/platform/stage/kustomization.yaml', 'apiVersion: v1\nkind: ResourceQuota\nmetadata:\n name: aevolution-quota\nspec:\n hard:\n' ' services.loadbalancers: "1"\n'), 'LoadBalancers only in evolution-dev'), + # Review 8a1e5ef P2: any effective ingress rule that can reach the region's session port. + ('reviewer reproduction (default-deny admits all ingress)', lambda t: inline_patch( + t, 'deploy/kubernetes/platform/dev/kustomization.yaml', + 'apiVersion: networking.k8s.io/v1\nkind: NetworkPolicy\nmetadata:\n name: aevolution-default-deny\n' + 'spec:\n ingress:\n - {}\n'), 'default-deny must select every pod and allow nothing'), + ('region policy without ports', lambda t: add_resource( + t, DEV, 'widened.yaml', region_policy('{matchLabels: {app.kubernetes.io/name: evo-region}}', + '{from: [{namespaceSelector: {}}]}')), 'admits the region session port'), + ('region policy with empty ports', lambda t: add_resource( + t, DEV, 'widened.yaml', region_policy('{matchLabels: {app.kubernetes.io/name: evo-region}}', + '{from: [{ipBlock: {cidr: 10.0.0.0/8}}], ports: []}')), + 'admits the region session port'), + ('region policy by the session port name', lambda t: add_resource( + t, DEV, 'widened.yaml', region_policy('{matchLabels: {app.kubernetes.io/name: evo-region}}', + '{ports: [{protocol: TCP, port: session}]}')), + 'admits the region session port'), + ('region policy with a port range spanning 7400', lambda t: add_resource( + t, DEV, 'widened.yaml', region_policy('{matchLabels: {app.kubernetes.io/name: evo-region}}', + '{ports: [{protocol: TCP, port: 7000, endPort: 8000}]}')), + 'admits the region session port'), + ('policy for every pod on 7400', lambda t: add_resource( + t, DEV, 'widened.yaml', region_policy('{}', '{from: [{ipBlock: {cidr: 0.0.0.0/0}}], ' + 'ports: [{protocol: TCP, port: 7400}]}')), + 'admits the region session port'), + ('policy by expression selector', lambda t: add_resource( + t, DEV, 'widened.yaml', region_policy( + '{matchExpressions: [{key: app.kubernetes.io/name, operator: Exists}]}', '{ports: [{port: 7400}]}')), + 'admits the region session port'), + ('policy port without number', lambda t: add_resource( + t, DEV, 'widened.yaml', region_policy('{matchLabels: {app.kubernetes.io/part-of: aevolution}}', + '{ports: [{protocol: TCP}]}')), 'admits the region session port'), + # Activation follow-up: the rendered route keeps every reviewed field. + ('rendered route selector patched', lambda t: (route_on(t), inline_patch( + t, DEV, '- op: replace\n path: /spec/selector\n value: {app.kubernetes.io/part-of: aevolution}\n', + {'kind': 'Service', 'name': 'evo-region-session'})), + '(rendered) Service/evo-region-session: the route selects only the region pod'), + ('rendered route VIP patched', lambda t: (route_on(t), inline_patch( + t, DEV, '- op: replace\n path: /metadata/annotations/metallb.io~1loadBalancerIPs\n value: 8.8.8.8\n', + {'kind': 'Service', 'name': 'evo-region-session'})), + '(rendered) Service/evo-region-session: the VIP must be an internal address'), + ('rendered route source range patched', lambda t: (route_on(t), inline_patch( + t, DEV, '- op: replace\n path: /spec/loadBalancerSourceRanges\n value: [0.0.0.0/0]\n', + {'kind': 'Service', 'name': 'evo-region-session'})), + '(rendered) Service/evo-region-session: 0.0.0.0/0 must be a private range'), ('secret generator', lambda t: append(t, DEV, 'secretGenerator:\n - name: leaked\n literals: [a=b]\n'), 'unsupported kustomize features'), ('helm chart inflation', lambda t: append(t, DEV, 'helmCharts:\n - name: x\n repo: https://example.invalid\n'),