require k8s: swarm services with a pod run in the cluster only
This commit is contained in:
parent
7819ed2dbd
commit
2e1f037f25
1 changed files with 1044 additions and 3125 deletions
|
|
@ -35,7 +35,6 @@ let
|
|||
|
||||
tlsCfg = config.services.hyperhive.deploy.hive-controller.tls;
|
||||
gatewayCfg = config.services.hyperhive.gateway;
|
||||
rootfsCheck = import ./lib/container-rootfs-check.nix { inherit config; };
|
||||
|
||||
caTrust = import ./lib/hive-ca-trust.nix { inherit lib tlsCfg gatewayCfg; };
|
||||
# The responder introspects authelia over https BY NAME. Its HTTP client is
|
||||
|
|
@ -60,17 +59,9 @@ let
|
|||
# host is the only place both filesystems are addressable.
|
||||
secretDirInContainer = "/var/lib/swarm-nats-auth";
|
||||
inContainer = name: "${secretDirInContainer}/${name}";
|
||||
secretDir = "/var/lib/nixos-containers/${machine}${secretDirInContainer}";
|
||||
hostPath = name: "${secretDir}/${name}";
|
||||
|
||||
introspectionUrl = "${toString autheliaUrl}/api/oidc/introspection";
|
||||
|
||||
# Host seed files `ensure-callout-keys` stores when the secret store holds
|
||||
# no seed for that key, and removes once it holds one. Never written here.
|
||||
importSeedDir = "/var/lib/swarm-nats-callout";
|
||||
importUserSeed = "${importSeedDir}/callout-user.seed";
|
||||
importIssuerSeed = "${importSeedDir}/issuer.seed";
|
||||
|
||||
# The runtime config directory: wrapper, settings symlink and fragment.
|
||||
# World-readable is correct (everything in it is public) and it is not in
|
||||
# the 0700 responder secret dir, which the `nats` user cannot traverse.
|
||||
|
|
@ -82,7 +73,6 @@ let
|
|||
# eval: the wrapper renders perfectly and the server refuses to start.
|
||||
runtimeDir = "/var/lib/nats-callout";
|
||||
runtimeWrapper = "${runtimeDir}/nats.conf";
|
||||
hostRuntimeDir = "/var/lib/nixos-containers/${machine}${runtimeDir}";
|
||||
|
||||
natsFormat = pkgs.formats.json { };
|
||||
|
||||
|
|
@ -193,7 +183,7 @@ let
|
|||
};
|
||||
|
||||
# The callout's three public keys: where the store keeps each, the file
|
||||
# `swarm-bao-nats-callout-xkey` leaves it in, the placeholder it fills in
|
||||
# the pod's `callout-config` init container leaves it in, the placeholder it fills in
|
||||
# `calloutTemplate`, and the shape `renderConfig` checks it has. The store
|
||||
# paths are `swarm-nats-auth`'s `xkey::PUBLIC_PATH` and the `public_path` of
|
||||
# its `nkey::ISSUER` and `nkey::USER`, readable by the `swarm-nats-auth` role
|
||||
|
|
@ -260,7 +250,7 @@ let
|
|||
|
||||
${lib.concatMapStrings (k: ''
|
||||
if [ ! -s "$d/${k.file}" ]; then
|
||||
echo "${runtimeDir}/${k.file} is missing: swarm-bao-nats-callout-xkey on the host fetches it from the secret store, and the queue does not start without it" >&2
|
||||
echo "${runtimeDir}/${k.file} is missing: the pod's callout-config init container fetches it from the secret store, and the queue does not start without it" >&2
|
||||
exit 1
|
||||
fi
|
||||
${k.var}="$(cat "$d/${k.file}")"
|
||||
|
|
@ -300,7 +290,6 @@ let
|
|||
tlsCertPath = "${tlsDir}/cert.pem";
|
||||
tlsKeyPath = "${tlsDir}/key.pem";
|
||||
tlsKeyCredential = "tls-key";
|
||||
tlsKeyCredentialPath = "/run/credentials/nats.service/${tlsKeyCredential}";
|
||||
|
||||
# The gateway's copy of that leaf, for the `/metrics` vhost below: the key
|
||||
# here is 0640 root:nginx, so nginx reads it while the queue's own stays
|
||||
|
|
@ -327,10 +316,6 @@ let
|
|||
};
|
||||
authCredential = id: "/run/credentials/swarm-nats-auth.service/${id}";
|
||||
|
||||
atomicWriteSecret = import ./lib/atomic-write-secret.nix { };
|
||||
refreshConsumer = import ./lib/refresh-consumer.nix { };
|
||||
storeRetry = import ./lib/store-retry.nix { };
|
||||
|
||||
# The script that issues the queue's leaf, which the host unit below and the
|
||||
# pod's init container both run.
|
||||
tlsIssue = import ./lib/swarm-nats-tls-issue.nix { inherit lib config; };
|
||||
|
|
@ -352,10 +337,6 @@ let
|
|||
"";
|
||||
};
|
||||
|
||||
# Where the queue runs: in its container on this host, or as a pod in this
|
||||
# host's cluster, which ./k8s/render.nix renders from the units below.
|
||||
onK8s = deployCfg.nats.runtime == "k8s";
|
||||
|
||||
# The Service in front of the pod's exporter, and the name the host's resolver
|
||||
# answers for it through the cluster's DNS.
|
||||
metricsService = "${machine}-metrics";
|
||||
|
|
@ -383,17 +364,8 @@ let
|
|||
# the bridge firewall does not open this port.
|
||||
privateNetwork = false;
|
||||
|
||||
# After the host restarts or starts the server in the container: the
|
||||
# responder is cleared of a failed state and started. `start`, not
|
||||
# `restart`: a running responder was already restarted with the server
|
||||
# (`Requires=`) and so has loaded the current keys; a stopped one has not.
|
||||
rearmResponder = ''
|
||||
systemctl --machine=${lib.escapeShellArg machine} reset-failed swarm-nats-auth.service
|
||||
systemctl --machine=${lib.escapeShellArg machine} start --no-block swarm-nats-auth.service'';
|
||||
|
||||
# The module the queue's container runs: its own NixOS system sharing the host's
|
||||
# network namespace, or, with the queue in the cluster, only the source of the
|
||||
# units the pod is rendered from (`deploy.nats.unit`, `deploy.nats.pod`).
|
||||
# The queue's NixOS system: the source of the units the pod is rendered from
|
||||
# (`deploy.nats.unit`, `deploy.nats.pod`).
|
||||
containerConfig =
|
||||
{ ... }@container:
|
||||
{
|
||||
|
|
@ -505,7 +477,7 @@ let
|
|||
cert_file = tlsCertPath;
|
||||
# The pod has no credential directory: its leaf and key are
|
||||
# files of one volume.
|
||||
key_file = if onK8s then tlsKeyPath else tlsKeyCredentialPath;
|
||||
key_file = tlsKeyPath;
|
||||
timeout = 2;
|
||||
};
|
||||
};
|
||||
|
|
@ -752,8 +724,7 @@ let
|
|||
environment.systemPackages = [ pkgs.nats-server ];
|
||||
};
|
||||
|
||||
# The container's system, evaluated as `containers.<name>.config` evaluates
|
||||
# it, for a host whose queue runs in the cluster and so has no such container.
|
||||
# The queue's system, evaluated on its own: the host has no container for it.
|
||||
podSystem =
|
||||
(import "${pkgs.path}/nixos/lib/eval-config.nix" {
|
||||
modules = [
|
||||
|
|
@ -777,9 +748,8 @@ let
|
|||
system = null;
|
||||
}).config;
|
||||
|
||||
# The system the units below are read from: the container's, or the same
|
||||
# configuration evaluated alone.
|
||||
natsSystem = if onK8s then podSystem else config.containers.${machine}.config;
|
||||
# The system the units below are read from.
|
||||
natsSystem = podSystem;
|
||||
|
||||
# The JetStream store's directory inside the container and in the pod, and the
|
||||
# container's copy of it on the host.
|
||||
|
|
@ -831,8 +801,7 @@ let
|
|||
'';
|
||||
|
||||
# The responder's own role creates the keys the store holds none of and
|
||||
# reads their public halves, as `swarm-bao-nats-callout-xkey` does on the
|
||||
# host. `renderConfig` is the host's script, which fails while a half is
|
||||
# reads their public halves. `renderConfig` fails while a half is
|
||||
# missing.
|
||||
keys = pkgs.writeShellScriptBin "swarm-nats-callout-config" ''
|
||||
export PATH=${lib.makeBinPath (tools ++ [ deployCfg.nats.authPackage ])}
|
||||
|
|
@ -1207,8 +1176,7 @@ in
|
|||
{option}`services.hyperhive.swarm.nats.domain` proxies it, behind
|
||||
the swarm collector's authelia check.
|
||||
|
||||
With the queue in the cluster this is the exporter container's port in
|
||||
the pod, which the Service `swarm-nats-metrics` publishes on the same
|
||||
This is the exporter container's port in the pod, which the Service `swarm-nats-metrics` publishes on the same
|
||||
number and only the node reaches.
|
||||
|
||||
⚠️ This and {option}`services.hyperhive.deploy.nats.monitorPort` are two
|
||||
|
|
@ -1295,8 +1263,7 @@ in
|
|||
description = ''
|
||||
Client certificate the auth-callout responder presents to the swarm's
|
||||
secret store to read the callout keys, its own OIDC client secret and
|
||||
agent queue credentials, and that `swarm-bao-nats-callout-xkey`
|
||||
presents to read the callout keys' public halves. Its subject must be
|
||||
agent queue credentials. Its subject must be
|
||||
{option}`services.hyperhive.deploy.bao.natsAuthCommonName`.
|
||||
|
||||
Required when `deploy.nats.enable` is set; unset fails at eval.
|
||||
|
|
@ -1305,9 +1272,7 @@ in
|
|||
({option}`services.hyperhive.deploy.bao.enable`); on any other queue
|
||||
host it is a file the operator places. Never derived from
|
||||
{option}`services.hyperhive.deploy.bao.clientCertFile`, which is the
|
||||
hive's own reader. Set to a file that does
|
||||
not exist, `swarm-nats-auth-secrets` and `swarm-bao-nats-callout-xkey`
|
||||
fail, and the queue's server does not start without the xkey.
|
||||
hive's own reader.
|
||||
|
||||
A path, never a value.
|
||||
'';
|
||||
|
|
@ -1342,36 +1307,21 @@ in
|
|||
Point `nats.<swarm domain>` in the operator's upstream DNS at the
|
||||
address the other hives reach.
|
||||
|
||||
With {option}`services.hyperhive.deploy.nats.runtime` set to `k8s` the
|
||||
pod's published port has the same reach: a rule in the firewall's raw
|
||||
The pod's published port has the same reach: a rule in the firewall's raw
|
||||
table drops it from every interface but the host's loopback, the bridge
|
||||
and the pods' unless this is set.
|
||||
'';
|
||||
};
|
||||
|
||||
runtime = lib.mkOption {
|
||||
type = lib.types.enum [
|
||||
"host"
|
||||
"k8s"
|
||||
];
|
||||
default = "host";
|
||||
system = lib.mkOption {
|
||||
type = lib.types.raw;
|
||||
readOnly = true;
|
||||
internal = true;
|
||||
default = natsSystem;
|
||||
description = ''
|
||||
Where the queue runs: `host` in its `swarm-nats` container, or `k8s` as
|
||||
a pod in this host's cluster
|
||||
({option}`services.hyperhive.deploy.k8s.enable`), which the host's
|
||||
configuration must enable. One switch covers the server and its auth
|
||||
callout: the pod holds both, so the server never runs without the
|
||||
responder that admits its clients. A swarm runs one queue, so a switch
|
||||
moves it: the container goes and the pod is applied, or the pod is
|
||||
deleted and the container returns.
|
||||
|
||||
The pod's JetStream store is the container's own directory on the host,
|
||||
mounted into the pod as a static local volume, so a switch moves the
|
||||
process and not the bytes: streams, consumers and key-value buckets are
|
||||
the same files on both sides. The container's unit hands the directory
|
||||
back to the server's user at each start, and the pod's group gets read
|
||||
and write access from the pod's `fsGroup`. At most one server holds the
|
||||
directory, since a switch stops the one before it starts the other.
|
||||
The queue's evaluated NixOS system, which the pod's units and server
|
||||
settings are read from: the server, its settings and the responder. The
|
||||
host runs none of it.
|
||||
'';
|
||||
};
|
||||
|
||||
|
|
@ -1382,10 +1332,8 @@ in
|
|||
default = natsSystem.systemd.services.swarm-nats-auth;
|
||||
description = ''
|
||||
The responder's evaluated systemd unit, which the pod's main container
|
||||
is rendered from (../host-modules/k8s/render.nix). It is the unit the
|
||||
container runs, taken from the container's configuration, or from the
|
||||
same configuration evaluated alone when the queue runs in the cluster
|
||||
and the host has no container.
|
||||
is rendered from (../host-modules/k8s/render.nix). It is the unit of
|
||||
{option}`system`, which no container runs.
|
||||
'';
|
||||
};
|
||||
|
||||
|
|
@ -1404,18 +1352,6 @@ in
|
|||
};
|
||||
|
||||
config = lib.mkMerge [
|
||||
{
|
||||
assertions = [
|
||||
{
|
||||
assertion = !onK8s || deployCfg.k8s.enable;
|
||||
message = ''
|
||||
services.hyperhive.deploy.nats.runtime = "k8s" needs the cluster: set
|
||||
services.hyperhive.deploy.k8s.enable on this host, or leave the
|
||||
queue's runtime at "host".
|
||||
'';
|
||||
}
|
||||
];
|
||||
}
|
||||
(lib.mkIf deployCfg.nats.enable {
|
||||
# The auth responder introspects authelia by name, and the gateway serves
|
||||
# the exporter's `/metrics` below.
|
||||
|
|
@ -1452,24 +1388,17 @@ in
|
|||
# Authelia's rules for it admit the collector's client and deny
|
||||
# every other subject (`metricsRules` in ./swarm-authelia.nix).
|
||||
"= /metrics" = {
|
||||
# In the cluster the exporter is behind a Service, which the host's
|
||||
# resolver answers through the cluster's DNS. The upstream is a
|
||||
# variable, so nginx asks the resolver per request: a name in the
|
||||
# `proxy_pass` gets one lookup when the configuration loads, which
|
||||
# fails the load while the cluster is down.
|
||||
proxyPass =
|
||||
if onK8s then
|
||||
"http://\$swarm_nats_metrics_upstream/metrics"
|
||||
else
|
||||
"http://127.0.0.1:${toString deployCfg.nats.metricsPort}/metrics";
|
||||
extraConfig =
|
||||
lib.optionalString onK8s ''
|
||||
resolver ${networkCfg.bridgeIp} valid=10s ipv6=off;
|
||||
set $swarm_nats_metrics_upstream ${metricsHost}:${toString deployCfg.nats.metricsPort};
|
||||
''
|
||||
+ ''
|
||||
auth_request /__nats_metrics_authz;
|
||||
'';
|
||||
# The exporter is behind a Service, which the host's resolver
|
||||
# answers through the cluster's DNS. The upstream is a variable, so
|
||||
# nginx asks the resolver per request: a name in the `proxy_pass`
|
||||
# gets one lookup when the configuration loads, which fails the
|
||||
# load while the cluster is down.
|
||||
proxyPass = "http://\$swarm_nats_metrics_upstream/metrics";
|
||||
extraConfig = ''
|
||||
resolver ${networkCfg.bridgeIp} valid=10s ipv6=off;
|
||||
set $swarm_nats_metrics_upstream ${metricsHost}:${toString deployCfg.nats.metricsPort};
|
||||
auth_request /__nats_metrics_authz;
|
||||
'';
|
||||
};
|
||||
|
||||
# The subrequest, same shape as the forge's `= /__forge_metrics_authz`
|
||||
|
|
@ -1545,24 +1474,23 @@ in
|
|||
{
|
||||
# The rules that keep the pod's ports as closed as the container's are
|
||||
# iptables rules.
|
||||
assertion = !onK8s || !config.networking.nftables.enable;
|
||||
assertion = !config.networking.nftables.enable;
|
||||
message = ''
|
||||
services.hyperhive.deploy.nats.runtime = "k8s" writes iptables rules
|
||||
that give the pod's ports the reach the container's have, and
|
||||
services.hyperhive.deploy.nats.enable writes iptables rules that
|
||||
keep the queue pod's ports as closed as the old container's were, and
|
||||
networking.nftables.enable replaces iptables. Leave nftables off on
|
||||
this host, or leave the queue's runtime at "host".
|
||||
this host.
|
||||
'';
|
||||
}
|
||||
{
|
||||
# The gateway's `/metrics` location dials the exporter's Service by
|
||||
# name, which only the hive's resolver answers.
|
||||
assertion = !onK8s || config.services.hyperhive.gateway.dns.enable;
|
||||
assertion = config.services.hyperhive.gateway.dns.enable;
|
||||
message = ''
|
||||
services.hyperhive.deploy.nats.runtime = "k8s" needs
|
||||
services.hyperhive.deploy.nats.enable needs
|
||||
services.hyperhive.gateway.dns.enable: the gateway dials the pod's
|
||||
metrics Service by name, and the hive's resolver is what hands the
|
||||
cluster's domain to its DNS. Set it, or leave the queue's runtime at
|
||||
"host".
|
||||
cluster's domain to its DNS. Set it.
|
||||
'';
|
||||
}
|
||||
{
|
||||
|
|
@ -1635,20 +1563,20 @@ in
|
|||
# An agent reaches it by name, which dnsmasq answers with the bridge IP.
|
||||
services.hyperhive.network.exposeHostPorts = [ cfg.port ];
|
||||
|
||||
# The server binds every address (the container shares the host netns and
|
||||
# sets no listen host), so reachability is the firewall alone.
|
||||
# The pod's port is published on every address of the node, so
|
||||
# reachability is the firewall alone.
|
||||
networking.firewall.allowedTCPPorts = lib.mkIf deployCfg.nats.openFirewall [ cfg.port ];
|
||||
|
||||
services.hyperhive.network.enable = lib.mkIf onK8s (lib.mkDefault true);
|
||||
services.hyperhive.network.enable = lib.mkDefault true;
|
||||
|
||||
# An agent dials the queue at the bridge's address, and the load
|
||||
# balancer's `hostPort` forwards that to the cluster, which the egress
|
||||
# forward chain would refuse.
|
||||
services.hyperhive.deploy.hive-controller.egress.publishedPorts = lib.mkIf onK8s [ cfg.port ];
|
||||
services.hyperhive.deploy.hive-controller.egress.publishedPorts = [ cfg.port ];
|
||||
|
||||
# The load balancer's `hostPort` is a DNAT rule, which the INPUT rules above
|
||||
# never see: ./lib/swarm-nats-raw-rules.nix gives it the container's reach.
|
||||
networking.firewall.extraCommands = lib.mkIf onK8s (
|
||||
networking.firewall.extraCommands = (
|
||||
import ./lib/swarm-nats-raw-rules.nix { inherit lib; } {
|
||||
chain = "nixos-fw-nats";
|
||||
clientPort = cfg.port;
|
||||
|
|
@ -1660,220 +1588,30 @@ in
|
|||
openClient = deployCfg.nats.openFirewall;
|
||||
}
|
||||
);
|
||||
networking.firewall.extraStopCommands = lib.mkIf onK8s ''
|
||||
networking.firewall.extraStopCommands = ''
|
||||
iptables -t raw -D PREROUTING -j nixos-fw-nats 2>/dev/null || true
|
||||
iptables -t raw -F nixos-fw-nats 2>/dev/null || true
|
||||
iptables -t raw -X nixos-fw-nats 2>/dev/null || true
|
||||
'';
|
||||
|
||||
# The bind source has to exist before the container starts, leaf or not:
|
||||
# nixos-container refuses to start over a missing one, which would take
|
||||
# the whole queue down rather than only its TLS.
|
||||
#
|
||||
# The pod's local volume needs its directory to exist before the pod
|
||||
# starts, which on a host that never ran the container nothing else makes.
|
||||
systemd.tmpfiles.rules = [
|
||||
"d ${tlsDir} 0755 root root -"
|
||||
]
|
||||
++ lib.optional onK8s "d ${hostStoreDir} 0700 root root -";
|
||||
|
||||
containers.swarm-nats = lib.mkIf (!onK8s) {
|
||||
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).
|
||||
inherit privateNetwork;
|
||||
# The public trust bundle (empty when the gateway is not self-signed)
|
||||
# and the queue's own TLS leaf, both read-only.
|
||||
bindMounts = caTrust.bindMount // {
|
||||
${tlsDir} = {
|
||||
hostPath = tlsDir;
|
||||
isReadOnly = true;
|
||||
};
|
||||
};
|
||||
config = containerConfig;
|
||||
};
|
||||
|
||||
# Order the container after the host CA generator, so the bind source
|
||||
# exists before nspawn sets the mount up. Without it a late CA fails the
|
||||
# container start outright rather than degrading.
|
||||
systemd.services."container@${machine}" = lib.mkIf (!onK8s) (
|
||||
lib.mkMerge [
|
||||
caTrust.containerOrdering
|
||||
(rootfsCheck.warnOnly machine)
|
||||
]
|
||||
);
|
||||
|
||||
# Deliver the responder's store identity into the container before it
|
||||
# starts. A copy rather than a `bindMounts` entry: nixos-container refuses
|
||||
# to start when a bind source is missing, so one absent file would take
|
||||
# down the **whole container including the queue**, not merely the
|
||||
# responder.
|
||||
systemd.services.swarm-nats-auth-secrets = {
|
||||
# The pod logs in with its ServiceAccount token and holds no copy of the
|
||||
# store identity.
|
||||
enable = !onK8s;
|
||||
description = "deliver the swarm queue responder's credentials";
|
||||
before = [ "container@swarm-nats.service" ];
|
||||
wantedBy = [ "container@swarm-nats.service" ];
|
||||
# Where the store's PKI is minted on this host, the responder's leaf is
|
||||
# one of its files, and the minting unit runs before every copy. Ordering
|
||||
# alone is not enough: the queue's container, which pulls this unit in,
|
||||
# starts from `machines.target`, outside the transaction that runs the
|
||||
# minting unit from `multi-user.target`. Ignored elsewhere, where the unit
|
||||
# does not exist.
|
||||
after = [ "swarm-bao-pki.service" ];
|
||||
wants = [ "swarm-bao-pki.service" ];
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
RemainAfterExit = true;
|
||||
SyslogIdentifier = "swarm-nats-auth-secrets";
|
||||
};
|
||||
path = [ pkgs.coreutils ];
|
||||
script = ''
|
||||
set -euo pipefail
|
||||
install -d -m 0700 ${lib.escapeShellArg secretDir}
|
||||
''
|
||||
+ lib.concatStrings (
|
||||
lib.mapAttrsToList (id: source: ''
|
||||
install -m 0400 ${lib.escapeShellArg source} ${lib.escapeShellArg (hostPath id)}
|
||||
'') authStoreFiles
|
||||
);
|
||||
};
|
||||
|
||||
# The callout's three public keys, from the store into the files
|
||||
# `renderConfig` reads at every server start. `ensure-callout-keys` first
|
||||
# creates each pair the store holds none of; it needs no queue, so on a
|
||||
# first boot the keys exist before the server that requires them, and
|
||||
# nothing here waits on the responder, which itself waits on the server.
|
||||
#
|
||||
# An issuer or user seed file left on this host is what `ensure-callout-keys`
|
||||
# stores for that key when the store holds none, so the key keeps its
|
||||
# public half. It removes the file once a read of the store returns a seed,
|
||||
# in this run and never before: a removal that ran ahead of the import,
|
||||
# with the store sealed, would leave the next run to mint a new key.
|
||||
#
|
||||
# Before the container and wanted by it, the shape `swarm-bao-nats-tls`
|
||||
# has: a store that comes up later fails this run, the server refuses to
|
||||
# start without the files (or starts with the ones last fetched), and the
|
||||
# retry that lands them starts or restarts the server. Under the
|
||||
# responder's own leaf, the one principal that may create the keys' paths.
|
||||
systemd.services.swarm-bao-nats-callout-xkey = {
|
||||
# The pod's init container creates the keys and fetches their public
|
||||
# halves itself.
|
||||
enable = !onK8s;
|
||||
description = "create the swarm queue's callout keys if absent and fetch their public halves for the server";
|
||||
# Ordered after the store where it runs here
|
||||
# (./glue-bao-readers-store-order.nix); elsewhere the timeout below
|
||||
# bounds the attempt.
|
||||
before = [ "container@${machine}.service" ];
|
||||
# On a host with the cluster node, also after the prune that deletes the
|
||||
# queue's pod on a switch back. The prune waits for the pod to end, and
|
||||
# `ensure-callout-keys` must not run beside a pod still terminating: its
|
||||
# responder is a second one against the same store. The prune names
|
||||
# this unit nowhere, so the order has no cycle.
|
||||
after = lib.optional deployCfg.k8s.enable "${machine}-k8s-prune.service";
|
||||
wantedBy = [
|
||||
"multi-user.target"
|
||||
"container@${machine}.service"
|
||||
];
|
||||
path = [
|
||||
deployCfg.nats.authPackage
|
||||
baoDeploy.package
|
||||
pkgs.coreutils
|
||||
pkgs.systemd
|
||||
];
|
||||
inherit (storeRetry) startLimitBurst startLimitIntervalSec;
|
||||
serviceConfig = storeRetry.serviceConfig // {
|
||||
Type = "oneshot";
|
||||
RemainAfterExit = true;
|
||||
SyslogIdentifier = "swarm-bao-nats-callout-xkey";
|
||||
TimeoutStartSec = 30;
|
||||
};
|
||||
environment = {
|
||||
BAO_ADDR = "https://${baoCfg.domain}:${toString baoCfg.port}";
|
||||
BAO_CLIENT_CERT = deployCfg.nats.authBaoClientCertFile;
|
||||
BAO_CLIENT_KEY = deployCfg.nats.authBaoClientKeyFile;
|
||||
}
|
||||
// lib.optionalAttrs (baoDeploy.serverCaFile != null) {
|
||||
BAO_CACERT = baoDeploy.serverCaFile;
|
||||
};
|
||||
script = ''
|
||||
set -euo pipefail
|
||||
|
||||
${atomicWriteSecret}
|
||||
${refreshConsumer}
|
||||
|
||||
swarm-nats-auth ensure-callout-keys --store-cert-role ${lib.escapeShellArg authCertRole} \
|
||||
--import-issuer-seed-file ${lib.escapeShellArg importIssuerSeed} \
|
||||
--import-user-seed-file ${lib.escapeShellArg importUserSeed}
|
||||
if [ -d ${lib.escapeShellArg importSeedDir} ]; then
|
||||
rmdir --ignore-fail-on-non-empty ${lib.escapeShellArg importSeedDir}
|
||||
fi
|
||||
|
||||
err="$(mktemp)"
|
||||
trap 'rm -f "$err"' EXIT
|
||||
|
||||
if ! BAO_TOKEN="$(bao login -method=cert -token-only 2>"$err")"; then
|
||||
echo "could not log in to swarm-bao with the queue responder's certificate; leaving the callout key files as they are." >&2
|
||||
cat "$err" >&2
|
||||
exit 1
|
||||
fi
|
||||
export BAO_TOKEN
|
||||
|
||||
install -d -m 0755 ${lib.escapeShellArg hostRuntimeDir}
|
||||
''
|
||||
+ lib.concatMapStrings (
|
||||
k:
|
||||
let
|
||||
file = "${hostRuntimeDir}/${k.file}";
|
||||
in
|
||||
''
|
||||
if ! key="$(bao kv get -field=value ${lib.escapeShellArg k.storePath} 2>"$err")"; then
|
||||
echo "swarm-bao did not return ${k.storePath}." >&2
|
||||
cat "$err" >&2
|
||||
exit 1
|
||||
fi
|
||||
if [ -z "$key" ]; then
|
||||
echo "swarm-bao returned an empty ${k.storePath}." >&2
|
||||
exit 1
|
||||
fi
|
||||
if secret_differs ${lib.escapeShellArg file} "$key"; then
|
||||
atomic_write_secret 0444 "" ${lib.escapeShellArg file} "$key"
|
||||
fi
|
||||
''
|
||||
) calloutPublicKeys
|
||||
# `renderConfig` reads the files as the server starts, and only then.
|
||||
+ lib.concatMapStrings (k: ''
|
||||
refresh_consumer ${lib.escapeShellArg machine} nats.service ${lib.escapeShellArg "${hostRuntimeDir}/${k.file}"}
|
||||
'') calloutPublicKeys
|
||||
+ ''
|
||||
if systemctl is-active --quiet ${lib.escapeShellArg "container@${machine}.service"}; then
|
||||
${rearmResponder}
|
||||
fi
|
||||
'';
|
||||
};
|
||||
"d ${hostStoreDir} 0700 root root -"
|
||||
];
|
||||
|
||||
# The queue's TLS leaf, issued by the secret store's `pki/roles/swarm-nats`
|
||||
# under this host's `swarm-nats` identity. Same shape as ./hive-tls.nix's
|
||||
# `swarm-services-cert`, whose comments carry the reasoning for the login,
|
||||
# the single `issue` call split with `jq`, and the unseal-sized retry.
|
||||
#
|
||||
# Before the container, so a normal boot starts the server with its leaf.
|
||||
# A store that comes up later fails this run; the retry lands the leaf and
|
||||
# restarts the server, which until then refuses to start (TLS required,
|
||||
# and its key credential is missing).
|
||||
#
|
||||
# With the queue in the cluster this leaf is the gateway's `/metrics` vhost
|
||||
# alone: the pod issues the one its server presents, and nothing here
|
||||
# restarts a server.
|
||||
# This leaf is the gateway's `/metrics` vhost alone: the pod issues the one
|
||||
# its server presents, and nothing here restarts a server. A store that
|
||||
# comes up later fails this run; the retry lands the leaf.
|
||||
systemd.services.swarm-bao-nats-tls = {
|
||||
description = "Issue the swarm queue's TLS leaf from the secret store's PKI";
|
||||
wantedBy = [
|
||||
"multi-user.target"
|
||||
]
|
||||
++ lib.optional (!onK8s) "container@${machine}.service";
|
||||
before = lib.optional (!onK8s) "container@${machine}.service";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
# Both absent where the store runs elsewhere, and ignored there; see
|
||||
# `swarm-services-cert`. The ordering after this leaf's policy unit is
|
||||
# ./glue-bao-readers-policy-order.nix's, because it holds only where the
|
||||
|
|
@ -1900,9 +1638,6 @@ in
|
|||
SyslogIdentifier = "swarm-bao-nats-tls";
|
||||
Restart = "on-failure";
|
||||
RestartSec = 30;
|
||||
# An attempt that rotates the leaf waits on a blocking restart of the
|
||||
# server in its container: up to 150s to stop (nixpkgs' nats module)
|
||||
# and 90s to start.
|
||||
TimeoutStartSec = "5min";
|
||||
};
|
||||
environment = {
|
||||
|
|
@ -1924,17 +1659,6 @@ in
|
|||
# The gateway's `/metrics` vhost serves a copy of this leaf.
|
||||
systemctl start swarm-nats-gateway-cert.service''}
|
||||
|
||||
# Renewal, or the late-store retry. On a normal boot the container is
|
||||
# not up yet and starts the server with this leaf itself. A restart,
|
||||
# not a reload: the key arrives by `LoadCredential`, which systemd
|
||||
# resolves at start only. `reset-failed` because a server that found
|
||||
# no key may have hit its start limit.
|
||||
if systemctl is-active --quiet ${lib.escapeShellArg "container@${machine}.service"}; then
|
||||
echo "queue leaf rotated — restarting the server in ${machine}"
|
||||
systemctl --machine=${lib.escapeShellArg machine} reset-failed nats.service
|
||||
systemctl --machine=${lib.escapeShellArg machine} restart nats.service
|
||||
${rearmResponder}
|
||||
fi
|
||||
'';
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue