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:
@@ -13,9 +13,24 @@ let
|
||||
isAddress = b: builtins.match "[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+" b != null;
|
||||
addressOf = b: if isAddress b then b else reserved.${b} or null;
|
||||
|
||||
# Files the services name (certificates with their keys, certificate
|
||||
# authorities). The settings editor only accepts paths in the state directory
|
||||
# (0700 root), which HAProxy, a user of its own, cannot enter: systemd reads
|
||||
# them as root and hands HAProxy copies (LoadCredential). Store paths are
|
||||
# public and are used as they are.
|
||||
isStore = p: lib.hasPrefix "/nix/store/" (toString p);
|
||||
filesOf =
|
||||
lb:
|
||||
lib.optional (lb.tls == "terminate" && lb.certificate != null) lb.certificate
|
||||
++ lib.optional (lb.backendTls.caFile != null) lb.backendTls.caFile;
|
||||
privateFiles = lib.unique (map toString (lib.filter (p: !isStore p) (lib.concatMap filesOf (lib.attrValues lbs))));
|
||||
credentialName = p: "lb" + lib.replaceStrings [ "/" ] [ "_" ] p;
|
||||
fileOf =
|
||||
p: if isStore p then toString p else "/run/credentials/haproxy.service/${credentialName (toString p)}";
|
||||
|
||||
backendPort = lb: if lb.backendPort != null then lb.backendPort else lb.listen.port;
|
||||
usesAcme = lb: lb.tls == "terminate" && lb.certificate == null;
|
||||
certOf = lb: if lb.certificate != null then toString lb.certificate else acmePem;
|
||||
certOf = lb: if lb.certificate != null then fileOf lb.certificate else acmePem;
|
||||
|
||||
# Services by port: one frontend each, the host name chooses the backend.
|
||||
ports = lib.unique (map (n: lbs.${n}.listen.port) names);
|
||||
@@ -69,7 +84,7 @@ let
|
||||
secure = lb.backendTls.enable;
|
||||
verify =
|
||||
if lb.backendTls.verify then
|
||||
"verify required${lib.optionalString (lb.backendTls.caFile != null) " ca-file ${toString lb.backendTls.caFile}"}"
|
||||
"verify required${lib.optionalString (lb.backendTls.caFile != null) " ca-file ${fileOf lb.backendTls.caFile}"}"
|
||||
else
|
||||
"verify none";
|
||||
# Passthrough sends the client's bytes as they are: only the checks speak TLS.
|
||||
@@ -119,6 +134,25 @@ in
|
||||
config = haproxyConfig;
|
||||
};
|
||||
|
||||
systemd.services.haproxy.serviceConfig.LoadCredential = map (p: "${credentialName p}:${p}") privateFiles;
|
||||
|
||||
# HAProxy has its copies from when it started: a file that changes (a
|
||||
# renewed certificate) needs it restarted.
|
||||
systemd.paths.router-lb-files = lib.mkIf (privateFiles != [ ]) {
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
pathConfig = {
|
||||
PathChanged = privateFiles;
|
||||
Unit = "router-lb-files.service";
|
||||
};
|
||||
};
|
||||
systemd.services.router-lb-files = lib.mkIf (privateFiles != [ ]) {
|
||||
description = "Restart the load balancers for changed certificates";
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
ExecStart = "${config.systemd.package}/bin/systemctl restart haproxy.service";
|
||||
};
|
||||
};
|
||||
|
||||
# The router's own certificate, for services that decrypt with it.
|
||||
security.acme.certs = lib.mkIf (acme.enable && lib.any (n: usesAcme lbs.${n}) names) {
|
||||
${acmeName} = {
|
||||
|
||||
Reference in New Issue
Block a user