k8s: the gateway's group reads the store credential files nginx includes
This commit is contained in:
parent
0d6065cf8f
commit
f65aed2147
5 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";
|
||||
|
|
|
|||
|
|
@ -2256,7 +2256,7 @@ let
|
|||
let
|
||||
h = allLocal.services.hyperhive;
|
||||
unit = allLocal.systemd.services.swarm-bao-store-auth;
|
||||
rule = store: "f /var/lib/swarm-store-auth/${store}.conf 0600 root root -";
|
||||
rule = store: "f /var/lib/swarm-store-auth/${store}.conf 0640 root nginx -";
|
||||
# One store's vhost: every location that dials it includes the file,
|
||||
# and no other location does.
|
||||
vhost =
|
||||
|
|
@ -2348,10 +2348,20 @@ let
|
|||
&& hasInfix "store-auth.pem" h.deploy.bao.storeAuthClientCertFile
|
||||
&& lib.elem (rule "victorialogs") allLocal.systemd.tmpfiles.rules
|
||||
&& lib.elem (rule "victoriametrics") allLocal.systemd.tmpfiles.rules
|
||||
# nginx runs as `User=nginx`: the directory and the nginx files are its
|
||||
# group's to read, written that way and fixed when they already hold the
|
||||
# value, and the passwords stay root's.
|
||||
&& lib.elem "d /var/lib/swarm-store-auth 0750 root nginx - -" allLocal.systemd.tmpfiles.rules
|
||||
&& hasInfix "install -d -m 0750 -o root -g nginx /var/lib/swarm-store-auth" unit.script
|
||||
&& hasInfix ''atomic_write_secret 0640 root:nginx "$nginx_file" "$directive"'' unit.script
|
||||
&& hasInfix ''chown root:nginx "$nginx_file"'' unit.script
|
||||
&& hasInfix ''chmod 0640 "$nginx_file"'' unit.script
|
||||
&& hasInfix ''atomic_write_secret 0600 root:root "$password_file" "$password"'' unit.script
|
||||
# Without either store there is no unit, no file and no location.
|
||||
&& !(noNode.systemd.services ? swarm-bao-store-auth)
|
||||
&& !(lib.elem (rule "victorialogs") noNode.systemd.tmpfiles.rules)
|
||||
&& !(lib.elem (rule "victoriametrics") noNode.systemd.tmpfiles.rules)
|
||||
&& !(lib.elem "d /var/lib/swarm-store-auth 0750 root nginx - -" noNode.systemd.tmpfiles.rules)
|
||||
&& !(noNode.services.nginx.virtualHosts ? ${h.swarm.victorialogs.domain})
|
||||
&& !(noNode.services.nginx.virtualHosts ? ${h.swarm.victoriametrics.domain});
|
||||
}
|
||||
|
|
@ -2369,7 +2379,7 @@ let
|
|||
flag: lib.elemAt exporter.args ((lib.lists.findFirstIndex (a: a == flag) (-1) exporter.args) + 1);
|
||||
locations = allLocal.services.nginx.virtualHosts.${h.swarm.nats.domain}.locations;
|
||||
include = "include /var/lib/swarm-store-auth/nats.conf;";
|
||||
rule = "f /var/lib/swarm-store-auth/nats.conf 0600 root root -";
|
||||
rule = "f /var/lib/swarm-store-auth/nats.conf 0640 root nginx -";
|
||||
in
|
||||
after "-http_user" == "swarm-exporter"
|
||||
# The argument is the variable Kubernetes substitutes, and the Secret it
|
||||
|
|
|
|||
|
|
@ -826,9 +826,9 @@ pkgs.testers.runNixOSTest {
|
|||
assert password not in kube(machine, f"get pod {pod_name} -o json")
|
||||
assert machine.succeed(f"stat -c '%a %U %G' {EXPORTER_PASSWORD} {EXPORTER_CONF} {EXPORTER_HASH_FILE} {STORE_AUTH_DIR}").split("\n")[:4] == [
|
||||
"600 root root",
|
||||
"640 root nginx",
|
||||
"600 root root",
|
||||
"600 root root",
|
||||
"700 root root",
|
||||
"750 root nginx",
|
||||
]
|
||||
login = base64.b64encode(f"swarm-exporter:{password}".encode()).decode()
|
||||
assert machine.succeed(f"cat {EXPORTER_CONF}").strip() == f'proxy_set_header Authorization "Basic {login}";'
|
||||
|
|
|
|||
|
|
@ -298,6 +298,13 @@ pkgs.testers.runNixOSTest {
|
|||
# which needs certificates this host does not mint, so a second nginx
|
||||
# carries the passthrough alone, as `k8s-bao-login` does.
|
||||
services.nginx.enable = lib.mkForce false;
|
||||
# The user the module would create: the stub gateway below runs as it, so a
|
||||
# file it includes has to be the gateway's to read, as under the real unit
|
||||
# (`User = cfg.user`).
|
||||
users.users.nginx = {
|
||||
isSystemUser = true;
|
||||
group = "nginx";
|
||||
};
|
||||
networking.hosts."${bridgeIp}" = [
|
||||
baoName
|
||||
natsName
|
||||
|
|
@ -510,16 +517,20 @@ pkgs.testers.runNixOSTest {
|
|||
wantedBy = [ "multi-user.target" ];
|
||||
after = [ "network.target" ];
|
||||
serviceConfig = {
|
||||
User = "nginx";
|
||||
Group = "nginx";
|
||||
RuntimeDirectory = "logs-gateway-test";
|
||||
ExecStart = "${pkgs.nginx}/bin/nginx -c ${gatewayConf config} -g 'daemon off;'";
|
||||
ExecStart = "${pkgs.nginx}/bin/nginx -e stderr -c ${gatewayConf config} -g 'daemon off;'";
|
||||
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
|
||||
Restart = "on-failure";
|
||||
RestartSec = 2;
|
||||
};
|
||||
};
|
||||
|
||||
environment.etc."logs-gateway.conf".source = gatewayConf config;
|
||||
environment.systemPackages = [
|
||||
pkgs.kubectl
|
||||
pkgs.nginx
|
||||
pkgs.curl
|
||||
pkgs.jq
|
||||
pkgs.openssl
|
||||
|
|
@ -1095,7 +1106,7 @@ pkgs.testers.runNixOSTest {
|
|||
assert machine.succeed(f"cat {STORE_PASSWORD}").strip() == stored
|
||||
secret = machine.succeed(f"kubectl -n {NS} get secret swarm-victorialogs-credentials -o jsonpath='{{.data.store-auth}}' | base64 -d").strip()
|
||||
assert secret == stored
|
||||
assert machine.succeed(f"stat -c '%a %U %G' {STORE_PASSWORD} {STORE_CONF} {STORE_AUTH_DIR}").split("\n")[:3] == ["600 root root", "600 root root", "700 root root"]
|
||||
assert machine.succeed(f"stat -c '%a %U %G' {STORE_PASSWORD} {STORE_CONF} {STORE_AUTH_DIR}").split("\n")[:3] == ["600 root root", "640 root nginx", "750 root nginx"]
|
||||
assert stored not in machine.succeed(f"cat {MANIFESTS}/{VL}.yaml"), "the manifest holds no credential"
|
||||
# Insert, select, a delete route, the admin routes, the store's own
|
||||
# metrics and the UI answer 401 with no credential and with a caller's
|
||||
|
|
@ -1159,6 +1170,26 @@ pkgs.testers.runNixOSTest {
|
|||
assert machine.succeed(f"cat {STORE_CONF}") == saved
|
||||
wait_for_record(AGENT_TOKEN, "switch", "written before the switch")
|
||||
|
||||
with subtest("the gateway's user reads the credential file and not the password, and a file only root reads keeps nginx from loading its configuration"):
|
||||
# The stub gateway runs as the user the module's nginx unit runs as
|
||||
# (`User = cfg.user`), so what it includes has to be that user's to read.
|
||||
nginx_check = "runuser -u nginx -- ${pkgs.nginx}/bin/nginx -e stderr -t -c /etc/logs-gateway.conf 2>&1"
|
||||
assert machine.succeed("systemctl show --property=User --value logs-gateway").strip() == "nginx"
|
||||
machine.succeed(f"runuser -u nginx -- cat {STORE_CONF}")
|
||||
machine.fail(f"runuser -u nginx -- cat {STORE_PASSWORD}")
|
||||
machine.succeed(nginx_check)
|
||||
# Control: the same file owned and moded as it was, root's alone.
|
||||
machine.succeed(f"chown root:root {STORE_CONF} && chmod 0600 {STORE_CONF}")
|
||||
status, out = machine.execute(nginx_check)
|
||||
assert status != 0 and f'open() "{STORE_CONF}" failed (13: Permission denied)' in out, (status, out)
|
||||
# The unit finds the value right and the owner wrong, and puts it back.
|
||||
machine.succeed("systemctl restart swarm-bao-store-auth.service")
|
||||
store_credential_published()
|
||||
assert machine.succeed(f"stat -c '%a %U %G' {STORE_CONF}").strip() == "640 root nginx"
|
||||
machine.succeed(nginx_check)
|
||||
machine.succeed("systemctl reload logs-gateway")
|
||||
machine.wait_until_succeeds("systemctl is-active logs-gateway", timeout=30)
|
||||
|
||||
with subtest("a record posted to the ingest route and the store's own metrics come through the gateway, and a restarted pod still holds both records"):
|
||||
import base64
|
||||
|
||||
|
|
@ -1485,7 +1516,7 @@ pkgs.testers.runNixOSTest {
|
|||
assert machine.succeed(f"cat {VM_PASSWORD}").strip() == stored
|
||||
secret = machine.succeed(f"kubectl -n {NS} get secret swarm-victoriametrics-credentials -o jsonpath='{{.data.store-auth}}' | base64 -d").strip()
|
||||
assert secret == stored
|
||||
assert machine.succeed(f"stat -c '%a %U %G' {VM_PASSWORD} {VM_CONF}").split("\n")[:2] == ["600 root root", "600 root root"]
|
||||
assert machine.succeed(f"stat -c '%a %U %G' {VM_PASSWORD} {VM_CONF}").split("\n")[:2] == ["600 root root", "640 root nginx"]
|
||||
assert stored not in machine.succeed(f"cat {MANIFESTS}/{VM}.yaml"), "the manifest holds no credential"
|
||||
# Writes, imports, queries, exports, the routes that delete and
|
||||
# snapshot, the config and reload routes, the store's own metrics and
|
||||
|
|
|
|||
Loading…
Reference in a new issue