Hand certificates and CA files to HAProxy as credentials
The settings only allow paths in the state directory, which is 0700 root and which HAProxy (a user of its own) cannot enter. systemd reads the files and gives the service copies; a path unit restarts it when one changes. The load balancer test keeps its files in the state directory now, and verifies the backends' certificate.
This commit is contained in:
+22
-2
@@ -129,6 +129,13 @@ pkgs.testers.runNixOSTest {
|
||||
networking.interfaces.eth1.ipv4.addresses = lib.mkForce [ ];
|
||||
networking.interfaces.eth2.ipv4.addresses = lib.mkForce [ ];
|
||||
|
||||
systemd.tmpfiles.rules = [
|
||||
"d /var/lib/router 0700 root root -"
|
||||
"d /var/lib/router/lb 0700 root root -"
|
||||
"C /var/lib/router/lb/both.pem 0600 root root - ${cert}/both.pem"
|
||||
"C /var/lib/router/lb/ca.pem 0600 root root - ${cert}/cert.pem"
|
||||
];
|
||||
|
||||
router = {
|
||||
enable = true;
|
||||
wan.interface = "eth1";
|
||||
@@ -162,12 +169,18 @@ pkgs.testers.runNixOSTest {
|
||||
"main"
|
||||
];
|
||||
hosts = [ "api.lb.test" ];
|
||||
certificate = "${cert}/both.pem";
|
||||
# Files in the router's state directory, as the settings editor requires:
|
||||
# 0700 root, so HAProxy (its own user) cannot read them itself.
|
||||
certificate = "/var/lib/router/lb/both.pem";
|
||||
backends = [
|
||||
"hv1"
|
||||
"hv2"
|
||||
];
|
||||
backendTls.enable = true;
|
||||
backendTls = {
|
||||
enable = true;
|
||||
verify = true;
|
||||
caFile = "/var/lib/router/lb/ca.pem";
|
||||
};
|
||||
check.path = "/healthz";
|
||||
check.interval = "1s";
|
||||
check.fall = 2;
|
||||
@@ -279,6 +292,13 @@ pkgs.testers.runNixOSTest {
|
||||
with subtest("the port is closed where the service does not listen"):
|
||||
client.fail("curl -sk --max-time 3 https://192.168.10.1:9443/")
|
||||
|
||||
with subtest("HAProxy only has copies of the files: a changed one restarts it"):
|
||||
pid = router.succeed("systemctl show -p MainPID --value haproxy.service").strip()
|
||||
router.succeed("echo >> /var/lib/router/lb/both.pem")
|
||||
router.wait_until_succeeds(f"test $(systemctl show -p MainPID --value haproxy.service) != {pid}", timeout=60)
|
||||
router.wait_until_succeeds("echo 'show stat' | nc -U -N /run/haproxy/haproxy.sock | grep -c ',UP,' | grep -qx 6", timeout=60)
|
||||
assert names(answers(upstream, outside)) == {"name=hv1", "name=hv2"}
|
||||
|
||||
with subtest("a backend that fails its check gets no connections, and returns when it passes"):
|
||||
hv1.succeed("touch /tmp/sick")
|
||||
router.wait_until_succeeds("echo 'show stat' | nc -U -N /run/haproxy/haproxy.sock | grep '^be_api,hv1' | grep -q ',DOWN,'", timeout=60)
|
||||
|
||||
Reference in New Issue
Block a user