diff options
| author | aur-build <aur-build@localhost> | 2026-11-19 15:15:53 +0000 |
|---|---|---|
| committer | aur-build <aur-build@localhost> | 2026-11-19 15:15:53 +0000 |
| commit | 8912f71afb9697c8645209ec0e67f67098bb16a1 (patch) | |
| tree | 91476b46d1de87f3fb3d9565a41925e52620edff /src/config.rs | |
| parent | 8096fde8d98cdde4e110865ebb15ad034b92ff27 (diff) | |
| download | aur-build-8912f71afb9697c8645209ec0e67f67098bb16a1.tar.gz aur-build-8912f71afb9697c8645209ec0e67f67098bb16a1.zip | |
config: the sandbox backend is an enum, not a string with a fallthrough
`sandbox_backend` was a free-form string and `backend_for` matched it with
`_ => loopback`. So `sandbox_backend = "kata"` — a real, intended backend,
documented in the design — meant "run the untrusted PKGBUILD as the hub's own
user", and nothing anywhere said so. There was also no validation of any kind in
the crate: an empty `agent_token` with a remote backend, or a missing listen
address, parsed fine and failed later as something else.
The four flat fields become one tagged `sandbox` object with three kinds:
`loopback` (needs nothing, still the default, so every config on disk keeps
working), `remote` (listen + token), and `agent-sandbox` (api, namespace,
bearer, image, listen, token). Required fields are required by the type, an
unknown kind is a parse error naming the valid set, and `deny_unknown_fields`
means a misspelled field is named rather than read as absent. `agent-sandbox`
parses and is refused at backend construction until it is wired up — a valid
kind that is not implemented is an error, never a substitution.
Retired keys are refused when they carry intent: a config saying
`sandbox_backend: "remote"` errors and names the replacement shape, because
quietly reinterpreting it as loopback is the bug being removed. A config saying
`"loopback"` with the empty strings `init` wrote is noise, not intent, and loads
unchanged. The token and the cluster bearer are redacted in `Debug`, so a config
that reaches a log line cannot leak them.
Diffstat (limited to 'src/config.rs')
| -rw-r--r-- | src/config.rs | 241 |
1 files changed, 222 insertions, 19 deletions
diff --git a/src/config.rs b/src/config.rs index 00ac3e4..b86aada 100644 --- a/src/config.rs +++ b/src/config.rs @@ -9,6 +9,157 @@ use std::path::{Path, PathBuf}; use serde::{Deserialize, Serialize}; +/// The tag a provisioned sandbox image gets when the configuration names a +/// provisioning kind but the caller has no image of its own (tests, mostly). +pub const DEFAULT_SANDBOX_IMAGE: &str = "aur-build-sandbox:latest"; + +/// Where build code runs (§8, the sandbox seam). +/// +/// Three kinds, and only three. This is an enum with required fields rather +/// than a string plus a `validate()` that has to be remembered, because the +/// failure mode of the string was silent: `sandbox_backend = "kata"` fell +/// through to loopback and a build that was supposed to run inside a VM ran +/// as the hub's own user. Here an unknown kind is a parse error naming the +/// valid set, and a kind is missing a field it needs is a parse error naming +/// the field. +/// +/// Secrets are redacted in `Debug` on purpose: a config that ends up in a log +/// line or a panic message must not carry the rendezvous token. +#[derive(Clone, Serialize, Deserialize)] +#[serde(tag = "kind", rename_all = "kebab-case", deny_unknown_fields)] +pub enum Sandbox { + /// Subprocesses of this process — tests, and the single-machine deployment + /// where §14's sudo rules are the boundary. The default, and the only kind + /// that needs no configuration, which is what keeps an existing + /// `config.json` valid without edits. + /// + /// Isolation is *none*: the sandbox is the hub's own user. + Loopback, + /// Sandboxes that dial in over the agent protocol (§8a). The hub never + /// provisions them; it waits for an agent carrying the token to connect. + Remote { + /// Address the hub listens on for agent dial-ins. Port 0 is allowed + /// and resolves at bind. + listen: String, + /// Shared secret an agent presents at rendezvous. A sandbox is + /// untrusted the moment it boots; this is the only thing saying "you + /// belong to me". Deploy it like a credential. + token: String, + /// Asserts that hub and sandbox see the same filesystem. The workspace + /// is not yet shipped to a remote sandbox (§8a), so a remote build only + /// works where the srcdir is shared; saying so explicitly is what + /// turns a silent misconfiguration into a refusal. See `Config`'s + /// startup guard. + #[serde(default)] + shared_workspace: bool, + }, + /// agent-sandbox: disposable Kata VMs provisioned by a Kubernetes CRD, so + /// the orchestrator enforces the TTL even if the hub dies (§8a). + AgentSandbox { + /// Kubernetes API server serving the agent-sandbox CRD. + api: String, + /// Namespace the sandboxes are created in. + namespace: String, + /// Bearer token for the API. A credential; never logged. + bearer: String, + /// Image a provisioned sandbox boots — the `sandbox-agent` entrypoint. + image: String, + /// Address the hub listens on for agent dial-ins. + listen: String, + /// Shared secret an agent presents at rendezvous. + token: String, + /// As [`Sandbox::Remote`]. + #[serde(default)] + shared_workspace: bool, + }, +} + +impl Default for Sandbox { + /// The fully local option, so a config that says nothing still builds. + fn default() -> Self { + Sandbox::Loopback + } +} + +impl std::fmt::Debug for Sandbox { + /// Hand-written so the secrets cannot ride along: a config that ends up in + /// a log line, a panic message, or an audit payload must not carry the + /// rendezvous token or the cluster bearer. + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Sandbox::Loopback => f.write_str("Sandbox::Loopback"), + Sandbox::Remote { listen, shared_workspace, .. } => f + .debug_struct("Sandbox::Remote") + .field("listen", listen) + .field("shared_workspace", shared_workspace) + .field("token", &"[redacted]") + .finish(), + Sandbox::AgentSandbox { api, namespace, image, listen, shared_workspace, .. } => f + .debug_struct("Sandbox::AgentSandbox") + .field("api", api) + .field("namespace", namespace) + .field("image", image) + .field("listen", listen) + .field("shared_workspace", shared_workspace) + .field("token", &"[redacted]") + .field("bearer", &"[redacted]") + .finish(), + } + } +} + +impl Sandbox { + /// The kind name, for error messages and the UI. + pub fn kind(&self) -> &'static str { + match self { + Sandbox::Loopback => "loopback", + Sandbox::Remote { .. } => "remote", + Sandbox::AgentSandbox { .. } => "agent-sandbox", + } + } + + /// The rendezvous listener address, for the kinds that have one. + pub fn listen(&self) -> Option<&str> { + match self { + Sandbox::Remote { listen, .. } => Some(listen), + Sandbox::AgentSandbox { listen, .. } => Some(listen), + Sandbox::Loopback => None, + } + } + + /// The rendezvous token, for the kinds that have one. Never log this. + pub fn token(&self) -> Option<&str> { + match self { + Sandbox::Remote { token, .. } => Some(token), + Sandbox::AgentSandbox { token, .. } => Some(token), + Sandbox::Loopback => None, + } + } + + /// The image a provisioning backend boots. `None` for kinds that + /// provision nothing — there is no honest default for loopback, and a + /// field that is never consulted is a field that lies. + pub fn image(&self) -> Option<&str> { + match self { + Sandbox::AgentSandbox { image, .. } => Some(image), + _ => None, + } + } + + /// Whether the operator has asserted that hub and sandbox share a + /// filesystem. Loopback shares it by construction. + pub fn shared_workspace(&self) -> bool { + match self { + Sandbox::Loopback => true, + Sandbox::Remote { shared_workspace, .. } => *shared_workspace, + Sandbox::AgentSandbox { shared_workspace, .. } => *shared_workspace, + } + } + + /// The valid kind names, for the error that names them. + pub const KINDS: [&'static str; 3] = ["loopback", "remote", "agent-sandbox"]; +} + /// Runtime configuration. Persisted as `<root>/config.json`. #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(default)] @@ -43,20 +194,11 @@ pub struct Config { pub gpg_bin: String, /// Unprivileged user that runs PKGBUILD code (§14). Empty = current user. pub build_user: String, - /// Where build code runs: `loopback` (subprocesses of this process — - /// tests and the single-machine deployment) or `remote` (sandboxes that - /// dial in via the agent protocol). See `crate::sandbox`. - pub sandbox_backend: String, - /// Address the hub listens on for sandbox agents when - /// `sandbox_backend = "remote"`. Port 0 is allowed and resolves at bind. - pub agent_listen: String, - /// Shared secret an agent must present at rendezvous. A sandbox is - /// untrusted the moment it boots; this token is the only thing telling - /// the hub "you belong to me" — deploy it like one. - pub agent_token: String, - /// Image a provisioned sandbox boots (the `sandbox-agent` entrypoint). - /// Only consulted by provisioning backends (kata). - pub sandbox_image: String, + /// Where build code runs (§8). A tagged enum rather than a free-form string + /// plus a validation pass: the configurations that cannot work are not + /// representable, so there is no input that silently becomes "loopback". + /// See [`Sandbox`]. + pub sandbox: Sandbox, /// Test-only hooks (§16a). Not part of the production configuration at all /// unless the crate is built with `test-support` — see [`TestHooks`]. #[cfg(feature = "test-support")] @@ -145,10 +287,7 @@ impl Default for Config { repo_add_bin: "repo-add".into(), gpg_bin: "gpg".into(), build_user: String::new(), - sandbox_backend: "loopback".into(), - agent_listen: String::new(), - agent_token: String::new(), - sandbox_image: "aur-build-sandbox:latest".into(), + sandbox: Sandbox::default(), #[cfg(feature = "test-support")] hooks: TestHooks::default(), max_concurrent_builds: 1, @@ -161,6 +300,17 @@ impl Default for Config { } impl Config { + /// The image a provisioning sandbox boots, when this configuration + /// provisions anything. Falls back to the conventional tag so a test that + /// builds a `KataBackend` over a default config still gets a name to hand + /// an agent. + pub fn sandbox_image(&self) -> String { + self.sandbox + .image() + .unwrap_or(DEFAULT_SANDBOX_IMAGE) + .to_string() + } + /// Read `<root>/config.json`. /// /// The crate's own error type, like everything else that can fail (m-15): @@ -187,7 +337,9 @@ impl Config { ))); } let s = std::fs::read_to_string(&p)?; - let mut cfg: Config = serde_json::from_str(&s)?; + let v: serde_json::Value = serde_json::from_str(&s)?; + check_retired_sandbox_keys(&v)?; + let mut cfg: Config = serde_json::from_value(v)?; cfg.root = dir; Ok(cfg) } @@ -331,6 +483,57 @@ impl Config { } } +/// Whether a retired key holds exactly the value `init` used to write for a +/// local deployment. Such a value states nothing the default does not already +/// state, so it is noise from an old file rather than an intent that could be +/// lost — which is what lets a working local config keep loading across the +/// change. Anything else is refused. +fn retired_key_is_inert(key: &str, value: &str) -> bool { + match key { + "sandbox_backend" => value == "loopback", + "sandbox_image" => value.is_empty() || value == DEFAULT_SANDBOX_IMAGE, + _ => value.is_empty(), + } +} + +/// The sandbox configuration moved from four flat keys to one tagged object +/// ([`Sandbox`]). A retired key is refused rather than quietly ignored — but +/// only when it would have changed behaviour. +/// +/// `init` used to write `sandbox_backend: "loopback"` alongside three empty +/// strings, and a config carrying exactly that says nothing the default does +/// not already say, so it keeps loading untouched. Anything else is an error: +/// silently turning a retired `sandbox_backend: "remote"` into loopback is +/// precisely the failure the enum exists to make impossible. +fn check_retired_sandbox_keys(v: &serde_json::Value) -> crate::error::Result<()> { + const RETIRED: [&str; 4] = ["sandbox_backend", "agent_listen", "agent_token", "sandbox_image"]; + let named: Vec<String> = RETIRED + .iter() + .filter(|k| { + let key: &str = k; + match v.get(key) { + None => false, + Some(serde_json::Value::String(s)) => !retired_key_is_inert(key, s), + Some(_) => true, + } + }) + .map(|k| format!("\"{k}\"")) + .collect(); + if named.is_empty() { + return Ok(()); + } + let example = concat!( + "replace them with a single \"sandbox\" object: ", + "{\"kind\":\"loopback\"}, ", + "{\"kind\":\"remote\",\"listen\":…,\"token\":…}, or ", + "{\"kind\":\"agent-sandbox\",\"api\":…,\"namespace\":…,\"bearer\":…,\"image\":…,\"listen\":…,\"token\":…}" + ); + Err(crate::error::Error::invalid(format!( + "{} are retired sandbox keys; {example}", + named.join(", ") + ))) +} + /// Absolutize a path without resolving symlinks and without requiring it to /// exist, both of which `canonicalize` insists on — and `init` saves its config /// before the tree under it is complete. |
