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:
parent
796fd6201f
commit
d9023217fc
1 changed files with 150 additions and 9 deletions
|
|
@ -2099,6 +2099,89 @@ let
|
|||
&& idx onCluster "ct original" < idx onCluster "counter"
|
||||
&& idx onCluster "@direct_mac accept" < idx onCluster "ct original";
|
||||
}
|
||||
{
|
||||
# The checker is the control that it finds a violation: the same pod with
|
||||
# a host port, a kept capability or a missing seccomp profile is flagged,
|
||||
# and the workloads it reads are all seven.
|
||||
name = "the namespace enforces Pod Security restricted, and no pod the module renders breaks it";
|
||||
ok =
|
||||
let
|
||||
ns = (render allLocal).namespace.metadata.labels;
|
||||
workloads = lib.filterAttrs (_: o: o.kind == "Deployment" || o.kind == "CronJob") (
|
||||
render onCluster
|
||||
);
|
||||
specOf =
|
||||
o: if o.kind == "CronJob" then o.spec.jobTemplate.spec.template.spec else o.spec.template.spec;
|
||||
specs = map specOf (lib.attrValues workloads);
|
||||
containersOf = s: s.containers ++ (s.initContainers or [ ]);
|
||||
problems =
|
||||
s:
|
||||
lib.concatMap (k: lib.optional (s.${k} or false) k) [
|
||||
"hostNetwork"
|
||||
"hostPID"
|
||||
"hostIPC"
|
||||
]
|
||||
++ lib.optional (lib.any (v: v ? hostPath) s.volumes) "hostPath"
|
||||
++ lib.optional (
|
||||
!(lib.elem (s.securityContext.seccompProfile.type or "") [
|
||||
"RuntimeDefault"
|
||||
"Localhost"
|
||||
])
|
||||
) "seccomp"
|
||||
++ lib.concatMap (
|
||||
c:
|
||||
let
|
||||
sc = c.securityContext or { };
|
||||
in
|
||||
lib.optional ((sc.allowPrivilegeEscalation or null) != false) "escalation ${c.name}"
|
||||
++ lib.optional ((sc.capabilities.drop or [ ]) != [ "ALL" ]) "drop ${c.name}"
|
||||
++ lib.optional (sc.capabilities ? add) "add ${c.name}"
|
||||
++ lib.optional (sc.privileged or false) "privileged ${c.name}"
|
||||
++ lib.optional (!(sc.runAsNonRoot or s.securityContext.runAsNonRoot or false)) "root ${c.name}"
|
||||
++ lib.concatMap (p: lib.optional (p ? hostPort) "hostPort ${c.name}") (c.ports or [ ])
|
||||
) (containersOf s);
|
||||
first = lib.head specs;
|
||||
withHostPort = first // {
|
||||
containers = [
|
||||
(
|
||||
(lib.head first.containers)
|
||||
// {
|
||||
ports = [
|
||||
{
|
||||
containerPort = 1;
|
||||
hostPort = 1;
|
||||
}
|
||||
];
|
||||
}
|
||||
)
|
||||
];
|
||||
};
|
||||
withCaps = first // {
|
||||
containers = [
|
||||
(
|
||||
(lib.head first.containers)
|
||||
// {
|
||||
securityContext = (lib.head first.containers).securityContext // {
|
||||
capabilities.add = [ "NET_ADMIN" ];
|
||||
};
|
||||
}
|
||||
)
|
||||
];
|
||||
};
|
||||
withoutSeccomp = first // {
|
||||
securityContext = removeAttrs first.securityContext [ "seccompProfile" ];
|
||||
};
|
||||
in
|
||||
ns."pod-security.kubernetes.io/enforce" == "restricted"
|
||||
&& ns."pod-security.kubernetes.io/warn" == "restricted"
|
||||
&& ns."pod-security.kubernetes.io/audit" == "restricted"
|
||||
&& ns."app.kubernetes.io/part-of" == "hyperhive"
|
||||
&& lib.length specs == 7
|
||||
&& lib.all (s: problems s == [ ]) specs
|
||||
&& problems withHostPort != [ ]
|
||||
&& problems withCaps != [ ]
|
||||
&& problems withoutSeccomp != [ ];
|
||||
}
|
||||
{
|
||||
# The file names the runtime and the host units of every service and no
|
||||
# state: nothing is copied. The units of the poller (timer and service)
|
||||
|
|
|
|||
Loading…
Reference in a new issue