boatramp_node/node.rs
1//! The node-graph assembly: given a built store (blobs + KV), a configured
2//! [`Auth`](boatramp_server::Auth), and resolved
3//! [`ServerOptions`](boatramp_server::ServerOptions), wire the deploy store,
4//! handler runtime, compute reconcile loop, and domain-verify reconcile loop
5//! into a [`RunningNode`] ready to hand to a transport (`serve_with` & friends).
6//!
7//! This is the headline extraction of `PLAN-node-library`: the binary's
8//! `serve::run` used to inline this wiring, so no embedder or in-process test
9//! could exercise the same graph the `boatramp serve` binary runs. `run` now
10//! resolves the *environment* (args -> backends -> store, signal handlers,
11//! migration, auth) and calls [`assemble`]; the cluster path keeps its own inline
12//! copy until a later step converges it here.
13
14use std::path::Path;
15use std::sync::Arc;
16
17use boatramp_core::deploy::DeployStore;
18use boatramp_core::kv::KvStore;
19use boatramp_core::Storage;
20
21use crate::config::ServerConfig;
22use crate::error::{Error, Result};
23
24/// How often the compute reconcile loop converges desired vs actual workloads.
25/// Defaults to 30s; override with `BOATRAMP_COMPUTE_RECONCILE_TICK_MS` (milliseconds)
26/// so compute-backed tests can converge in a fraction of a second instead of
27/// waiting a full tick for the launch/scale reconcile.
28pub fn compute_reconcile_tick() -> std::time::Duration {
29 std::env::var("BOATRAMP_COMPUTE_RECONCILE_TICK_MS")
30 .ok()
31 .and_then(|s| s.parse::<u64>().ok())
32 .filter(|&ms| ms > 0)
33 .map(std::time::Duration::from_millis)
34 .unwrap_or(std::time::Duration::from_secs(30))
35}
36/// How often the domain-verify reconcile loop re-checks pending challenges.
37pub const DOMAIN_VERIFY_RECONCILE_TICK: std::time::Duration = std::time::Duration::from_secs(60);
38/// How long a compute workload may be idle before scale-to-zero sleeps it.
39pub const COMPUTE_IDLE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(300);
40
41/// The built store + resolved config handed to [`assemble`]. Owns the blob/KV
42/// backends and the auth/options the caller already resolved; borrows the parsed
43/// config and data directory.
44pub struct NodeInput<'a> {
45 /// The full parsed server config (the handler + compute sections are read here).
46 pub config: &'a ServerConfig,
47 /// The node data directory (per-site SQL, handler state).
48 pub data_dir: &'a Path,
49 /// The object store built by [`crate::blobs::build_blobs`].
50 pub storage: Arc<dyn Storage>,
51 /// The metadata KV, already cache-fronted, built by [`crate::backends::build_kv`].
52 pub kv: Arc<dyn KvStore>,
53 /// The control-plane auth built by [`crate::auth::configure_auth`].
54 pub auth: boatramp_server::Auth,
55 /// Server options, already carrying the resolved posture, daemon runtime, and
56 /// (post-`configure_auth`/`configure_oidc`) issuer / OIDC verifier.
57 pub options: boatramp_server::ServerOptions,
58 /// The public HTTP serve bind address, if known — used (under
59 /// `allow_guest_self_egress`) to let a handler guest's `wasi:http` reach this
60 /// instance's own front door over loopback. `None` (an in-process embedder with no
61 /// listener) disables self-egress.
62 pub serve_addr: Option<std::net::SocketAddr>,
63 /// The cloud blob-change watch provider (FA-5b2), if the backend is a cloud one.
64 pub watch_provider: Option<Arc<dyn boatramp_core::blob_provision::WatchProvider>>,
65 /// The provisioning tier for the watch provider.
66 pub provision_tier: boatramp_core::blob_notify::ProvisionTier,
67 /// The `wasi:messaging` substrate override for the handler runtime. `None` uses
68 /// the single-node default (`LogMessaging` over the same backends); the cluster
69 /// path passes its Raft-backed coordinator.
70 pub messaging: Option<Arc<dyn boatramp_core::messaging::Messaging>>,
71 /// The single leader gate for cron firing + the compute / domain-verify reconcile
72 /// loops. Single-node passes an always-true gate (there is one node); the cluster
73 /// passes its Raft `is_leader` check so a single node drives each sweep.
74 pub is_leader: boatramp_server::CronLeaderGate,
75 /// This node's compute scheduler id (`0` single-node; the cluster node id in a
76 /// fleet, so replicas are tagged to the right node).
77 pub node_id: u64,
78 /// The binary the re-exec'd compute workers run as — the container backend's
79 /// `__sandbox` jailer and the microVM backends' `__vmm-run`/`__vz-run` VM hosts.
80 /// `None` uses this process's own executable (`current_exe`), which is what
81 /// `boatramp serve` wants (the child *is* boatramp). An **embedding harness**
82 /// whose own binary doesn't implement those subcommands should point this at a
83 /// built `boatramp` binary, so it can drive the real container/microVM backends
84 /// in-process (only the per-workload worker re-execs; the serving plane stays
85 /// embedded). The docker backend needs neither — it talks to a daemon.
86 pub worker_exe: Option<std::path::PathBuf>,
87}
88
89/// A fully wired node: the deploy store, handler runtime, auth, and options a
90/// transport consumes, plus the detached reconcile loops kept alive for the
91/// node's serving life. Destructure it and hold `reconcile` across the serve
92/// await so the loops outlive assembly.
93pub struct RunningNode {
94 /// The deploy store (blob + KV) the router serves from.
95 pub deploy: DeployStore,
96 /// The handler runtime for wasm handlers (a disabled build ⇒ a no-op runtime).
97 pub handlers: boatramp_server::HandlerRuntime,
98 /// The control-plane auth.
99 pub auth: boatramp_server::Auth,
100 /// The resolved server options.
101 pub options: boatramp_server::ServerOptions,
102 /// The detached reconcile loops (compute + domain-verify). Tokio `JoinHandle`s
103 /// do not abort on drop, so the loops run for the process life regardless; the
104 /// handles are retained so an embedder can join/abort them on shutdown.
105 pub reconcile: Vec<tokio::task::JoinHandle<()>>,
106}
107
108/// The instance's own serve socket(s) a guest self-call may reach, given the bind `addr` and
109/// whether the posture (`allow_guest_self_egress`) permits it. A wildcard bind
110/// (`0.0.0.0`/`::`) is reachable over loopback, so it normalizes to `127.0.0.1` **and** `::1`
111/// on the serve port; a specific bind is reachable at itself. Empty when disabled or no
112/// listener.
113fn self_egress_addrs(
114 addr: Option<std::net::SocketAddr>,
115 enabled: bool,
116) -> Vec<std::net::SocketAddr> {
117 use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
118 let Some(addr) = addr.filter(|_| enabled) else {
119 return Vec::new();
120 };
121 if addr.ip().is_unspecified() {
122 let port = addr.port();
123 vec![
124 SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), port),
125 SocketAddr::new(IpAddr::V6(Ipv6Addr::LOCALHOST), port),
126 ]
127 } else {
128 vec![addr]
129 }
130}
131
132/// Wire [`NodeInput`] into a [`RunningNode`]: build the handler runtime, the
133/// deploy store (materializing the reserved `default` project), the compute
134/// backends + reconcile loop, and the domain-verify reconcile loop.
135///
136/// The caller has already built the store and configured auth/OIDC on `options`;
137/// this is the pure node-graph wiring, identical to what `boatramp serve` runs.
138pub async fn assemble(input: NodeInput<'_>) -> Result<RunningNode> {
139 let NodeInput {
140 config,
141 data_dir,
142 storage,
143 kv,
144 auth,
145 options,
146 serve_addr,
147 watch_provider,
148 provision_tier,
149 messaging,
150 is_leader,
151 node_id,
152 worker_exe,
153 } = input;
154 // Copy out the posture scalars up front so `options` can be moved into the
155 // returned `RunningNode` without a lingering borrow.
156 let max_handler_blob_bytes = options.posture.max_handler_blob_bytes;
157 let max_component_bytes = options.posture.max_component_bytes;
158 let allow_guest_private_egress = options.posture.allow_guest_private_egress;
159 let allow_env_secret_refs = options.posture.allow_env_secret_refs;
160 // The instance's own serve socket(s) a guest self-call may reach, when the posture allows
161 // it: a wildcard bind (`0.0.0.0`/`::`) is reachable on loopback, so normalize to
162 // `127.0.0.1`/`::1`; a specific bind is itself.
163 let self_egress_addrs = self_egress_addrs(serve_addr, options.posture.allow_guest_self_egress);
164 let allow_shared_kernel = options.posture.allow_shared_kernel_compute;
165 let domain_verify_allow_private = options.posture.domain_verify_allow_private;
166
167 // The deploy store the router serves from — built up front so the handler
168 // runtime's managed compute-backed `sql` binding can resolve DB endpoints from
169 // the same store the reconcile writes.
170 let compute_storage = storage.clone();
171 let deploy = DeployStore::new(storage, kv.clone());
172 // The `[secrets]` envelope (local KEK / Vault) that seals a managed SQL
173 // credential at rest. `None` ⇒ no wrapping (a managed DB then fails closed).
174 let secrets_envelope = build_secrets_envelope(config.secrets.as_ref(), data_dir)?;
175 // The project-scoped internal secret store, built from the same KV + `[secrets]`
176 // envelope that seal managed-DB credentials. Backs both the `boatramp:<name>`
177 // resolver (wired into the handler runtime below, when that feature is present)
178 // and the admin secrets API (threaded into `ServerOptions` unconditionally, so it
179 // works on a lean node too). `None` when no envelope is configured — the admin
180 // endpoints then fail closed with a clear 501, never a panic.
181 let secret_store = secrets_envelope.clone().map(|envelope| {
182 Arc::new(boatramp_core::secret_store::SecretStore::new(
183 kv.clone(),
184 envelope,
185 ))
186 });
187
188 // The handler runtime reuses the same blob/KV backends (per-site prefixed)
189 // for its wasi:blobstore/keyvalue bindings; the sql binding is selected by
190 // `[handlers.bindings.sql]` (default: per-site libsql files under <data-dir>).
191 let handlers = crate::handlers::build_handler_runtime(
192 kv.clone(),
193 compute_storage.clone(),
194 data_dir,
195 config.handlers.as_ref(),
196 messaging,
197 max_handler_blob_bytes,
198 max_component_bytes,
199 allow_guest_private_egress,
200 self_egress_addrs,
201 allow_env_secret_refs,
202 &deploy,
203 secrets_envelope.clone(),
204 )
205 .await?;
206 // Leader-gate cron firing (cluster: only the Raft leader fires; single-node: an
207 // always-true gate, equivalent to the unset default). The same gate drives the
208 // reconcile loops below, so all three converge on one leader per fleet. Only the
209 // handler runtime has a scheduler, so this is a no-op without the `handlers` feature.
210 #[cfg(feature = "handlers")]
211 handlers.set_cron_leader_gate(is_leader.clone());
212 // FA-5b2: on a cloud backend, wire the blob-change notification provisioner +
213 // its tier so adding a `blob` trigger provisions (and removing it retracts).
214 #[cfg(feature = "handlers")]
215 if let Some(provider) = watch_provider {
216 handlers.set_watch_provider(provider);
217 handlers.set_provision_tier(provision_tier);
218 }
219 #[cfg(not(feature = "handlers"))]
220 let _ = (watch_provider, provision_tier);
221
222 // Materialize the reserved `default` project so `project ls` / `project show
223 // default` reflect it on a fresh install, not only after a migration. Best
224 // effort: the reader backstop keeps listings correct even if this write can't
225 // land, so a transient failure must never block serving.
226 match deploy.ensure_default_project().await {
227 Ok(true) => tracing::info!("materialized the reserved `default` project record"),
228 Ok(false) => {}
229 Err(e) => tracing::warn!(
230 error = %e,
231 "could not materialize the `default` project record; readers use the synthesized default"
232 ),
233 }
234 // Wire the function-to-function invoke resolver now the deploy store exists,
235 // so a function granted `invoke` can call a sibling in-process (FI).
236 #[cfg(feature = "handlers")]
237 handlers.set_invoker(deploy.clone());
238
239 // Compute reconcile loop. Single-node is always the "leader". Backends are
240 // built from the `[compute]` config + capability detection; a no-op when none
241 // are registered. Detached for the server's life.
242 let (compute_backends, compute_node) = crate::compute::build_compute(
243 config.compute.as_ref(),
244 compute_storage,
245 data_dir,
246 node_id,
247 !allow_shared_kernel,
248 options.daemon_runtime.clone(),
249 worker_exe.as_deref(),
250 )
251 .await;
252 // Adopt the IPs of already-running replicas into each backend's fresh-on-boot
253 // IP pool BEFORE the reconcile loop starts allocating. A backend with a per-node
254 // pool (the native container backend) rebuilds it empty each process start; without
255 // this the boot reconcile could re-hand a live address to a different workload —
256 // the container-IP collision — or move a replica's endpoint on relaunch. Feeds
257 // every persisted replica's `(workload, replica, endpoint-ip)`; each backend keeps
258 // only the IPs in its own subnet (a cheap no-op for docker/cloudflare/VMM).
259 crate::compute::adopt_running_replica_ips(&deploy, &compute_backends).await;
260 // Per-project internal DNS (service discovery): start the resolver on the bridge
261 // gateway so a guest resolves peers by name within its project. On by default;
262 // starts only when the container backend + bridge are up (Linux). Detached for
263 // the node's serving life (pushed into `reconcile` below). Started before the
264 // reconcile loop consumes `compute_backends` — it borrows the registry to check
265 // the container backend is present.
266 let internal_dns =
267 crate::compute::spawn_internal_dns(config.compute.as_ref(), &compute_backends, &deploy);
268 // Activate the compute sql-shim (PLAN-compute-bindings): bind its listener +
269 // build the resolver when a sql provider and `compute.sql_shim_url` are both present.
270 #[cfg(feature = "handlers")]
271 let sql_resolver = boatramp_server::sql_shim::spawn_sql_shim(
272 handlers.sql_backends(),
273 config.compute.as_ref().and_then(|c| c.sql_shim_url.clone()),
274 )
275 .await;
276 #[cfg(not(feature = "handlers"))]
277 let sql_resolver: Option<Arc<dyn boatramp_core::compute::ComputeBindingResolver>> = None;
278
279 // Managed compute-backed SQL (PLAN-managed-compute-sql P2-b): if the handler
280 // `sql` config declares any managed database, inject its `POSTGRES_*`/`MYSQL_*`
281 // server-init env into the DB workload at launch from the sealed credential.
282 // Reaching here with a managed DB implies an envelope (build_handler_runtime
283 // fails closed otherwise), so the credential store always has one to seal with.
284 // Keep a clone of the secrets envelope for the operator-SQL capability below
285 // (the managed_db_resolver match moves the original).
286 #[cfg(any(feature = "sql-postgres", feature = "sql-mysql"))]
287 let operator_envelope = secrets_envelope.clone();
288 // …and a second clone for the tenant-deprovision capability (drops a deleted
289 // tenant's managed DB/role/credential on project/site delete). It needs a real
290 // envelope to seal/unseal + delete per-tenant credentials, so it is wired only
291 // when one is present (same fail-closed gating as the managed-DB paths).
292 #[cfg(any(feature = "sql-postgres", feature = "sql-mysql"))]
293 let deprovision_envelope = secrets_envelope.clone();
294 // …and a third clone for the soft-delete tombstone reaper (the leader-gated task
295 // that hard-drops a Shared-Postgres tenant once its grace window elapses). It, too,
296 // needs a real envelope to unseal the superuser credential + delete the per-tenant
297 // one on hard-drop.
298 #[cfg(any(feature = "sql-postgres", feature = "sql-mysql"))]
299 let reaper_envelope = secrets_envelope.clone();
300 #[cfg(any(feature = "sql-postgres", feature = "sql-mysql"))]
301 let managed_db_resolver: Option<Arc<dyn boatramp_core::compute::ManagedDbEnvResolver>> = match (
302 config
303 .handlers
304 .as_ref()
305 .and_then(|h| h.bindings.sql.as_ref()),
306 secrets_envelope,
307 ) {
308 (Some(sql), Some(envelope)) if !sql.databases.is_empty() => {
309 let creds = crate::managed_sql::ManagedSqlCredentials::new(kv.clone(), envelope);
310 let privilege = config
311 .compute
312 .as_ref()
313 .map(|c| c.managed_db_privilege)
314 .unwrap_or_default();
315 let env =
316 crate::managed_sql::ManagedDbEnv::from_config(&sql.databases, creds, privilege);
317 (!env.is_empty()).then(|| Arc::new(env) as Arc<_>)
318 }
319 _ => None,
320 };
321 #[cfg(not(any(feature = "sql-postgres", feature = "sql-mysql")))]
322 let managed_db_resolver: Option<Arc<dyn boatramp_core::compute::ManagedDbEnvResolver>> = None;
323
324 // Turnkey managed DB: auto-register the compute workload(s) backing each managed
325 // co-located database that has none yet, so declaring the `databases` binding is
326 // enough to boot the DB (no separate `compute set` / apply). Tenant-aware — a
327 // `Shared` binding registers its one shared server; a `Single` binding registers
328 // nothing at boot (its per-tenant `<compute>-<ident>` is created durably by the lazy
329 // resolve on first `sql` use and relaunched by the reconcile, so a project that never
330 // uses `sql` — e.g. a static-only site — never gets a spurious DB). Non-clobbering +
331 // idempotent; runs before the reconcile loop so its first tick can launch what it
332 // registered.
333 #[cfg(any(feature = "sql-postgres", feature = "sql-mysql"))]
334 if let Some(sql) = config
335 .handlers
336 .as_ref()
337 .and_then(|h| h.bindings.sql.as_ref())
338 .filter(|sql| !sql.databases.is_empty())
339 {
340 crate::managed_sql::auto_register_managed_db_workloads(&deploy, &sql.databases).await;
341 }
342
343 // Operator SQL capability (managed-DB migrations/queries via the sealed
344 // credential, resolved server-side) — backs `POST /api/sql/{db}/{exec,query}`.
345 #[cfg(any(feature = "sql-postgres", feature = "sql-mysql"))]
346 let operator_sql: Option<Arc<dyn boatramp_core::sql::OperatorSql>> = config
347 .handlers
348 .as_ref()
349 .and_then(|h| h.bindings.sql.as_ref())
350 .filter(|sql| !sql.databases.is_empty())
351 .map(|sql| {
352 Arc::new(crate::managed_sql::NodeOperatorSql::new(
353 sql.databases.clone(),
354 kv.clone(),
355 operator_envelope,
356 deploy.clone(),
357 )) as Arc<_>
358 });
359 #[cfg(not(any(feature = "sql-postgres", feature = "sql-mysql")))]
360 let operator_sql: Option<Arc<dyn boatramp_core::sql::OperatorSql>> = None;
361
362 // Tenant-deprovision capability (drop a deleted tenant's managed DB/role/sealed
363 // credential on project/site delete). Wired only when a compute-backed managed
364 // database + a secrets envelope are both present — same gating as operator_sql,
365 // plus the envelope requirement (it must seal/unseal per-tenant credentials).
366 // The soft-delete grace window for a Shared-Postgres managed tenant
367 // (`handlers.bindings.sql.deprovision_grace_secs`, env-settable). Default 7 days;
368 // `0` disables the soft path (immediate hard drop). Threaded to the deprovisioner
369 // (which soft-deletes) and implicitly honored by the reaper (which only ever finds
370 // tombstones a >0 grace produced).
371 #[cfg(any(feature = "sql-postgres", feature = "sql-mysql"))]
372 let deprovision_grace_secs = config
373 .handlers
374 .as_ref()
375 .and_then(|h| h.bindings.sql.as_ref())
376 .and_then(|sql| sql.deprovision_grace_secs)
377 .unwrap_or(crate::tenant_sql::DEFAULT_DEPROVISION_GRACE_SECS);
378 #[cfg(any(feature = "sql-postgres", feature = "sql-mysql"))]
379 let tenant_deprovisioner: Option<Arc<dyn boatramp_core::sql::TenantDeprovisioner>> = config
380 .handlers
381 .as_ref()
382 .and_then(|h| h.bindings.sql.as_ref())
383 .filter(|sql| !sql.databases.is_empty())
384 .zip(deprovision_envelope)
385 .map(|(sql, envelope)| {
386 Arc::new(crate::tenant_sql::NodeTenantDeprovisioner::new(
387 deploy.clone(),
388 kv.clone(),
389 envelope,
390 sql.databases.clone(),
391 deprovision_grace_secs,
392 )) as Arc<_>
393 });
394 #[cfg(not(any(feature = "sql-postgres", feature = "sql-mysql")))]
395 let tenant_deprovisioner: Option<Arc<dyn boatramp_core::sql::TenantDeprovisioner>> = None;
396
397 // Operator compute-exec capability (run a command inside a running workload) —
398 // backs `POST /api/compute/{name}/exec`, gated by the `allow_compute_exec`
399 // posture. Clone the backend registry before the reconcile loop consumes it.
400 let compute_exec: Option<Arc<dyn boatramp_core::compute::ComputeExec>> = Some(Arc::new(
401 crate::compute::NodeComputeExec::new(compute_backends.clone(), deploy.clone()),
402 ) as Arc<_>);
403
404 // Operator volume-reclamation capability (list + remove persistent volumes) —
405 // backs `GET /api/compute/volumes` + `DELETE /api/compute/volumes/{name}`.
406 // Same admin-scoped `/api/compute/*` gate; clone the registry before the
407 // reconcile loop consumes the original below.
408 let compute_volumes: Option<Arc<dyn boatramp_core::compute::ComputeVolumes>> = Some(Arc::new(
409 crate::compute::NodeComputeVolumes::new(compute_backends.clone(), deploy.clone()),
410 )
411 as Arc<_>);
412
413 let compute_reconcile = boatramp_server::spawn_compute_reconcile(
414 deploy.clone(),
415 compute_backends,
416 vec![compute_node],
417 boatramp_core::compute::BackendPolicy::from_shared_kernel_allowed(allow_shared_kernel),
418 is_leader.clone(),
419 compute_reconcile_tick(),
420 COMPUTE_IDLE_TIMEOUT,
421 sql_resolver,
422 managed_db_resolver,
423 );
424
425 // Tenant tombstone reaper: leader-gated hard-drop of soft-deleted Shared-Postgres
426 // tenants past their grace window (safe deprovision — see `tenant_sql`). Wired only
427 // when a compute-backed managed database + a secrets envelope are both present
428 // (same gating as the deprovisioner); each tombstone carries its own server +
429 // superuser, so the reaper needs no per-binding config. A `0` grace never writes a
430 // tombstone, so the sweep is simply inert then.
431 #[cfg(any(feature = "sql-postgres", feature = "sql-mysql"))]
432 let tombstone_reaper: Option<tokio::task::JoinHandle<()>> = config
433 .handlers
434 .as_ref()
435 .and_then(|h| h.bindings.sql.as_ref())
436 .filter(|sql| !sql.databases.is_empty())
437 .zip(reaper_envelope)
438 .map(|(_sql, envelope)| {
439 crate::tenant_sql::spawn_tenant_tombstone_reaper(
440 deploy.clone(),
441 kv.clone(),
442 envelope,
443 is_leader.clone(),
444 crate::tenant_sql::TOMBSTONE_REAPER_TICK,
445 )
446 });
447 #[cfg(not(any(feature = "sql-postgres", feature = "sql-mysql")))]
448 let tombstone_reaper: Option<tokio::task::JoinHandle<()>> = None;
449
450 // Domain-verify auto-complete: periodically re-check every site's pending
451 // ownership challenges and attach any that now pass — a published token (e.g.
452 // via `domain add --provider`) converges without a manual `domain verify`.
453 let dv_reconcile = boatramp_server::spawn_domain_verify_reconcile(
454 deploy.clone(),
455 domain_verify_allow_private,
456 is_leader,
457 DOMAIN_VERIFY_RECONCILE_TICK,
458 );
459
460 // Wire the operator capabilities onto the options the router is built from.
461 let mut options = options;
462 options.operator_sql = operator_sql;
463 options.tenant_deprovisioner = tenant_deprovisioner;
464 options.compute_exec = compute_exec;
465 options.compute_volumes = compute_volumes;
466 // The internal secret store backs the admin secrets API (set/list/delete). Not
467 // handlers-gated — it must be reachable even on a lean node.
468 options.secret_store = secret_store;
469
470 // The detached reconcile loops: the always-present compute + domain-verify ones,
471 // plus the optional tenant-tombstone reaper (only when a managed DB is configured).
472 let mut reconcile = vec![compute_reconcile, dv_reconcile];
473 if let Some(reaper) = tombstone_reaper {
474 reconcile.push(reaper);
475 }
476 if let Some(dns) = internal_dns {
477 reconcile.push(dns);
478 }
479
480 Ok(RunningNode {
481 deploy,
482 handlers,
483 auth,
484 options,
485 reconcile,
486 })
487}
488
489/// Build the `[secrets]` envelope (secrets-at-rest wrapping) from `boatramp.cfg`'s
490/// `[secrets]` section: `local` (a machine-local AES-256-GCM KEK) or `vault` (Vault
491/// Transit). `None`/empty ⇒ no wrapping. The Vault token is read from the
492/// environment (`token_env`), never a file. This seals a managed SQL credential at
493/// rest; a managed database fails closed without it.
494fn build_secrets_envelope(
495 secrets: Option<&crate::config::SecretsConfig>,
496 data_dir: &Path,
497) -> Result<Option<Arc<dyn boatramp_core::envelope::KeyEnvelope>>> {
498 use boatramp_server::envelope::{build_envelope, EnvelopeSpec};
499 let Some(cfg) = secrets else {
500 return Ok(None);
501 };
502 let spec = match cfg.envelope.as_str() {
503 "" => EnvelopeSpec::None,
504 "local" => EnvelopeSpec::Local {
505 kek_file: cfg
506 .kek_file
507 .clone()
508 .unwrap_or_else(|| data_dir.join("secrets/kek")),
509 },
510 "vault" => {
511 let v = cfg.vault.as_ref().ok_or_else(|| {
512 Error::Envelope(
513 "secrets.envelope = \"vault\" needs a [secrets.vault] section".into(),
514 )
515 })?;
516 let token = std::env::var(&v.token_env).map_err(|_| {
517 Error::Envelope(format!("Vault token env `{}` is not set", v.token_env))
518 })?;
519 EnvelopeSpec::Vault {
520 addr: v.addr.clone(),
521 key: v.key.clone(),
522 token,
523 }
524 }
525 other => {
526 return Err(Error::Envelope(format!(
527 "unknown secrets.envelope {other:?} (want \"local\" or \"vault\")"
528 )))
529 }
530 };
531 build_envelope(spec).map_err(|e| Error::Envelope(e.to_string()))
532}
533
534#[cfg(all(test, feature = "fs"))]
535mod tests {
536 use super::*;
537 use boatramp_core::kv::MemoryKv;
538 use boatramp_core::security::SecurityProfile;
539
540 /// The headline in-process fidelity check (PLAN-node-library N2b.3): `assemble`
541 /// over a temp `FsStorage` + `MemoryKv` produces a `RunningNode` whose deploy
542 /// store is live (the reserved `default` project was materialized during
543 /// assembly) and whose router — the exact one `boatramp serve` builds — answers
544 /// `/healthz`. No listener is bound: the request is driven through the router
545 /// via `tower::oneshot`, so the whole assembly runs in-process.
546 #[tokio::test]
547 async fn assemble_produces_a_serving_node_over_a_temp_store() {
548 use axum::body::Body;
549 use axum::http::{Request, StatusCode};
550 use tower::ServiceExt;
551
552 let tmp = tempfile::tempdir().unwrap();
553 let storage: Arc<dyn Storage> = Arc::new(boatramp_storage::FsStorage::new(tmp.path()));
554 let kv: Arc<dyn KvStore> = Arc::new(MemoryKv::new());
555 let config = ServerConfig::default();
556 let options = boatramp_server::ServerOptions {
557 // The strict `multi-tenant` posture, as an unconfigured `serve` resolves.
558 posture: SecurityProfile::MultiTenant.preset(),
559 ..Default::default()
560 };
561
562 let node = assemble(NodeInput {
563 config: &config,
564 data_dir: tmp.path(),
565 storage,
566 kv,
567 auth: boatramp_server::Auth::disabled(),
568 options,
569 serve_addr: None,
570 watch_provider: None,
571 provision_tier: boatramp_core::blob_notify::ProvisionTier::default(),
572 messaging: None,
573 is_leader: Arc::new(|| true),
574 node_id: 0,
575 worker_exe: None,
576 })
577 .await
578 .expect("assemble a node over a temp store");
579
580 // The deploy store is live: `assemble` already materialized the reserved
581 // `default` project, so a second ensure reports "already present" (`false`).
582 assert!(
583 !node
584 .deploy
585 .ensure_default_project()
586 .await
587 .expect("read the default project"),
588 "assemble should have materialized the default project"
589 );
590
591 // The assembled router (the same wiring `serve` binds) answers /healthz.
592 let router =
593 boatramp_server::router_with(node.deploy, node.auth, node.handlers, node.options);
594 let response = router
595 .oneshot(
596 Request::builder()
597 .uri("/healthz")
598 .body(Body::empty())
599 .unwrap(),
600 )
601 .await
602 .expect("route /healthz");
603 assert_eq!(response.status(), StatusCode::OK);
604 }
605}