Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions pkgs/fleet/crates/fleet-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,10 @@ impl Config {
/// its own `[host]` section instead of growing this table.
pub fn builtin_host(location: &str) -> Option<(InitMode, Vec<String>)> {
match location {
"sandbox" => Some((InitMode::Spawn, vec!["proxy".to_string()])),
"sandbox" => Some((
InitMode::Spawn,
vec!["proxy".to_string(), "secrets-broker".to_string()],
)),
"laptop" => Some((
InitMode::Systemd,
vec![
Expand Down Expand Up @@ -2326,15 +2329,21 @@ socket = "rel/s.sock"
fn host_env_location_sandbox_builtin_map_is_proxy_spawn() {
// The hermetic-provisioning shape: FLEET_LOCATION alone on
// a machine with no [host] section resolves to exactly
// today's sandbox behaviour (design #5687 §5/§6).
// today's sandbox behaviour (design #5687 §5/§6): the
// session proxy plus the standing sandbox secrets broker
// (stage-2 broker resilience: secrets resolve locally, no
// laptop round trip).
let cfg = Config::default();
let path = PathBuf::from("/nonexistent/fleet.toml");
let resolved = cfg
.resolve_host_from(None, Some("sandbox"), &path)
.unwrap()
.unwrap();
assert_eq!(resolved.init_mode, InitMode::Spawn);
assert_eq!(resolved.roles, vec!["proxy".to_string()]);
assert_eq!(
resolved.roles,
vec!["proxy".to_string(), "secrets-broker".to_string()]
);
}

#[test]
Expand Down
115 changes: 108 additions & 7 deletions pkgs/fleet/crates/fleet-proxy/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,10 @@ Autostart this host's fleet roles if they are not running. The
FLEET_HOST_CONFIG) names this machine's location and roles: on
the sandbox that is the session proxy (probe the broker socket,
sync the PATH shims when a broker answers, start one when none
does — a strict no-op when a broker is already running). An
unknown location reports state, starts nothing, and exits 69.
does — a strict no-op when a broker is already running) plus the
standing secrets broker (probe its Unix socket, start one bound
to loopback when none answers). An unknown location reports
state, starts nothing, and exits 69.

Takes no arguments.";

Expand Down Expand Up @@ -592,22 +594,33 @@ fn run_init_resolved(
match host.init_mode {
InitMode::Spawn => {
let mut proxy_active = false;
let mut secrets_broker_active = false;
for role in &active {
if role == "proxy" {
proxy_active = true;
} else if role == "secrets-broker" {
secrets_broker_active = true;
} else {
// v1 spawn semantics exist for the proxy only
// (the sandbox's one role, §5); any other role
// in a spawn manifest is reported, not spawned.
// v1 spawn semantics exist for the proxy and the
// standing sandbox secrets broker (stage-2 broker
// resilience) only; any other role in a spawn
// manifest is reported, not spawned.
println!(
"fleet: role {role}: not spawn-managed in this build — reported, skipped"
);
}
}
let mut code = 0;
if proxy_active {
return run_init_proxy(config, client, capabilities);
code = run_init_proxy(config, client, capabilities)?;
}
Ok(0)
if secrets_broker_active {
let sb_code = run_init_secrets_broker(config)?;
if code == 0 {
code = sb_code;
}
}
Ok(code)
}
InitMode::Systemd => {
for role in &active {
Expand Down Expand Up @@ -772,6 +785,94 @@ fn run_init_proxy(
Ok(0)
}

/// The secrets-broker role's spawn-mode ensure (stage-2 broker
/// resilience): probe the broker's own Unix socket first — a live
/// owner means status and out, never a spawn, never a restart
/// (the init strict no-op, §8). Only an unanswered socket spawns
/// `fleet serve secrets-broker` detached, bound to loopback: the
/// sandbox broker is a local capability, never a LAN surface.
///
/// The loopback bind is enforced twice: the spawn sets
/// `SECRETS_BROKER_BIND=127.0.0.1:4800` explicitly, and the broker
/// itself forces loopback when `FLEET_LOCATION=sandbox`, so a stale
/// environment can never widen it.
fn run_init_secrets_broker(config: &Config) -> Result<i32, ClientError> {
let socket = config.secrets_broker_socket();
if fleet_config::probe_socket(&socket) {
println!(
"fleet: secrets-broker already running ({}); nothing spawned",
socket.display()
);
return Ok(0);
}
ensure_secrets_broker(config)?;
println!(
"fleet: secrets-broker started ({}; loopback only)",
socket.display()
);
Ok(0)
}

/// Wait for the spawned secrets broker's socket to answer, like
/// `ensure_broker`'s post-spawn wait: the spawn returns as soon as
/// the daemon execs, and callers (and the proof) need the socket
/// to be dialable before init reports success.
fn ensure_secrets_broker(config: &Config) -> Result<(), ClientError> {
spawn_secrets_broker(config)?;
let socket = config.secrets_broker_socket();
for _ in 0..50 {
std::thread::sleep(Duration::from_millis(100));
if fleet_config::probe_socket(&socket) {
return Ok(());
}
}
Err(ClientError::Transport(format!(
"secrets-broker did not come up at {}",
socket.display()
)))
}

/// Spawn the secrets-broker serve role detached (setsid, output
/// appended to secrets-broker.log beside the socket — the proxy
/// spawn's shape). The argv is the post-rename spelling, `fleet
/// serve secrets-broker`.
fn spawn_secrets_broker(config: &Config) -> Result<(), ClientError> {
use std::os::unix::process::CommandExt;
let socket = config.secrets_broker_socket();
let sock_dir = socket
.parent()
.map(|p| p.to_path_buf())
.unwrap_or_else(|| std::path::PathBuf::from("."));
std::fs::create_dir_all(&sock_dir)
.map_err(|e| ClientError::Transport(format!("socket dir: {e}")))?;
let log = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(sock_dir.join("secrets-broker.log"))
.map_err(|e| ClientError::Transport(format!("secrets-broker.log: {e}")))?;
let exe =
std::env::current_exe().map_err(|e| ClientError::Transport(format!("current exe: {e}")))?;
let mut cmd = std::process::Command::new(exe);
cmd.args(["serve", "secrets-broker"])
.env("SECRETS_BROKER_BIND", "127.0.0.1:4800")
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::from(log.try_clone().map_err(|e| {
ClientError::Transport(format!("secrets-broker.log: {e}"))
})?))
.stderr(std::process::Stdio::from(log));
unsafe {
cmd.pre_exec(|| {
if libc::setsid() == -1 {
return Err(std::io::Error::last_os_error());
}
Ok(())
});
}
cmd.spawn()
.map_err(|e| ClientError::Transport(format!("spawn secrets-broker: {e}")))?;
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
16 changes: 14 additions & 2 deletions pkgs/fleet/crates/fleet-queue/src/secrets_broker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,8 +631,20 @@ pub fn parse_ttl_s(raw: Option<&str>) -> Result<TtlParse, String> {

pub fn run() -> i32 {
use std::io::Read as _;
let bind =
std::env::var("SECRETS_BROKER_BIND").unwrap_or_else(|_| "100.99.113.103:4800".to_string());
// Stage-2 broker resilience: on the sandbox the broker is a
// local capability, never a LAN surface — force loopback
// regardless of the environment, so a stale or widened
// SECRETS_BROKER_BIND can never expose the vault off-host.
// The spawn in `fleet init` sets the loopback bind explicitly;
// this is the second enforcement, at the bind itself.
let sandbox = std::env::var("FLEET_LOCATION")
.ok()
.is_some_and(|v| v.trim() == "sandbox");
let bind = if sandbox {
"127.0.0.1:4800".to_string()
} else {
std::env::var("SECRETS_BROKER_BIND").unwrap_or_else(|_| "100.99.113.103:4800".to_string())
};
let home = match crate::home::from_env() {
Ok(home) => home,
Err(error) => {
Expand Down