k8s: the gateway's group reads the store credential files nginx includes
This commit is contained in:
parent
0d6065cf8f
commit
f65aed2147
2 changed files with 119 additions and 64 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -19,8 +19,9 @@ rec {
|
|||
# `victorialogs` or `victoriametrics`.
|
||||
leaf = store: "swarm/services/${store}/store-auth";
|
||||
|
||||
# On the host, a directory only root reads: the password the Secret is made from,
|
||||
# and the nginx file the gateway's locations include.
|
||||
# On the host, a directory for root and the gateway's group (`0750`): the nginx
|
||||
# file the gateway's locations include, which the group reads (`0640`), and the
|
||||
# password the Secret is made from, which only root reads.
|
||||
dir = "/var/lib/swarm-store-auth";
|
||||
passwordFile = store: "${dir}/${store}.password";
|
||||
nginxFile = store: "${dir}/${store}.conf";
|
||||
|
|
|
|||
Loading…
Reference in a new issue