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
a70710dc7d
commit
2a6b0ee00a
2 changed files with 720 additions and 69 deletions
|
|
@ -2743,6 +2743,7 @@ let
|
|||
"sys/auth/cert"
|
||||
"sys/auth/approle"
|
||||
"sys/auth/oidc"
|
||||
"sys/auth/jwt-k8s"
|
||||
"sys/policies/acl/bao-granter"
|
||||
"auth/cert/certs/bao-granter"
|
||||
]
|
||||
|
|
|
|||
|
|
@ -27,6 +27,8 @@ let
|
|||
runGroup
|
||||
hive
|
||||
hiveWithController
|
||||
baoSettings
|
||||
bridgePorts
|
||||
;
|
||||
|
||||
allLocal = shared.allLocal;
|
||||
|
|
@ -97,6 +99,21 @@ let
|
|||
# compared with themselves.
|
||||
collectorEndpoint = "http://hive-collector.hive:4318";
|
||||
|
||||
# What the pod's environment changes about the store login: no client
|
||||
# certificate or key, and the token login on the listener that asks for no
|
||||
# certificate. Spelled out here, not read from the module.
|
||||
storeLogin = name: {
|
||||
BAO_ADDR = "https://${allLocal.services.hyperhive.swarm.bao.domain}:8208";
|
||||
BAO_AUTH = "jwt";
|
||||
BAO_JWT_FILE = "/var/run/secrets/hyperhive/bao/token";
|
||||
BAO_JWT_ROLE = name;
|
||||
BAO_JWT_MOUNT = "jwt-k8s";
|
||||
};
|
||||
clientVars = [
|
||||
"BAO_CLIENT_CERT"
|
||||
"BAO_CLIENT_KEY"
|
||||
];
|
||||
|
||||
# What the pod adds on top of the unit's own environment.
|
||||
added = {
|
||||
swarm-controller = {
|
||||
|
|
@ -136,6 +153,15 @@ let
|
|||
) (lib.attrValues units);
|
||||
rendered = builtins.unsafeDiscardStringContext (builtins.toJSON objects);
|
||||
|
||||
# The credentials that hold a client certificate or its key: the pods log in
|
||||
# with a token instead.
|
||||
clientCertIds = [
|
||||
"bao-client.pem"
|
||||
"bao-client-key.pem"
|
||||
"notify-router-bao-cert"
|
||||
"notify-router-bao-key"
|
||||
];
|
||||
|
||||
ciTimer = allLocal.systemd.timers.swarm-ci-metrics.timerConfig;
|
||||
cron = objects."swarm-ci-metrics/cronjob".spec;
|
||||
|
||||
|
|
@ -151,12 +177,18 @@ let
|
|||
{
|
||||
# Each service's own credential path is the control: the comparison
|
||||
# reads a non-empty environment.
|
||||
name = "each workload carries its unit's environment, plus only its directories and health listener";
|
||||
name = "each workload carries its unit's environment, plus only its directories, its health listener and the token login to the store";
|
||||
ok =
|
||||
lib.all (name: envOf name == unitEnv name // added.${name}) (lib.attrNames units)
|
||||
lib.all (
|
||||
name: envOf name == removeAttrs (unitEnv name) clientVars // storeLogin name // added.${name}
|
||||
) (lib.attrNames units)
|
||||
# The units keep their certificates, and the pods name none.
|
||||
&&
|
||||
(envOf "swarm-controller").BAO_CLIENT_CERT
|
||||
(unitEnv "swarm-controller").BAO_CLIENT_CERT
|
||||
== "/run/credentials/swarm-controller.service/bao-client.pem"
|
||||
&& lib.all (name: lib.all (v: (unitEnv name) ? ${v} && !((envOf name) ? ${v})) clientVars) (
|
||||
lib.attrNames units
|
||||
)
|
||||
&& (envOf "swarm-notify-router") ? SWARM_NOTIFY_ROUTER_NATS_URL;
|
||||
}
|
||||
{
|
||||
|
|
@ -166,7 +198,9 @@ let
|
|||
ok =
|
||||
lib.length credentialSources >= 7
|
||||
&& !(lib.any (s: lib.hasInfix s rendered) credentialSources)
|
||||
&& lib.all (id: lib.hasInfix "\"${id}\"" rendered) credentialIds
|
||||
&& lib.all (id: lib.hasInfix "\"${id}\"" rendered) (lib.subtractLists clientCertIds credentialIds)
|
||||
# The units load the client certificates and the pods do not.
|
||||
&& lib.all (id: lib.elem id credentialIds && !(lib.hasInfix "\"${id}\"" rendered)) clientCertIds
|
||||
&& !(lib.any (o: o.kind == "Secret") (lib.attrValues objects))
|
||||
&&
|
||||
(lib.head (podSpec "swarm-controller").volumes).secret.secretName == "swarm-controller-credentials";
|
||||
|
|
@ -239,10 +273,13 @@ let
|
|||
&& lib.length loaded == 4;
|
||||
}
|
||||
{
|
||||
# Nothing the host already runs differs: every unit, timer and
|
||||
# container is still there, the only new units are k3s's, every unit but
|
||||
# the firewall (whose rules carry the trusted interfaces) starts the same
|
||||
# command, and the manifests are the same derivation as without the node.
|
||||
# Nothing the host already runs differs but the store's side of the pods'
|
||||
# login: every unit, timer and container is still there, the only new
|
||||
# units are k3s's and the one that writes the pods' login, every unit but
|
||||
# the firewall (whose rules carry the pod network's interfaces) and the
|
||||
# granter's role unit (which enables the login's auth mount) starts the
|
||||
# same command, and the manifests are the same derivation as without the
|
||||
# node.
|
||||
name = "the node changes no unit, container, timer or manifest of the all-local host";
|
||||
ok =
|
||||
let
|
||||
|
|
@ -253,7 +290,8 @@ let
|
|||
&& names allLocal.containers == names withNode.containers
|
||||
&& lib.subtractLists (names withNode.systemd.services) (names allLocal.systemd.services) == [ ]
|
||||
&& lib.elem "k3s" added
|
||||
&& lib.all (n: lib.hasPrefix "k3s" n) added
|
||||
&& lib.all (n: lib.hasPrefix "k3s" n || n == "swarm-bao-k8s-jwt-policy") added
|
||||
&& lib.elem "swarm-bao-k8s-jwt-policy" added
|
||||
&&
|
||||
lib.all
|
||||
(
|
||||
|
|
@ -265,6 +303,7 @@ let
|
|||
lib.subtractLists [
|
||||
"firewall"
|
||||
"opentelemetry-collector"
|
||||
"swarm-bao-granter-role"
|
||||
] (names allLocal.systemd.services)
|
||||
)
|
||||
&& withNode.system.build.k8sManifests.drvPath == allLocal.system.build.k8sManifests.drvPath;
|
||||
|
|
@ -425,6 +464,182 @@ let
|
|||
&&
|
||||
acls == [ "A+ /var/log/pods - - - - u:opentelemetry-collector:rX,d:u:opentelemetry-collector:rX" ];
|
||||
}
|
||||
{
|
||||
# The projected token is the pod's only credential from its account, and
|
||||
# the roles in the store bind exactly this account and audience.
|
||||
name = "each in-cluster service runs under its own ServiceAccount, with a token projected for the store's audience";
|
||||
ok =
|
||||
lib.all (
|
||||
name:
|
||||
let
|
||||
sa = objects."${name}/serviceaccount";
|
||||
vol = lib.findFirst (v: v.name == "bao-token") null (podSpec name).volumes;
|
||||
mount = lib.findFirst (m: m.name == "bao-token") null (container name).volumeMounts;
|
||||
source = (lib.head vol.projected.sources).serviceAccountToken;
|
||||
in
|
||||
sa.kind == "ServiceAccount"
|
||||
&& sa.metadata.name == name
|
||||
&& sa.metadata.namespace == "hyperhive"
|
||||
&& !sa.automountServiceAccountToken
|
||||
&& (podSpec name).serviceAccountName == name
|
||||
&& !(podSpec name).automountServiceAccountToken
|
||||
&& vol != null
|
||||
&& lib.attrNames (lib.head vol.projected.sources) == [ "serviceAccountToken" ]
|
||||
&& source.audience == "bao"
|
||||
&& source.expirationSeconds >= 600
|
||||
&& mount.mountPath == "/var/run/secrets/hyperhive/bao"
|
||||
&& mount.readOnly
|
||||
&& (envOf name).BAO_JWT_FILE == "${mount.mountPath}/${source.path}"
|
||||
) (lib.attrNames units)
|
||||
# The control: the credentials volume is still there beside it.
|
||||
&& lib.any (v: v.name == "credentials") (podSpec "swarm-controller").volumes
|
||||
&& lib.length (lib.filter (v: v.name == "bao-token") (podSpec "swarm-controller").volumes) == 1;
|
||||
}
|
||||
{
|
||||
# The off arm and the on arm read the same unit, so what differs is the
|
||||
# node alone. Roles are written one to one with the principals' cert
|
||||
# roles: the same name, policy and token lifetimes.
|
||||
name = "the store writes one jwt role per in-cluster service, binding its account and audience, with its cert role's policy and lifetimes";
|
||||
ok =
|
||||
let
|
||||
s = withNode.systemd.services;
|
||||
script = s.swarm-bao-k8s-jwt-policy.script;
|
||||
certScript = {
|
||||
swarm-notify-router = s.swarm-bao-notify-router-policy.script;
|
||||
swarm-ci-metrics = s.swarm-bao-ci-metrics-policy.script;
|
||||
swarm-controller = s.swarm-bao-controller-policy.script;
|
||||
};
|
||||
roles = map lib.head (lib.filter lib.isList (builtins.split "auth/jwt-k8s/role/([a-z-]+)" script));
|
||||
ttl = "token_ttl=1h \\\n token_max_ttl=1h";
|
||||
roleText = name: ''
|
||||
bao write auth/jwt-k8s/role/${name} \
|
||||
role_type=jwt \
|
||||
user_claim=sub \
|
||||
bound_subject=system:serviceaccount:hyperhive:${name} \
|
||||
bound_audiences=bao \
|
||||
token_policies=${name} \
|
||||
${ttl}
|
||||
'';
|
||||
in
|
||||
lib.length roles == 3
|
||||
&& lib.sort lib.lessThan roles == lib.sort lib.lessThan (lib.attrNames units)
|
||||
&&
|
||||
lib.sort lib.lessThan withNode.services.hyperhive.deploy.bao.k8sPrincipals
|
||||
== lib.sort lib.lessThan roles
|
||||
&& lib.all (
|
||||
name:
|
||||
lib.hasInfix (lib.removeSuffix "\n" (roleText name)) script
|
||||
&& lib.hasInfix "auth/cert/certs/${name}" certScript.${name}
|
||||
&& lib.hasInfix "token_policies=${name}" certScript.${name}
|
||||
&& lib.hasInfix "token_ttl=1h" certScript.${name}
|
||||
&& lib.hasInfix "token_max_ttl=1h" certScript.${name}
|
||||
) roles
|
||||
&& lib.hasInfix "bao write auth/jwt-k8s/config -" script;
|
||||
}
|
||||
{
|
||||
# The keys come from the node's own files, and the issuer is the one the
|
||||
# node's flags give its API server.
|
||||
name = "the jwt config is the public half of k3s's ServiceAccount keys and its issuer, derived by a unit ordered after k3s";
|
||||
ok =
|
||||
let
|
||||
u = withNode.systemd.services.swarm-bao-k8s-jwt-policy;
|
||||
start = withNode.systemd.services.k3s.serviceConfig.ExecStart;
|
||||
in
|
||||
lib.hasInfix "/var/lib/rancher/k3s/server/tls/service.key" u.script
|
||||
&& lib.hasInfix "/var/lib/rancher/k3s/server/tls/service.current.key" u.script
|
||||
&& lib.hasInfix "openssl pkey -in \"$block\" -pubout" u.script
|
||||
&& lib.hasInfix "jwt_validation_pubkeys" u.script
|
||||
&& lib.hasInfix "https://kubernetes.default.svc.cluster.local" u.script
|
||||
&& lib.hasInfix "\"RS256\", \"ES256\"" u.script
|
||||
&& lib.hasInfix "--data-dir=/var/lib/rancher/k3s" start
|
||||
&& lib.hasInfix "--cluster-domain=cluster.local" start
|
||||
&& lib.elem "k3s.service" u.after
|
||||
&& lib.elem "k3s.service" u.wants;
|
||||
}
|
||||
{
|
||||
# The mount is a bootstrap-token act, like the oidc one, and the granter
|
||||
# may then write its config and roles only.
|
||||
name = "the granter's role unit enables the jwt mount, the bootstrap policy carries it and the granter writes its config and roles";
|
||||
ok =
|
||||
let
|
||||
g = withNode.systemd.services.swarm-bao-granter-role;
|
||||
hcl = builtins.readFile ../host-modules/bao-bootstrap-policy.hcl;
|
||||
in
|
||||
lib.hasInfix "bao auth enable -path=jwt-k8s jwt" g.script
|
||||
&& lib.hasInfix "path \"auth/jwt-k8s/config\" {\n capabilities = [\"create\", \"update\"]\n}" g.script
|
||||
&& lib.hasInfix "path \"auth/jwt-k8s/role/swarm-*\" {\n capabilities = [\"create\", \"update\"]\n}" g.script
|
||||
&& lib.hasInfix "path \"sys/auth/jwt-k8s\" {\n capabilities = [\"create\", \"update\", \"sudo\"]\n}" hcl
|
||||
&& !(lib.hasInfix "sys/auth/jwt-k8s" g.script)
|
||||
&& lib.hasInfix "swarm-bao-k8s-jwt-policy.service" g.serviceConfig.ExecStartPost;
|
||||
}
|
||||
{
|
||||
# The host with the node on is the control that each lookup finds what
|
||||
# the host without it lacks.
|
||||
name = "without the node the store has no pod listener, jwt mount, jwt unit or firewall rule for pods";
|
||||
ok =
|
||||
let
|
||||
off = allLocal.systemd.services.swarm-bao-granter-role;
|
||||
on = withNode.systemd.services.swarm-bao-granter-role;
|
||||
in
|
||||
(baoSettings withNode).listener ? pod
|
||||
&& withNode.systemd.services ? swarm-bao-k8s-jwt-policy
|
||||
&& lib.hasInfix "jwt-k8s" on.script
|
||||
&& lib.hasInfix "swarm-bao-k8s-jwt-policy" on.serviceConfig.ExecStartPost
|
||||
&& withNode.networking.firewall.interfaces ? cni0
|
||||
&& !((baoSettings allLocal).listener ? pod)
|
||||
&& !(allLocal.systemd.services ? swarm-bao-k8s-jwt-policy)
|
||||
&& !(lib.hasInfix "jwt-k8s" off.script)
|
||||
&& !(lib.hasInfix "swarm-bao-k8s-jwt-policy" off.serviceConfig.ExecStartPost)
|
||||
&& lib.hasInfix "swarm-bao-operator-viewer-policy" off.serviceConfig.ExecStartPost
|
||||
&& !(allLocal.networking.firewall.interfaces ? cni0)
|
||||
&& !(allLocal.networking.firewall.interfaces ? "flannel.1");
|
||||
}
|
||||
{
|
||||
# Pods log in on a listener that asks for no client certificate, and no
|
||||
# other listener has lost the requirement.
|
||||
name = "the pods' listener asks for no client certificate and every other API listener still does";
|
||||
ok =
|
||||
let
|
||||
l = (baoSettings withNode).listener;
|
||||
others = removeAttrs l [
|
||||
"metrics"
|
||||
"ui"
|
||||
"pod"
|
||||
];
|
||||
in
|
||||
l.pod.type == "tcp"
|
||||
&& l.pod.address == "${withNode.services.hyperhive.network.bridgeIp}:8208"
|
||||
&& l.pod.cluster_address == "127.0.0.1:8209"
|
||||
&& l.pod ? tls_cert_file
|
||||
&& l.pod.tls_client_ca_file == l.loopback.tls_client_ca_file
|
||||
&& !(l.pod ? tls_require_and_verify_client_cert)
|
||||
&& others ? loopback
|
||||
&& lib.all (x: (x.tls_require_and_verify_client_cert or false) == true) (lib.attrValues others);
|
||||
}
|
||||
{
|
||||
# An agent on the hive bridge reaches a port only if the bridge's own
|
||||
# firewall lists it, so the port's absence there is the whole of its
|
||||
# isolation. The bridge's listed ports are the control: they hold the
|
||||
# store's other port.
|
||||
name = "the pods' port is open on the pod network's interfaces and on no other, and not on the hive bridge";
|
||||
ok =
|
||||
let
|
||||
fw = withNode.networking.firewall;
|
||||
portsOn = i: fw.interfaces.${i}.allowedTCPPorts or [ ];
|
||||
cni = withNode.services.hyperhive.deploy.k8s.cniInterfaces;
|
||||
in
|
||||
cni == [
|
||||
"cni0"
|
||||
"flannel.1"
|
||||
]
|
||||
&& lib.all (i: lib.elem 8208 (portsOn i)) cni
|
||||
&& lib.all (i: lib.elem i cni || !(lib.elem 8208 (portsOn i))) (lib.attrNames fw.interfaces)
|
||||
&& lib.elem 8200 (bridgePorts withNode)
|
||||
&& lib.elem 443 (bridgePorts withNode)
|
||||
&& !(lib.elem 8208 (bridgePorts withNode))
|
||||
&& !(lib.elem 8208 fw.allowedTCPPorts)
|
||||
&& !(lib.elem 8208 withNode.services.hyperhive.network.exposeHostPorts);
|
||||
}
|
||||
];
|
||||
in
|
||||
runGroup "k8s" cases
|
||||
|
|
|
|||
Loading…
Reference in a new issue