Skip to content

Commit f525d86

Browse files
committed
feat(supervisor): add --mode flag to gate network/process leaves
Add a --mode flag (default "network,process") that selects which supervisor leaves run in the current process. Two new shapes are unlocked without splitting the binary: --mode=network # network-only sidecar --mode=process # process-only supervisor --mode=network,process # combined (default; current behavior) In network-only mode the orchestrator skips run_process and waits on SIGINT/SIGTERM before tearing down the proxy. The entrypoint PID stays at 0 for the lifetime of the process, which silently degrades the proxy's binary-identity TOFU and the bypass monitor's PID enrichment; this is correct in a split-pod topology where the workload's /proc lives in another pod. In process-only mode run_networking is skipped entirely. SSH sessions get no proxy URL, no netns FD, and no CA paths, matching what a split-pod consumer would expect when network enforcement is delegated to a sidecar. The policy poll loop continues to run unconditionally; its OPA-reload and policy.local hooks already gate on the resources only present when network is enabled, and the env-refresh / proposals-toggle hooks remain active in process mode. Closes a step toward the RFC-0001 supervisor topology proposed in issue NVIDIA#1305 by drew. Signed-off-by: Radoslav Hubenov <[email protected]>
1 parent d0335cf commit f525d86

2 files changed

Lines changed: 157 additions & 38 deletions

File tree

‎crates/openshell-sandbox/src/lib.rs‎

Lines changed: 108 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ use openshell_supervisor_process::skills;
6363
/// # Errors
6464
///
6565
/// Returns an error if the command fails to start or encounters a fatal error.
66-
#[allow(clippy::too_many_arguments, clippy::similar_names)]
66+
#[allow(
67+
clippy::too_many_arguments,
68+
clippy::similar_names,
69+
clippy::fn_params_excessive_bools
70+
)]
6771
pub async fn run_sandbox(
6872
command: Vec<String>,
6973
workdir: Option<String>,
@@ -79,6 +83,8 @@ pub async fn run_sandbox(
7983
_health_port: u16,
8084
inference_routes: Option<String>,
8185
ocsf_enabled: Arc<std::sync::atomic::AtomicBool>,
86+
network_enabled: bool,
87+
process_enabled: bool,
8288
) -> Result<i32> {
8389
let (program, args) = command
8490
.split_first()
@@ -198,22 +204,32 @@ pub async fn run_sandbox(
198204
// it via setns(). The RAII handle lives in this frame for the duration
199205
// of the sandbox.
200206
#[cfg(target_os = "linux")]
201-
let netns = openshell_supervisor_network::run::create_netns_for_proxy(&policy)?;
207+
let netns = if network_enabled {
208+
openshell_supervisor_network::run::create_netns_for_proxy(&policy)?
209+
} else {
210+
None
211+
};
202212

203-
let mut networking = openshell_supervisor_network::run::run_networking(
204-
&policy,
205-
#[cfg(target_os = "linux")]
206-
netns.as_ref(),
207-
opa_engine.as_ref(),
208-
retained_proto.as_ref(),
209-
entrypoint_pid.clone(),
210-
&provider_credentials,
211-
sandbox_id.as_deref(),
212-
sandbox_name_for_agg.as_deref(),
213-
openshell_endpoint_for_proxy.as_deref(),
214-
inference_routes.as_deref(),
215-
)
216-
.await?;
213+
let mut networking = if network_enabled {
214+
Some(
215+
openshell_supervisor_network::run::run_networking(
216+
&policy,
217+
#[cfg(target_os = "linux")]
218+
netns.as_ref(),
219+
opa_engine.as_ref(),
220+
retained_proto.as_ref(),
221+
entrypoint_pid.clone(),
222+
&provider_credentials,
223+
sandbox_id.as_deref(),
224+
sandbox_name_for_agg.as_deref(),
225+
openshell_endpoint_for_proxy.as_deref(),
226+
inference_routes.as_deref(),
227+
)
228+
.await?,
229+
)
230+
} else {
231+
None
232+
};
217233

218234
// Spawn background policy poll task (gRPC mode only).
219235
if let (Some(id), Some(endpoint), Some(engine)) = (
@@ -227,7 +243,7 @@ pub async fn run_sandbox(
227243
let poll_ocsf_enabled = ocsf_enabled.clone();
228244
let poll_pid = entrypoint_pid.clone();
229245
let poll_provider_credentials = provider_credentials.clone();
230-
let poll_policy_local = networking.policy_local_ctx.clone();
246+
let poll_policy_local = networking.as_ref().map(|n| n.policy_local_ctx.clone());
231247
let poll_interval_secs: u64 = std::env::var("OPENSHELL_POLICY_POLL_INTERVAL_SECS")
232248
.ok()
233249
.and_then(|v| v.parse().ok())
@@ -240,7 +256,7 @@ pub async fn run_sandbox(
240256
interval_secs: poll_interval_secs,
241257
ocsf_enabled: poll_ocsf_enabled,
242258
provider_credentials: poll_provider_credentials,
243-
policy_local_ctx: Some(poll_policy_local),
259+
policy_local_ctx: poll_policy_local,
244260
};
245261

246262
tokio::spawn(async move {
@@ -257,30 +273,84 @@ pub async fn run_sandbox(
257273
});
258274
}
259275

260-
let exit_code = openshell_supervisor_process::run::run_process(
261-
program,
262-
args,
263-
workdir.as_deref(),
264-
timeout_secs,
265-
interactive,
266-
sandbox_id.as_deref(),
267-
openshell_endpoint.as_deref(),
268-
ssh_socket_path,
269-
&policy,
270-
entrypoint_pid,
271-
provider_credentials,
272-
provider_env,
273-
networking.ssh_proxy_url.take(),
274-
networking.ssh_netns_fd,
275-
networking.ca_file_paths.clone(),
276-
#[cfg(target_os = "linux")]
277-
netns.as_ref(),
278-
)
279-
.await?;
276+
let exit_code = if process_enabled {
277+
let (ssh_proxy_url, ssh_netns_fd, ca_file_paths) = match networking.as_mut() {
278+
Some(n) => (
279+
n.ssh_proxy_url.take(),
280+
n.ssh_netns_fd,
281+
n.ca_file_paths.clone(),
282+
),
283+
None => (None, None, None),
284+
};
285+
286+
openshell_supervisor_process::run::run_process(
287+
program,
288+
args,
289+
workdir.as_deref(),
290+
timeout_secs,
291+
interactive,
292+
sandbox_id.as_deref(),
293+
openshell_endpoint.as_deref(),
294+
ssh_socket_path,
295+
&policy,
296+
entrypoint_pid,
297+
provider_credentials,
298+
provider_env,
299+
ssh_proxy_url,
300+
ssh_netns_fd,
301+
ca_file_paths,
302+
#[cfg(target_os = "linux")]
303+
netns.as_ref(),
304+
)
305+
.await?
306+
} else {
307+
// Network-only sidecar mode: keep the proxy and its background
308+
// tasks alive (held via the `networking` value) until SIGINT or
309+
// SIGTERM. Exit 0 on clean shutdown.
310+
wait_for_shutdown_signal().await;
311+
0
312+
};
313+
314+
// Drop networking explicitly so the proxy + bypass monitor RAII
315+
// handles tear down before we return.
316+
drop(networking);
280317

281318
Ok(exit_code)
282319
}
283320

321+
/// Wait for SIGINT or SIGTERM. Used in network-only mode where there is
322+
/// no entrypoint child whose lifetime drives the supervisor's exit.
323+
async fn wait_for_shutdown_signal() {
324+
#[cfg(unix)]
325+
{
326+
use tokio::signal::unix::{SignalKind, signal};
327+
let mut sigterm = match signal(SignalKind::terminate()) {
328+
Ok(s) => s,
329+
Err(e) => {
330+
tracing::warn!(
331+
error = %e,
332+
"Failed to install SIGTERM handler; waiting on SIGINT only"
333+
);
334+
let _ = tokio::signal::ctrl_c().await;
335+
return;
336+
}
337+
};
338+
tokio::select! {
339+
_ = tokio::signal::ctrl_c() => {
340+
info!("Received SIGINT, shutting down network-only supervisor");
341+
}
342+
_ = sigterm.recv() => {
343+
info!("Received SIGTERM, shutting down network-only supervisor");
344+
}
345+
}
346+
}
347+
#[cfg(not(unix))]
348+
{
349+
let _ = tokio::signal::ctrl_c().await;
350+
info!("Received Ctrl-C, shutting down network-only supervisor");
351+
}
352+
}
353+
284354
// ============================================================================
285355
// Baseline filesystem path enrichment
286356
// ============================================================================

‎crates/openshell-sandbox/src/main.rs‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,45 @@ const COPY_SELF_SUBCOMMAND: &str = "copy-self";
3333
/// to confirm the cross-sandbox IDOR guard fires.
3434
const DEBUG_RPC_SUBCOMMAND: &str = "debug-rpc";
3535

36+
/// Default `--mode` value: run both supervisor leaves in a single binary.
37+
const DEFAULT_MODE: &str = "network,process";
38+
39+
/// Which supervisor leaves are enabled in this process.
40+
///
41+
/// Parsed from a comma-separated `--mode` value, e.g. `network`,
42+
/// `process`, or `network,process`. At least one must be set.
43+
#[derive(Clone, Copy, Debug)]
44+
struct Mode {
45+
network: bool,
46+
process: bool,
47+
}
48+
49+
impl std::str::FromStr for Mode {
50+
type Err = String;
51+
52+
fn from_str(s: &str) -> Result<Self, Self::Err> {
53+
let mut mode = Self {
54+
network: false,
55+
process: false,
56+
};
57+
for part in s.split(',').map(str::trim).filter(|p| !p.is_empty()) {
58+
match part {
59+
"network" => mode.network = true,
60+
"process" => mode.process = true,
61+
other => {
62+
return Err(format!(
63+
"unknown mode component '{other}' (expected 'network' and/or 'process')"
64+
));
65+
}
66+
}
67+
}
68+
if !mode.network && !mode.process {
69+
return Err("--mode must enable at least one of: network, process".into());
70+
}
71+
Ok(mode)
72+
}
73+
}
74+
3675
/// `OpenShell` Sandbox - process isolation and monitoring.
3776
#[derive(Parser, Debug)]
3877
#[command(name = "openshell-sandbox")]
@@ -105,6 +144,14 @@ struct Args {
105144
/// Port for health check endpoint.
106145
#[arg(long, default_value = "8080")]
107146
health_port: u16,
147+
148+
/// Which supervisor components to run. Comma-separated list of
149+
/// "network" and/or "process". Defaults to both (single-binary
150+
/// topology). Use --mode=network for a network-only sidecar, or
151+
/// --mode=process for a process-only supervisor when network
152+
/// enforcement runs in another pod.
153+
#[arg(long, default_value = DEFAULT_MODE)]
154+
mode: Mode,
108155
}
109156

110157
/// Copy the running executable to `dest`, creating parent directories as
@@ -308,6 +355,8 @@ fn main() -> Result<()> {
308355
args.health_port,
309356
args.inference_routes,
310357
ocsf_enabled,
358+
args.mode.network,
359+
args.mode.process,
311360
)
312361
.await
313362
})?;

0 commit comments

Comments
 (0)