nats: runtime k8s runs the queue and its auth callout as one pod
This commit is contained in:
parent
1c8798a3ca
commit
a2db26d344
2 changed files with 1487 additions and 842 deletions
41
nix/host-modules/lib/swarm-nats-raw-rules.nix
Normal file
41
nix/host-modules/lib/swarm-nats-raw-rules.nix
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
# The iptables rules that give the queue pod's `hostPort`s the reach the
|
||||
# queue's container has. A `hostPort` is a DNAT rule, and what a DNAT rule
|
||||
# rewrites is forwarded, never delivered to the host: the INPUT rules (the
|
||||
# firewall's `allowedTCPPorts` and per-interface rules) never see it, and the
|
||||
# cluster's own forwarding rules accept it. The raw table is read before the
|
||||
# DNAT and before any of those, so a rule there reads the original
|
||||
# destination, as INPUT does for the container's listener.
|
||||
#
|
||||
# The client port is dropped for a packet addressed to the host unless it
|
||||
# arrives on one of `trusted` (the host's loopback, the hive bridge and the
|
||||
# pods' interfaces), or `openClient` is set, which is
|
||||
# `deploy.nats.openFirewall`. The exporter's port answers with every client's
|
||||
# name, so it is dropped for all but the host's own loopback.
|
||||
#
|
||||
# Pure function — NOT a NixOS module. Returns the text for the firewall's
|
||||
# `extraCommands`.
|
||||
{ lib }:
|
||||
{
|
||||
chain,
|
||||
clientPort,
|
||||
metricsPort,
|
||||
trusted,
|
||||
openClient,
|
||||
}:
|
||||
let
|
||||
rule = "iptables -t raw -A ${chain}";
|
||||
local = "-m addrtype --dst-type LOCAL -j DROP";
|
||||
in
|
||||
''
|
||||
iptables -t raw -N ${chain} 2>/dev/null || iptables -t raw -F ${chain}
|
||||
${rule} -p tcp --dport ${toString metricsPort} -i lo -j RETURN
|
||||
${rule} -p tcp --dport ${toString metricsPort} ${local}
|
||||
${lib.optionalString (!openClient) ''
|
||||
${lib.concatMapStrings (i: ''
|
||||
${rule} -p tcp --dport ${toString clientPort} -i ${i} -j RETURN
|
||||
'') trusted}
|
||||
${rule} -p tcp --dport ${toString clientPort} ${local}
|
||||
''}
|
||||
iptables -t raw -D PREROUTING -j ${chain} 2>/dev/null || true
|
||||
iptables -t raw -I PREROUTING -j ${chain}
|
||||
''
|
||||
89
nix/host-modules/lib/swarm-nats-tls-issue.nix
Normal file
89
nix/host-modules/lib/swarm-nats-tls-issue.nix
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
# The script that issues the queue's TLS leaf from the store's `pki` mount, and
|
||||
# the two places that run it: the host's `swarm-bao-nats-tls.service`
|
||||
# (../swarm-nats.nix) and the pod's init container (the same file's `pod`).
|
||||
# One script, so the leaf's checks (name, half-life, the role it is issued
|
||||
# through) cannot differ between them.
|
||||
#
|
||||
# Pure function of the evaluated configuration, NOT a NixOS module. `dir` is
|
||||
# where the leaf lands, `login` the command that prints a store token on
|
||||
# stdout, and `credential` what it logs in with (for the error). `createDir` is
|
||||
# false where the directory exists and belongs to another user, as an
|
||||
# `emptyDir` does in a pod. `preLogin` is shell run before the login, once the
|
||||
# leaf is known to need issuing.
|
||||
{ lib, config }:
|
||||
let
|
||||
cfg = config.services.hyperhive.swarm.nats;
|
||||
baoDeploy = config.services.hyperhive.deploy.bao;
|
||||
|
||||
# Re-issue once the leaf is past half of the 720h `pki/roles/swarm-nats`
|
||||
# grants it (./swarm-bao.nix), so the daily check has two weeks of retries
|
||||
# before it lapses.
|
||||
leafRenewSeconds = 360 * 3600;
|
||||
in
|
||||
{
|
||||
inherit leafRenewSeconds;
|
||||
|
||||
script =
|
||||
{
|
||||
dir,
|
||||
login,
|
||||
credential,
|
||||
createDir ? true,
|
||||
preLogin ? "",
|
||||
}:
|
||||
lib.removeSuffix "\n" ''
|
||||
set -euo pipefail
|
||||
d=${lib.escapeShellArg dir}
|
||||
name=${lib.escapeShellArg cfg.domain}
|
||||
${lib.optionalString createDir ''install -d -m 0755 "$d"''}
|
||||
|
||||
# Missing, past half its window, or naming something other than the
|
||||
# configured domain.
|
||||
reissue=0
|
||||
{ [ -s "$d/cert.pem" ] && [ -s "$d/key.pem" ]; } || reissue=1
|
||||
openssl x509 -in "$d/cert.pem" -noout -checkend ${toString leafRenewSeconds} >/dev/null 2>&1 || reissue=1
|
||||
openssl x509 -in "$d/cert.pem" -noout -checkhost "$name" 2>/dev/null | grep -q ' does match ' || reissue=1
|
||||
|
||||
if [ "$reissue" = 0 ]; then
|
||||
echo "queue leaf valid for $name — leaving it alone"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
${preLogin}
|
||||
|
||||
err="$(mktemp)"
|
||||
trap 'rm -f "$err"' EXIT
|
||||
|
||||
if ! BAO_TOKEN="$(${login} 2>"$err")"; then
|
||||
echo "could not log in to the swarm secret store with the queue's ${credential}." >&2
|
||||
cat "$err" >&2
|
||||
exit 1
|
||||
fi
|
||||
export BAO_TOKEN
|
||||
|
||||
echo "requesting the queue's TLS leaf for $name from the store"
|
||||
resp="$(mktemp "$d/issue.json.XXXXXX")"
|
||||
trap 'rm -f "$err" "$resp"' EXIT
|
||||
|
||||
if ! bao write -format=json \
|
||||
${lib.escapeShellArg "${baoDeploy.servicesPkiMountPath}/issue/${baoDeploy.natsPkiRoleName}"} \
|
||||
common_name="$name" > "$resp" 2>"$err"; then
|
||||
echo "the store refused to issue the queue's certificate." >&2
|
||||
cat "$err" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
jq -r '.data.private_key // empty' < "$resp" > "$d/key.pem.new"
|
||||
jq -r '.data.certificate // empty' < "$resp" > "$d/cert.pem.new"
|
||||
for f in "$d/key.pem.new" "$d/cert.pem.new"; do
|
||||
if [ ! -s "$f" ]; then
|
||||
echo "the store's response was missing a field: $f is empty" >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
chmod 0600 "$d/key.pem.new"
|
||||
chmod 0644 "$d/cert.pem.new"
|
||||
mv -f "$d/key.pem.new" "$d/key.pem"
|
||||
mv -f "$d/cert.pem.new" "$d/cert.pem"
|
||||
'';
|
||||
}
|
||||
Loading…
Reference in a new issue