Watch
0
0
Fork
You've already forked hyperhive
0

full-setup and the host swarmctl reach the controller as a pod, not through a host socket

This commit is contained in:
atlas 2026-11-26 09:22:29 +01:00 • committed by mara
commit 087c51eef6
5 changed files with 222 additions and 157 deletions

View file

@ -136,14 +136,6 @@ let
SWARM_CONTROLLER_AGENT_PKI_ROLE = deployCfg.bao.agentPkiRoleName;
};
# Where `swarmctl agent create` posts to. Unconditional: this binary is
# only installed where the daemon runs, so the socket is always the one
# right below it — and it is the same `socketPath` the daemon binds, read
# once rather than restated.
controllerSocketEnv = {
SWARM_CONTROLLER_SOCKET = deployCfg.swarm-controller.socketPath;
};
forgeCfg = config.services.hyperhive.swarm.forge;
swarmDomain = config.services.hyperhive.swarm.domain;
matrixCfg = config.services.hyperhive.swarm.matrix;
@ -159,10 +151,8 @@ let
# The swarm the host's `swarmctl` talks to: this host's own, through the
# gateway, as the operator's own SSO account (`swarmctl login`). The
# controller socket then serves the gateway alone, which always sets
# `Remote-User`. Both are defaults, so an operator's own value wins: an
# empty `SWARMCTL_SWARM` puts the socket verbs back on the socket for
# root.
# controller runs as a pod and has no socket on the host, so the gateway is
# the only way in. Both are defaults, so an operator's own value wins.
swarmDefaultEnv = lib.optionalAttrs (swarmDomain != null) {
SWARMCTL_SWARM = "https://${swarmDomain}";
SWARMCTL_LOGIN_DEFAULT_SWARM = "https://${swarmDomain}";
@ -341,7 +331,7 @@ let
wrapProgram $out/bin/swarmctl ${
lib.concatStringsSep " " (
lib.mapAttrsToList (name: value: "--set ${name} ${lib.escapeShellArg value}") (
controllerSocketEnv // swarmctlTrustEnv // deployCfg.swarm-controller.swarmctlEnvironment
swarmctlTrustEnv // deployCfg.swarm-controller.swarmctlEnvironment
)
++ lib.mapAttrsToList (
name: value: "--set-default ${name} ${lib.escapeShellArg value}"
@ -662,10 +652,8 @@ in
per-agent sockets, and the directory is `0750` owned by the
`${clientsGroup}` group. Members of that group (the gateway's
nginx, for `/api/` and `/webhook/forge/`) and root (which bypasses mode
bits) can reach the socket. The host's `swarmctl` goes through the
gateway by default, so only a root-run verb with an empty
`SWARMCTL_SWARM` opens it; every other local
user gets `EACCES` at the directory. What keeps `0666` safe is that
bits) can reach the socket. The host's `swarmctl` does not open it: it
goes through the gateway, and sets no socket path. What keeps `0666` safe is that
the directory holds exactly one socket.
Moving this path under a directory that carries anything else —
`/run/hyperhive`, which holds the host admin socket, above all —
@ -838,7 +826,7 @@ in
internal = true;
description = ''
Variables the installed `swarmctl` wrapper sets, on top of the
controller's socket path. Empty unless another module on this host
trust bundle. Empty unless another module on this host
supplies some: ./glue-controller-env.nix adds the configured authelia
where authelia runs here.
'';

View file

@ -154,13 +154,14 @@ let
or null == "https://matrix.hive.t.local";
}
{
# The host's swarmctl reaches the gateway by default and the socket only when
# the operator empties `SWARMCTL_SWARM`: `--set-default` lets that empty value
# through, `--set` would not. The socket variable is the control that the
# wrapper text was found at all.
name = "the host's swarmctl defaults to its own swarm's gateway, and an operator's own value wins";
# The host's swarmctl reaches the gateway by default and sets no controller socket: the
# controller is a pod, so the host has none. `--set-default` lets an operator's own value
# through, `--set` would not. The trust variable is the control that the wrapper text was
# found at all.
name = "the host's swarmctl defaults to its own swarm's gateway with no controller socket, and an operator's own value wins";
ok =
lib.hasInfix "--set SWARM_CONTROLLER_SOCKET " swarmctlWrapperCmd
lib.hasInfix "--set SSL_CERT_FILE " swarmctlWrapperCmd
&& !(lib.hasInfix "SWARM_CONTROLLER_SOCKET" swarmctlWrapperCmd)
&& lib.hasInfix "--set-default SWARMCTL_SWARM https://t.local" swarmctlWrapperCmd
&& lib.hasInfix "--set-default SWARMCTL_LOGIN_DEFAULT_SWARM https://t.local" swarmctlWrapperCmd
&& !(lib.hasInfix "--set SWARMCTL_SWARM" swarmctlWrapperCmd);

View file

@ -9,16 +9,16 @@
# needs a TPM). A pkcs11 store unseals itself at init, so here
# the unseal comes right after init, before the granter.
# 2 SSO account `swarmctl user add mara --group admins`.
# 8 spawn `POST /api/agents` on the controller socket, as the swarm
# 8 spawn `POST /api/agents` on the controller pod's listener, as the swarm
# UI's create form does, with the reviewer archetype's defaults.
# 9 first agent The same endpoint, for `iris`: `swarmctl agent create` has no
# archetype flag, and a custom agent's config repo would need
# inputs the VM cannot resolve offline.
# `swarmctl setup status` is read before the agents deploy: steps 1-6 done; step 9
# (the product's hard-coded `ruth`) stays undone, as no root agent exists here. Not
# scripted yet: step 3, the first SSO login to the forge (an OIDC code flow against
# authelia), and `swarmctl forge make-admin`.
# Green when `GET /api/agents/status` on the controller socket lists `iris` and
# The script reaches the controller pod's TLS listener as the gateway does (ClusterIP, services
# root, gateway client leaf). `swarmctl login` needs a browser, so the host's swarmctl stops at
# "not logged in": `setup status` leaves steps 9 and 10 unknown, `schedule list` names the login.
# Not scripted yet: step 3, the first SSO login to the forge, and `swarmctl forge make-admin`.
# Green when `GET /api/agents/status` on the controller pod's listener lists `iris` and
# `spawn` with `freshness` fresh and `needs_login` true.
#
# Offline: the test seeds the swarm's forge with `hyperhive/hyperhive` (one
@ -140,7 +140,12 @@ in
# find their binaries on the node's PATH.
test.overrideTestDerivation (old: {
env = (old.env or { }) // {
FULL_SETUP_SOCKET = deploy.swarm-controller.socketPath;
FULL_SETUP_CONTROLLER_NAME = swarm.controller.tcp.domain;
FULL_SETUP_CONTROLLER_PORT = toString swarm.controller.tcp.port;
FULL_SETUP_CONTROLLER_SERVICE = deploy.swarm-controller.tcp.serviceHost;
FULL_SETUP_CONTROLLER_CERT = deploy.swarm-controller.tcp.gatewayClientCertFile;
FULL_SETUP_CONTROLLER_KEY = deploy.swarm-controller.tcp.gatewayClientKeyFile;
FULL_SETUP_SERVICES_ROOT = "${deploy.hive-controller.tls.stateDir}/swarm-services-root.pem";
FULL_SETUP_FORGE_DOMAIN = swarm.forge.domain;
FULL_SETUP_SWARM_DOMAIN = swarm.domain;
FULL_SETUP_HIVE = hive;

View file

@ -2,8 +2,6 @@ import json
import os
import shlex
API = "http://localhost"
SOCKET = os.environ["FULL_SETUP_SOCKET"]
FORGE = "https://" + os.environ["FULL_SETUP_FORGE_DOMAIN"]
HIVE = os.environ["FULL_SETUP_HIVE"]
TOPLEVELS = json.loads(os.environ["FULL_SETUP_TOPLEVELS"])
@ -11,10 +9,19 @@ REPO = os.environ["FULL_SETUP_HYPERHIVE_REPO"]
CONTROL_NIX = os.environ["FULL_SETUP_CONTROL_NIX"]
# Where `swarmctl bao init` saves what `bao operator init` printed, for root.
INIT = "/root/.local/state/swarmctl/bao-init.json"
# The host's swarmctl defaults to the swarm's gateway, where each controller verb needs a
# `swarmctl login`, which a VM without a browser cannot do. An empty `SWARMCTL_SWARM` puts a
# verb on the controller socket.
VIA_SOCKET = "SWARMCTL_SWARM="
# The controller is a pod, so the host reaches its TLS listener the way the gateway does: the
# Service's ClusterIP, the swarm services root as trust, and the gateway's client leaf. The host's
# swarmctl goes through the gateway instead, where each controller verb needs a `swarmctl login`,
# whose browser flow a VM cannot complete.
CONTROLLER_NAME = os.environ["FULL_SETUP_CONTROLLER_NAME"]
CONTROLLER_PORT = os.environ["FULL_SETUP_CONTROLLER_PORT"]
CONTROLLER_URL = f"https://{CONTROLLER_NAME}:{CONTROLLER_PORT}"
CONTROLLER_CURL = (
f"curl --silent --cacert {os.environ['FULL_SETUP_SERVICES_ROOT']} "
f"--cert {os.environ['FULL_SETUP_CONTROLLER_CERT']} --key {os.environ['FULL_SETUP_CONTROLLER_KEY']} "
f"--resolve {CONTROLLER_NAME}:{CONTROLLER_PORT}:"
f"$(getent ahostsv4 {os.environ['FULL_SETUP_CONTROLLER_SERVICE']} | awk 'NR==1 {{print $1}}')"
)
SWARM_URL = "https://" + os.environ["FULL_SETUP_SWARM_DOMAIN"]
FORGE_AUTH = "/root/forge-auth.header"
NIX = "nix --extra-experimental-features nix-command"
@ -26,17 +33,6 @@ POLICY_UNITS_ACTIVE = (
)
def controller(method, path, body=None, check="--fail-with-body"):
data = (
f"-H 'Content-Type: application/json' -d {shlex.quote(json.dumps(body))}"
if body
else ""
)
return machine.succeed(
f"curl {check} --silent --unix-socket {SOCKET} -X {method} {API}{path} {data}"
)
def forge(method, path, body=None):
data = (
f"-H 'Content-Type: application/json' -d {shlex.quote(json.dumps(body))}"
@ -84,8 +80,8 @@ def create_agent(body):
# archetype list comes from the builder's evaluation), so ask again.
for _ in range(60):
status, out = machine.execute(
f"curl --silent --unix-socket {SOCKET} -o /root/create.out -w '%{{http_code}}' "
f"-X POST {API}/api/agents -H 'Content-Type: application/json' -d {shlex.quote(json.dumps(body))}"
f"{CONTROLLER_CURL} -o /root/create.out -w '%{{http_code}}' "
f"-X POST {CONTROLLER_URL}/api/agents -H 'Content-Type: application/json' -d {shlex.quote(json.dumps(body))}"
)
if out.strip().startswith("2"):
return
@ -306,7 +302,7 @@ with subtest(
"step 8: spawn, a reviewer-archetype agent, through the endpoint the swarm UI calls"
):
machine.wait_until_succeeds(
f"curl --fail --silent --unix-socket {SOCKET} {API}/api/permissions >/dev/null",
f"{CONTROLLER_CURL} --fail {CONTROLLER_URL}/api/permissions >/dev/null",
timeout=900,
)
# A custom root agent (ruth, as the UI creates her) is a follow-up, not covered yet.
@ -343,8 +339,10 @@ with subtest("internal/meta holds the flake the toplevels were built from"):
)
with subtest("swarmctl setup status reports the steps done so far"):
# Step 9 names ruth, the root agent of the real setup; this test creates none, so the report
# ends on step 9 not done and the exit status is 1.
# The host's swarmctl goes through the gateway and no login exists in the VM, so steps 1-6,
# which read the host, are reported and the two that read the controller are unknown, with
# the login named as the reason. Step 9 names ruth, the root agent of the real setup, which
# this test does not create, so the exit status is 1 either way.
reported = [
"✅ 1 secret store initialised",
"✅ 2 secret store unsealed",
@ -352,12 +350,14 @@ with subtest("swarmctl setup status reports the steps done so far"):
"✅ 4 granting units active",
"✅ 5 store unsealed after the granting units",
"✅ 6 SSO account in the admins group",
"👉 9 ruth created and up",
"✅ 10 another agent [optional]",
]
unreadable = ["❓ 9 ", "❓ 10 "]
for _ in range(60):
lines = machine.execute(f"{VIA_SOCKET} swarmctl setup status")[1].splitlines()
if all(any(line.startswith(prefix) for line in lines) for prefix in reported):
lines = machine.execute("swarmctl setup status")[1].splitlines()
if all(
any(line.startswith(prefix) for line in lines)
for prefix in reported + unreadable
):
break
machine.sleep(10)
else:
@ -365,23 +365,25 @@ with subtest("swarmctl setup status reports the steps done so far"):
"swarmctl setup status did not report the steps done so far:\n"
+ "\n".join(lines)
)
assert machine.execute(f"{VIA_SOCKET} swarmctl setup status >/dev/null")[0] == 1
for prefix in unreadable:
line = next(line for line in lines if line.startswith(prefix))
assert f"not logged in to {SWARM_URL}" in line, line
assert machine.execute("swarmctl setup status >/dev/null")[0] == 1
with subtest(
"the host's swarmctl reaches the controller through the gateway by default"
"the host's swarmctl reaches the controller through the gateway, and stops at the login"
):
# No login exists in the VM, so a verb that names the controller stops at the login, and
# says which swarm it went to. Over the socket the same verb needs none.
# No login exists in the VM, so a verb that names the controller says which swarm it went to
# and that it is not logged in.
status, out = machine.execute("swarmctl schedule list 2>&1")
assert status != 0 and f"not logged in to {SWARM_URL}" in out, (status, out)
machine.succeed(f"{VIA_SOCKET} swarmctl schedule list")
with subtest("swarmctl login discovers the swarm's SSO provider from the gateway"):
# The swarm's own CA signs the gateway's leaf. A VM has no browser, so the login runs to the
# point where it prints the authorization URL and waits for the answer, which the timeout ends.
# Reaching that URL takes the gateway's 401 challenge, its published metadata, and authelia
# accepting the pushed authorization request.
ca = "/var/lib/hive-tls/swarm-services-root.pem"
ca = os.environ["FULL_SETUP_SERVICES_ROOT"]
auth = f"https://auth.{SWARM_URL.removeprefix('https://')}"
challenge = machine.succeed(
f"curl --silent --cacert {ca} -D - -o /dev/null {SWARM_URL}/api/"
@ -408,7 +410,7 @@ with subtest("swarmctl login discovers the swarm's SSO provider from the gateway
with subtest("the agents are deployed and need a login"):
settle(
f"curl --fail --silent --unix-socket {SOCKET} {API}/api/agents/status "
f"{CONTROLLER_CURL} --fail {CONTROLLER_URL}/api/agents/status "
"| jq -e '[.[] | select(.hive == \""
+ HIVE
+ '" and .freshness == "fresh" and .needs_login == true)'

View file

@ -136,7 +136,7 @@ let
# evaluate no node.
scriptWith =
{
controllerSocket,
controller,
publicNames,
swarm,
nameOf,
@ -149,8 +149,14 @@ let
import shlex
import time
API = "http://localhost"
SOCKET = "${controllerSocket}"
# The controller is a pod: the swarm machine reaches its TLS listener the way the gateway
# does, by the Service's ClusterIP with the services root as trust and the gateway's client leaf.
CONTROLLER_URL = "https://${controller.name}:${toString controller.port}"
CONTROLLER_CURL = (
"curl --silent --cacert ${controller.root} --cert ${controller.cert} --key ${controller.key} "
"--resolve ${controller.name}:${toString controller.port}:"
"$(getent ahostsv4 ${controller.service} | awk 'NR==1 {print $1}')"
)
FORGE = "https://${publicNames.forge}"
HIVE = "${hiveName}"
TOPLEVELS = ${builtins.toJSON toplevelPaths}
@ -297,12 +303,6 @@ let
diagnose(swarm, "openbao", "${swarm.bao.machine}")
raise
def controller(method, path, body=None, check="--fail-with-body"):
data = f"-H 'Content-Type: application/json' -d {shlex.quote(json.dumps(body))}" if body else ""
return swarm.succeed(
f"curl {check} --silent --unix-socket {SOCKET} -X {method} {API}{path} {data}"
)
def forge(method, path, body=None):
data = f"-H 'Content-Type: application/json' -d {shlex.quote(json.dumps(body))}" if body else ""
return swarm.succeed(
@ -315,8 +315,8 @@ let
# archetype list comes from the builder's evaluation), so ask again.
for _ in range(60):
status, out = swarm.execute(
f"curl --silent --unix-socket {SOCKET} -o /root/create.out -w '%{{http_code}}' "
f"-X POST {API}/api/agents -H 'Content-Type: application/json' -d {shlex.quote(json.dumps(body))}"
f"{CONTROLLER_CURL} -o /root/create.out -w '%{{http_code}}' "
f"-X POST {CONTROLLER_URL}/api/agents -H 'Content-Type: application/json' -d {shlex.quote(json.dumps(body))}"
)
if out.strip().startswith("2"):
return
@ -563,7 +563,7 @@ let
with subtest("setup.md steps 8 and 9: two agents, through the endpoint the swarm UI calls"):
swarm.wait_until_succeeds(
f"curl --fail --silent --unix-socket {SOCKET} {API}/api/permissions >/dev/null", timeout=1800
f"{CONTROLLER_CURL} --fail {CONTROLLER_URL}/api/permissions >/dev/null", timeout=1800
)
created = int(swarm.succeed("date +%s"))
create_agent({"name": "spawn", "hive": HIVE, "archetype": ARCHETYPE})
@ -571,7 +571,7 @@ let
with subtest("assert 4: the agents are deployed on the hive, from the cache on heavyduty, and need a login"):
settle_hive_agents(
f"curl --fail --silent --unix-socket {SOCKET} {API}/api/agents/status "
f"{CONTROLLER_CURL} --fail {CONTROLLER_URL}/api/agents/status "
"| jq -e '[.[] | select(.hive == \"" + HIVE + "\" and .freshness == \"fresh\" and .needs_login == true)"
" | .name] | map(select(. == \"iris\" or . == \"spawn\")) | unique | length == 2'",
1200,
@ -592,7 +592,14 @@ pkgs.testers.runNixOSTest (
let
swarmCfg = config.nodes.swarm.services.hyperhive;
inherit (swarmCfg) swarm;
controllerSocket = swarmCfg.deploy.swarm-controller.socketPath;
controller = {
name = swarm.controller.tcp.domain;
inherit (swarm.controller.tcp) port;
service = swarmCfg.deploy.swarm-controller.tcp.serviceHost;
cert = swarmCfg.deploy.swarm-controller.tcp.gatewayClientCertFile;
key = swarmCfg.deploy.swarm-controller.tcp.gatewayClientKeyFile;
root = "${swarmCfg.deploy.hive-controller.tls.stateDir}/swarm-services-root.pem";
};
settingsJson =
config.nodes.swarm.systemd.services.swarm-controller.environment.SWARM_CONTROLLER_META_FLAKE;
@ -761,7 +768,7 @@ pkgs.testers.runNixOSTest (
testScript = scriptWith {
inherit
controllerSocket
controller
publicNames
swarm
nameOf
@ -773,7 +780,14 @@ pkgs.testers.runNixOSTest (
# What `nix/vm-test-typecheck.nix` checks: the same text with stand-in values.
passthru.typecheckScript = scriptWith {
controllerSocket = "/stub/socket";
controller = {
name = "controller.stub";
port = 9443;
service = "controller.stub.svc";
cert = "/stub/cert.pem";
key = "/stub/key.pem";
root = "/stub/root.pem";
};
publicNames.forge = "forge.stub";
swarm = {
bao.machine = "stub";