759 lines
34 KiB
Nix
759 lines
34 KiB
Nix
# `checks.module-eval-k8s` — see ./lib.nix for the shared rationale. The k8s
|
|
# manifests ../host-modules/k8s/render.nix builds from the single-host swarm's
|
|
# units: each workload carries its unit's environment, no credential source
|
|
# reaches the world-readable YAML, and the CronJob follows the timer. And the
|
|
# k3s node ../host-modules/k8s/node.nix adds to that host: present with
|
|
# `deploy.k8s.enable`, absent and changing nothing without it.
|
|
#
|
|
# The all-local host with the node on is this cluster's own fixture; the
|
|
# rest are shared ones.
|
|
{
|
|
pkgs,
|
|
lib,
|
|
self,
|
|
nixosSystem,
|
|
shared,
|
|
}:
|
|
let
|
|
inherit
|
|
(import ./lib.nix {
|
|
inherit
|
|
pkgs
|
|
lib
|
|
self
|
|
nixosSystem
|
|
;
|
|
})
|
|
runGroup
|
|
hive
|
|
hiveWithController
|
|
baoSettings
|
|
bridgePorts
|
|
;
|
|
|
|
allLocal = shared.allLocal;
|
|
|
|
# The four auth responder packages pinned to four distinct derivations, as
|
|
# in ./shared.nix: the all-local host needs them set.
|
|
marker = name: pkgs.runCommand "auth-marker-${name}" { } "mkdir $out";
|
|
|
|
# The all-local host with the node on. It does not set
|
|
# `services.hyperhive.k8s.enable`: the node turns the rendering on.
|
|
nodeSettings = {
|
|
deploy.singleHostSwarm = true;
|
|
deploy.k8s.enable = true;
|
|
deploy.victorialogs.authPackage = marker "logs";
|
|
deploy.victorialogs.grafanaAuthPackage = marker "logs-grafana";
|
|
deploy.victoriametrics.authPackage = marker "metrics";
|
|
deploy.victoriametrics.agentAuthPackage = marker "metrics-agent";
|
|
};
|
|
withNode = hiveWithController nodeSettings;
|
|
|
|
# The same host with the router in the cluster, so it has no container. A
|
|
# second full host, because the claim is about what the configuration
|
|
# drops; the other cases of this group read `withNode`.
|
|
onCluster = hiveWithController (
|
|
lib.recursiveUpdate nodeSettings { deploy.notify-router.runtime = "k8s"; }
|
|
);
|
|
|
|
# A host that asks for the cluster with no node.
|
|
runtimeRefused = hive { deploy.notify-router.runtime = "k8s"; };
|
|
runtimeRefusals = lib.filter (a: !a.assertion) runtimeRefused.assertions;
|
|
|
|
# A node every assertion refuses: no rendering, a pod range containing the
|
|
# hive bridge's subnet, and a service range inside the pod range.
|
|
nodeRefused = hive {
|
|
deploy.k8s.enable = true;
|
|
k8s.enable = false;
|
|
network.enable = true;
|
|
deploy.k8s.podCidr = "10.42.0.0/16";
|
|
deploy.k8s.serviceCidr = "10.42.128.0/17";
|
|
};
|
|
# The node on a host with no hive bridge, so no address to resolve names to.
|
|
nodeNoBridge = hive { deploy.k8s.enable = true; };
|
|
coreDnsKey = "hyperhive-host-services.override";
|
|
refusals = lib.filter (a: !a.assertion) nodeRefused.assertions;
|
|
refuses = needle: lib.any (a: lib.hasInfix needle a.message) refusals;
|
|
|
|
render =
|
|
config:
|
|
(import ../host-modules/k8s/render.nix {
|
|
inherit lib pkgs config;
|
|
}).objects;
|
|
objects = render allLocal;
|
|
|
|
units = {
|
|
swarm-controller = allLocal.systemd.services.swarm-controller;
|
|
swarm-notify-router =
|
|
allLocal.containers.swarm-notify-router.config.systemd.services.swarm-notify-router;
|
|
swarm-ci-metrics = allLocal.systemd.services.swarm-ci-metrics;
|
|
};
|
|
|
|
podSpec =
|
|
name:
|
|
let
|
|
o = objects."${name}/deployment" or objects."${name}/cronjob";
|
|
in
|
|
if o.kind == "CronJob" then o.spec.jobTemplate.spec.template.spec else o.spec.template.spec;
|
|
container = name: lib.head (podSpec name).containers;
|
|
envOf = name: lib.listToAttrs (map (e: lib.nameValuePair e.name e.value) (container name).env);
|
|
|
|
# The unit's environment as the pod should see it: no PATH, and `%d`
|
|
# spelled out as the credential directory.
|
|
unitEnv =
|
|
name:
|
|
lib.mapAttrs (
|
|
_: v: builtins.replaceStrings [ "%d" ] [ "/run/credentials/${name}.service" ] (toString v)
|
|
) (removeAttrs units.${name}.environment [ "PATH" ]);
|
|
|
|
# Where a pod pushes telemetry: the name the hive collector answers to, on
|
|
# the port it binds. A literal, so the render and the collector are not
|
|
# 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 = {
|
|
OTEL_EXPORTER_OTLP_ENDPOINT = collectorEndpoint;
|
|
RUNTIME_DIRECTORY = "/run/swarm-controller";
|
|
STATE_DIRECTORY = "/var/lib/swarm-controller";
|
|
};
|
|
swarm-notify-router = {
|
|
OTEL_EXPORTER_OTLP_ENDPOINT = collectorEndpoint;
|
|
SWARM_NOTIFY_ROUTER_HEALTH_ADDR = "0.0.0.0:8080";
|
|
};
|
|
swarm-ci-metrics = {
|
|
OTEL_EXPORTER_OTLP_ENDPOINT = collectorEndpoint;
|
|
SWARM_CI_METRICS_OTLP_ENDPOINT = collectorEndpoint;
|
|
STATE_DIRECTORY = "/var/lib/swarm-ci-metrics";
|
|
};
|
|
};
|
|
|
|
# Host paths the credentials are loaded from: the host units' own
|
|
# `LoadCredential=id:source`, and the router container's nspawn flags.
|
|
credentialSources =
|
|
lib.concatMap
|
|
(
|
|
e:
|
|
let
|
|
parts = lib.splitString ":" e;
|
|
in
|
|
lib.optional (lib.length parts > 1) (lib.last parts)
|
|
)
|
|
(
|
|
units.swarm-controller.serviceConfig.LoadCredential
|
|
++ units.swarm-ci-metrics.serviceConfig.LoadCredential
|
|
)
|
|
++ map (f: lib.last (lib.splitString ":" f)) allLocal.containers.swarm-notify-router.extraFlags;
|
|
credentialIds = lib.concatMap (
|
|
u: map (e: lib.head (lib.splitString ":" e)) u.serviceConfig.LoadCredential
|
|
) (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;
|
|
|
|
# The same host with one more variable on the poller's unit, naming a path
|
|
# the pod does not mount.
|
|
withStrayPath = lib.recursiveUpdate allLocal {
|
|
systemd.services.swarm-ci-metrics.environment.SWARM_CI_METRICS_STRAY = "/etc/stray.json";
|
|
};
|
|
renders =
|
|
config: (builtins.tryEval (builtins.deepSeq (builtins.toJSON (render config)) true)).success;
|
|
|
|
routerUnit = c: c.services.hyperhive.deploy.notify-router.unit;
|
|
manifestNames = c: lib.sort lib.lessThan (lib.attrNames c.services.k3s.manifests);
|
|
routerPrune = withNode.systemd.services.swarm-notify-router-k8s-prune;
|
|
clusterRouterSpec = (render onCluster)."swarm-notify-router/deployment".spec.template.spec;
|
|
cases = [
|
|
{
|
|
# 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, its health listener and the token login to the store";
|
|
ok =
|
|
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.
|
|
&&
|
|
(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;
|
|
}
|
|
{
|
|
# The ids are the control: they are in the same text, so the search
|
|
# finds what is there.
|
|
name = "the manifests name each credential by id and never the host path it is loaded from";
|
|
ok =
|
|
lib.length credentialSources >= 7
|
|
&& !(lib.any (s: lib.hasInfix s rendered) credentialSources)
|
|
&& 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";
|
|
}
|
|
{
|
|
name = "the poller's CronJob runs on its timer's interval and deadline, never overlapping";
|
|
ok =
|
|
ciTimer.OnUnitInactiveSec == "5min"
|
|
&& cron.schedule == "*/5 * * * *"
|
|
&& cron.concurrencyPolicy == "Forbid"
|
|
&& cron.jobTemplate.spec.activeDeadlineSeconds == 1800;
|
|
}
|
|
{
|
|
name = "the router's probes reach the listener its variable binds, and every pod outlasts the drain";
|
|
ok =
|
|
let
|
|
c = container "swarm-notify-router";
|
|
in
|
|
(lib.head c.ports).containerPort == 8080
|
|
&& c.readinessProbe.httpGet.path == "/readyz"
|
|
&& c.livenessProbe.httpGet.path == "/healthz"
|
|
&& lib.all (name: (podSpec name).terminationGracePeriodSeconds > 10) (lib.attrNames units);
|
|
}
|
|
{
|
|
# The unchanged host is the control that the render itself succeeds.
|
|
name = "a unit variable naming a path the pod does not mount fails the render";
|
|
ok = renders allLocal && !(renders withStrayPath);
|
|
}
|
|
{
|
|
# The host without the node is the control that the lookups find what
|
|
# is there: the same paths on the host with it on.
|
|
name = "the node exists only where deploy.k8s.enable is set";
|
|
ok =
|
|
!allLocal.services.k3s.enable
|
|
&& !(allLocal.systemd.services ? k3s)
|
|
&& allLocal.services.k3s.images == [ ]
|
|
&& !(lib.elem "cni0" allLocal.networking.firewall.trustedInterfaces)
|
|
&& withNode.services.k3s.enable
|
|
&& withNode.services.k3s.role == "server"
|
|
&& withNode.systemd.services ? k3s
|
|
&& lib.elem "cni0" withNode.networking.firewall.trustedInterfaces
|
|
&& lib.elem "flannel.1" withNode.networking.firewall.trustedInterfaces;
|
|
}
|
|
{
|
|
name = "the node runs k3s with traefik and metrics-server off and its secrets encrypted";
|
|
ok =
|
|
let
|
|
start = withNode.systemd.services.k3s.serviceConfig.ExecStart;
|
|
in
|
|
lib.hasInfix "--disable=traefik" start
|
|
&& lib.hasInfix "--disable=metrics-server" start
|
|
&& lib.hasInfix "--secrets-encryption" start
|
|
&& lib.hasInfix "--cluster-cidr=10.200.0.0/16" start
|
|
&& lib.hasInfix "--service-cidr=10.201.0.0/16" start
|
|
&& !(lib.hasInfix "--disable=coredns" start)
|
|
&& !(lib.hasInfix "--disable=local-storage" start);
|
|
}
|
|
{
|
|
# The images are the ones the manifests run, one per rendered service:
|
|
# the three workloads are the control that the list is not empty.
|
|
name = "the node loads the image of every service the manifests run, and k3s's own";
|
|
ok =
|
|
let
|
|
loaded = map toString withNode.services.k3s.images;
|
|
rendered = map toString withNode.system.build.k8sImages;
|
|
in
|
|
lib.length rendered == 3
|
|
&& lib.all (i: lib.elem i loaded) rendered
|
|
&& lib.elem (toString withNode.services.k3s.package.airgap-images) loaded
|
|
&& lib.length loaded == 4;
|
|
}
|
|
{
|
|
# 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 adds only k3s, the jwt unit and the router's prune unit to the all-local host, and changes no container, timer or manifest";
|
|
ok =
|
|
let
|
|
names = a: lib.attrNames a;
|
|
added = lib.subtractLists (names allLocal.systemd.services) (names withNode.systemd.services);
|
|
in
|
|
names allLocal.systemd.timers == names withNode.systemd.timers
|
|
&& 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 || n == "swarm-bao-k8s-jwt-policy" || n == "swarm-notify-router-k8s-prune"
|
|
) added
|
|
&& lib.elem "swarm-bao-k8s-jwt-policy" added
|
|
&& lib.elem "swarm-notify-router-k8s-prune" added
|
|
&&
|
|
lib.all
|
|
(
|
|
n:
|
|
withNode.systemd.services.${n}.serviceConfig.ExecStart or null
|
|
== allLocal.systemd.services.${n}.serviceConfig.ExecStart or null
|
|
)
|
|
(
|
|
lib.subtractLists [
|
|
"firewall"
|
|
"opentelemetry-collector"
|
|
"swarm-bao-granter-role"
|
|
] (names allLocal.systemd.services)
|
|
)
|
|
&& withNode.system.build.k8sManifests.drvPath == allLocal.system.build.k8sManifests.drvPath;
|
|
}
|
|
{
|
|
# The all-local host's assertions are the control: they all hold.
|
|
name = "the node refuses a host that does not render the manifests it loads";
|
|
ok = lib.all (a: a.assertion) withNode.assertions && refuses "deploy.k8s.enable loads the images";
|
|
}
|
|
{
|
|
# The default ranges on the all-local host, whose bridge is up, are the
|
|
# control: the same assertions hold there, and the flags carry the ranges.
|
|
name = "the node refuses pod and service ranges that overlap the hive bridge or each other";
|
|
ok =
|
|
withNode.services.hyperhive.network.enable
|
|
&& withNode.services.hyperhive.network.networkCidr == "10.42.0.0/24"
|
|
&& withNode.services.hyperhive.deploy.k8s.podCidr == "10.200.0.0/16"
|
|
&& withNode.services.hyperhive.deploy.k8s.serviceCidr == "10.201.0.0/16"
|
|
&& refuses "overlap the hive bridge"
|
|
&& refuses "overlap each other";
|
|
}
|
|
{
|
|
# The names are the control that the search finds something: the swarm's
|
|
# own bao name and the hive domain are among them, and every one is in the
|
|
# A and AAAA templates.
|
|
name = "the node's CoreDNS answers every swarm name this host serves with the hive bridge's address";
|
|
ok =
|
|
let
|
|
h = withNode.services.hyperhive;
|
|
cm = withNode.services.k3s.manifests.coredns-custom.content;
|
|
text = cm.data.${coreDnsKey};
|
|
names = lib.unique ([ h.domain ] ++ h.gateway.localNames);
|
|
in
|
|
cm.kind == "ConfigMap"
|
|
&& cm.metadata.name == "coredns-custom"
|
|
&& cm.metadata.namespace == "kube-system"
|
|
&& lib.length names >= 6
|
|
&& lib.elem h.swarm.bao.domain names
|
|
&& lib.all (n: lib.hasInfix (builtins.replaceStrings [ "." ] [ "[.]" ] n) text) names
|
|
&& lib.hasInfix "template IN A {" text
|
|
&& lib.hasInfix "template IN AAAA {" text
|
|
&& lib.hasInfix "answer \"{{ .Name }} 60 IN A ${h.network.bridgeIp}\"" text
|
|
&& lib.hasInfix " fallthrough\n}\n" text
|
|
&& !(lib.hasInfix "127.0.0.1" text)
|
|
&& !(lib.hasInfix "hosts" text)
|
|
&& lib.attrNames h.deploy.k8s.hostServices == lib.sort lib.lessThan names
|
|
&& lib.all (s: s.address == h.network.bridgeIp) (lib.attrValues h.deploy.k8s.hostServices);
|
|
}
|
|
{
|
|
# The host with the node on and a bridge is the control that the manifest
|
|
# is there to be absent.
|
|
name = "the node applies the CoreDNS override only on a host with the hive bridge";
|
|
ok =
|
|
withNode.services.hyperhive.network.enable
|
|
&& withNode.services.k3s.manifests ? coredns-custom
|
|
&& allLocal.services.k3s.manifests == { }
|
|
&& !nodeNoBridge.services.hyperhive.network.enable
|
|
&& nodeNoBridge.services.hyperhive.domain == "h1.t.local"
|
|
&& nodeNoBridge.services.hyperhive.deploy.k8s.hostServices == { }
|
|
&& !(nodeNoBridge.services.k3s.manifests ? coredns-custom);
|
|
}
|
|
{
|
|
# The units' own endpoints are the control: each differs from the
|
|
# collector's, so the override replaces something. The host's units keep
|
|
# theirs.
|
|
name = "every pod pushes telemetry to the hive collector, and the host units keep their endpoints";
|
|
ok =
|
|
lib.all (
|
|
name:
|
|
(envOf name).OTEL_EXPORTER_OTLP_ENDPOINT == collectorEndpoint
|
|
&& (units.${name}.environment.OTEL_EXPORTER_OTLP_ENDPOINT or null) != collectorEndpoint
|
|
) (lib.attrNames units)
|
|
&& units.swarm-ci-metrics.environment.SWARM_CI_METRICS_OTLP_ENDPOINT != collectorEndpoint
|
|
&& (envOf "swarm-ci-metrics").SWARM_CI_METRICS_OTLP_ENDPOINT == collectorEndpoint
|
|
&& !(envOf "swarm-notify-router" ? SWARM_CI_METRICS_OTLP_ENDPOINT)
|
|
&&
|
|
allLocal.systemd.services.swarm-controller.environment.OTEL_EXPORTER_OTLP_ENDPOINT
|
|
== "https://${allLocal.services.hyperhive.swarm.otel.domain}/${allLocal.services.hyperhive.swarm.otel.producerName}";
|
|
}
|
|
{
|
|
# Off, the collector's configuration holds nothing of the pod logs, and
|
|
# on, taking that out leaves the configuration it has off. The host with
|
|
# the node on is the control that the lookups find what is there.
|
|
name = "the collector reads pod logs only where deploy.k8s.enable is set";
|
|
ok =
|
|
let
|
|
mine = {
|
|
receivers = [ "file_log/pods" ];
|
|
processors = [ "transform/pod-service-name" ];
|
|
};
|
|
settings = c: c.services.opentelemetry-collector.settings;
|
|
bare =
|
|
s:
|
|
s
|
|
// {
|
|
receivers = removeAttrs s.receivers mine.receivers;
|
|
processors = removeAttrs s.processors mine.processors;
|
|
service = s.service // {
|
|
pipelines = removeAttrs s.service.pipelines [ "logs/pods" ];
|
|
};
|
|
};
|
|
unit = c: c.systemd.services.opentelemetry-collector.serviceConfig;
|
|
collectorAcls = c: lib.filter (lib.hasInfix "u:opentelemetry-collector:") c.systemd.tmpfiles.rules;
|
|
in
|
|
!((settings allLocal).receivers ? "file_log/pods")
|
|
&& !((settings allLocal).service.pipelines ? "logs/pods")
|
|
&& (settings withNode).receivers ? "file_log/pods"
|
|
&& (settings withNode).service.pipelines ? "logs/pods"
|
|
&& bare (settings withNode) == settings allLocal
|
|
&& (unit allLocal).DynamicUser
|
|
&& !(unit allLocal ? User)
|
|
&& !(allLocal.users.users ? opentelemetry-collector)
|
|
&& collectorAcls allLocal == [ ]
|
|
&& !(unit withNode).DynamicUser
|
|
&& (unit withNode).User == "opentelemetry-collector"
|
|
&& withNode.users.users.opentelemetry-collector.isSystemUser;
|
|
}
|
|
{
|
|
# The receiver reads the files kubelet's runtime writes, the parser reads
|
|
# the CRI framing and then hive-log's level, and the service name is the
|
|
# container's without `swarm-`. The ACL is on /var/log/pods alone, access
|
|
# and default, for the collector's user.
|
|
name = "the pod logs receiver parses CRI and the text level, names the service, and the ACL covers /var/log/pods only";
|
|
ok =
|
|
let
|
|
s = withNode.services.opentelemetry-collector.settings;
|
|
r = s.receivers."file_log/pods";
|
|
ops = r.operators;
|
|
op = t: lib.findFirst (o: o.type == t) null ops;
|
|
statements = lib.concatMap (
|
|
l: l.statements
|
|
) s.processors."transform/pod-service-name".log_statements;
|
|
acls = lib.filter (lib.hasInfix "u:opentelemetry-collector:") withNode.systemd.tmpfiles.rules;
|
|
in
|
|
r.include == [ "/var/log/pods/*/*/*.log" ]
|
|
&& r.include_file_path
|
|
&& r.storage == "file_storage"
|
|
&& (lib.head ops).type == "container"
|
|
&& lib.all (l: lib.hasInfix l (op "regex_parser").regex) [
|
|
"WARN"
|
|
"ERROR"
|
|
"INFO"
|
|
"DEBUG"
|
|
"TRACE"
|
|
]
|
|
&& (op "severity_parser").parse_from == "attributes.level"
|
|
&& (op "severity_parser").overwrite_text
|
|
&& lib.elem ''set(attributes["service.name"], attributes["k8s.container.name"])'' statements
|
|
&& lib.elem ''replace_pattern(attributes["service.name"], "^swarm-", "")'' statements
|
|
&&
|
|
s.service.pipelines."logs/pods" == {
|
|
receivers = [ "file_log/pods" ];
|
|
processors = [ "transform/pod-service-name" ];
|
|
exporters = [ "otlphttp/swarm" ];
|
|
}
|
|
&& s.exporters ? "otlphttp/swarm"
|
|
&& lib.elem "file_storage" s.service.extensions
|
|
&&
|
|
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);
|
|
}
|
|
{
|
|
# The host with the router on it is the control for every absence below:
|
|
# it holds the container, the prune unit and the pieces the cluster
|
|
# case lacks.
|
|
name = "a router on the host keeps its container, the node adds no manifest and no CA unit, and its prune unit runs before the container";
|
|
ok =
|
|
withNode.containers ? swarm-notify-router
|
|
&& withNode.systemd.services ? "container@swarm-notify-router"
|
|
&& manifestNames withNode == [ "coredns-custom" ]
|
|
&& !(withNode.systemd.services ? hyperhive-k8s-ca-trust)
|
|
&& routerPrune.before == [ "container@swarm-notify-router.service" ]
|
|
&& routerPrune.requiredBy == [ "container@swarm-notify-router.service" ]
|
|
&& lib.hasInfix "swarm-notify-router.yaml" routerPrune.script
|
|
&& allLocal.services.k3s.manifests == { }
|
|
&& !(allLocal.systemd.services ? swarm-notify-router-k8s-prune)
|
|
&& !(allLocal.systemd.services ? hyperhive-k8s-ca-trust);
|
|
}
|
|
{
|
|
name = "a router in the cluster leaves the host no container and no prune unit, and applies the namespace and its objects";
|
|
ok =
|
|
!(onCluster.containers ? swarm-notify-router)
|
|
&& !(onCluster.systemd.services ? "container@swarm-notify-router")
|
|
&& !(onCluster.systemd.services ? swarm-notify-router-k8s-prune)
|
|
&&
|
|
manifestNames onCluster == [
|
|
"coredns-custom"
|
|
"hyperhive-namespace"
|
|
"swarm-notify-router"
|
|
]
|
|
&& onCluster.services.k3s.manifests.hyperhive-namespace.content.kind == "Namespace"
|
|
&&
|
|
lib.sort lib.lessThan (map (o: o.kind) onCluster.services.k3s.manifests.swarm-notify-router.content)
|
|
== [
|
|
"Deployment"
|
|
"ServiceAccount"
|
|
];
|
|
}
|
|
{
|
|
name = "the router's manifests and its unit are the same whether the container or the stand-alone evaluation supplies the unit";
|
|
ok =
|
|
(routerUnit withNode).environment ? SWARM_NOTIFY_ROUTER_NATS_URL
|
|
&& (routerUnit onCluster).environment == (routerUnit withNode).environment
|
|
&& (routerUnit onCluster).serviceConfig.ExecStart == (routerUnit withNode).serviceConfig.ExecStart
|
|
&&
|
|
(routerUnit onCluster).serviceConfig.LoadCredential == (routerUnit withNode)
|
|
.serviceConfig.LoadCredential
|
|
&& render onCluster == render withNode;
|
|
}
|
|
{
|
|
# The controller's pod is the control for the Secret lookup: its
|
|
# credentials still come from one.
|
|
name = "the router's CA file comes from the ConfigMap the host publishes, and the pod mounts no Secret";
|
|
ok =
|
|
let
|
|
volumes = clusterRouterSpec.volumes;
|
|
credentials = lib.findFirst (v: v.name == "credentials") null volumes;
|
|
sources = credentials.projected.sources;
|
|
caUnit = onCluster.systemd.services.hyperhive-k8s-ca-trust;
|
|
serverCa = onCluster.services.hyperhive.deploy.bao.serverCaFile;
|
|
in
|
|
credentials != null
|
|
&& lib.length sources == 1
|
|
&& (lib.head sources).configMap.name == "hyperhive-ca-trust"
|
|
&&
|
|
map (i: [
|
|
i.key
|
|
i.path
|
|
]) (lib.head sources).configMap.items == [
|
|
[
|
|
"bao-server-ca.pem"
|
|
"notify-router-bao-server-ca"
|
|
]
|
|
]
|
|
&& !(lib.any (v: v ? secret) volumes)
|
|
&& lib.any (v: v ? secret) (podSpec "swarm-controller").volumes
|
|
&& lib.hasInfix "--from-file=bao-server-ca.pem=${serverCa}" caUnit.script
|
|
&& lib.hasInfix "--from-file=trust-bundle.pem=" caUnit.script
|
|
&& lib.elem "multi-user.target" caUnit.wantedBy
|
|
# A change to either file publishes again with no manual step, and the
|
|
# host with the router on its container has no path unit.
|
|
&&
|
|
lib.sort lib.lessThan onCluster.systemd.paths.hyperhive-k8s-ca-trust.pathConfig.PathChanged
|
|
== lib.sort lib.lessThan [
|
|
serverCa
|
|
"${onCluster.services.hyperhive.deploy.hive-controller.tls.stateDir}/trust-bundle.pem"
|
|
]
|
|
&& !(caUnit.serviceConfig ? RemainAfterExit)
|
|
&& !(withNode.systemd.paths ? hyperhive-k8s-ca-trust);
|
|
}
|
|
{
|
|
name = "the router's runtime \"k8s\" needs the node";
|
|
ok =
|
|
lib.any (a: lib.hasInfix "runtime = \"k8s\" needs the" a.message) runtimeRefusals
|
|
&& lib.all (a: a.assertion) onCluster.assertions;
|
|
}
|
|
];
|
|
in
|
|
runGroup "k8s" cases
|