run the log store and its auth responders as pods
swarm-logs-auth and swarm-metrics-auth take --listen for a TCP address beside --socket. The log store is a Deployment on a static local volume that is its old container's data directory, behind a ClusterIP Service the gateway dials by name. Its two auth_request responders are one unpinned pod with a jwt role. The renderer gains a plain HTTP listener, a probe path, fsGroupChangePolicy, and one policy for a pod with both a listener and egress.
This commit is contained in:
parent
62c206169c
commit
dca4720b7b
1 changed files with 1385 additions and 746 deletions
|
|
@ -1,5 +1,6 @@
|
|||
# The swarm's log store: one VictoriaLogs for the whole swarm, in a
|
||||
# `swarm-victorialogs` nixos-container beside the metrics store it mirrors.
|
||||
# The swarm's log store: one VictoriaLogs for the whole swarm and the gateway's
|
||||
# `auth_request` responders for it, each a pod in the host's cluster
|
||||
# (./k8s/render.nix, docs/4-implementation/k8s/runtime.md).
|
||||
#
|
||||
# Why a store at all rather than reading journals directly: an agent can
|
||||
# verify that a unit was *launched* and never that it is *working*. The
|
||||
|
|
@ -14,13 +15,12 @@
|
|||
# Gateway vhost is authenticated, unlike the metrics store's: an operator
|
||||
# reaches this at `https://${cfg.domain}/` gated by the same `auth_request`
|
||||
# check against authelia that swarm-ui's own vhost uses (`swarmAuthRequest`
|
||||
# below, same shape as `swarm-ui.nix`'s) — see that file's copy for the full
|
||||
# rationale (forceSSL is load-bearing there too: authelia answers a plain-http
|
||||
# auth subrequest with 400, which `auth_request` cannot interpret as anything
|
||||
# but a broken check). The store's own listener is a unix socket in a
|
||||
# netns of its own and is unauthenticated, and the vhost is the ONLY way in: the
|
||||
# collector pushes through it too, at its own `= /insert/...` location, because
|
||||
# a swarm has one log store and the collector need not share a host with it.
|
||||
# below; `swarm-ui.nix` says why forceSSL is load-bearing: authelia answers a
|
||||
# plain-http auth subrequest with 400). The store's own listener is unauthenticated and takes
|
||||
# writes as well as reads. Its Service admits only the node's own traffic, and
|
||||
# the vhost, which dials it by name, is the ONLY way in: the collector pushes
|
||||
# through it too, at its own `= /insert/...` location, because the collector
|
||||
# need not share a host with the store.
|
||||
# ⚠️ That ingest location deliberately does not carry `swarmAuthRequest` — a
|
||||
# pusher handed its `error_page 401 =302` follows the redirect and POSTs at a
|
||||
# login page, which answers 200. See the location itself.
|
||||
|
|
@ -40,6 +40,8 @@ let
|
|||
networkCfg = config.services.hyperhive.network;
|
||||
hyperhiveCfg = config.services.hyperhive;
|
||||
gatewayCfg = hyperhiveCfg.gateway;
|
||||
k8sCfg = hyperhiveCfg.k8s;
|
||||
inherit (import ../images { inherit pkgs lib; }) mkServiceImage;
|
||||
|
||||
# Copy of `swarm-ui.nix`'s own `swarmAuthRequest` — not shared code because
|
||||
# this vhost needs exactly the two locations that reference it (`/` and the
|
||||
|
|
@ -75,74 +77,192 @@ let
|
|||
proxy_ignore_headers Cache-Control Expires Set-Cookie Vary;
|
||||
'';
|
||||
|
||||
# The agents' `auth_request` responder (`swarm-logs-auth`). Its socket's
|
||||
# directory holds that socket alone and is traverse-only for others, the
|
||||
# shape `swarm-controller.nix` gives its own.
|
||||
logsAuthUnit = "swarm-logs-auth";
|
||||
logsAuthSocket = "/run/${logsAuthUnit}/auth.sock";
|
||||
logsAuthCredential = id: "/run/credentials/${logsAuthUnit}.service/${id}";
|
||||
# Credential id → host path of the responder's store identity.
|
||||
logsAuthStoreFiles = {
|
||||
"bao-client.pem" = deployCfg.victorialogs.authBaoClientCertFile;
|
||||
"bao-client-key.pem" = deployCfg.victorialogs.authBaoClientKeyFile;
|
||||
}
|
||||
// lib.optionalAttrs (deployCfg.bao.serverCaFile != null) {
|
||||
"bao-ca.pem" = deployCfg.bao.serverCaFile;
|
||||
};
|
||||
# The store identity the responders' units name by credential id: the pod
|
||||
# logs in with its ServiceAccount token instead, and mounts the CA file.
|
||||
logsAuthStoreIds = [
|
||||
"bao-client.pem"
|
||||
"bao-client-key.pem"
|
||||
]
|
||||
++ lib.optional (deployCfg.bao.serverCaFile != null) "bao-ca.pem";
|
||||
baoCfg = hyperhiveCfg.swarm.bao;
|
||||
|
||||
# Grafana's bearer, checked by a second `swarm-metrics-auth` instance. It
|
||||
# reads the store with the same leaf as `swarm-logs-auth`, which is granted
|
||||
# the one extra path below. The same leaf as `grafanaBearerStorePath` in
|
||||
# ./swarm-victoriametrics.nix and `grafanaMetricsBearerLeaf` in ./swarm-bao.nix.
|
||||
grafanaAuthUnit = "swarm-logs-grafana-auth";
|
||||
grafanaAuthSocket = "/run/${grafanaAuthUnit}/auth.sock";
|
||||
grafanaAuthCredential = id: "/run/credentials/${grafanaAuthUnit}.service/${id}";
|
||||
grafanaBearerStorePath = "swarm/services/${hyperhiveCfg.swarm.grafana.oidc.clientId}/metrics/bearer";
|
||||
|
||||
# A netns of its own on the hive bridge, as the cache's container has
|
||||
# (./swarm-attic.nix): the forwarder below dials the swarm collector, authelia
|
||||
# and the store by name, so the container keeps an interface and the bridge's
|
||||
# resolver. The store listens on a unix socket only, so no host port is
|
||||
# bound. Grafana reaches the store by its domain through the gateway.
|
||||
privateNetwork = true;
|
||||
# The workloads, as the cluster names them: the store, and the one pod that
|
||||
# holds both responders. Each has a Service of its name, which the gateway
|
||||
# dials. `legacyContainer` is the name of the container the store ran in
|
||||
# before it ran in the cluster, whose data directory the volume is.
|
||||
storeService = "swarm-victorialogs";
|
||||
authService = "swarm-logs-auth";
|
||||
grafanaAuthService = "swarm-logs-grafana-auth";
|
||||
legacyContainer = "swarm-victorialogs";
|
||||
storePort = 9428;
|
||||
authPort = 8081;
|
||||
grafanaAuthPort = 8082;
|
||||
# The Grafana responder's own `/healthz` and `/readyz`, beside the logs
|
||||
# responder's on 8080 in the same pod.
|
||||
grafanaAuthHealthPort = 8083;
|
||||
dataDir = "/var/lib/victorialogs";
|
||||
hostDataDir = "/var/lib/nixos-containers/${legacyContainer}${dataDir}";
|
||||
|
||||
# The unix socket the gateway's nginx connects to. The directory's mode is
|
||||
# what keeps it private, so it holds this one socket.
|
||||
socketDir = "/run/swarm-victorialogs";
|
||||
socketPath = "${socketDir}/victorialogs.sock";
|
||||
socketGroup = "gateway-nginx";
|
||||
# Only the number crosses into the container, which knows the group by name.
|
||||
nginxGid = config.ids.gids.nginx;
|
||||
# The upstream every location below forwards to. A location that adds a path
|
||||
# writes `<upstream>:<path>`; one with no path leaves the request's own.
|
||||
upstream = "http://unix:${socketPath}";
|
||||
serviceHost = service: "${service}.${k8sCfg.namespace}.svc.${deployCfg.k8s.clusterDomain}";
|
||||
|
||||
# The container's journal forwarder pushes to its own receiver on the swarm
|
||||
# collector, the route `serviceProducers.swarm-victorialogs` in
|
||||
# ./swarm-otel-service.nix declares.
|
||||
forwarderClientId = cfg.otel.clientId;
|
||||
forwarderEndpoint = "https://${hyperhiveCfg.swarm.otel.domain}/${cfg.machine}";
|
||||
forwarderUnit = "swarm-journal-forwarder-oidc";
|
||||
# Loopback, in the container's own netns.
|
||||
forwarderTelemetryPort = 8897;
|
||||
forwarderCredential = id: "/run/credentials/${forwarderUnit}.service/${id}";
|
||||
# The responder's store identity, handed into the container as system
|
||||
# credentials for the forwarder's fetch unit. The host units keep reading
|
||||
# the same files.
|
||||
forwarderStoreFiles = lib.filterAttrs (_: source: source != null) logsAuthStoreFiles;
|
||||
# nixos-container expands `extraFlags` unquoted, so a source path must be
|
||||
# one word.
|
||||
oneWord = path: builtins.match ".*[[:space:]].*" path == null;
|
||||
# What a location needs to dial a Service: the host's resolver, which hands
|
||||
# the cluster's domain to CoreDNS, and the name in a variable, so that nginx
|
||||
# looks it up per request. A name written into `proxy_pass` gets one lookup
|
||||
# when the configuration loads, which fails the load while the cluster is
|
||||
# down. A `proxy_pass` with a variable and a URI part sends that URI and not
|
||||
# the request's own, so the store's locations write none. Each role has a
|
||||
# variable of its own, so that an auth subrequest's `set` can never change
|
||||
# where the request it checks is sent.
|
||||
viaService = variable: service: port: ''
|
||||
resolver ${networkCfg.bridgeIp} valid=10s ipv6=off;
|
||||
set ${variable} ${serviceHost service}:${toString port};
|
||||
'';
|
||||
storeVariable = "$swarm_logs_store_upstream";
|
||||
upstream = "http://${storeVariable}";
|
||||
agentAuthVariable = "$swarm_logs_agent_auth_upstream";
|
||||
grafanaAuthVariable = "$swarm_logs_grafana_auth_upstream";
|
||||
|
||||
rootfsCheck = import ./lib/container-rootfs-check.nix { inherit config; };
|
||||
|
||||
# The forwarder reaches the swarm collector and authelia by name over TLS; in
|
||||
# self-signed mode their leaves chain to the hive CA.
|
||||
caTrust = import ./lib/hive-ca-trust.nix {
|
||||
inherit lib gatewayCfg;
|
||||
tlsCfg = deployCfg.hive-controller.tls;
|
||||
# The store's unit: the daemon, its flags and its state directory, which is
|
||||
# all the pod is rendered from. Nothing evaluates a systemd unit for it.
|
||||
storeUnit = {
|
||||
serviceConfig = {
|
||||
ExecStart = lib.concatStringsSep " " [
|
||||
"${deployCfg.victorialogs.package}/bin/victoria-logs"
|
||||
"-storageDataPath=${dataDir}"
|
||||
"-httpListenAddr=:${toString storePort}"
|
||||
"-retentionPeriod=${deployCfg.victorialogs.retentionPeriod}"
|
||||
];
|
||||
StateDirectory = "victorialogs";
|
||||
};
|
||||
environment = { };
|
||||
path = [ ];
|
||||
};
|
||||
|
||||
# The agents' `auth_request` responder, with the cert and key ids that the
|
||||
# pod's login drops.
|
||||
authCredential = id: "/run/credentials/${authService}.service/${id}";
|
||||
authUnit = {
|
||||
serviceConfig = {
|
||||
ExecStart = lib.concatStringsSep " " [
|
||||
"${deployCfg.victorialogs.authPackage}/bin/swarm-logs-auth"
|
||||
"--listen 0.0.0.0:${toString authPort}"
|
||||
# The role ./swarm-bao.nix writes on the store's `cert` mount.
|
||||
"--store-cert-role swarm-logs-auth"
|
||||
"--surface logs"
|
||||
"--verdict-ttl ${toString deployCfg.victorialogs.authVerdictTtl}"
|
||||
];
|
||||
LoadCredential = logsAuthStoreIds;
|
||||
};
|
||||
environment = {
|
||||
BAO_ADDR = "https://${baoCfg.domain}:${toString baoCfg.port}";
|
||||
BAO_CLIENT_CERT = authCredential "bao-client.pem";
|
||||
BAO_CLIENT_KEY = authCredential "bao-client-key.pem";
|
||||
}
|
||||
// lib.optionalAttrs (builtins.elem "bao-ca.pem" logsAuthStoreIds) {
|
||||
BAO_CACERT = authCredential "bao-ca.pem";
|
||||
};
|
||||
path = [ ];
|
||||
};
|
||||
|
||||
# What the responders' pod holds besides `swarm-logs-auth`: Grafana's
|
||||
# responder, the metrics store's binary pointed at the bearer Grafana's
|
||||
# datasources share. It logs in under the pod's one ServiceAccount, with the
|
||||
# environment and mounts of the first container.
|
||||
authPod =
|
||||
{
|
||||
env,
|
||||
volumeMounts,
|
||||
metaFor,
|
||||
securityContext,
|
||||
}:
|
||||
let
|
||||
image = mkServiceImage {
|
||||
name = grafanaAuthService;
|
||||
package = deployCfg.victorialogs.grafanaAuthPackage;
|
||||
bin = "swarm-metrics-auth";
|
||||
};
|
||||
probe = path: {
|
||||
httpGet = {
|
||||
inherit path;
|
||||
port = "health";
|
||||
};
|
||||
periodSeconds = 10;
|
||||
};
|
||||
in
|
||||
{
|
||||
containers = [
|
||||
{
|
||||
name = "grafana-auth";
|
||||
image = "${image.imageName}:${image.imageTag}";
|
||||
imagePullPolicy = "IfNotPresent";
|
||||
args = [
|
||||
"--listen"
|
||||
"0.0.0.0:${toString grafanaAuthPort}"
|
||||
"--store-cert-role"
|
||||
"swarm-logs-auth"
|
||||
"--store-path"
|
||||
grafanaBearerStorePath
|
||||
];
|
||||
env = lib.mapAttrsToList (name: value: { inherit name value; }) (
|
||||
lib.filterAttrs (n: _: lib.hasPrefix "BAO_" n) env
|
||||
// {
|
||||
SWARM_METRICS_AUTH_HEALTH_ADDR = "0.0.0.0:${toString grafanaAuthHealthPort}";
|
||||
}
|
||||
);
|
||||
ports = [
|
||||
{
|
||||
name = "api";
|
||||
containerPort = grafanaAuthPort;
|
||||
}
|
||||
{
|
||||
name = "health";
|
||||
containerPort = grafanaAuthHealthPort;
|
||||
}
|
||||
];
|
||||
livenessProbe = probe "/healthz";
|
||||
readinessProbe = probe "/readyz";
|
||||
volumeMounts = lib.filter (
|
||||
m:
|
||||
lib.elem m.name [
|
||||
"credentials"
|
||||
"bao-token"
|
||||
"tmp"
|
||||
]
|
||||
) volumeMounts;
|
||||
inherit securityContext;
|
||||
}
|
||||
];
|
||||
|
||||
# The pod's policy, from ./k8s/render.nix, covers this port too: only the
|
||||
# node reaches it.
|
||||
objects."${grafanaAuthService}/service" = {
|
||||
apiVersion = "v1";
|
||||
kind = "Service";
|
||||
metadata = metaFor authService // {
|
||||
name = grafanaAuthService;
|
||||
};
|
||||
spec = {
|
||||
type = "ClusterIP";
|
||||
selector = (metaFor authService).labels;
|
||||
ports = [
|
||||
{
|
||||
name = "api";
|
||||
protocol = "TCP";
|
||||
port = grafanaAuthPort;
|
||||
targetPort = "api";
|
||||
}
|
||||
];
|
||||
};
|
||||
};
|
||||
|
||||
images = [ image ];
|
||||
};
|
||||
in
|
||||
{
|
||||
# What the store IS from any hive's point of view, the name it answers on, is
|
||||
|
|
@ -151,10 +271,10 @@ in
|
|||
# runs it and live under `deploy.*`.
|
||||
|
||||
# Retention is a property of the store this host runs, not something the
|
||||
# swarm has to agree on: it is read only where the container is defined,
|
||||
# and a hive that is a *client* of the log store never consults it. That
|
||||
# makes it a `deploy.*` value by the same rule as the seal on the secret
|
||||
# store — options on the auto-deployed service itself.
|
||||
# swarm has to agree on: it is read only where the pod is defined, and a hive
|
||||
# that is a *client* of the log store never consults it. That makes it a
|
||||
# `deploy.*` value by the same rule as the seal on the secret store —
|
||||
# options on the auto-deployed service itself.
|
||||
options.services.hyperhive.deploy.victorialogs.package = lib.mkOption {
|
||||
type = lib.types.package;
|
||||
default = pkgs.victorialogs;
|
||||
|
|
@ -271,6 +391,68 @@ in
|
|||
'';
|
||||
};
|
||||
|
||||
options.services.hyperhive.deploy.victorialogs.unit = lib.mkOption {
|
||||
type = lib.types.raw;
|
||||
readOnly = true;
|
||||
internal = true;
|
||||
default = storeUnit;
|
||||
description = ''
|
||||
The store's unit, the `serviceConfig`, `environment` and `path` that
|
||||
../host-modules/k8s/render.nix renders its pod from.
|
||||
'';
|
||||
};
|
||||
|
||||
options.services.hyperhive.deploy.victorialogs.authUnit = lib.mkOption {
|
||||
type = lib.types.raw;
|
||||
readOnly = true;
|
||||
internal = true;
|
||||
default = authUnit;
|
||||
description = ''
|
||||
The agents' responder's unit, which the first container of the
|
||||
responders' pod is rendered from.
|
||||
'';
|
||||
};
|
||||
|
||||
options.services.hyperhive.deploy.victorialogs.pod = lib.mkOption {
|
||||
type = lib.types.raw;
|
||||
readOnly = true;
|
||||
internal = true;
|
||||
default = authPod;
|
||||
description = ''
|
||||
What the responders' pod holds besides the agents' responder: a function
|
||||
of that container that ../host-modules/k8s/render.nix calls, giving
|
||||
Grafana's responder, its Service and its image.
|
||||
'';
|
||||
};
|
||||
|
||||
options.services.hyperhive.deploy.victorialogs.servicePort = lib.mkOption {
|
||||
type = lib.types.port;
|
||||
readOnly = true;
|
||||
internal = true;
|
||||
default = storePort;
|
||||
description = "The port the store's Service publishes.";
|
||||
};
|
||||
|
||||
options.services.hyperhive.deploy.victorialogs.authServicePort = lib.mkOption {
|
||||
type = lib.types.port;
|
||||
readOnly = true;
|
||||
internal = true;
|
||||
default = authPort;
|
||||
description = "The port the agents' responder's Service publishes.";
|
||||
};
|
||||
|
||||
options.services.hyperhive.deploy.victorialogs.hostDataDir = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
readOnly = true;
|
||||
internal = true;
|
||||
default = hostDataDir;
|
||||
description = ''
|
||||
The store's data directory on the host, which the pod's volume is: the
|
||||
directory of the container the store ran in. A host that never ran it gets
|
||||
the directory made.
|
||||
'';
|
||||
};
|
||||
|
||||
config = lib.mkIf deployCfg.victorialogs.enable {
|
||||
assertions = [
|
||||
{
|
||||
|
|
@ -289,37 +471,6 @@ in
|
|||
both options.
|
||||
'';
|
||||
}
|
||||
{
|
||||
assertion = lib.all oneWord (lib.attrValues forwarderStoreFiles);
|
||||
message = ''
|
||||
The log store responder's store identity paths must not contain
|
||||
whitespace: nspawn's --load-credential receives them through
|
||||
nixos-container's unquoted extra flags.
|
||||
'';
|
||||
}
|
||||
{
|
||||
assertion =
|
||||
(hyperhiveCfg.swarm.otel.serviceProducers.${cfg.machine}.clientId or null) == forwarderClientId;
|
||||
message = ''
|
||||
services.hyperhive.swarm.otel.serviceProducers has no entry
|
||||
"${cfg.machine}" with clientId "${forwarderClientId}", so the swarm
|
||||
collector has no receiver for the log store's journal forwarder.
|
||||
Declare it in swarm-otel-service.nix.
|
||||
'';
|
||||
}
|
||||
];
|
||||
|
||||
# nspawn fails to start the container when a credential source is
|
||||
# missing, so where this host mints the leaf the container pulls the
|
||||
# minting unit in and waits for it, as ./swarm-builder.nix's does. The CA
|
||||
# ordering keeps the trust bundle's bind source there first.
|
||||
systemd.services."container@${cfg.machine}" = lib.mkMerge [
|
||||
caTrust.containerOrdering
|
||||
(rootfsCheck.warnOnly cfg.machine)
|
||||
{
|
||||
after = [ "swarm-bao-pki.service" ];
|
||||
wants = [ "swarm-bao-pki.service" ];
|
||||
}
|
||||
];
|
||||
|
||||
# Which check a `/select/logsql/` request gets. `auth_request` takes a
|
||||
|
|
@ -353,14 +504,14 @@ in
|
|||
# `swarm-victoriametrics.nix`).
|
||||
services.hyperhive.gateway.localNames = [ cfg.domain ];
|
||||
|
||||
# This host serves the vhost, and the container behind it resolves
|
||||
# This host serves the vhost and resolves the cluster's names for it
|
||||
# through the hive's dnsmasq.
|
||||
services.hyperhive.gateway.enable = lib.mkDefault true;
|
||||
services.hyperhive.gateway.dns.enable = lib.mkDefault true;
|
||||
|
||||
# Authenticated front door onto the store's socket — see the
|
||||
# file-top comment for why this is safe to add without touching the
|
||||
# store's own (still unauthenticated, socket-only) listener at all.
|
||||
# Authenticated front door onto the store's Service — see the file-top
|
||||
# comment for why the store's own unauthenticated listener is reached
|
||||
# through this vhost alone.
|
||||
# `forceSSL`: same asymmetry `swarm-ui.nix` documents —
|
||||
# authelia answers a plain-http auth subrequest with 400, which
|
||||
# `auth_request` cannot read as anything but a broken check, so this
|
||||
|
|
@ -370,8 +521,8 @@ in
|
|||
forceSSL = true;
|
||||
locations = {
|
||||
"/" = {
|
||||
proxyPass = "${upstream}:/";
|
||||
extraConfig = swarmAuthRequest;
|
||||
proxyPass = upstream;
|
||||
extraConfig = viaService storeVariable storeService storePort + swarmAuthRequest;
|
||||
};
|
||||
|
||||
# The swarm collector's ingest route. `=` so it outranks the `/`
|
||||
|
|
@ -389,8 +540,9 @@ in
|
|||
# note on `swarmAuthRequest` above), so naming it here is required
|
||||
# rather than redundant.
|
||||
"= /insert/opentelemetry/v1/logs" = {
|
||||
proxyPass = "${upstream}:/insert/opentelemetry/v1/logs";
|
||||
proxyPass = upstream;
|
||||
extraConfig = ''
|
||||
${viaService storeVariable storeService storePort}
|
||||
auth_request /__swarm_logs_ingest_authz;
|
||||
'';
|
||||
};
|
||||
|
|
@ -425,6 +577,7 @@ in
|
|||
# the store as the caller sent them.
|
||||
proxyPass = upstream;
|
||||
extraConfig = ''
|
||||
${viaService storeVariable storeService storePort}
|
||||
auth_request /__swarm_logs_auth;
|
||||
'';
|
||||
};
|
||||
|
|
@ -440,8 +593,9 @@ in
|
|||
# authelia's rules for this URL admit the collector's client and
|
||||
# deny every other subject (see `metricsRules` in ./swarm-authelia.nix).
|
||||
"= /metrics" = {
|
||||
proxyPass = "${upstream}:/metrics";
|
||||
proxyPass = upstream;
|
||||
extraConfig = ''
|
||||
${viaService storeVariable storeService storePort}
|
||||
auth_request /__hive_authelia;
|
||||
'';
|
||||
};
|
||||
|
|
@ -463,8 +617,9 @@ in
|
|||
# statuses `auth_request` passes through are 401 and 403, and
|
||||
# `swarm-logs` retries a 401.
|
||||
"= /__swarm_logs_agent_auth" = {
|
||||
proxyPass = "http://unix:${logsAuthSocket}:/";
|
||||
proxyPass = "http://${agentAuthVariable}/";
|
||||
extraConfig = ''
|
||||
${viaService agentAuthVariable authService authPort}
|
||||
internal;
|
||||
limit_req zone=swarm_logs_agent_auth burst=${toString deployCfg.victorialogs.authBurst} nodelay;
|
||||
limit_req_status 403;
|
||||
|
|
@ -477,8 +632,9 @@ in
|
|||
# `swarm-metrics-auth`. The responder reads the store at most once per
|
||||
# its cache TTL whatever the bearers, so no rate limit sits in front.
|
||||
"= /__swarm_logs_grafana_auth" = {
|
||||
proxyPass = "http://unix:${grafanaAuthSocket}:/";
|
||||
proxyPass = "http://${grafanaAuthVariable}/";
|
||||
extraConfig = ''
|
||||
${viaService grafanaAuthVariable grafanaAuthService grafanaAuthPort}
|
||||
internal;
|
||||
proxy_pass_request_body off;
|
||||
proxy_set_header Content-Length "";
|
||||
|
|
@ -529,241 +685,14 @@ in
|
|||
};
|
||||
};
|
||||
|
||||
# The agents' check on `/select/logsql/`. On the host rather than in the
|
||||
# store's container: it needs nothing of the store, only nginx's reach to
|
||||
# its socket and its own store identity.
|
||||
users.users.${logsAuthUnit} = {
|
||||
isSystemUser = true;
|
||||
group = logsAuthUnit;
|
||||
description = "swarm log store auth_request responder";
|
||||
};
|
||||
users.groups.${logsAuthUnit} = { };
|
||||
# The store's volume is its old container's data directory, which a host
|
||||
# that never ran the container does not have. The rule leaves an existing
|
||||
# directory's owner and mode alone: the pod's group is given the tree when
|
||||
# it starts.
|
||||
systemd.tmpfiles.rules = [ "d ${hostDataDir} - - - -" ];
|
||||
|
||||
systemd.services.${logsAuthUnit} = {
|
||||
description = "swarm log store auth_request responder for agent credentials";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
# Where the store's PKI is minted on this host, the leaf is one of its
|
||||
# files. Ordering only: elsewhere the unit does not exist.
|
||||
after = [
|
||||
"network.target"
|
||||
"swarm-bao-pki.service"
|
||||
];
|
||||
serviceConfig = {
|
||||
ExecStart = lib.concatStringsSep " " [
|
||||
"${deployCfg.victorialogs.authPackage}/bin/swarm-logs-auth"
|
||||
"--socket ${logsAuthSocket}"
|
||||
# The role ./swarm-bao.nix writes on the store's `cert` mount.
|
||||
"--store-cert-role swarm-logs-auth"
|
||||
"--surface logs"
|
||||
"--verdict-ttl ${toString deployCfg.victorialogs.authVerdictTtl}"
|
||||
];
|
||||
# Paths only: `argv` is world-readable via /proc/<pid>/cmdline.
|
||||
LoadCredential = lib.mapAttrsToList (id: path: "${id}:${path}") logsAuthStoreFiles;
|
||||
# ⚠️ A static user, never `DynamicUser`: with `RuntimeDirectoryPreserve`
|
||||
# systemd puts a dynamic user's runtime directory under
|
||||
# `/run/private/` (0700 root:root), which nginx cannot traverse.
|
||||
User = logsAuthUnit;
|
||||
Group = logsAuthUnit;
|
||||
RuntimeDirectory = logsAuthUnit;
|
||||
# Traverse-only for others, so nginx reaches the socket path.
|
||||
RuntimeDirectoryMode = "0751";
|
||||
# The path never vanishes from under a running nginx; the responder
|
||||
# unlinks a stale socket as it starts.
|
||||
RuntimeDirectoryPreserve = "yes";
|
||||
Restart = "on-failure";
|
||||
RestartSec = "5s";
|
||||
SyslogIdentifier = logsAuthUnit;
|
||||
ProtectSystem = "strict";
|
||||
ProtectHome = true;
|
||||
PrivateTmp = "disconnected";
|
||||
PrivateDevices = true;
|
||||
NoNewPrivileges = true;
|
||||
RestrictSUIDSGID = true;
|
||||
RemoveIPC = true;
|
||||
};
|
||||
environment = {
|
||||
BAO_ADDR = "https://${baoCfg.domain}:${toString baoCfg.port}";
|
||||
BAO_CLIENT_CERT = logsAuthCredential "bao-client.pem";
|
||||
BAO_CLIENT_KEY = logsAuthCredential "bao-client-key.pem";
|
||||
}
|
||||
// lib.optionalAttrs (logsAuthStoreFiles ? "bao-ca.pem") {
|
||||
BAO_CACERT = logsAuthCredential "bao-ca.pem";
|
||||
};
|
||||
};
|
||||
|
||||
# Grafana's check on `/select/logsql/`: the metrics store's responder
|
||||
# binary, pointed at the bearer Grafana's datasources share.
|
||||
users.users.${grafanaAuthUnit} = {
|
||||
isSystemUser = true;
|
||||
group = grafanaAuthUnit;
|
||||
description = "swarm log store auth_request responder for Grafana's bearer";
|
||||
};
|
||||
users.groups.${grafanaAuthUnit} = { };
|
||||
|
||||
systemd.services.${grafanaAuthUnit} = {
|
||||
description = "swarm log store auth_request responder for Grafana's bearer";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
after = [
|
||||
"network.target"
|
||||
"swarm-bao-pki.service"
|
||||
];
|
||||
serviceConfig = {
|
||||
ExecStart = lib.concatStringsSep " " [
|
||||
"${deployCfg.victorialogs.grafanaAuthPackage}/bin/swarm-metrics-auth"
|
||||
"--socket ${grafanaAuthSocket}"
|
||||
"--store-cert-role swarm-logs-auth"
|
||||
"--store-path ${grafanaBearerStorePath}"
|
||||
];
|
||||
LoadCredential = lib.mapAttrsToList (id: path: "${id}:${path}") logsAuthStoreFiles;
|
||||
# ⚠️ A static user, never `DynamicUser`: see the `swarm-logs-auth` unit above.
|
||||
User = grafanaAuthUnit;
|
||||
Group = grafanaAuthUnit;
|
||||
RuntimeDirectory = grafanaAuthUnit;
|
||||
RuntimeDirectoryMode = "0751";
|
||||
RuntimeDirectoryPreserve = "yes";
|
||||
Restart = "on-failure";
|
||||
RestartSec = "5s";
|
||||
SyslogIdentifier = grafanaAuthUnit;
|
||||
ProtectSystem = "strict";
|
||||
ProtectHome = true;
|
||||
PrivateTmp = "disconnected";
|
||||
PrivateDevices = true;
|
||||
NoNewPrivileges = true;
|
||||
RestrictSUIDSGID = true;
|
||||
RemoveIPC = true;
|
||||
};
|
||||
environment = {
|
||||
BAO_ADDR = "https://${baoCfg.domain}:${toString baoCfg.port}";
|
||||
BAO_CLIENT_CERT = grafanaAuthCredential "bao-client.pem";
|
||||
BAO_CLIENT_KEY = grafanaAuthCredential "bao-client-key.pem";
|
||||
}
|
||||
// lib.optionalAttrs (logsAuthStoreFiles ? "bao-ca.pem") {
|
||||
BAO_CACERT = grafanaAuthCredential "bao-ca.pem";
|
||||
};
|
||||
};
|
||||
|
||||
# The socket directory is the gateway's way in: its group is nginx's.
|
||||
systemd.tmpfiles.rules = [ "d ${socketDir} 0770 root ${toString nginxGid} - -" ];
|
||||
|
||||
# The hive bridge carries the container's DHCP lease and its resolver.
|
||||
# The pods reach the secret store through the hive bridge.
|
||||
services.hyperhive.network.enable = lib.mkDefault true;
|
||||
|
||||
containers.${cfg.machine} = {
|
||||
autoStart = true;
|
||||
ephemeral = false;
|
||||
# No `--link-journal=host`: the journal stays in the guest, where the
|
||||
# container's own forwarder reads it (../container-modules/swarm-journal-forwarder.nix).
|
||||
extraFlags = lib.mapAttrsToList (
|
||||
id: source: "--load-credential=${id}:${source}"
|
||||
) forwarderStoreFiles;
|
||||
inherit privateNetwork;
|
||||
hostBridge = networkCfg.bridgeName;
|
||||
# A bind mount, not a path both sides know: a container's `/run` is an
|
||||
# nspawn tmpfs the host cannot see into. Connecting to a socket is a
|
||||
# write to it.
|
||||
bindMounts = {
|
||||
${socketDir} = {
|
||||
hostPath = socketDir;
|
||||
isReadOnly = false;
|
||||
};
|
||||
}
|
||||
// caTrust.bindMount;
|
||||
|
||||
config =
|
||||
{ pkgs, ... }:
|
||||
{
|
||||
imports = [
|
||||
../container-modules/swarm-container.nix
|
||||
../container-modules/swarm-journal-forwarder.nix
|
||||
(import ../container-modules/swarm-container-resolver.nix {
|
||||
inherit (networkCfg) bridgeIp;
|
||||
# The forwarder and its secret fetch dial the swarm collector,
|
||||
# authelia and the store by name too.
|
||||
dnsConsumers = [
|
||||
"victorialogs.service"
|
||||
"opentelemetry-collector.service"
|
||||
"${forwarderUnit}.service"
|
||||
];
|
||||
})
|
||||
# Go: `SSL_CERT_FILE` replaces its trust store, for the swarm
|
||||
# collector's and authelia's names.
|
||||
(caTrust.trustBundle {
|
||||
inherit pkgs;
|
||||
name = cfg.machine;
|
||||
consumers = [ "opentelemetry-collector" ];
|
||||
})
|
||||
];
|
||||
|
||||
services.hyperhive.swarmContainer = { inherit privateNetwork; };
|
||||
|
||||
# The bridge address comes from the gateway's DHCP. `swarm-bridge-dns`
|
||||
# owns resolv.conf, so dhcpcd must not write it over that unit.
|
||||
networking.interfaces.eth0.useDHCP = true;
|
||||
networking.dhcpcd.extraConfig = "nohook resolv.conf";
|
||||
|
||||
# The socket's group is the store's primary group, so that group is
|
||||
# nginx's gid. A static user, not nixpkgs' DynamicUser: a dynamic
|
||||
# user's group is allocated per start, and nginx is never in it.
|
||||
users.groups.${socketGroup}.gid = nginxGid;
|
||||
users.users.victorialogs = {
|
||||
isSystemUser = true;
|
||||
group = socketGroup;
|
||||
};
|
||||
systemd.services.victorialogs = {
|
||||
serviceConfig = {
|
||||
DynamicUser = lib.mkForce false;
|
||||
User = "victorialogs";
|
||||
Group = socketGroup;
|
||||
# The store creates its socket under the process umask, so `0007`
|
||||
# makes it `0770` and nginx's group can connect.
|
||||
UMask = "0007";
|
||||
};
|
||||
# nixpkgs' readiness loop curls a TCP URL, which no `unix:` address
|
||||
# answers.
|
||||
postStart = lib.mkForce ''
|
||||
until ${lib.getExe pkgs.curl} -s -o /dev/null --unix-socket ${socketPath} http://localhost/ping; do
|
||||
sleep 1;
|
||||
done
|
||||
'';
|
||||
};
|
||||
|
||||
# Authenticated as the forwarder's own OIDC client, with the
|
||||
# responder's store identity: one identity for the whole container.
|
||||
# It is a system credential, so the fetch unit loads it.
|
||||
services.hyperhive.swarmJournalForwarder = {
|
||||
enable = true;
|
||||
serviceName = "victorialogs";
|
||||
endpoint = forwarderEndpoint;
|
||||
clientId = forwarderClientId;
|
||||
tokenUrl = "${toString hyperhiveCfg.swarm.authelia.url}/api/oidc/token";
|
||||
telemetryPort = forwarderTelemetryPort;
|
||||
store = {
|
||||
package = deployCfg.bao.package;
|
||||
addr = "https://${baoCfg.domain}:${toString baoCfg.port}";
|
||||
secretPath = "secret/swarm/services/${forwarderClientId}/oidc/client";
|
||||
clientCertFile = forwarderCredential "bao-client.pem";
|
||||
clientKeyFile = forwarderCredential "bao-client-key.pem";
|
||||
caFile = if forwarderStoreFiles ? "bao-ca.pem" then forwarderCredential "bao-ca.pem" else null;
|
||||
};
|
||||
};
|
||||
systemd.services.${forwarderUnit}.serviceConfig.LoadCredential = lib.attrNames forwarderStoreFiles;
|
||||
|
||||
services.victorialogs = {
|
||||
enable = true;
|
||||
package = deployCfg.victorialogs.package;
|
||||
|
||||
# ⚠️ A SOCKET, not upstream's default of every interface: this
|
||||
# endpoint accepts writes as well as reads. Nothing authenticates it
|
||||
# either — upstream offers one basic-auth pair
|
||||
# (`-httpAuth.username` / `-httpAuth.password`) and this module sets
|
||||
# neither. The socket's directory mode is the boundary: only the
|
||||
# gateway's group can connect.
|
||||
listenAddress = "unix:${socketPath}";
|
||||
|
||||
extraOptions = [ "-retentionPeriod=${deployCfg.victorialogs.retentionPeriod}" ];
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
# 🔑 OTLP ingest is served at `/insert/opentelemetry/v1/logs`, measured
|
||||
|
|
@ -780,7 +709,7 @@ in
|
|||
# code is not evidence.
|
||||
#
|
||||
# ⚠️ Like the metrics store's, that endpoint is unauthenticated — which is
|
||||
# why `listenAddress` above is a socket and why nothing reaches it except
|
||||
# through the gateway, where the ingest route is authenticated. (This module
|
||||
# declares a vhost.)
|
||||
# why the Service's policy admits only the node and why nothing reaches it
|
||||
# except through the gateway, where the ingest route is authenticated. (This
|
||||
# module declares a vhost.)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue