gateway reaches the controller over tls with its own client leaf when the tcp listener is on
This commit is contained in:
parent
17fffaae79
commit
187ed82de0
1 changed files with 768 additions and 49 deletions
|
|
@ -292,6 +292,13 @@ let
|
|||
BAO_CACERT = baoDeploy.serverCaFile;
|
||||
};
|
||||
storeRetry = import ./lib/store-retry.nix { };
|
||||
|
||||
# The controller's TCP listener (./swarm-controller.nix): the gateway that
|
||||
# fronts the controller presents a client leaf of its own to it. Only the
|
||||
# host that runs the controller declares that vhost, so only it needs the leaf.
|
||||
tcpCfg = hyperhiveCfg.swarm.controller.tcp;
|
||||
swarmControllerCfg = hyperhiveCfg.deploy.swarm-controller;
|
||||
gatewayClientLeaf = swarmControllerCfg.enable && tcpCfg.enable;
|
||||
in
|
||||
{
|
||||
# Host-side TLS for the self-signed gateway mode.
|
||||
|
|
@ -953,6 +960,125 @@ in
|
|||
};
|
||||
};
|
||||
|
||||
# The gateway's client leaf for the controller's TCP listener, issued by
|
||||
# the store's `swarm-controller-gateway-client` role: `clientAuth` only,
|
||||
# one name, so the swarm-services leaf is not a substitute. Logs in with
|
||||
# the services-issuer identity this host already holds, whose policy gains
|
||||
# the one issue path where the listener is on.
|
||||
#
|
||||
# nginx names both files in `proxy_ssl_certificate*`, and refuses to start
|
||||
# on a config naming a missing file, so a pair always exists once this
|
||||
# unit has run: a throwaway self-signed one the listener refuses stands in
|
||||
# until the store answers, and the controller's locations answer 502 in
|
||||
# the meantime instead of the whole gateway staying down.
|
||||
systemd.services.swarm-controller-gateway-client = lib.mkIf gatewayClientLeaf {
|
||||
description = "Issue the gateway's client certificate for the swarm-controller's TCP listener";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
before = [ "nginx.service" ];
|
||||
after = [
|
||||
"container@${baoCfg.machine}.service"
|
||||
"swarm-bao-pki.service"
|
||||
"swarm-bao-services-issuer-policy.service"
|
||||
];
|
||||
path = servicesCertPath;
|
||||
inherit (storeRetry) startLimitBurst startLimitIntervalSec;
|
||||
serviceConfig = storeRetry.serviceConfig // {
|
||||
Type = "oneshot";
|
||||
UMask = "0077";
|
||||
SyslogIdentifier = "swarm-controller-gateway-client";
|
||||
TimeoutStartSec = servicesCertTimeoutStartSec;
|
||||
};
|
||||
environment = servicesCertEnvironment;
|
||||
script = ''
|
||||
set -euo pipefail
|
||||
d=${lib.escapeShellArg cfg.stateDir}
|
||||
cert=${lib.escapeShellArg swarmControllerCfg.tcp.gatewayClientCertFile}
|
||||
key=${lib.escapeShellArg swarmControllerCfg.tcp.gatewayClientKeyFile}
|
||||
root="$d/swarm-services-root.pem"
|
||||
name=${lib.escapeShellArg (toString tcpCfg.clientName)}
|
||||
install -d -m 0755 "$d"
|
||||
|
||||
# Same readability as the gateway's own key: nginx's pre-start config
|
||||
# test runs as the nginx user.
|
||||
place() { # $1 = certificate, $2 = key, written in place of the pair
|
||||
chgrp nginx "$2"
|
||||
chmod 0640 "$2"
|
||||
chmod 0644 "$1"
|
||||
mv -f "$2" "$key"
|
||||
mv -f "$1" "$cert"
|
||||
}
|
||||
|
||||
if [ ! -s "$cert" ] || [ ! -s "$key" ]; then
|
||||
echo "no client certificate yet — placing a throwaway one so nginx can load its config"
|
||||
openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes \
|
||||
-days 1 -subj "/CN=placeholder" -keyout "$key.new" -out "$cert.new" 2>/dev/null
|
||||
place "$cert.new" "$key.new"
|
||||
fi
|
||||
|
||||
# Missing, past half its window, naming something else, not chaining
|
||||
# to the store's root, or not usable as a client certificate. The
|
||||
# throwaway above fails the chain test, which is what replaces it.
|
||||
reissue=0
|
||||
openssl x509 -in "$cert" -noout -checkend ${
|
||||
toString (baoDeploy.servicesPkiLeafTtlHours * 3600 / 2)
|
||||
} >/dev/null 2>&1 || reissue=1
|
||||
openssl x509 -in "$cert" -noout -checkhost "$name" 2>/dev/null | grep -q ' does match ' || reissue=1
|
||||
{ [ -s "$root" ] && openssl verify -CAfile "$root" -purpose sslclient "$cert" >/dev/null 2>&1; } || reissue=1
|
||||
if [ "$reissue" = 0 ]; then
|
||||
echo "gateway client leaf valid for $name — leaving it alone"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
${requireServicesIssuer "ask the store for the gateway's client leaf"}
|
||||
|
||||
err="$(mktemp)"
|
||||
resp="$(mktemp "$d/controller-client.json.XXXXXX")"
|
||||
trap 'rm -f "$err" "$resp"' EXIT
|
||||
|
||||
if ! BAO_TOKEN="$(bao login -method=cert -token-only 2>"$err")"; then
|
||||
echo "could not log in to the swarm secret store with this host's services-issuer certificate." >&2
|
||||
cat "$err" >&2
|
||||
exit 1
|
||||
fi
|
||||
export BAO_TOKEN
|
||||
|
||||
echo "requesting the gateway's client leaf for $name from the store"
|
||||
if ! bao write -format=json \
|
||||
${lib.escapeShellArg "${baoServicesPkiMount}/issue/${tcpCfg.clientPkiRole}"} \
|
||||
common_name="$name" > "$resp" 2>"$err"; then
|
||||
echo "the store refused to issue the gateway's client certificate." >&2
|
||||
cat "$err" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
jq -r '.data.private_key // empty' < "$resp" > "$key.new"
|
||||
jq -r '.data.certificate // empty' < "$resp" > "$cert.new"
|
||||
for f in "$key.new" "$cert.new"; do
|
||||
if [ ! -s "$f" ]; then
|
||||
echo "the store's response was missing a field: $f is empty" >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
place "$cert.new" "$key.new"
|
||||
|
||||
# `--no-block` for the reason `swarm-services-cert` gives: this unit
|
||||
# is ordered before nginx, so a blocking reload would wait on us.
|
||||
if systemctl is-active --quiet nginx.service; then
|
||||
echo "gateway client leaf rotated — reloading nginx"
|
||||
systemctl reload --no-block nginx.service
|
||||
fi
|
||||
'';
|
||||
};
|
||||
|
||||
systemd.timers.swarm-controller-gateway-client = lib.mkIf gatewayClientLeaf {
|
||||
description = "Daily renewal check for the gateway's controller client certificate";
|
||||
wantedBy = [ "timers.target" ];
|
||||
timerConfig = {
|
||||
OnCalendar = "daily";
|
||||
Persistent = true;
|
||||
};
|
||||
};
|
||||
|
||||
# Daily rather than weekly, for the reason
|
||||
# ./swarm-nats.nix's `swarm-bao-nats-tls` timer is: this leaf needs the
|
||||
# store, which can be sealed or unreachable, so there is a retry per day
|
||||
|
|
|
|||
Loading…
Reference in a new issue