k8s: deny ingress to every pod by default
This commit is contained in:
parent
fbc670087a
commit
67218f4052
1 changed files with 241 additions and 27 deletions
|
|
@ -28,6 +28,7 @@ let
|
|||
inherit (pkgs) lib;
|
||||
|
||||
bridgeIp = "10.42.0.1";
|
||||
agentIp = "10.42.0.200";
|
||||
baoName = "bao.t.local";
|
||||
baoPort = 8200;
|
||||
podPort = 8208;
|
||||
|
|
@ -135,7 +136,10 @@ pkgs.testers.runNixOSTest {
|
|||
nodes.machine =
|
||||
{ config, ... }:
|
||||
{
|
||||
imports = [ self.nixosModules.default ];
|
||||
imports = [
|
||||
self.nixosModules.default
|
||||
./lib/agent-netns.nix
|
||||
];
|
||||
|
||||
services.hyperhive = {
|
||||
deploy.k8s.enable = true;
|
||||
|
|
@ -544,6 +548,26 @@ pkgs.testers.runNixOSTest {
|
|||
for _ in range(3):
|
||||
assert machine.execute(dials(other, ip, PORT))[0] != 0
|
||||
|
||||
with subtest("a client of the hive bridge and another pod cannot reach the controller's pod, and the node reads its health port"):
|
||||
deny = json.loads(kube(machine, "get networkpolicy default-deny-ingress -o json"))["spec"]
|
||||
assert deny == {"podSelector": {}, "policyTypes": ["Ingress"]}, deny
|
||||
bridge_if = machine.succeed("ip -o -4 addr show | grep -F 'inet ${bridgeIp}/' | cut -d' ' -f2").strip()
|
||||
machine.succeed(
|
||||
"ip link add va type veth peer name vb && ip link set vb netns agent "
|
||||
f"&& ip link set va master {bridge_if} && ip link set va up "
|
||||
"&& ip -n agent addr add ${agentIp}/24 dev vb && ip -n agent link set vb up && ip -n agent link set lo up "
|
||||
"&& ip -n agent route add default via ${bridgeIp}"
|
||||
)
|
||||
pod_ip = kube(machine, f"get pod {pod_name} -o jsonpath='{{.status.podIP}}'").strip()
|
||||
code = machine.succeed(f"curl -s -o /dev/null -w '%{{http_code}}' --max-time 5 http://{pod_ip}:8080/healthz").strip()
|
||||
assert code == "200", code
|
||||
# The listener is reached by the node's own dial, the control that the
|
||||
# refusals below are the policy's.
|
||||
machine.succeed(f"timeout 5 bash -c '</dev/tcp/{pod_ip}/{PORT}'")
|
||||
for port in (8080, PORT):
|
||||
machine.wait_until_fails(dials(other, pod_ip, port), timeout=120)
|
||||
machine.wait_until_fails(f"ip netns exec agent timeout 5 bash -c '</dev/tcp/{pod_ip}/{port}'", timeout=120)
|
||||
|
||||
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
|
||||
|
|
|
|||
Loading…
Reference in a new issue