Watch
0
0
Fork
You've already forked hyperhive
0

k8s: the gateway's group reads the store credential files nginx includes

This commit is contained in:
atlas 2026-11-28 05:29:08 +01:00 • committed by maia
commit e0b3c0a644

View file

@ -23,6 +23,10 @@ let
storeAuth = import ../lib/store-auth.nix;
storeRetry = import ../lib/store-retry.nix { };
# The gateway's group: nginx's master and workers run as its user, and read
# the files `include`d into the store locations.
nginxGroup = config.services.nginx.group;
# The stores this host runs, by the name `store-auth.nix` takes.
stores =
lib.optional deployCfg.victorialogs.enable "victorialogs"
@ -59,16 +63,22 @@ in
config = lib.mkIf (stores != [ ] || exporterOn) (
lib.mkMerge [
{
# The group exists on a host where the NixOS nginx unit is off too, since
# the files below are made with it.
users.groups.${nginxGroup} = { };
# The files exist before nginx first reads its configuration, because
# an `include` of a missing file is a fatal error there. `f` creates
# a file and never overwrites one, and an empty file sets no header:
# the store then refuses the request, which is the only thing a
# gateway without the credential can get from it.
# gateway without the credential can get from it. The directory and
# the nginx files are the gateway's group's to read, which nginx needs
# to start; the passwords and the hash stay in files only root reads.
systemd.tmpfiles.rules = [
"d ${storeAuth.dir} 0700 root root - -"
"d ${storeAuth.dir} 0750 root ${nginxGroup} - -"
]
++ map (store: "f ${storeAuth.nginxFile store} 0600 root root -") stores
++ lib.optional exporterOn "f ${exporter.nginxFile} 0600 root root -";
++ map (store: "f ${storeAuth.nginxFile store} 0640 root ${nginxGroup} -") stores
++ lib.optional exporterOn "f ${exporter.nginxFile} 0640 root ${nginxGroup} -";
}
(lib.mkIf (node.enable && k8sCfg.enable) {
@ -137,7 +147,7 @@ in
${import ../lib/atomic-write-secret.nix { }}
${import ../lib/refresh-consumer.nix { }}
install -d -m 0700 ${storeAuth.dir}
install -d -m 0750 -o root -g ${lib.escapeShellArg nginxGroup} ${storeAuth.dir}
# A password of 64 hex digits, as Grafana's bearer is.
mint_password() {
@ -160,11 +170,14 @@ in
directive="proxy_set_header Authorization \"Basic $(printf '%s:%s' "$user" "$password" | base64 -w0)\";"
if secret_differs "$nginx_file" "$directive"; then
atomic_write_secret 0600 root:root "$nginx_file" "$directive"
atomic_write_secret 0640 root:${nginxGroup} "$nginx_file" "$directive"
reload=1
elif written_after_start nginx.service "$nginx_file"; then
reload=1
fi
# A file that already holds the value keeps the owner it was written with.
chown root:${nginxGroup} "$nginx_file"
chmod 0640 "$nginx_file"
if secret_differs "$password_file" "$password"; then
atomic_write_secret 0600 root:root "$password_file" "$password"
password_changed=1