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:
@@ -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'),
|
||||
|
||||
Reference in New Issue
Block a user