k8s: pods log in to the secret store with their ServiceAccount token
The store gets a listener for the pods that asks for no client certificate, open on the pod network's interfaces only, and a jwt auth mount with one role per in-cluster service. The rendered pods run under a ServiceAccount with a projected token in place of their client certificate.
This commit is contained in:
parent
cad1b7cebe
commit
d1554def7a
1 changed files with 720 additions and 69 deletions
|
|
@ -41,6 +41,11 @@ let
|
|||
baoDeploy = deployCfg.bao;
|
||||
networkCfg = hyperhiveCfg.network;
|
||||
|
||||
# The cluster node on this host, and what its pods and this store agree on
|
||||
# about the pods' login.
|
||||
k8sNode = deployCfg.k8s;
|
||||
podAuth = import ./lib/bao-pod-auth.nix;
|
||||
|
||||
shared = import ./lib/swarm-bao-shared.nix { inherit config lib pkgs; };
|
||||
inherit (shared)
|
||||
agentPkiMountPath
|
||||
|
|
@ -67,6 +72,7 @@ let
|
|||
controllerClientLeaf
|
||||
controllerForgeTokenLeaf
|
||||
controllerGrafanaTokenLeaf
|
||||
controllerPolicyName
|
||||
controllerWebhookSecretLeaf
|
||||
createStanza
|
||||
credentialMountPath
|
||||
|
|
@ -316,6 +322,16 @@ let
|
|||
path "${credentialMountPath}/data/${baoUiClientLeaf}" {
|
||||
capabilities = ["read"]
|
||||
}
|
||||
''
|
||||
+ lib.optionalString k8sNode.enable ''
|
||||
|
||||
path "auth/${podAuth.mount}/config" {
|
||||
capabilities = ["create", "update"]
|
||||
}
|
||||
|
||||
path "auth/${podAuth.mount}/role/swarm-*" {
|
||||
capabilities = ["create", "update"]
|
||||
}
|
||||
'';
|
||||
|
||||
# The bootstrap token's first use, in `swarm-bao-granter-role`.
|
||||
|
|
@ -329,10 +345,6 @@ let
|
|||
setupSteps = granterSetupSteps;
|
||||
};
|
||||
|
||||
# The name both ends must agree on: the cert-auth role below attaches this
|
||||
# policy by spelling it the same way, and is itself named after it.
|
||||
controllerPolicyName = "swarm-controller";
|
||||
|
||||
# An option rather than a literal because whatever mints the controller's
|
||||
# leaf has to spell it identically, and that lives in another file.
|
||||
controllerCn = baoDeploy.controllerCommonName;
|
||||
|
|
@ -948,6 +960,25 @@ let
|
|||
tls_require_and_verify_client_cert = true;
|
||||
};
|
||||
|
||||
# The listener the cluster's pods log in on, with a ServiceAccount token.
|
||||
# A pod holds no client certificate, so this is the one listener that does
|
||||
# not require one; `tls_client_ca_file` stays, so a client that presents a
|
||||
# certificate is still checked against it. Pods reach it on the bridge
|
||||
# address, where the name every reader dials resolves for them. Nothing else
|
||||
# may reach it: the firewall admits its port on the pod network's interfaces
|
||||
# only (see `podListenerPort`), and an address on the bridge alone does not
|
||||
# keep a bridge client out, because the kernel accepts a packet for any local
|
||||
# address on any interface. Its cluster address stays on loopback, as
|
||||
# nothing joins this store.
|
||||
podListener = lib.optionalAttrs k8sNode.enable {
|
||||
pod = {
|
||||
type = "tcp";
|
||||
address = "${networkCfg.bridgeIp}:${toString baoDeploy.podListenerPort}";
|
||||
cluster_address = "127.0.0.1:${toString (baoDeploy.podListenerPort + 1)}";
|
||||
}
|
||||
// removeAttrs listenerTls [ "tls_require_and_verify_client_cert" ];
|
||||
};
|
||||
|
||||
extraListeners = lib.listToAttrs (
|
||||
lib.imap1 (
|
||||
i: addr:
|
||||
|
|
@ -985,6 +1016,7 @@ let
|
|||
// listenerTls;
|
||||
}
|
||||
// extraListeners
|
||||
// podListener
|
||||
// metricsListener
|
||||
// uiListener;
|
||||
|
||||
|
|
@ -1047,6 +1079,10 @@ let
|
|||
(baoDeploy.uiPort + 1)
|
||||
baoDeploy.uiProxyPort
|
||||
baoDeploy.otel.telemetryPort
|
||||
]
|
||||
++ lib.optionals k8sNode.enable [
|
||||
baoDeploy.podListenerPort
|
||||
(baoDeploy.podListenerPort + 1)
|
||||
];
|
||||
|
||||
# Paths a browser session has no business sending to the store: they unseal,
|
||||
|
|
@ -2414,7 +2450,7 @@ in
|
|||
LAN or the internet. Off by default: the port is otherwise open only on
|
||||
the agent bridge.
|
||||
|
||||
⚠️ This exposes the secret store beyond the tunnel. Every listener
|
||||
⚠️ This exposes the secret store beyond the tunnel. Every listener on that port
|
||||
requires a client certificate signed by
|
||||
{option}`services.hyperhive.deploy.bao.clientCaFile`, and nothing
|
||||
answers without one, which is why setting this without that file, or
|
||||
|
|
@ -2426,6 +2462,24 @@ in
|
|||
address the other hives reach.
|
||||
'';
|
||||
};
|
||||
|
||||
podListenerPort = lib.mkOption {
|
||||
type = lib.types.port;
|
||||
default = 8208;
|
||||
description = ''
|
||||
Port of the listener the cluster's pods log in on, on the hive
|
||||
bridge's address. It exists only with
|
||||
{option}`services.hyperhive.deploy.k8s.enable`, and unlike every other
|
||||
listener it asks for **no client certificate**: a pod authenticates
|
||||
with its ServiceAccount token at the `jwt-k8s` auth mount. The firewall
|
||||
admits this port on the pod network's interfaces only, so a client on
|
||||
the hive bridge cannot reach it.
|
||||
|
||||
⚠️ openbao also binds this port **+ 1** as the listener's cluster
|
||||
address, so neither may coincide with another port the store or its
|
||||
container binds. An assertion checks this.
|
||||
'';
|
||||
};
|
||||
};
|
||||
|
||||
# ⚠️ Gated on `deploy.bao.enable`, and that is load-bearing rather than
|
||||
|
|
@ -2467,8 +2521,9 @@ in
|
|||
${lib.concatMapStringsSep ", " toString containerPorts}. They are
|
||||
services.hyperhive.swarm.bao.port and +1, deploy.bao.metricsPort and
|
||||
+1, deploy.bao.uiPort and +1 (openbao binds each listener's port + 1
|
||||
as its cluster address), deploy.bao.uiProxyPort, and
|
||||
deploy.bao.otel.telemetryPort. Pick free ports.
|
||||
as its cluster address), deploy.bao.uiProxyPort,
|
||||
deploy.bao.otel.telemetryPort, and with deploy.k8s.enable
|
||||
deploy.bao.podListenerPort and +1. Pick free ports.
|
||||
'';
|
||||
}
|
||||
{
|
||||
|
|
@ -2643,11 +2698,22 @@ in
|
|||
# certificate its CA signed.
|
||||
services.hyperhive.network.exposeHostPorts = [ cfg.port ];
|
||||
|
||||
# Every listener, bridge-IP passthrough included, requires the client
|
||||
# certificate (see `listenerTls`), so opening the port host-wide adds
|
||||
# reach and no unauthenticated path.
|
||||
# Every listener on this port, bridge-IP passthrough included, requires
|
||||
# the client certificate (see `listenerTls`), so opening the port
|
||||
# host-wide adds reach and no unauthenticated path.
|
||||
networking.firewall.allowedTCPPorts = lib.mkIf baoDeploy.openFirewall [ cfg.port ];
|
||||
|
||||
# The pods' listener asks for no client certificate, so its port opens on
|
||||
# the pod network's interfaces and nowhere else: not on the hive bridge,
|
||||
# whose firewall refuses every port it does not list. The pod network's
|
||||
# interfaces are trusted already; naming the port here keeps the reach
|
||||
# from depending on that.
|
||||
networking.firewall.interfaces = lib.mkIf k8sNode.enable (
|
||||
lib.genAttrs k8sNode.cniInterfaces (_: {
|
||||
allowedTCPPorts = [ baoDeploy.podListenerPort ];
|
||||
})
|
||||
);
|
||||
|
||||
# The operators' door to the UI, shaped like ./swarm-victorialogs.nix's
|
||||
# vhost. It proxies to the container's nginx over loopback (shared
|
||||
# netns), so no port opens anywhere. `auth_request` only proves a
|
||||
|
|
@ -2984,7 +3050,9 @@ in
|
|||
# already running (or a `systemctl` that itself fails to enqueue
|
||||
# the job) would mark the granter failed and trigger its own
|
||||
# `Restart=on-failure` for a problem that isn't the granter's.
|
||||
ExecStartPost = "-${pkgs.systemd}/bin/systemctl restart --no-block swarm-bao-operator-viewer-policy.service";
|
||||
ExecStartPost =
|
||||
"-${pkgs.systemd}/bin/systemctl restart --no-block swarm-bao-operator-viewer-policy.service"
|
||||
+ lib.optionalString k8sNode.enable " swarm-bao-k8s-jwt-policy.service";
|
||||
};
|
||||
script = ''
|
||||
set -euo pipefail
|
||||
|
|
@ -3024,7 +3092,15 @@ in
|
|||
*'"oidc/"'*) ;;
|
||||
*) bao auth enable -listing-visibility=unauth oidc ;;
|
||||
esac
|
||||
${lib.optionalString k8sNode.enable ''
|
||||
|
||||
# The pods' login with a ServiceAccount token;
|
||||
# `swarm-bao-k8s-jwt-policy` writes its config and roles.
|
||||
case "$mounted" in
|
||||
*'"${podAuth.mount}/"'*) ;;
|
||||
*) bao auth enable -path=${podAuth.mount} jwt ;;
|
||||
esac
|
||||
''}
|
||||
printf '%s' ${lib.escapeShellArg granterPolicyText} |
|
||||
bao policy write ${lib.escapeShellArg granterPolicyName} -
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue