Watch
0
0
Fork
You've already forked hyperhive
0

k8s: the hyperhive namespace enforces pod security restricted

No pod in the namespace has a hostPort any more, and the census found nothing else that restricted refuses. The namespace now carries the restricted level for enforce, warn and audit, so the API server refuses a pod that drifts. The bao-login test's probe pods get the container security context restricted asks for. The controller switch test shows the API server admitting a compliant pod and refusing the same pod with a host port, a kept capability or privilege.
This commit is contained in:
atlas 2026-11-23 19:32:16 +01:00 • committed by maia
commit 51565fdba6
2 changed files with 150 additions and 9 deletions

View file

@ -103,6 +103,10 @@ pkgs.testers.runNixOSTest {
"sleep"
"3600"
];
# The namespace enforces Pod Security `restricted`, which asks the
# container for these: the pod-level settings alone leave it able to
# escalate and to keep capabilities.
inherit (lib.head podSpec.containers) securityContext;
volumeMounts = [
tokenMount
{

View file

@ -17,6 +17,7 @@
# resolver answers through CoreDNS, verifying the leaf as the listener's
# name; the controller answers an unsigned webhook 401 both times;
# - the Service is a ClusterIP the host dials and a pod can't (no `hostPort`);
# Pod Security `restricted` refuses a host port, a capability or privilege;
# - a changed hyperhive rev rolls the Deployment; the pod's state directory is
# an emptyDir, with no claim and no gate;
# - the switch back leaves no pod, Deployment, Service, NetworkPolicy or
@ -607,12 +608,40 @@ pkgs.testers.runNixOSTest {
for _ in range(3):
assert machine.execute(dials(other, ip, PORT))[0] != 0
with subtest("Pod Security restricted would admit the namespace's pods: the API server's dry run of the label names none, and does name kube-system's"):
dry = "label --dry-run=server --overwrite namespace {} pod-security.kubernetes.io/enforce=restricted 2>&1"
control = machine.succeed("kubectl " + dry.format("kube-system"))
assert "violate" in control, control
mine = machine.succeed("kubectl " + dry.format(NS))
assert "violate" not in mine, mine
with subtest("the namespace enforces Pod Security restricted: the API server admits a compliant pod and refuses the same pod with a host port, a kept capability or privilege"):
labels = json.loads(kube(machine, f"get namespace {NS} -o json"))["metadata"]["labels"]
assert labels["pod-security.kubernetes.io/enforce"] == "restricted", labels
compliant = {
"apiVersion": "v1",
"kind": "Pod",
"metadata": {"name": "psa-probe", "namespace": NS},
"spec": {
"securityContext": {"runAsNonRoot": True, "seccompProfile": {"type": "RuntimeDefault"}},
"containers": [{
"name": "c",
"image": main["image"],
"imagePullPolicy": "Never",
"securityContext": {"allowPrivilegeEscalation": False, "capabilities": {"drop": ["ALL"]}},
}],
},
}
def admission(change) -> tuple[int, str]:
"""What the API server answers to a server-side dry run of the compliant pod with `change` applied to its container."""
pod = json.loads(json.dumps(compliant))
change(pod["spec"]["containers"][0])
machine.succeed(f"cat > /root/psa.json <<'EOF'\n{json.dumps(pod)}\nEOF")
return machine.execute("kubectl apply --dry-run=server -f /root/psa.json 2>&1")
status, out = admission(lambda c: None)
assert status == 0, out
for what, change in [
("host port", lambda c: c.update(ports=[{"containerPort": 9, "hostPort": 9}])),
("kept capability", lambda c: c["securityContext"]["capabilities"].update(drop=[])),
("privilege", lambda c: c["securityContext"].update(privileged=True, allowPrivilegeEscalation=True)),
]:
status, out = admission(change)
assert status != 0 and 'violates PodSecurity "restricted:latest"' in out, (what, out)
with subtest("a changed rev rolls the Deployment, and the old pod is gone first"):
old_uid = pod(machine, pod_name)["metadata"]["uid"]