From 4510b0d1bcd546fb8f98adda9c44dcf806ada8be Mon Sep 17 00:00:00 2001 From: Mesut Oezdil <114185853+mesutoezdil@users.noreply.github.com> Date: Wed, 29 Apr 2026 16:40:38 +0200 Subject: [PATCH 1/4] fix(net): catch IPv4-mapped blocked ranges in is_always_blocked_net (#1032) * fix(net): catch IPv4-mapped blocked ranges in is_always_blocked_net The IPv6 branch only checked whether the network address itself mapped to a blocked IPv4 address. A broader prefix like ::ffff:168.0.0.0/103 has a public network address but spans ::ffff:169.254.0.0, so the old code accepted it at policy load time while is_always_blocked_ip silently rejected every connection at runtime. Add three containment checks for the IPv4-mapped loopback, link-local, and unspecified representatives. The existing network-address check is kept because it handles single-host entries (/128) whose network address is already in a blocked range. Five new tests cover: single-host loopback and link-local mapped addresses, broad prefixes that span each blocked range without starting there, and a public single-host address that must not be blocked. * fix(net): address clippy warnings in is_always_blocked_net Use Ipv4Addr::LOCALHOST instead of Ipv4Addr::new(127, 0, 0, 1) and collapse the nested if let / if into is_some_and. --- crates/openshell-core/src/net.rs | 58 +++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/crates/openshell-core/src/net.rs b/crates/openshell-core/src/net.rs index 3cd00d7b8a..a73cb38dc3 100644 --- a/crates/openshell-core/src/net.rs +++ b/crates/openshell-core/src/net.rs @@ -97,11 +97,23 @@ pub fn is_always_blocked_net(net: ipnet::IpNet) -> bool { return true; } - // Check IPv4-mapped IPv6 (::ffff:127.0.0.1, ::ffff:169.254.x.x, etc.) - if let Some(v4) = network.to_ipv4_mapped() { - if v4.is_loopback() || v4.is_link_local() || v4.is_unspecified() { - return true; - } + // Check IPv4-mapped IPv6 addresses. The network-address check covers + // ranges whose first address is already in a blocked range + // (e.g. ::ffff:127.0.0.1/128, ::ffff:169.254.0.1/128). The + // containment checks below catch broader prefixes that only reach + // into a blocked range further in — e.g. ::ffff:168.0.0.0/103 + // has a public network address but spans ::ffff:169.254.0.0. + if network + .to_ipv4_mapped() + .is_some_and(|v4| v4.is_loopback() || v4.is_link_local() || v4.is_unspecified()) + { + return true; + } + if v6net.contains(&Ipv4Addr::LOCALHOST.to_ipv6_mapped()) + || v6net.contains(&Ipv4Addr::new(169, 254, 0, 0).to_ipv6_mapped()) + || v6net.contains(&Ipv4Addr::UNSPECIFIED.to_ipv6_mapped()) + { + return true; } false @@ -332,6 +344,42 @@ mod tests { assert!(is_always_blocked_net(net)); } + #[test] + fn test_always_blocked_net_v6_ipv4_mapped_loopback_single() { + let net: ipnet::IpNet = "::ffff:127.0.0.1/128".parse().unwrap(); + assert!(is_always_blocked_net(net)); + } + + #[test] + fn test_always_blocked_net_v6_ipv4_mapped_link_local_single() { + let net: ipnet::IpNet = "::ffff:169.254.0.1/128".parse().unwrap(); + assert!(is_always_blocked_net(net)); + } + + #[test] + fn test_always_blocked_net_v6_ipv4_mapped_broad_spans_link_local() { + // ::ffff:168.0.0.0/103 has a public network address (168.0.0.0) but + // the range covers 168.0.0.0–169.255.255.255, which includes the + // link-local block 169.254.0.0/16. + let net: ipnet::IpNet = "::ffff:168.0.0.0/103".parse().unwrap(); + assert!(is_always_blocked_net(net)); + } + + #[test] + fn test_always_blocked_net_v6_ipv4_mapped_broad_spans_loopback() { + // ::ffff:64.0.0.0/98 has a public network address (64.0.0.0) but the + // range covers 64.0.0.0–127.255.255.255, which includes loopback. + let net: ipnet::IpNet = "::ffff:64.0.0.0/98".parse().unwrap(); + assert!(is_always_blocked_net(net)); + } + + #[test] + fn test_always_blocked_net_v6_ipv4_mapped_allows_public() { + // ::ffff:8.8.8.8/128 is a public address — should not be blocked. + let net: ipnet::IpNet = "::ffff:8.8.8.8/128".parse().unwrap(); + assert!(!is_always_blocked_net(net)); + } + // -- is_internal_ip -- #[test] From c0ffa933ed8a51dc14c87e13a5dfe2db7592c2aa Mon Sep 17 00:00:00 2001 From: Florent BENOIT Date: Wed, 29 Apr 2026 16:49:45 +0200 Subject: [PATCH 2/4] feat(openshell-vm): allow to have tty with exec (#939) add support on both side (rust and python) fixes https://github.com/NVIDIA/OpenShell/issues/936 Signed-off-by: Florent Benoit --- .../scripts/openshell-vm-exec-agent.py | 161 ++++++- crates/openshell-vm/src/exec.rs | 415 +++++++++++++++++- 2 files changed, 568 insertions(+), 8 deletions(-) diff --git a/crates/openshell-vm/scripts/openshell-vm-exec-agent.py b/crates/openshell-vm/scripts/openshell-vm-exec-agent.py index d7ffd81df4..f2b384cf9d 100644 --- a/crates/openshell-vm/scripts/openshell-vm-exec-agent.py +++ b/crates/openshell-vm/scripts/openshell-vm-exec-agent.py @@ -3,11 +3,15 @@ # SPDX-License-Identifier: Apache-2.0 import base64 +import fcntl import json import os +import pty import socket +import struct import subprocess import sys +import termios import threading @@ -42,6 +46,11 @@ def validate_env(env_items): return env +def set_winsize(fd, cols, rows): + winsize = struct.pack("HHHH", rows, cols, 0, 0) + fcntl.ioctl(fd, termios.TIOCSWINSZ, winsize) + + def stream_reader(pipe, frame_type, sock_file, lock): try: while True: @@ -79,6 +88,8 @@ def stdin_writer(proc, sock_file, sock, lock): proc.stdin.flush() elif kind == "stdin_close": break + elif kind == "resize": + pass else: send_frame( sock_file, @@ -96,14 +107,10 @@ def stdin_writer(proc, sock_file, sock, lock): pass -def handle_client(conn): - sock_file = conn.makefile("rwb", buffering=0) +def handle_client_pipe(conn, request, sock_file): + """Handle a client connection using pipes (non-TTY mode).""" lock = threading.Lock() try: - request = recv_line(sock_file) - if request is None: - return - argv = request.get("argv") or ["sh"] cwd = request.get("cwd") env = os.environ.copy() @@ -153,6 +160,148 @@ def handle_client(conn): conn.close() +def handle_client_tty(conn, request, sock_file): + """Handle a client connection with PTY allocation.""" + lock = threading.Lock() + master_fd = -1 + try: + argv = request.get("argv") or ["sh"] + cwd = request.get("cwd") + env = os.environ.copy() + env.update(validate_env(request.get("env") or [])) + env.setdefault("TERM", "xterm-256color") + + master_fd, slave_fd = pty.openpty() + + # Consume any resize frame sent right after the ExecRequest. + # The host sends it before starting the stdin pump, so it + # should arrive quickly. Use a short socket timeout. + conn.settimeout(0.5) + try: + pending = sock_file.readline() + if pending: + frame = json.loads(pending.decode("utf-8")) + if frame.get("type") == "resize": + set_winsize( + slave_fd, + frame.get("cols", 80), + frame.get("rows", 24), + ) + except (socket.timeout, ValueError, OSError): + pass + finally: + conn.settimeout(None) + + proc = subprocess.Popen( + argv, + cwd=cwd or "/", + env=env, + stdin=slave_fd, + stdout=slave_fd, + stderr=slave_fd, + preexec_fn=os.setsid, + ) + os.close(slave_fd) + + def pty_reader(): + try: + while True: + try: + chunk = os.read(master_fd, 8192) + except OSError: + break + if not chunk: + break + send_frame( + sock_file, + lock, + { + "type": "stdout", + "data": base64.b64encode(chunk).decode("ascii"), + }, + ) + except Exception: + pass + + def pty_stdin_writer(): + try: + while True: + frame = recv_line(sock_file) + if frame is None: + break + kind = frame.get("type") + if kind == "stdin": + payload = base64.b64decode(frame.get("data", "")) + try: + os.write(master_fd, payload) + except OSError: + break + elif kind == "resize": + try: + set_winsize( + master_fd, + frame.get("cols", 80), + frame.get("rows", 24), + ) + except OSError: + pass + elif kind == "stdin_close": + break + else: + send_frame( + sock_file, + lock, + {"type": "error", "message": f"unknown frame type: {kind}"}, + ) + break + except (BrokenPipeError, OSError): + pass + + reader_thread = threading.Thread(target=pty_reader, daemon=True) + stdin_thread = threading.Thread(target=pty_stdin_writer, daemon=True) + reader_thread.start() + stdin_thread.start() + + code = proc.wait() + reader_thread.join(timeout=2) + send_frame(sock_file, lock, {"type": "exit", "code": code}) + except Exception as exc: + try: + send_frame(sock_file, lock, {"type": "error", "message": str(exc)}) + except Exception: + pass + finally: + if master_fd >= 0: + try: + os.close(master_fd) + except OSError: + pass + try: + sock_file.close() + except Exception: + pass + conn.close() + + +def handle_client(conn): + sock_file = conn.makefile("rwb", buffering=0) + try: + request = recv_line(sock_file) + if request is None: + sock_file.close() + conn.close() + return + except Exception: + sock_file.close() + conn.close() + return + + if request.get("tty"): + handle_client_tty(conn, request, sock_file) + else: + handle_client_pipe(conn, request, sock_file) + + def main(): if not hasattr(socket, "AF_VSOCK"): print("AF_VSOCK is not available", file=sys.stderr) diff --git a/crates/openshell-vm/src/exec.rs b/crates/openshell-vm/src/exec.rs index 6195556e14..f3198a6bef 100644 --- a/crates/openshell-vm/src/exec.rs +++ b/crates/openshell-vm/src/exec.rs @@ -3,12 +3,14 @@ use std::fs::{self, File}; use std::io::{BufRead, BufReader, Read, Write}; +use std::os::fd::{AsFd, BorrowedFd}; use std::os::unix::net::UnixStream; use std::path::{Path, PathBuf}; use std::thread; use std::time::{SystemTime, UNIX_EPOCH}; use base64::Engine as _; +use nix::sys::termios::{self, SetArg, Termios}; use serde::{Deserialize, Serialize}; use crate::VmError; @@ -87,6 +89,7 @@ struct ExecRequest { enum ClientFrame { Stdin { data: String }, StdinClose, + Resize { cols: u16, rows: u16 }, } #[derive(Debug, Deserialize)] @@ -98,6 +101,46 @@ enum ServerFrame { Error { message: String }, } +struct RawModeGuard { + raw_fd: i32, + original: Termios, +} + +impl RawModeGuard { + fn enter() -> Result { + let stdin = std::io::stdin(); + let fd = stdin.as_fd(); + let original = + termios::tcgetattr(&fd).map_err(|e| VmError::Exec(format!("tcgetattr: {e}")))?; + let mut raw = original.clone(); + termios::cfmakeraw(&mut raw); + termios::tcsetattr(&fd, SetArg::TCSANOW, &raw) + .map_err(|e| VmError::Exec(format!("tcsetattr: {e}")))?; + Ok(Self { + raw_fd: std::os::unix::io::AsRawFd::as_raw_fd(&stdin), + original, + }) + } +} + +impl Drop for RawModeGuard { + fn drop(&mut self) { + let fd = unsafe { BorrowedFd::borrow_raw(self.raw_fd) }; + let _ = termios::tcsetattr(&fd, SetArg::TCSANOW, &self.original); + } +} + +fn get_terminal_size() -> Option<(u16, u16)> { + let fd = std::os::unix::io::AsRawFd::as_raw_fd(&std::io::stdout()); + let mut ws: libc::winsize = unsafe { std::mem::zeroed() }; + let rc = unsafe { libc::ioctl(fd, libc::TIOCGWINSZ, &mut ws) }; + if rc == 0 && ws.ws_col > 0 && ws.ws_row > 0 { + Some((ws.ws_col, ws.ws_row)) + } else { + None + } +} + pub fn vm_exec_socket_path(rootfs: &Path) -> PathBuf { // Prefer XDG_RUNTIME_DIR (per-user, restricted permissions on Linux), // fall back to /tmp. Ownership/symlink validation happens in @@ -495,9 +538,19 @@ pub fn exec_running_vm(options: VmExecOptions) -> Result { }; send_json_line(&mut writer, &request)?; + let tty = options.tty; + let _raw_guard = if tty { + if let Some((cols, rows)) = get_terminal_size() { + send_json_line(&mut writer, &ClientFrame::Resize { cols, rows })?; + } + Some(RawModeGuard::enter()?) + } else { + None + }; + let stdin_writer = writer; thread::spawn(move || { - let _ = pump_stdin(stdin_writer); + let _ = pump_stdin(stdin_writer, tty); }); let mut reader = BufReader::new(&mut stream); @@ -724,10 +777,11 @@ fn send_json_line(writer: &mut UnixStream, value: &T) -> Result<() .map_err(|e| VmError::Exec(format!("write VM exec request: {e}"))) } -fn pump_stdin(mut writer: UnixStream) -> Result<(), VmError> { +fn pump_stdin(mut writer: UnixStream, tty: bool) -> Result<(), VmError> { let stdin = std::io::stdin(); let mut stdin = stdin.lock(); let mut buf = [0u8; 8192]; + let mut last_size: Option<(u16, u16)> = None; loop { let read = stdin @@ -736,6 +790,22 @@ fn pump_stdin(mut writer: UnixStream) -> Result<(), VmError> { if read == 0 { break; } + + if tty { + if let Some(size) = get_terminal_size() { + if last_size != Some(size) { + last_size = Some(size); + let _ = send_json_line( + &mut writer, + &ClientFrame::Resize { + cols: size.0, + rows: size.1, + }, + ); + } + } + } + let frame = ClientFrame::Stdin { data: base64::engine::general_purpose::STANDARD.encode(&buf[..read]), }; @@ -765,3 +835,344 @@ fn now_ms() -> Result { .map_err(|e| VmError::RuntimeState(format!("read system clock: {e}")))?; Ok(duration.as_millis()) } + +#[cfg(test)] +mod tests { + use super::*; + + // ── ExecRequest serialization ──────────────────────────────────── + + #[test] + fn exec_request_serializes_with_tty() { + let req = ExecRequest { + argv: vec!["sh".into()], + env: vec!["TERM=xterm".into()], + cwd: None, + tty: true, + }; + let json: serde_json::Value = serde_json::to_value(&req).unwrap(); + assert_eq!(json["argv"], serde_json::json!(["sh"])); + assert_eq!(json["tty"], true); + assert_eq!(json["cwd"], serde_json::Value::Null); + } + + #[test] + fn exec_request_serializes_without_tty() { + let req = ExecRequest { + argv: vec!["echo".into(), "hello".into()], + env: vec![], + cwd: Some("/tmp".into()), + tty: false, + }; + let json: serde_json::Value = serde_json::to_value(&req).unwrap(); + assert_eq!(json["tty"], false); + assert_eq!(json["cwd"], "/tmp"); + } + + // ── ClientFrame serialization ──────────────────────────────────── + + #[test] + fn client_frame_stdin_serializes() { + let frame = ClientFrame::Stdin { + data: "aGVsbG8=".into(), + }; + let json: serde_json::Value = serde_json::to_value(&frame).unwrap(); + assert_eq!(json["type"], "stdin"); + assert_eq!(json["data"], "aGVsbG8="); + } + + #[test] + fn client_frame_stdin_close_serializes() { + let frame = ClientFrame::StdinClose; + let json: serde_json::Value = serde_json::to_value(&frame).unwrap(); + assert_eq!(json["type"], "stdin_close"); + } + + #[test] + fn client_frame_resize_serializes() { + let frame = ClientFrame::Resize { + cols: 120, + rows: 40, + }; + let json: serde_json::Value = serde_json::to_value(&frame).unwrap(); + assert_eq!(json["type"], "resize"); + assert_eq!(json["cols"], 120); + assert_eq!(json["rows"], 40); + } + + // ── ServerFrame deserialization ─────────────────────────────────── + + #[test] + fn server_frame_stdout_deserializes() { + let json = r#"{"type":"stdout","data":"aGVsbG8="}"#; + let frame: ServerFrame = serde_json::from_str(json).unwrap(); + assert!(matches!(frame, ServerFrame::Stdout { data } if data == "aGVsbG8=")); + } + + #[test] + fn server_frame_stderr_deserializes() { + let json = r#"{"type":"stderr","data":"ZXJy"}"#; + let frame: ServerFrame = serde_json::from_str(json).unwrap(); + assert!(matches!(frame, ServerFrame::Stderr { data } if data == "ZXJy")); + } + + #[test] + fn server_frame_exit_deserializes() { + let json = r#"{"type":"exit","code":42}"#; + let frame: ServerFrame = serde_json::from_str(json).unwrap(); + assert!(matches!(frame, ServerFrame::Exit { code: 42 })); + } + + #[test] + fn server_frame_error_deserializes() { + let json = r#"{"type":"error","message":"boom"}"#; + let frame: ServerFrame = serde_json::from_str(json).unwrap(); + assert!(matches!(frame, ServerFrame::Error { message } if message == "boom")); + } + + #[test] + fn server_frame_unknown_type_fails() { + let json = r#"{"type":"unknown","data":"x"}"#; + assert!(serde_json::from_str::(json).is_err()); + } + + // ── ClientFrame ↔ ServerFrame round-trip compatibility ─────────── + // Verify that what the Rust host serializes can be parsed by the + // Python agent (same JSON shape), and vice versa. + + #[test] + fn resize_frame_has_expected_json_shape() { + let frame = ClientFrame::Resize { cols: 80, rows: 24 }; + let s = serde_json::to_string(&frame).unwrap(); + let v: serde_json::Value = serde_json::from_str(&s).unwrap(); + assert_eq!(v["type"].as_str().unwrap(), "resize"); + assert!(v["cols"].is_u64()); + assert!(v["rows"].is_u64()); + } + + // ── validate_env_vars ──────────────────────────────────────────── + + #[test] + fn validate_env_vars_accepts_valid() { + let items = vec![ + "HOME=/root".to_string(), + "PATH=/usr/bin".to_string(), + "_UNDERSCORE=1".to_string(), + "A1B2=val".to_string(), + ]; + assert!(validate_env_vars(&items).is_ok()); + } + + #[test] + fn validate_env_vars_rejects_missing_equals() { + let items = vec!["NOEQUALS".to_string()]; + assert!(validate_env_vars(&items).is_err()); + } + + #[test] + fn validate_env_vars_rejects_empty_key() { + let items = vec!["=value".to_string()]; + assert!(validate_env_vars(&items).is_err()); + } + + #[test] + fn validate_env_vars_rejects_leading_digit() { + let items = vec!["1BAD=val".to_string()]; + assert!(validate_env_vars(&items).is_err()); + } + + #[test] + fn validate_env_vars_rejects_special_chars() { + let items = vec!["BAD-KEY=val".to_string()]; + assert!(validate_env_vars(&items).is_err()); + } + + // ── decode_payload ─────────────────────────────────────────────── + + #[test] + fn decode_payload_valid_base64() { + let decoded = decode_payload("aGVsbG8=").unwrap(); + assert_eq!(decoded, b"hello"); + } + + #[test] + fn decode_payload_empty() { + let decoded = decode_payload("").unwrap(); + assert!(decoded.is_empty()); + } + + #[test] + fn decode_payload_invalid_base64() { + assert!(decode_payload("!!!not-base64!!!").is_err()); + } + + // ── Resize frame edge cases ────────────────────────────────────── + + #[test] + fn resize_frame_max_dimensions() { + let frame = ClientFrame::Resize { + cols: u16::MAX, + rows: u16::MAX, + }; + let json: serde_json::Value = serde_json::to_value(&frame).unwrap(); + assert_eq!(json["cols"], u16::MAX as u64); + assert_eq!(json["rows"], u16::MAX as u64); + } + + #[test] + fn resize_frame_minimum_dimensions() { + let frame = ClientFrame::Resize { cols: 1, rows: 1 }; + let json: serde_json::Value = serde_json::to_value(&frame).unwrap(); + assert_eq!(json["cols"], 1); + assert_eq!(json["rows"], 1); + } + + // ── Wire format: newline-delimited JSON ────────────────────────── + // The protocol sends one JSON object per line. Verify that + // serialized frames produce valid single-line JSON that the + // Python agent can split on '\n' and json.loads(). + + #[test] + fn client_frames_serialize_to_single_line_json() { + let frames: Vec = vec![ + ClientFrame::Stdin { + data: "dGVzdA==".into(), + }, + ClientFrame::StdinClose, + ClientFrame::Resize { cols: 80, rows: 24 }, + ]; + for frame in &frames { + let s = serde_json::to_string(frame).unwrap(); + assert!(!s.contains('\n'), "frame should be single-line: {s}"); + let _: serde_json::Value = serde_json::from_str(&s).unwrap(); + } + } + + #[test] + fn exec_request_serializes_to_single_line_json() { + let req = ExecRequest { + argv: vec!["bash".into(), "-c".into(), "echo 'hello world'".into()], + env: vec!["HOME=/root".into(), "TERM=xterm-256color".into()], + cwd: Some("/home/user".into()), + tty: true, + }; + let s = serde_json::to_string(&req).unwrap(); + assert!(!s.contains('\n')); + let _: serde_json::Value = serde_json::from_str(&s).unwrap(); + } + + // ── Stdin data encode → decode round-trip ──────────────────────── + // Mirrors the flow: host encodes payload as base64 in a Stdin + // frame, guest decodes with decode_payload(). + + #[test] + fn stdin_payload_round_trip() { + let original = b"echo hello\n"; + let encoded = base64::engine::general_purpose::STANDARD.encode(original); + let frame = ClientFrame::Stdin { + data: encoded.clone(), + }; + let json = serde_json::to_string(&frame).unwrap(); + let parsed: serde_json::Value = serde_json::from_str(&json).unwrap(); + let decoded = decode_payload(parsed["data"].as_str().unwrap()).unwrap(); + assert_eq!(decoded, original); + } + + #[test] + fn stdin_payload_round_trip_binary() { + let original: Vec = (0..=255).collect(); + let encoded = base64::engine::general_purpose::STANDARD.encode(&original); + let decoded = decode_payload(&encoded).unwrap(); + assert_eq!(decoded, original); + } + + // ── Python agent compatibility ─────────────────────────────────── + // The Python agent parses frames with json.loads() and dispatches + // on frame["type"]. These tests verify the exact field names and + // values match what the Python code expects. + + #[test] + fn exec_request_tty_field_matches_python_dispatch() { + // Python: request.get("tty") — must be a JSON boolean + let req = ExecRequest { + argv: vec!["sh".into()], + env: vec![], + cwd: None, + tty: true, + }; + let v: serde_json::Value = serde_json::to_value(&req).unwrap(); + assert!(v["tty"].is_boolean()); + assert_eq!(v["tty"].as_bool().unwrap(), true); + + let req_no_tty = ExecRequest { + argv: vec!["echo".into()], + env: vec![], + cwd: None, + tty: false, + }; + let v: serde_json::Value = serde_json::to_value(&req_no_tty).unwrap(); + assert_eq!(v["tty"].as_bool().unwrap(), false); + } + + #[test] + fn resize_type_tag_is_snake_case() { + // Python: kind == "resize" — must be lowercase snake_case + let frame = ClientFrame::Resize { cols: 80, rows: 24 }; + let v: serde_json::Value = serde_json::to_value(&frame).unwrap(); + assert_eq!(v["type"].as_str().unwrap(), "resize"); + } + + #[test] + fn stdin_close_type_tag_is_snake_case() { + // Python: kind == "stdin_close" + let frame = ClientFrame::StdinClose; + let v: serde_json::Value = serde_json::to_value(&frame).unwrap(); + assert_eq!(v["type"].as_str().unwrap(), "stdin_close"); + } + + #[test] + fn resize_fields_are_integers_not_strings() { + // Python: frame.get("cols", 80) — expects int, not string + let frame = ClientFrame::Resize { + cols: 200, + rows: 50, + }; + let v: serde_json::Value = serde_json::to_value(&frame).unwrap(); + assert!(v["cols"].is_u64()); + assert!(v["rows"].is_u64()); + } + + // ── ServerFrame: Python agent output ───────────────────────────── + // These mirror the exact JSON the Python agent produces with + // json.dumps(frame, separators=(",", ":")) + + #[test] + fn server_frame_parses_compact_json() { + // Python uses separators=(",", ":") — no spaces + let compact = r#"{"type":"stdout","data":"aGk="}"#; + let frame: ServerFrame = serde_json::from_str(compact).unwrap(); + assert!(matches!(frame, ServerFrame::Stdout { data } if data == "aGk=")); + } + + #[test] + fn server_frame_exit_code_zero() { + let json = r#"{"type":"exit","code":0}"#; + let frame: ServerFrame = serde_json::from_str(json).unwrap(); + assert!(matches!(frame, ServerFrame::Exit { code: 0 })); + } + + #[test] + fn server_frame_exit_code_negative() { + let json = r#"{"type":"exit","code":-1}"#; + let frame: ServerFrame = serde_json::from_str(json).unwrap(); + assert!(matches!(frame, ServerFrame::Exit { code: -1 })); + } + + #[test] + fn server_frame_tolerates_extra_fields() { + // Future-proofing: agent may add fields we don't know about + let json = r#"{"type":"exit","code":0,"extra":"ignored"}"#; + let frame: ServerFrame = serde_json::from_str(json).unwrap(); + assert!(matches!(frame, ServerFrame::Exit { code: 0 })); + } +} From 2adddaa6b2a82c593186514a6c741c4e9b647dcb Mon Sep 17 00:00:00 2001 From: vince-brisebois Date: Wed, 29 Apr 2026 09:18:27 -0700 Subject: [PATCH 3/4] feat: Adding qemu vm driver support with GPU pass-through (#992) * Adding qemu vm driver support with GPU pass-through Signed-off-by: Vincent Caux-Brisebois * Add GPU rootfs variant, harden VFIO binding, and fix networking and supervisor reliability issues discovered during GPU VM bring-up. Signed-off-by: Vincent Caux-Brisebois --------- Signed-off-by: Vincent Caux-Brisebois --- Cargo.lock | 14 + architecture/podman-rootless-networking.md | 16 +- crates/openshell-cli/src/main.rs | 8 + crates/openshell-cli/src/run.rs | 4 + .../sandbox_create_lifecycle_integration.rs | 5 + crates/openshell-driver-docker/src/lib.rs | 1 + crates/openshell-driver-docker/src/tests.rs | 1 + .../openshell-driver-kubernetes/src/driver.rs | 1 + crates/openshell-driver-podman/src/driver.rs | 1 + crates/openshell-driver-vm/Cargo.toml | 3 + crates/openshell-driver-vm/README.md | 18 +- crates/openshell-driver-vm/build.rs | 20 +- .../scripts/openshell-vm-sandbox-init.sh | 184 +- crates/openshell-driver-vm/src/driver.rs | 411 ++++- crates/openshell-driver-vm/src/gpu.rs | 316 ++++ crates/openshell-driver-vm/src/lib.rs | 6 +- crates/openshell-driver-vm/src/main.rs | 57 +- crates/openshell-driver-vm/src/rootfs.rs | 45 +- crates/openshell-driver-vm/src/runtime.rs | 654 ++++++- crates/openshell-driver-vm/start.sh | 60 +- crates/openshell-sandbox/src/lib.rs | 70 +- crates/openshell-server/src/compute/mod.rs | 3 + crates/openshell-vfio/Cargo.toml | 23 + crates/openshell-vfio/src/lib.rs | 1575 +++++++++++++++++ mise.lock | 112 ++ proto/compute_driver.proto | 6 + proto/openshell.proto | 4 + tasks/scripts/vm/build-rootfs-tarball.sh | 319 +++- tasks/scripts/vm/compress-vm-runtime.sh | 4 +- tasks/vm.toml | 2 +- 30 files changed, 3801 insertions(+), 142 deletions(-) create mode 100644 crates/openshell-driver-vm/src/gpu.rs create mode 100644 crates/openshell-vfio/Cargo.toml create mode 100644 crates/openshell-vfio/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index e58168a8ae..0e59eb64fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3210,8 +3210,11 @@ dependencies = [ "miette", "nix", "openshell-core", + "openshell-vfio", "polling", "prost-types", + "serde", + "serde_json", "tar", "tokio", "tokio-stream", @@ -3407,6 +3410,17 @@ dependencies = [ "url", ] +[[package]] +name = "openshell-vfio" +version = "0.0.0" +dependencies = [ + "serde", + "serde_json", + "tempfile", + "thiserror 2.0.18", + "tracing", +] + [[package]] name = "openshell-vm" version = "0.0.0" diff --git a/architecture/podman-rootless-networking.md b/architecture/podman-rootless-networking.md index b267cfffac..d13b9ca849 100644 --- a/architecture/podman-rootless-networking.md +++ b/architecture/podman-rootless-networking.md @@ -35,7 +35,7 @@ For rootful bridge networking: 6. Netavark configures iptables/nftables rules -- masquerade for outbound, DNAT for port mappings 7. Netavark starts aardvark-dns if DNS is enabled, listening on the bridge gateway address -``` +```text Host Kernel | +-- Bridge interface (e.g., "podman0") <-- created by Netavark @@ -60,7 +60,7 @@ Unprivileged users cannot create network interfaces on the host. They cannot cre Pasta (part of the `passt` project -- same binary, different command name) operates entirely in userspace, translating between the container's L2 TAP interface and the host's L4 sockets. It requires no capabilities or privileges. -``` +```text Container Network Namespace | +-- TAP device (e.g., "eth0") @@ -131,7 +131,7 @@ Unlike bridge networking, pasta containers are isolated from each other by defau The Podman compute driver creates three layers of network isolation: -``` +```text Namespace 1: Host | pasta manages port forwarding (127.0.0.1:) @@ -164,7 +164,7 @@ client.ensure_network(&config.network_name).await?; This creates a bridge network named `"openshell"` (default from `DEFAULT_NETWORK_NAME` in `openshell-core/src/config.rs`) with `dns_enabled: true`. In rootless mode, this bridge exists inside a user namespace managed by pasta. The bridge IP range (e.g., `10.89.x.x`) is not routable from the host. -``` +```text Host (your machine) | 127.0.0.1: <--- pasta binds this on the host @@ -212,7 +212,7 @@ The bridge gateway IP does NOT work for this purpose in rootless mode because it Inside the container, the supervisor creates another network namespace (`netns.rs:53-178`, setup at lines 53-63, `ip netns add` at line 77) for the user workload: -``` +```text Container (10.89.1.2 on the Podman bridge) | [Supervisor process - runs in container's default netns] @@ -247,7 +247,7 @@ A tmpfs is mounted at `/run/netns` in the container spec (`container.rs:458-463` ### SSH Session: Client to Sandbox Shell -``` +```text Client (CLI on user's machine) | 1. gRPC: CreateSshSession -> gateway (returns token, connect_path) @@ -281,7 +281,7 @@ The SSH daemon listens on a Unix socket (not a TCP port) with 0600 permissions. ### Outbound HTTP Request from Sandbox Process -``` +```text User's code (inner netns, 10.200.0.2) | 1. curl https://api.example.com @@ -306,7 +306,7 @@ Supervisor proxy (10.200.0.1:3128 in container netns) ### Supervisor gRPC Callback to Gateway -``` +```text Supervisor (container netns, 10.89.x.2) | 1. gRPC connect to http://host.containers.internal:8080 diff --git a/crates/openshell-cli/src/main.rs b/crates/openshell-cli/src/main.rs index df5fb00617..3853993129 100644 --- a/crates/openshell-cli/src/main.rs +++ b/crates/openshell-cli/src/main.rs @@ -1138,6 +1138,11 @@ enum SandboxCommands { #[arg(long)] gpu: bool, + /// Target a specific GPU by PCI address (e.g. "0000:2d:00.0") or index (e.g. "0", "1"). + /// Only valid with --gpu. When omitted with --gpu, the first available GPU is assigned. + #[arg(long, requires = "gpu")] + gpu_device: Option, + /// SSH destination for remote bootstrap (e.g., user@hostname). /// Only used when no cluster exists yet; ignored if a cluster is /// already active. @@ -2307,6 +2312,7 @@ async fn main() -> Result<()> { no_keep, editor, gpu, + gpu_device, remote, ssh_key, providers, @@ -2402,6 +2408,7 @@ async fn main() -> Result<()> { upload_spec.as_ref(), keep, gpu, + gpu_device.as_deref(), editor, remote.as_deref(), ssh_key.as_deref(), @@ -2425,6 +2432,7 @@ async fn main() -> Result<()> { upload_spec.as_ref(), keep, gpu, + gpu_device.as_deref(), editor, remote.as_deref(), ssh_key.as_deref(), diff --git a/crates/openshell-cli/src/run.rs b/crates/openshell-cli/src/run.rs index 769c37748f..21cb40b4e4 100644 --- a/crates/openshell-cli/src/run.rs +++ b/crates/openshell-cli/src/run.rs @@ -1923,6 +1923,7 @@ pub async fn sandbox_create_with_bootstrap( upload: Option<&(String, Option, bool)>, keep: bool, gpu: bool, + gpu_device: Option<&str>, editor: Option, remote: Option<&str>, ssh_key: Option<&str>, @@ -1954,6 +1955,7 @@ pub async fn sandbox_create_with_bootstrap( upload, keep, gpu, + gpu_device, editor, remote, ssh_key, @@ -2010,6 +2012,7 @@ pub async fn sandbox_create( upload: Option<&(String, Option, bool)>, keep: bool, gpu: bool, + gpu_device: Option<&str>, editor: Option, remote: Option<&str>, ssh_key: Option<&str>, @@ -2117,6 +2120,7 @@ pub async fn sandbox_create( let request = CreateSandboxRequest { spec: Some(SandboxSpec { gpu: requested_gpu, + gpu_device: gpu_device.unwrap_or_default().to_string(), policy, providers: configured_providers, template, diff --git a/crates/openshell-cli/tests/sandbox_create_lifecycle_integration.rs b/crates/openshell-cli/tests/sandbox_create_lifecycle_integration.rs index 79d482fdb9..6b78dab9a2 100644 --- a/crates/openshell-cli/tests/sandbox_create_lifecycle_integration.rs +++ b/crates/openshell-cli/tests/sandbox_create_lifecycle_integration.rs @@ -574,6 +574,7 @@ async fn sandbox_create_keeps_command_sessions_by_default() { None, None, None, + None, &[], None, None, @@ -615,6 +616,7 @@ async fn sandbox_create_deletes_command_sessions_with_no_keep() { None, None, None, + None, &[], None, None, @@ -659,6 +661,7 @@ async fn sandbox_create_deletes_shell_sessions_with_no_keep() { None, None, None, + None, &[], None, None, @@ -703,6 +706,7 @@ async fn sandbox_create_keeps_sandbox_with_hidden_keep_flag() { None, None, None, + None, &[], None, None, @@ -744,6 +748,7 @@ async fn sandbox_create_keeps_sandbox_with_forwarding() { None, None, None, + None, &[], None, Some(openshell_core::forward::ForwardSpec::new(8080)), diff --git a/crates/openshell-driver-docker/src/lib.rs b/crates/openshell-driver-docker/src/lib.rs index b4371ce7d9..d9683d416b 100644 --- a/crates/openshell-driver-docker/src/lib.rs +++ b/crates/openshell-driver-docker/src/lib.rs @@ -231,6 +231,7 @@ impl DockerComputeDriver { driver_version: self.config.daemon_version.clone(), default_image: self.config.default_image.clone(), supports_gpu: false, + gpu_count: 0, } } diff --git a/crates/openshell-driver-docker/src/tests.rs b/crates/openshell-driver-docker/src/tests.rs index b20fbf5cea..cf9632c40d 100644 --- a/crates/openshell-driver-docker/src/tests.rs +++ b/crates/openshell-driver-docker/src/tests.rs @@ -30,6 +30,7 @@ fn test_sandbox() -> DriverSandbox { platform_config: None, }), gpu: false, + gpu_device: String::new(), }), status: None, } diff --git a/crates/openshell-driver-kubernetes/src/driver.rs b/crates/openshell-driver-kubernetes/src/driver.rs index 444e0f55d3..a3f90457c8 100644 --- a/crates/openshell-driver-kubernetes/src/driver.rs +++ b/crates/openshell-driver-kubernetes/src/driver.rs @@ -150,6 +150,7 @@ impl KubernetesComputeDriver { driver_version: openshell_core::VERSION.to_string(), default_image: self.config.default_image.clone(), supports_gpu: self.has_gpu_capacity().await.unwrap_or(false), + gpu_count: 0, }) } diff --git a/crates/openshell-driver-podman/src/driver.rs b/crates/openshell-driver-podman/src/driver.rs index e4b0170023..dff95a5324 100644 --- a/crates/openshell-driver-podman/src/driver.rs +++ b/crates/openshell-driver-podman/src/driver.rs @@ -161,6 +161,7 @@ impl PodmanComputeDriver { driver_version: openshell_core::VERSION.to_string(), default_image: self.config.default_image.clone(), supports_gpu, + gpu_count: 0, }) } diff --git a/crates/openshell-driver-vm/Cargo.toml b/crates/openshell-driver-vm/Cargo.toml index b4d92b0fc1..04f4e9fc5f 100644 --- a/crates/openshell-driver-vm/Cargo.toml +++ b/crates/openshell-driver-vm/Cargo.toml @@ -20,6 +20,7 @@ path = "src/main.rs" [dependencies] openshell-core = { path = "../openshell-core" } +openshell-vfio = { path = "../openshell-vfio" } tokio = { workspace = true } tonic = { workspace = true, features = ["transport"] } @@ -32,6 +33,8 @@ tracing = { workspace = true } tracing-subscriber = { workspace = true } miette = { workspace = true } url = { workspace = true } +serde = { workspace = true } +serde_json = { workspace = true } libc = "0.2" libloading = "0.8" tar = "0.4" diff --git a/crates/openshell-driver-vm/README.md b/crates/openshell-driver-vm/README.md index 44f326c3b3..39be02676e 100644 --- a/crates/openshell-driver-vm/README.md +++ b/crates/openshell-driver-vm/README.md @@ -36,9 +36,17 @@ mise run gateway:vm ``` First run takes a few minutes while `mise run vm:setup` stages libkrun/libkrunfw/gvproxy and `mise run vm:rootfs -- --base` builds the embedded rootfs. Subsequent runs are cached. To keep the Unix socket path under macOS `SUN_LEN`, `mise run gateway:vm` and `start.sh` default the state dir to `/tmp/openshell-vm-driver-dev-$USER-port-$PORT/` (SQLite DB + per-sandbox rootfs + `compute-driver.sock`) unless `OPENSHELL_VM_DRIVER_STATE_DIR` is set. -The wrapper also prints the recommended gateway name (`vm-driver-port-$PORT` by default) plus the exact repo-local `scripts/bin/openshell gateway add` and `scripts/bin/openshell gateway select` commands to use from another terminal. This avoids accidentally hitting an older `openshell` binary elsewhere on your `PATH`. +The wrapper auto-registers the gateway with the CLI (`gateway destroy` + `gateway add`) so no manual registration step is needed. When running under `sudo`, it uses `sudo -u $SUDO_USER` for the registration so the config is written under the invoking user's home directory. Re-runs are idempotent. It also exports `OPENSHELL_DRIVER_DIR=$PWD/target/debug` before starting the gateway so local dev runs use the freshly built `openshell-driver-vm` instead of an older installed copy from `~/.local/libexec/openshell` or `/usr/local/libexec`. +For GPU passthrough (VFIO), pass `-- --gpu` and run with root privileges: + +```shell +sudo -E env "PATH=$PATH" mise run gateway:vm -- --gpu +``` + +See [`architecture/vm-gpu-sandbox-guide.md`](../../architecture/vm-gpu-sandbox-guide.md) for full GPU prerequisites and usage. + Override via environment: ```shell @@ -129,13 +137,11 @@ See [`openshell-gateway --help`](../openshell-server/src/cli.rs) for the full fl ## Verifying the gateway -In another terminal: +The gateway is auto-registered by `start.sh`. In another terminal: ```shell -export OPENSHELL_GATEWAY_URL=http://127.0.0.1:8080 -cargo run -p openshell-cli -- gateway register local --url $OPENSHELL_GATEWAY_URL --no-tls -cargo run -p openshell-cli -- sandbox create --name demo -cargo run -p openshell-cli -- sandbox connect demo +scripts/bin/openshell sandbox create --name demo +scripts/bin/openshell sandbox connect demo ``` First sandbox takes 10–30 seconds to boot (rootfs extraction + libkrun + guest init). Subsequent creates reuse the prepared sandbox rootfs. diff --git a/crates/openshell-driver-vm/build.rs b/crates/openshell-driver-vm/build.rs index 174a90fc87..981cf8ff8a 100644 --- a/crates/openshell-driver-vm/build.rs +++ b/crates/openshell-driver-vm/build.rs @@ -22,6 +22,7 @@ fn main() { "libkrunfw.5.dylib.zst", "gvproxy.zst", "rootfs.tar.zst", + "rootfs-gpu.tar.zst", ] { println!("cargo:rerun-if-changed={dir}/{name}"); } @@ -36,7 +37,15 @@ fn main() { "linux" => ("libkrun.so", "libkrunfw.so.5"), _ => { println!("cargo:warning=VM runtime not available for {target_os}-{target_arch}"); - generate_stub_resources(&out_dir, &["libkrun", "libkrunfw", "rootfs.tar.zst"]); + generate_stub_resources( + &out_dir, + &[ + "libkrun", + "libkrunfw", + "rootfs.tar.zst", + "rootfs-gpu.tar.zst", + ], + ); return; } }; @@ -53,6 +62,7 @@ fn main() { &format!("{libkrunfw_name}.zst"), "gvproxy.zst", "rootfs.tar.zst", + "rootfs-gpu.tar.zst", ], ); return; @@ -71,6 +81,7 @@ fn main() { &format!("{libkrunfw_name}.zst"), "gvproxy.zst", "rootfs.tar.zst", + "rootfs-gpu.tar.zst", ], ); return; @@ -84,6 +95,10 @@ fn main() { ), ("gvproxy.zst".to_string(), "gvproxy.zst".to_string()), ("rootfs.tar.zst".to_string(), "rootfs.tar.zst".to_string()), + ( + "rootfs-gpu.tar.zst".to_string(), + "rootfs-gpu.tar.zst".to_string(), + ), ]; let mut all_found = true; @@ -124,12 +139,13 @@ fn main() { &format!("{libkrunfw_name}.zst"), "gvproxy.zst", "rootfs.tar.zst", + "rootfs-gpu.tar.zst", ], ); } } -fn generate_stub_resources(out_dir: &PathBuf, names: &[&str]) { +fn generate_stub_resources(out_dir: &std::path::Path, names: &[&str]) { for name in names { let path = out_dir.join(name); if !path.exists() { diff --git a/crates/openshell-driver-vm/scripts/openshell-vm-sandbox-init.sh b/crates/openshell-driver-vm/scripts/openshell-vm-sandbox-init.sh index e449003f97..1c009a7f11 100644 --- a/crates/openshell-driver-vm/scripts/openshell-vm-sandbox-init.sh +++ b/crates/openshell-driver-vm/scripts/openshell-vm-sandbox-init.sh @@ -3,13 +3,35 @@ # SPDX-License-Identifier: Apache-2.0 # Minimal init for sandbox VMs. Runs as PID 1 inside the guest, mounts the -# essential filesystems, configures gvproxy networking when present, then -# execs the OpenShell sandbox supervisor. +# essential filesystems, configures networking (gvproxy DHCP or TAP static), +# optionally loads NVIDIA GPU drivers, then execs the OpenShell sandbox +# supervisor. set -euo pipefail +# Source QEMU-injected environment variables if present +if [ -f /srv/openshell-env.sh ]; then + source /srv/openshell-env.sh +fi + BOOT_START=$(date +%s%3N 2>/dev/null || date +%s) GVPROXY_GATEWAY_IP="192.168.127.1" +GATEWAY_IP="$GVPROXY_GATEWAY_IP" + +# Parse kernel cmdline for GPU and TAP networking parameters +GPU_ENABLED="${GPU_ENABLED:-false}" +VM_NET_IP="${VM_NET_IP:-}" +VM_NET_GW="${VM_NET_GW:-}" +VM_NET_DNS="${VM_NET_DNS:-}" + +for param in $(cat /proc/cmdline 2>/dev/null || true); do + case "$param" in + GPU_ENABLED=*) GPU_ENABLED="${param#GPU_ENABLED=}" ;; + VM_NET_IP=*) VM_NET_IP="${param#VM_NET_IP=}" ;; + VM_NET_GW=*) VM_NET_GW="${param#VM_NET_GW=}" ;; + VM_NET_DNS=*) VM_NET_DNS="${param#VM_NET_DNS=}" ;; + esac +done ts() { local now @@ -82,7 +104,7 @@ ensure_host_gateway_aliases() { : > "$hosts_tmp" fi - printf '%s host.openshell.internal\n' "$GVPROXY_GATEWAY_IP" >> "$hosts_tmp" + printf '%s host.openshell.internal\n' "$GATEWAY_IP" >> "$hosts_tmp" cat "$hosts_tmp" > /etc/hosts rm -f "$hosts_tmp" } @@ -107,7 +129,7 @@ rewrite_openshell_endpoint_if_needed() { return 0 fi - for candidate in host.openshell.internal host.containers.internal host.docker.internal "$GVPROXY_GATEWAY_IP"; do + for candidate in host.openshell.internal host.containers.internal host.docker.internal "$GATEWAY_IP"; do if [ "$candidate" = "$host" ]; then continue fi @@ -126,6 +148,95 @@ rewrite_openshell_endpoint_if_needed() { ts "WARNING: could not reach OpenShell endpoint ${host}:${port}" } +create_gpu_device_nodes_mknod() { + # Mode 666 is intentional: single-tenant microVM with the VM itself as the + # isolation boundary. The sandbox user is the only non-root user. + local nv_major + nv_major=$(awk '$2 == "nvidia" {print $1}' /proc/devices 2>/dev/null || true) + if [ -n "$nv_major" ]; then + mknod -m 666 /dev/nvidiactl c "$nv_major" 255 2>/dev/null || true + + local gpu_count=0 + if [ -d /proc/driver/nvidia/gpus ]; then + for gpu_dir in /proc/driver/nvidia/gpus/*/; do + [ -d "$gpu_dir" ] || continue + mknod -m 666 "/dev/nvidia${gpu_count}" c "$nv_major" "$gpu_count" 2>/dev/null || true + gpu_count=$((gpu_count + 1)) + done + fi + if [ "$gpu_count" -eq 0 ]; then + mknod -m 666 /dev/nvidia0 c "$nv_major" 0 2>/dev/null || true + fi + + local modeset_major + modeset_major=$(awk '$2 == "nvidia-modeset" {print $1}' /proc/devices 2>/dev/null || true) + if [ -n "$modeset_major" ]; then + mknod -m 666 /dev/nvidia-modeset c "$modeset_major" 254 2>/dev/null || true + fi + + local uvm_major + uvm_major=$(awk '$2 == "nvidia-uvm" {print $1}' /proc/devices 2>/dev/null || true) + if [ -n "$uvm_major" ]; then + mknod -m 666 /dev/nvidia-uvm c "$uvm_major" 0 2>/dev/null || true + mknod -m 666 /dev/nvidia-uvm-tools c "$uvm_major" 1 2>/dev/null || true + fi + + local caps_major + caps_major=$(awk '$2 == "nvidia-caps" {print $1}' /proc/devices 2>/dev/null || true) + if [ -n "$caps_major" ]; then + mkdir -p /dev/nvidia-caps 2>/dev/null || true + mknod -m 666 /dev/nvidia-caps/nvidia-cap1 c "$caps_major" 1 2>/dev/null || true + mknod -m 666 /dev/nvidia-caps/nvidia-cap2 c "$caps_major" 2 2>/dev/null || true + fi + + ts "GPU device nodes created via mknod (${gpu_count} GPU(s), major=${nv_major})" + else + ts "WARNING: 'nvidia' not in /proc/devices; device nodes unavailable" + fi +} + +setup_gpu() { + ts "GPU_ENABLED=true — initializing GPU passthrough" + + if ! command -v modprobe >/dev/null 2>&1; then + ts "FATAL: modprobe not found; cannot load nvidia kernel modules" + return 1 + fi + + # Stage GSP firmware from virtiofs to tmpfs to avoid slow FUSE reads + if [ -d /lib/firmware/nvidia ]; then + ts "staging GPU firmware to tmpfs" + mkdir -p /run/firmware/nvidia + cp -a /lib/firmware/nvidia/* /run/firmware/nvidia/ 2>/dev/null || true + if [ -e /sys/module/firmware_class/parameters/path ]; then + echo /run/firmware > /sys/module/firmware_class/parameters/path + fi + fi + + ts "loading nvidia kernel modules" + modprobe nvidia || { ts "FATAL: modprobe nvidia failed"; return 1; } + modprobe nvidia_uvm 2>/dev/null || true + modprobe nvidia_modeset 2>/dev/null || true + + rm -rf /run/firmware 2>/dev/null || true + + if command -v nvidia-smi >/dev/null 2>&1; then + ts "running nvidia-smi to create device nodes and validate GPU" + local smi_rc=0 + nvidia-smi >/dev/null 2>&1 || smi_rc=$? + if [ "$smi_rc" -eq 0 ]; then + nvidia-smi -L 2>/dev/null | while read -r line; do ts " $line"; done + ts "GPU initialization successful" + else + ts "WARNING: nvidia-smi failed (exit ${smi_rc}); falling back to mknod" + create_gpu_device_nodes_mknod + fi + else + ts "nvidia-smi not found; creating device nodes via mknod" + create_gpu_device_nodes_mknod + fi +} + mount -t proc proc /proc 2>/dev/null & mount -t sysfs sysfs /sys 2>/dev/null & mount -t tmpfs tmpfs /tmp 2>/dev/null & @@ -146,7 +257,50 @@ chown sandbox:sandbox /sandbox 2>/dev/null || true hostname openshell-sandbox-vm 2>/dev/null || true ip link set lo up 2>/dev/null || true -if ip link show eth0 >/dev/null 2>&1; then +# GPU initialization (before networking so nvidia-smi output is visible early) +if [ "${GPU_ENABLED}" = "true" ]; then + setup_gpu || ts "WARNING: GPU init failed; continuing without GPU" +fi + +# Networking: use TAP static config if VM_NET_IP is set (QEMU path), +# otherwise fall back to gvproxy DHCP on eth0 (libkrun path). +if [ -n "${VM_NET_IP}" ] && [ -n "${VM_NET_GW}" ]; then + ts "configuring TAP networking (static ${VM_NET_IP} gw ${VM_NET_GW})" + GATEWAY_IP="${VM_NET_GW}" + + TAP_NIC="" + NIC_WAIT=0 + while [ -z "$TAP_NIC" ] && [ "$NIC_WAIT" -lt 10 ]; do + for candidate in eth0 ens3 enp0s2 $(ls /sys/class/net/ 2>/dev/null | grep -v '^lo$'); do + if ip link show "$candidate" >/dev/null 2>&1 && [ "$candidate" != "lo" ]; then + TAP_NIC="$candidate" + break + fi + done + if [ -z "$TAP_NIC" ]; then + sleep 1 + NIC_WAIT=$((NIC_WAIT + 1)) + fi + done + + if [ -n "$TAP_NIC" ]; then + ts "using NIC ${TAP_NIC} for TAP networking" + ip link set "$TAP_NIC" up 2>/dev/null || true + ip addr add "${VM_NET_IP}/30" dev "$TAP_NIC" 2>/dev/null || true + ip route add default via "${VM_NET_GW}" 2>/dev/null || true + else + ts "WARNING: no network interface found for TAP networking" + fi + + if [ -n "${VM_NET_DNS}" ]; then + echo "nameserver ${VM_NET_DNS}" > /etc/resolv.conf + elif [ ! -s /etc/resolv.conf ]; then + echo "nameserver 8.8.8.8" > /etc/resolv.conf + echo "nameserver 8.8.4.4" >> /etc/resolv.conf + fi + + ensure_host_gateway_aliases +elif ip link show eth0 >/dev/null 2>&1; then ts "detected eth0 (gvproxy networking)" ip link set eth0 up 2>/dev/null || true @@ -193,7 +347,7 @@ DHCP_SCRIPT ensure_host_gateway_aliases else - ts "WARNING: eth0 not found; supervisor will start without guest egress" + ts "WARNING: no network interface found; supervisor will start without guest egress" fi export HOME=/sandbox @@ -201,5 +355,23 @@ export USER=sandbox rewrite_openshell_endpoint_if_needed +# Log supervisor connectivity state for debugging stuck-in-Provisioning issues +if [ -n "${OPENSHELL_ENDPOINT:-}" ]; then + _ep_parsed="$(parse_endpoint "$OPENSHELL_ENDPOINT" 2>/dev/null || true)" + if [ -n "$_ep_parsed" ]; then + _ep_host="$(printf '%s\n' "$_ep_parsed" | sed -n '2p')" + _ep_port="$(printf '%s\n' "$_ep_parsed" | sed -n '3p')" + if tcp_probe "$_ep_host" "$_ep_port"; then + ts "gateway reachable at ${_ep_host}:${_ep_port}" + else + ts "WARNING: gateway NOT reachable at ${_ep_host}:${_ep_port} — supervisor may fail to connect" + fi + fi + ts "OPENSHELL_ENDPOINT=${OPENSHELL_ENDPOINT}" +fi +if [ -n "${OPENSHELL_SANDBOX_ID:-}" ]; then + ts "OPENSHELL_SANDBOX_ID=${OPENSHELL_SANDBOX_ID}" +fi + ts "starting openshell-sandbox supervisor" exec /opt/openshell/bin/openshell-sandbox --workdir /sandbox diff --git a/crates/openshell-driver-vm/src/driver.rs b/crates/openshell-driver-vm/src/driver.rs index d649a585a4..e5520b5a18 100644 --- a/crates/openshell-driver-vm/src/driver.rs +++ b/crates/openshell-driver-vm/src/driver.rs @@ -1,7 +1,12 @@ // SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 -use crate::rootfs::{extract_sandbox_rootfs_to, sandbox_guest_init_path}; +use crate::gpu::{ + GpuInventory, SubnetAllocator, allocate_vsock_cid, mac_from_sandbox_id, tap_device_name, +}; +use crate::rootfs::{ + extract_gpu_sandbox_rootfs_to, extract_sandbox_rootfs_to, sandbox_guest_init_path, +}; use futures::Stream; use nix::errno::Errno; use nix::sys::signal::{Signal, kill}; @@ -16,7 +21,9 @@ use openshell_core::proto::compute::v1::{ WatchSandboxesPlatformEvent, WatchSandboxesRequest, WatchSandboxesSandboxEvent, compute_driver_server::ComputeDriver, watch_sandboxes_event, }; +use openshell_vfio::SysfsRoot; use std::collections::{HashMap, HashSet}; +use std::net::Ipv4Addr; use std::os::unix::fs::PermissionsExt; use std::path::{Path, PathBuf}; use std::pin::Pin; @@ -62,6 +69,9 @@ pub struct VmDriverConfig { pub guest_tls_ca: Option, pub guest_tls_cert: Option, pub guest_tls_key: Option, + pub gpu_enabled: bool, + pub gpu_mem_mib: u32, + pub gpu_vcpus: u8, } impl Default for VmDriverConfig { @@ -79,6 +89,9 @@ impl Default for VmDriverConfig { guest_tls_ca: None, guest_tls_cert: None, guest_tls_key: None, + gpu_enabled: false, + gpu_mem_mib: 8192, + gpu_vcpus: 4, } } } @@ -167,14 +180,17 @@ struct SandboxRecord { snapshot: Sandbox, state_dir: PathBuf, process: Arc>, + gpu_bdf: Option, } -#[derive(Debug, Clone)] +#[derive(Clone)] pub struct VmDriver { config: VmDriverConfig, launcher_bin: PathBuf, registry: Arc>>, events: broadcast::Sender, + gpu_inventory: Option>>, + subnet_allocator: Arc>, } impl VmDriver { @@ -185,6 +201,14 @@ impl VmDriver { validate_openshell_endpoint(&config.openshell_endpoint)?; let _ = config.tls_paths()?; + #[cfg(target_os = "linux")] + if config.gpu_enabled { + check_gpu_privileges()?; + tokio::task::spawn_blocking(crate::cleanup_stale_tap_interfaces) + .await + .map_err(|e| format!("cleanup stale TAP interfaces panicked: {e}"))?; + } + let state_root = config.state_dir.join("sandboxes"); tokio::fs::create_dir_all(&state_root) .await @@ -202,36 +226,65 @@ impl VmDriver { .map_err(|err| format!("failed to resolve vm driver executable: {err}"))? }; + let gpu_inventory = if config.gpu_enabled { + let sysfs = SysfsRoot::system(); + let inventory = GpuInventory::new(sysfs, &config.state_dir); + tracing::info!( + gpu_count = inventory.gpu_count(), + "GPU inventory initialized" + ); + Some(Arc::new(std::sync::Mutex::new(inventory))) + } else { + None + }; + + let subnet_allocator = Arc::new(std::sync::Mutex::new(SubnetAllocator::new( + Ipv4Addr::new(10, 0, 128, 0), + 17, + ))); + let (events, _) = broadcast::channel(WATCH_BUFFER); Ok(Self { config, launcher_bin, registry: Arc::new(Mutex::new(HashMap::new())), events, + gpu_inventory, + subnet_allocator, }) } #[must_use] pub fn capabilities(&self) -> GetCapabilitiesResponse { + let gpu_count = self + .gpu_inventory + .as_ref() + .and_then(|inv| inv.lock().ok()) + .map_or(0, |inv| inv.gpu_count()); GetCapabilitiesResponse { driver_name: DRIVER_NAME.to_string(), driver_version: openshell_core::VERSION.to_string(), default_image: String::new(), - supports_gpu: false, + supports_gpu: self.gpu_inventory.is_some(), + gpu_count, } } pub async fn validate_sandbox(&self, sandbox: &Sandbox) -> Result<(), Status> { - validate_vm_sandbox(sandbox) + validate_vm_sandbox(sandbox, self.config.gpu_enabled) } pub async fn create_sandbox(&self, sandbox: &Sandbox) -> Result { - validate_vm_sandbox(sandbox)?; + validate_vm_sandbox(sandbox, self.config.gpu_enabled)?; if self.registry.lock().await.contains_key(&sandbox.id) { return Err(Status::already_exists("sandbox already exists")); } + let spec = sandbox.spec.as_ref(); + let is_gpu = spec.is_some_and(|s| s.gpu); + let gpu_device = spec.map_or("", |s| s.gpu_device.as_str()); + let state_dir = sandbox_state_dir(&self.config.state_dir, &sandbox.id); let rootfs = state_dir.join("rootfs"); @@ -244,7 +297,12 @@ impl VmDriver { .tls_paths() .map_err(Status::failed_precondition)?; let rootfs_for_extract = rootfs.clone(); - tokio::task::spawn_blocking(move || extract_sandbox_rootfs_to(&rootfs_for_extract)) + let extract_fn = if is_gpu { + extract_gpu_sandbox_rootfs_to + } else { + extract_sandbox_rootfs_to + }; + tokio::task::spawn_blocking(move || extract_fn(&rootfs_for_extract)) .await .map_err(|err| Status::internal(format!("sandbox rootfs extraction panicked: {err}")))? .map_err(|err| Status::internal(format!("extract sandbox rootfs failed: {err}")))?; @@ -256,21 +314,39 @@ impl VmDriver { })?; } + let gpu_bdf = if is_gpu { + let inventory = self + .gpu_inventory + .as_ref() + .ok_or_else(|| Status::internal("GPU inventory not initialized"))?; + match inventory + .lock() + .map_err(|e| Status::internal(format!("GPU inventory lock poisoned: {e}"))) + .and_then(|mut inv| { + inv.assign(&sandbox.id, gpu_device) + .map_err(|e| Status::failed_precondition(e)) + }) { + Ok(assignment) => { + tracing::info!( + sandbox_id = %sandbox.id, + bdf = %assignment.bdf, + gpu_name = %assignment.name, + iommu_group = assignment.iommu_group, + "assigned GPU to sandbox" + ); + Some(assignment.bdf) + } + Err(err) => { + let _ = tokio::fs::remove_dir_all(&state_dir).await; + return Err(err); + } + } + } else { + None + }; + let console_output = state_dir.join("rootfs-console.log"); let mut command = Command::new(&self.launcher_bin); - // Intentionally DO NOT set kill_on_drop(true). On a signal-driven - // driver exit (SIGKILL, SIGTERM without a handler, panic), - // tokio's Drop is racy with the launcher's procguard-initiated - // cleanup: if kill_on_drop SIGKILLs the launcher first, its - // cleanup callback never gets to SIGTERM gvproxy, and gvproxy is - // reparented to init as an orphan. Instead the whole cleanup - // cascade runs via procguard: - // driver exits → launcher's kqueue (macOS) or PR_SET_PDEATHSIG - // (Linux) fires → launcher kills gvproxy + libkrun fork → - // launcher exits → its own children die under pdeathsig. - // The explicit Drop path in VmProcess::terminate_vm_process still - // handles voluntary `delete_sandbox` teardown cleanly, where we - // do want SIGTERM + wait + SIGKILL semantics. command.stdin(Stdio::null()); command.stdout(Stdio::inherit()); command.stderr(Stdio::inherit()); @@ -278,21 +354,83 @@ impl VmDriver { command.arg("--vm-rootfs").arg(&rootfs); command.arg("--vm-exec").arg(sandbox_guest_init_path()); command.arg("--vm-workdir").arg("/"); - command.arg("--vm-vcpus").arg(self.config.vcpus.to_string()); - command - .arg("--vm-mem-mib") - .arg(self.config.mem_mib.to_string()); + command.arg("--vm-console-output").arg(&console_output); + + // Compute the endpoint override before building the env so + // there is a single OPENSHELL_ENDPOINT value in the env list. + let endpoint_override = if gpu_bdf.is_some() { + let subnet = match self + .subnet_allocator + .lock() + .map_err(|e| Status::internal(format!("subnet allocator lock poisoned: {e}"))) + .and_then(|mut alloc| { + alloc + .allocate(&sandbox.id) + .map_err(|e| Status::failed_precondition(e)) + }) { + Ok(s) => s, + Err(err) => { + self.release_gpu_and_subnet(&sandbox.id); + let _ = tokio::fs::remove_dir_all(&state_dir).await; + return Err(err); + } + }; + let vsock_cid = allocate_vsock_cid(); + let mac = mac_from_sandbox_id(&sandbox.id); + let mac_str = format!( + "{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}", + mac[0], mac[1], mac[2], mac[3], mac[4], mac[5] + ); + let tap = tap_device_name(&sandbox.id); + + let tap_endpoint = guest_visible_openshell_endpoint_for_tap( + &self.config.openshell_endpoint, + &subnet.host_ip.to_string(), + ); + + command.arg("--vm-backend").arg("qemu"); + command + .arg("--vm-vcpus") + .arg(self.config.gpu_vcpus.to_string()); + command + .arg("--vm-mem-mib") + .arg(self.config.gpu_mem_mib.to_string()); + command.arg("--vm-gpu-bdf").arg(gpu_bdf.as_ref().unwrap()); + command.arg("--vm-tap-device").arg(&tap); + command + .arg("--vm-guest-ip") + .arg(subnet.guest_ip.to_string()); + command.arg("--vm-host-ip").arg(subnet.host_ip.to_string()); + command.arg("--vm-vsock-cid").arg(vsock_cid.to_string()); + command.arg("--vm-guest-mac").arg(&mac_str); + + if let Some(port) = gateway_port_from_endpoint(&self.config.openshell_endpoint) { + command.arg("--vm-gateway-port").arg(port.to_string()); + } + + Some(tap_endpoint) + } else { + command.arg("--vm-vcpus").arg(self.config.vcpus.to_string()); + command + .arg("--vm-mem-mib") + .arg(self.config.mem_mib.to_string()); + None + }; + command .arg("--vm-krun-log-level") .arg(self.config.krun_log_level.to_string()); - command.arg("--vm-console-output").arg(&console_output); - for env in build_guest_environment(sandbox, &self.config) { + + for env in build_guest_environment(sandbox, &self.config, endpoint_override.as_deref()) { command.arg("--vm-env").arg(env); } let child = match command.spawn() { Ok(child) => child, Err(err) => { + if gpu_bdf.is_some() { + self.release_gpu_and_subnet(&sandbox.id); + } let _ = tokio::fs::remove_dir_all(&state_dir).await; return Err(Status::internal(format!( "failed to launch vm helper '{}': {err}", @@ -314,6 +452,7 @@ impl VmDriver { snapshot: snapshot.clone(), state_dir: state_dir.clone(), process: process.clone(), + gpu_bdf: gpu_bdf.clone(), }, ); } @@ -338,21 +477,31 @@ impl VmDriver { let record = { let registry = self.registry.lock().await; if let Some((id, record)) = registry.get_key_value(sandbox_id) { - Some((id.clone(), record.state_dir.clone(), record.process.clone())) + Some(( + id.clone(), + record.state_dir.clone(), + record.process.clone(), + record.gpu_bdf.clone(), + )) } else { let matched_id = registry .iter() .find(|(_, record)| record.snapshot.name == sandbox_name) .map(|(id, _)| id.clone()); matched_id.and_then(|id| { - registry - .get(&id) - .map(|record| (id, record.state_dir.clone(), record.process.clone())) + registry.get(&id).map(|record| { + ( + id, + record.state_dir.clone(), + record.process.clone(), + record.gpu_bdf.clone(), + ) + }) }) } }; - let Some((record_id, state_dir, process)) = record else { + let Some((record_id, state_dir, process, gpu_bdf)) = record else { return Ok(DeleteSandboxResponse { deleted: false }); }; @@ -371,6 +520,10 @@ impl VmDriver { .map_err(|err| Status::internal(format!("failed to stop vm: {err}")))?; } + if gpu_bdf.is_some() { + self.release_gpu_and_subnet(&record_id); + } + if let Err(err) = tokio::fs::remove_dir_all(&state_dir).await && err.kind() != std::io::ErrorKind::NotFound { @@ -417,6 +570,17 @@ impl VmDriver { snapshots } + fn release_gpu_and_subnet(&self, sandbox_id: &str) { + if let Some(ref inventory) = self.gpu_inventory { + if let Ok(mut inv) = inventory.lock() { + inv.release(sandbox_id); + } + } + if let Ok(mut alloc) = self.subnet_allocator.lock() { + alloc.release(sandbox_id); + } + } + /// Watch the launcher child process and surface errors as driver /// conditions. /// @@ -487,6 +651,16 @@ impl VmDriver { sandbox_id.clone(), platform_event("vm", "Warning", "ProcessExited", message), ); + let has_gpu = { + let registry = self.registry.lock().await; + registry + .get(&sandbox_id) + .and_then(|r| r.gpu_bdf.as_ref()) + .is_some() + }; + if has_gpu { + self.release_gpu_and_subnet(&sandbox_id); + } return; } @@ -678,16 +852,35 @@ impl ComputeDriver for VmDriver { } } -fn validate_vm_sandbox(sandbox: &Sandbox) -> Result<(), Status> { +#[cfg(target_os = "linux")] +#[allow(unsafe_code)] +fn check_gpu_privileges() -> Result<(), String> { + if unsafe { libc::geteuid() } != 0 { + return Err( + "GPU support requires root privileges for VFIO bind/unbind and TAP networking. \ + Run with sudo or ensure CAP_SYS_ADMIN + CAP_NET_ADMIN capabilities are set." + .to_string(), + ); + } + Ok(()) +} + +fn validate_vm_sandbox(sandbox: &Sandbox, gpu_enabled: bool) -> Result<(), Status> { let spec = sandbox .spec .as_ref() .ok_or_else(|| Status::invalid_argument("sandbox spec is required"))?; - if spec.gpu { + + if spec.gpu && !gpu_enabled { return Err(Status::failed_precondition( - "vm sandboxes do not support gpu=true", + "GPU support is not enabled on this driver; start with --gpu", )); } + + if !spec.gpu && !spec.gpu_device.is_empty() { + return Err(Status::invalid_argument("gpu_device requires gpu=true")); + } + if let Some(template) = spec.template.as_ref() { if !template.image.is_empty() { return Err(Status::failed_precondition( @@ -744,7 +937,29 @@ fn guest_visible_openshell_endpoint(endpoint: &str) -> String { endpoint.to_string() } -fn build_guest_environment(sandbox: &Sandbox, config: &VmDriverConfig) -> Vec { +fn gateway_port_from_endpoint(endpoint: &str) -> Option { + Url::parse(endpoint).ok().and_then(|url| url.port()) +} + +fn guest_visible_openshell_endpoint_for_tap(endpoint: &str, host_ip: &str) -> String { + let Ok(mut url) = Url::parse(endpoint) else { + return endpoint.to_string(); + }; + if url.set_host(Some(host_ip)).is_ok() { + url.to_string() + } else { + endpoint.to_string() + } +} + +fn build_guest_environment( + sandbox: &Sandbox, + config: &VmDriverConfig, + endpoint_override: Option<&str>, +) -> Vec { + let openshell_endpoint = endpoint_override + .map(String::from) + .unwrap_or_else(|| guest_visible_openshell_endpoint(&config.openshell_endpoint)); let mut environment = HashMap::from([ ("HOME".to_string(), "/root".to_string()), ( @@ -752,10 +967,7 @@ fn build_guest_environment(sandbox: &Sandbox, config: &VmDriverConfig) -> Vec Vec i64 { #[cfg(test)] mod tests { use super::*; + use crate::gpu::{SubnetAllocator, allocate_vsock_cid, mac_from_sandbox_id, tap_device_name}; use openshell_core::proto::compute::v1::{ DriverSandboxSpec as SandboxSpec, DriverSandboxTemplate as SandboxTemplate, }; @@ -945,7 +1162,7 @@ mod tests { use tonic::Code; #[test] - fn validate_vm_sandbox_rejects_gpu() { + fn validate_vm_sandbox_rejects_gpu_when_not_enabled() { let sandbox = Sandbox { spec: Some(SandboxSpec { gpu: true, @@ -953,9 +1170,38 @@ mod tests { }), ..Default::default() }; - let err = validate_vm_sandbox(&sandbox).expect_err("gpu should be rejected"); + let err = validate_vm_sandbox(&sandbox, false) + .expect_err("gpu should be rejected when not enabled"); assert_eq!(err.code(), Code::FailedPrecondition); - assert!(err.message().contains("gpu")); + assert!(err.message().contains("GPU support is not enabled")); + } + + #[test] + fn validate_vm_sandbox_accepts_gpu_when_enabled() { + let sandbox = Sandbox { + spec: Some(SandboxSpec { + gpu: true, + ..Default::default() + }), + ..Default::default() + }; + validate_vm_sandbox(&sandbox, true).expect("gpu should be accepted when enabled"); + } + + #[test] + fn validate_vm_sandbox_rejects_gpu_device_without_gpu() { + let sandbox = Sandbox { + spec: Some(SandboxSpec { + gpu: false, + gpu_device: "0000:2d:00.0".to_string(), + ..Default::default() + }), + ..Default::default() + }; + let err = validate_vm_sandbox(&sandbox, true) + .expect_err("gpu_device without gpu should be rejected"); + assert_eq!(err.code(), Code::InvalidArgument); + assert!(err.message().contains("gpu_device requires gpu=true")); } #[test] @@ -979,7 +1225,8 @@ mod tests { }), ..Default::default() }; - let err = validate_vm_sandbox(&sandbox).expect_err("platform config should be rejected"); + let err = + validate_vm_sandbox(&sandbox, false).expect_err("platform config should be rejected"); assert_eq!(err.code(), Code::FailedPrecondition); assert!(err.message().contains("platform_config")); } @@ -1019,7 +1266,7 @@ mod tests { ..Default::default() }; - let env = build_guest_environment(&sandbox, &config); + let env = build_guest_environment(&sandbox, &config, None); assert!(env.contains(&"HOME=/root".to_string())); assert!(env.contains(&format!( "OPENSHELL_ENDPOINT=http://{GVPROXY_GATEWAY_IP}:8080/" @@ -1028,6 +1275,39 @@ mod tests { assert!(env.contains(&format!( "OPENSHELL_SSH_SOCKET_PATH={GUEST_SSH_SOCKET_PATH}" ))); + assert!( + env.contains(&"OPENSHELL_SSH_HANDSHAKE_SECRET=secret".to_string()), + "SSH handshake secret must be passed to the guest" + ); + } + + #[test] + fn build_guest_environment_uses_endpoint_override_for_tap() { + let config = VmDriverConfig { + openshell_endpoint: "http://127.0.0.1:8080".to_string(), + ssh_handshake_secret: "secret".to_string(), + ..Default::default() + }; + let sandbox = Sandbox { + id: "sandbox-123".to_string(), + name: "sandbox-123".to_string(), + spec: Some(SandboxSpec::default()), + ..Default::default() + }; + + let env = build_guest_environment(&sandbox, &config, Some("http://10.0.128.1:8080")); + assert!( + env.contains(&"OPENSHELL_ENDPOINT=http://10.0.128.1:8080".to_string()), + "TAP endpoint override must replace the default" + ); + let endpoint_count = env + .iter() + .filter(|e| e.starts_with("OPENSHELL_ENDPOINT=")) + .count(); + assert_eq!( + endpoint_count, 1, + "must have exactly one OPENSHELL_ENDPOINT" + ); } #[test] @@ -1085,7 +1365,7 @@ mod tests { ..Default::default() }; - let env = build_guest_environment(&sandbox, &config); + let env = build_guest_environment(&sandbox, &config, None); assert!(env.contains(&format!("OPENSHELL_TLS_CA={GUEST_TLS_CA_PATH}"))); assert!(env.contains(&format!("OPENSHELL_TLS_CERT={GUEST_TLS_CERT_PATH}"))); assert!(env.contains(&format!("OPENSHELL_TLS_KEY={GUEST_TLS_KEY_PATH}"))); @@ -1111,6 +1391,11 @@ mod tests { launcher_bin: PathBuf::from("openshell-driver-vm"), registry: Arc::new(Mutex::new(HashMap::new())), events, + gpu_inventory: None, + subnet_allocator: Arc::new(std::sync::Mutex::new(SubnetAllocator::new( + Ipv4Addr::new(10, 0, 128, 0), + 17, + ))), }; let base = unique_temp_dir(); @@ -1233,6 +1518,43 @@ mod tests { let _ = std::fs::remove_dir_all(base); } + #[test] + fn subnet_allocator_assigns_and_releases() { + let mut alloc = SubnetAllocator::new(Ipv4Addr::new(10, 0, 128, 0), 17); + let s1 = alloc.allocate("sandbox-1").unwrap(); + assert_eq!(s1.host_ip, Ipv4Addr::new(10, 0, 128, 1)); + assert_eq!(s1.guest_ip, Ipv4Addr::new(10, 0, 128, 2)); + assert_eq!(s1.prefix_len, 30); + + let s2 = alloc.allocate("sandbox-2").unwrap(); + assert_ne!(s1.host_ip, s2.host_ip); + + alloc.release("sandbox-1"); + let s3 = alloc.allocate("sandbox-3").unwrap(); + assert!(s3.host_ip != s2.host_ip); + } + + #[test] + fn tap_device_name_fits_ifnamsiz() { + let name = tap_device_name("sandbox-abc-def-ghi"); + assert!(name.len() <= 15); + assert!(name.starts_with("vmtap-")); + } + + #[test] + fn mac_address_is_locally_administered() { + let mac = mac_from_sandbox_id("test-sandbox"); + assert_eq!(mac[0] & 0x02, 0x02); + assert_eq!(mac[0] & 0x01, 0x00); + } + + #[test] + fn vsock_cid_monotonically_increases() { + let cid1 = allocate_vsock_cid(); + let cid2 = allocate_vsock_cid(); + assert!(cid2 > cid1); + } + fn unique_temp_dir() -> PathBuf { static COUNTER: AtomicU64 = AtomicU64::new(0); let nanos = SystemTime::now() @@ -1280,6 +1602,7 @@ mod tests { snapshot: sandbox, state_dir, process, + gpu_bdf: None, }, ); } diff --git a/crates/openshell-driver-vm/src/gpu.rs b/crates/openshell-driver-vm/src/gpu.rs new file mode 100644 index 0000000000..9089a166b8 --- /dev/null +++ b/crates/openshell-driver-vm/src/gpu.rs @@ -0,0 +1,316 @@ +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +use openshell_vfio::{ + GpuBindGuard, GpuBindState, GpuBinding, GpuInfo, SysfsRoot, prepare_gpu_for_passthrough, + probe_host_nvidia_vfio_readiness, reconcile_stale_bindings, validate_bdf, +}; +use std::collections::HashMap; +use std::net::Ipv4Addr; +use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicU32, Ordering}; + +/// Tracks available GPUs and their assignment to sandboxes. +pub struct GpuInventory { + slots: Vec, + sysfs: SysfsRoot, + state_path: PathBuf, +} + +struct GpuSlot { + info: GpuInfo, + assigned_to: Option, + bind_guard: Option, +} + +impl GpuInventory { + pub fn new(sysfs: SysfsRoot, state_dir: &Path) -> Self { + let state_path = state_dir.join("gpu-bindings.json"); + + let restored = reconcile_stale_bindings(&sysfs, &state_path); + for bdf in &restored { + tracing::info!(bdf = %bdf, "restored stale GPU binding from previous crash"); + } + + let gpus = probe_host_nvidia_vfio_readiness(&sysfs); + let slots = gpus + .into_iter() + .map(|info| GpuSlot { + info, + assigned_to: None, + bind_guard: None, + }) + .collect(); + + Self { + slots, + sysfs, + state_path, + } + } + + pub fn gpu_count(&self) -> u32 { + self.slots.len() as u32 + } + + pub fn available_count(&self) -> u32 { + self.slots + .iter() + .filter(|s| s.assigned_to.is_none()) + .count() as u32 + } + + /// Assign a GPU to a sandbox. Returns the assignment details including BDF. + pub fn assign(&mut self, sandbox_id: &str, gpu_device: &str) -> Result { + let slot_idx = if gpu_device.is_empty() { + self.slots + .iter() + .position(|s| s.assigned_to.is_none()) + .ok_or_else(|| "all GPUs are currently assigned to other sandboxes".to_string())? + } else if let Ok(idx) = gpu_device.parse::() { + if idx >= self.slots.len() { + return Err(format!( + "GPU index {idx} out of range (have {} GPUs)", + self.slots.len() + )); + } + if self.slots[idx].assigned_to.is_some() { + return Err(format!( + "GPU at index {idx} ({}) is already assigned to another sandbox", + self.slots[idx].info.bdf + )); + } + idx + } else { + validate_bdf(gpu_device).map_err(|e| e.to_string())?; + let idx = self + .slots + .iter() + .position(|s| s.info.bdf == gpu_device) + .ok_or_else(|| format!("GPU {gpu_device} not found in inventory"))?; + if self.slots[idx].assigned_to.is_some() { + return Err(format!( + "GPU {gpu_device} is already assigned to another sandbox" + )); + } + idx + }; + + let bdf = self.slots[slot_idx].info.bdf.clone(); + let guard = prepare_gpu_for_passthrough(&self.sysfs, &bdf) + .map_err(|e| format!("failed to prepare GPU {bdf} for passthrough: {e}"))?; + + self.slots[slot_idx].assigned_to = Some(sandbox_id.to_string()); + self.slots[slot_idx].bind_guard = Some(guard); + self.persist_state(); + + Ok(GpuAssignment { + bdf, + name: self.slots[slot_idx].info.name.clone(), + iommu_group: self.slots[slot_idx].info.iommu_group, + }) + } + + /// Release a GPU assignment. The `GpuBindGuard` is dropped, restoring the GPU. + pub fn release(&mut self, sandbox_id: &str) { + if let Some(slot) = self + .slots + .iter_mut() + .find(|s| s.assigned_to.as_deref() == Some(sandbox_id)) + { + let bdf = slot.info.bdf.clone(); + slot.assigned_to = None; + slot.bind_guard.take(); + self.persist_state(); + tracing::info!(bdf = %bdf, sandbox_id = %sandbox_id, "released GPU assignment"); + } + } + + fn persist_state(&self) { + let bindings: Vec = self + .slots + .iter() + .filter_map(|s| { + s.assigned_to.as_ref().map(|id| GpuBinding { + bdf: s.info.bdf.clone(), + sandbox_id: id.clone(), + bound_at_ms: std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map_or(0, |d| d.as_millis() as i64), + }) + }) + .collect(); + let state = GpuBindState { bindings }; + if let Err(err) = state.save(&self.state_path) { + tracing::warn!(error = %err, "failed to persist GPU bind state"); + } + } +} + +pub struct GpuAssignment { + pub bdf: String, + pub name: String, + pub iommu_group: u32, +} + +// --------------------------------------------------------------------------- +// Subnet allocation for per-sandbox TAP networking +// --------------------------------------------------------------------------- + +/// Allocates /30 subnets from a pool for per-sandbox TAP networking. +pub struct SubnetAllocator { + base: Ipv4Addr, + prefix_len: u8, + next_offset: u32, + allocated: HashMap, +} + +pub struct SubnetAllocation { + pub host_ip: Ipv4Addr, + pub guest_ip: Ipv4Addr, + pub prefix_len: u8, + pub offset: u32, +} + +static NEXT_VSOCK_CID: AtomicU32 = AtomicU32::new(3); + +impl SubnetAllocator { + pub fn new(base: Ipv4Addr, prefix_len: u8) -> Self { + Self { + base, + prefix_len, + next_offset: 0, + allocated: HashMap::new(), + } + } + + pub fn allocate(&mut self, sandbox_id: &str) -> Result { + let pool_size = 1u32 << (32 - self.prefix_len); + let max_subnets = pool_size / 4; + + if self.allocated.len() as u32 >= max_subnets { + return Err("subnet pool exhausted".to_string()); + } + + while self + .allocated + .values() + .any(|a| a.offset == self.next_offset) + { + self.next_offset = (self.next_offset + 1) % max_subnets; + } + + let base_u32 = u32::from(self.base); + let subnet_base = base_u32 + (self.next_offset * 4); + let host_ip = Ipv4Addr::from(subnet_base + 1); + let guest_ip = Ipv4Addr::from(subnet_base + 2); + + let allocation = SubnetAllocation { + host_ip, + guest_ip, + prefix_len: 30, + offset: self.next_offset, + }; + + self.allocated.insert(sandbox_id.to_string(), allocation); + self.next_offset = (self.next_offset + 1) % max_subnets; + + let alloc = &self.allocated[sandbox_id]; + Ok(SubnetAllocation { + host_ip: alloc.host_ip, + guest_ip: alloc.guest_ip, + prefix_len: alloc.prefix_len, + offset: alloc.offset, + }) + } + + pub fn release(&mut self, sandbox_id: &str) { + self.allocated.remove(sandbox_id); + } +} + +pub fn allocate_vsock_cid() -> u32 { + NEXT_VSOCK_CID.fetch_add(1, Ordering::Relaxed) +} + +/// Generate a locally-administered MAC from sandbox ID using FNV-1a. +pub fn mac_from_sandbox_id(sandbox_id: &str) -> [u8; 6] { + let mut hash: u64 = 0xcbf29ce484222325; + for byte in sandbox_id.as_bytes() { + hash ^= u64::from(*byte); + hash = hash.wrapping_mul(0x100000001b3); + } + let bytes = hash.to_le_bytes(); + let mut mac = [bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5]]; + mac[0] = (mac[0] & 0xFE) | 0x02; + mac +} + +/// TAP device name from sandbox ID (fits `IFNAMSIZ=16`). +pub fn tap_device_name(sandbox_id: &str) -> String { + let end = sandbox_id.len().min(8); + let end = sandbox_id.floor_char_boundary(end); + let prefix = &sandbox_id[..end]; + format!("vmtap-{prefix}") +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn subnet_allocator_assigns_sequential_blocks() { + let mut alloc = SubnetAllocator::new(Ipv4Addr::new(10, 0, 128, 0), 17); + + let s1 = alloc.allocate("sandbox-1").unwrap(); + assert_eq!(s1.host_ip, Ipv4Addr::new(10, 0, 128, 1)); + assert_eq!(s1.guest_ip, Ipv4Addr::new(10, 0, 128, 2)); + assert_eq!(s1.prefix_len, 30); + + let s2 = alloc.allocate("sandbox-2").unwrap(); + assert_eq!(s2.host_ip, Ipv4Addr::new(10, 0, 128, 5)); + assert_eq!(s2.guest_ip, Ipv4Addr::new(10, 0, 128, 6)); + } + + #[test] + fn subnet_allocator_recycles_after_release() { + let mut alloc = SubnetAllocator::new(Ipv4Addr::new(10, 0, 128, 0), 17); + + let _s1 = alloc.allocate("sandbox-1").unwrap(); + let _s2 = alloc.allocate("sandbox-2").unwrap(); + alloc.release("sandbox-1"); + + let s3 = alloc.allocate("sandbox-3").unwrap(); + assert_eq!(s3.host_ip, Ipv4Addr::new(10, 0, 128, 9)); + } + + #[test] + fn tap_device_name_truncates_long_ids() { + assert_eq!(tap_device_name("abc"), "vmtap-abc"); + assert_eq!(tap_device_name("abcdefghijklmnop"), "vmtap-abcdefgh"); + } + + #[test] + fn mac_from_sandbox_id_sets_locally_administered_bit() { + let mac = mac_from_sandbox_id("sandbox-123"); + assert_eq!(mac[0] & 0x02, 0x02, "locally-administered bit must be set"); + assert_eq!(mac[0] & 0x01, 0x00, "multicast bit must be clear"); + } + + #[test] + fn mac_from_sandbox_id_deterministic() { + let mac1 = mac_from_sandbox_id("sandbox-x"); + let mac2 = mac_from_sandbox_id("sandbox-x"); + assert_eq!(mac1, mac2); + + let mac3 = mac_from_sandbox_id("sandbox-y"); + assert_ne!(mac1, mac3); + } + + #[test] + fn vsock_cid_increments() { + let cid1 = allocate_vsock_cid(); + let cid2 = allocate_vsock_cid(); + assert_eq!(cid2, cid1 + 1); + } +} diff --git a/crates/openshell-driver-vm/src/lib.rs b/crates/openshell-driver-vm/src/lib.rs index 772db47b31..194dde43c2 100644 --- a/crates/openshell-driver-vm/src/lib.rs +++ b/crates/openshell-driver-vm/src/lib.rs @@ -4,9 +4,13 @@ pub mod driver; mod embedded_runtime; mod ffi; +pub mod gpu; pub mod procguard; mod rootfs; mod runtime; pub use driver::{VmDriver, VmDriverConfig}; -pub use runtime::{VM_RUNTIME_DIR_ENV, VmLaunchConfig, configured_runtime_dir, run_vm}; +pub use runtime::{ + VM_RUNTIME_DIR_ENV, VmBackend, VmLaunchConfig, cleanup_stale_tap_interfaces, + configured_runtime_dir, run_vm, +}; diff --git a/crates/openshell-driver-vm/src/main.rs b/crates/openshell-driver-vm/src/main.rs index 5a675e78a8..94169a61fb 100644 --- a/crates/openshell-driver-vm/src/main.rs +++ b/crates/openshell-driver-vm/src/main.rs @@ -5,10 +5,9 @@ use clap::Parser; use miette::{IntoDiagnostic, Result}; use openshell_core::VERSION; use openshell_core::proto::compute::v1::compute_driver_server::ComputeDriverServer; -use openshell_driver_vm::{ - VM_RUNTIME_DIR_ENV, VmDriver, VmDriverConfig, VmLaunchConfig, configured_runtime_dir, - procguard, run_vm, -}; +#[cfg(target_os = "macos")] +use openshell_driver_vm::{VM_RUNTIME_DIR_ENV, configured_runtime_dir}; +use openshell_driver_vm::{VmBackend, VmDriver, VmDriverConfig, VmLaunchConfig, procguard, run_vm}; use std::net::SocketAddr; use std::path::PathBuf; use tokio::net::UnixListener; @@ -93,6 +92,39 @@ struct Args { #[arg(long, env = "OPENSHELL_VM_DRIVER_MEM_MIB", default_value_t = 2048)] mem_mib: u32, + + #[arg(long, env = "OPENSHELL_VM_GPU")] + gpu: bool, + + #[arg(long, env = "OPENSHELL_VM_GPU_MEM_MIB", default_value_t = 8192)] + gpu_mem_mib: u32, + + #[arg(long, env = "OPENSHELL_VM_GPU_VCPUS", default_value_t = 4)] + gpu_vcpus: u8, + + #[arg(long, hide = true)] + vm_backend: Option, + + #[arg(long, hide = true)] + vm_gpu_bdf: Option, + + #[arg(long, hide = true)] + vm_tap_device: Option, + + #[arg(long, hide = true)] + vm_guest_ip: Option, + + #[arg(long, hide = true)] + vm_host_ip: Option, + + #[arg(long, hide = true)] + vm_vsock_cid: Option, + + #[arg(long, hide = true)] + vm_guest_mac: Option, + + #[arg(long, hide = true)] + vm_gateway_port: Option, } #[tokio::main] @@ -146,6 +178,9 @@ async fn main() -> Result<()> { guest_tls_ca: args.guest_tls_ca, guest_tls_cert: args.guest_tls_cert, guest_tls_key: args.guest_tls_key, + gpu_enabled: args.gpu, + gpu_mem_mib: args.gpu_mem_mib, + gpu_vcpus: args.gpu_vcpus, }) .await .map_err(|err| miette::miette!("{err}"))?; @@ -193,6 +228,12 @@ fn build_vm_launch_config(args: &Args) -> std::result::Result VmBackend::Qemu, + Some("libkrun") | None => VmBackend::Libkrun, + Some(other) => return Err(format!("unknown VM backend: {other}")), + }; + Ok(VmLaunchConfig { rootfs, vcpus: args.vm_vcpus, @@ -203,6 +244,14 @@ fn build_vm_launch_config(args: &Args) -> std::result::Result &'static str { } pub fn extract_sandbox_rootfs_to(dest: &Path) -> Result<(), String> { - if ROOTFS.is_empty() { - return Err( - "sandbox rootfs not embedded. Build openshell-driver-vm with OPENSHELL_VM_RUNTIME_COMPRESSED_DIR set or run `mise run vm:setup` first" - .to_string(), - ); + extract_variant( + ROOTFS, + "sandbox", + "sandbox rootfs not embedded. Build openshell-driver-vm with OPENSHELL_VM_RUNTIME_COMPRESSED_DIR set or run `mise run vm:setup` first", + dest, + ) +} + +pub fn extract_gpu_sandbox_rootfs_to(dest: &Path) -> Result<(), String> { + extract_variant( + ROOTFS_GPU, + "sandbox-gpu", + "GPU sandbox rootfs not embedded. Build with `mise run vm:rootfs -- --gpu` first", + dest, + ) +} + +fn extract_variant(blob: &[u8], variant: &str, empty_msg: &str, dest: &Path) -> Result<(), String> { + if blob.is_empty() { + return Err(empty_msg.to_string()); } - let expected_marker = format!("{}:sandbox", env!("CARGO_PKG_VERSION")); + let expected_marker = format!("{}:{variant}", env!("CARGO_PKG_VERSION")); let marker_path = dest.join(ROOTFS_VARIANT_MARKER); if dest.is_dir() @@ -37,22 +53,25 @@ pub fn extract_sandbox_rootfs_to(dest: &Path) -> Result<(), String> { .map_err(|e| format!("remove old rootfs {}: {e}", dest.display()))?; } - extract_rootfs_to(dest)?; + unpack_zstd_tar(blob, variant, dest)?; prepare_sandbox_rootfs(dest)?; fs::write(marker_path, format!("{expected_marker}\n")) .map_err(|e| format!("write rootfs variant marker: {e}"))?; Ok(()) } -fn extract_rootfs_to(dest: &Path) -> Result<(), String> { +fn unpack_zstd_tar(blob: &[u8], label: &str, dest: &Path) -> Result<(), String> { fs::create_dir_all(dest).map_err(|e| format!("create rootfs dir {}: {e}", dest.display()))?; - let decoder = - zstd::Decoder::new(Cursor::new(ROOTFS)).map_err(|e| format!("decompress rootfs: {e}"))?; + let decoder = zstd::Decoder::new(Cursor::new(blob)) + .map_err(|e| format!("decompress {label} rootfs: {e}"))?; let mut archive = tar::Archive::new(decoder); - archive - .unpack(dest) - .map_err(|e| format!("extract rootfs tarball into {}: {e}", dest.display())) + archive.unpack(dest).map_err(|e| { + format!( + "extract {label} rootfs tarball into {}: {e}", + dest.display() + ) + }) } fn prepare_sandbox_rootfs(rootfs: &Path) -> Result<(), String> { diff --git a/crates/openshell-driver-vm/src/runtime.rs b/crates/openshell-driver-vm/src/runtime.rs index e20c7d4e51..62f2e314c9 100644 --- a/crates/openshell-driver-vm/src/runtime.rs +++ b/crates/openshell-driver-vm/src/runtime.rs @@ -14,15 +14,22 @@ use crate::{embedded_runtime, ffi, procguard}; pub const VM_RUNTIME_DIR_ENV: &str = "OPENSHELL_VM_RUNTIME_DIR"; -/// PID of the forked libkrun worker (the VM's PID 1). Zero when not running. +/// PID of the VM worker process (libkrun fork or QEMU). Zero when not running. /// Used by the SIGTERM/SIGINT handler to forward signals to the VM. static CHILD_PID: AtomicI32 = AtomicI32::new(0); -/// PID of the gvproxy helper process. Zero when not running. Used by the -/// SIGTERM/SIGINT handler to make sure gvproxy doesn't survive the -/// launcher on macOS (where we can't use `PR_SET_PDEATHSIG`). +/// PID of the helper process (gvproxy for libkrun, virtiofsd for QEMU). +/// Zero when not running. Used by the SIGTERM/SIGINT handler and +/// procguard cleanup callback to ensure the helper doesn't outlive the +/// launcher (especially on macOS where `PR_SET_PDEATHSIG` is absent). static GVPROXY_PID: AtomicI32 = AtomicI32::new(0); +#[derive(Debug, Clone, PartialEq)] +pub enum VmBackend { + Libkrun, + Qemu, +} + pub struct VmLaunchConfig { pub rootfs: PathBuf, pub vcpus: u8, @@ -33,9 +40,608 @@ pub struct VmLaunchConfig { pub workdir: String, pub log_level: u32, pub console_output: PathBuf, + pub backend: VmBackend, + pub gpu_bdf: Option, + pub tap_device: Option, + pub guest_ip: Option, + pub host_ip: Option, + pub vsock_cid: Option, + pub guest_mac: Option, + pub gateway_port: Option, } pub fn run_vm(config: &VmLaunchConfig) -> Result<(), String> { + match config.backend { + VmBackend::Qemu => run_qemu_vm(config), + VmBackend::Libkrun => run_libkrun_vm(config), + } +} + +fn run_qemu_vm(config: &VmLaunchConfig) -> Result<(), String> { + let gpu_bdf = config + .gpu_bdf + .as_deref() + .ok_or("gpu_bdf is required for QEMU backend")?; + let tap_device = config + .tap_device + .as_deref() + .ok_or("tap_device is required for QEMU backend")?; + let guest_mac = config + .guest_mac + .as_deref() + .ok_or("guest_mac is required for QEMU backend")?; + let vsock_cid = config + .vsock_cid + .ok_or("vsock_cid is required for QEMU backend")?; + let _guest_ip = config + .guest_ip + .as_deref() + .ok_or("guest_ip is required for QEMU backend")?; + let host_ip = config + .host_ip + .as_deref() + .ok_or("host_ip is required for QEMU backend")?; + + if !config.rootfs.is_dir() { + return Err(format!( + "rootfs directory not found: {}", + config.rootfs.display() + )); + } + + if let Err(err) = procguard::die_with_parent_cleanup(procguard_kill_children) { + return Err(format!("procguard arm failed: {err}")); + } + + #[cfg(target_os = "linux")] + check_kvm_access()?; + + write_guest_env_file(&config.rootfs, &config.env)?; + + let rootfs_str = config.rootfs.to_str().ok_or("rootfs path not UTF-8")?; + let sandbox_dir = config.rootfs.parent().unwrap_or(&config.rootfs); + let sock_prefix = tap_device.trim_start_matches("vmtap-"); + let virtiofsd_sock_dir = PathBuf::from(format!("/tmp/ovm-qemu-{sock_prefix}")); + std::fs::create_dir_all(&virtiofsd_sock_dir) + .map_err(|e| format!("create virtiofsd sock dir: {e}"))?; + let virtiofsd_sock = virtiofsd_sock_dir.join("virtiofsd.sock"); + let shm_path = format!("/dev/shm/ovm-qemu-{sock_prefix}"); + + std::fs::create_dir_all(&shm_path).map_err(|e| format!("create shm dir: {e}"))?; + + let runtime_dir = qemu_runtime_dir()?; + + let gw_port = config.gateway_port.unwrap_or(0); + setup_tap_networking(tap_device, host_ip, gw_port)?; + let mut tap_guard = TapGuard::new(tap_device.to_string(), host_ip.to_string(), gw_port); + + let virtiofsd_log = sandbox_dir.join("virtiofsd.log"); + let virtiofsd_log_file = + std::fs::File::create(&virtiofsd_log).map_err(|e| format!("create virtiofsd log: {e}"))?; + + let virtiofsd_bin = { + let runtime_virtiofsd = runtime_dir.join("virtiofsd"); + if runtime_virtiofsd.is_file() { + runtime_virtiofsd + } else { + PathBuf::from("virtiofsd") + } + }; + + let mut virtiofsd_cmd = StdCommand::new(&virtiofsd_bin); + virtiofsd_cmd + .arg("--socket-path") + .arg(&virtiofsd_sock) + .arg("--shared-dir") + .arg(rootfs_str) + .arg("--cache=auto") + .stdin(Stdio::null()) + .stdout(Stdio::null()) + .stderr(virtiofsd_log_file); + + #[cfg(target_os = "linux")] + { + use nix::sys::signal::Signal; + use std::os::unix::process::CommandExt as _; + unsafe { + virtiofsd_cmd.pre_exec(|| { + nix::sys::prctl::set_pdeathsig(Signal::SIGKILL) + .map_err(|err| std::io::Error::other(format!("pdeathsig: {err}"))) + }); + } + } + + let virtiofsd_child = virtiofsd_cmd + .spawn() + .map_err(|e| format!("failed to start virtiofsd: {e}"))?; + let virtiofsd_pid = virtiofsd_child.id() as i32; + GVPROXY_PID.store(virtiofsd_pid, Ordering::Relaxed); + let mut virtiofsd_guard = GvproxyGuard::new(virtiofsd_child); + + wait_for_path(&virtiofsd_sock, Duration::from_secs(5), "virtiofsd socket")?; + + let vmlinux = runtime_dir.join("vmlinux"); + if !vmlinux.is_file() { + return Err(format!("VM kernel not found: {}", vmlinux.display())); + } + + let kernel_cmdline = build_kernel_cmdline(config); + + let mut qemu_cmd = StdCommand::new("qemu-system-x86_64"); + qemu_cmd + .arg("-machine") + .arg("q35,accel=kvm") + .arg("-cpu") + .arg("host") + .arg("-smp") + .arg(config.vcpus.to_string()) + .arg("-m") + .arg(format!("{}M", config.mem_mib)) + .arg("-nographic") + .arg("-no-reboot") + .arg("-kernel") + .arg(&vmlinux) + .arg("-append") + .arg(&kernel_cmdline) + .arg("-chardev") + .arg(format!( + "socket,id=virtiofs,path={}", + virtiofsd_sock.display() + )) + .arg("-device") + .arg("vhost-user-fs-pci,chardev=virtiofs,tag=rootfs") + .arg("-object") + .arg(format!( + "memory-backend-memfd,id=mem,size={}M,share=on", + config.mem_mib + )) + .arg("-numa") + .arg("node,memdev=mem") + .arg("-netdev") + .arg(format!( + "tap,id=net0,ifname={tap_device},script=no,downscript=no" + )) + .arg("-device") + .arg("pcie-root-port,id=net_root,slot=3") + .arg("-device") + .arg(format!( + "virtio-net-pci-non-transitional,netdev=net0,mac={guest_mac},bus=net_root" + )) + .arg("-device") + .arg("pcie-root-port,id=vsock_root,slot=1") + .arg("-device") + .arg(format!( + "vhost-vsock-pci,guest-cid={vsock_cid},bus=vsock_root" + )) + .arg("-device") + .arg("pcie-root-port,id=gpu_root,slot=2") + .arg("-device") + .arg(format!("vfio-pci,host={gpu_bdf},bus=gpu_root")) + .arg("-serial") + .arg(format!("file:{}", config.console_output.display())); + + qemu_cmd.stdin(Stdio::null()); + qemu_cmd.stdout(Stdio::inherit()); + qemu_cmd.stderr(Stdio::inherit()); + + #[cfg(target_os = "linux")] + { + use nix::sys::signal::Signal; + use std::os::unix::process::CommandExt as _; + unsafe { + qemu_cmd.pre_exec(|| { + nix::sys::prctl::set_pdeathsig(Signal::SIGKILL) + .map_err(|err| std::io::Error::other(format!("pdeathsig: {err}"))) + }); + } + } + + let mut qemu_child = qemu_cmd + .spawn() + .map_err(|e| format!("failed to start QEMU: {e}"))?; + + let qemu_pid = qemu_child.id() as i32; + install_signal_forwarding(qemu_pid); + + let status = qemu_child + .wait() + .map_err(|e| format!("failed to wait for QEMU: {e}"))?; + + CHILD_PID.store(0, Ordering::Relaxed); + unsafe { + libc::kill(virtiofsd_pid, libc::SIGTERM); + } + virtiofsd_guard.disarm(); + GVPROXY_PID.store(0, Ordering::Relaxed); + teardown_tap_networking(tap_device, host_ip, gw_port); + tap_guard.disarm(); + let _ = std::fs::remove_dir_all(&shm_path); + let _ = std::fs::remove_dir_all(&virtiofsd_sock_dir); + + if status.success() { + Ok(()) + } else { + Err(format!("QEMU exited with status {status}")) + } +} + +/// Write environment variables into the rootfs so the guest init script +/// can source them. virtiofs shares the host rootfs directory into the guest. +fn write_guest_env_file(rootfs: &Path, env_vars: &[String]) -> Result<(), String> { + let srv_dir = rootfs.join("srv"); + std::fs::create_dir_all(&srv_dir).map_err(|e| format!("create /srv in rootfs: {e}"))?; + let env_file = srv_dir.join("openshell-env.sh"); + let mut content = String::new(); + for var in env_vars { + if let Some((key, value)) = var.split_once('=') { + content.push_str(&format!("export {key}=\"{}\"\n", shell_escape(value))); + } + } + std::fs::write(&env_file, &content).map_err(|e| format!("write guest env file: {e}"))?; + Ok(()) +} + +/// Escape a string for use inside bash double quotes. +fn shell_escape(s: &str) -> String { + s.replace('\\', "\\\\") + .replace('"', "\\\"") + .replace('$', "\\$") + .replace('`', "\\`") + .replace('\n', "\\n") + .replace('\r', "\\r") +} + +fn build_kernel_cmdline(config: &VmLaunchConfig) -> String { + let mut parts = vec![ + "console=ttyS0".to_string(), + "root=rootfs".to_string(), + "rootfstype=virtiofs".to_string(), + "rw".to_string(), + "panic=-1".to_string(), + format!("init={}", config.exec_path), + ]; + + if let Some(ip) = &config.guest_ip { + if let Some(host_ip) = &config.host_ip { + parts.push(format!("ip={ip}::{host_ip}:255.255.255.252:sandbox::off")); + parts.push(format!("VM_NET_IP={ip}")); + parts.push(format!("VM_NET_GW={host_ip}")); + } + } + + if let Some(dns) = host_dns_server() { + parts.push(format!("VM_NET_DNS={dns}")); + } + + if config.gpu_bdf.is_some() { + parts.push("GPU_ENABLED=true".to_string()); + parts.push("firmware_class.path=/lib/firmware".to_string()); + } + + parts.join(" ") +} + +fn host_dns_server() -> Option { + // Prefer systemd-resolved upstream config (skips the 127.0.0.53 + // stub listener which is unreachable from inside QEMU/TAP guests). + for path in &["/run/systemd/resolve/resolv.conf", "/etc/resolv.conf"] { + let Ok(resolv) = std::fs::read_to_string(path) else { + continue; + }; + for line in resolv.lines() { + let line = line.trim(); + if let Some(server) = line.strip_prefix("nameserver") { + let server = server.trim(); + if server == "127.0.0.53" || server.starts_with("127.") { + continue; + } + if !server.is_empty() { + return Some(server.to_string()); + } + } + } + } + None +} + +/// Remove leftover `vmtap-*` interfaces from previous driver runs that +/// were not torn down (e.g. the launcher was SIGKILLed before teardown). +/// Called once at driver startup so stale interfaces cannot cause subnet +/// routing conflicts with newly allocated TAPs. +pub fn cleanup_stale_tap_interfaces() { + let Ok(entries) = std::fs::read_dir("/sys/class/net") else { + return; + }; + for entry in entries.flatten() { + let name = entry.file_name(); + let Some(name) = name.to_str() else { + continue; + }; + if !name.starts_with("vmtap-") { + continue; + } + // Read the IP address so we can clean up iptables rules too. + // Port 0 tells teardown we don't know the original gateway port; + // the blanket legacy rule is still cleaned up best-effort. + let ip = read_tap_host_ip(name); + if let Some(ref host_ip) = ip { + teardown_tap_networking(name, host_ip, 0); + } else { + let _ = run_cmd("ip", &["link", "set", name, "down"]); + let _ = run_cmd("ip", &["tuntap", "del", "dev", name, "mode", "tap"]); + } + tracing::warn!(interface = %name, "removed stale TAP interface from previous run"); + } +} + +/// Read the first IPv4 address assigned to a network interface. +fn read_tap_host_ip(device: &str) -> Option { + let output = StdCommand::new("ip") + .args(["-4", "-o", "addr", "show", "dev", device]) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::null()) + .output() + .ok()?; + let stdout = String::from_utf8_lossy(&output.stdout); + // Format: "28: vmtap-xxx inet 10.0.128.1/30 ..." + for token in stdout.split_whitespace() { + if let Some((ip, _prefix)) = token.split_once('/') { + if ip.parse::().is_ok() { + return Some(ip.to_string()); + } + } + } + None +} + +fn setup_tap_networking(tap_device: &str, host_ip: &str, gateway_port: u16) -> Result<(), String> { + run_cmd("ip", &["tuntap", "add", "dev", tap_device, "mode", "tap"])?; + run_cmd( + "ip", + &["addr", "add", &format!("{host_ip}/30"), "dev", tap_device], + )?; + run_cmd("ip", &["link", "set", tap_device, "up"])?; + + // Deprioritize routes through down interfaces so a stale vmtap-* + // that somehow survives cleanup cannot shadow the active one. + let _ = std::fs::write( + format!("/proc/sys/net/ipv4/conf/{tap_device}/ignore_routes_with_linkdown"), + "1", + ); + + enable_ip_forwarding()?; + + let subnet = tap_subnet_from_host_ip(host_ip); + let _ = run_cmd( + "iptables", + &[ + "-t", + "nat", + "-D", + "POSTROUTING", + "-s", + &subnet, + "-j", + "MASQUERADE", + ], + ); + run_cmd( + "iptables", + &[ + "-t", + "nat", + "-A", + "POSTROUTING", + "-s", + &subnet, + "-j", + "MASQUERADE", + ], + )?; + let _ = run_cmd( + "iptables", + &["-D", "FORWARD", "-i", tap_device, "-j", "ACCEPT"], + ); + run_cmd( + "iptables", + &["-A", "FORWARD", "-i", tap_device, "-j", "ACCEPT"], + )?; + let _ = run_cmd( + "iptables", + &[ + "-D", + "FORWARD", + "-o", + tap_device, + "-m", + "state", + "--state", + "RELATED,ESTABLISHED", + "-j", + "ACCEPT", + ], + ); + run_cmd( + "iptables", + &[ + "-A", + "FORWARD", + "-o", + tap_device, + "-m", + "state", + "--state", + "RELATED,ESTABLISHED", + "-j", + "ACCEPT", + ], + )?; + // Allow guest → host traffic only to the gateway gRPC port. + // Previous versions accepted ALL inbound traffic from the TAP + // interface; scope to the specific port so the guest cannot reach + // other host services. + let port_str = gateway_port.to_string(); + let _ = run_cmd( + "iptables", + &[ + "-D", "INPUT", "-i", tap_device, "-p", "tcp", "--dport", &port_str, "-j", "ACCEPT", + ], + ); + run_cmd( + "iptables", + &[ + "-A", "INPUT", "-i", tap_device, "-p", "tcp", "--dport", &port_str, "-j", "ACCEPT", + ], + )?; + + Ok(()) +} + +fn teardown_tap_networking(tap_device: &str, host_ip: &str, gateway_port: u16) { + let subnet = tap_subnet_from_host_ip(host_ip); + let _ = run_cmd( + "iptables", + &[ + "-D", + "FORWARD", + "-o", + tap_device, + "-m", + "state", + "--state", + "RELATED,ESTABLISHED", + "-j", + "ACCEPT", + ], + ); + let _ = run_cmd( + "iptables", + &["-D", "FORWARD", "-i", tap_device, "-j", "ACCEPT"], + ); + // Remove the port-scoped INPUT rule. Also try the legacy blanket + // rule so stale rules from older driver versions are cleaned up. + if gateway_port > 0 { + let port_str = gateway_port.to_string(); + let _ = run_cmd( + "iptables", + &[ + "-D", "INPUT", "-i", tap_device, "-p", "tcp", "--dport", &port_str, "-j", "ACCEPT", + ], + ); + } + let _ = run_cmd( + "iptables", + &["-D", "INPUT", "-i", tap_device, "-j", "ACCEPT"], + ); + let _ = run_cmd( + "iptables", + &[ + "-t", + "nat", + "-D", + "POSTROUTING", + "-s", + &subnet, + "-j", + "MASQUERADE", + ], + ); + let _ = run_cmd("ip", &["link", "set", tap_device, "down"]); + let _ = run_cmd("ip", &["tuntap", "del", "dev", tap_device, "mode", "tap"]); +} + +fn tap_subnet_from_host_ip(host_ip: &str) -> String { + if let Ok(ip) = host_ip.parse::() { + let base = u32::from(ip) & !3; + let base_ip = std::net::Ipv4Addr::from(base); + format!("{base_ip}/30") + } else { + format!("{host_ip}/30") + } +} + +fn enable_ip_forwarding() -> Result<(), String> { + std::fs::write("/proc/sys/net/ipv4/ip_forward", "1") + .map_err(|e| format!("enable ip_forward: {e}")) +} + +fn run_cmd(cmd: &str, args: &[&str]) -> Result<(), String> { + let output = StdCommand::new(cmd) + .args(args) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .output() + .map_err(|e| format!("failed to run {cmd}: {e}"))?; + if output.status.success() { + Ok(()) + } else { + let stderr = String::from_utf8_lossy(&output.stderr); + Err(format!("{cmd} {} failed: {stderr}", args.join(" "))) + } +} + +/// RAII guard that tears down TAP networking on drop. +struct TapGuard { + tap_device: String, + host_ip: String, + gateway_port: u16, + disarmed: bool, +} + +impl TapGuard { + fn new(tap_device: String, host_ip: String, gateway_port: u16) -> Self { + Self { + tap_device, + host_ip, + gateway_port, + disarmed: false, + } + } + + fn disarm(&mut self) { + self.disarmed = true; + } +} + +impl Drop for TapGuard { + fn drop(&mut self) { + if !self.disarmed { + teardown_tap_networking(&self.tap_device, &self.host_ip, self.gateway_port); + } + } +} + +/// Shared procguard cleanup callback for both libkrun and QEMU paths. +/// Only async-signal-safe calls: atomic loads and `kill(2)`. +fn procguard_kill_children() { + let helper_pid = GVPROXY_PID.load(Ordering::Relaxed); + let child_pid = CHILD_PID.load(Ordering::Relaxed); + if helper_pid > 0 { + unsafe { + libc::kill(helper_pid, libc::SIGTERM); + } + } + if child_pid > 0 { + unsafe { + libc::kill(child_pid, libc::SIGTERM); + } + } + std::thread::sleep(Duration::from_millis(200)); + if helper_pid > 0 { + unsafe { + libc::kill(helper_pid, libc::SIGKILL); + } + } + if child_pid > 0 { + unsafe { + libc::kill(child_pid, libc::SIGKILL); + } + } +} + +fn run_libkrun_vm(config: &VmLaunchConfig) -> Result<(), String> { if !config.rootfs.is_dir() { return Err(format!( "rootfs directory not found: {}", @@ -50,37 +656,7 @@ pub fn run_vm(config: &VmLaunchConfig) -> Result<(), String> { // those slots get populated later in this function. Only ONE arm // per process: racing two watchers for the same NOTE_EXIT event // would cause whichever wins to skip the cleanup. - if let Err(err) = procguard::die_with_parent_cleanup(|| { - // Cleanup order: SIGTERM gvproxy and the libkrun fork first so - // they can drain cleanly, then SIGKILL after a brief grace - // window. We can't rely on Rust destructors here; when - // procguard's watcher thread returns we call `std::process::exit` - // and the process tears down. Only async-signal-safe calls here: - // atomic loads and `kill(2)` are both on the POSIX list. - let gv_pid = GVPROXY_PID.load(Ordering::Relaxed); - let child_pid = CHILD_PID.load(Ordering::Relaxed); - if gv_pid > 0 { - unsafe { - libc::kill(gv_pid, libc::SIGTERM); - } - } - if child_pid > 0 { - unsafe { - libc::kill(child_pid, libc::SIGTERM); - } - } - std::thread::sleep(Duration::from_millis(200)); - if gv_pid > 0 { - unsafe { - libc::kill(gv_pid, libc::SIGKILL); - } - } - if child_pid > 0 { - unsafe { - libc::kill(child_pid, libc::SIGKILL); - } - } - }) { + if let Err(err) = procguard::die_with_parent_cleanup(procguard_kill_children) { return Err(format!("procguard arm failed: {err}")); } @@ -310,6 +886,14 @@ pub fn configured_runtime_dir() -> Result { embedded_runtime::ensure_runtime_extracted() } +fn qemu_runtime_dir() -> Result { + configured_runtime_dir().map_err(|_| { + "QEMU backend requires OPENSHELL_VM_RUNTIME_DIR to be set (pointing to a directory \ + containing vmlinux). Set the env var or run `mise run vm:setup`." + .to_string() + }) +} + #[cfg(target_os = "macos")] fn configure_runtime_loader_env(runtime_dir: &Path) -> Result<(), String> { let existing = std::env::var_os("DYLD_FALLBACK_LIBRARY_PATH"); diff --git a/crates/openshell-driver-vm/start.sh b/crates/openshell-driver-vm/start.sh index 0579e8aa08..d98bb7b917 100755 --- a/crates/openshell-driver-vm/start.sh +++ b/crates/openshell-driver-vm/start.sh @@ -4,6 +4,16 @@ set -euo pipefail +# Under sudo, PATH is reset and user-local tools (mise, cargo) disappear. +# Restore the invoking user's tool directories so mise and its shims work. +if [ -n "${SUDO_USER:-}" ]; then + _sudo_home=$(getent passwd "${SUDO_USER}" | cut -d: -f6) + for _p in "${_sudo_home}/.local/bin" "${_sudo_home}/.local/share/mise/shims" "${_sudo_home}/.cargo/bin"; do + [ -d "${_p}" ] && PATH="${_p}:${PATH}" + done + export PATH +fi + ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" CLI_BIN="${ROOT}/scripts/bin/openshell" COMPRESSED_DIR="${ROOT}/target/vm-runtime-compressed" @@ -28,6 +38,13 @@ DRIVER_DIR="${OPENSHELL_DRIVER_DIR:-${DRIVER_DIR_DEFAULT}}" export OPENSHELL_VM_RUNTIME_COMPRESSED_DIR="${OPENSHELL_VM_RUNTIME_COMPRESSED_DIR:-${COMPRESSED_DIR}}" +for arg in "$@"; do + if [ "${arg}" = "--gpu" ]; then + export OPENSHELL_VM_GPU=true + break + fi +done + mkdir -p "${STATE_DIR}" normalize_bool() { @@ -73,13 +90,19 @@ check_supervisor_cross_toolchain() { fi } -if [ ! -f "${COMPRESSED_DIR}/rootfs.tar.zst" ]; then +if [ ! -s "${OPENSHELL_VM_RUNTIME_COMPRESSED_DIR}/rootfs.tar.zst" ]; then check_supervisor_cross_toolchain echo "==> Building base VM rootfs tarball" mise run vm:rootfs -- --base fi -if [ ! -f "${COMPRESSED_DIR}/rootfs.tar.zst" ] || ! find "${COMPRESSED_DIR}" -maxdepth 1 -name 'libkrun*.zst' | grep -q .; then +if [ "${OPENSHELL_VM_GPU:-}" = "true" ] && [ ! -s "${OPENSHELL_VM_RUNTIME_COMPRESSED_DIR}/rootfs-gpu.tar.zst" ]; then + check_supervisor_cross_toolchain + echo "==> Building GPU VM rootfs tarball" + mise run vm:rootfs -- --gpu +fi + +if [ ! -s "${OPENSHELL_VM_RUNTIME_COMPRESSED_DIR}/rootfs.tar.zst" ] || ! find "${OPENSHELL_VM_RUNTIME_COMPRESSED_DIR}" -maxdepth 1 -name 'libkrun*.zst' | grep -q .; then echo "==> Preparing embedded VM runtime" mise run vm:setup fi @@ -106,12 +129,37 @@ export OPENSHELL_SSH_GATEWAY_PORT="${OPENSHELL_SSH_GATEWAY_PORT:-${SERVER_PORT}} export OPENSHELL_SSH_HANDSHAKE_SECRET="${OPENSHELL_SSH_HANDSHAKE_SECRET:-dev-vm-driver-secret}" export OPENSHELL_VM_DRIVER_STATE_DIR="${STATE_DIR}" -echo "==> Gateway registration" +# Resolve the VM runtime directory (contains vmlinux, virtiofsd, etc.) +# so the child --internal-run-vm process can find it under sudo. +if [ -z "${OPENSHELL_VM_RUNTIME_DIR:-}" ]; then + _candidate="${HOME}/.local/share/openshell/vm-runtime/0.0.0" + if [ -n "${SUDO_USER:-}" ]; then + _sudo_home=$(getent passwd "${SUDO_USER}" | cut -d: -f6) + _candidate="${_sudo_home}/.local/share/openshell/vm-runtime/0.0.0" + fi + if [ -f "${_candidate}/vmlinux" ]; then + export OPENSHELL_VM_RUNTIME_DIR="${_candidate}" + fi +fi + +echo "==> Registering gateway" echo " Name: ${GATEWAY_NAME}" echo " Endpoint: ${LOCAL_GATEWAY_ENDPOINT}" -echo " Register: ${CLI_BIN} gateway add --name ${GATEWAY_NAME} ${LOCAL_GATEWAY_ENDPOINT}" -echo " Select: ${CLI_BIN} gateway select ${GATEWAY_NAME}" -echo " Driver: ${OPENSHELL_DRIVER_DIR}/openshell-driver-vm" +echo " Driver: ${OPENSHELL_DRIVER_DIR}/openshell-driver-vm" + +# GPU passthrough requires root, but gateway config must be written to the +# real user's home directory — not /root/.config/openshell/. +# Unset XDG_CONFIG_HOME so the CLI falls back to $HOME/.config (sudo -u +# sets HOME correctly but may inherit XDG_CONFIG_HOME from the root env). +if [ -n "${SUDO_USER:-}" ]; then + sudo -u "${SUDO_USER}" env -u XDG_CONFIG_HOME "PATH=${PATH}" "${CLI_BIN}" gateway destroy --name "${GATEWAY_NAME}" 2>/dev/null || true + sudo -u "${SUDO_USER}" env -u XDG_CONFIG_HOME "PATH=${PATH}" "${CLI_BIN}" gateway add --name "${GATEWAY_NAME}" "${LOCAL_GATEWAY_ENDPOINT}" + sudo -u "${SUDO_USER}" env -u XDG_CONFIG_HOME "PATH=${PATH}" "${CLI_BIN}" gateway select "${GATEWAY_NAME}" +else + "${CLI_BIN}" gateway destroy --name "${GATEWAY_NAME}" 2>/dev/null || true + "${CLI_BIN}" gateway add --name "${GATEWAY_NAME}" "${LOCAL_GATEWAY_ENDPOINT}" + "${CLI_BIN}" gateway select "${GATEWAY_NAME}" +fi echo "==> Starting OpenShell server with VM compute driver" exec "${ROOT}/target/debug/openshell-gateway" diff --git a/crates/openshell-sandbox/src/lib.rs b/crates/openshell-sandbox/src/lib.rs index 34ee80bb59..262859c338 100644 --- a/crates/openshell-sandbox/src/lib.rs +++ b/crates/openshell-sandbox/src/lib.rs @@ -1567,6 +1567,68 @@ mod baseline_tests { } } +/// Returns `true` if the error is transient and worth retrying. +/// +/// Walks the `miette::Report` error chain looking for a `tonic::Status`. If +/// found, only the gRPC codes that represent transient failures are retryable. +/// If no `tonic::Status` is present (e.g. a raw connection error), assume the +/// failure is transient. +fn is_retryable_error(err: &miette::Report) -> bool { + let mut source: Option<&dyn std::error::Error> = Some(err.as_ref()); + while let Some(e) = source { + if let Some(status) = e.downcast_ref::() { + return matches!( + status.code(), + tonic::Code::Unavailable + | tonic::Code::DeadlineExceeded + | tonic::Code::ResourceExhausted + | tonic::Code::Aborted + | tonic::Code::Internal + | tonic::Code::Unknown + ); + } + source = e.source(); + } + true +} + +/// Retry a gRPC operation with exponential backoff (capped at 4 s). +/// +/// Non-transient gRPC errors (e.g. `NOT_FOUND`, `INVALID_ARGUMENT`, +/// `PERMISSION_DENIED`) are returned immediately without retrying. +async fn grpc_retry(op_name: &str, f: F) -> miette::Result +where + F: Fn() -> Fut, + Fut: std::future::Future>, +{ + let mut last_err = None; + for attempt in 1..=5u32 { + match f().await { + Ok(val) => return Ok(val), + Err(e) => { + if !is_retryable_error(&e) { + return Err(e); + } + if attempt < 5 { + warn!( + attempt, + max_attempts = 5, + error = %e, + "{op_name} failed, retrying" + ); + let backoff = Duration::from_secs((1u64 << (attempt - 1)).min(4)); + tokio::time::sleep(backoff).await; + } + last_err = Some(e); + } + } + } + Err(miette::miette!( + "{op_name} failed after 5 attempts: {}", + last_err.expect("loop executed at least once") + )) +} + /// Load sandbox policy from local files or gRPC. /// /// Priority: @@ -1627,7 +1689,8 @@ async fn load_policy( endpoint = %endpoint, "Fetching sandbox policy via gRPC" ); - let proto_policy = grpc_client::fetch_policy(endpoint, id).await?; + let proto_policy = + grpc_retry("Policy fetch", || grpc_client::fetch_policy(endpoint, id)).await?; let mut proto_policy = match proto_policy { Some(p) => p, @@ -1656,7 +1719,10 @@ async fn load_policy( // Sync and re-fetch over a single connection to avoid extra // TLS handshakes. - grpc_client::discover_and_sync_policy(endpoint, id, sandbox, &discovered).await? + grpc_retry("Policy discovery sync", || { + grpc_client::discover_and_sync_policy(endpoint, id, sandbox, &discovered) + }) + .await? } }; diff --git a/crates/openshell-server/src/compute/mod.rs b/crates/openshell-server/src/compute/mod.rs index 8f3152b33d..a218d18a8f 100644 --- a/crates/openshell-server/src/compute/mod.rs +++ b/crates/openshell-server/src/compute/mod.rs @@ -1109,6 +1109,7 @@ fn driver_sandbox_spec_from_public(spec: &SandboxSpec) -> DriverSandboxSpec { .as_ref() .map(driver_sandbox_template_from_public), gpu: spec.gpu, + gpu_device: spec.gpu_device.clone(), } } @@ -1516,6 +1517,7 @@ impl ComputeDriver for NoopTestDriver { driver_version: "test".to_string(), default_image: "openshell/sandbox:test".to_string(), supports_gpu: false, + gpu_count: 0, }, )) } @@ -1662,6 +1664,7 @@ mod tests { driver_version: "test".to_string(), default_image: "openshell/sandbox:test".to_string(), supports_gpu: true, + gpu_count: 0, })) } diff --git a/crates/openshell-vfio/Cargo.toml b/crates/openshell-vfio/Cargo.toml new file mode 100644 index 0000000000..b93500fdcb --- /dev/null +++ b/crates/openshell-vfio/Cargo.toml @@ -0,0 +1,23 @@ +# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +[package] +name = "openshell-vfio" +description = "VFIO GPU passthrough lifecycle for OpenShell VM sandboxes" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +repository.workspace = true + +[dependencies] +serde = { workspace = true } +serde_json = { workspace = true } +thiserror = { workspace = true } +tracing = { workspace = true } + +[dev-dependencies] +tempfile = "3" + +[lints] +workspace = true diff --git a/crates/openshell-vfio/src/lib.rs b/crates/openshell-vfio/src/lib.rs new file mode 100644 index 0000000000..e7276b3018 --- /dev/null +++ b/crates/openshell-vfio/src/lib.rs @@ -0,0 +1,1575 @@ +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +//! VFIO GPU passthrough lifecycle management for `OpenShell` VM sandboxes. +//! +//! Provides discovery, binding, and crash-recovery for NVIDIA GPUs using +//! the VFIO subsystem. All sysfs access goes through [`SysfsRoot`] so the +//! entire stack is testable without root or real hardware. + +use std::collections::HashMap; +use std::fs; +use std::path::{Path, PathBuf}; +use std::sync::Mutex; +use std::time::Duration; + +const NVIDIA_VENDOR_ID: &str = "0x10de"; +const GPU_CLASS_DISPLAY_VGA: &str = "0x030000"; +const GPU_CLASS_DISPLAY_3D: u32 = 0x0302; + +const VFIO_BIND_POLL_INTERVAL: Duration = Duration::from_millis(100); +const VFIO_BIND_MAX_POLL_ATTEMPTS: u32 = 20; + +/// Reference counter for vendor:device ID registrations in the vfio-pci +/// match table. Multiple GPUs may share the same vendor:device pair (e.g., +/// two A100s). We only write to the kernel's `new_id`/`remove_id` sysfs +/// files when the first GPU registers or the last GPU deregisters an ID. +static VFIO_ID_REFCOUNTS: std::sync::LazyLock>> = + std::sync::LazyLock::new(|| Mutex::new(HashMap::new())); + +// --------------------------------------------------------------------------- +// Errors +// --------------------------------------------------------------------------- + +#[derive(Debug, thiserror::Error)] +pub enum VfioError { + #[error("GPU {bdf} not found in sysfs")] + GpuNotFound { bdf: String }, + + #[error("GPU {bdf} is not an NVIDIA device (vendor={vendor})")] + NotNvidia { bdf: String, vendor: String }, + + #[error("GPU {bdf} has no IOMMU group — is IOMMU enabled?")] + NoIommuGroup { bdf: String }, + + #[error("GPU {bdf} IOMMU group {group} has other non-vfio-pci devices: {peers:?}")] + IommuGroupConflict { + bdf: String, + group: u32, + peers: Vec, + }, + + #[error("failed to bind GPU {bdf} to vfio-pci: {reason}")] + BindFailed { bdf: String, reason: String }, + + #[error("failed to unbind GPU {bdf} from vfio-pci: {reason}")] + UnbindFailed { bdf: String, reason: String }, + + #[error("sysfs I/O error for {path}: {source}")] + SysfsIo { + path: String, + #[source] + source: std::io::Error, + }, + + #[error("invalid PCI BDF address: {bdf}")] + InvalidBdf { bdf: String }, +} + +// --------------------------------------------------------------------------- +// SysfsRoot +// --------------------------------------------------------------------------- + +/// Abstraction over sysfs paths, enabling test mocks via a temporary directory. +#[derive(Debug, Clone)] +pub struct SysfsRoot { + base: PathBuf, +} + +impl SysfsRoot { + /// Production root pointing at the real `/sys` filesystem. + pub fn system() -> Self { + Self { + base: PathBuf::from("/sys"), + } + } + + /// Custom root for testing. + pub fn new(base: impl Into) -> Self { + Self { base: base.into() } + } + + pub fn pci_devices_dir(&self) -> PathBuf { + self.base.join("bus/pci/devices") + } + + pub fn pci_device(&self, bdf: &str) -> PathBuf { + self.pci_devices_dir().join(bdf) + } + + pub fn drivers_probe(&self) -> PathBuf { + self.base.join("bus/pci/drivers_probe") + } + + pub fn vfio_pci_new_id(&self) -> PathBuf { + self.base.join("bus/pci/drivers/vfio-pci/new_id") + } + + pub fn vfio_pci_remove_id(&self) -> PathBuf { + self.base.join("bus/pci/drivers/vfio-pci/remove_id") + } + + pub fn iommu_group(&self, bdf: &str) -> Result { + let link = self.pci_device(bdf).join("iommu_group"); + let target = fs::read_link(&link).map_err(|_| VfioError::NoIommuGroup { + bdf: bdf.to_string(), + })?; + let group_str = + target + .file_name() + .and_then(|n| n.to_str()) + .ok_or_else(|| VfioError::NoIommuGroup { + bdf: bdf.to_string(), + })?; + group_str + .parse::() + .map_err(|_| VfioError::NoIommuGroup { + bdf: bdf.to_string(), + }) + } + + /// Enumerate all PCI BDFs in the given IOMMU group. + pub fn iommu_group_devices(&self, group_id: u32) -> Result, VfioError> { + let group_dir = self + .base + .join(format!("kernel/iommu_groups/{group_id}/devices")); + let entries = fs::read_dir(&group_dir).map_err(|source| VfioError::SysfsIo { + path: group_dir.display().to_string(), + source, + })?; + let mut devices = Vec::new(); + for entry in entries.filter_map(Result::ok) { + devices.push(entry.file_name().to_string_lossy().into_owned()); + } + devices.sort(); + Ok(devices) + } +} + +// --------------------------------------------------------------------------- +// GpuInfo +// --------------------------------------------------------------------------- + +/// Information about a discovered NVIDIA GPU eligible for VFIO passthrough. +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] +pub struct GpuInfo { + pub bdf: String, + pub name: String, + pub vendor: String, + pub device: String, + pub iommu_group: u32, +} + +// --------------------------------------------------------------------------- +// GpuBindGuard +// --------------------------------------------------------------------------- + +/// RAII guard that restores a GPU to its host driver when dropped. +/// +/// Call [`disarm`](Self::disarm) to transfer ownership (e.g. the VM took over +/// the device successfully and we should not unbind it on cleanup). +pub struct GpuBindGuard { + bdf: String, + companion_bdfs: Vec, + sysfs: SysfsRoot, + disarmed: bool, + /// Cached "VVVV DDDD" string captured at bind time so that + /// deregistration from vfio-pci's match table succeeds even if the + /// device's sysfs entries have disappeared (e.g. physical removal). + vfio_id: Option, +} + +impl GpuBindGuard { + pub fn bdf(&self) -> &str { + &self.bdf + } + + /// Prevent the guard from restoring the GPU on drop. + pub fn disarm(mut self) { + self.disarmed = true; + } +} + +impl GpuBindGuard { + /// Deregister the cached vfio-pci match-table entry, then restore the + /// device to its host driver. + /// + /// Using the cached ID avoids re-reading vendor/device from sysfs, + /// which would fail if the GPU has been physically removed. + fn restore_with_cached_id(&self) { + if let Some(ref id_str) = self.vfio_id { + deregister_vfio_id_by_value(&self.sysfs, id_str); + } + + for peer in &self.companion_bdfs { + if let Err(err) = restore_gpu_to_host_driver(&self.sysfs, peer) { + tracing::error!(bdf = %peer, error = %err, "failed to restore companion device to host driver"); + } + } + + if let Err(err) = + restore_gpu_to_host_driver_ex(&self.sysfs, &self.bdf, self.vfio_id.is_some()) + { + tracing::error!(bdf = %self.bdf, error = %err, "failed to restore GPU to host driver"); + } + } +} + +impl Drop for GpuBindGuard { + fn drop(&mut self) { + if self.disarmed { + return; + } + if self.vfio_id.is_some() { + self.restore_with_cached_id(); + } else { + for peer in &self.companion_bdfs { + if let Err(err) = restore_gpu_to_host_driver(&self.sysfs, peer) { + tracing::error!(bdf = %peer, error = %err, "failed to restore companion device to host driver on drop"); + } + } + if let Err(err) = restore_gpu_to_host_driver(&self.sysfs, &self.bdf) { + tracing::error!(bdf = %self.bdf, error = %err, "failed to restore GPU to host driver on drop"); + } + } + } +} + +// --------------------------------------------------------------------------- +// GpuBindState (crash-recovery persistence) +// --------------------------------------------------------------------------- + +/// Persisted record of GPUs currently bound to vfio-pci, for crash recovery. +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] +pub struct GpuBindState { + pub bindings: Vec, +} + +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] +pub struct GpuBinding { + pub bdf: String, + pub sandbox_id: String, + pub bound_at_ms: i64, +} + +impl GpuBindState { + pub fn load(path: &Path) -> Result { + let data = fs::read_to_string(path)?; + serde_json::from_str(&data) + .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e)) + } + + pub fn save(&self, path: &Path) -> Result<(), std::io::Error> { + let data = serde_json::to_string_pretty(self) + .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?; + let tmp = path.with_extension("tmp"); + fs::write(&tmp, &data)?; + fs::rename(&tmp, path) + } +} + +// --------------------------------------------------------------------------- +// Validation helpers +// --------------------------------------------------------------------------- + +/// Validate a PCI BDF address (format `DDDD:BB:DD.F`). +pub fn validate_bdf(bdf: &str) -> Result<(), VfioError> { + let bytes = bdf.as_bytes(); + if bytes.len() != 12 { + return Err(VfioError::InvalidBdf { + bdf: bdf.to_string(), + }); + } + + // Expected layout: [hex×4]:[hex×2]:[hex×2].[hex×1] + // 0123 4 56 7 89 A B + let ok = is_hex(bytes[0]) + && is_hex(bytes[1]) + && is_hex(bytes[2]) + && is_hex(bytes[3]) + && bytes[4] == b':' + && is_hex(bytes[5]) + && is_hex(bytes[6]) + && bytes[7] == b':' + && is_hex(bytes[8]) + && is_hex(bytes[9]) + && bytes[10] == b'.' + && is_hex(bytes[11]); + + if ok { + Ok(()) + } else { + Err(VfioError::InvalidBdf { + bdf: bdf.to_string(), + }) + } +} + +fn is_hex(b: u8) -> bool { + b.is_ascii_hexdigit() +} + +/// Returns `true` if `data` contains only safe characters for sysfs values +/// (alphanumeric plus `:`, `.`, `-`, `_`). +pub fn validate_sysfs_data(data: &str) -> bool { + !data.is_empty() + && data + .chars() + .all(|c| c.is_alphanumeric() || matches!(c, ':' | '.' | '-' | '_')) +} + +// --------------------------------------------------------------------------- +// Sysfs helpers +// --------------------------------------------------------------------------- + +fn read_sysfs_trimmed(path: &Path) -> Result { + fs::read_to_string(path) + .map(|s| s.trim().to_string()) + .map_err(|source| VfioError::SysfsIo { + path: path.display().to_string(), + source, + }) +} + +fn write_sysfs(path: &Path, value: &str) -> Result<(), VfioError> { + fs::write(path, value).map_err(|source| VfioError::SysfsIo { + path: path.display().to_string(), + source, + }) +} + +fn current_driver_name(sysfs: &SysfsRoot, bdf: &str) -> Option { + let driver_link = sysfs.pci_device(bdf).join("driver"); + fs::read_link(&driver_link) + .ok() + .and_then(|p| p.file_name().map(|n| n.to_string_lossy().into_owned())) +} + +fn is_gpu_class(class_str: &str) -> bool { + if class_str == GPU_CLASS_DISPLAY_VGA { + return true; + } + // 3D controller: 0x0302xx + if let Some(hex) = class_str.strip_prefix("0x") + && let Ok(val) = u32::from_str_radix(hex, 16) + { + return (val >> 8) == GPU_CLASS_DISPLAY_3D; + } + false +} + +// --------------------------------------------------------------------------- +// Discovery +// --------------------------------------------------------------------------- + +/// Scan sysfs for NVIDIA GPUs eligible for VFIO passthrough. +pub fn probe_host_nvidia_vfio_readiness(sysfs: &SysfsRoot) -> Vec { + let devices_dir = sysfs.pci_devices_dir(); + let entries = match fs::read_dir(&devices_dir) { + Ok(e) => e, + Err(err) => { + tracing::warn!(path = %devices_dir.display(), %err, "cannot read PCI devices directory"); + return Vec::new(); + } + }; + + let mut gpus = Vec::new(); + + for entry in entries.filter_map(Result::ok) { + let bdf = entry.file_name().to_string_lossy().into_owned(); + let dev_dir = sysfs.pci_device(&bdf); + + let Ok(vendor) = read_sysfs_trimmed(&dev_dir.join("vendor")) else { + continue; + }; + if vendor != NVIDIA_VENDOR_ID { + continue; + } + + let Ok(class) = read_sysfs_trimmed(&dev_dir.join("class")) else { + continue; + }; + if !is_gpu_class(&class) { + continue; + } + + let device = read_sysfs_trimmed(&dev_dir.join("device")).unwrap_or_default(); + + let name = read_sysfs_trimmed(&dev_dir.join("label")) + .unwrap_or_else(|_| format!("NVIDIA {device}")); + + let Ok(iommu_group) = sysfs.iommu_group(&bdf) else { + continue; + }; + + gpus.push(GpuInfo { + bdf, + name, + vendor, + device, + iommu_group, + }); + } + + gpus +} + +// --------------------------------------------------------------------------- +// Bind / unbind +// --------------------------------------------------------------------------- + +/// Read vendor and device IDs from sysfs and format as `"VVVV DDDD"` (no `0x` prefix). +fn vfio_id_string(sysfs: &SysfsRoot, bdf: &str) -> Option { + let dev_dir = sysfs.pci_device(bdf); + let vendor = read_sysfs_trimmed(&dev_dir.join("vendor")).ok()?; + let device = read_sysfs_trimmed(&dev_dir.join("device")).ok()?; + let vendor_hex = vendor.strip_prefix("0x").unwrap_or(&vendor); + let device_hex = device.strip_prefix("0x").unwrap_or(&device); + Some(format!("{vendor_hex} {device_hex}")) +} + +/// Best-effort registration of a device's vendor:device ID with `vfio-pci`. +/// +/// Some kernel configurations require the ID to be pre-registered in +/// `/sys/bus/pci/drivers/vfio-pci/new_id` before `drivers_probe` will +/// bind the device, even when `driver_override` is set. Writing an +/// already-registered ID returns `EEXIST`, which we silently ignore. +fn register_vfio_new_id(sysfs: &SysfsRoot, bdf: &str) { + let Some(id_str) = vfio_id_string(sysfs, bdf) else { + return; + }; + + let should_write = { + let mut map = VFIO_ID_REFCOUNTS.lock().unwrap(); + let count = map.entry(id_str.clone()).or_insert(0); + *count += 1; + *count == 1 + }; + + if !should_write { + tracing::debug!( + bdf, id = %id_str, + "vfio-pci new_id already registered by another GPU, refcount incremented" + ); + return; + } + + let new_id_path = sysfs.vfio_pci_new_id(); + match write_sysfs(&new_id_path, &id_str) { + Ok(()) => { + tracing::debug!(bdf, id = %id_str, "registered vfio-pci new_id"); + } + Err(_) => { + tracing::debug!( + bdf, id = %id_str, + "vfio-pci new_id write skipped (already registered or driver not loaded)" + ); + } + } +} + +/// Best-effort deregistration of a device's vendor:device ID from `vfio-pci`. +/// +/// Reverses the effect of [`register_vfio_new_id`] by writing to +/// `/sys/bus/pci/drivers/vfio-pci/remove_id`. This prevents vfio-pci +/// from winning the probe race against the host driver when +/// `drivers_probe` runs during restore. +/// +/// `ENODEV` is silently ignored (the ID may never have been registered +/// or was already removed). +fn deregister_vfio_new_id(sysfs: &SysfsRoot, bdf: &str) { + let Some(id_str) = vfio_id_string(sysfs, bdf) else { + return; + }; + + let should_write = { + let mut map = VFIO_ID_REFCOUNTS.lock().unwrap(); + match map.get_mut(&id_str) { + Some(count) if *count > 1 => { + *count -= 1; + false + } + Some(_) => { + map.remove(&id_str); + true + } + None => true, + } + }; + + if !should_write { + tracing::debug!( + bdf, id = %id_str, + "vfio-pci remove_id skipped (other GPUs still using this ID)" + ); + return; + } + + let remove_id_path = sysfs.vfio_pci_remove_id(); + match write_sysfs(&remove_id_path, &id_str) { + Ok(()) => { + tracing::debug!(bdf, id = %id_str, "deregistered vfio-pci new_id"); + } + Err(_) => { + tracing::debug!( + bdf, id = %id_str, + "vfio-pci remove_id write skipped (not registered or already removed)" + ); + } + } +} + +/// Best-effort deregistration using a pre-captured ID string. +/// +/// Unlike [`deregister_vfio_new_id`], this does not read vendor/device +/// from sysfs at call time, making it reliable even when the device has +/// been physically removed or sysfs is otherwise inaccessible. +fn deregister_vfio_id_by_value(sysfs: &SysfsRoot, id_str: &str) { + let remove_id_path = sysfs.vfio_pci_remove_id(); + match write_sysfs(&remove_id_path, id_str) { + Ok(()) => { + tracing::debug!(id = %id_str, "deregistered vfio-pci new_id (by cached value)"); + } + Err(_) => { + tracing::debug!( + id = %id_str, + "vfio-pci remove_id write skipped (not registered or already removed)" + ); + } + } +} + +/// Bind a single PCI device to `vfio-pci`. Skips devices already bound. +fn bind_device_to_vfio(sysfs: &SysfsRoot, bdf: &str) -> Result { + if let Some(drv) = current_driver_name(sysfs, bdf) { + if drv == "vfio-pci" { + return Ok(false); + } + let unbind_path = sysfs.pci_device(bdf).join("driver/unbind"); + write_sysfs(&unbind_path, bdf).map_err(|e| VfioError::BindFailed { + bdf: bdf.to_string(), + reason: format!("unbind from {drv}: {e}"), + })?; + tracing::info!(bdf, driver = %drv, "unbound device from current driver"); + } + + register_vfio_new_id(sysfs, bdf); + + let override_path = sysfs.pci_device(bdf).join("driver_override"); + if let Err(e) = write_sysfs(&override_path, "vfio-pci") { + deregister_vfio_new_id(sysfs, bdf); + return Err(VfioError::BindFailed { + bdf: bdf.to_string(), + reason: format!("driver_override: {e}"), + }); + } + + if let Err(e) = write_sysfs(&sysfs.drivers_probe(), bdf) { + deregister_vfio_new_id(sysfs, bdf); + return Err(VfioError::BindFailed { + bdf: bdf.to_string(), + reason: format!("drivers_probe: {e}"), + }); + } + + if matches!(current_driver_name(sysfs, bdf).as_deref(), Some("vfio-pci")) { + return Ok(true); + } + + // The kernel processes drivers_probe asynchronously on some systems; + // poll briefly to let the driver attach before declaring failure. + for _ in 0..VFIO_BIND_MAX_POLL_ATTEMPTS { + std::thread::sleep(VFIO_BIND_POLL_INTERVAL); + if matches!(current_driver_name(sysfs, bdf).as_deref(), Some("vfio-pci")) { + tracing::debug!(bdf, "vfio-pci binding confirmed after polling"); + return Ok(true); + } + } + + deregister_vfio_new_id(sysfs, bdf); + Err(VfioError::BindFailed { + bdf: bdf.to_string(), + reason: format!( + "after drivers_probe with {}ms polling, driver is {:?} instead of vfio-pci", + VFIO_BIND_MAX_POLL_ATTEMPTS as u64 * VFIO_BIND_POLL_INTERVAL.as_millis() as u64, + current_driver_name(sysfs, bdf) + .as_deref() + .unwrap_or("") + ), + }) +} + +/// Bind a GPU to `vfio-pci`, returning an RAII guard that restores it on drop. +/// +/// Also binds all companion devices in the same IOMMU group (e.g. the +/// HD Audio function on consumer GPUs). All bound companions are tracked +/// and restored when the guard is dropped. +pub fn prepare_gpu_for_passthrough( + sysfs: &SysfsRoot, + bdf: &str, +) -> Result { + validate_bdf(bdf)?; + + let dev_dir = sysfs.pci_device(bdf); + if !dev_dir.exists() { + return Err(VfioError::GpuNotFound { + bdf: bdf.to_string(), + }); + } + + let vendor = read_sysfs_trimmed(&dev_dir.join("vendor"))?; + if vendor != NVIDIA_VENDOR_ID { + return Err(VfioError::NotNvidia { + bdf: bdf.to_string(), + vendor, + }); + } + + let iommu_group = sysfs.iommu_group(bdf)?; + let group_devices = sysfs.iommu_group_devices(iommu_group)?; + let peers: Vec = group_devices.into_iter().filter(|d| d != bdf).collect(); + + let mut bound_companions = Vec::new(); + for peer in &peers { + if !sysfs.pci_device(peer).exists() { + continue; + } + match bind_device_to_vfio(sysfs, peer) { + Ok(was_bound) => { + if was_bound { + tracing::info!(bdf = %peer, iommu_group, "bound IOMMU group companion to vfio-pci"); + bound_companions.push(peer.clone()); + } + } + Err(err) => { + for already_bound in bound_companions.iter().rev() { + if let Err(restore_err) = restore_gpu_to_host_driver(sysfs, already_bound) { + tracing::error!(bdf = %already_bound, error = %restore_err, "failed to restore companion during rollback"); + } + } + return Err(VfioError::BindFailed { + bdf: peer.clone(), + reason: format!("IOMMU group {iommu_group} companion bind failed: {err}"), + }); + } + } + } + + match bind_device_to_vfio(sysfs, bdf) { + Ok(was_bound) => { + if was_bound { + tracing::info!(bdf, "GPU bound to vfio-pci"); + } else { + tracing::info!(bdf, "GPU already bound to vfio-pci"); + } + } + Err(err) => { + for companion in bound_companions.iter().rev() { + if let Err(restore_err) = restore_gpu_to_host_driver(sysfs, companion) { + tracing::error!(bdf = %companion, error = %restore_err, "failed to restore companion during rollback"); + } + } + return Err(err); + } + } + + let vfio_id = vfio_id_string(sysfs, bdf); + + Ok(GpuBindGuard { + bdf: bdf.to_string(), + companion_bdfs: bound_companions, + sysfs: sysfs.clone(), + disarmed: false, + vfio_id, + }) +} + +/// Restore a GPU from `vfio-pci` back to the host's default driver. +fn restore_gpu_to_host_driver(sysfs: &SysfsRoot, bdf: &str) -> Result<(), VfioError> { + restore_gpu_to_host_driver_ex(sysfs, bdf, false) +} + +/// Inner restore implementation. +/// +/// When `skip_deregister` is `true` the caller has already removed the +/// device's vendor:device ID from vfio-pci's match table (e.g. via a +/// cached value), so we skip the sysfs-based deregistration. +fn restore_gpu_to_host_driver_ex( + sysfs: &SysfsRoot, + bdf: &str, + skip_deregister: bool, +) -> Result<(), VfioError> { + let dev_dir = sysfs.pci_device(bdf); + + if !skip_deregister { + // Deregister the device ID from vfio-pci's match table before + // unbind+reprobe. Without this, drivers_probe re-binds to vfio-pci + // via the still-registered new_id entry. + deregister_vfio_new_id(sysfs, bdf); + } + + let unbind_path = dev_dir.join("driver/unbind"); + if unbind_path.exists() { + write_sysfs(&unbind_path, bdf).map_err(|e| VfioError::UnbindFailed { + bdf: bdf.to_string(), + reason: format!("unbind: {e}"), + })?; + } + + let override_path = dev_dir.join("driver_override"); + if override_path.exists() { + write_sysfs(&override_path, "\n").map_err(|e| VfioError::UnbindFailed { + bdf: bdf.to_string(), + reason: format!("clear driver_override: {e}"), + })?; + } + + let probe = sysfs.drivers_probe(); + if probe.exists() { + write_sysfs(&probe, bdf).map_err(|e| VfioError::UnbindFailed { + bdf: bdf.to_string(), + reason: format!("drivers_probe: {e}"), + })?; + } + + tracing::info!(bdf, "GPU restored to host driver"); + Ok(()) +} + +// --------------------------------------------------------------------------- +// Crash-recovery reconciliation +// --------------------------------------------------------------------------- + +/// Reconcile stale VFIO bindings left over from a previous crash. +/// +/// Loads persisted state, checks each GPU, and restores any that are still +/// bound to `vfio-pci`. Returns the list of BDFs that were restored. +/// Removes the state file only when all bindings are resolved; rewrites it +/// with the remaining entries when some restorations fail so they can be +/// retried on the next process start. +pub fn reconcile_stale_bindings(sysfs: &SysfsRoot, state_path: &Path) -> Vec { + let state = match GpuBindState::load(state_path) { + Ok(s) => s, + Err(err) => { + tracing::debug!(%err, path = %state_path.display(), "no stale GPU bind state to reconcile"); + return Vec::new(); + } + }; + + // Any in-memory refcounts are stale (from a previous process that + // crashed). Clear them so deregister writes through to sysfs. + if let Ok(mut map) = VFIO_ID_REFCOUNTS.lock() { + map.clear(); + } + + let mut restored = Vec::new(); + let mut failed_bindings = Vec::new(); + + for binding in &state.bindings { + match current_driver_name(sysfs, &binding.bdf) { + Some(ref drv) if drv == "vfio-pci" => { + tracing::warn!( + bdf = %binding.bdf, + sandbox_id = %binding.sandbox_id, + "stale VFIO binding detected, restoring GPU to host driver" + ); + if let Err(err) = restore_gpu_to_host_driver(sysfs, &binding.bdf) { + tracing::error!(bdf = %binding.bdf, %err, "failed to restore stale GPU binding"); + failed_bindings.push(binding.clone()); + continue; + } + restored.push(binding.bdf.clone()); + } + _ => { + let override_path = sysfs.pci_device(&binding.bdf).join("driver_override"); + if let Ok(val) = read_sysfs_trimmed(&override_path) + && val == "vfio-pci" + { + tracing::warn!( + bdf = %binding.bdf, + sandbox_id = %binding.sandbox_id, + "stale driver_override detected, clearing and re-probing" + ); + deregister_vfio_new_id(sysfs, &binding.bdf); + if let Err(err) = write_sysfs(&override_path, "\n") { + tracing::error!(bdf = %binding.bdf, %err, "failed to clear stale driver_override"); + failed_bindings.push(binding.clone()); + continue; + } + let probe = sysfs.drivers_probe(); + if let Err(err) = write_sysfs(&probe, &binding.bdf) { + tracing::error!(bdf = %binding.bdf, %err, "failed to re-probe after clearing driver_override"); + } + restored.push(binding.bdf.clone()); + } else { + tracing::debug!(bdf = %binding.bdf, "GPU no longer bound to vfio-pci, skipping"); + } + } + } + } + + if failed_bindings.is_empty() { + if let Err(err) = fs::remove_file(state_path) { + tracing::warn!(%err, path = %state_path.display(), "failed to remove stale bind state file"); + } + } else { + let remaining = GpuBindState { + bindings: failed_bindings, + }; + match serde_json::to_string_pretty(&remaining) { + Ok(json) => { + if let Err(err) = fs::write(state_path, json) { + tracing::error!(%err, path = %state_path.display(), "failed to persist remaining stale bindings"); + } else { + tracing::warn!( + count = remaining.bindings.len(), + "some GPU bindings could not be restored; state preserved for retry" + ); + } + } + Err(err) => { + tracing::error!(%err, "failed to serialize remaining stale bindings"); + } + } + } + + restored +} + +// --------------------------------------------------------------------------- +// Tests +// --------------------------------------------------------------------------- + +#[cfg(test)] +mod tests { + use super::*; + use std::os::unix::fs::symlink; + use tempfile::TempDir; + + fn setup_mock_sysfs() -> (TempDir, SysfsRoot) { + let tmp = TempDir::new().unwrap(); + let sysfs = SysfsRoot::new(tmp.path()); + (tmp, sysfs) + } + + fn create_pci_device( + sysfs: &SysfsRoot, + tmp: &Path, + bdf: &str, + vendor: &str, + device: &str, + class: &str, + iommu_group: u32, + ) { + let dev = sysfs.pci_device(bdf); + fs::create_dir_all(&dev).unwrap(); + + fs::write(dev.join("vendor"), format!("{vendor}\n")).unwrap(); + fs::write(dev.join("device"), format!("{device}\n")).unwrap(); + fs::write(dev.join("class"), format!("{class}\n")).unwrap(); + + let group_dir = tmp.join(format!("kernel/iommu_groups/{iommu_group}")); + fs::create_dir_all(&group_dir).unwrap(); + symlink(&group_dir, dev.join("iommu_group")).unwrap(); + + let group_devices_dir = group_dir.join("devices"); + fs::create_dir_all(&group_devices_dir).unwrap(); + symlink(&dev, group_devices_dir.join(bdf)).unwrap(); + } + + /// Remove a specific vendor:device key from the global refcount map. + /// Used by tests to clean up their own entries without disturbing + /// parallel tests that hold refcounts for different device IDs. + fn clear_vfio_id_refcount(id: &str) { + VFIO_ID_REFCOUNTS.lock().unwrap().remove(id); + } + + // -- validate_bdf ------------------------------------------------------- + + #[test] + fn test_validate_bdf_valid() { + assert!(validate_bdf("0000:2d:00.0").is_ok()); + assert!(validate_bdf("0000:00:00.0").is_ok()); + assert!(validate_bdf("abcd:ef:01.a").is_ok()); + assert!(validate_bdf("ABCD:EF:01.A").is_ok()); + } + + #[test] + fn test_validate_bdf_invalid() { + assert!(validate_bdf("").is_err()); + assert!(validate_bdf("0000:2d:00").is_err()); // too short + assert!(validate_bdf("0000:2d:00.00").is_err()); // too long + assert!(validate_bdf("000g:2d:00.0").is_err()); // non-hex + assert!(validate_bdf("0000-2d-00.0").is_err()); // wrong separators + assert!(validate_bdf("0000:2d:00:0").is_err()); // colon instead of dot + } + + #[test] + fn test_validate_bdf_rejects_metacharacters() { + assert!(validate_bdf("$(rm -rf /)").is_err()); + assert!(validate_bdf("; echo pwned").is_err()); + assert!(validate_bdf("0000:2d;00.0").is_err()); + assert!(validate_bdf("0000:2d:0`.0").is_err()); + assert!(validate_bdf("../../../../").is_err()); + } + + // -- validate_sysfs_data ------------------------------------------------ + + #[test] + fn test_validate_sysfs_data() { + assert!(validate_sysfs_data("0x10de")); + assert!(validate_sysfs_data("vfio-pci")); + assert!(validate_sysfs_data("nvidia_gpu_0")); + assert!(validate_sysfs_data("0000:2d:00.0")); + + assert!(!validate_sysfs_data("")); + assert!(!validate_sysfs_data("$(echo)")); + assert!(!validate_sysfs_data("a b")); + assert!(!validate_sysfs_data("foo;bar")); + assert!(!validate_sysfs_data("a\nb")); + } + + // -- probe_host_nvidia_vfio_readiness ----------------------------------- + + #[test] + fn test_probe_discovers_nvidia_gpu() { + let (tmp, sysfs) = setup_mock_sysfs(); + create_pci_device( + &sysfs, + tmp.path(), + "0000:2d:00.0", + "0x10de", + "0x2684", + "0x030000", + 42, + ); + + let gpus = probe_host_nvidia_vfio_readiness(&sysfs); + assert_eq!(gpus.len(), 1); + assert_eq!(gpus[0].bdf, "0000:2d:00.0"); + assert_eq!(gpus[0].vendor, "0x10de"); + assert_eq!(gpus[0].device, "0x2684"); + assert_eq!(gpus[0].iommu_group, 42); + } + + #[test] + fn test_probe_skips_non_nvidia() { + let (tmp, sysfs) = setup_mock_sysfs(); + create_pci_device( + &sysfs, + tmp.path(), + "0000:01:00.0", + "0x8086", + "0x1234", + "0x030000", + 10, + ); + + let gpus = probe_host_nvidia_vfio_readiness(&sysfs); + assert!(gpus.is_empty()); + } + + #[test] + fn test_probe_skips_non_gpu_nvidia() { + let (tmp, sysfs) = setup_mock_sysfs(); + // Audio device class 0x040300 + create_pci_device( + &sysfs, + tmp.path(), + "0000:2d:00.1", + "0x10de", + "0x228b", + "0x040300", + 42, + ); + + let gpus = probe_host_nvidia_vfio_readiness(&sysfs); + assert!(gpus.is_empty()); + } + + // -- GpuBindState ------------------------------------------------------- + + #[test] + fn test_gpu_bind_state_roundtrip() { + let tmp = TempDir::new().unwrap(); + let path = tmp.path().join("gpu-state.json"); + + let state = GpuBindState { + bindings: vec![ + GpuBinding { + bdf: "0000:2d:00.0".to_string(), + sandbox_id: "sandbox-123".to_string(), + bound_at_ms: 1_700_000_000_000, + }, + GpuBinding { + bdf: "0000:3b:00.0".to_string(), + sandbox_id: "sandbox-456".to_string(), + bound_at_ms: 1_700_000_001_000, + }, + ], + }; + + state.save(&path).unwrap(); + let loaded = GpuBindState::load(&path).unwrap(); + + assert_eq!(loaded.bindings.len(), 2); + assert_eq!(loaded.bindings[0].bdf, "0000:2d:00.0"); + assert_eq!(loaded.bindings[0].sandbox_id, "sandbox-123"); + assert_eq!(loaded.bindings[1].bdf, "0000:3b:00.0"); + } + + // -- SysfsRoot ---------------------------------------------------------- + + #[test] + fn test_sysfs_root_paths() { + let sysfs = SysfsRoot::system(); + assert_eq!( + sysfs.pci_device("0000:2d:00.0"), + PathBuf::from("/sys/bus/pci/devices/0000:2d:00.0") + ); + assert_eq!( + sysfs.pci_devices_dir(), + PathBuf::from("/sys/bus/pci/devices") + ); + assert_eq!( + sysfs.drivers_probe(), + PathBuf::from("/sys/bus/pci/drivers_probe") + ); + assert_eq!( + sysfs.vfio_pci_new_id(), + PathBuf::from("/sys/bus/pci/drivers/vfio-pci/new_id") + ); + assert_eq!( + sysfs.vfio_pci_remove_id(), + PathBuf::from("/sys/bus/pci/drivers/vfio-pci/remove_id") + ); + + let custom = SysfsRoot::new("/tmp/test-sys"); + assert_eq!( + custom.pci_device("0000:01:00.0"), + PathBuf::from("/tmp/test-sys/bus/pci/devices/0000:01:00.0") + ); + } + + #[test] + fn test_sysfs_root_iommu_group() { + let (tmp, sysfs) = setup_mock_sysfs(); + create_pci_device( + &sysfs, + tmp.path(), + "0000:2d:00.0", + "0x10de", + "0x2684", + "0x030000", + 42, + ); + + assert_eq!(sysfs.iommu_group("0000:2d:00.0").unwrap(), 42); + assert!(sysfs.iommu_group("0000:ff:ff.f").is_err()); + } + + // -- is_gpu_class ------------------------------------------------------- + + #[test] + fn test_is_gpu_class() { + assert!(is_gpu_class("0x030000")); + assert!(is_gpu_class("0x030200")); + assert!(is_gpu_class("0x030201")); + assert!(!is_gpu_class("0x040300")); + assert!(!is_gpu_class("0x060000")); + assert!(!is_gpu_class("")); + } + + // -- iommu_group_devices ------------------------------------------------ + + #[test] + fn test_iommu_group_devices_lists_all_members() { + let (tmp, sysfs) = setup_mock_sysfs(); + create_pci_device( + &sysfs, + tmp.path(), + "0000:2d:00.0", + "0x10de", + "0x2684", + "0x030000", + 42, + ); + create_pci_device( + &sysfs, + tmp.path(), + "0000:2d:00.1", + "0x10de", + "0x228b", + "0x040300", + 42, + ); + + let devices = sysfs.iommu_group_devices(42).unwrap(); + assert_eq!(devices.len(), 2); + assert!(devices.contains(&"0000:2d:00.0".to_string())); + assert!(devices.contains(&"0000:2d:00.1".to_string())); + } + + #[test] + fn test_iommu_group_devices_single_device() { + let (tmp, sysfs) = setup_mock_sysfs(); + create_pci_device( + &sysfs, + tmp.path(), + "0000:2d:00.0", + "0x10de", + "0x2684", + "0x030000", + 99, + ); + + let devices = sysfs.iommu_group_devices(99).unwrap(); + assert_eq!(devices.len(), 1); + assert_eq!(devices[0], "0000:2d:00.0"); + } + + // -- register_vfio_new_id ----------------------------------------------- + + #[test] + fn test_register_vfio_new_id_writes_vendor_device() { + clear_vfio_id_refcount("10de 26b3"); + let (tmp, sysfs) = setup_mock_sysfs(); + create_pci_device( + &sysfs, + tmp.path(), + "0000:2d:00.0", + "0x10de", + "0x26b3", + "0x030000", + 42, + ); + + let new_id_path = sysfs.vfio_pci_new_id(); + fs::create_dir_all(new_id_path.parent().unwrap()).unwrap(); + fs::write(&new_id_path, "").unwrap(); + + register_vfio_new_id(&sysfs, "0000:2d:00.0"); + + let written = fs::read_to_string(&new_id_path).unwrap(); + assert_eq!(written, "10de 26b3"); + } + + #[test] + fn test_register_vfio_new_id_ignores_missing_new_id_file() { + clear_vfio_id_refcount("10de 26b4"); + let (tmp, sysfs) = setup_mock_sysfs(); + create_pci_device( + &sysfs, + tmp.path(), + "0000:2d:00.0", + "0x10de", + "0x26b4", + "0x030000", + 42, + ); + + // Don't create the new_id file — should not panic or error + register_vfio_new_id(&sysfs, "0000:2d:00.0"); + } + + // -- deregister_vfio_new_id --------------------------------------------- + + #[test] + fn test_deregister_vfio_new_id_writes_vendor_device() { + clear_vfio_id_refcount("10de 26b5"); + let (tmp, sysfs) = setup_mock_sysfs(); + create_pci_device( + &sysfs, + tmp.path(), + "0000:2d:00.0", + "0x10de", + "0x26b5", + "0x030000", + 42, + ); + + let remove_id_path = sysfs.vfio_pci_remove_id(); + fs::create_dir_all(remove_id_path.parent().unwrap()).unwrap(); + fs::write(&remove_id_path, "").unwrap(); + + deregister_vfio_new_id(&sysfs, "0000:2d:00.0"); + + let written = fs::read_to_string(&remove_id_path).unwrap(); + assert_eq!(written, "10de 26b5"); + } + + #[test] + fn test_deregister_vfio_new_id_ignores_missing_remove_id_file() { + clear_vfio_id_refcount("10de 26b6"); + let (tmp, sysfs) = setup_mock_sysfs(); + create_pci_device( + &sysfs, + tmp.path(), + "0000:2d:00.0", + "0x10de", + "0x26b6", + "0x030000", + 42, + ); + + deregister_vfio_new_id(&sysfs, "0000:2d:00.0"); + } + + // -- refcount safety ---------------------------------------------------- + + #[test] + fn test_register_deregister_refcount() { + clear_vfio_id_refcount("10de 26b8"); + let (tmp, sysfs) = setup_mock_sysfs(); + + // Two GPUs with the same vendor:device ID (e.g., two A100s). + // Uses 0x26b8 — unique to this test to avoid parallel interference. + create_pci_device( + &sysfs, + tmp.path(), + "0000:2d:00.0", + "0x10de", + "0x26b8", + "0x030000", + 42, + ); + create_pci_device( + &sysfs, + tmp.path(), + "0000:3b:00.0", + "0x10de", + "0x26b8", + "0x030200", + 43, + ); + + let new_id_path = sysfs.vfio_pci_new_id(); + fs::create_dir_all(new_id_path.parent().unwrap()).unwrap(); + fs::write(&new_id_path, "").unwrap(); + + let remove_id_path = sysfs.vfio_pci_remove_id(); + fs::write(&remove_id_path, "").unwrap(); + + // Register the same vendor:device for two different BDFs + register_vfio_new_id(&sysfs, "0000:2d:00.0"); + let written = fs::read_to_string(&new_id_path).unwrap(); + assert_eq!( + written, "10de 26b8", + "first register should write to new_id" + ); + + // Clear the file to detect whether the second register writes + fs::write(&new_id_path, "").unwrap(); + register_vfio_new_id(&sysfs, "0000:3b:00.0"); + let written = fs::read_to_string(&new_id_path).unwrap(); + assert_eq!( + written, "", + "second register should NOT write to new_id (refcount > 1)" + ); + + // Deregister once — should NOT write to remove_id (one GPU still using it) + deregister_vfio_new_id(&sysfs, "0000:2d:00.0"); + let written = fs::read_to_string(&remove_id_path).unwrap(); + assert_eq!( + written, "", + "first deregister should NOT write to remove_id" + ); + + // Deregister again — should write to remove_id (last user) + deregister_vfio_new_id(&sysfs, "0000:3b:00.0"); + let written = fs::read_to_string(&remove_id_path).unwrap(); + assert_eq!( + written, "10de 26b8", + "second deregister SHOULD write to remove_id" + ); + } + + // -- companion binding -------------------------------------------------- + + /// Helper to create a fake driver symlink for a mock PCI device. + fn set_mock_driver(sysfs: &SysfsRoot, bdf: &str, driver_name: &str) { + let driver_dir = sysfs.base.join(format!("bus/pci/drivers/{driver_name}")); + fs::create_dir_all(&driver_dir).unwrap(); + let dev_driver_link = sysfs.pci_device(bdf).join("driver"); + let _ = fs::remove_file(&dev_driver_link); + symlink(&driver_dir, &dev_driver_link).unwrap(); + } + + #[test] + fn test_prepare_gpu_skips_already_bound_companions() { + let (tmp, sysfs) = setup_mock_sysfs(); + create_pci_device( + &sysfs, + tmp.path(), + "0000:2d:00.0", + "0x10de", + "0x2684", + "0x030000", + 42, + ); + create_pci_device( + &sysfs, + tmp.path(), + "0000:2d:00.1", + "0x10de", + "0x228b", + "0x040300", + 42, + ); + + let probe = sysfs.drivers_probe(); + fs::create_dir_all(probe.parent().unwrap()).unwrap(); + fs::write(&probe, "").unwrap(); + + // Both devices already on vfio-pci + set_mock_driver(&sysfs, "0000:2d:00.0", "vfio-pci"); + set_mock_driver(&sysfs, "0000:2d:00.1", "vfio-pci"); + + let guard = prepare_gpu_for_passthrough(&sysfs, "0000:2d:00.0").unwrap(); + + // Both were already bound, no companions should be tracked for restore + assert!(guard.companion_bdfs.is_empty()); + assert_eq!(guard.bdf, "0000:2d:00.0"); + } + + #[test] + fn test_prepare_gpu_solo_iommu_group_no_companions() { + let (tmp, sysfs) = setup_mock_sysfs(); + create_pci_device( + &sysfs, + tmp.path(), + "0000:2d:00.0", + "0x10de", + "0x2684", + "0x030000", + 99, + ); + + let probe = sysfs.drivers_probe(); + fs::create_dir_all(probe.parent().unwrap()).unwrap(); + fs::write(&probe, "").unwrap(); + + // GPU already on vfio-pci + set_mock_driver(&sysfs, "0000:2d:00.0", "vfio-pci"); + + let guard = prepare_gpu_for_passthrough(&sysfs, "0000:2d:00.0").unwrap(); + assert!(guard.companion_bdfs.is_empty()); + } + + #[test] + fn test_bind_device_to_vfio_already_bound() { + let (tmp, sysfs) = setup_mock_sysfs(); + create_pci_device( + &sysfs, + tmp.path(), + "0000:2d:00.0", + "0x10de", + "0x2684", + "0x030000", + 42, + ); + + let probe = sysfs.drivers_probe(); + fs::create_dir_all(probe.parent().unwrap()).unwrap(); + fs::write(&probe, "").unwrap(); + + set_mock_driver(&sysfs, "0000:2d:00.0", "vfio-pci"); + + let was_bound = bind_device_to_vfio(&sysfs, "0000:2d:00.0").unwrap(); + assert!(!was_bound, "should report false when already on vfio-pci"); + } + + #[test] + fn test_guard_drop_restores_companions() { + clear_vfio_id_refcount("10de 2684"); + let (tmp, sysfs) = setup_mock_sysfs(); + create_pci_device( + &sysfs, + tmp.path(), + "0000:2d:00.0", + "0x10de", + "0x2684", + "0x030000", + 42, + ); + create_pci_device( + &sysfs, + tmp.path(), + "0000:2d:00.1", + "0x10de", + "0x228b", + "0x040300", + 42, + ); + + let probe = sysfs.drivers_probe(); + fs::create_dir_all(probe.parent().unwrap()).unwrap(); + fs::write(&probe, "").unwrap(); + + // Simulate bound state: driver link and driver_override both set + set_mock_driver(&sysfs, "0000:2d:00.0", "vfio-pci"); + set_mock_driver(&sysfs, "0000:2d:00.1", "vfio-pci"); + fs::write( + sysfs.pci_device("0000:2d:00.0").join("driver_override"), + "vfio-pci", + ) + .unwrap(); + fs::write( + sysfs.pci_device("0000:2d:00.1").join("driver_override"), + "vfio-pci", + ) + .unwrap(); + + { + let _guard = GpuBindGuard { + bdf: "0000:2d:00.0".to_string(), + companion_bdfs: vec!["0000:2d:00.1".to_string()], + sysfs: sysfs.clone(), + disarmed: false, + vfio_id: None, + }; + // guard drops here — should attempt restore on both devices + } + + // After drop, driver_override should be cleared (written with "\n") + let gpu_override = + fs::read_to_string(sysfs.pci_device("0000:2d:00.0").join("driver_override")).unwrap(); + assert_eq!( + gpu_override.trim(), + "", + "GPU driver_override should be cleared after drop" + ); + + let companion_override = + fs::read_to_string(sysfs.pci_device("0000:2d:00.1").join("driver_override")).unwrap(); + assert_eq!( + companion_override.trim(), + "", + "companion driver_override should be cleared after drop" + ); + } + + #[test] + fn test_guard_disarm_skips_restore() { + let (tmp, sysfs) = setup_mock_sysfs(); + create_pci_device( + &sysfs, + tmp.path(), + "0000:2d:00.0", + "0x10de", + "0x2684", + "0x030000", + 42, + ); + + // Write a non-empty driver_override to verify it's NOT cleared + fs::write( + sysfs.pci_device("0000:2d:00.0").join("driver_override"), + "vfio-pci", + ) + .unwrap(); + + let guard = GpuBindGuard { + bdf: "0000:2d:00.0".to_string(), + companion_bdfs: vec![], + sysfs: sysfs.clone(), + disarmed: false, + vfio_id: None, + }; + guard.disarm(); + + // driver_override should still be vfio-pci (not cleared by disarmed guard) + let override_val = + fs::read_to_string(sysfs.pci_device("0000:2d:00.0").join("driver_override")).unwrap(); + assert_eq!(override_val, "vfio-pci"); + } + + // -- restore writes remove_id ------------------------------------------- + + #[test] + fn test_restore_gpu_deregisters_new_id_before_reprobe() { + clear_vfio_id_refcount("10de 26b7"); + let (tmp, sysfs) = setup_mock_sysfs(); + create_pci_device( + &sysfs, + tmp.path(), + "0000:2d:00.0", + "0x10de", + "0x26b7", + "0x030000", + 42, + ); + + let probe = sysfs.drivers_probe(); + fs::create_dir_all(probe.parent().unwrap()).unwrap(); + fs::write(&probe, "").unwrap(); + + let remove_id_path = sysfs.vfio_pci_remove_id(); + fs::create_dir_all(remove_id_path.parent().unwrap()).unwrap(); + fs::write(&remove_id_path, "").unwrap(); + + set_mock_driver(&sysfs, "0000:2d:00.0", "vfio-pci"); + fs::write( + sysfs.pci_device("0000:2d:00.0").join("driver_override"), + "vfio-pci", + ) + .unwrap(); + + restore_gpu_to_host_driver(&sysfs, "0000:2d:00.0").unwrap(); + + let written = fs::read_to_string(&remove_id_path).unwrap(); + assert_eq!( + written, "10de 26b7", + "remove_id should be written during restore" + ); + } + + // -- reconcile_stale_bindings ------------------------------------------- + + #[test] + fn test_reconcile_clears_stale_driver_override_when_not_on_vfio() { + clear_vfio_id_refcount("10de 2684"); + let (tmp, sysfs) = setup_mock_sysfs(); + create_pci_device( + &sysfs, + tmp.path(), + "0000:2d:00.0", + "0x10de", + "0x2684", + "0x030000", + 42, + ); + + let probe = sysfs.drivers_probe(); + fs::create_dir_all(probe.parent().unwrap()).unwrap(); + fs::write(&probe, "").unwrap(); + + set_mock_driver(&sysfs, "0000:2d:00.0", "nvidia"); + fs::write( + sysfs.pci_device("0000:2d:00.0").join("driver_override"), + "vfio-pci", + ) + .unwrap(); + + let state_path = tmp.path().join("gpu-state.json"); + let state = GpuBindState { + bindings: vec![GpuBinding { + bdf: "0000:2d:00.0".to_string(), + sandbox_id: "sandbox-orphan".to_string(), + bound_at_ms: 0, + }], + }; + state.save(&state_path).unwrap(); + + let restored = reconcile_stale_bindings(&sysfs, &state_path); + assert!(restored.contains(&"0000:2d:00.0".to_string())); + + let override_val = + fs::read_to_string(sysfs.pci_device("0000:2d:00.0").join("driver_override")).unwrap(); + assert_eq!( + override_val.trim(), + "", + "driver_override should be cleared even when device is not on vfio-pci" + ); + } +} diff --git a/mise.lock b/mise.lock index d5d110bccb..2a6b7e02a7 100644 --- a/mise.lock +++ b/mise.lock @@ -35,16 +35,36 @@ checksum = "sha256:afe92510c467f952a009b994f2d998ff8f9dd266dc26eca55d14a0dd46fec url = "https://github.com/anchore/syft/releases/download/v1.43.0/syft_1.43.0_linux_arm64.tar.gz" url_api = "https://api.github.com/repos/anchore/syft/releases/assets/402658323" +[tools."github:anchore/syft"."platforms.linux-arm64-musl"] +checksum = "sha256:afe92510c467f952a009b994f2d998ff8f9dd266dc26eca55d14a0dd46fec7f2" +url = "https://github.com/anchore/syft/releases/download/v1.43.0/syft_1.43.0_linux_arm64.tar.gz" +url_api = "https://api.github.com/repos/anchore/syft/releases/assets/402658323" + [tools."github:anchore/syft"."platforms.linux-x64"] checksum = "sha256:7b98251d2d08926bb5d4639b56b1f0996a58ef6667c5830e3fe3cd3ad5f4214a" url = "https://github.com/anchore/syft/releases/download/v1.43.0/syft_1.43.0_linux_amd64.tar.gz" url_api = "https://api.github.com/repos/anchore/syft/releases/assets/402658325" +[tools."github:anchore/syft"."platforms.linux-x64-musl"] +checksum = "sha256:7b98251d2d08926bb5d4639b56b1f0996a58ef6667c5830e3fe3cd3ad5f4214a" +url = "https://github.com/anchore/syft/releases/download/v1.43.0/syft_1.43.0_linux_amd64.tar.gz" +url_api = "https://api.github.com/repos/anchore/syft/releases/assets/402658325" + [tools."github:anchore/syft"."platforms.macos-arm64"] checksum = "sha256:3640e2181c8be7a56377f3c96e520d5380c924dbafd115ee3c8d45fcbc89cac2" url = "https://github.com/anchore/syft/releases/download/v1.43.0/syft_1.43.0_darwin_arm64.tar.gz" url_api = "https://api.github.com/repos/anchore/syft/releases/assets/402658324" +[tools."github:anchore/syft"."platforms.macos-x64"] +checksum = "sha256:08fd18f55037f999f50b2c2256a9285f0146978a0b16cdc58662ecdc85d0e3c0" +url = "https://github.com/anchore/syft/releases/download/v1.43.0/syft_1.43.0_darwin_amd64.tar.gz" +url_api = "https://api.github.com/repos/anchore/syft/releases/assets/402658329" + +[tools."github:anchore/syft"."platforms.windows-x64"] +checksum = "sha256:c51695d171c61460369dabdd5c71b8f350ef8618466818356a30808d7105c710" +url = "https://github.com/anchore/syft/releases/download/v1.43.0/syft_1.43.0_windows_amd64.zip" +url_api = "https://api.github.com/repos/anchore/syft/releases/assets/402658321" + [[tools."github:mozilla/sccache"]] version = "0.14.0" backend = "github:mozilla/sccache" @@ -54,16 +74,36 @@ checksum = "sha256:62a6c942c47c93333bc0174704800cef7edfa0416d08e1356c1d3e39f0b46 url = "https://github.com/mozilla/sccache/releases/download/v0.14.0/sccache-v0.14.0-aarch64-unknown-linux-musl.tar.gz" url_api = "https://api.github.com/repos/mozilla/sccache/releases/assets/353136010" +[tools."github:mozilla/sccache"."platforms.linux-arm64-musl"] +checksum = "sha256:62a6c942c47c93333bc0174704800cef7edfa0416d08e1356c1d3e39f0b462f2" +url = "https://github.com/mozilla/sccache/releases/download/v0.14.0/sccache-v0.14.0-aarch64-unknown-linux-musl.tar.gz" +url_api = "https://api.github.com/repos/mozilla/sccache/releases/assets/353136010" + [tools."github:mozilla/sccache"."platforms.linux-x64"] checksum = "sha256:8424b38cda4ecce616a1557d81328f3d7c96503a171eab79942fad618b42af44" url = "https://github.com/mozilla/sccache/releases/download/v0.14.0/sccache-v0.14.0-x86_64-unknown-linux-musl.tar.gz" url_api = "https://api.github.com/repos/mozilla/sccache/releases/assets/353136108" +[tools."github:mozilla/sccache"."platforms.linux-x64-musl"] +checksum = "sha256:8424b38cda4ecce616a1557d81328f3d7c96503a171eab79942fad618b42af44" +url = "https://github.com/mozilla/sccache/releases/download/v0.14.0/sccache-v0.14.0-x86_64-unknown-linux-musl.tar.gz" +url_api = "https://api.github.com/repos/mozilla/sccache/releases/assets/353136108" + [tools."github:mozilla/sccache"."platforms.macos-arm64"] checksum = "sha256:a781e8018260ab128e7690d8497736fa231b6ca895d57131d5b5b966ca987594" url = "https://github.com/mozilla/sccache/releases/download/v0.14.0/sccache-v0.14.0-aarch64-apple-darwin.tar.gz" url_api = "https://api.github.com/repos/mozilla/sccache/releases/assets/353135984" +[tools."github:mozilla/sccache"."platforms.macos-x64"] +checksum = "sha256:f86c5ecf9b9a1aee53022601725c5cea0e1d9318d80a8233017101063936ab62" +url = "https://github.com/mozilla/sccache/releases/download/v0.14.0/sccache-v0.14.0-x86_64-apple-darwin.tar.gz" +url_api = "https://api.github.com/repos/mozilla/sccache/releases/assets/353136084" + +[tools."github:mozilla/sccache"."platforms.windows-x64"] +checksum = "sha256:74a3ffd4207e8e0e62af7747bd03b42deab0f6dabc7ef0a8cdd950f83f1037c8" +url = "https://github.com/mozilla/sccache/releases/download/v0.14.0/sccache-v0.14.0-x86_64-pc-windows-msvc.zip" +url_api = "https://api.github.com/repos/mozilla/sccache/releases/assets/353136140" + [[tools.helm]] version = "4.1.4" backend = "aqua:helm/helm" @@ -88,14 +128,30 @@ backend = "aqua:kubernetes/kubernetes/kubectl" checksum = "sha256:6a5a4cc4e396d7626a7a693a3044b51c75520f81db30fe6816c2554e53be336f" url = "https://dl.k8s.io/v1.35.4/bin/linux/arm64/kubectl" +[tools.kubectl."platforms.linux-arm64-musl"] +checksum = "sha256:6a5a4cc4e396d7626a7a693a3044b51c75520f81db30fe6816c2554e53be336f" +url = "https://dl.k8s.io/v1.35.4/bin/linux/arm64/kubectl" + [tools.kubectl."platforms.linux-x64"] checksum = "sha256:b529430df69a688fd61b64ad2299edb5fd71cb58be2a4779dba624c7d3510efd" url = "https://dl.k8s.io/v1.35.4/bin/linux/amd64/kubectl" +[tools.kubectl."platforms.linux-x64-musl"] +checksum = "sha256:b529430df69a688fd61b64ad2299edb5fd71cb58be2a4779dba624c7d3510efd" +url = "https://dl.k8s.io/v1.35.4/bin/linux/amd64/kubectl" + [tools.kubectl."platforms.macos-arm64"] checksum = "sha256:ec644a2473b64b486987f695dfb1867963ce6d42d267b86e944585a546f92b5d" url = "https://dl.k8s.io/v1.35.4/bin/darwin/arm64/kubectl" +[tools.kubectl."platforms.macos-x64"] +checksum = "sha256:dddb01bddb96f78e48e33105ccfa2feedff585a8b2e3b812f5d0f64c7403710a" +url = "https://dl.k8s.io/v1.35.4/bin/darwin/amd64/kubectl" + +[tools.kubectl."platforms.windows-x64"] +checksum = "sha256:d77d03309bd80de56dafe8ca59ff6f2076e2ed4ee61c6a94657a4b6e945210e6" +url = "https://dl.k8s.io/v1.35.4/bin/windows/amd64/kubectl.exe" + [[tools.node]] version = "24.15.0" backend = "core:node" @@ -104,14 +160,30 @@ backend = "core:node" checksum = "sha256:73afc234d558c24919875f51c2d1ea002a2ada4ea6f83601a383869fefa64eed" url = "https://nodejs.org/dist/v24.15.0/node-v24.15.0-linux-arm64.tar.gz" +[tools.node."platforms.linux-arm64-musl"] +checksum = "sha256:73afc234d558c24919875f51c2d1ea002a2ada4ea6f83601a383869fefa64eed" +url = "https://nodejs.org/dist/v24.15.0/node-v24.15.0-linux-arm64.tar.gz" + [tools.node."platforms.linux-x64"] checksum = "sha256:44836872d9aec49f1e6b52a9a922872db9a2b02d235a616a5681b6a85fec8d89" url = "https://nodejs.org/dist/v24.15.0/node-v24.15.0-linux-x64.tar.gz" +[tools.node."platforms.linux-x64-musl"] +checksum = "sha256:44836872d9aec49f1e6b52a9a922872db9a2b02d235a616a5681b6a85fec8d89" +url = "https://nodejs.org/dist/v24.15.0/node-v24.15.0-linux-x64.tar.gz" + [tools.node."platforms.macos-arm64"] checksum = "sha256:372331b969779ab5d15b949884fc6eaf88d5afe87bde8ba881d6400b9100ffc4" url = "https://nodejs.org/dist/v24.15.0/node-v24.15.0-darwin-arm64.tar.gz" +[tools.node."platforms.macos-x64"] +checksum = "sha256:ffd5ee293467927f3ee731a553eb88fd1f48cf74eebc2d74a6babe4af228673b" +url = "https://nodejs.org/dist/v24.15.0/node-v24.15.0-darwin-x64.tar.gz" + +[tools.node."platforms.windows-x64"] +checksum = "sha256:cc5149eabd53779ce1e7bdc5401643622d0c7e6800ade18928a767e940bb0e62" +url = "https://nodejs.org/dist/v24.15.0/node-v24.15.0-win-x64.zip" + [[tools."npm:markdownlint-cli2"]] version = "0.22.0" backend = "npm:markdownlint-cli2" @@ -144,16 +216,36 @@ checksum = "sha256:0556f1260a9a1fc83210dcecf9d4cbacf17eb4a684541c84798ffc8b4d618 url = "https://github.com/astral-sh/python-build-standalone/releases/download/20260414/cpython-3.13.13+20260414-aarch64-unknown-linux-gnu-install_only_stripped.tar.gz" provenance = "github-attestations" +[tools.python."platforms.linux-arm64-musl"] +checksum = "sha256:0556f1260a9a1fc83210dcecf9d4cbacf17eb4a684541c84798ffc8b4d618c35" +url = "https://github.com/astral-sh/python-build-standalone/releases/download/20260414/cpython-3.13.13+20260414-aarch64-unknown-linux-gnu-install_only_stripped.tar.gz" +provenance = "github-attestations" + [tools.python."platforms.linux-x64"] checksum = "sha256:13d3b6d15f4c3c1dd1955a3c81e06bdc5aef4cb5cb65076878374948be3b0412" url = "https://github.com/astral-sh/python-build-standalone/releases/download/20260414/cpython-3.13.13+20260414-x86_64-unknown-linux-gnu-install_only_stripped.tar.gz" provenance = "github-attestations" +[tools.python."platforms.linux-x64-musl"] +checksum = "sha256:13d3b6d15f4c3c1dd1955a3c81e06bdc5aef4cb5cb65076878374948be3b0412" +url = "https://github.com/astral-sh/python-build-standalone/releases/download/20260414/cpython-3.13.13+20260414-x86_64-unknown-linux-gnu-install_only_stripped.tar.gz" +provenance = "github-attestations" + [tools.python."platforms.macos-arm64"] checksum = "sha256:874f9931ad40dcce38caf6f408aa7e10ec3d0dfce2184ba7af62a965a66b9cd9" url = "https://github.com/astral-sh/python-build-standalone/releases/download/20260414/cpython-3.13.13+20260414-aarch64-apple-darwin-install_only_stripped.tar.gz" provenance = "github-attestations" +[tools.python."platforms.macos-x64"] +checksum = "sha256:d34198cd856fa80ebf3aa821fe329a25fab66eeda44f72ac9576591282e31bb7" +url = "https://github.com/astral-sh/python-build-standalone/releases/download/20260414/cpython-3.13.13+20260414-x86_64-apple-darwin-install_only_stripped.tar.gz" +provenance = "github-attestations" + +[tools.python."platforms.windows-x64"] +checksum = "sha256:b84dce293464cfd366ee792a3d5b42abe5174fc9cce733ba895b3ef467cb3161" +url = "https://github.com/astral-sh/python-build-standalone/releases/download/20260414/cpython-3.13.13+20260414-x86_64-pc-windows-msvc-install_only_stripped.tar.gz" +provenance = "github-attestations" + [[tools.rust]] version = "stable" backend = "core:rust" @@ -167,16 +259,36 @@ checksum = "sha256:55bd1c1c10ec8b95a8c184f5e18b566703c6ab105f0fc118aaa4d748aabf2 url = "https://github.com/astral-sh/uv/releases/download/0.10.12/uv-aarch64-unknown-linux-musl.tar.gz" provenance = "github-attestations" +[tools.uv."platforms.linux-arm64-musl"] +checksum = "sha256:55bd1c1c10ec8b95a8c184f5e18b566703c6ab105f0fc118aaa4d748aabf28e4" +url = "https://github.com/astral-sh/uv/releases/download/0.10.12/uv-aarch64-unknown-linux-musl.tar.gz" +provenance = "github-attestations" + [tools.uv."platforms.linux-x64"] checksum = "sha256:adccf40b5d1939a5e0093081ec2307ea24235adf7c2d96b122c561fa37711c46" url = "https://github.com/astral-sh/uv/releases/download/0.10.12/uv-x86_64-unknown-linux-musl.tar.gz" provenance = "github-attestations" +[tools.uv."platforms.linux-x64-musl"] +checksum = "sha256:adccf40b5d1939a5e0093081ec2307ea24235adf7c2d96b122c561fa37711c46" +url = "https://github.com/astral-sh/uv/releases/download/0.10.12/uv-x86_64-unknown-linux-musl.tar.gz" +provenance = "github-attestations" + [tools.uv."platforms.macos-arm64"] checksum = "sha256:ae738b5661a900579ec621d3918c0ef17bdec0da2a8a6d8b161137cd15f25414" url = "https://github.com/astral-sh/uv/releases/download/0.10.12/uv-aarch64-apple-darwin.tar.gz" provenance = "github-attestations" +[tools.uv."platforms.macos-x64"] +checksum = "sha256:17443e293f2ae407bb2d8d34b875ebfe0ae01cf1296de5647e69e7b2e2b428f0" +url = "https://github.com/astral-sh/uv/releases/download/0.10.12/uv-x86_64-apple-darwin.tar.gz" +provenance = "github-attestations" + +[tools.uv."platforms.windows-x64"] +checksum = "sha256:4c1d55501869b3330d4aabf45ad6024ce2367e0f3af83344395702d272c22e88" +url = "https://github.com/astral-sh/uv/releases/download/0.10.12/uv-x86_64-pc-windows-msvc.zip" +provenance = "github-attestations" + [[tools.zig]] version = "0.14.1" backend = "core:zig" diff --git a/proto/compute_driver.proto b/proto/compute_driver.proto index 68af695e51..3c4308f3f0 100644 --- a/proto/compute_driver.proto +++ b/proto/compute_driver.proto @@ -53,6 +53,8 @@ message GetCapabilitiesResponse { string default_image = 3; // True when the driver can provision GPU-backed sandboxes. bool supports_gpu = 4; + // Number of GPUs available for sandbox assignment. + uint32 gpu_count = 5; } // Driver-owned sandbox model used for create requests and platform observations. @@ -84,6 +86,10 @@ message DriverSandboxSpec { DriverSandboxTemplate template = 6; // Request NVIDIA GPU resources for this sandbox. bool gpu = 9; + // Optional PCI BDF address (e.g. "0000:2d:00.0") or device index + // (e.g. "0", "1"). When empty with gpu=true, the driver assigns the + // first available GPU. + string gpu_device = 10; } // Driver-owned runtime template consumed by the compute platform. diff --git a/proto/openshell.proto b/proto/openshell.proto index 1d7eba2186..75490f3385 100644 --- a/proto/openshell.proto +++ b/proto/openshell.proto @@ -204,6 +204,10 @@ message SandboxSpec { repeated string providers = 8; // Request NVIDIA GPU resources for this sandbox. bool gpu = 9; + // Optional PCI BDF address (e.g. "0000:2d:00.0") or device index + // (e.g. "0", "1"). When empty with gpu=true, the driver assigns the + // first available GPU. + string gpu_device = 10; } // Public sandbox template mapped onto compute-driver template inputs. diff --git a/tasks/scripts/vm/build-rootfs-tarball.sh b/tasks/scripts/vm/build-rootfs-tarball.sh index 57b215aad4..87abca27ed 100755 --- a/tasks/scripts/vm/build-rootfs-tarball.sh +++ b/tasks/scripts/vm/build-rootfs-tarball.sh @@ -9,15 +9,20 @@ # 2. Compresses it to a zstd tarball for embedding # # Usage: -# ./build-rootfs-tarball.sh [--base] +# ./build-rootfs-tarball.sh [--base|--gpu|--gpu-cuda] # # Options: # --base Build a base rootfs (~200-300MB) without pre-loaded images. # First boot will be slower but binary size is much smaller. # Default: full rootfs with pre-loaded images (~2GB+). +# --gpu Build a GPU-augmented rootfs that layers kmod, nvidia kernel +# modules, and nvidia firmware on top of the base rootfs. +# Output: target/vm-runtime-compressed/rootfs-gpu.tar.zst +# --gpu-cuda Like --gpu but also includes CUDA driver libraries +# (libcuda.so, libnvidia-ptxjitcompiler.so) for CUDA workloads. # # The resulting tarball is placed at target/vm-runtime-compressed/rootfs.tar.zst -# for inclusion in the embedded binary build. +# (or rootfs-gpu.tar.zst for --gpu) for inclusion in the embedded binary build. set -euo pipefail @@ -28,19 +33,36 @@ ROOTFS_BUILD_DIR="${ROOT}/target/rootfs-build" OUTPUT_DIR="${ROOT}/target/vm-runtime-compressed" OUTPUT="${OUTPUT_DIR}/rootfs.tar.zst" +KERNEL_VERSION="6.12.76" +NVIDIA_MODULES_DIR="${ROOT}/target/libkrun-build/nvidia-modules" +NVIDIA_USERSPACE_DIR="${ROOT}/target/libkrun-build/nvidia-userspace" + # Parse arguments BASE_ONLY=false +GPU_BUILD=false +GPU_CUDA=false for arg in "$@"; do case "$arg" in --base) BASE_ONLY=true ;; + --gpu) + GPU_BUILD=true + ;; + --gpu-cuda) + GPU_CUDA=true + GPU_BUILD=true + ;; --help|-h) - echo "Usage: $0 [--base]" + echo "Usage: $0 [--base|--gpu|--gpu-cuda]" echo "" echo "Options:" - echo " --base Build base rootfs (~200-300MB) without pre-loaded images" - echo " First boot will be slower but binary size is much smaller" + echo " --base Build base rootfs (~200-300MB) without pre-loaded images" + echo " First boot will be slower but binary size is much smaller" + echo " --gpu Build GPU rootfs with kmod, nvidia modules, and firmware" + echo " Layers on top of base rootfs, output: rootfs-gpu.tar.zst" + echo " --gpu-cuda Like --gpu but also includes CUDA driver libraries" + echo " (libcuda.so, libnvidia-ptxjitcompiler.so)" exit 0 ;; *) @@ -51,6 +73,286 @@ for arg in "$@"; do esac done +if [ "$GPU_BUILD" = true ]; then + GPU_OUTPUT="${OUTPUT_DIR}/rootfs-gpu.tar.zst" + GPU_ROOTFS_DIR="${ROOT}/target/rootfs-gpu-build" + trap 'echo "ERROR: GPU rootfs build failed; cleaning up ${GPU_ROOTFS_DIR}" >&2; rm -rf "${GPU_ROOTFS_DIR}"' ERR + + echo "==> Building GPU rootfs for embedding" + echo " Build dir: ${GPU_ROOTFS_DIR}" + echo " Output: ${GPU_OUTPUT}" + echo "" + + # Build base rootfs first if it doesn't exist + if [ ! -d "${ROOTFS_BUILD_DIR}" ]; then + echo "==> Step 1/3: Base rootfs not found, building it first..." + "${ROOT}/crates/openshell-vm/scripts/build-rootfs.sh" --base "${ROOTFS_BUILD_DIR}" + echo "" + fi + + echo "==> Step 2/3: Layering GPU tools onto base rootfs..." + + rm -rf "${GPU_ROOTFS_DIR}" + cp -a "${ROOTFS_BUILD_DIR}" "${GPU_ROOTFS_DIR}" + + # --- kmod --- + KMOD_BIN="$(command -v kmod 2>/dev/null || true)" + if [ -z "${KMOD_BIN}" ]; then + echo "WARNING: kmod not found on host; skipping kmod installation" + else + echo " Installing kmod from ${KMOD_BIN}" + mkdir -p "${GPU_ROOTFS_DIR}/bin" + cp "${KMOD_BIN}" "${GPU_ROOTFS_DIR}/bin/kmod" + chmod 755 "${GPU_ROOTFS_DIR}/bin/kmod" + + # Copy shared libraries required by kmod (host and guest must share compatible glibc) + if command -v ldd &>/dev/null; then + mkdir -p "${GPU_ROOTFS_DIR}/lib" "${GPU_ROOTFS_DIR}/lib64" + ldd "${KMOD_BIN}" 2>/dev/null | while read -r line; do + lib_path="$(echo "${line}" | sed -n 's/.* => \(\/[^ ]*\).*/\1/p')" + if [ -n "${lib_path}" ] && [ -f "${lib_path}" ]; then + # Skip core system libraries that already exist in the base rootfs. + # The host glibc may be older and overwriting breaks rootfs binaries. + lib_basename="$(basename "${lib_path}")" + case "${lib_basename}" in + libc.so*|libm.so*|libpthread.so*|libdl.so*|librt.so*|ld-linux*) continue ;; + esac + lib_dir="$(dirname "${lib_path}")" + mkdir -p "${GPU_ROOTFS_DIR}${lib_dir}" + cp -Lf "${lib_path}" "${GPU_ROOTFS_DIR}${lib_path}" 2>/dev/null || true + fi + done + fi + + # Fix broken .so symlinks left by Docker export (e.g. libzstd.so.1.5.5 -> itself). + # These cause ELOOP when the dynamic linker resolves the SONAME chain. + # Use -xtype l to find symlinks whose targets are missing or circular. + find "${GPU_ROOTFS_DIR}" -xtype l -name '*.so*' 2>/dev/null | while read -r broken; do + sobase="$(basename "$broken" | sed 's/\.so.*/\.so/')" + host_real="$(find /usr/lib /lib -name "${sobase}*" -type f 2>/dev/null | head -1)" + if [ -n "$host_real" ]; then + rm -f "$broken" + cp -L "$host_real" "$broken" 2>/dev/null || true + fi + done || true + + mkdir -p "${GPU_ROOTFS_DIR}/usr/sbin" + for tool in modprobe insmod rmmod lsmod depmod; do + ln -sf ../../bin/kmod "${GPU_ROOTFS_DIR}/usr/sbin/${tool}" + done + echo " Created symlinks: modprobe insmod rmmod lsmod depmod -> ../../bin/kmod" + fi + + # --- nvidia kernel modules --- + MODULES_DST="${GPU_ROOTFS_DIR}/lib/modules/${KERNEL_VERSION}/kernel/drivers/video" + if [ -d "${NVIDIA_MODULES_DIR}" ]; then + ko_files=("${NVIDIA_MODULES_DIR}"/*.ko) + if [ -e "${ko_files[0]}" ]; then + mkdir -p "${MODULES_DST}" + cp "${NVIDIA_MODULES_DIR}"/*.ko "${MODULES_DST}/" + echo " Installed nvidia kernel modules into lib/modules/${KERNEL_VERSION}/kernel/drivers/video/" + ls -1 "${MODULES_DST}"/*.ko | xargs -I{} basename {} | sed 's/^/ /' + if command -v depmod &>/dev/null; then + depmod -b "${GPU_ROOTFS_DIR}" "${KERNEL_VERSION}" 2>/dev/null || true + echo " Generated modules.dep" + fi + else + echo "WARNING: ${NVIDIA_MODULES_DIR} exists but contains no .ko files" + fi + else + echo "WARNING: nvidia kernel modules not found at ${NVIDIA_MODULES_DIR}" + echo " GPU rootfs will not contain nvidia drivers" + fi + + # Determine the kernel module driver version so we can match firmware + userspace. + NV_DRIVER_VERSION="" + if command -v modinfo &>/dev/null && [ -f "${NVIDIA_MODULES_DIR}/nvidia.ko" ]; then + NV_DRIVER_VERSION="$(modinfo -F version "${NVIDIA_MODULES_DIR}/nvidia.ko" 2>/dev/null || true)" + fi + if [ -n "${NV_DRIVER_VERSION}" ]; then + echo " Kernel module driver version: ${NV_DRIVER_VERSION}" + fi + + # --- nvidia firmware (GSP) --- + # Prefer version-matched firmware from nvidia-firmware/ directory. + # Fall back to host /lib/firmware/nvidia if version-matched is unavailable. + rm -rf "${GPU_ROOTFS_DIR}/lib/firmware/nvidia" 2>/dev/null || true + NVIDIA_FW_MATCHED_DIR="${ROOT}/target/libkrun-build/nvidia-firmware/${NV_DRIVER_VERSION}" + FW_DST="${GPU_ROOTFS_DIR}/lib/firmware/nvidia/${NV_DRIVER_VERSION}" + if [ -n "${NV_DRIVER_VERSION}" ] && [ -d "${NVIDIA_FW_MATCHED_DIR}" ]; then + mkdir -p "${FW_DST}" + cp "${NVIDIA_FW_MATCHED_DIR}"/*.bin "${FW_DST}/" 2>/dev/null || true + echo " Installed nvidia firmware from ${NVIDIA_FW_MATCHED_DIR} (version-matched)" + else + HOST_FW_DIR="" + for candidate in /lib/firmware/nvidia /usr/lib/firmware/nvidia; do + if [ -d "${candidate}" ]; then + HOST_FW_DIR="${candidate}" + break + fi + done + if [ -n "${HOST_FW_DIR}" ]; then + mkdir -p "${GPU_ROOTFS_DIR}/lib/firmware/nvidia" + cp -r "${HOST_FW_DIR}"/* "${GPU_ROOTFS_DIR}/lib/firmware/nvidia/" 2>/dev/null || true + echo " Installed nvidia firmware from ${HOST_FW_DIR}" + if [ -n "${NV_DRIVER_VERSION}" ]; then + echo " WARNING: host firmware version may not match kernel module version ${NV_DRIVER_VERSION}" + fi + else + echo "WARNING: nvidia firmware not found" + echo " GPU guests may fail to initialize the GPU without GSP firmware" + fi + fi + + # --- nvidia userspace (nvidia-smi + NVML) --- + + # Remove any pre-existing nvidia userspace from the base rootfs to avoid + # version conflicts. The base image may ship nvidia-smi and libs from a + # different driver version than the kernel modules we're installing. + for search_dir in "${GPU_ROOTFS_DIR}/usr/lib/x86_64-linux-gnu" \ + "${GPU_ROOTFS_DIR}/usr/lib64" \ + "${GPU_ROOTFS_DIR}/usr/lib"; do + rm -f "${search_dir}"/libnvidia-ml.so* 2>/dev/null || true + rm -f "${search_dir}"/libcuda.so* 2>/dev/null || true + rm -f "${search_dir}"/libnvidia-ptxjitcompiler.so* 2>/dev/null || true + done + rm -f "${GPU_ROOTFS_DIR}/usr/bin/nvidia-smi" 2>/dev/null || true + echo " Cleaned pre-existing nvidia userspace from base rootfs" + + # Prefer pre-extracted version-matched userspace from nvidia-userspace/. + # Fall back to host binaries only if the pre-extracted ones don't exist. + if [ -f "${NVIDIA_USERSPACE_DIR}/nvidia-smi" ]; then + mkdir -p "${GPU_ROOTFS_DIR}/usr/bin" + cp "${NVIDIA_USERSPACE_DIR}/nvidia-smi" "${GPU_ROOTFS_DIR}/usr/bin/nvidia-smi" + chmod 755 "${GPU_ROOTFS_DIR}/usr/bin/nvidia-smi" + echo " Installed nvidia-smi from ${NVIDIA_USERSPACE_DIR} (version-matched)" + else + NV_SMI="$(command -v nvidia-smi 2>/dev/null || true)" + if [ -n "${NV_SMI}" ]; then + mkdir -p "${GPU_ROOTFS_DIR}/usr/bin" + cp "${NV_SMI}" "${GPU_ROOTFS_DIR}/usr/bin/nvidia-smi" + chmod 755 "${GPU_ROOTFS_DIR}/usr/bin/nvidia-smi" + echo " Installed nvidia-smi from host: ${NV_SMI}" + echo " WARNING: host nvidia-smi version may not match kernel module version ${NV_DRIVER_VERSION}" + else + echo "WARNING: nvidia-smi not found; GPU rootfs will use mknod fallback" + fi + fi + + # libnvidia-ml.so — required by nvidia-smi (dlopen'd at runtime) + if [ -f "${NVIDIA_USERSPACE_DIR}/libnvidia-ml.so.${NV_DRIVER_VERSION}" ]; then + NV_ML_REAL="${NVIDIA_USERSPACE_DIR}/libnvidia-ml.so.${NV_DRIVER_VERSION}" + NV_LIB_DEST="${GPU_ROOTFS_DIR}/usr/lib/x86_64-linux-gnu" + mkdir -p "${NV_LIB_DEST}" + cp "${NV_ML_REAL}" "${NV_LIB_DEST}/libnvidia-ml.so.${NV_DRIVER_VERSION}" + ln -sf "libnvidia-ml.so.${NV_DRIVER_VERSION}" "${NV_LIB_DEST}/libnvidia-ml.so.1" + ln -sf libnvidia-ml.so.1 "${NV_LIB_DEST}/libnvidia-ml.so" + echo " Installed libnvidia-ml.so.${NV_DRIVER_VERSION} (version-matched)" + else + NV_ML_REAL="" + for search_dir in /usr/lib/x86_64-linux-gnu /usr/lib64 /usr/lib; do + NV_ML_REAL="$(find "${search_dir}" -maxdepth 1 -name 'libnvidia-ml.so.*.*.*' -type f 2>/dev/null | head -1)" + [ -n "${NV_ML_REAL}" ] && break + done + if [ -n "${NV_ML_REAL}" ]; then + NV_LIB_DIR="$(dirname "${NV_ML_REAL}")" + mkdir -p "${GPU_ROOTFS_DIR}${NV_LIB_DIR}" + cp "${NV_ML_REAL}" "${GPU_ROOTFS_DIR}${NV_ML_REAL}" + ln -sf "$(basename "${NV_ML_REAL}")" "${GPU_ROOTFS_DIR}${NV_LIB_DIR}/libnvidia-ml.so.1" + ln -sf libnvidia-ml.so.1 "${GPU_ROOTFS_DIR}${NV_LIB_DIR}/libnvidia-ml.so" + echo " Installed libnvidia-ml.so from host: ${NV_ML_REAL}" + echo " WARNING: host library version may not match kernel module version ${NV_DRIVER_VERSION}" + else + echo "WARNING: libnvidia-ml.so not found; nvidia-smi may not work at runtime" + fi + fi + + # --- CUDA driver libraries (optional, via --gpu-cuda) --- + if [ "${GPU_CUDA}" = true ]; then + echo " Installing CUDA driver libraries..." + + # libcuda.so + if [ -f "${NVIDIA_USERSPACE_DIR}/libcuda.so.${NV_DRIVER_VERSION}" ]; then + NV_LIB_DEST="${GPU_ROOTFS_DIR}/usr/lib/x86_64-linux-gnu" + mkdir -p "${NV_LIB_DEST}" + cp "${NVIDIA_USERSPACE_DIR}/libcuda.so.${NV_DRIVER_VERSION}" "${NV_LIB_DEST}/" + ln -sf "libcuda.so.${NV_DRIVER_VERSION}" "${NV_LIB_DEST}/libcuda.so.1" + ln -sf libcuda.so.1 "${NV_LIB_DEST}/libcuda.so" + echo " Installed libcuda.so.${NV_DRIVER_VERSION} (version-matched)" + else + CUDA_REAL="" + for search_dir in /usr/lib/x86_64-linux-gnu /usr/lib64 /usr/lib; do + CUDA_REAL="$(find "${search_dir}" -maxdepth 1 -name 'libcuda.so.*.*.*' -type f 2>/dev/null | head -1)" + [ -n "${CUDA_REAL}" ] && break + done + if [ -n "${CUDA_REAL}" ]; then + CUDA_LIB_DIR="$(dirname "${CUDA_REAL}")" + mkdir -p "${GPU_ROOTFS_DIR}${CUDA_LIB_DIR}" + cp "${CUDA_REAL}" "${GPU_ROOTFS_DIR}${CUDA_REAL}" + ln -sf "$(basename "${CUDA_REAL}")" "${GPU_ROOTFS_DIR}${CUDA_LIB_DIR}/libcuda.so.1" + ln -sf libcuda.so.1 "${GPU_ROOTFS_DIR}${CUDA_LIB_DIR}/libcuda.so" + echo " Installed libcuda.so from host: ${CUDA_REAL}" + echo " WARNING: host library version may not match kernel module version ${NV_DRIVER_VERSION}" + else + echo "WARNING: libcuda.so not found; CUDA workloads will not work" + fi + fi + + # libnvidia-ptxjitcompiler.so + if [ -f "${NVIDIA_USERSPACE_DIR}/libnvidia-ptxjitcompiler.so.${NV_DRIVER_VERSION}" ]; then + NV_LIB_DEST="${GPU_ROOTFS_DIR}/usr/lib/x86_64-linux-gnu" + mkdir -p "${NV_LIB_DEST}" + cp "${NVIDIA_USERSPACE_DIR}/libnvidia-ptxjitcompiler.so.${NV_DRIVER_VERSION}" "${NV_LIB_DEST}/" + ln -sf "libnvidia-ptxjitcompiler.so.${NV_DRIVER_VERSION}" "${NV_LIB_DEST}/libnvidia-ptxjitcompiler.so.1" + ln -sf libnvidia-ptxjitcompiler.so.1 "${NV_LIB_DEST}/libnvidia-ptxjitcompiler.so" + echo " Installed libnvidia-ptxjitcompiler.so.${NV_DRIVER_VERSION} (version-matched)" + else + PTX_REAL="" + for search_dir in /usr/lib/x86_64-linux-gnu /usr/lib64 /usr/lib; do + PTX_REAL="$(find "${search_dir}" -maxdepth 1 -name 'libnvidia-ptxjitcompiler.so.*.*.*' -type f 2>/dev/null | head -1)" + [ -n "${PTX_REAL}" ] && break + done + if [ -n "${PTX_REAL}" ]; then + PTX_LIB_DIR="$(dirname "${PTX_REAL}")" + mkdir -p "${GPU_ROOTFS_DIR}${PTX_LIB_DIR}" + cp "${PTX_REAL}" "${GPU_ROOTFS_DIR}${PTX_REAL}" + ln -sf "$(basename "${PTX_REAL}")" "${GPU_ROOTFS_DIR}${PTX_LIB_DIR}/libnvidia-ptxjitcompiler.so.1" + ln -sf libnvidia-ptxjitcompiler.so.1 "${GPU_ROOTFS_DIR}${PTX_LIB_DIR}/libnvidia-ptxjitcompiler.so" + echo " Installed libnvidia-ptxjitcompiler.so from host: ${PTX_REAL}" + echo " WARNING: host library version may not match kernel module version ${NV_DRIVER_VERSION}" + else + echo "WARNING: libnvidia-ptxjitcompiler.so not found; PTX JIT may not work" + fi + fi + fi + + # Ensure nvidia library path is in ld.so.conf for dlopen resolution + mkdir -p "${GPU_ROOTFS_DIR}/etc/ld.so.conf.d" + echo "/usr/lib/x86_64-linux-gnu" > "${GPU_ROOTFS_DIR}/etc/ld.so.conf.d/nvidia.conf" + if command -v ldconfig &>/dev/null; then + ldconfig -r "${GPU_ROOTFS_DIR}" 2>/dev/null || true + fi + + echo "" + echo "==> Step 3/3: Compressing GPU rootfs to tarball..." + mkdir -p "${OUTPUT_DIR}" + rm -f "${GPU_OUTPUT}" + + echo " Uncompressed size: $(du -sh "${GPU_ROOTFS_DIR}" | cut -f1)" + echo " Compressing with zstd (level 3)..." + tar -C "${GPU_ROOTFS_DIR}" -cf - . | zstd -3 -T0 -o "${GPU_OUTPUT}" + + echo "" + echo "==> GPU rootfs tarball created successfully!" + echo " Output: ${GPU_OUTPUT}" + echo " Compressed: $(du -sh "${GPU_OUTPUT}" | cut -f1)" + echo " Type: gpu (kmod + nvidia modules + firmware)" + echo "" + echo "Next step: mise run vm:build" + trap - ERR + exit 0 +fi + # Check if container engine is running if ! ce info &>/dev/null; then echo "Error: container engine is not running" >&2 @@ -65,7 +367,6 @@ if [ "$BASE_ONLY" = true ]; then echo " Mode: base (no pre-loaded images, ~200-300MB)" echo "" - # Build base rootfs echo "==> Step 1/2: Building base rootfs..." "${ROOT}/crates/openshell-vm/scripts/build-rootfs.sh" --base "${ROOTFS_BUILD_DIR}" else @@ -75,29 +376,23 @@ else echo " Mode: full (pre-loaded images, pre-initialized, ~2GB+)" echo "" - # Build full rootfs echo "==> Step 1/2: Building full rootfs (this may take 10-15 minutes)..." "${ROOT}/crates/openshell-vm/scripts/build-rootfs.sh" "${ROOTFS_BUILD_DIR}" fi -# Compress to tarball echo "" echo "==> Step 2/2: Compressing rootfs to tarball..." mkdir -p "${OUTPUT_DIR}" -# Remove existing tarball if present rm -f "${OUTPUT}" -# Get uncompressed size for display echo " Uncompressed size: $(du -sh "${ROOTFS_BUILD_DIR}" | cut -f1)" -# Create tarball with zstd compression # -19 = high compression (slower but smaller) # -T0 = use all available threads echo " Compressing with zstd (level 19, this may take a few minutes)..." tar -C "${ROOTFS_BUILD_DIR}" -cf - . | zstd -19 -T0 -o "${OUTPUT}" -# Report results echo "" echo "==> Rootfs tarball created successfully!" echo " Output: ${OUTPUT}" diff --git a/tasks/scripts/vm/compress-vm-runtime.sh b/tasks/scripts/vm/compress-vm-runtime.sh index efada8a2e0..db5fbbd5b8 100755 --- a/tasks/scripts/vm/compress-vm-runtime.sh +++ b/tasks/scripts/vm/compress-vm-runtime.sh @@ -90,9 +90,9 @@ if [ -z "${VM_RUNTIME_TARBALL:-}" ] && _check_compressed_artifacts "$OUTPUT_DIR" mkdir -p "$WORK_DIR" for f in "${OUTPUT_DIR}"/*.zst; do [ -f "$f" ] || continue + [ -s "$f" ] || continue name="$(basename "${f%.zst}")" - # Skip rootfs tarball — bundle-vm-runtime.sh doesn't need it - [[ "$name" == rootfs.tar ]] && continue + [[ "$name" == rootfs*.tar ]] && continue zstd -d "$f" -o "${WORK_DIR}/${name}" -f -q chmod 0755 "${WORK_DIR}/${name}" done diff --git a/tasks/vm.toml b/tasks/vm.toml index 0a44b4ff72..2549f230f0 100644 --- a/tasks/vm.toml +++ b/tasks/vm.toml @@ -17,7 +17,7 @@ # ═══════════════════════════════════════════════════════════════════════════ ["gateway:vm"] -description = "Build openshell-gateway + openshell-driver-vm and start the gateway with the VM driver" +description = "Build openshell-gateway + openshell-driver-vm and start the gateway with the VM driver (pass -- --gpu for GPU support)" run = "crates/openshell-driver-vm/start.sh" [vm] From 24724742a89da8a61bd948921c9cfd6abff7f289 Mon Sep 17 00:00:00 2001 From: Drew Newberry Date: Wed, 29 Apr 2026 12:12:53 -0700 Subject: [PATCH 4/4] ci(rust): enforce -D warnings on clippy (#1008) --- crates/openshell-bootstrap/src/docker.rs | 69 ++-- crates/openshell-bootstrap/src/lib.rs | 17 +- crates/openshell-bootstrap/src/metadata.rs | 11 +- crates/openshell-cli/src/auth.rs | 11 +- crates/openshell-cli/src/bootstrap.rs | 2 +- crates/openshell-cli/src/completers.rs | 10 +- crates/openshell-cli/src/main.rs | 34 +- crates/openshell-cli/src/policy_update.rs | 16 +- crates/openshell-cli/src/run.rs | 242 ++++++------ crates/openshell-cli/src/ssh.rs | 81 ++-- .../tests/ensure_providers_integration.rs | 25 +- .../openshell-cli/tests/mtls_integration.rs | 9 +- .../tests/provider_commands_integration.rs | 25 +- .../sandbox_create_lifecycle_integration.rs | 9 +- .../sandbox_name_fallback_integration.rs | 9 +- crates/openshell-core/src/config.rs | 4 +- crates/openshell-core/src/error.rs | 6 +- crates/openshell-core/src/forward.rs | 18 +- crates/openshell-core/src/inference.rs | 10 +- crates/openshell-core/src/lib.rs | 2 +- crates/openshell-core/src/metadata.rs | 30 +- crates/openshell-core/src/net.rs | 8 +- crates/openshell-core/src/paths.rs | 4 +- crates/openshell-driver-docker/src/lib.rs | 2 +- .../openshell-driver-kubernetes/src/driver.rs | 35 +- .../openshell-driver-kubernetes/src/grpc.rs | 2 + crates/openshell-driver-podman/src/client.rs | 42 ++- crates/openshell-driver-podman/src/config.rs | 13 +- .../openshell-driver-podman/src/container.rs | 23 +- crates/openshell-driver-podman/src/driver.rs | 21 +- crates/openshell-driver-podman/src/grpc.rs | 15 +- crates/openshell-driver-podman/src/watcher.rs | 27 +- crates/openshell-driver-vm/build.rs | 6 +- crates/openshell-driver-vm/src/driver.rs | 67 ++-- .../src/embedded_runtime.rs | 9 +- crates/openshell-driver-vm/src/ffi.rs | 8 +- crates/openshell-driver-vm/src/gpu.rs | 29 +- crates/openshell-driver-vm/src/main.rs | 2 + crates/openshell-driver-vm/src/procguard.rs | 12 +- crates/openshell-driver-vm/src/rootfs.rs | 4 +- crates/openshell-driver-vm/src/runtime.rs | 117 +++--- crates/openshell-ocsf/src/format/shorthand.rs | 1 - .../src/tracing_layers/jsonl_layer.rs | 8 +- .../src/tracing_layers/shorthand_layer.rs | 28 +- crates/openshell-policy/src/lib.rs | 52 +-- crates/openshell-policy/src/merge.rs | 14 +- crates/openshell-prover/src/lib.rs | 30 +- crates/openshell-prover/src/model.rs | 23 +- crates/openshell-prover/src/policy.rs | 18 +- crates/openshell-prover/src/queries.rs | 3 +- crates/openshell-prover/src/registry.rs | 13 +- crates/openshell-router/src/backend.rs | 30 +- crates/openshell-router/src/config.rs | 13 +- .../openshell-sandbox/src/bypass_monitor.rs | 27 +- crates/openshell-sandbox/src/child_env.rs | 6 +- .../src/denial_aggregator.rs | 3 +- crates/openshell-sandbox/src/grpc_client.rs | 16 +- crates/openshell-sandbox/src/l7/mod.rs | 33 +- crates/openshell-sandbox/src/l7/path.rs | 17 +- crates/openshell-sandbox/src/l7/relay.rs | 9 +- crates/openshell-sandbox/src/l7/rest.rs | 26 +- crates/openshell-sandbox/src/l7/tls.rs | 4 +- crates/openshell-sandbox/src/lib.rs | 347 +++++++++--------- crates/openshell-sandbox/src/log_push.rs | 6 +- crates/openshell-sandbox/src/main.rs | 8 +- .../src/mechanistic_mapper.rs | 21 +- crates/openshell-sandbox/src/opa.rs | 69 ++-- crates/openshell-sandbox/src/process.rs | 21 +- crates/openshell-sandbox/src/procfs.rs | 28 +- crates/openshell-sandbox/src/proxy.rs | 101 +++-- .../src/sandbox/linux/landlock.rs | 3 +- .../src/sandbox/linux/mod.rs | 150 ++++---- .../src/sandbox/linux/netns.rs | 77 ++-- .../src/sandbox/linux/seccomp.rs | 23 +- crates/openshell-sandbox/src/sandbox/mod.rs | 3 + crates/openshell-sandbox/src/secrets.rs | 132 ++++--- crates/openshell-sandbox/src/ssh.rs | 38 +- .../tests/websocket_upgrade.rs | 7 + crates/openshell-server/src/auth.rs | 49 ++- crates/openshell-server/src/cli.rs | 2 +- crates/openshell-server/src/compute/mod.rs | 33 +- crates/openshell-server/src/compute/vm.rs | 32 +- crates/openshell-server/src/grpc/mod.rs | 6 +- crates/openshell-server/src/grpc/policy.rs | 76 ++-- crates/openshell-server/src/grpc/provider.rs | 26 +- crates/openshell-server/src/grpc/sandbox.rs | 30 +- .../openshell-server/src/grpc/validation.rs | 15 +- crates/openshell-server/src/inference.rs | 21 +- crates/openshell-server/src/lib.rs | 28 +- .../openshell-server/src/persistence/mod.rs | 29 +- .../src/persistence/postgres.rs | 2 +- .../src/persistence/sqlite.rs | 2 +- .../openshell-server/src/persistence/tests.rs | 4 +- crates/openshell-server/src/policy_store.rs | 13 +- crates/openshell-server/src/sandbox_watch.rs | 20 +- crates/openshell-server/src/ssh_tunnel.rs | 35 +- .../src/supervisor_session.rs | 40 +- .../tests/edge_tunnel_auth.rs | 22 +- .../tests/multiplex_integration.rs | 8 +- .../tests/multiplex_tls_integration.rs | 8 +- .../tests/ws_tunnel_integration.rs | 11 +- crates/openshell-tui/src/app.rs | 152 +++----- crates/openshell-tui/src/event.rs | 24 +- crates/openshell-tui/src/lib.rs | 326 ++++++++-------- crates/openshell-tui/src/theme.rs | 2 +- .../openshell-tui/src/ui/create_provider.rs | 5 +- crates/openshell-tui/src/ui/create_sandbox.rs | 8 +- .../openshell-tui/src/ui/global_settings.rs | 4 +- crates/openshell-tui/src/ui/mod.rs | 33 +- crates/openshell-tui/src/ui/sandbox_detail.rs | 6 +- crates/openshell-tui/src/ui/sandbox_draft.rs | 2 +- crates/openshell-tui/src/ui/sandbox_logs.rs | 36 +- .../openshell-tui/src/ui/sandbox_settings.rs | 6 +- crates/openshell-vfio/src/lib.rs | 3 +- crates/openshell-vm/build.rs | 6 +- crates/openshell-vm/src/embedded.rs | 26 +- crates/openshell-vm/src/exec.rs | 74 ++-- crates/openshell-vm/src/ffi.rs | 6 +- crates/openshell-vm/src/health.rs | 5 +- crates/openshell-vm/src/lib.rs | 85 ++--- crates/openshell-vm/src/main.rs | 50 +-- .../openshell-vm/tests/gateway_integration.rs | 10 +- e2e/rust/src/harness/binary.rs | 3 +- e2e/rust/src/harness/sandbox.rs | 8 +- e2e/rust/tests/cf_auth_smoke.rs | 2 +- e2e/rust/tests/docker_preflight.rs | 2 +- e2e/rust/tests/sandbox_labels.rs | 3 +- e2e/rust/tests/sync.rs | 13 +- tasks/rust.toml | 7 +- 129 files changed, 2007 insertions(+), 1906 deletions(-) diff --git a/crates/openshell-bootstrap/src/docker.rs b/crates/openshell-bootstrap/src/docker.rs index 65482739f6..0f71294706 100644 --- a/crates/openshell-bootstrap/src/docker.rs +++ b/crates/openshell-bootstrap/src/docker.rs @@ -28,7 +28,7 @@ const REGISTRY_NAMESPACE_DEFAULT: &str = "openshell"; /// `connect_with_local_defaults()` ceiling is 120s, which is far too short for /// multi-GB image exports — a 7 GB image on a laptop SSD takes ~4–5 minutes. /// One hour is a safe upper bound; override with `OPENSHELL_DOCKER_TIMEOUT_SECS`. -pub(crate) const DEFAULT_LARGE_TRANSFER_TIMEOUT_SECS: u64 = 3600; +pub const DEFAULT_LARGE_TRANSFER_TIMEOUT_SECS: u64 = 3600; /// Build a local-Docker client suitable for large streaming transfers. /// Respects `OPENSHELL_DOCKER_TIMEOUT_SECS` (in seconds); falls back to @@ -50,7 +50,7 @@ pub fn connect_local_for_large_transfers() -> std::result::Result Vec { +pub fn resolve_gpu_device_ids(gpu: &[String], cdi_enabled: bool) -> Vec { match gpu { [] => vec![], [v] if v == "auto" => { @@ -346,22 +346,25 @@ pub async fn find_gateway_container(docker: &Docker, port: Option) -> Resul let matches: Vec = containers .iter() - .filter(|c| is_gateway_image(c) && port.map_or(true, |p| has_port(c, p))) + .filter(|c| is_gateway_image(c) && port.is_none_or(|p| has_port(c, p))) .filter_map(container_name) .collect(); match matches.len() { 0 => { - let hint = if let Some(p) = port { - format!( - "No openshell gateway container found listening on port {p}.\n\ + let hint = port.map_or_else( + || { + "No openshell gateway container found.\n\ Is the gateway running? Check with: docker ps" - ) - } else { - "No openshell gateway container found.\n\ - Is the gateway running? Check with: docker ps" - .to_string() - }; + .to_string() + }, + |p| { + format!( + "No openshell gateway container found listening on port {p}.\n\ + Is the gateway running? Check with: docker ps" + ) + }, + ); Err(miette::miette!("{hint}")) } 1 => Ok(matches.into_iter().next().unwrap()), @@ -488,6 +491,8 @@ pub async fn ensure_image( /// Returns the actual host port the container is using. When an existing /// container is reused (same image), this may differ from `gateway_port` /// because the container was originally created with a different port. +// Refactoring this signature would touch many call sites across the workspace. +#[allow(clippy::too_many_arguments)] pub async fn ensure_container( docker: &Docker, name: &str, @@ -744,10 +749,7 @@ pub async fn ensure_container( // When OPENSHELL_PUSH_IMAGES is set the entrypoint overrides the baked-in // HelmChart manifest so k3s uses the locally-pushed images with // IfNotPresent pull policy instead of pulling from the remote registry. - let push_mode = std::env::var("OPENSHELL_PUSH_IMAGES") - .ok() - .filter(|v| !v.trim().is_empty()) - .is_some(); + let push_mode = std::env::var("OPENSHELL_PUSH_IMAGES").is_ok_and(|v| !v.trim().is_empty()); let effective_tag = std::env::var("IMAGE_TAG") .ok() .filter(|v| !v.trim().is_empty()) @@ -863,22 +865,22 @@ pub async fn check_port_conflicts( let ports = container.ports.as_deref().unwrap_or_default(); for port in ports { - if let Some(public) = port.public_port { - if needed_ports.contains(&public) { - let cname = names - .first() - .map(|n| n.trim_start_matches('/').to_string()) - .unwrap_or_else(|| { - container - .id - .clone() - .unwrap_or_else(|| "".to_string()) - }); - conflicts.push(PortConflict { - container_name: cname, - host_port: public, - }); - } + if let Some(public) = port.public_port + && needed_ports.contains(&public) + { + let cname = names.first().map_or_else( + || { + container + .id + .clone() + .unwrap_or_else(|| "".to_string()) + }, + |n| n.trim_start_matches('/').to_string(), + ); + conflicts.push(PortConflict { + container_name: cname, + host_port: public, + }); } } } @@ -1371,6 +1373,9 @@ mod tests { ); } + // Test-only: mutates DOCKER_HOST env var via std::env::set_var/remove_var, + // which require unsafe in the 2024 edition. + #[allow(unsafe_code)] #[test] fn docker_not_reachable_error_with_docker_host() { // Simulate: DOCKER_HOST is set but daemon unresponsive. diff --git a/crates/openshell-bootstrap/src/lib.rs b/crates/openshell-bootstrap/src/lib.rs index 53f659fc68..5f38a1251b 100644 --- a/crates/openshell-bootstrap/src/lib.rs +++ b/crates/openshell-bootstrap/src/lib.rs @@ -117,7 +117,7 @@ pub struct DeployOptions { /// - `[]` — no GPU passthrough (default) /// - `["legacy"]` — internal non-CDI fallback path (`driver="nvidia"`, `count=-1`) /// - `["auto"]` — resolved at deploy time: CDI if enabled on the daemon, else the non-CDI fallback - /// - `[cdi-ids…]` — CDI DeviceRequest with the given device IDs + /// - `[cdi-ids…]` — CDI `DeviceRequest` with the given device IDs pub gpu: Vec, /// When true, destroy any existing gateway resources before deploying. /// When false, an existing gateway is left as-is and deployment is @@ -340,8 +340,8 @@ where // the image to recreate the container, so the pull must happen. let need_image = !resume || !resume_container_exists; if need_image { + log("[status] Downloading gateway".to_string()); if remote_opts.is_some() { - log("[status] Downloading gateway".to_string()); let on_log_clone = Arc::clone(&on_log); let progress_cb = move |msg: String| { if let Ok(mut f) = on_log_clone.lock() { @@ -358,7 +358,6 @@ where .await?; } else { // Local deployment: ensure image exists (pull if needed) - log("[status] Downloading gateway".to_string()); ensure_image( &target_docker, &image_ref, @@ -732,16 +731,16 @@ pub async fn gateway_container_logs( Ok(()) } -/// Fetch the last `n` lines of container logs for a local gateway as a -/// `String`. This is a convenience wrapper for diagnostic call sites (e.g. -/// failure diagnosis in the CLI) that do not hold a Docker client handle. +/// Fetch the last `n` lines of container logs for a local gateway as a `String`. +/// +/// This is a convenience wrapper for diagnostic call sites (e.g. failure +/// diagnosis in the CLI) that do not hold a Docker client handle. /// /// Returns an empty string on any Docker/connection error so callers don't /// need to worry about error handling. pub async fn fetch_gateway_logs(name: &str, n: usize) -> String { - let docker = match Docker::connect_with_local_defaults() { - Ok(d) => d, - Err(_) => return String::new(), + let Ok(docker) = Docker::connect_with_local_defaults() else { + return String::new(); }; let container = container_name(name); fetch_recent_logs(&docker, &container, n).await diff --git a/crates/openshell-bootstrap/src/metadata.rs b/crates/openshell-bootstrap/src/metadata.rs index 8e6b8a0703..41e75e811f 100644 --- a/crates/openshell-bootstrap/src/metadata.rs +++ b/crates/openshell-bootstrap/src/metadata.rs @@ -305,12 +305,11 @@ pub fn load_last_sandbox(gateway: &str) -> Option { /// This should be called after a sandbox is deleted so that subsequent commands /// don't try to connect to a sandbox that no longer exists. pub fn clear_last_sandbox_if_matches(gateway: &str, sandbox: &str) { - if let Some(current) = load_last_sandbox(gateway) { - if current == sandbox { - if let Ok(path) = last_sandbox_path(gateway) { - let _ = std::fs::remove_file(path); - } - } + if let Some(current) = load_last_sandbox(gateway) + && current == sandbox + && let Ok(path) = last_sandbox_path(gateway) + { + let _ = std::fs::remove_file(path); } } diff --git a/crates/openshell-cli/src/auth.rs b/crates/openshell-cli/src/auth.rs index e961828c47..a5ae991dfb 100644 --- a/crates/openshell-cli/src/auth.rs +++ b/crates/openshell-cli/src/auth.rs @@ -72,6 +72,9 @@ fn generate_confirmation_code() -> String { let hash_b = hasher_b.finish(); prev_hash = hash_b; + // hash_b is `u64`; truncation to `usize` is acceptable here since we mod + // by charset.len() (small) and only use it as an index. + #[allow(clippy::cast_possible_truncation)] let idx = (hash_b as usize) % charset.len(); code.push(charset[idx] as char); } @@ -91,8 +94,7 @@ pub async fn browser_auth_flow(gateway_endpoint: &str) -> Result { // listener, spawns a callback server, and waits the full AUTH_TIMEOUT // (120 s) for a POST that will never arrive. let no_browser = std::env::var("OPENSHELL_NO_BROWSER") - .map(|v| v == "1" || v.eq_ignore_ascii_case("true")) - .unwrap_or(false); + .is_ok_and(|v| v == "1" || v.eq_ignore_ascii_case("true")); if no_browser { return Err(miette::miette!( "authentication skipped (OPENSHELL_NO_BROWSER is set).\n\ @@ -129,8 +131,9 @@ pub async fn browser_auth_flow(gateway_endpoint: &str) -> Result { eprint!("Press Enter to open the browser for authentication..."); std::io::stderr().flush().ok(); - let mut _input = String::new(); - std::io::stdin().read_line(&mut _input).ok(); + let mut input = String::new(); + std::io::stdin().read_line(&mut input).ok(); + drop(input); if let Err(e) = open_browser(&auth_url) { debug!(error = %e, "failed to open browser"); diff --git a/crates/openshell-cli/src/bootstrap.rs b/crates/openshell-cli/src/bootstrap.rs index ee9a481aa2..d6245a7609 100644 --- a/crates/openshell-cli/src/bootstrap.rs +++ b/crates/openshell-cli/src/bootstrap.rs @@ -101,7 +101,7 @@ fn is_connectivity_error(error: &miette::Report) -> bool { /// `false` to skip bootstrap. Otherwise returns `true` — a gateway is created /// automatically without prompting the user. pub fn confirm_bootstrap(override_value: Option) -> Result { - if let Some(false) = override_value { + if override_value == Some(false) { return Ok(false); } Ok(true) diff --git a/crates/openshell-cli/src/completers.rs b/crates/openshell-cli/src/completers.rs index 257ceb9ffa..c8b5c82a37 100644 --- a/crates/openshell-cli/src/completers.rs +++ b/crates/openshell-cli/src/completers.rs @@ -188,11 +188,11 @@ mod tests { .unwrap(); let result = complete_gateway_names(OsStr::new("a")); - let names: Vec = result - .iter() - .map(|candidate| candidate.get_value().to_string_lossy().into_owned()) - .collect(); - assert!(names.contains(&"alpha".to_string())); + assert!( + result + .iter() + .any(|candidate| candidate.get_value().to_string_lossy() == "alpha") + ); }); } } diff --git a/crates/openshell-cli/src/main.rs b/crates/openshell-cli/src/main.rs index 3853993129..57f3dbc843 100644 --- a/crates/openshell-cli/src/main.rs +++ b/crates/openshell-cli/src/main.rs @@ -151,7 +151,7 @@ fn resolve_sandbox_name(name: Option, gateway: &str) -> Result { Specify a sandbox name or connect to one first: nav sandbox connect " ) })?; - eprintln!("{} Using sandbox '{}' (last used)", "→".bold(), last.bold(),); + eprintln!("{} Using sandbox '{}' (last used)", "→".bold(), last.bold()); Ok(last) } @@ -816,7 +816,7 @@ enum GatewayCommands { /// `nvidia.com/gpu` resources. Requires NVIDIA drivers and the /// NVIDIA Container Toolkit on the host. /// - /// When enabled, OpenShell auto-selects CDI when the Docker daemon has + /// When enabled, `OpenShell` auto-selects CDI when the Docker daemon has /// CDI enabled and falls back to Docker's NVIDIA GPU request path /// (`--gpus all`) otherwise. #[arg(long)] @@ -1163,7 +1163,7 @@ enum SandboxCommands { policy: Option, /// Forward a local port to the sandbox before the initial command or shell starts. - /// Accepts [bind_address:]port (e.g. 8080, 0.0.0.0:8080). Keeps the sandbox alive. + /// Accepts [`bind_address`:]port (e.g. 8080, 0.0.0.0:8080). Keeps the sandbox alive. #[arg(long, conflicts_with = "no_keep")] forward: Option, @@ -1472,11 +1472,11 @@ enum PolicyCommands { #[arg(long = "remove-endpoint")] remove_endpoints: Vec, - /// Add a REST allow rule: host:port:METHOD:path_glob. + /// Add a REST allow rule: `host:port:METHOD:path_glob`. #[arg(long = "add-allow")] add_allow: Vec, - /// Add a REST deny rule: host:port:METHOD:path_glob. + /// Add a REST deny rule: `host:port:METHOD:path_glob`. #[arg(long = "add-deny")] add_deny: Vec, @@ -1556,7 +1556,7 @@ enum PolicyCommands { /// Prove properties of a sandbox policy — or find counterexamples. #[command(help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")] Prove { - /// Path to OpenShell sandbox policy YAML. + /// Path to `OpenShell` sandbox policy YAML. #[arg(long, value_hint = ValueHint::FilePath)] policy: String, @@ -1646,7 +1646,7 @@ enum ForwardCommands { /// Start forwarding a local port to a sandbox. #[command(help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")] Start { - /// Port to forward: [bind_address:]port (e.g. 8080, 0.0.0.0:8080). + /// Port to forward: [`bind_address`:]port (e.g. 8080, 0.0.0.0:8080). port: String, /// Sandbox name (defaults to last-used sandbox). @@ -1675,6 +1675,7 @@ enum ForwardCommands { } #[tokio::main] +#[allow(clippy::large_stack_frames)] // CLI dispatch holds many futures; OK at top level. async fn main() -> Result<()> { // Install the rustls crypto provider before completion runs — completers may // establish TLS connections to the gateway. @@ -1744,7 +1745,7 @@ async fn main() -> Result<()> { } else { vec![] }; - run::gateway_admin_deploy( + Box::pin(run::gateway_admin_deploy( &name, remote.as_deref(), ssh_key.as_deref(), @@ -1756,7 +1757,7 @@ async fn main() -> Result<()> { registry_username.as_deref(), registry_token.as_deref(), gpu, - ) + )) .await?; } GatewayCommands::Stop { @@ -1873,7 +1874,7 @@ async fn main() -> Result<()> { } else { println!("{}", "Gateway Status".cyan().bold()); println!(); - println!(" {} No gateway configured.", "Status:".dimmed(),); + println!(" {} No gateway configured.", "Status:".dimmed()); println!(); println!( "Deploy a gateway with: {}", @@ -1891,16 +1892,15 @@ async fn main() -> Result<()> { ForwardCommands::Stop { port, name } => { let name = match name { Some(n) => n, - None => match run::find_forward_by_port(port)? { - Some(n) => { + None => { + if let Some(n) = run::find_forward_by_port(port)? { eprintln!("→ Found forward on sandbox '{n}'"); n - } - None => { - eprintln!("{} No active forward found for port {port}", "!".yellow(),); + } else { + eprintln!("{} No active forward found for port {port}", "!".yellow()); return Ok(()); } - }, + } }; if run::stop_forward(&name, port)? { eprintln!( @@ -3280,7 +3280,7 @@ mod tests { }); } - /// Verify the flag names the TUI uses to build its ProxyCommand are + /// Verify the flag names the TUI uses to build its `ProxyCommand` are /// accepted by the `SshProxy` subcommand and land in the right fields. /// This catches drift when CLI flags are renamed or restructured. #[test] diff --git a/crates/openshell-cli/src/policy_update.rs b/crates/openshell-cli/src/policy_update.rs index 9f053f73b9..f51edfff64 100644 --- a/crates/openshell-cli/src/policy_update.rs +++ b/crates/openshell-cli/src/policy_update.rs @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 -use std::collections::BTreeMap; +use std::collections::{BTreeMap, HashMap}; use miette::{Result, miette}; use openshell_core::proto::policy_merge_operation; @@ -13,12 +13,12 @@ use openshell_core::proto::{ use openshell_policy::{PolicyMergeOp, generated_rule_name}; #[derive(Debug, Clone)] -pub(crate) struct PolicyUpdatePlan { +pub struct PolicyUpdatePlan { pub merge_operations: Vec, pub preview_operations: Vec, } -pub(crate) fn build_policy_update_plan( +pub fn build_policy_update_plan( add_endpoints: &[String], remove_endpoints: &[String], add_deny: &[String], @@ -51,8 +51,10 @@ pub(crate) fn build_policy_update_plan( let target_rule_name = rule_name .map(str::trim) .filter(|name| !name.is_empty()) - .map(ToString::to_string) - .unwrap_or_else(|| generated_rule_name(&endpoint.host, endpoint.port)); + .map_or_else( + || generated_rule_name(&endpoint.host, endpoint.port), + ToString::to_string, + ); let rule = NetworkPolicyRule { name: target_rule_name.clone(), endpoints: vec![endpoint.clone()], @@ -165,7 +167,7 @@ fn group_allow_rules(specs: &[String]) -> Result Result String { const GB: u64 = 1024 * MB; if bytes >= GB { - format!("{:.1} GB", bytes as f64 / GB as f64) + // GB-scale precision loss is acceptable for a human-readable label. + #[allow(clippy::cast_precision_loss)] + let gb = bytes as f64 / GB as f64; + format!("{gb:.1} GB") } else if bytes >= MB { format!("{} MB", bytes / MB) } else if bytes >= KB { format!("{} KB", bytes / KB) } else { - format!("{} B", bytes) + format!("{bytes} B") } } @@ -426,9 +429,7 @@ const CLUSTER_DEPLOY_LOG_LINES: usize = 15; /// Return the current terminal width, falling back to 80 columns. fn term_width() -> usize { - crossterm::terminal::size() - .map(|(w, _)| w as usize) - .unwrap_or(80) + crossterm::terminal::size().map_or(80, |(w, _)| w as usize) } /// Build a horizontal rule of `─` characters with an optional centered label. @@ -443,7 +444,7 @@ fn horizontal_rule(label: Option<&str>, width: usize) -> String { let remaining = width - text_len; let left = remaining / 2; let right = remaining - left; - format!("{}{}{}", "─".repeat(left), text_with_pad, "─".repeat(right),) + format!("{}{}{}", "─".repeat(left), text_with_pad, "─".repeat(right)) } None => "─".repeat(width), } @@ -623,11 +624,10 @@ impl GatewayDeployLogPanel { } fn update_spinner_message(&self) { - let msg = if let Some(detail) = &self.progress { - format!("{} ({})", self.status, detail.dimmed()) - } else { - self.status.clone() - }; + let msg = self.progress.as_ref().map_or_else( + || self.status.clone(), + |detail| format!("{} ({})", self.status, detail.dimmed()), + ); self.spinner.set_message(msg); } @@ -881,13 +881,11 @@ fn plaintext_gateway_metadata( remote: Option<&str>, local: bool, ) -> GatewayMetadata { - let (remote_host, resolved_host) = if let Some(dest) = remote { + let (remote_host, resolved_host) = remote.map_or((None, None), |dest| { let ssh_host = extract_host_from_ssh_destination(dest); let resolved = resolve_ssh_hostname(&ssh_host); (Some(dest.to_string()), Some(resolved)) - } else { - (None, None) - }; + }); GatewayMetadata { name: name.to_string(), @@ -1084,13 +1082,11 @@ pub async fn gateway_add( openshell_bootstrap::extract_and_store_pki(name, remote_opts.as_ref(), endpoint_port) .await?; - let (remote_host, resolved_host) = if let Some(dest) = remote { + let (remote_host, resolved_host) = remote.map_or((None, None), |dest| { let ssh_host = extract_host_from_ssh_destination(dest); let resolved = resolve_ssh_hostname(&ssh_host); (Some(dest.to_string()), Some(resolved)) - } else { - (None, None) - }; + }); let metadata = GatewayMetadata { name: name.to_string(), @@ -1184,7 +1180,7 @@ pub async fn gateway_login(name: &str) -> Result<()> { let token = crate::auth::browser_auth_flow(&metadata.gateway_endpoint).await?; openshell_bootstrap::edge_token::store_edge_token(name, &token)?; - eprintln!("{} Authenticated to gateway '{name}'", "✓".green().bold(),); + eprintln!("{} Authenticated to gateway '{name}'", "✓".green().bold()); Ok(()) } @@ -1424,6 +1420,7 @@ fn print_failure_diagnosis(diagnosis: &openshell_bootstrap::errors::GatewayFailu } /// Provision or start a gateway (local or remote). +#[allow(clippy::too_many_arguments)] // user-facing CLI command pub async fn gateway_admin_deploy( name: &str, remote: Option<&str>, @@ -1450,18 +1447,16 @@ pub async fn gateway_admin_deploy( }); // If the gateway is already running and we're not recreating, short-circuit. - if !recreate { - if let Some(existing) = + if !recreate + && let Some(existing) = openshell_bootstrap::check_existing_deployment(name, remote_opts.as_ref()).await? - { - if existing.container_running { - eprintln!( - "{} Gateway '{name}' is already running.", - "✓".green().bold() - ); - return Ok(()); - } - } + && existing.container_running + { + eprintln!( + "{} Gateway '{name}' is already running.", + "✓".green().bold() + ); + return Ok(()); } // When resuming an existing gateway (not recreating), prefer the port @@ -1469,10 +1464,10 @@ pub async fn gateway_admin_deploy( // may have originally bootstrapped on a non-default port (e.g. `--port // 8082`) or with `--gateway-host host.docker.internal`, and a bare // `gateway start` without those flags should honour the original values. - let stored_metadata = if !recreate { - openshell_bootstrap::load_gateway_metadata(name).ok() - } else { + let stored_metadata = if recreate { None + } else { + openshell_bootstrap::load_gateway_metadata(name).ok() }; let effective_port = stored_metadata .as_ref() @@ -1688,6 +1683,7 @@ pub fn gateway_admin_info(name: &str) -> Result<()> { /// /// Connects to the Docker daemon (local or remote via SSH) and retrieves /// logs from the `openshell-cluster-{name}` container. +#[allow(clippy::future_not_send)] // Holds stdout lock; CLI command, never sent across threads. pub async fn doctor_logs( name: &str, lines: Option, @@ -1696,24 +1692,29 @@ pub async fn doctor_logs( ssh_key: Option<&str>, ) -> Result<()> { // Build remote options: explicit --remote flag, or auto-resolve from metadata - let remote_opts = if let Some(dest) = remote { - let mut opts = RemoteOptions::new(dest); - if let Some(key) = ssh_key { - opts = opts.with_ssh_key(key); - } - Some(opts) - } else if let Some(metadata) = get_gateway_metadata(name) - && metadata.is_remote - && let Some(ref host) = metadata.remote_host - { - let mut opts = RemoteOptions::new(host.clone()); - if let Some(key) = ssh_key { - opts = opts.with_ssh_key(key); - } - Some(opts) - } else { - None - }; + let remote_opts = remote.map_or_else( + || { + if let Some(metadata) = get_gateway_metadata(name) + && metadata.is_remote + && let Some(ref host) = metadata.remote_host + { + let mut opts = RemoteOptions::new(host.clone()); + if let Some(key) = ssh_key { + opts = opts.with_ssh_key(key); + } + Some(opts) + } else { + None + } + }, + |dest| { + let mut opts = RemoteOptions::new(dest); + if let Some(key) = ssh_key { + opts = opts.with_ssh_key(key); + } + Some(opts) + }, + ); let stdout = std::io::stdout().lock(); openshell_bootstrap::gateway_container_logs(remote_opts.as_ref(), name, lines, tail, stdout) @@ -1744,15 +1745,18 @@ pub fn doctor_exec( }; // Resolve remote destination: explicit --remote flag, or auto-resolve from metadata - let remote_host = if let Some(dest) = remote { - Some(dest.to_string()) - } else if let Some(metadata) = get_gateway_metadata(name) - && metadata.is_remote - { - metadata.remote_host.clone() - } else { - None - }; + let remote_host = remote.map_or_else( + || { + if let Some(metadata) = get_gateway_metadata(name) + && metadata.is_remote + { + metadata.remote_host + } else { + None + } + }, + |dest| Some(dest.to_string()), + ); let mut cmd = if let Some(ref host) = remote_host { validate_ssh_host(host)?; @@ -1828,6 +1832,7 @@ pub fn doctor_llm() -> Result<()> { /// /// Checks Docker connectivity and reports the result. Returns exit code 0 /// if all checks pass, 1 otherwise. +#[allow(clippy::future_not_send)] // Holds stdout lock; CLI command, never sent across threads. pub async fn doctor_check() -> Result<()> { use std::io::Write; let mut stdout = std::io::stdout().lock(); @@ -1848,7 +1853,7 @@ pub async fn doctor_check() -> Result<()> { match std::env::var("DOCKER_HOST") { Ok(val) => writeln!(stdout, "{val}").into_diagnostic()?, Err(_) => writeln!(stdout, "(not set, using default socket)").into_diagnostic()?, - }; + } writeln!(stdout, "\nAll checks passed.").into_diagnostic()?; Ok(()) @@ -1943,11 +1948,15 @@ pub async fn sandbox_create_with_bootstrap( )); } let requested_gpu = gpu || from.is_some_and(source_requests_gpu); - let (tls, server, gateway_name) = - crate::bootstrap::run_bootstrap(remote, ssh_key, requested_gpu).await?; + let (tls, server, gateway_name) = Box::pin(crate::bootstrap::run_bootstrap( + remote, + ssh_key, + requested_gpu, + )) + .await?; // Disable bootstrap inside sandbox_create so that a transient connection // failure right after deploy does not trigger a second bootstrap attempt. - sandbox_create( + Box::pin(sandbox_create( &server, name, from, @@ -1966,9 +1975,9 @@ pub async fn sandbox_create_with_bootstrap( tty_override, Some(false), auto_providers_override, - &std::collections::HashMap::new(), + &HashMap::new(), &tls, - ) + )) .await } @@ -2003,7 +2012,7 @@ async fn finalize_sandbox_create_session( } /// Create a sandbox with default settings. -#[allow(clippy::too_many_arguments)] +#[allow(clippy::too_many_arguments, clippy::implicit_hasher)] // user-facing CLI command; default hasher is fine pub async fn sandbox_create( server: &str, name: Option<&str>, @@ -2023,7 +2032,7 @@ pub async fn sandbox_create( tty_override: Option, bootstrap_override: Option, auto_providers_override: Option, - labels: &std::collections::HashMap, + labels: &HashMap, tls: &TlsOptions, ) -> Result<()> { if editor.is_some() && !command.is_empty() { @@ -2072,8 +2081,12 @@ pub async fn sandbox_create( return Err(err); } let requested_gpu = gpu || from.is_some_and(source_requests_gpu); - let (new_tls, new_server, _) = - crate::bootstrap::run_bootstrap(remote, ssh_key, requested_gpu).await?; + let (new_tls, new_server, _) = Box::pin(crate::bootstrap::run_bootstrap( + remote, + ssh_key, + requested_gpu, + )) + .await?; let c = grpc_client(&new_server, &new_tls) .await .wrap_err("bootstrap succeeded but failed to connect to gateway")?; @@ -2336,7 +2349,7 @@ pub async fn sandbox_create( let label = if size_label.is_empty() { "Image pulled".to_string() } else { - format!("Image pulled ({})", size_label) + format!("Image pulled ({size_label})") }; if let Some(d) = display.as_mut() { d.complete_step_with_label( @@ -2375,10 +2388,10 @@ pub async fn sandbox_create( eprintln!(" {} {} {}", ts.dimmed(), "WARN".yellow(), w.message); } } - Some(openshell_core::proto::sandbox_stream_event::Payload::DraftPolicyUpdate(_)) => { + Some(openshell_core::proto::sandbox_stream_event::Payload::DraftPolicyUpdate(_)) + | None => { // Draft policy updates are handled in the draft panel, not during provisioning. } - None => {} } } @@ -2436,7 +2449,7 @@ pub async fn sandbox_create( ) .await?; } - eprintln!(" {} Files uploaded", "\u{2713}".green().bold(),); + eprintln!(" {} Files uploaded", "\u{2713}".green().bold()); } // If --forward was requested, start the background port forward @@ -2625,14 +2638,10 @@ fn resolve_from(value: &str) -> Result { } fn source_requests_gpu(source: &str) -> bool { - if let Ok(resolved) = resolve_from(source) { - match resolved { - ResolvedSource::Image(image) => image_requests_gpu(&image), - ResolvedSource::Dockerfile { .. } => false, - } - } else { - false - } + resolve_from(source).is_ok_and(|resolved| match resolved { + ResolvedSource::Image(image) => image_requests_gpu(&image), + ResolvedSource::Dockerfile { .. } => false, + }) } fn image_requests_gpu(image: &str) -> bool { @@ -2816,14 +2825,14 @@ pub async fn sandbox_get( println!(" {} {}", "Phase:".dimmed(), phase_name(sandbox.phase)); // Display labels if present - if let Some(metadata) = &sandbox.metadata { - if !metadata.labels.is_empty() { - println!(" {} ", "Labels:".dimmed()); - let mut labels: Vec<_> = metadata.labels.iter().collect(); - labels.sort_by_key(|(k, _)| *k); - for (key, value) in labels { - println!(" {}: {}", key, value); - } + if let Some(metadata) = &sandbox.metadata + && !metadata.labels.is_empty() + { + println!(" {} ", "Labels:".dimmed()); + let mut labels: Vec<_> = metadata.labels.iter().collect(); + labels.sort_by_key(|(k, _)| *k); + for (key, value) in labels { + println!(" {key}: {value}"); } } @@ -2903,7 +2912,9 @@ pub async fn sandbox_exec_grpc( // Read stdin if piped (not a TTY), using spawn_blocking to avoid blocking // the async runtime. Cap the read at MAX_STDIN_PAYLOAD + 1 so we never // buffer more than the limit into memory. - let stdin_payload = if !std::io::stdin().is_terminal() { + let stdin_payload = if std::io::stdin().is_terminal() { + Vec::new() + } else { tokio::task::spawn_blocking(|| { let limit = (MAX_STDIN_PAYLOAD + 1) as u64; let mut buf = Vec::new(); @@ -2921,8 +2932,6 @@ pub async fn sandbox_exec_grpc( }) .await .into_diagnostic()?? // first ? unwraps JoinError, second ? unwraps Result - } else { - Vec::new() }; // Resolve TTY mode: explicit --tty / --no-tty wins, otherwise auto-detect. @@ -3067,7 +3076,7 @@ pub async fn sandbox_list( if names_only { for sandbox in sandboxes { - println!("{}", sandbox.object_name().to_string()); + println!("{}", sandbox.object_name()); } return Ok(()); } @@ -3099,13 +3108,7 @@ pub async fn sandbox_list( Ok(SandboxPhase::Deleting) => phase.dimmed().to_string(), _ => phase.to_string(), }; - let created = format_epoch_ms( - sandbox - .metadata - .as_ref() - .map(|m| m.created_at_ms) - .unwrap_or(0), - ); + let created = format_epoch_ms(sandbox.metadata.as_ref().map_or(0, |m| m.created_at_ms)); println!( "{: Result< println!("{}", "Provider:".cyan().bold()); println!(); - println!(" {} {}", "Id:".dimmed(), provider.object_id().to_string()); - println!( - " {} {}", - "Name:".dimmed(), - provider.object_name().to_string() - ); + println!(" {} {}", "Id:".dimmed(), provider.object_id()); + println!(" {} {}", "Name:".dimmed(), provider.object_name()); println!(" {} {}", "Type:".dimmed(), provider.r#type); println!( " {} {}", @@ -3668,7 +3667,7 @@ pub async fn provider_list( if names_only { for provider in providers { - println!("{}", provider.object_name().to_string()); + println!("{}", provider.object_name()); } return Ok(()); } @@ -3764,7 +3763,7 @@ pub async fn provider_update( id: String::new(), name: name.to_string(), created_at_ms: 0, - labels: std::collections::HashMap::new(), + labels: HashMap::new(), }), r#type: String::new(), credentials: credential_map, @@ -3782,7 +3781,7 @@ pub async fn provider_update( println!( "{} Updated provider {}", "✓".green().bold(), - provider.object_name().to_string() + provider.object_name() ); Ok(()) } @@ -4374,9 +4373,9 @@ pub async fn sandbox_settings_get( "sandbox" }; - println!("Sandbox: {}", name); + println!("Sandbox: {name}"); println!("Config Rev: {}", response.config_revision); - println!("Policy Source: {}", policy_source); + println!("Policy Source: {policy_source}"); println!("Policy Hash: {}", response.policy_hash); if response.settings.is_empty() { @@ -4598,7 +4597,7 @@ pub async fn gateway_setting_delete( response.settings_revision ); } else { - println!("{} Global setting {} not found", "!".yellow(), key,); + println!("{} Global setting {} not found", "!".yellow(), key); } Ok(()) } @@ -5121,6 +5120,7 @@ fn print_policy_revision_table(revisions: &[openshell_core::proto::SandboxPolicy // Sandbox logs command // --------------------------------------------------------------------------- +#[allow(clippy::too_many_arguments)] // user-facing CLI command pub async fn sandbox_logs( server: &str, name: &str, diff --git a/crates/openshell-cli/src/ssh.rs b/crates/openshell-cli/src/ssh.rs index e6273413c8..45e98b359b 100644 --- a/crates/openshell-cli/src/ssh.rs +++ b/crates/openshell-cli/src/ssh.rs @@ -44,7 +44,7 @@ impl Editor { } } - fn remote_target(self, host_alias: &str) -> String { + fn remote_target(host_alias: &str) -> String { format!("ssh-remote+{host_alias}") } @@ -357,12 +357,13 @@ pub async fn sandbox_forward( command.status().await.into_diagnostic()? } else { let mut child = command.spawn().into_diagnostic()?; - match tokio::time::timeout(FOREGROUND_FORWARD_STARTUP_GRACE_PERIOD, child.wait()).await { - Ok(status) => status.into_diagnostic()?, - Err(_) => { - eprintln!("{}", foreground_forward_started_message(name, spec)); - child.wait().await.into_diagnostic()? - } + if let Ok(status) = + tokio::time::timeout(FOREGROUND_FORWARD_STARTUP_GRACE_PERIOD, child.wait()).await + { + status.into_diagnostic()? + } else { + eprintln!("{}", foreground_forward_started_message(name, spec)); + child.wait().await.into_diagnostic()? } }; @@ -511,8 +512,7 @@ fn write_upload_archive(writer: W, source: UploadSource) -> Result<()> let full_path = base_dir.join(file); let archive_path = archive_prefix .as_ref() - .map(|prefix| prefix.join(file)) - .unwrap_or_else(|| PathBuf::from(file)); + .map_or_else(|| PathBuf::from(file), |prefix| prefix.join(file)); if full_path.is_file() { archive .append_path_with_name(&full_path, &archive_path) @@ -556,10 +556,7 @@ async fn ssh_tar_upload( // When no explicit destination is given, use the unescaped `$HOME` shell // variable so the remote shell resolves it at runtime. - let escaped_dest = match dest_dir { - Some(d) => shell_escape(d), - None => "$HOME".to_string(), - }; + let escaped_dest = dest_dir.map_or_else(|| "$HOME".to_string(), shell_escape); let mut ssh = ssh_base_command(&session.proxy_command); ssh.arg("-T") @@ -598,7 +595,7 @@ async fn ssh_tar_upload( Ok(()) } -/// Split a sandbox path into (parent_directory, basename). +/// Split a sandbox path into (`parent_directory`, basename). /// /// Examples: /// `"/sandbox/.bashrc"` -> `("/sandbox", ".bashrc")` @@ -666,22 +663,23 @@ pub async fn sandbox_sync_up( // passed "/sandbox"), fall through to directory semantics instead. The // sandbox user cannot write to "/" and the intent is almost certainly // "put the file inside /sandbox", not "create a file named sandbox in /". - if let Some(path) = sandbox_path { - if local_path.is_file() && !path.ends_with('/') { - let (parent, target_name) = split_sandbox_path(path); - if parent != "/" { - return ssh_tar_upload( - server, - name, - Some(parent), - UploadSource::SinglePath { - local_path: local_path.to_path_buf(), - tar_name: target_name.into(), - }, - tls, - ) - .await; - } + if let Some(path) = sandbox_path + && local_path.is_file() + && !path.ends_with('/') + { + let (parent, target_name) = split_sandbox_path(path); + if parent != "/" { + return ssh_tar_upload( + server, + name, + Some(parent), + UploadSource::SinglePath { + local_path: local_path.to_path_buf(), + tar_name: target_name.into(), + }, + tls, + ) + .await; } } @@ -720,8 +718,7 @@ pub async fn sandbox_sync_up( fn directory_upload_prefix(local_path: &Path) -> std::ffi::OsString { local_path .file_name() - .map(|n| n.to_os_string()) - .unwrap_or_else(|| ".".into()) + .map_or_else(|| ".".into(), std::ffi::OsStr::to_os_string) } fn file_list_archive_prefix(local_path: &Path) -> Option { @@ -824,6 +821,13 @@ pub async fn sandbox_ssh_proxy( token: &str, tls: &TlsOptions, ) -> Result<()> { + // The gateway returns 412 (Precondition Failed) when the sandbox pod + // exists but hasn't reached Ready phase yet. This is a transient state + // after sandbox allocation — retry with backoff instead of failing + // immediately. + const MAX_CONNECT_WAIT: Duration = Duration::from_secs(60); + const INITIAL_BACKOFF: Duration = Duration::from_secs(1); + let url: url::Url = gateway_url .parse() .into_diagnostic() @@ -842,13 +846,6 @@ pub async fn sandbox_ssh_proxy( "CONNECT {connect_path} HTTP/1.1\r\nHost: {gateway_host}\r\nX-Sandbox-Id: {sandbox_id}\r\nX-Sandbox-Token: {token}\r\n\r\n" ); - // The gateway returns 412 (Precondition Failed) when the sandbox pod - // exists but hasn't reached Ready phase yet. This is a transient state - // after sandbox allocation — retry with backoff instead of failing - // immediately. - const MAX_CONNECT_WAIT: Duration = Duration::from_secs(60); - const INITIAL_BACKOFF: Duration = Duration::from_secs(1); - let start = std::time::Instant::now(); let mut backoff = INITIAL_BACKOFF; let mut buf_stream; @@ -1038,8 +1035,7 @@ fn upsert_host_block(contents: &str, alias: &str, block: &str) -> String { .enumerate() .skip(start + 1) .find(|(_, line)| line.trim_start().starts_with("Host ")) - .map(|(idx, _)| idx) - .unwrap_or(lines.len()); + .map_or(lines.len(), |(idx, _)| idx); out.extend_from_slice(&lines[..start]); if !out.is_empty() && !out.last().is_some_and(|line| line.is_empty()) { @@ -1088,7 +1084,7 @@ fn launch_editor(editor: Editor, host_alias: &str) -> Result<()> { launch_editor_command( editor.binary(), editor.label(), - &editor.remote_target(host_alias), + &Editor::remote_target(host_alias), ) } @@ -1244,6 +1240,7 @@ mod tests { } #[test] + #[allow(unsafe_code)] // Test-only: env vars require unsafe in Rust 2024. fn install_ssh_config_adds_include_once_and_updates_managed_file() { let _guard = TEST_ENV_LOCK .lock() diff --git a/crates/openshell-cli/tests/ensure_providers_integration.rs b/crates/openshell-cli/tests/ensure_providers_integration.rs index 34485377d0..a5a4857358 100644 --- a/crates/openshell-cli/tests/ensure_providers_integration.rs +++ b/crates/openshell-cli/tests/ensure_providers_integration.rs @@ -210,10 +210,10 @@ impl OpenShell for TestOpenShell { if providers.contains_key(&provider_name) { return Err(Status::already_exists("provider already exists")); } - if provider.object_id().is_empty() { - if let Some(metadata) = &mut provider.metadata { - metadata.id = format!("id-{provider_name}"); - } + if provider.object_id().is_empty() + && let Some(metadata) = &mut provider.metadata + { + metadata.id = format!("id-{provider_name}"); } providers.insert(provider_name, provider.clone()); Ok(Response::new(ProviderResponse { @@ -266,9 +266,9 @@ impl OpenShell for TestOpenShell { .cloned() .ok_or_else(|| Status::not_found("provider not found"))?; // Merge semantics: empty map = no change, empty value = delete key. - let merge = |mut base: std::collections::HashMap, - incoming: std::collections::HashMap| - -> std::collections::HashMap { + let merge = |mut base: HashMap, + incoming: HashMap| + -> HashMap { if incoming.is_empty() { return base; } @@ -286,7 +286,7 @@ impl OpenShell for TestOpenShell { let updated = Provider { metadata: Some(openshell_core::proto::datamodel::v1::ObjectMeta { id: existing_metadata.id, - name: provider_metadata.name.clone(), + name: provider_metadata.name, created_at_ms: existing_metadata.created_at_ms, labels: existing_metadata.labels, }), @@ -449,15 +449,14 @@ impl OpenShell for TestOpenShell { Err(Status::unimplemented("not implemented in test")) } - type RelayStreamStream = tokio_stream::wrappers::ReceiverStream< - Result, - >; + type RelayStreamStream = + tokio_stream::wrappers::ReceiverStream>; async fn relay_stream( &self, _request: tonic::Request>, - ) -> Result, tonic::Status> { - Err(tonic::Status::unimplemented("not implemented in test")) + ) -> Result, Status> { + Err(Status::unimplemented("not implemented in test")) } } diff --git a/crates/openshell-cli/tests/mtls_integration.rs b/crates/openshell-cli/tests/mtls_integration.rs index e78c915782..77d33f7b00 100644 --- a/crates/openshell-cli/tests/mtls_integration.rs +++ b/crates/openshell-cli/tests/mtls_integration.rs @@ -336,15 +336,14 @@ impl OpenShell for TestOpenShell { Err(Status::unimplemented("not implemented in test")) } - type RelayStreamStream = tokio_stream::wrappers::ReceiverStream< - Result, - >; + type RelayStreamStream = + tokio_stream::wrappers::ReceiverStream>; async fn relay_stream( &self, _request: tonic::Request>, - ) -> Result, tonic::Status> { - Err(tonic::Status::unimplemented("not implemented in test")) + ) -> Result, Status> { + Err(Status::unimplemented("not implemented in test")) } } diff --git a/crates/openshell-cli/tests/provider_commands_integration.rs b/crates/openshell-cli/tests/provider_commands_integration.rs index 9bda696c1d..d151e5a1ce 100644 --- a/crates/openshell-cli/tests/provider_commands_integration.rs +++ b/crates/openshell-cli/tests/provider_commands_integration.rs @@ -160,10 +160,10 @@ impl OpenShell for TestOpenShell { if providers.contains_key(&provider_name) { return Err(Status::already_exists("provider already exists")); } - if provider.object_id().is_empty() { - if let Some(metadata) = &mut provider.metadata { - metadata.id = format!("id-{provider_name}"); - } + if provider.object_id().is_empty() + && let Some(metadata) = &mut provider.metadata + { + metadata.id = format!("id-{provider_name}"); } providers.insert(provider_name, provider.clone()); Ok(Response::new(ProviderResponse { @@ -216,9 +216,9 @@ impl OpenShell for TestOpenShell { .cloned() .ok_or_else(|| Status::not_found("provider not found"))?; // Merge semantics: empty map = no change, empty value = delete key. - let merge = |mut base: std::collections::HashMap, - incoming: std::collections::HashMap| - -> std::collections::HashMap { + let merge = |mut base: HashMap, + incoming: HashMap| + -> HashMap { if incoming.is_empty() { return base; } @@ -236,7 +236,7 @@ impl OpenShell for TestOpenShell { let updated = Provider { metadata: Some(openshell_core::proto::datamodel::v1::ObjectMeta { id: existing_metadata.id, - name: provider_metadata.name.clone(), + name: provider_metadata.name, created_at_ms: existing_metadata.created_at_ms, labels: existing_metadata.labels, }), @@ -399,15 +399,14 @@ impl OpenShell for TestOpenShell { Err(Status::unimplemented("not implemented in test")) } - type RelayStreamStream = tokio_stream::wrappers::ReceiverStream< - Result, - >; + type RelayStreamStream = + tokio_stream::wrappers::ReceiverStream>; async fn relay_stream( &self, _request: tonic::Request>, - ) -> Result, tonic::Status> { - Err(tonic::Status::unimplemented("not implemented in test")) + ) -> Result, Status> { + Err(Status::unimplemented("not implemented in test")) } } diff --git a/crates/openshell-cli/tests/sandbox_create_lifecycle_integration.rs b/crates/openshell-cli/tests/sandbox_create_lifecycle_integration.rs index 6b78dab9a2..e69d06f4fd 100644 --- a/crates/openshell-cli/tests/sandbox_create_lifecycle_integration.rs +++ b/crates/openshell-cli/tests/sandbox_create_lifecycle_integration.rs @@ -423,15 +423,14 @@ impl OpenShell for TestOpenShell { Err(Status::unimplemented("not implemented in test")) } - type RelayStreamStream = tokio_stream::wrappers::ReceiverStream< - Result, - >; + type RelayStreamStream = + tokio_stream::wrappers::ReceiverStream>; async fn relay_stream( &self, _request: tonic::Request>, - ) -> Result, tonic::Status> { - Err(tonic::Status::unimplemented("not implemented in test")) + ) -> Result, Status> { + Err(Status::unimplemented("not implemented in test")) } } diff --git a/crates/openshell-cli/tests/sandbox_name_fallback_integration.rs b/crates/openshell-cli/tests/sandbox_name_fallback_integration.rs index 5c463de9ec..7d6a9536a8 100644 --- a/crates/openshell-cli/tests/sandbox_name_fallback_integration.rs +++ b/crates/openshell-cli/tests/sandbox_name_fallback_integration.rs @@ -360,15 +360,14 @@ impl OpenShell for TestOpenShell { Err(Status::unimplemented("not implemented in test")) } - type RelayStreamStream = tokio_stream::wrappers::ReceiverStream< - Result, - >; + type RelayStreamStream = + tokio_stream::wrappers::ReceiverStream>; async fn relay_stream( &self, _request: tonic::Request>, - ) -> Result, tonic::Status> { - Err(tonic::Status::unimplemented("not implemented in test")) + ) -> Result, Status> { + Err(Status::unimplemented("not implemented in test")) } } diff --git a/crates/openshell-core/src/config.rs b/crates/openshell-core/src/config.rs index 40a87fc41e..f5d844fb8b 100644 --- a/crates/openshell-core/src/config.rs +++ b/crates/openshell-core/src/config.rs @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 -//! Configuration management for OpenShell components. +//! Configuration management for `OpenShell` components. use serde::{Deserialize, Serialize}; use std::fmt; @@ -138,7 +138,7 @@ pub struct Config { #[serde(default)] pub sandbox_image_pull_policy: String, - /// gRPC endpoint for sandboxes to connect back to OpenShell. + /// gRPC endpoint for sandboxes to connect back to `OpenShell`. /// Used by sandbox pods to fetch their policy at startup. #[serde(default)] pub grpc_endpoint: String, diff --git a/crates/openshell-core/src/error.rs b/crates/openshell-core/src/error.rs index 9d16501355..7c33c9eaf8 100644 --- a/crates/openshell-core/src/error.rs +++ b/crates/openshell-core/src/error.rs @@ -1,15 +1,15 @@ // SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 -//! Common error types for OpenShell. +//! Common error types for `OpenShell`. use miette::Diagnostic; use thiserror::Error; -/// Result type alias using OpenShell's error type. +/// Result type alias using `OpenShell`'s error type. pub type Result = std::result::Result; -/// OpenShell error type. +/// `OpenShell` error type. #[derive(Debug, Error, Diagnostic)] pub enum Error { /// Configuration error. diff --git a/crates/openshell-core/src/forward.rs b/crates/openshell-core/src/forward.rs index e6c7d2f7cc..27f3275c02 100644 --- a/crates/openshell-core/src/forward.rs +++ b/crates/openshell-core/src/forward.rs @@ -135,18 +135,17 @@ pub fn pid_matches_forward(pid: u32, port: u16, sandbox_id: Option<&str>) -> boo /// match is expected. pub fn find_forward_by_port(port: u16) -> Result> { let dir = forward_pid_dir()?; - let entries = match std::fs::read_dir(&dir) { - Ok(e) => e, - Err(_) => return Ok(None), + let Ok(entries) = std::fs::read_dir(&dir) else { + return Ok(None); }; let suffix = format!("-{port}.pid"); for entry in entries.flatten() { let file_name = entry.file_name(); let file_name = file_name.to_string_lossy(); - if let Some(name) = file_name.strip_suffix(&suffix) { - if !name.is_empty() { - return Ok(Some(name.to_string())); - } + if let Some(name) = file_name.strip_suffix(&suffix) + && !name.is_empty() + { + return Ok(Some(name.to_string())); } } Ok(None) @@ -1049,9 +1048,8 @@ mod tests { // `python3 -m http.server` which listens on [::] by default. The // IPv4-only TcpListener::bind("127.0.0.1", port) might succeed, but // lsof should detect the listener and the check should still fail. - let listener = match TcpListener::bind("[::]:0") { - Ok(l) => l, - Err(_) => return, // IPv6 not available, skip + let Ok(listener) = TcpListener::bind("[::]:0") else { + return; // IPv6 not available, skip }; let port = listener.local_addr().unwrap().port(); diff --git a/crates/openshell-core/src/inference.rs b/crates/openshell-core/src/inference.rs index d2581f7eb0..0360cae5c4 100644 --- a/crates/openshell-core/src/inference.rs +++ b/crates/openshell-core/src/inference.rs @@ -124,8 +124,9 @@ pub fn auth_for_provider_type(provider_type: &str) -> (AuthHeader, Vec<(String, pub fn route_headers_for_provider_type( provider_type: &str, ) -> (AuthHeader, Vec<(String, String)>, Vec) { - match profile_for(provider_type) { - Some(profile) => { + profile_for(provider_type).map_or_else( + || (AuthHeader::Bearer, Vec::new(), Vec::new()), + |profile| { let headers = profile .default_headers .iter() @@ -137,9 +138,8 @@ pub fn route_headers_for_provider_type( .map(|name| (*name).to_string()) .collect(); (profile.auth.clone(), headers, passthrough_headers) - } - None => (AuthHeader::Bearer, Vec::new(), Vec::new()), - } + }, + ) } // --------------------------------------------------------------------------- diff --git a/crates/openshell-core/src/lib.rs b/crates/openshell-core/src/lib.rs index dff6d82ecb..8cdcd5bab6 100644 --- a/crates/openshell-core/src/lib.rs +++ b/crates/openshell-core/src/lib.rs @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 -//! OpenShell Core - shared library for OpenShell components. +//! `OpenShell` Core - shared library for `OpenShell` components. //! //! This crate provides: //! - Protocol buffer definitions and generated code diff --git a/crates/openshell-core/src/metadata.rs b/crates/openshell-core/src/metadata.rs index 30f0999fb5..90566dcfd6 100644 --- a/crates/openshell-core/src/metadata.rs +++ b/crates/openshell-core/src/metadata.rs @@ -3,7 +3,7 @@ //! Object metadata accessors for Kubernetes-style resources. //! -//! These traits provide uniform access to ObjectMeta fields across all resource types. +//! These traits provide uniform access to `ObjectMeta` fields across all resource types. use crate::proto::{InferenceRoute, ObjectForTest, Provider, Sandbox, SshSession}; use std::collections::HashMap; @@ -26,16 +26,13 @@ pub trait ObjectLabels { // Implementations for Sandbox impl ObjectId for Sandbox { fn object_id(&self) -> &str { - self.metadata.as_ref().map(|m| m.id.as_str()).unwrap_or("") + self.metadata.as_ref().map_or("", |m| m.id.as_str()) } } impl ObjectName for Sandbox { fn object_name(&self) -> &str { - self.metadata - .as_ref() - .map(|m| m.name.as_str()) - .unwrap_or("") + self.metadata.as_ref().map_or("", |m| m.name.as_str()) } } @@ -48,16 +45,13 @@ impl ObjectLabels for Sandbox { // Implementations for Provider impl ObjectId for Provider { fn object_id(&self) -> &str { - self.metadata.as_ref().map(|m| m.id.as_str()).unwrap_or("") + self.metadata.as_ref().map_or("", |m| m.id.as_str()) } } impl ObjectName for Provider { fn object_name(&self) -> &str { - self.metadata - .as_ref() - .map(|m| m.name.as_str()) - .unwrap_or("") + self.metadata.as_ref().map_or("", |m| m.name.as_str()) } } @@ -70,16 +64,13 @@ impl ObjectLabels for Provider { // Implementations for SshSession impl ObjectId for SshSession { fn object_id(&self) -> &str { - self.metadata.as_ref().map(|m| m.id.as_str()).unwrap_or("") + self.metadata.as_ref().map_or("", |m| m.id.as_str()) } } impl ObjectName for SshSession { fn object_name(&self) -> &str { - self.metadata - .as_ref() - .map(|m| m.name.as_str()) - .unwrap_or("") + self.metadata.as_ref().map_or("", |m| m.name.as_str()) } } @@ -92,16 +83,13 @@ impl ObjectLabels for SshSession { // Implementations for InferenceRoute impl ObjectId for InferenceRoute { fn object_id(&self) -> &str { - self.metadata.as_ref().map(|m| m.id.as_str()).unwrap_or("") + self.metadata.as_ref().map_or("", |m| m.id.as_str()) } } impl ObjectName for InferenceRoute { fn object_name(&self) -> &str { - self.metadata - .as_ref() - .map(|m| m.name.as_str()) - .unwrap_or("") + self.metadata.as_ref().map_or("", |m| m.name.as_str()) } } diff --git a/crates/openshell-core/src/net.rs b/crates/openshell-core/src/net.rs index a73cb38dc3..5dca4feb67 100644 --- a/crates/openshell-core/src/net.rs +++ b/crates/openshell-core/src/net.rs @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 -//! Network IP classification utilities shared across OpenShell crates. +//! Network IP classification utilities shared across `OpenShell` crates. //! //! These helpers enforce the always-blocked IP invariant (loopback, link-local, //! unspecified) and the broader internal-IP classification (adds RFC 1918 and @@ -133,7 +133,7 @@ pub fn is_always_blocked_net(net: ipnet::IpNet) -> bool { /// when `allowed_ips` should be populated in proposals. pub fn is_internal_ip(ip: IpAddr) -> bool { match ip { - IpAddr::V4(v4) => is_internal_v4(&v4), + IpAddr::V4(v4) => is_internal_v4(v4), IpAddr::V6(v6) => { if v6.is_loopback() || v6.is_unspecified() { return true; @@ -148,7 +148,7 @@ pub fn is_internal_ip(ip: IpAddr) -> bool { } // Check IPv4-mapped IPv6 (::ffff:x.x.x.x) if let Some(v4) = v6.to_ipv4_mapped() { - return is_internal_v4(&v4); + return is_internal_v4(v4); } false } @@ -157,7 +157,7 @@ pub fn is_internal_ip(ip: IpAddr) -> bool { /// IPv4 internal address check covering RFC 1918, CGNAT (RFC 6598), and other /// special-use ranges that should never be reachable from sandbox egress. -fn is_internal_v4(v4: &Ipv4Addr) -> bool { +fn is_internal_v4(v4: Ipv4Addr) -> bool { if v4.is_loopback() || v4.is_private() || v4.is_link_local() || v4.is_unspecified() { return true; } diff --git a/crates/openshell-core/src/paths.rs b/crates/openshell-core/src/paths.rs index fd0a141b38..00104f3c2b 100644 --- a/crates/openshell-core/src/paths.rs +++ b/crates/openshell-core/src/paths.rs @@ -105,9 +105,7 @@ pub fn ensure_parent_dir_restricted(path: &Path) -> Result<()> { #[cfg(unix)] pub fn is_file_permissions_too_open(path: &Path) -> bool { use std::os::unix::fs::PermissionsExt; - std::fs::metadata(path) - .map(|m| m.permissions().mode() & 0o077 != 0) - .unwrap_or(false) + std::fs::metadata(path).is_ok_and(|m| m.permissions().mode() & 0o077 != 0) } #[cfg(test)] diff --git a/crates/openshell-driver-docker/src/lib.rs b/crates/openshell-driver-docker/src/lib.rs index d9683d416b..525cb2c74f 100644 --- a/crates/openshell-driver-docker/src/lib.rs +++ b/crates/openshell-driver-docker/src/lib.rs @@ -1607,7 +1607,7 @@ fn write_cache_binary_atomic(final_path: &Path, bytes: &[u8]) -> CoreResult<()> )) })?; temp.as_file().sync_all().map_err(|err| { - Error::config(format!("failed to sync supervisor binary temp file: {err}",)) + Error::config(format!("failed to sync supervisor binary temp file: {err}")) })?; #[cfg(unix)] diff --git a/crates/openshell-driver-kubernetes/src/driver.rs b/crates/openshell-driver-kubernetes/src/driver.rs index a3f90457c8..360a941528 100644 --- a/crates/openshell-driver-kubernetes/src/driver.rs +++ b/crates/openshell-driver-kubernetes/src/driver.rs @@ -411,6 +411,8 @@ impl KubernetesComputeDriver { } } + // Kept `async` to match the gRPC handler signature in `grpc.rs`, which awaits this method. + #[allow(clippy::unused_async)] pub async fn watch_sandboxes(&self) -> Result { let namespace = self.config.namespace.clone(); let sandbox_api = self.watch_api(); @@ -580,7 +582,6 @@ fn sandbox_from_object(namespace: &str, obj: DynamicObject) -> Result Option, sandbox_id: &str, @@ -1503,7 +1504,7 @@ mod tests { .expect("command should be set"); assert_eq!( command[0].as_str().unwrap(), - format!("{}/openshell-sandbox", SUPERVISOR_MOUNT_PATH) + format!("{SUPERVISOR_MOUNT_PATH}/openshell-sandbox") ); // Volume mount should be read-only @@ -1597,13 +1598,12 @@ mod tests { fn gpu_sandbox_uses_template_runtime_class_name_when_set() { let template = SandboxTemplate { platform_config: Some(Struct { - fields: [( + fields: std::iter::once(( "runtime_class_name".to_string(), Value { kind: Some(Kind::StringValue("kata-containers".to_string())), }, - )] - .into_iter() + )) .collect(), }), ..SandboxTemplate::default() @@ -1636,13 +1636,12 @@ mod tests { fn non_gpu_sandbox_uses_template_runtime_class_name_when_set() { let template = SandboxTemplate { platform_config: Some(Struct { - fields: [( + fields: std::iter::once(( "runtime_class_name".to_string(), Value { kind: Some(Kind::StringValue("kata-containers".to_string())), }, - )] - .into_iter() + )) .collect(), }), ..SandboxTemplate::default() @@ -1851,9 +1850,7 @@ mod tests { // Verify we did not inject one (which would cause a duplicate). let has_pvc_vol = pod_template["spec"]["volumes"] .as_array() - .map_or(false, |vols| { - vols.iter().any(|v| v["name"] == WORKSPACE_VOLUME_NAME) - }); + .is_some_and(|vols| vols.iter().any(|v| v["name"] == WORKSPACE_VOLUME_NAME)); assert!( !has_pvc_vol, "apply_workspace_persistence must NOT add a PVC volume (the CRD controller does that)" @@ -1933,16 +1930,14 @@ mod tests { pod_template["spec"]["initContainers"].is_null() || pod_template["spec"]["initContainers"] .as_array() - .is_none_or(|a| a.is_empty()), + .is_none_or(Vec::is_empty), "workspace init container must NOT be present when inject_workspace is false" ); // No workspace volume mount on agent let has_workspace_mount = pod_template["spec"]["containers"][0]["volumeMounts"] .as_array() - .map_or(false, |mounts| { - mounts.iter().any(|m| m["name"] == WORKSPACE_VOLUME_NAME) - }); + .is_some_and(|mounts| mounts.iter().any(|m| m["name"] == WORKSPACE_VOLUME_NAME)); assert!( !has_workspace_mount, "workspace mount must NOT be present when inject_workspace is false" diff --git a/crates/openshell-driver-kubernetes/src/grpc.rs b/crates/openshell-driver-kubernetes/src/grpc.rs index 75e131d417..51488f694d 100644 --- a/crates/openshell-driver-kubernetes/src/grpc.rs +++ b/crates/openshell-driver-kubernetes/src/grpc.rs @@ -1,6 +1,8 @@ // SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 +#![allow(clippy::result_large_err)] // gRPC handlers return Result<_, tonic::Status> + use futures::{Stream, StreamExt}; use openshell_core::proto::compute::v1::{ CreateSandboxRequest, CreateSandboxResponse, DeleteSandboxRequest, DeleteSandboxResponse, diff --git a/crates/openshell-driver-podman/src/client.rs b/crates/openshell-driver-podman/src/client.rs index 1c590db2c1..12ea0901fc 100644 --- a/crates/openshell-driver-podman/src/client.rs +++ b/crates/openshell-driver-podman/src/client.rs @@ -54,7 +54,7 @@ const MAX_NAME_LEN: usize = 255; /// Valid names start with an alphanumeric character and contain only /// alphanumerics, dots, underscores, and hyphens — matching Podman's /// own naming rules. Names longer than [`MAX_NAME_LEN`] are rejected. -pub(crate) fn validate_name(name: &str) -> Result<(), PodmanApiError> { +pub fn validate_name(name: &str) -> Result<(), PodmanApiError> { // Regex-equivalent: ^[a-zA-Z0-9][a-zA-Z0-9._-]*$ if name.is_empty() { return Err(PodmanApiError::InvalidInput( @@ -92,6 +92,7 @@ pub struct ContainerInspect { pub name: String, pub state: ContainerState, #[serde(default)] + #[allow(dead_code)] // kept for podman API compat pub network_settings: NetworkSettings, #[serde(default)] pub config: ContainerConfig, @@ -101,6 +102,7 @@ pub struct ContainerInspect { #[serde(rename_all = "PascalCase")] pub struct ContainerState { pub status: String, + #[allow(dead_code)] // kept for podman API compat pub running: bool, #[serde(default)] pub exit_code: i64, @@ -125,8 +127,10 @@ pub struct HealthState { #[serde(rename_all = "PascalCase")] pub struct NetworkSettings { #[serde(default)] + #[allow(dead_code)] // kept for podman API compat pub networks: HashMap, #[serde(default)] + #[allow(dead_code)] // kept for podman API compat pub ports: HashMap>>, } @@ -135,6 +139,7 @@ pub struct NetworkSettings { pub struct NetworkInfo { #[serde(rename = "IPAddress")] #[serde(default)] + #[allow(dead_code)] // kept for podman API compat pub ip_address: String, } @@ -142,9 +147,11 @@ pub struct NetworkInfo { #[serde(rename_all = "PascalCase")] pub struct PortBinding { #[serde(default)] + #[allow(dead_code)] // kept for podman API compat pub host_port: String, #[serde(rename = "HostIp")] #[serde(default)] + #[allow(dead_code)] // kept for podman API compat pub host_ip: String, } @@ -166,19 +173,26 @@ pub struct ContainerListEntry { #[serde(default)] pub labels: HashMap, #[serde(default)] + #[allow(dead_code)] // kept for podman API compat pub ports: Option>, #[serde(default)] + #[allow(dead_code)] // kept for podman API compat pub networks: Option>, #[serde(default)] + #[allow(dead_code)] // kept for podman API compat pub exit_code: i64, } #[derive(Debug, Clone, serde::Deserialize)] pub struct PortMappingEntry { + #[allow(dead_code)] // kept for podman API compat pub host_port: u16, + #[allow(dead_code)] // kept for podman API compat pub container_port: u16, + #[allow(dead_code)] // kept for podman API compat pub protocol: String, #[serde(default)] + #[allow(dead_code)] // kept for podman API compat pub host_ip: String, } @@ -187,11 +201,13 @@ pub struct PortMappingEntry { #[serde(rename_all = "PascalCase")] pub struct PodmanEvent { #[serde(rename = "Type")] + #[allow(dead_code)] // kept for podman API compat pub event_type: String, pub action: String, #[serde(default)] pub actor: EventActor, #[serde(rename = "timeNano", default)] + #[allow(dead_code)] // kept for podman API compat pub time_nano: i64, } @@ -407,7 +423,7 @@ impl PodmanClient { timeout_secs: u32, ) -> Result<(), PodmanApiError> { validate_name(name)?; - let http_timeout = Duration::from_secs(timeout_secs as u64 + 5); + let http_timeout = Duration::from_secs(u64::from(timeout_secs) + 5); let (status, bytes) = self .request( hyper::Method::POST, @@ -614,17 +630,15 @@ impl PodmanClient { } // The response is NDJSON. Check the last line for an error field. let body = String::from_utf8_lossy(&bytes); - if let Some(last_line) = body.lines().filter(|l| !l.is_empty()).last() { - if let Ok(obj) = serde_json::from_str::(last_line) { - if let Some(err) = obj.get("error").and_then(|v| v.as_str()) { - if !err.is_empty() { - return Err(PodmanApiError::Api { - status: 500, - message: format!("image pull failed: {err}"), - }); - } - } - } + if let Some(last_line) = body.lines().rfind(|l| !l.is_empty()) + && let Ok(obj) = serde_json::from_str::(last_line) + && let Some(err) = obj.get("error").and_then(|v| v.as_str()) + && !err.is_empty() + { + return Err(PodmanApiError::Api { + status: 500, + message: format!("image pull failed: {err}"), + }); } Ok(()) } @@ -730,7 +744,7 @@ impl PodmanClient { // Parse complete newline-delimited JSON lines. while let Some(pos) = buffer.iter().position(|&b| b == b'\n') { let line: Vec = buffer.drain(..=pos).collect(); - let trimmed = line.strip_suffix(&[b'\n']).unwrap_or(&line); + let trimmed = line.strip_suffix(b"\n").unwrap_or(&line); if trimmed.is_empty() { continue; } diff --git a/crates/openshell-driver-podman/src/config.rs b/crates/openshell-driver-podman/src/config.rs index e90eae3e70..1586002abe 100644 --- a/crates/openshell-driver-podman/src/config.rs +++ b/crates/openshell-driver-podman/src/config.rs @@ -121,12 +121,13 @@ impl PodmanComputeConfig { } #[cfg(target_os = "linux")] { - if let Ok(xdg) = std::env::var("XDG_RUNTIME_DIR") { - PathBuf::from(xdg).join("podman/podman.sock") - } else { - let uid = nix::unistd::getuid(); - PathBuf::from(format!("/run/user/{uid}/podman/podman.sock")) - } + std::env::var("XDG_RUNTIME_DIR").map_or_else( + |_| { + let uid = nix::unistd::getuid(); + PathBuf::from(format!("/run/user/{uid}/podman/podman.sock")) + }, + |xdg| PathBuf::from(xdg).join("podman/podman.sock"), + ) } } } diff --git a/crates/openshell-driver-podman/src/container.rs b/crates/openshell-driver-podman/src/container.rs index d992881fe3..cc7bbc5191 100644 --- a/crates/openshell-driver-podman/src/container.rs +++ b/crates/openshell-driver-podman/src/container.rs @@ -48,7 +48,7 @@ pub fn secret_name(sandbox_id: &str) -> String { /// Truncate a container ID to 12 characters (standard short form). #[must_use] -pub(crate) fn short_id(id: &str) -> String { +pub fn short_id(id: &str) -> String { id.chars().take(12).collect() } @@ -80,7 +80,7 @@ struct ContainerSpec { healthconfig: HealthConfig, resource_limits: ResourceLimits, /// Env-type secrets: map of `ENV_VAR_NAME → secret_name`. - /// Podman's libpod SpecGenerator uses `secret_env` (a flat map) for + /// Podman's libpod `SpecGenerator` uses `secret_env` (a flat map) for /// environment-variable injection, distinct from `secrets` which only /// handles file-mounted secrets under `/run/secrets/`. secret_env: BTreeMap, @@ -90,17 +90,21 @@ struct ContainerSpec { /// the gateway server running on the host in rootless mode. hostadd: Vec, netns: NetNS, + // Matches libpod's network spec format, which is `{name: {opts}}` where + // empty opts is a unit struct rather than `()`. Keep as a map so JSON + // serialization matches the API exactly. + #[allow(clippy::zero_sized_map_values)] networks: BTreeMap, #[serde(skip_serializing_if = "Option::is_none")] devices: Option>, - /// Extra mounts for the libpod SpecGenerator (e.g. tmpfs entries). + /// Extra mounts for the libpod `SpecGenerator` (e.g. tmpfs entries). mounts: Vec, - /// Port mappings from host to container. Using host_port=0 requests an + /// Port mappings from host to container. Using `host_port=0` requests an /// ephemeral port, readable back from the inspect response. portmappings: Vec, } -/// A port mapping entry for the libpod SpecGenerator. +/// A port mapping entry for the libpod `SpecGenerator`. #[derive(Serialize)] struct PortMapping { host_port: u16, @@ -112,12 +116,12 @@ struct PortMapping { /// /// Unlike `volumes` (named Podman volumes) or `image_volumes` (OCI image /// mounts resolved at the libpod layer), these mounts are passed to the -/// libpod SpecGenerator and support arbitrary mount types (e.g. tmpfs). +/// libpod `SpecGenerator` and support arbitrary mount types (e.g. tmpfs). /// Field names must be lowercase to match the libpod JSON schema. #[derive(Serialize)] struct Mount { #[serde(rename = "type")] - mount_type: String, + kind: String, source: String, destination: String, #[serde(skip_serializing_if = "Vec::is_empty")] @@ -331,6 +335,9 @@ pub fn build_container_spec(sandbox: &DriverSandbox, config: &PodmanComputeConfi let devices = build_devices(sandbox); // Network configuration -- always bridge mode. + // Matches libpod's network spec format `{name: {opts}}`; the unit-struct + // value mirrors empty opts in the JSON. + #[allow(clippy::zero_sized_map_values)] let mut networks = BTreeMap::new(); networks.insert(config.network_name.clone(), NetworkAttachment {}); @@ -456,7 +463,7 @@ pub fn build_container_spec(sandbox: &DriverSandbox, config: &PodmanComputeConfi // fails with EPERM. A private tmpfs gives the supervisor its own writable // /run/netns without needing host filesystem access. mounts: vec![Mount { - mount_type: "tmpfs".into(), + kind: "tmpfs".into(), source: "tmpfs".into(), destination: "/run/netns".into(), options: vec!["rw".into(), "nosuid".into(), "nodev".into()], diff --git a/crates/openshell-driver-podman/src/driver.rs b/crates/openshell-driver-podman/src/driver.rs index dff95a5324..ae9492d74a 100644 --- a/crates/openshell-driver-podman/src/driver.rs +++ b/crates/openshell-driver-podman/src/driver.rs @@ -154,8 +154,8 @@ impl PodmanComputeDriver { } /// Report driver capabilities. - pub async fn capabilities(&self) -> Result { - let supports_gpu = self.has_gpu_capacity(); + pub fn capabilities(&self) -> Result { + let supports_gpu = Self::has_gpu_capacity(); Ok(GetCapabilitiesResponse { driver_name: "podman".to_string(), driver_version: openshell_core::VERSION.to_string(), @@ -175,17 +175,17 @@ impl PodmanComputeDriver { /// The Podman system info response doesn't directly list CDI devices in all /// versions. As a heuristic, check if the NVIDIA device node exists (this /// works for both rootful and rootless). - fn has_gpu_capacity(&self) -> bool { + fn has_gpu_capacity() -> bool { std::path::Path::new("/dev/nvidia0").exists() } /// Validate a sandbox before creation. - pub async fn validate_sandbox_create( + pub fn validate_sandbox_create( &self, sandbox: &DriverSandbox, ) -> Result<(), ComputeDriverError> { let gpu_requested = sandbox.spec.as_ref().is_some_and(|s| s.gpu); - if gpu_requested && !self.has_gpu_capacity() { + if gpu_requested && !Self::has_gpu_capacity() { return Err(ComputeDriverError::Precondition( "GPU sandbox requested, but no NVIDIA GPU devices are available.".to_string(), )); @@ -664,7 +664,7 @@ mod tests { .len(); let socket_path_for_task = socket_path.clone(); let log_for_task = request_log.clone(); - let queue_for_task = response_queue.clone(); + let queue_for_task = response_queue; let handle = tokio::spawn(async move { for _ in 0..expected { let (stream, _) = listener.accept().await.expect("test stub should accept"); @@ -677,11 +677,10 @@ mod tests { let log = log.clone(); let queue = queue.clone(); async move { - let path = req - .uri() - .path_and_query() - .map(|pq| pq.as_str().to_string()) - .unwrap_or_else(|| req.uri().path().to_string()); + let path = req.uri().path_and_query().map_or_else( + || req.uri().path().to_string(), + |pq| pq.as_str().to_string(), + ); log.lock() .expect("request log lock should not be poisoned") .push(format!("{} {}", req.method(), path)); diff --git a/crates/openshell-driver-podman/src/grpc.rs b/crates/openshell-driver-podman/src/grpc.rs index d11f95b78f..df4c90d13f 100644 --- a/crates/openshell-driver-podman/src/grpc.rs +++ b/crates/openshell-driver-podman/src/grpc.rs @@ -1,6 +1,8 @@ // SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 +#![allow(clippy::result_large_err)] // gRPC handlers return Result<_, tonic::Status> + use futures::{Stream, StreamExt}; use openshell_core::proto::compute::v1::{ CreateSandboxRequest, CreateSandboxResponse, DeleteSandboxRequest, DeleteSandboxResponse, @@ -35,7 +37,6 @@ impl ComputeDriver for ComputeDriverService { ) -> Result, Status> { self.driver .capabilities() - .await .map(Response::new) .map_err(status_from_driver_error) } @@ -50,7 +51,6 @@ impl ComputeDriver for ComputeDriverService { .ok_or_else(|| Status::invalid_argument("sandbox is required"))?; self.driver .validate_sandbox_create(&sandbox) - .await .map_err(status_from_driver_error)?; Ok(Response::new(ValidateSandboxCreateResponse {})) } @@ -247,7 +247,7 @@ mod tests { .len(); let socket_path_for_task = socket_path.clone(); let log_for_task = request_log.clone(); - let queue_for_task = response_queue.clone(); + let queue_for_task = response_queue; let handle = tokio::spawn(async move { for _ in 0..expected { let (stream, _) = listener.accept().await.expect("test stub should accept"); @@ -260,11 +260,10 @@ mod tests { let log = log.clone(); let queue = queue.clone(); async move { - let path = req - .uri() - .path_and_query() - .map(|pq| pq.as_str().to_string()) - .unwrap_or_else(|| req.uri().path().to_string()); + let path = req.uri().path_and_query().map_or_else( + || req.uri().path().to_string(), + |pq| pq.as_str().to_string(), + ); log.lock() .expect("request log lock should not be poisoned") .push(format!("{} {}", req.method(), path)); diff --git a/crates/openshell-driver-podman/src/watcher.rs b/crates/openshell-driver-podman/src/watcher.rs index 69cab6d884..54606ea442 100644 --- a/crates/openshell-driver-podman/src/watcher.rs +++ b/crates/openshell-driver-podman/src/watcher.rs @@ -103,12 +103,12 @@ pub async fn start_watch(client: PodmanClient) -> Result Result { - if let Some(we) = map_podman_event(&event, &client).await { - if tx.send(Ok(we)).await.is_err() { - return; - } + if let Some(we) = map_podman_event(&event, &client).await + && tx.send(Ok(we)).await.is_err() + { + return; } } Err(e) => { @@ -292,7 +292,7 @@ pub fn driver_sandbox_from_inspect(inspect: &ContainerInspect) -> Option Option { +pub fn driver_sandbox_from_list_entry(entry: &ContainerListEntry) -> Option { let sandbox_id = entry.labels.get(LABEL_SANDBOX_ID)?.clone(); let sandbox_name = entry .labels @@ -508,9 +508,8 @@ mod tests { }; let payload = event.payload.unwrap(); - let sandbox_event = match payload { - watch_sandboxes_event::Payload::Sandbox(s) => s, - _ => panic!("expected Sandbox payload"), + let watch_sandboxes_event::Payload::Sandbox(sandbox_event) = payload else { + panic!("expected Sandbox payload") }; let status = sandbox_event.sandbox.unwrap().status.unwrap(); assert_eq!(status.conditions.len(), 1); diff --git a/crates/openshell-driver-vm/build.rs b/crates/openshell-driver-vm/build.rs index 981cf8ff8a..e10a1dde0b 100644 --- a/crates/openshell-driver-vm/build.rs +++ b/crates/openshell-driver-vm/build.rs @@ -7,7 +7,7 @@ //! artifacts it needs to boot base VMs without depending on the openshell-vm //! binary or crate. -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::{env, fs}; fn main() { @@ -126,7 +126,7 @@ fn main() { e ) }); - let size = fs::metadata(&dst_path).map(|m| m.len()).unwrap_or(0); + let size = fs::metadata(&dst_path).map_or(0, |m| m.len()); println!("cargo:warning=Embedded {src_name}: {size} bytes"); } @@ -145,7 +145,7 @@ fn main() { } } -fn generate_stub_resources(out_dir: &std::path::Path, names: &[&str]) { +fn generate_stub_resources(out_dir: &Path, names: &[&str]) { for name in names { let path = out_dir.join(name); if !path.exists() { diff --git a/crates/openshell-driver-vm/src/driver.rs b/crates/openshell-driver-vm/src/driver.rs index e5520b5a18..704f916100 100644 --- a/crates/openshell-driver-vm/src/driver.rs +++ b/crates/openshell-driver-vm/src/driver.rs @@ -270,10 +270,16 @@ impl VmDriver { } } - pub async fn validate_sandbox(&self, sandbox: &Sandbox) -> Result<(), Status> { + // `tonic::Status` is large but is the standard error type across the + // gRPC API surface; boxing here would diverge from every other handler. + #[allow(clippy::result_large_err)] + pub fn validate_sandbox(&self, sandbox: &Sandbox) -> Result<(), Status> { validate_vm_sandbox(sandbox, self.config.gpu_enabled) } + // `tonic::Status` is large but is the standard error type across the + // gRPC API surface; boxing here would diverge from every other handler. + #[allow(clippy::result_large_err)] pub async fn create_sandbox(&self, sandbox: &Sandbox) -> Result { validate_vm_sandbox(sandbox, self.config.gpu_enabled)?; @@ -324,7 +330,7 @@ impl VmDriver { .map_err(|e| Status::internal(format!("GPU inventory lock poisoned: {e}"))) .and_then(|mut inv| { inv.assign(&sandbox.id, gpu_device) - .map_err(|e| Status::failed_precondition(e)) + .map_err(Status::failed_precondition) }) { Ok(assignment) => { tracing::info!( @@ -358,7 +364,7 @@ impl VmDriver { // Compute the endpoint override before building the env so // there is a single OPENSHELL_ENDPOINT value in the env list. - let endpoint_override = if gpu_bdf.is_some() { + let endpoint_override = if let Some(bdf) = gpu_bdf.as_ref() { let subnet = match self .subnet_allocator .lock() @@ -366,7 +372,7 @@ impl VmDriver { .and_then(|mut alloc| { alloc .allocate(&sandbox.id) - .map_err(|e| Status::failed_precondition(e)) + .map_err(Status::failed_precondition) }) { Ok(s) => s, Err(err) => { @@ -395,7 +401,7 @@ impl VmDriver { command .arg("--vm-mem-mib") .arg(self.config.gpu_mem_mib.to_string()); - command.arg("--vm-gpu-bdf").arg(gpu_bdf.as_ref().unwrap()); + command.arg("--vm-gpu-bdf").arg(bdf); command.arg("--vm-tap-device").arg(&tap); command .arg("--vm-guest-ip") @@ -547,14 +553,14 @@ impl VmDriver { sandbox_name: &str, ) -> Result, Status> { let registry = self.registry.lock().await; - let sandbox = if !sandbox_id.is_empty() { + let sandbox = if sandbox_id.is_empty() { registry - .get(sandbox_id) + .values() + .find(|record| record.snapshot.name == sandbox_name) .map(|record| record.snapshot.clone()) } else { registry - .values() - .find(|record| record.snapshot.name == sandbox_name) + .get(sandbox_id) .map(|record| record.snapshot.clone()) }; Ok(sandbox) @@ -571,10 +577,10 @@ impl VmDriver { } fn release_gpu_and_subnet(&self, sandbox_id: &str) { - if let Some(ref inventory) = self.gpu_inventory { - if let Ok(mut inv) = inventory.lock() { - inv.release(sandbox_id); - } + if let Some(inventory) = self.gpu_inventory.as_ref() + && let Ok(mut inv) = inventory.lock() + { + inv.release(sandbox_id); } if let Ok(mut alloc) = self.subnet_allocator.lock() { alloc.release(sandbox_id); @@ -633,10 +639,10 @@ impl VmDriver { }; if let Some(status) = exit_status { - let message = match status.code() { - Some(code) => format!("VM process exited with status {code}"), - None => "VM process exited".to_string(), - }; + let message = status.code().map_or_else( + || "VM process exited".to_string(), + |code| format!("VM process exited with status {code}"), + ); if let Some(snapshot) = self .set_snapshot_condition( &sandbox_id, @@ -727,7 +733,7 @@ impl ComputeDriver for VmDriver { .into_inner() .sandbox .ok_or_else(|| Status::invalid_argument("sandbox is required"))?; - self.validate_sandbox(&sandbox).await?; + self.validate_sandbox(&sandbox)?; Ok(Response::new(ValidateSandboxCreateResponse {})) } @@ -842,7 +848,7 @@ impl ComputeDriver for VmDriver { return; } } - Err(broadcast::error::RecvError::Lagged(_)) => continue, + Err(broadcast::error::RecvError::Lagged(_)) => {} Err(broadcast::error::RecvError::Closed) => return, } } @@ -853,7 +859,7 @@ impl ComputeDriver for VmDriver { } #[cfg(target_os = "linux")] -#[allow(unsafe_code)] +#[allow(unsafe_code)] // libc::geteuid is a thin syscall wrapper fn check_gpu_privileges() -> Result<(), String> { if unsafe { libc::geteuid() } != 0 { return Err( @@ -865,6 +871,9 @@ fn check_gpu_privileges() -> Result<(), String> { Ok(()) } +// `tonic::Status` is ~176 bytes; it's the standard error type across the +// gRPC API surface, so boxing here would diverge from every other handler. +#[allow(clippy::result_large_err)] fn validate_vm_sandbox(sandbox: &Sandbox, gpu_enabled: bool) -> Result<(), Status> { let spec = sandbox .spec @@ -957,9 +966,10 @@ fn build_guest_environment( config: &VmDriverConfig, endpoint_override: Option<&str>, ) -> Vec { - let openshell_endpoint = endpoint_override - .map(String::from) - .unwrap_or_else(|| guest_visible_openshell_endpoint(&config.openshell_endpoint)); + let openshell_endpoint = endpoint_override.map_or_else( + || guest_visible_openshell_endpoint(&config.openshell_endpoint), + String::from, + ); let mut environment = HashMap::from([ ("HOME".to_string(), "/root".to_string()), ( @@ -1052,7 +1062,7 @@ async fn copy_guest_tls_material( async fn terminate_vm_process(child: &mut Child) -> Result<(), std::io::Error> { if let Some(pid) = child.id() - && let Err(err) = kill(Pid::from_raw(pid as i32), Signal::SIGTERM) + && let Err(err) = kill(Pid::from_raw(pid.cast_signed()), Signal::SIGTERM) && err != Errno::ESRCH { return Err(std::io::Error::other(format!( @@ -1146,7 +1156,9 @@ fn platform_event(source: &str, event_type: &str, reason: &str, message: String) fn current_time_ms() -> i64 { std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) - .map_or(0, |duration| duration.as_millis() as i64) + .map_or(0, |duration| { + i64::try_from(duration.as_millis()).unwrap_or(i64::MAX) + }) } #[cfg(test)] @@ -1210,13 +1222,12 @@ mod tests { spec: Some(SandboxSpec { template: Some(SandboxTemplate { platform_config: Some(Struct { - fields: [( + fields: std::iter::once(( "runtime_class_name".to_string(), Value { kind: Some(Kind::StringValue("kata".to_string())), }, - )] - .into_iter() + )) .collect(), }), ..Default::default() diff --git a/crates/openshell-driver-vm/src/embedded_runtime.rs b/crates/openshell-driver-vm/src/embedded_runtime.rs index 63f83b874a..70626edd98 100644 --- a/crates/openshell-driver-vm/src/embedded_runtime.rs +++ b/crates/openshell-driver-vm/src/embedded_runtime.rs @@ -115,7 +115,7 @@ pub fn validate_runtime_dir(dir: &Path) -> Result<(), String> { if !path.is_file() { return Err(format!("missing runtime file: {}", path.display())); } - let size = fs::metadata(path).map(|m| m.len()).unwrap_or(0); + let size = fs::metadata(path).map_or(0, |m| m.len()); if size == 0 { return Err(format!("runtime file is empty (stub): {}", path.display())); } @@ -134,10 +134,11 @@ fn runtime_cache_key() -> String { let sample = &chunk[..chunk.len().min(64)]; let mut word: u64 = 0; for (offset, byte) in sample.iter().enumerate() { - word ^= (*byte as u64) << ((offset % 8) * 8); + word ^= u64::from(*byte) << ((offset % 8) * 8); } - fp ^= word.rotate_left((index as u32) * 13 + 7); - fp ^= (chunk.len() as u64).rotate_left((index as u32) * 17 + 3); + let index_u32 = u32::try_from(index).unwrap_or(u32::MAX); + fp ^= word.rotate_left(index_u32 * 13 + 7); + fp ^= (chunk.len() as u64).rotate_left(index_u32 * 17 + 3); } format!("{VERSION}-{fp:016x}") } diff --git a/crates/openshell-driver-vm/src/ffi.rs b/crates/openshell-driver-vm/src/ffi.rs index a81b150afb..db5d3ec108 100644 --- a/crates/openshell-driver-vm/src/ffi.rs +++ b/crates/openshell-driver-vm/src/ffi.rs @@ -59,6 +59,9 @@ type KrunAddNetUnixstream = unsafe extern "C" fn( flags: u32, ) -> i32; +// Field names mirror the libkrun C API symbol names (`krun_*`); preserving +// the prefix keeps the FFI binding 1:1 with the upstream library. +#[allow(clippy::struct_field_names)] pub struct LibKrun { pub krun_init_log: KrunInitLog, pub krun_create_ctx: KrunCreateCtx, @@ -155,7 +158,10 @@ fn preload_runtime_support_libraries(runtime_dir: &Path) -> Result, .is_some_and(|name| { #[cfg(target_os = "macos")] { - name.starts_with("libkrunfw") && name.ends_with(".dylib") + name.starts_with("libkrunfw") + && Path::new(name) + .extension() + .is_some_and(|ext| ext.eq_ignore_ascii_case("dylib")) } #[cfg(not(target_os = "macos"))] { diff --git a/crates/openshell-driver-vm/src/gpu.rs b/crates/openshell-driver-vm/src/gpu.rs index 9089a166b8..dc5883b5ba 100644 --- a/crates/openshell-driver-vm/src/gpu.rs +++ b/crates/openshell-driver-vm/src/gpu.rs @@ -50,14 +50,17 @@ impl GpuInventory { } pub fn gpu_count(&self) -> u32 { - self.slots.len() as u32 + u32::try_from(self.slots.len()).unwrap_or(u32::MAX) } pub fn available_count(&self) -> u32 { - self.slots - .iter() - .filter(|s| s.assigned_to.is_none()) - .count() as u32 + u32::try_from( + self.slots + .iter() + .filter(|s| s.assigned_to.is_none()) + .count(), + ) + .unwrap_or(u32::MAX) } /// Assign a GPU to a sandbox. Returns the assignment details including BDF. @@ -136,7 +139,7 @@ impl GpuInventory { sandbox_id: id.clone(), bound_at_ms: std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) - .map_or(0, |d| d.as_millis() as i64), + .map_or(0, |d| i64::try_from(d.as_millis()).unwrap_or(i64::MAX)), }) }) .collect(); @@ -188,7 +191,7 @@ impl SubnetAllocator { let pool_size = 1u32 << (32 - self.prefix_len); let max_subnets = pool_size / 4; - if self.allocated.len() as u32 >= max_subnets { + if u32::try_from(self.allocated.len()).unwrap_or(u32::MAX) >= max_subnets { return Err("subnet pool exhausted".to_string()); } @@ -235,10 +238,10 @@ pub fn allocate_vsock_cid() -> u32 { /// Generate a locally-administered MAC from sandbox ID using FNV-1a. pub fn mac_from_sandbox_id(sandbox_id: &str) -> [u8; 6] { - let mut hash: u64 = 0xcbf29ce484222325; + let mut hash: u64 = 0xcbf2_9ce4_8422_2325; for byte in sandbox_id.as_bytes() { hash ^= u64::from(*byte); - hash = hash.wrapping_mul(0x100000001b3); + hash = hash.wrapping_mul(0x0000_0100_0000_01b3); } let bytes = hash.to_le_bytes(); let mut mac = [bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5]]; @@ -248,8 +251,12 @@ pub fn mac_from_sandbox_id(sandbox_id: &str) -> [u8; 6] { /// TAP device name from sandbox ID (fits `IFNAMSIZ=16`). pub fn tap_device_name(sandbox_id: &str) -> String { - let end = sandbox_id.len().min(8); - let end = sandbox_id.floor_char_boundary(end); + let mut end = sandbox_id.len().min(8); + // Walk back to a UTF-8 char boundary (str::floor_char_boundary requires + // Rust 1.91 — we still build on older toolchains). + while end > 0 && !sandbox_id.is_char_boundary(end) { + end -= 1; + } let prefix = &sandbox_id[..end]; format!("vmtap-{prefix}") } diff --git a/crates/openshell-driver-vm/src/main.rs b/crates/openshell-driver-vm/src/main.rs index 94169a61fb..ca1842596a 100644 --- a/crates/openshell-driver-vm/src/main.rs +++ b/crates/openshell-driver-vm/src/main.rs @@ -301,6 +301,8 @@ fn maybe_reexec_internal_vm_with_runtime_env() -> Result<()> { } #[cfg(not(target_os = "macos"))] +// Signature must match the macOS variant which can fail. +#[allow(clippy::unnecessary_wraps)] fn maybe_reexec_internal_vm_with_runtime_env() -> Result<()> { Ok(()) } diff --git a/crates/openshell-driver-vm/src/procguard.rs b/crates/openshell-driver-vm/src/procguard.rs index 1d91880f74..fd4d3c872c 100644 --- a/crates/openshell-driver-vm/src/procguard.rs +++ b/crates/openshell-driver-vm/src/procguard.rs @@ -43,11 +43,13 @@ pub fn die_with_parent() -> Result<(), String> { die_with_parent_cleanup(|| ()) } -/// Like [`die_with_parent`], but run `cleanup` (best-effort, -/// async-signal-unsafe — it runs on the helper thread) immediately -/// before terminating the process. Use this when we own children that -/// cannot arm their own procguard; the cleanup hook is the only chance -/// we get to send them SIGTERM after the kernel reparents us. +/// Like [`die_with_parent`], but run `cleanup` before terminating. +/// +/// The cleanup hook is best-effort and async-signal-unsafe — it runs on +/// the helper thread immediately before terminating the process. Use this +/// when we own children that cannot arm their own procguard; the cleanup +/// hook is the only chance we get to send them SIGTERM after the kernel +/// reparents us. /// /// On Linux the cleanup is a no-op: `PR_SET_PDEATHSIG` delivers SIGKILL /// directly to us, there is no Rust-controlled moment between "parent diff --git a/crates/openshell-driver-vm/src/rootfs.rs b/crates/openshell-driver-vm/src/rootfs.rs index ef3e3409d1..7c7fa19f4a 100644 --- a/crates/openshell-driver-vm/src/rootfs.rs +++ b/crates/openshell-driver-vm/src/rootfs.rs @@ -41,9 +41,7 @@ fn extract_variant(blob: &[u8], variant: &str, empty_msg: &str, dest: &Path) -> let marker_path = dest.join(ROOTFS_VARIANT_MARKER); if dest.is_dir() - && fs::read_to_string(&marker_path) - .map(|value| value.trim() == expected_marker) - .unwrap_or(false) + && fs::read_to_string(&marker_path).is_ok_and(|value| value.trim() == expected_marker) { return Ok(()); } diff --git a/crates/openshell-driver-vm/src/runtime.rs b/crates/openshell-driver-vm/src/runtime.rs index 62f2e314c9..c063da10b9 100644 --- a/crates/openshell-driver-vm/src/runtime.rs +++ b/crates/openshell-driver-vm/src/runtime.rs @@ -24,12 +24,26 @@ static CHILD_PID: AtomicI32 = AtomicI32::new(0); /// launcher (especially on macOS where `PR_SET_PDEATHSIG` is absent). static GVPROXY_PID: AtomicI32 = AtomicI32::new(0); -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum VmBackend { Libkrun, Qemu, } +// virtio-net feature bits (see Linux `include/uapi/linux/virtio_net.h`). +const NET_FEATURE_CSUM: u32 = 1 << 0; +const NET_FEATURE_GUEST_CSUM: u32 = 1 << 1; +const NET_FEATURE_GUEST_TSO4: u32 = 1 << 7; +const NET_FEATURE_GUEST_UFO: u32 = 1 << 10; +const NET_FEATURE_HOST_TSO4: u32 = 1 << 11; +const NET_FEATURE_HOST_UFO: u32 = 1 << 14; +const COMPAT_NET_FEATURES: u32 = NET_FEATURE_CSUM + | NET_FEATURE_GUEST_CSUM + | NET_FEATURE_GUEST_TSO4 + | NET_FEATURE_GUEST_UFO + | NET_FEATURE_HOST_TSO4 + | NET_FEATURE_HOST_UFO; + pub struct VmLaunchConfig { pub rootfs: PathBuf, pub vcpus: u8, @@ -154,7 +168,7 @@ fn run_qemu_vm(config: &VmLaunchConfig) -> Result<(), String> { let virtiofsd_child = virtiofsd_cmd .spawn() .map_err(|e| format!("failed to start virtiofsd: {e}"))?; - let virtiofsd_pid = virtiofsd_child.id() as i32; + let virtiofsd_pid = virtiofsd_child.id().cast_signed(); GVPROXY_PID.store(virtiofsd_pid, Ordering::Relaxed); let mut virtiofsd_guard = GvproxyGuard::new(virtiofsd_child); @@ -240,7 +254,7 @@ fn run_qemu_vm(config: &VmLaunchConfig) -> Result<(), String> { .spawn() .map_err(|e| format!("failed to start QEMU: {e}"))?; - let qemu_pid = qemu_child.id() as i32; + let qemu_pid = qemu_child.id().cast_signed(); install_signal_forwarding(qemu_pid); let status = qemu_child @@ -274,7 +288,8 @@ fn write_guest_env_file(rootfs: &Path, env_vars: &[String]) -> Result<(), String let mut content = String::new(); for var in env_vars { if let Some((key, value)) = var.split_once('=') { - content.push_str(&format!("export {key}=\"{}\"\n", shell_escape(value))); + use std::fmt::Write as _; + let _ = writeln!(content, "export {key}=\"{}\"", shell_escape(value)); } } std::fs::write(&env_file, &content).map_err(|e| format!("write guest env file: {e}"))?; @@ -301,12 +316,12 @@ fn build_kernel_cmdline(config: &VmLaunchConfig) -> String { format!("init={}", config.exec_path), ]; - if let Some(ip) = &config.guest_ip { - if let Some(host_ip) = &config.host_ip { - parts.push(format!("ip={ip}::{host_ip}:255.255.255.252:sandbox::off")); - parts.push(format!("VM_NET_IP={ip}")); - parts.push(format!("VM_NET_GW={host_ip}")); - } + if let Some(ip) = &config.guest_ip + && let Some(host_ip) = &config.host_ip + { + parts.push(format!("ip={ip}::{host_ip}:255.255.255.252:sandbox::off")); + parts.push(format!("VM_NET_IP={ip}")); + parts.push(format!("VM_NET_GW={host_ip}")); } if let Some(dns) = host_dns_server() { @@ -344,10 +359,11 @@ fn host_dns_server() -> Option { None } -/// Remove leftover `vmtap-*` interfaces from previous driver runs that -/// were not torn down (e.g. the launcher was SIGKILLed before teardown). -/// Called once at driver startup so stale interfaces cannot cause subnet -/// routing conflicts with newly allocated TAPs. +/// Remove leftover `vmtap-*` interfaces from previous driver runs. +/// +/// Called once at driver startup for interfaces that were not torn down +/// (e.g. the launcher was `SIGKILL`-ed before teardown), so stale +/// interfaces cannot cause subnet routing conflicts with newly allocated TAPs. pub fn cleanup_stale_tap_interfaces() { let Ok(entries) = std::fs::read_dir("/sys/class/net") else { return; @@ -386,10 +402,10 @@ fn read_tap_host_ip(device: &str) -> Option { let stdout = String::from_utf8_lossy(&output.stdout); // Format: "28: vmtap-xxx inet 10.0.128.1/30 ..." for token in stdout.split_whitespace() { - if let Some((ip, _prefix)) = token.split_once('/') { - if ip.parse::().is_ok() { - return Some(ip.to_string()); - } + if let Some((ip, _prefix)) = token.split_once('/') + && ip.parse::().is_ok() + { + return Some(ip.to_string()); } } None @@ -552,13 +568,14 @@ fn teardown_tap_networking(tap_device: &str, host_ip: &str, gateway_port: u16) { } fn tap_subnet_from_host_ip(host_ip: &str) -> String { - if let Ok(ip) = host_ip.parse::() { - let base = u32::from(ip) & !3; - let base_ip = std::net::Ipv4Addr::from(base); - format!("{base_ip}/30") - } else { - format!("{host_ip}/30") - } + host_ip.parse::().map_or_else( + |_| format!("{host_ip}/30"), + |ip| { + let base = u32::from(ip) & !3; + let base_ip = std::net::Ipv4Addr::from(base); + format!("{base_ip}/30") + }, + ) } fn enable_ip_forwarding() -> Result<(), String> { @@ -778,7 +795,7 @@ fn run_libkrun_vm(config: &VmLaunchConfig) -> Result<(), String> { // The procguard cleanup reads GVPROXY_PID atomically. Storing it // here makes the callback able to SIGTERM gvproxy if the driver // dies from this moment onward. - GVPROXY_PID.store(child.id() as i32, Ordering::Relaxed); + GVPROXY_PID.store(child.id().cast_signed(), Ordering::Relaxed); wait_for_path(&net_sock, Duration::from_secs(5), "gvproxy data socket")?; @@ -786,18 +803,6 @@ fn run_libkrun_vm(config: &VmLaunchConfig) -> Result<(), String> { vm.add_vsock(0)?; let mac: [u8; 6] = [0x5a, 0x94, 0xef, 0xe4, 0x0c, 0xee]; - const NET_FEATURE_CSUM: u32 = 1 << 0; - const NET_FEATURE_GUEST_CSUM: u32 = 1 << 1; - const NET_FEATURE_GUEST_TSO4: u32 = 1 << 7; - const NET_FEATURE_GUEST_UFO: u32 = 1 << 10; - const NET_FEATURE_HOST_TSO4: u32 = 1 << 11; - const NET_FEATURE_HOST_UFO: u32 = 1 << 14; - const COMPAT_NET_FEATURES: u32 = NET_FEATURE_CSUM - | NET_FEATURE_GUEST_CSUM - | NET_FEATURE_GUEST_TSO4 - | NET_FEATURE_GUEST_UFO - | NET_FEATURE_HOST_TSO4 - | NET_FEATURE_HOST_UFO; #[cfg(target_os = "linux")] vm.add_net_unixstream(&net_sock, &mac, COMPAT_NET_FEATURES)?; @@ -980,7 +985,7 @@ impl VmContext { Ok(Self { krun, - ctx_id: ctx_id as u32, + ctx_id: ctx_id.cast_unsigned(), }) } @@ -1078,10 +1083,10 @@ impl VmContext { fn set_exec(&self, exec_path: &str, args: &[String], env: &[String]) -> Result<(), String> { let exec_c = CString::new(exec_path).map_err(|e| format!("invalid exec path: {e}"))?; - let argv_strs: Vec<&str> = args.iter().map(String::as_str).collect(); - let (_argv_owners, argv_ptrs) = c_string_array(&argv_strs)?; - let env_strs: Vec<&str> = env.iter().map(String::as_str).collect(); - let (_env_owners, env_ptrs) = c_string_array(&env_strs)?; + let argv_slices: Vec<&str> = args.iter().map(String::as_str).collect(); + let (_argv_owners, argv_ptrs) = c_string_array(&argv_slices)?; + let env_slices: Vec<&str> = env.iter().map(String::as_str).collect(); + let (_env_owners, env_ptrs) = c_string_array(&env_slices)?; check( unsafe { @@ -1154,24 +1159,26 @@ fn wait_for_path(path: &Path, timeout: Duration, label: &str) -> Result<(), Stri } fn hash_path_id(path: &Path) -> String { - let mut hash: u64 = 0xcbf29ce484222325; + let mut hash: u64 = 0xcbf2_9ce4_8422_2325; for byte in path.to_string_lossy().as_bytes() { hash ^= u64::from(*byte); - hash = hash.wrapping_mul(0x100000001b3); + hash = hash.wrapping_mul(0x0100_0000_01b3); } format!("{:012x}", hash & 0x0000_ffff_ffff_ffff) } fn secure_socket_base(subdir: &str) -> Result { - let base = if let Some(xdg) = std::env::var_os("XDG_RUNTIME_DIR") { - PathBuf::from(xdg) - } else { - let mut base = PathBuf::from("/tmp"); - if !base.is_dir() { - base = std::env::temp_dir(); - } - base - }; + let base = std::env::var_os("XDG_RUNTIME_DIR").map_or_else( + || { + let fallback = PathBuf::from("/tmp"); + if fallback.is_dir() { + fallback + } else { + std::env::temp_dir() + } + }, + PathBuf::from, + ); let dir = base.join(subdir); if dir.exists() { @@ -1298,7 +1305,7 @@ fn path_to_cstring(path: &Path) -> Result { let path = path .to_str() .ok_or_else(|| format!("path is not valid UTF-8: {}", path.display()))?; - CString::new(path).map_err(|e| format!("invalid path string {}: {e}", path)) + CString::new(path).map_err(|e| format!("invalid path string {path}: {e}")) } #[cfg(target_os = "linux")] diff --git a/crates/openshell-ocsf/src/format/shorthand.rs b/crates/openshell-ocsf/src/format/shorthand.rs index 7e2296de9d..42b30fbae9 100644 --- a/crates/openshell-ocsf/src/format/shorthand.rs +++ b/crates/openshell-ocsf/src/format/shorthand.rs @@ -50,7 +50,6 @@ pub fn severity_char(severity_id: u8) -> char { #[must_use] pub fn severity_tag(severity_id: u8) -> &'static str { match severity_id { - 1 => "[INFO]", 2 => "[LOW]", 3 => "[MED]", 4 => "[HIGH]", diff --git a/crates/openshell-ocsf/src/tracing_layers/jsonl_layer.rs b/crates/openshell-ocsf/src/tracing_layers/jsonl_layer.rs index 1f7022ef8e..920483700a 100644 --- a/crates/openshell-ocsf/src/tracing_layers/jsonl_layer.rs +++ b/crates/openshell-ocsf/src/tracing_layers/jsonl_layer.rs @@ -60,10 +60,10 @@ where } // If an enabled flag is set and it reads `false`, skip writing. - if let Some(ref flag) = self.enabled { - if !flag.load(Ordering::Relaxed) { - return; - } + if let Some(ref flag) = self.enabled + && !flag.load(Ordering::Relaxed) + { + return; } if let Some(ocsf_event) = clone_current_event() diff --git a/crates/openshell-ocsf/src/tracing_layers/shorthand_layer.rs b/crates/openshell-ocsf/src/tracing_layers/shorthand_layer.rs index ea75cf0dc1..dee9848ad4 100644 --- a/crates/openshell-ocsf/src/tracing_layers/shorthand_layer.rs +++ b/crates/openshell-ocsf/src/tracing_layers/shorthand_layer.rs @@ -94,6 +94,20 @@ impl tracing::field::Visit for MessageVisitor<'_> { } } +/// Test helper: wraps `Arc>>` so it implements `Write + Send`. +#[cfg(test)] +struct SyncWriter(std::sync::Arc>>); + +#[cfg(test)] +impl Write for SyncWriter { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + self.0.lock().unwrap().write(buf) + } + fn flush(&mut self) -> std::io::Result<()> { + self.0.lock().unwrap().flush() + } +} + #[cfg(test)] mod tests { use super::*; @@ -139,17 +153,3 @@ mod tests { ); } } - -/// Test helper: wraps `Arc>>` so it implements `Write + Send`. -#[cfg(test)] -struct SyncWriter(std::sync::Arc>>); - -#[cfg(test)] -impl Write for SyncWriter { - fn write(&mut self, buf: &[u8]) -> std::io::Result { - self.0.lock().unwrap().write(buf) - } - fn flush(&mut self) -> std::io::Result<()> { - self.0.lock().unwrap().flush() - } -} diff --git a/crates/openshell-policy/src/lib.rs b/crates/openshell-policy/src/lib.rs index 0296879afd..f1abda06bd 100644 --- a/crates/openshell-policy/src/lib.rs +++ b/crates/openshell-policy/src/lib.rs @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 -//! Shared sandbox policy parsing and defaults for OpenShell. +//! Shared sandbox policy parsing and defaults for `OpenShell`. //! //! Provides bidirectional YAML↔proto conversion for sandbox policies. //! @@ -117,6 +117,8 @@ struct NetworkEndpointDef { allow_encoded_slash: bool, } +// Signature dictated by serde's `skip_serializing_if`, which requires `&T`. +#[allow(clippy::trivially_copy_pass_by_ref)] fn is_zero(v: &u16) -> bool { *v == 0 } @@ -369,12 +371,12 @@ fn from_proto(policy: &SandboxPolicy) -> PolicyFile { .query .into_iter() .map(|(key, matcher)| { - let yaml_matcher = if !matcher.any.is_empty() { + let yaml_matcher = if matcher.any.is_empty() { + QueryMatcherDef::Glob(matcher.glob) + } else { QueryMatcherDef::Any(QueryAnyDef { any: matcher.any, }) - } else { - QueryMatcherDef::Glob(matcher.glob) }; (key, yaml_matcher) }) @@ -395,12 +397,12 @@ fn from_proto(policy: &SandboxPolicy) -> PolicyFile { .query .iter() .map(|(key, matcher)| { - let yaml_matcher = if !matcher.any.is_empty() { + let yaml_matcher = if matcher.any.is_empty() { + QueryMatcherDef::Glob(matcher.glob.clone()) + } else { QueryMatcherDef::Any(QueryAnyDef { any: matcher.any.clone(), }) - } else { - QueryMatcherDef::Glob(matcher.glob.clone()) }; (key.clone(), yaml_matcher) }) @@ -535,9 +537,7 @@ pub fn restrictive_default_policy() -> SandboxPolicy { /// the required `"sandbox"` value. Call this before validation so that /// policies without an explicit process section get the correct default. pub fn ensure_sandbox_process_identity(policy: &mut SandboxPolicy) { - let process = policy - .process - .get_or_insert_with(|| ProcessPolicy::default()); + let process = policy.process.get_or_insert_with(ProcessPolicy::default); if process.run_as_user.is_empty() { process.run_as_user = "sandbox".into(); } @@ -812,7 +812,7 @@ network_policies: /// Verify that the network policy `name` field survives the round-trip. #[test] fn round_trip_preserves_policy_name() { - let yaml = r#" + let yaml = r" version: 1 network_policies: my_api: @@ -822,7 +822,7 @@ network_policies: port: 443 binaries: - path: /usr/bin/curl -"#; +"; let proto1 = parse_sandbox_policy(yaml).expect("parse failed"); assert_eq!(proto1.network_policies["my_api"].name, "my-custom-api-name"); @@ -891,7 +891,7 @@ network_policies: #[test] fn parse_policy_with_network_rules() { - let yaml = r#" + let yaml = r" version: 1 network_policies: test: @@ -900,7 +900,7 @@ network_policies: - { host: example.com, port: 443 } binaries: - { path: /usr/bin/curl } -"#; +"; let policy = parse_sandbox_policy(yaml).expect("should parse"); assert_eq!(policy.network_policies.len(), 1); let rule = &policy.network_policies["test"]; @@ -1277,7 +1277,7 @@ network_policies: #[test] fn parse_ports_array() { - let yaml = r#" + let yaml = r" version: 1 network_policies: test: @@ -1286,7 +1286,7 @@ network_policies: - { host: api.example.com, ports: [80, 443] } binaries: - { path: /usr/bin/curl } -"#; +"; let policy = parse_sandbox_policy(yaml).expect("should parse"); let ep = &policy.network_policies["test"].endpoints[0]; assert_eq!(ep.ports, vec![80, 443]); @@ -1296,7 +1296,7 @@ network_policies: #[test] fn parse_single_port_normalized_to_ports() { - let yaml = r#" + let yaml = r" version: 1 network_policies: test: @@ -1305,7 +1305,7 @@ network_policies: - { host: api.example.com, port: 443 } binaries: - { path: /usr/bin/curl } -"#; +"; let policy = parse_sandbox_policy(yaml).expect("should parse"); let ep = &policy.network_policies["test"].endpoints[0]; assert_eq!(ep.ports, vec![443]); @@ -1314,7 +1314,7 @@ network_policies: #[test] fn round_trip_preserves_multi_port() { - let yaml = r#" + let yaml = r" version: 1 network_policies: test: @@ -1326,7 +1326,7 @@ network_policies: - 443 binaries: - { path: /usr/bin/curl } -"#; +"; let proto1 = parse_sandbox_policy(yaml).expect("parse failed"); let yaml_out = serialize_sandbox_policy(&proto1).expect("serialize failed"); let proto2 = parse_sandbox_policy(&yaml_out).expect("re-parse failed"); @@ -1339,7 +1339,7 @@ network_policies: #[test] fn serialize_single_port_uses_compact_form() { - let yaml = r#" + let yaml = r" version: 1 network_policies: test: @@ -1348,7 +1348,7 @@ network_policies: - { host: api.example.com, port: 443 } binaries: - { path: /usr/bin/curl } -"#; +"; let proto = parse_sandbox_policy(yaml).expect("parse failed"); let yaml_out = serialize_sandbox_policy(&proto).expect("serialize failed"); // Should use compact `port: 443` form, not `ports: [443]` @@ -1493,7 +1493,7 @@ network_policies: #[test] fn parse_rejects_unknown_fields_in_deny_rule() { - let yaml = r#" + let yaml = r" version: 1 network_policies: test: @@ -1504,20 +1504,20 @@ network_policies: - method: POST path: /foo bogus: true -"#; +"; assert!(parse_sandbox_policy(yaml).is_err()); } #[test] fn rejects_port_above_65535() { - let yaml = r#" + let yaml = r" version: 1 network_policies: test: endpoints: - host: example.com port: 70000 -"#; +"; assert!( parse_sandbox_policy(yaml).is_err(), "port >65535 should fail to parse" diff --git a/crates/openshell-policy/src/merge.rs b/crates/openshell-policy/src/merge.rs index 5f5d2d40dd..0d008b90fa 100644 --- a/crates/openshell-policy/src/merge.rs +++ b/crates/openshell-policy/src/merge.rs @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 -use std::collections::HashSet; +use std::collections::{HashMap, HashSet}; use openshell_core::proto::{ L7Allow, L7DenyRule, L7Rule, NetworkBinary, NetworkEndpoint, NetworkPolicyRule, SandboxPolicy, @@ -387,7 +387,7 @@ fn merge_endpoint( .unwrap_or(0); if existing.host.is_empty() { - existing.host = incoming.host.clone(); + existing.host.clone_from(&incoming.host); } merge_endpoint_ports(existing, incoming); @@ -446,7 +446,7 @@ fn merge_endpoint( incoming: incoming.access.clone(), }); } else if existing.access.is_empty() { - existing.access = incoming.access.clone(); + existing.access.clone_from(&incoming.access); } else if existing.access != incoming.access { warnings.push(PolicyMergeWarning::ExistingAccessRetained { host, @@ -488,8 +488,8 @@ fn merge_endpoint_ports(existing: &mut NetworkEndpoint, incoming: &NetworkEndpoi } ports.sort_unstable(); ports.dedup(); - existing.ports = ports.clone(); existing.port = ports.first().copied().unwrap_or(0); + existing.ports = ports; } fn rules_share_endpoint( @@ -626,7 +626,7 @@ fn expand_access_preset(access: &str) -> Option> { method: method.to_string(), path: "**".to_string(), command: String::new(), - query: Default::default(), + query: HashMap::default(), }), }) .collect(), @@ -678,8 +678,8 @@ fn normalize_endpoint(endpoint: &mut NetworkEndpoint) { let mut ports = canonical_ports(endpoint); ports.sort_unstable(); ports.dedup(); - endpoint.ports = ports.clone(); endpoint.port = ports.first().copied().unwrap_or(0); + endpoint.ports = ports; dedup_strings(&mut endpoint.allowed_ips); dedup_l7_rules(&mut endpoint.rules); dedup_deny_rules(&mut endpoint.deny_rules); @@ -745,8 +745,8 @@ fn remove_endpoint(policy: &mut SandboxPolicy, rule_name: Option<&str>, host: &s return false; } - endpoint.ports = remaining_ports.clone(); endpoint.port = remaining_ports[0]; + endpoint.ports = remaining_ports; true }); diff --git a/crates/openshell-prover/src/lib.rs b/crates/openshell-prover/src/lib.rs index feff7d1d54..82922253de 100644 --- a/crates/openshell-prover/src/lib.rs +++ b/crates/openshell-prover/src/lib.rs @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 -//! Formal policy verification for OpenShell sandboxes. +//! Formal policy verification for `OpenShell` sandboxes. //! //! Encodes sandbox policies, binary capabilities, and credential scopes as Z3 //! SMT constraints, then checks reachability queries to detect data exfiltration @@ -91,7 +91,7 @@ mod tests { #[test] fn test_parse_policy() { let path = testdata_dir().join("policy.yaml"); - let model = policy::parse_policy(&path).expect("failed to parse policy"); + let model = parse_policy(&path).expect("failed to parse policy"); assert_eq!(model.version, 1); assert!(model.network_policies.contains_key("github_api")); let rule = &model.network_policies["github_api"]; @@ -104,7 +104,7 @@ mod tests { #[test] fn test_filesystem_policy() { let path = testdata_dir().join("policy.yaml"); - let model = policy::parse_policy(&path).expect("failed to parse policy"); + let model = parse_policy(&path).expect("failed to parse policy"); let readable = model.filesystem_policy.readable_paths(); assert!(readable.contains(&"/usr".to_owned())); assert!(readable.contains(&"/sandbox".to_owned())); @@ -114,12 +114,12 @@ mod tests { // 3. Workdir NOT included by default (matches runtime behavior). #[test] fn test_include_workdir_default() { - let yaml = r#" + let yaml = r" version: 1 filesystem_policy: read_only: - /usr -"#; +"; let model = policy::parse_policy_str(yaml).expect("parse"); let readable = model.filesystem_policy.readable_paths(); assert!(!readable.contains(&"/sandbox".to_owned())); @@ -128,13 +128,13 @@ filesystem_policy: // 4. Workdir excluded when include_workdir: false. #[test] fn test_include_workdir_false() { - let yaml = r#" + let yaml = r" version: 1 filesystem_policy: include_workdir: false read_only: - /usr -"#; +"; let model = policy::parse_policy_str(yaml).expect("parse"); let readable = model.filesystem_policy.readable_paths(); assert!(!readable.contains(&"/sandbox".to_owned())); @@ -143,14 +143,14 @@ filesystem_policy: // 5. No duplicate when workdir already in read_write. #[test] fn test_include_workdir_no_duplicate() { - let yaml = r#" + let yaml = r" version: 1 filesystem_policy: include_workdir: true read_write: - /sandbox - /tmp -"#; +"; let model = policy::parse_policy_str(yaml).expect("parse"); let readable = model.filesystem_policy.readable_paths(); let sandbox_count = readable.iter().filter(|p| *p == "/sandbox").count(); @@ -163,12 +163,12 @@ filesystem_policy: let policy_path = testdata_dir().join("policy.yaml"); let creds_path = testdata_dir().join("credentials.yaml"); - let pol = policy::parse_policy(&policy_path).expect("parse policy"); + let pol = parse_policy(&policy_path).expect("parse policy"); let cred_set = credentials::load_credential_set_embedded(&creds_path).expect("load creds"); let bin_reg = registry::load_embedded_binary_registry().expect("load registry"); - let z3_model = model::build_model(pol, cred_set, bin_reg); - let findings = queries::run_all_queries(&z3_model); + let z3_model = build_model(pol, cred_set, bin_reg); + let findings = run_all_queries(&z3_model); let query_types: std::collections::HashSet<&str> = findings.iter().map(|f| f.query.as_str()).collect(); @@ -195,12 +195,12 @@ filesystem_policy: let policy_path = testdata_dir().join("empty-policy.yaml"); let creds_path = testdata_dir().join("credentials.yaml"); - let pol = policy::parse_policy(&policy_path).expect("parse policy"); + let pol = parse_policy(&policy_path).expect("parse policy"); let cred_set = credentials::load_credential_set_embedded(&creds_path).expect("load creds"); let bin_reg = registry::load_embedded_binary_registry().expect("load registry"); - let z3_model = model::build_model(pol, cred_set, bin_reg); - let findings = queries::run_all_queries(&z3_model); + let z3_model = build_model(pol, cred_set, bin_reg); + let findings = run_all_queries(&z3_model); assert!( findings.is_empty(), diff --git a/crates/openshell-prover/src/model.rs b/crates/openshell-prover/src/model.rs index b96e7fef2c..bf52993d47 100644 --- a/crates/openshell-prover/src/model.rs +++ b/crates/openshell-prover/src/model.rs @@ -27,7 +27,7 @@ impl EndpointId { } } -/// Z3-backed reachability model for an OpenShell sandbox policy. +/// Z3-backed reachability model for an `OpenShell` sandbox policy. pub struct ReachabilityModel { pub policy: PolicyModel, pub credentials: CredentialSet, @@ -169,18 +169,18 @@ impl ReachabilityModel { allowed.iter().any(|m| write_set.contains(m.as_str())) }; - let l7w_var = Bool::new_const(format!("l7_allows_write_{ek}")); + let l7_write_var = Bool::new_const(format!("l7_allows_write_{ek}")); if ep.is_l7_enforced() { if has_write { - self.solver.assert(&l7w_var); + self.solver.assert(&l7_write_var); } else { - self.solver.assert(&!l7w_var.clone()); + self.solver.assert(&!l7_write_var.clone()); } } else { // L4-only: all methods pass through - self.solver.assert(&l7w_var); + self.solver.assert(&l7_write_var); } - self.l7_allows_write.insert(ek, l7w_var); + self.l7_allows_write.insert(ek, l7_write_var); } } } @@ -256,13 +256,14 @@ impl ReachabilityModel { } self.credential_has_write.insert(host.clone(), cw_var); - let cd_var = Bool::new_const(format!("credential_has_destructive_{host}")); + let destructive_var = Bool::new_const(format!("credential_has_destructive_{host}")); if has_destructive { - self.solver.assert(&cd_var); + self.solver.assert(&destructive_var); } else { - self.solver.assert(&!cd_var.clone()); + self.solver.assert(&!destructive_var.clone()); } - self.credential_has_destructive.insert(host.clone(), cd_var); + self.credential_has_destructive + .insert(host.clone(), destructive_var); } } @@ -364,7 +365,7 @@ impl ReachabilityModel { has_access, exfil, Bool::or(&[ - Bool::and(&[!l7_enforced.clone(), http.clone()]), + Bool::and(&[!l7_enforced, http.clone()]), Bool::and(&[l7_write, http]), bypass, ]), diff --git a/crates/openshell-prover/src/policy.rs b/crates/openshell-prover/src/policy.rs index b116fd5b09..8aea4b7d00 100644 --- a/crates/openshell-prover/src/policy.rs +++ b/crates/openshell-prover/src/policy.rs @@ -249,23 +249,13 @@ pub struct NetworkPolicyRule { } /// Filesystem access policy. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Default)] pub struct FilesystemPolicy { pub include_workdir: bool, pub read_only: Vec, pub read_write: Vec, } -impl Default for FilesystemPolicy { - fn default() -> Self { - Self { - include_workdir: false, - read_only: Vec::new(), - read_write: Vec::new(), - } - } -} - impl FilesystemPolicy { /// All readable paths (union of `read_only` and `read_write`), with workdir /// added when `include_workdir` is true and not already present. @@ -302,7 +292,7 @@ impl Default for PolicyModel { } impl PolicyModel { - /// All (policy_name, endpoint) pairs. + /// All (`policy_name`, endpoint) pairs. pub fn all_endpoints(&self) -> Vec<(&str, &Endpoint)> { let mut result = Vec::new(); for (name, rule) in &self.network_policies { @@ -327,7 +317,7 @@ impl PolicyModel { result } - /// All (binary, policy_name, endpoint) triples. + /// All (binary, `policy_name`, endpoint) triples. pub fn binary_endpoint_pairs(&self) -> Vec<(&Binary, &str, &Endpoint)> { let mut result = Vec::new(); for (name, rule) in &self.network_policies { @@ -345,7 +335,7 @@ impl PolicyModel { // Parsing // --------------------------------------------------------------------------- -/// Parse an OpenShell policy YAML file into a [`PolicyModel`]. +/// Parse an `OpenShell` policy YAML file into a [`PolicyModel`]. pub fn parse_policy(path: &Path) -> Result { let contents = std::fs::read_to_string(path) .into_diagnostic() diff --git a/crates/openshell-prover/src/queries.rs b/crates/openshell-prover/src/queries.rs index 001255ceae..6a0c7f6a66 100644 --- a/crates/openshell-prover/src/queries.rs +++ b/crates/openshell-prover/src/queries.rs @@ -44,8 +44,7 @@ pub fn check_data_exfiltration(model: &ReachabilityModel) -> Vec { ( "l4_only".to_owned(), format!( - "L4-only endpoint — no HTTP inspection, {} can send arbitrary data", - bpath + "L4-only endpoint — no HTTP inspection, {bpath} can send arbitrary data" ), ) } else { diff --git a/crates/openshell-prover/src/registry.rs b/crates/openshell-prover/src/registry.rs index 3acd52f828..63a3fce3bc 100644 --- a/crates/openshell-prover/src/registry.rs +++ b/crates/openshell-prover/src/registry.rs @@ -129,7 +129,7 @@ impl BinaryCapability { /// Whether the binary can perform write actions. pub fn can_write(&self) -> bool { - self.protocols.iter().any(|p| p.can_write()) || self.can_construct_http + self.protocols.iter().any(BinaryProtocol::can_write) || self.can_construct_http } /// Short mechanisms by which this binary can write. @@ -170,12 +170,11 @@ impl BinaryRegistry { return cap.clone(); } for (reg_path, cap) in &self.binaries { - if reg_path.contains('*') { - if let Ok(pattern) = glob::Pattern::new(reg_path) { - if pattern.matches(path) { - return cap.clone(); - } - } + if reg_path.contains('*') + && let Ok(pattern) = glob::Pattern::new(reg_path) + && pattern.matches(path) + { + return cap.clone(); } } BinaryCapability { diff --git a/crates/openshell-router/src/backend.rs b/crates/openshell-router/src/backend.rs index fbca70ae18..88a6e213ab 100644 --- a/crates/openshell-router/src/backend.rs +++ b/crates/openshell-router/src/backend.rs @@ -33,7 +33,7 @@ struct ValidationProbe { protocol: &'static str, body: bytes::Bytes, /// Alternate body to try when the primary probe fails with HTTP 400. - /// Used for OpenAI chat completions where newer models require + /// Used for `OpenAI` chat completions where newer models require /// `max_completion_tokens` while legacy/self-hosted backends only /// accept `max_tokens`. fallback_body: Option, @@ -158,7 +158,7 @@ fn prepare_backend_request( body: bytes::Bytes, ) -> Result<(reqwest::RequestBuilder, String), RouterError> { let url = build_backend_url(&route.endpoint, path); - let headers = sanitize_request_headers(route, &headers); + let headers = sanitize_request_headers(route, headers); let reqwest_method: reqwest::Method = method .parse() @@ -353,18 +353,18 @@ pub async fn verify_backend_endpoint( // there is a fallback body, retry with the alternate token parameter. // This handles the split between `max_completion_tokens` (GPT-5+) and // `max_tokens` (legacy/self-hosted backends). - if let (Err(err), Some(fallback_body)) = (&result, probe.fallback_body) { - if err.kind == ValidationFailureKind::RequestShape { - return try_validation_request( - client, - route, - probe.path, - probe.protocol, - headers, - fallback_body, - ) - .await; - } + if let (Err(err), Some(fallback_body)) = (&result, probe.fallback_body) + && err.kind == ValidationFailureKind::RequestShape + { + return try_validation_request( + client, + route, + probe.path, + probe.protocol, + headers, + fallback_body, + ) + .await; } result @@ -768,7 +768,7 @@ mod tests { assert_eq!(validated.protocol, "openai_chat_completions"); } - /// Non-chat-completions probes (e.g. anthropic_messages) should not + /// Non-chat-completions probes (e.g. `anthropic_messages`) should not /// have a fallback — a 400 remains a hard failure. #[tokio::test] async fn verify_non_chat_completions_no_fallback() { diff --git a/crates/openshell-router/src/config.rs b/crates/openshell-router/src/config.rs index 660509d9e5..ef5d90946b 100644 --- a/crates/openshell-router/src/config.rs +++ b/crates/openshell-router/src/config.rs @@ -163,7 +163,7 @@ mod tests { #[test] fn load_from_file_valid_yaml_round_trip() { - let yaml = r#" + let yaml = r" routes: - name: inference.local endpoint: http://localhost:8000/v1 @@ -175,7 +175,7 @@ routes: model: gpt-4o protocols: [openai_chat_completions, anthropic_messages] api_key: sk-prod-key -"#; +"; let mut f = tempfile::NamedTempFile::new().unwrap(); f.write_all(yaml.as_bytes()).unwrap(); @@ -208,13 +208,13 @@ routes: #[test] fn load_from_file_missing_api_key_returns_error() { - let yaml = r#" + let yaml = r" routes: - name: inference.local endpoint: http://localhost:8000/v1 model: llama-3 protocols: [openai_chat_completions] -"#; +"; let mut f = tempfile::NamedTempFile::new().unwrap(); f.write_all(yaml.as_bytes()).unwrap(); @@ -231,15 +231,16 @@ routes: } #[test] + #[allow(unsafe_code)] // std::env::set_var/remove_var require unsafe in Rust 2024 fn load_from_file_api_key_env_resolves_from_environment() { - let yaml = r#" + let yaml = r" routes: - name: inference.local endpoint: http://localhost:8000/v1 model: llama-3 protocols: [openai_chat_completions] api_key_env: NAV_TEST_API_KEY_FOR_YAML_TEST -"#; +"; // SAFETY: this test runs single-threaded; no other thread reads this var. unsafe { std::env::set_var("NAV_TEST_API_KEY_FOR_YAML_TEST", "from-env") }; let mut f = tempfile::NamedTempFile::new().unwrap(); diff --git a/crates/openshell-sandbox/src/bypass_monitor.rs b/crates/openshell-sandbox/src/bypass_monitor.rs index dfa46cab16..1a7ec5f99f 100644 --- a/crates/openshell-sandbox/src/bypass_monitor.rs +++ b/crates/openshell-sandbox/src/bypass_monitor.rs @@ -170,17 +170,14 @@ pub fn spawn( } }; - let stdout = match child.stdout.take() { - Some(s) => s, - None => { - let event = NetworkActivityBuilder::new(crate::ocsf_ctx()) - .activity(ActivityId::Other) - .severity(SeverityId::Low) - .message("dmesg --follow produced no stdout; bypass monitor will not run") - .build(); - ocsf_emit!(event); - return; - } + let Some(stdout) = child.stdout.take() else { + let event = NetworkActivityBuilder::new(crate::ocsf_ctx()) + .activity(ActivityId::Other) + .severity(SeverityId::Low) + .message("dmesg --follow produced no stdout; bypass monitor will not run") + .build(); + ocsf_emit!(event); + return; }; let reader = std::io::BufReader::new(stdout); @@ -515,6 +512,8 @@ mod tests { let (_accepted, _) = listener.accept().expect("accept"); let fd = stream.as_raw_fd(); + // libc/syscall FFI requires unsafe + #[allow(unsafe_code)] unsafe { let flags = libc::fcntl(fd, libc::F_GETFD); assert!(flags >= 0, "F_GETFD failed"); @@ -528,9 +527,13 @@ mod tests { let sleep_path = CString::new("/bin/sleep").unwrap(); let arg0 = CString::new("sleep").unwrap(); let arg1 = CString::new("30").unwrap(); + // libc/syscall FFI requires unsafe + #[allow(unsafe_code)] let child_pid = unsafe { libc::fork() }; assert!(child_pid >= 0, "fork failed"); if child_pid == 0 { + // libc/syscall FFI requires unsafe + #[allow(unsafe_code)] unsafe { libc::execl( sleep_path.as_ptr(), @@ -558,6 +561,8 @@ mod tests { let (binary, pid, ancestors) = resolve_process_identity(std::process::id(), peer_port); + // libc/syscall FFI requires unsafe + #[allow(unsafe_code)] unsafe { libc::kill(child_pid, libc::SIGKILL); libc::waitpid(child_pid, std::ptr::null_mut(), 0); diff --git a/crates/openshell-sandbox/src/child_env.rs b/crates/openshell-sandbox/src/child_env.rs index 914e06ea5a..e764afdfef 100644 --- a/crates/openshell-sandbox/src/child_env.rs +++ b/crates/openshell-sandbox/src/child_env.rs @@ -5,7 +5,7 @@ use std::path::Path; const LOCAL_NO_PROXY: &str = "127.0.0.1,localhost,::1"; -pub(crate) fn proxy_env_vars(proxy_url: &str) -> [(&'static str, String); 9] { +pub fn proxy_env_vars(proxy_url: &str) -> [(&'static str, String); 9] { [ ("ALL_PROXY", proxy_url.to_owned()), ("HTTP_PROXY", proxy_url.to_owned()), @@ -21,14 +21,14 @@ pub(crate) fn proxy_env_vars(proxy_url: &str) -> [(&'static str, String); 9] { ] } -pub(crate) fn tls_env_vars( +pub fn tls_env_vars( ca_cert_path: &Path, combined_bundle_path: &Path, ) -> [(&'static str, String); 5] { let ca_cert_path = ca_cert_path.display().to_string(); let combined_bundle_path = combined_bundle_path.display().to_string(); [ - ("NODE_EXTRA_CA_CERTS", ca_cert_path.clone()), + ("NODE_EXTRA_CA_CERTS", ca_cert_path), ("SSL_CERT_FILE", combined_bundle_path.clone()), ("REQUESTS_CA_BUNDLE", combined_bundle_path.clone()), ("CURL_CA_BUNDLE", combined_bundle_path.clone()), diff --git a/crates/openshell-sandbox/src/denial_aggregator.rs b/crates/openshell-sandbox/src/denial_aggregator.rs index 175f31afc8..d64be7f1d1 100644 --- a/crates/openshell-sandbox/src/denial_aggregator.rs +++ b/crates/openshell-sandbox/src/denial_aggregator.rs @@ -221,6 +221,5 @@ pub struct FlushableL7Sample { fn current_time_ms() -> i64 { SystemTime::now() .duration_since(UNIX_EPOCH) - .map(|d| i64::try_from(d.as_millis()).unwrap_or(i64::MAX)) - .unwrap_or(0) + .map_or(0, |d| i64::try_from(d.as_millis()).unwrap_or(i64::MAX)) } diff --git a/crates/openshell-sandbox/src/grpc_client.rs b/crates/openshell-sandbox/src/grpc_client.rs index 0af6476c58..68f710d8c1 100644 --- a/crates/openshell-sandbox/src/grpc_client.rs +++ b/crates/openshell-sandbox/src/grpc_client.rs @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 //! gRPC client for fetching sandbox policy, provider environment, and inference -//! route bundles from OpenShell server. +//! route bundles from `OpenShell` server. use std::collections::HashMap; use std::time::Duration; @@ -17,7 +17,7 @@ use openshell_core::proto::{ use tonic::transport::{Certificate, Channel, ClientTlsConfig, Endpoint, Identity}; use tracing::debug; -/// Create a channel to the OpenShell server. +/// Create a channel to the `OpenShell` server. /// /// When the endpoint uses `https://`, mTLS is configured using these env vars: /// - `OPENSHELL_TLS_CA` -- path to the CA certificate @@ -78,18 +78,18 @@ async fn connect_channel(endpoint: &str) -> Result { .wrap_err("failed to connect to OpenShell server") } -/// Create a channel to the OpenShell server (public for use by supervisor_session). +/// Create a channel to the `OpenShell` server (public for use by `supervisor_session`). pub async fn connect_channel_pub(endpoint: &str) -> Result { connect_channel(endpoint).await } -/// Connect to the OpenShell server (mTLS or plaintext based on endpoint scheme). +/// Connect to the `OpenShell` server (mTLS or plaintext based on endpoint scheme). async fn connect(endpoint: &str) -> Result> { let channel = connect_channel(endpoint).await?; Ok(OpenShellClient::new(channel)) } -/// Fetch sandbox policy from OpenShell server via gRPC. +/// Fetch sandbox policy from `OpenShell` server via gRPC. /// /// Returns `Ok(Some(policy))` when the server has a policy configured, /// or `Ok(None)` when the sandbox was created without a policy (the sandbox @@ -191,7 +191,7 @@ pub async fn sync_policy(endpoint: &str, sandbox: &str, policy: &ProtoSandboxPol sync_policy_with_client(&mut client, sandbox, policy).await } -/// Fetch provider environment variables for a sandbox from OpenShell server via gRPC. +/// Fetch provider environment variables for a sandbox from `OpenShell` server via gRPC. /// /// Returns a map of environment variable names to values derived from provider /// credentials configured on the sandbox. Returns an empty map if the sandbox @@ -214,7 +214,7 @@ pub async fn fetch_provider_environment( Ok(response.into_inner().environment) } -/// A reusable gRPC client for the OpenShell service. +/// A reusable gRPC client for the `OpenShell` service. /// /// Wraps a tonic channel connected once and reused for policy polling /// and status reporting, avoiding per-request TLS handshake overhead. @@ -231,7 +231,7 @@ pub struct SettingsPollResult { pub config_revision: u64, pub policy_source: PolicySource, /// Effective settings keyed by name. - pub settings: std::collections::HashMap, + pub settings: HashMap, /// When `policy_source` is `Global`, the version of the global policy revision. pub global_policy_version: u32, } diff --git a/crates/openshell-sandbox/src/l7/mod.rs b/crates/openshell-sandbox/src/l7/mod.rs index 3b26f8122c..3e0ee2cd3d 100644 --- a/crates/openshell-sandbox/src/l7/mod.rs +++ b/crates/openshell-sandbox/src/l7/mod.rs @@ -144,7 +144,7 @@ pub fn parse_l7_config(val: ®orus::Value) -> Option { pub fn parse_tls_mode(val: ®orus::Value) -> TlsMode { match get_object_str(val, "tls").as_deref() { Some("skip") => TlsMode::Skip, - Some("terminate") | Some("passthrough") => TlsMode::Auto, // deprecation logged by parse_l7_config + // "terminate" and "passthrough" are deprecated aliases (logged by parse_l7_config); fall through to Auto. _ => TlsMode::Auto, } } @@ -252,17 +252,16 @@ pub fn validate_l7_policies(data_json: &serde_json::Value) -> (Vec, Vec< let host = ep.get("host").and_then(|v| v.as_str()).unwrap_or(""); // Read ports from either "ports" array or scalar "port". - let ports: Vec = ep - .get("ports") - .and_then(|v| v.as_array()) - .map(|arr| arr.iter().filter_map(|v| v.as_u64()).collect()) - .unwrap_or_else(|| { + let ports: Vec = ep.get("ports").and_then(|v| v.as_array()).map_or_else( + || { ep.get("port") .and_then(serde_json::Value::as_u64) .filter(|p| *p > 0) .into_iter() .collect() - }); + }, + |arr| arr.iter().filter_map(serde_json::Value::as_u64).collect(), + ); let loc = format!("{name}.endpoints[{i}]"); // Validate host wildcard patterns. @@ -392,10 +391,10 @@ pub fn validate_l7_policies(data_json: &serde_json::Value) -> (Vec, Vec< } // Validate path glob syntax - if let Some(path) = deny_rule.get("path").and_then(|p| p.as_str()) { - if let Some(warning) = check_glob_syntax(path) { - warnings.push(format!("{deny_loc}.path: {warning}")); - } + if let Some(path) = deny_rule.get("path").and_then(|p| p.as_str()) + && let Some(warning) = check_glob_syntax(path) + { + warnings.push(format!("{deny_loc}.path: {warning}")); } // Validate query matchers — mirrors allow-side validation exactly @@ -498,12 +497,12 @@ pub fn validate_l7_policies(data_json: &serde_json::Value) -> (Vec, Vec< } // SQL command validation - if let Some(command) = deny_rule.get("command").and_then(|c| c.as_str()) { - if !command.is_empty() && protocol == "rest" { - warnings.push(format!( - "{deny_loc}: command is for SQL protocol, not REST" - )); - } + if let Some(command) = deny_rule.get("command").and_then(|c| c.as_str()) + && !command.is_empty() + && protocol == "rest" + { + warnings + .push(format!("{deny_loc}: command is for SQL protocol, not REST")); } } } diff --git a/crates/openshell-sandbox/src/l7/path.rs b/crates/openshell-sandbox/src/l7/path.rs index 1b425b5f49..db2e4e9847 100644 --- a/crates/openshell-sandbox/src/l7/path.rs +++ b/crates/openshell-sandbox/src/l7/path.rs @@ -136,15 +136,12 @@ pub fn canonicalize_request_target( }; // 4. Handle absolute-form by stripping scheme://authority. - let raw_path = if let Some(idx) = path_part.find("://") { + let raw_path = path_part.find("://").map_or(path_part, |idx| { let after_scheme = &path_part[idx + 3..]; - match after_scheme.find('/') { - Some(slash) => &after_scheme[slash..], - None => "/", - } - } else { - path_part - }; + after_scheme + .find('/') + .map_or("/", |slash| &after_scheme[slash..]) + }); // 5. Empty is equivalent to "/". let raw_path = if raw_path.is_empty() { "/" } else { raw_path }; @@ -170,7 +167,7 @@ pub fn canonicalize_request_target( // 10. Reconstruct. Strip `;params` per segment if requested; re-encode // any byte that must be percent-encoded in the pchar set. - let canonical = build_canonical_path(&resolved, decoded.last().copied() == Some(b'/'), opts); + let canonical = build_canonical_path(&resolved, decoded.last().copied() == Some(b'/'), *opts); let rewritten = canonical != raw_path; Ok(( @@ -270,7 +267,7 @@ fn resolve_dot_segments(segments: Vec<&[u8]>) -> Result>, Canonicali fn build_canonical_path( segments: &[Vec], _trailing_slash_hint: bool, - opts: &CanonicalizeOptions, + opts: CanonicalizeOptions, ) -> String { let mut out = String::from("/"); for (idx, seg) in segments.iter().enumerate() { diff --git a/crates/openshell-sandbox/src/l7/relay.rs b/crates/openshell-sandbox/src/l7/relay.rs index a0e54062d9..02636c713a 100644 --- a/crates/openshell-sandbox/src/l7/relay.rs +++ b/crates/openshell-sandbox/src/l7/relay.rs @@ -17,7 +17,7 @@ use openshell_ocsf::{ }; use std::sync::{Arc, Mutex}; use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt}; -use tracing::{debug, info, warn}; +use tracing::{debug, warn}; /// Context for L7 request policy evaluation. pub struct L7EvalContext { @@ -218,13 +218,8 @@ where // Uses redacted_target (path only, no query params) to avoid logging secrets. { let (action_id, disposition_id, severity) = match decision_str { - "allow" => ( - ActionId::Allowed, - DispositionId::Allowed, - SeverityId::Informational, - ), "deny" => (ActionId::Denied, DispositionId::Blocked, SeverityId::Medium), - "audit" => ( + "allow" | "audit" => ( ActionId::Allowed, DispositionId::Allowed, SeverityId::Informational, diff --git a/crates/openshell-sandbox/src/l7/rest.rs b/crates/openshell-sandbox/src/l7/rest.rs index 4d8909b9eb..737190e2c8 100644 --- a/crates/openshell-sandbox/src/l7/rest.rs +++ b/crates/openshell-sandbox/src/l7/rest.rs @@ -12,7 +12,7 @@ use crate::secrets::rewrite_http_header_block; use miette::{IntoDiagnostic, Result, miette}; use std::collections::HashMap; use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt}; -use tracing::{debug, warn}; +use tracing::debug; const MAX_HEADER_BYTES: usize = 16384; // 16 KiB for HTTP headers const RELAY_BUF_SIZE: usize = 8192; @@ -233,6 +233,8 @@ fn rewrite_request_line_target( Ok(out) } +// Used only in tests; kept as a `pub(crate)` helper for clarity. +#[allow(dead_code)] pub(crate) fn parse_target_query(target: &str) -> Result<(String, HashMap>)> { match target.split_once('?') { Some((path, query)) => Ok((path.to_string(), parse_query_params(query)?)), @@ -475,12 +477,12 @@ fn parse_body_length(headers: &str) -> Result { let len: u64 = val .parse() .map_err(|_| miette!("Request contains invalid Content-Length value"))?; - if let Some(prev) = cl_value { - if prev != len { - return Err(miette!( - "Request contains multiple Content-Length headers with differing values ({prev} vs {len})" - )); - } + if let Some(prev) = cl_value + && prev != len + { + return Err(miette!( + "Request contains multiple Content-Length headers with differing values ({prev} vs {len})" + )); } cl_value = Some(len); } @@ -652,6 +654,9 @@ fn find_crlf(buf: &[u8], start: usize) -> Option { /// /// Note: callers that receive `Upgraded` are responsible for switching to /// raw bidirectional relay and forwarding the overflow bytes. +// Public helper retained as part of the relay API surface; internal callers +// currently use `relay_response` directly. +#[allow(dead_code)] pub(crate) async fn relay_response_to_client( upstream: &mut U, client: &mut C, @@ -931,6 +936,13 @@ fn is_benign_close(err: &std::io::Error) -> bool { } #[cfg(test)] +#[allow( + clippy::iter_on_single_items, + clippy::manual_string_new, + clippy::collapsible_if, + clippy::cast_possible_truncation, + reason = "Test code: test fixtures and explicit value-shape assertions are idiomatic in tests." +)] mod tests { use super::*; use crate::secrets::SecretResolver; diff --git a/crates/openshell-sandbox/src/l7/tls.rs b/crates/openshell-sandbox/src/l7/tls.rs index a11674da12..70e198f420 100644 --- a/crates/openshell-sandbox/src/l7/tls.rs +++ b/crates/openshell-sandbox/src/l7/tls.rs @@ -313,10 +313,10 @@ pub fn parse_pem_certs(path: &Path) -> Result>> { } /// Peek the first bytes of a stream and determine if it looks like a TLS -/// ClientHello handshake. +/// `ClientHello` handshake. /// /// A TLS record starts with: -/// - byte 0: `0x16` (ContentType::Handshake) +/// - byte 0: `0x16` (`ContentType::Handshake`) /// - bytes 1-2: TLS version (0x0301 = TLS 1.0, 0x0302 = TLS 1.1, 0x0303 = TLS 1.2/1.3) /// /// Returns `true` if the peeked bytes match the TLS handshake pattern. diff --git a/crates/openshell-sandbox/src/lib.rs b/crates/openshell-sandbox/src/lib.rs index 262859c338..fbd7460e2b 100644 --- a/crates/openshell-sandbox/src/lib.rs +++ b/crates/openshell-sandbox/src/lib.rs @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 -//! OpenShell Sandbox library. +//! `OpenShell` Sandbox library. //! //! This crate provides process sandboxing and monitoring capabilities. @@ -26,12 +26,14 @@ mod supervisor_session; use miette::{IntoDiagnostic, Result}; #[cfg(target_os = "linux")] use std::collections::HashSet; +use std::future::Future; use std::net::SocketAddr; use std::sync::Arc; +use std::sync::LazyLock; +#[cfg(any(target_os = "linux", test))] +use std::sync::Mutex; use std::sync::OnceLock; use std::sync::atomic::{AtomicU32, Ordering}; -#[cfg(target_os = "linux")] -use std::sync::{LazyLock, Mutex}; use std::time::Duration; use tokio::time::timeout; use tracing::{debug, info, trace, warn}; @@ -67,16 +69,15 @@ static OCSF_CTX: OnceLock = OnceLock::new(); /// Fallback context used when `OCSF_CTX` has not been initialized (e.g. in /// unit tests that exercise individual functions without calling `run_sandbox`). -static OCSF_CTX_FALLBACK: std::sync::LazyLock = - std::sync::LazyLock::new(|| SandboxContext { - sandbox_id: String::new(), - sandbox_name: String::new(), - container_image: String::new(), - hostname: "test".to_string(), - product_version: openshell_core::VERSION.to_string(), - proxy_ip: std::net::IpAddr::from([127, 0, 0, 1]), - proxy_port: 3128, - }); +static OCSF_CTX_FALLBACK: LazyLock = LazyLock::new(|| SandboxContext { + sandbox_id: String::new(), + sandbox_name: String::new(), + container_image: String::new(), + hostname: "test".to_string(), + product_version: openshell_core::VERSION.to_string(), + proxy_ip: std::net::IpAddr::from([127, 0, 0, 1]), + proxy_port: 3128, +}); /// Return a reference to the process-wide [`SandboxContext`]. /// @@ -133,27 +134,27 @@ fn disable_inference_on_empty_routes(source: InferenceRouteSource) -> bool { } fn route_refresh_interval_secs() -> u64 { - match std::env::var("OPENSHELL_ROUTE_REFRESH_INTERVAL_SECS") { - Ok(value) => match value.parse::() { - Ok(interval) if interval > 0 => interval, - Ok(_) => { - warn!( - default_interval_secs = DEFAULT_ROUTE_REFRESH_INTERVAL_SECS, - "Ignoring zero route refresh interval" - ); - DEFAULT_ROUTE_REFRESH_INTERVAL_SECS - } - Err(error) => { - warn!( - interval = %value, - error = %error, - default_interval_secs = DEFAULT_ROUTE_REFRESH_INTERVAL_SECS, - "Ignoring invalid route refresh interval" - ); - DEFAULT_ROUTE_REFRESH_INTERVAL_SECS - } - }, - Err(_) => DEFAULT_ROUTE_REFRESH_INTERVAL_SECS, + let Ok(value) = std::env::var("OPENSHELL_ROUTE_REFRESH_INTERVAL_SECS") else { + return DEFAULT_ROUTE_REFRESH_INTERVAL_SECS; + }; + match value.parse::() { + Ok(interval) if interval > 0 => interval, + Ok(_) => { + warn!( + default_interval_secs = DEFAULT_ROUTE_REFRESH_INTERVAL_SECS, + "Ignoring zero route refresh interval" + ); + DEFAULT_ROUTE_REFRESH_INTERVAL_SECS + } + Err(error) => { + warn!( + interval = %value, + error = %error, + default_interval_secs = DEFAULT_ROUTE_REFRESH_INTERVAL_SECS, + "Ignoring invalid route refresh interval" + ); + DEFAULT_ROUTE_REFRESH_INTERVAL_SECS + } } } @@ -227,9 +228,10 @@ pub async fn run_sandbox( // Proxy IP/port use defaults here; they are only significant for network // events which happen after the netns is created. { - let hostname = std::fs::read_to_string("/etc/hostname") - .map(|s| s.trim().to_string()) - .unwrap_or_else(|_| "openshell-sandbox".to_string()); + let hostname = std::fs::read_to_string("/etc/hostname").map_or_else( + |_| "openshell-sandbox".to_string(), + |s| s.trim().to_string(), + ); if OCSF_CTX .set(SandboxContext { @@ -491,15 +493,13 @@ pub async fn run_sandbox( // Reads /dev/kmsg for iptables LOG entries and emits structured // tracing events for direct connection attempts that bypass the proxy. #[cfg(target_os = "linux")] - let _bypass_monitor = if netns.is_some() { + let _bypass_monitor = netns.as_ref().and_then(|ns| { bypass_monitor::spawn( - netns.as_ref().expect("netns is Some").name().to_string(), + ns.name().to_string(), entrypoint_pid.clone(), bypass_denial_tx, ) - } else { - None - }; + }); // On non-Linux, bypass_denial_tx is unused (no /dev/kmsg). #[cfg(not(target_os = "linux"))] @@ -596,11 +596,11 @@ pub async fn run_sandbox( } match waitpid(pid, Some(WaitPidFlag::WNOHANG)) { - Ok(WaitStatus::StillAlive) | Err(nix::errno::Errno::ECHILD) => {} + Ok(WaitStatus::StillAlive) + | Err(nix::errno::Errno::ECHILD | nix::errno::Errno::EINTR) => {} Ok(reaped) => { tracing::debug!(?reaped, "Reaped orphaned child process"); } - Err(nix::errno::Errno::EINTR) => {} Err(e) => { tracing::debug!(error = %e, "waitpid error during orphan reap"); break; @@ -903,6 +903,9 @@ pub async fn run_sandbox( /// wins and the cluster bundle is not fetched. /// /// Returns `None` if neither source is configured (inference routing disabled). +// `routes`/`router` are intentionally distinct nouns (the route list vs the +// router that consumes them); both names are clearer than alternatives. +#[allow(clippy::similar_names)] async fn build_inference_context( sandbox_id: Option<&str>, openshell_endpoint: Option<&str>, @@ -1429,6 +1432,14 @@ fn enrich_sandbox_baseline_paths(policy: &mut SandboxPolicy) { } #[cfg(test)] +#[allow( + clippy::needless_raw_string_hashes, + clippy::iter_on_single_items, + clippy::similar_names, + clippy::manual_string_new, + clippy::doc_markdown, + reason = "Test code: test fixtures often use idiomatic forms not flagged in production." +)] mod baseline_tests { use super::*; use crate::policy::{FilesystemPolicy, LandlockPolicy, ProcessPolicy}; @@ -1596,10 +1607,10 @@ fn is_retryable_error(err: &miette::Report) -> bool { /// /// Non-transient gRPC errors (e.g. `NOT_FOUND`, `INVALID_ARGUMENT`, /// `PERMISSION_DENIED`) are returned immediately without retrying. -async fn grpc_retry(op_name: &str, f: F) -> miette::Result +async fn grpc_retry(op_name: &str, f: F) -> Result where F: Fn() -> Fut, - Fut: std::future::Future>, + Fut: Future>, { let mut last_err = None; for attempt in 1..=5u32 { @@ -1692,55 +1703,51 @@ async fn load_policy( let proto_policy = grpc_retry("Policy fetch", || grpc_client::fetch_policy(endpoint, id)).await?; - let mut proto_policy = match proto_policy { - Some(p) => p, - None => { - // No policy configured on the server. Discover from disk or - // fall back to the restrictive default, then sync to the - // gateway so it becomes the authoritative baseline. - ocsf_emit!( - ConfigStateChangeBuilder::new(ocsf_ctx()) - .severity(SeverityId::Informational) - .status(StatusId::Success) - .state(StateId::Other, "discovery") - .message("Server returned no policy; attempting local discovery") - .build() - ); - let mut discovered = discover_policy_from_disk_or_default(); - // Enrich before syncing so the gateway baseline includes - // baseline paths from the start. - enrich_proto_baseline_paths(&mut discovered); - let sandbox = sandbox.as_deref().ok_or_else(|| { - miette::miette!( - "Cannot sync discovered policy: sandbox not available.\n\ - Set OPENSHELL_SANDBOX or --sandbox to enable policy sync." - ) - })?; + let mut proto_policy = if let Some(p) = proto_policy { + p + } else { + // No policy configured on the server. Discover from disk or + // fall back to the restrictive default, then sync to the + // gateway so it becomes the authoritative baseline. + ocsf_emit!( + ConfigStateChangeBuilder::new(ocsf_ctx()) + .severity(SeverityId::Informational) + .status(StatusId::Success) + .state(StateId::Other, "discovery") + .message("Server returned no policy; attempting local discovery") + .build() + ); + let mut discovered = discover_policy_from_disk_or_default(); + // Enrich before syncing so the gateway baseline includes + // baseline paths from the start. + enrich_proto_baseline_paths(&mut discovered); + let sandbox = sandbox.as_deref().ok_or_else(|| { + miette::miette!( + "Cannot sync discovered policy: sandbox not available.\n\ + Set OPENSHELL_SANDBOX or --sandbox to enable policy sync." + ) + })?; - // Sync and re-fetch over a single connection to avoid extra - // TLS handshakes. - grpc_retry("Policy discovery sync", || { - grpc_client::discover_and_sync_policy(endpoint, id, sandbox, &discovered) - }) - .await? - } + // Sync and re-fetch over a single connection to avoid extra + // TLS handshakes. + grpc_retry("Policy discovery sync", || { + grpc_client::discover_and_sync_policy(endpoint, id, sandbox, &discovered) + }) + .await? }; // Ensure baseline filesystem paths are present for proxy-mode // sandboxes. If the policy was enriched, sync the updated version // back to the gateway so users can see the effective policy. let enriched = enrich_proto_baseline_paths(&mut proto_policy); - if enriched { - if let Some(sandbox_name) = sandbox.as_deref() { - if let Err(e) = - grpc_client::sync_policy(endpoint, sandbox_name, &proto_policy).await - { - warn!( - error = %e, - "Failed to sync enriched policy back to gateway (non-fatal)" - ); - } - } + if enriched + && let Some(sandbox_name) = sandbox.as_deref() + && let Err(e) = grpc_client::sync_policy(endpoint, sandbox_name, &proto_policy).await + { + warn!( + error = %e, + "Failed to sync enriched policy back to gateway (non-fatal)" + ); } // Build OPA engine from baked-in rules + typed proto data. @@ -1802,76 +1809,71 @@ fn discover_policy_from_path(path: &std::path::Path) -> openshell_core::proto::S parse_sandbox_policy, restrictive_default_policy, validate_sandbox_policy, }; - match std::fs::read_to_string(path) { - Ok(yaml) => { - ocsf_emit!( - ConfigStateChangeBuilder::new(ocsf_ctx()) - .severity(SeverityId::Informational) - .status(StatusId::Success) - .state(StateId::Enabled, "loaded") + let Ok(yaml) = std::fs::read_to_string(path) else { + ocsf_emit!( + ConfigStateChangeBuilder::new(ocsf_ctx()) + .severity(SeverityId::Informational) + .status(StatusId::Success) + .state(StateId::Enabled, "default") + .message(format!( + "No policy file on disk, using restrictive default [path:{}]", + path.display() + )) + .build() + ); + return restrictive_default_policy(); + }; + ocsf_emit!( + ConfigStateChangeBuilder::new(ocsf_ctx()) + .severity(SeverityId::Informational) + .status(StatusId::Success) + .state(StateId::Enabled, "loaded") + .message(format!( + "Loaded sandbox policy from container disk [path:{}]", + path.display() + )) + .build() + ); + match parse_sandbox_policy(&yaml) { + Ok(policy) => { + // Validate the disk-loaded policy for safety. + if let Err(violations) = validate_sandbox_policy(&policy) { + let messages: Vec = violations.iter().map(ToString::to_string).collect(); + ocsf_emit!(DetectionFindingBuilder::new(ocsf_ctx()) + .activity(ActivityId::Open) + .severity(SeverityId::Medium) + .action(ActionId::Denied) + .disposition(DispositionId::Blocked) + .finding_info( + FindingInfo::new( + "unsafe-disk-policy", + "Unsafe Disk Policy Content", + ) + .with_desc(&format!( + "Disk policy at {} contains unsafe content: {}", + path.display(), + messages.join("; "), + )), + ) .message(format!( - "Loaded sandbox policy from container disk [path:{}]", + "Disk policy contains unsafe content, using restrictive default [path:{}]", path.display() )) - .build() - ); - match parse_sandbox_policy(&yaml) { - Ok(policy) => { - // Validate the disk-loaded policy for safety. - if let Err(violations) = validate_sandbox_policy(&policy) { - let messages: Vec = - violations.iter().map(ToString::to_string).collect(); - ocsf_emit!(DetectionFindingBuilder::new(ocsf_ctx()) - .activity(ActivityId::Open) - .severity(SeverityId::Medium) - .action(ActionId::Denied) - .disposition(DispositionId::Blocked) - .finding_info( - FindingInfo::new( - "unsafe-disk-policy", - "Unsafe Disk Policy Content", - ) - .with_desc(&format!( - "Disk policy at {} contains unsafe content: {}", - path.display(), - messages.join("; "), - )), - ) - .message(format!( - "Disk policy contains unsafe content, using restrictive default [path:{}]", - path.display() - )) - .build()); - return restrictive_default_policy(); - } - policy - } - Err(e) => { - ocsf_emit!(ConfigStateChangeBuilder::new(ocsf_ctx()) - .severity(SeverityId::Medium) - .status(StatusId::Failure) - .state(StateId::Other, "fallback") - .message(format!( - "Failed to parse disk policy, using restrictive default [path:{} error:{e}]", - path.display() - )) - .build()); - restrictive_default_policy() - } + .build()); + return restrictive_default_policy(); } + policy } - Err(_) => { - ocsf_emit!( - ConfigStateChangeBuilder::new(ocsf_ctx()) - .severity(SeverityId::Informational) - .status(StatusId::Success) - .state(StateId::Enabled, "default") - .message(format!( - "No policy file on disk, using restrictive default [path:{}]", - path.display() - )) - .build() - ); + Err(e) => { + ocsf_emit!(ConfigStateChangeBuilder::new(ocsf_ctx()) + .severity(SeverityId::Medium) + .status(StatusId::Failure) + .state(StateId::Other, "fallback") + .message(format!( + "Failed to parse disk policy, using restrictive default [path:{} error:{e}]", + path.display() + )) + .build()); restrictive_default_policy() } } @@ -2036,7 +2038,7 @@ async fn flush_proposals_to_gateway( .map(|s| DenialSummary { sandbox_id: String::new(), host: s.host, - port: s.port as u32, + port: u32::from(s.port), binary: s.binary, ancestors: s.ancestors, deny_reason: s.deny_reason, @@ -2202,13 +2204,13 @@ async fn run_policy_poll_loop( .build() ); } - if result.version > 0 && result.policy_source == PolicySource::Sandbox { - if let Err(e) = client + if result.version > 0 + && result.policy_source == PolicySource::Sandbox + && let Err(e) = client .report_policy_status(sandbox_id, result.version, true, "") .await - { - warn!(error = %e, "Failed to report policy load success"); - } + { + warn!(error = %e, "Failed to report policy load success"); } } Err(e) => { @@ -2223,13 +2225,13 @@ async fn run_policy_poll_loop( result.version )) .build()); - if result.version > 0 && result.policy_source == PolicySource::Sandbox { - if let Err(report_err) = client + if result.version > 0 + && result.policy_source == PolicySource::Sandbox + && let Err(report_err) = client .report_policy_status(sandbox_id, result.version, false, &e.to_string()) .await - { - warn!(error = %report_err, "Failed to report policy load failure"); - } + { + warn!(error = %report_err, "Failed to report policy load failure"); } } } @@ -2281,8 +2283,8 @@ fn log_setting_changes( .status(StatusId::Success) .state(StateId::Enabled, "updated") .unmapped("key", serde_json::json!(key)) - .unmapped("old", serde_json::json!(old_val.to_string())) - .unmapped("new", serde_json::json!(new_val.to_string())) + .unmapped("old", serde_json::json!(old_val.clone())) + .unmapped("new", serde_json::json!(new_val.clone())) .message(format!( "Setting changed [key:{key} old:{old_val} new:{new_val}]" )) @@ -2297,7 +2299,7 @@ fn log_setting_changes( .status(StatusId::Success) .state(StateId::Enabled, "enabled") .unmapped("key", serde_json::json!(key)) - .unmapped("value", serde_json::json!(new_val.to_string())) + .unmapped("value", serde_json::json!(new_val.clone())) .message(format!("Setting added [key:{key} value:{new_val}]")) .build() ); @@ -2332,6 +2334,14 @@ fn format_setting_value(es: &openshell_core::proto::EffectiveSetting) -> String } #[cfg(test)] +#[allow( + clippy::needless_raw_string_hashes, + clippy::iter_on_single_items, + clippy::similar_names, + clippy::manual_string_new, + clippy::doc_markdown, + reason = "Test code: test fixtures often use idiomatic forms not flagged in production." +)] mod tests { use super::*; use crate::policy::{FilesystemPolicy, LandlockPolicy, ProcessPolicy}; @@ -2341,8 +2351,7 @@ mod tests { use std::os::unix::fs::{MetadataExt, symlink}; use temp_env::with_vars; - static ENV_LOCK: std::sync::LazyLock> = - std::sync::LazyLock::new(|| std::sync::Mutex::new(())); + static ENV_LOCK: LazyLock> = LazyLock::new(|| Mutex::new(())); #[test] fn bundle_to_resolved_routes_converts_all_fields() { diff --git a/crates/openshell-sandbox/src/log_push.rs b/crates/openshell-sandbox/src/log_push.rs index 17f9bcc3d7..8e053f79f9 100644 --- a/crates/openshell-sandbox/src/log_push.rs +++ b/crates/openshell-sandbox/src/log_push.rs @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 -//! Push sandbox tracing events to the OpenShell server via gRPC. +//! Push sandbox tracing events to the `OpenShell` server via gRPC. //! //! A [`tracing`] layer captures log events and sends them through an mpsc //! channel to a background task. The task batches lines and streams them to @@ -15,7 +15,7 @@ use tracing::{Event, Subscriber}; use tracing_subscriber::Layer; use tracing_subscriber::layer::Context; -/// Tracing layer that pushes log events to the OpenShell server. +/// Tracing layer that pushes log events to the `OpenShell` server. /// /// Events are sent best-effort via `try_send` — if the channel is full the /// event is dropped. Logging must never block the sandbox. @@ -248,7 +248,7 @@ async fn drain_during_backoff( let deadline = tokio::time::Instant::now() + delay; loop { tokio::select! { - _ = tokio::time::sleep_until(deadline) => { return; } + () = tokio::time::sleep_until(deadline) => { return; } line = rx.recv() => { match line { Some(l) => { diff --git a/crates/openshell-sandbox/src/main.rs b/crates/openshell-sandbox/src/main.rs index c7aa41a8f1..a62129582e 100644 --- a/crates/openshell-sandbox/src/main.rs +++ b/crates/openshell-sandbox/src/main.rs @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 -//! OpenShell Sandbox - process sandbox and monitor. +//! `OpenShell` Sandbox - process sandbox and monitor. use std::sync::Arc; use std::sync::atomic::AtomicBool; @@ -16,7 +16,7 @@ use tracing_subscriber::{Layer, layer::SubscriberExt, util::SubscriberInitExt}; use openshell_sandbox::run_sandbox; -/// OpenShell Sandbox - process isolation and monitoring. +/// `OpenShell` Sandbox - process isolation and monitoring. #[derive(Parser, Debug)] #[command(name = "openshell-sandbox")] #[command(version = openshell_core::VERSION)] @@ -40,7 +40,7 @@ struct Args { #[arg(long, short = 'i')] interactive: bool, - /// Sandbox ID for fetching policy via gRPC from OpenShell server. + /// Sandbox ID for fetching policy via gRPC from `OpenShell` server. /// Requires --openshell-endpoint to be set. #[arg(long, env = "OPENSHELL_SANDBOX_ID")] sandbox_id: Option, @@ -50,7 +50,7 @@ struct Args { #[arg(long, env = "OPENSHELL_SANDBOX")] sandbox: Option, - /// OpenShell server gRPC endpoint for fetching policy. + /// `OpenShell` server gRPC endpoint for fetching policy. /// Required when using --sandbox-id. #[arg(long, env = "OPENSHELL_ENDPOINT")] openshell_endpoint: Option, diff --git a/crates/openshell-sandbox/src/mechanistic_mapper.rs b/crates/openshell-sandbox/src/mechanistic_mapper.rs index 3cfdf3f541..825353b527 100644 --- a/crates/openshell-sandbox/src/mechanistic_mapper.rs +++ b/crates/openshell-sandbox/src/mechanistic_mapper.rs @@ -178,13 +178,13 @@ pub async fn generate_proposals(summaries: &[DenialSummary]) -> Vec .map(|(_, name)| format!(" ({name})")) .unwrap_or_default(); - let private_ip_note = if !allowed_ips.is_empty() { + let private_ip_note = if allowed_ips.is_empty() { + String::new() + } else { format!( " Host resolves to private IP ({}); allowed_ips included for SSRF override.", allowed_ips.join(", ") ) - } else { - String::new() }; // Note: hit_count in the DB accumulates across flush cycles, so we @@ -226,7 +226,7 @@ pub async fn generate_proposals(summaries: &[DenialSummary]) -> Vec decided_at_ms: 0, stage, supersedes_chunk_id: String::new(), - hit_count: total_count as i32, + hit_count: total_count.cast_signed(), first_seen_ms, last_seen_ms, binary: binary.clone(), @@ -336,16 +336,15 @@ fn generate_security_notes(host: &str, port: u16, is_ssrf: bool) -> String { /// Falls back to the exact observed path when no pattern applies. fn build_l7_rules(samples: &HashMap<(String, String), u32>) -> Vec { // Deduplicate after generalisation. - let mut seen: HashMap<(String, String), ()> = HashMap::new(); + let mut seen: std::collections::HashSet<(String, String)> = std::collections::HashSet::new(); let mut rules = Vec::new(); for (method, path) in samples.keys() { let generalised = generalise_path(path); let key = (method.clone(), generalised.clone()); - if seen.contains_key(&key) { + if !seen.insert(key) { continue; } - seen.insert(key, ()); rules.push(L7Rule { allow: Some(L7Allow { @@ -405,7 +404,7 @@ fn looks_like_id(segment: &str) -> bool { return true; } // UUID-ish (contains dashes, 32+ hex chars) - let hex_only: String = segment.chars().filter(|c| c.is_ascii_hexdigit()).collect(); + let hex_only: String = segment.chars().filter(char::is_ascii_hexdigit).collect(); if hex_only.len() >= 24 && segment.contains('-') { return true; } @@ -450,10 +449,11 @@ async fn resolve_allowed_ips_if_private(host: &str, port: u32) -> Vec { let addrs = match tokio::net::lookup_host(&addr).await { Ok(addrs) => addrs.collect::>(), Err(e) => { + let port_u16 = u16::try_from(port).unwrap_or(u16::MAX); let event = openshell_ocsf::NetworkActivityBuilder::new(crate::ocsf_ctx()) .activity(openshell_ocsf::ActivityId::Fail) .severity(openshell_ocsf::SeverityId::Low) - .dst_endpoint(openshell_ocsf::Endpoint::from_domain(host, port as u16)) + .dst_endpoint(openshell_ocsf::Endpoint::from_domain(host, port_u16)) .message(format!("DNS resolution failed for allowed_ips check: {e}")) .build(); openshell_ocsf::ocsf_emit!(event); @@ -462,10 +462,11 @@ async fn resolve_allowed_ips_if_private(host: &str, port: u32) -> Vec { }; if addrs.is_empty() { + let port_u16 = u16::try_from(port).unwrap_or(u16::MAX); let event = openshell_ocsf::NetworkActivityBuilder::new(crate::ocsf_ctx()) .activity(openshell_ocsf::ActivityId::Fail) .severity(openshell_ocsf::SeverityId::Low) - .dst_endpoint(openshell_ocsf::Endpoint::from_domain(host, port as u16)) + .dst_endpoint(openshell_ocsf::Endpoint::from_domain(host, port_u16)) .message(format!( "DNS resolution returned no addresses for {host}:{port}" )) diff --git a/crates/openshell-sandbox/src/opa.rs b/crates/openshell-sandbox/src/opa.rs index 0069bcc3ee..841f20ce3b 100644 --- a/crates/openshell-sandbox/src/opa.rs +++ b/crates/openshell-sandbox/src/opa.rs @@ -137,7 +137,7 @@ impl OpaEngine { .severity(openshell_ocsf::SeverityId::Medium) .status(openshell_ocsf::StatusId::Success) .state(openshell_ocsf::StateId::Enabled, "validated") - .unmapped("warning", serde_json::json!(w.to_string())) + .unmapped("warning", serde_json::json!(w.clone())) .message(format!("L7 policy validation warning: {w}")) .build() ); @@ -278,15 +278,14 @@ impl OpaEngine { Some(value_to_string(&matched)) }; - match action_str.as_str() { - "allow" => Ok(NetworkAction::Allow { matched_policy }), - _ => { - let reason_val = engine - .eval_rule("data.openshell.sandbox.deny_reason".into()) - .map_err(|e| miette::miette!("{e}"))?; - let reason = value_to_string(&reason_val); - Ok(NetworkAction::Deny { reason }) - } + if action_str == "allow" { + Ok(NetworkAction::Allow { matched_policy }) + } else { + let reason_val = engine + .eval_rule("data.openshell.sandbox.deny_reason".into()) + .map_err(|e| miette::miette!("{e}"))?; + let reason = value_to_string(&reason_val); + Ok(NetworkAction::Deny { reason }) } } @@ -435,10 +434,10 @@ impl OpaEngine { /// match. This is used by the proxy to decide between full SSRF blocking /// and allowlist-based IP validation. pub fn query_allowed_ips(&self, input: &NetworkInput) -> Result> { - match self.query_endpoint_config(input)? { - Some(val) => Ok(get_str_array(&val, "allowed_ips")), - None => Ok(vec![]), - } + Ok(self + .query_endpoint_config(input)? + .map(|val| get_str_array(&val, "allowed_ips")) + .unwrap_or_default()) } /// Clone the inner regorus engine for per-tunnel L7 evaluation. @@ -557,7 +556,7 @@ fn preprocess_yaml_data(yaml_str: &str) -> Result { .severity(openshell_ocsf::SeverityId::Medium) .status(openshell_ocsf::StatusId::Success) .state(openshell_ocsf::StateId::Enabled, "validated") - .unmapped("warning", serde_json::json!(w.to_string())) + .unmapped("warning", serde_json::json!(w.clone())) .message(format!("L7 policy validation warning: {w}")) .build() ); @@ -594,9 +593,8 @@ fn normalize_endpoint_ports(data: &mut serde_json::Value) { }; for ep in endpoints.iter_mut() { - let ep_obj = match ep.as_object_mut() { - Some(obj) => obj, - None => continue, + let Some(ep_obj) = ep.as_object_mut() else { + continue; }; // If "ports" already exists and is non-empty, keep it. @@ -637,10 +635,12 @@ fn normalize_endpoint_ports(data: &mut serde_json::Value) { /// - Path is not a symlink /// - Resolution fails (binary doesn't exist in container) /// - Resolved path equals the original +/// /// Normalize a path by resolving `.` and `..` components without touching /// the filesystem. Only works correctly for absolute paths. -fn normalize_path(path: &std::path::Path) -> std::path::PathBuf { - let mut result = std::path::PathBuf::new(); +#[cfg(any(target_os = "linux", test))] +fn normalize_path(path: &Path) -> PathBuf { + let mut result = PathBuf::new(); for component in path.components() { match component { std::path::Component::ParentDir => { @@ -664,7 +664,7 @@ fn resolve_binary_in_container(policy_path: &str, entrypoint_pid: u32) -> Option // /proc//root itself (a kernel pseudo-symlink to /) which // strips the prefix we need. read_link only reads the target of // the specified symlink, keeping us in the container's namespace. - let mut resolved = std::path::PathBuf::from(policy_path); + let mut resolved = PathBuf::from(policy_path); // Linux SYMLOOP_MAX is 40; stop before infinite loops for _ in 0..40 { @@ -946,6 +946,13 @@ fn proto_to_opa_data_json(proto: &ProtoSandboxPolicy, entrypoint_pid: u32) -> St } #[cfg(test)] +#[allow( + clippy::needless_raw_string_hashes, + clippy::similar_names, + clippy::doc_markdown, + clippy::match_wildcard_for_single_variants, + reason = "Test code: test fixtures and panic-on-unexpected matches are idiomatic in tests." +)] mod tests { use super::*; @@ -3673,9 +3680,8 @@ network_policies: #[cfg(target_os = "linux")] fn procfs_root_accessible() -> bool { use std::os::unix::fs::symlink; - let dir = match tempfile::tempdir() { - Ok(d) => d, - Err(_) => return false, + let Ok(dir) = tempfile::tempdir() else { + return false; }; let target = dir.path().join("probe_target"); let link = dir.path().join("probe_link"); @@ -3694,6 +3700,8 @@ network_policies: #[cfg(target_os = "linux")] #[test] fn resolve_binary_with_real_symlink() { + use std::os::unix::fs::symlink; + if !procfs_root_accessible() { eprintln!("Skipping: /proc//root/ not accessible in this environment"); return; @@ -3701,7 +3709,6 @@ network_policies: // Create a real symlink in a temp directory and verify resolution // works through /proc/self/root (which maps to / on the host) - use std::os::unix::fs::symlink; let dir = tempfile::tempdir().unwrap(); let target = dir.path().join("python3.11"); let link = dir.path().join("python3"); @@ -3730,13 +3737,14 @@ network_policies: #[cfg(target_os = "linux")] #[test] fn resolve_binary_non_symlink_returns_none() { + use std::io::Write; + if !procfs_root_accessible() { eprintln!("Skipping: /proc//root/ not accessible in this environment"); return; } // A regular file should return None (no expansion needed) - use std::io::Write; let mut tmp = tempfile::NamedTempFile::new().unwrap(); tmp.write_all(b"regular file").unwrap(); tmp.flush().unwrap(); @@ -3754,13 +3762,14 @@ network_policies: #[cfg(target_os = "linux")] #[test] fn resolve_binary_multi_level_symlink() { + use std::os::unix::fs::symlink; + if !procfs_root_accessible() { eprintln!("Skipping: /proc//root/ not accessible in this environment"); return; } // Test multi-level symlink resolution: python3 -> python3.11 -> cpython3.11 - use std::os::unix::fs::symlink; let dir = tempfile::tempdir().unwrap(); let final_target = dir.path().join("cpython3.11"); let mid_link = dir.path().join("python3.11"); @@ -3785,6 +3794,8 @@ network_policies: #[cfg(target_os = "linux")] #[test] fn from_proto_with_pid_expands_symlinks_in_container() { + use std::os::unix::fs::symlink; + if !procfs_root_accessible() { eprintln!("Skipping: /proc//root/ not accessible in this environment"); return; @@ -3792,7 +3803,6 @@ network_policies: // End-to-end test: create a symlink, build engine with our PID, // verify the resolved path is allowed - use std::os::unix::fs::symlink; let dir = tempfile::tempdir().unwrap(); let target = dir.path().join("node22"); let link = dir.path().join("node"); @@ -3861,6 +3871,8 @@ network_policies: #[cfg(target_os = "linux")] #[test] fn reload_from_proto_with_pid_resolves_symlinks() { + use std::os::unix::fs::symlink; + if !procfs_root_accessible() { eprintln!("Skipping: /proc//root/ not accessible in this environment"); return; @@ -3868,7 +3880,6 @@ network_policies: // Test hot-reload path: initial engine at pid=0, then reload with // real PID to trigger symlink resolution - use std::os::unix::fs::symlink; let dir = tempfile::tempdir().unwrap(); let target = dir.path().join("python3.11"); let link = dir.path().join("python3"); diff --git a/crates/openshell-sandbox/src/process.rs b/crates/openshell-sandbox/src/process.rs index 85a57b4e72..d13a99a45f 100644 --- a/crates/openshell-sandbox/src/process.rs +++ b/crates/openshell-sandbox/src/process.rs @@ -35,13 +35,13 @@ fn scrub_sensitive_env(cmd: &mut Command) { } #[cfg(unix)] -#[allow(unsafe_code)] -pub(crate) fn harden_child_process() -> Result<()> { +#[allow(unsafe_code, clippy::borrow_as_ptr)] +pub fn harden_child_process() -> Result<()> { let core_limit = libc::rlimit { rlim_cur: 0, rlim_max: 0, }; - let rc = unsafe { libc::setrlimit(libc::RLIMIT_CORE, &core_limit) }; + let rc = unsafe { libc::setrlimit(libc::RLIMIT_CORE, &raw const core_limit) }; if rc != 0 { return Err(miette::miette!( "Failed to disable core dumps: {}", @@ -57,7 +57,7 @@ pub(crate) fn harden_child_process() -> Result<()> { rlim_cur: 512, rlim_max: 512, }; - let rc = unsafe { libc::setrlimit(libc::RLIMIT_NPROC, &nproc_limit) }; + let rc = unsafe { libc::setrlimit(libc::RLIMIT_NPROC, &raw const nproc_limit) }; if rc != 0 { return Err(miette::miette!( "Failed to set RLIMIT_NPROC: {}", @@ -427,7 +427,10 @@ impl Drop for ProcessHandle { } } +// `effective_gid`/`effective_uid` are intentionally parallel names (same role +// for different identifiers) and the noise from renaming would obscure intent. #[cfg(unix)] +#[allow(clippy::similar_names)] pub fn drop_privileges(policy: &SandboxPolicy) -> Result<()> { let user_name = match policy.process.run_as_user.as_deref() { Some(name) if !name.is_empty() => Some(name), @@ -740,11 +743,7 @@ mod tests { let written = unsafe { libc::write(fds[1], bytes.as_ptr().cast(), bytes.len()) }; unsafe { libc::close(fds[1]); - libc::_exit(if written == bytes.len() as isize { - 0 - } else { - 1 - }); + libc::_exit(i32::from(written != bytes.len().cast_signed())); } } ForkResult::Parent { child } => { @@ -753,7 +752,7 @@ mod tests { let read = unsafe { libc::read(fds[0], bytes.as_mut_ptr().cast(), bytes.len()) }; unsafe { libc::close(fds[0]) }; assert_eq!( - read as usize, + read.cast_unsigned(), bytes.len(), "expected {} probe bytes, got {}", bytes.len(), @@ -791,7 +790,7 @@ mod tests { #[cfg(target_os = "linux")] #[allow(unsafe_code)] unsafe fn dumpable_flag_probe() -> i64 { - unsafe { libc::prctl(libc::PR_GET_DUMPABLE, 0, 0, 0, 0) as i64 } + unsafe { i64::from(libc::prctl(libc::PR_GET_DUMPABLE, 0, 0, 0, 0)) } } #[test] diff --git a/crates/openshell-sandbox/src/procfs.rs b/crates/openshell-sandbox/src/procfs.rs index 9a6d3f9ddb..988ee24120 100644 --- a/crates/openshell-sandbox/src/procfs.rs +++ b/crates/openshell-sandbox/src/procfs.rs @@ -162,7 +162,7 @@ fn resolve_single_tcp_peer_owner(entrypoint_pid: u32, peer_port: u16) -> Result< /// Like `resolve_tcp_peer_binary`, but also returns the PID that owns the socket. /// -/// Needed for the ancestor walk: we must know the PID to walk `/proc//status` PPid chain. +/// Needed for the ancestor walk: we must know the PID to walk `/proc//status` `PPid` chain. #[cfg(target_os = "linux")] pub fn resolve_tcp_peer_identity(entrypoint_pid: u32, peer_port: u16) -> Result<(PathBuf, u32)> { let owner = resolve_single_tcp_peer_owner(entrypoint_pid, peer_port)?; @@ -423,7 +423,7 @@ fn check_pid_fds(pid: u32, target: &str) -> bool { /// /// Performs a BFS walk of the process tree. If `/proc//task//children` /// is not available (requires `CONFIG_PROC_CHILDREN`), returns only the root PID. -#[cfg(target_os = "linux")] +#[cfg(all(test, target_os = "linux"))] fn collect_descendant_pids(root_pid: u32) -> Vec { collect_descendant_pids_with_depth(root_pid) .into_iter() @@ -480,7 +480,7 @@ pub fn file_sha256(path: &Path) -> Result { let mut file = std::fs::File::open(path) .map_err(|e| miette::miette!("Failed to open {}: {e}", path.display()))?; let mut hasher = Sha256::new(); - let mut buf = [0u8; 65536]; + let mut buf = vec![0u8; 65536].into_boxed_slice(); let mut total_read = 0u64; loop { let n = file @@ -514,7 +514,7 @@ mod tests { /// binary for a brief window. Byte-level `starts_with` tolerates the kernel's /// `" (deleted)"` suffix on unlinked executables. #[cfg(target_os = "linux")] - fn wait_for_child_exec(pid: i32, target: &std::path::Path) { + fn wait_for_child_exec(pid: i32, target: &Path) { use std::os::unix::ffi::OsStrExt as _; let target_bytes = target.as_os_str().as_bytes(); let deadline = std::time::Instant::now() + std::time::Duration::from_secs(2); @@ -752,10 +752,7 @@ mod tests { fn collect_descendants_dedupes_pids() { let pid = std::process::id(); let pids = collect_descendant_pids(pid); - let unique = pids - .iter() - .copied() - .collect::>(); + let unique = pids.iter().copied().collect::>(); assert_eq!(pids.len(), unique.len()); } @@ -771,16 +768,20 @@ mod tests { let peer_port = stream.local_addr().unwrap().port(); let (_accepted, _) = listener.accept().expect("accept"); + // libc/syscall FFI requires unsafe + #[allow(unsafe_code)] let child_pid = unsafe { libc::fork() }; assert!(child_pid >= 0, "fork failed"); if child_pid == 0 { + // libc/syscall FFI requires unsafe + #[allow(unsafe_code)] unsafe { libc::sleep(30); libc::_exit(0); } } - let child_pid_u32 = child_pid as u32; + let child_pid_u32 = child_pid.cast_unsigned(); let entrypoint_pid = std::process::id(); let deadline = Instant::now() + Duration::from_secs(2); let owners = loop { @@ -790,18 +791,19 @@ mod tests { .owners .iter() .map(|owner| owner.pid) - .collect::>(); + .collect::>(); if owner_pids.contains(&entrypoint_pid) && owner_pids.contains(&child_pid_u32) { break owners; } assert!( Instant::now() < deadline, - "timed out waiting for forked child to appear as a socket owner; got {:?}", - owner_pids + "timed out waiting for forked child to appear as a socket owner; got {owner_pids:?}" ); std::thread::sleep(Duration::from_millis(20)); }; + // libc/syscall FFI requires unsafe + #[allow(unsafe_code)] unsafe { libc::kill(child_pid, libc::SIGKILL); libc::waitpid(child_pid, std::ptr::null_mut(), 0); @@ -811,7 +813,7 @@ mod tests { .owners .iter() .map(|owner| owner.pid) - .collect::>(); + .collect::>(); assert!(owner_pids.contains(&entrypoint_pid)); assert!(owner_pids.contains(&child_pid_u32)); } diff --git a/crates/openshell-sandbox/src/proxy.rs b/crates/openshell-sandbox/src/proxy.rs index 69ff97b045..a4ad694a9c 100644 --- a/crates/openshell-sandbox/src/proxy.rs +++ b/crates/openshell-sandbox/src/proxy.rs @@ -79,6 +79,9 @@ pub struct InferenceContext { } impl InferenceContext { + // `router`/`routes` are intentionally distinct nouns (the router and the + // route list it consumes); both names are clearer than alternatives. + #[allow(clippy::similar_names)] pub fn new( patterns: Vec, router: openshell_router::Router, @@ -295,6 +298,10 @@ fn emit_denial_simple( } } +// Many distinct, non-related context parameters are required for a CONNECT +// dispatch; bundling them into a struct would just shift the noise into call +// sites. +#[allow(clippy::too_many_arguments)] async fn handle_tcp_connection( mut client: TcpStream, opa_engine: Arc, @@ -497,6 +504,9 @@ async fn handle_tcp_connection( // Defense-in-depth: resolve DNS and reject connections to internal IPs. let dns_connect_start = std::time::Instant::now(); + // The "non-empty" branch is the explicit-allowlist path; reading it first + // matches the policy decision narrative. + #[allow(clippy::if_not_else)] let mut upstream = if !raw_allowed_ips.is_empty() { // allowed_ips mode: validate resolved IPs against CIDR allowlist. // Loopback and link-local are still always blocked. @@ -1597,7 +1607,7 @@ fn query_l7_config( // Only query if action is Allow (not Deny) let has_policy = match &decision.action { NetworkAction::Allow { matched_policy } => matched_policy.is_some(), - _ => false, + NetworkAction::Deny { .. } => false, }; if !has_policy { return None; @@ -1640,7 +1650,7 @@ fn query_tls_mode( ) -> crate::l7::TlsMode { let has_policy = match &decision.action { NetworkAction::Allow { matched_policy } => matched_policy.is_some(), - _ => false, + NetworkAction::Deny { .. } => false, }; if !has_policy { return crate::l7::TlsMode::Auto; @@ -1763,7 +1773,10 @@ async fn resolve_from_sandbox_hosts( if addrs.is_empty() { None } else { Some(addrs) } } +// Mirrors the Linux signature so call sites can `.await` uniformly across +// platforms; the non-Linux path has nothing to await. #[cfg(not(target_os = "linux"))] +#[allow(clippy::unused_async)] async fn resolve_from_sandbox_hosts( _host: &str, _port: u16, @@ -1969,7 +1982,7 @@ fn parse_allowed_ips(raw: &[String]) -> std::result::Result, S } nets.push(n); } - Err(_) => errors.push(format!("invalid CIDR/IP in allowed_ips: {entry}")), + Err(()) => errors.push(format!("invalid CIDR/IP in allowed_ips: {entry}")), } } @@ -1980,7 +1993,7 @@ fn parse_allowed_ips(raw: &[String]) -> std::result::Result, S } } -/// Query allowed_ips from the matched endpoint config for a CONNECT decision. +/// Query `allowed_ips` from the matched endpoint config for a CONNECT decision. fn query_allowed_ips( engine: &OpaEngine, decision: &ConnectDecision, @@ -1990,7 +2003,7 @@ fn query_allowed_ips( // Only query if action is Allow with a matched policy let has_policy = match &decision.action { NetworkAction::Allow { matched_policy } => matched_policy.is_some(), - _ => false, + NetworkAction::Deny { .. } => false, }; if !has_policy { return vec![]; @@ -2049,15 +2062,12 @@ fn normalize_inference_path(path: &str) -> String { fn extract_host_from_uri(uri: &str) -> String { // Absolute-form URIs look like "http://host[:port]/path" // Strip the scheme prefix, then extract the authority (host[:port]) before the first '/'. - let after_scheme = uri.find("://").map(|i| &uri[i + 3..]).unwrap_or(uri); + let after_scheme = uri.find("://").map_or(uri, |i| &uri[i + 3..]); let authority = after_scheme.split('/').next().unwrap_or(after_scheme); // Strip port if present (handle IPv6 bracket notation) let host = if authority.starts_with('[') { // IPv6: [::1]:port - authority - .find(']') - .map(|i| &authority[..=i]) - .unwrap_or(authority) + authority.find(']').map_or(authority, |i| &authority[..=i]) } else { authority.split(':').next().unwrap_or(authority) }; @@ -2092,14 +2102,12 @@ fn parse_proxy_uri(uri: &str) -> Result<(String, String, u16, String)> { .find(']') .ok_or_else(|| miette::miette!("Unclosed IPv6 bracket in URI: {uri}"))?; let after_bracket = &rest[bracket_end + 1..]; - if let Some(slash_pos) = after_bracket.find('/') { + after_bracket.find('/').map_or((rest, "/"), |slash_pos| { ( - &rest[..bracket_end + 1 + slash_pos], + &rest[..=bracket_end + slash_pos], &after_bracket[slash_pos..], ) - } else { - (&rest[..], "/") - } + }) } else if let Some(slash_pos) = rest.find('/') { (&rest[..slash_pos], &rest[slash_pos..]) } else { @@ -2210,10 +2218,10 @@ fn rewrite_forward_request( continue; } - let rewritten_line = match secret_resolver { - Some(resolver) => rewrite_header_line(line, resolver), - None => line.to_string(), - }; + let rewritten_line = secret_resolver.map_or_else( + || line.to_string(), + |resolver| rewrite_header_line(line, resolver), + ); output.extend_from_slice(rewritten_line.as_bytes()); output.extend_from_slice(b"\r\n"); @@ -2256,6 +2264,9 @@ fn rewrite_forward_request( /// Private IPs require explicit `allowed_ips` on the endpoint config (SSRF /// override). Rewrites the absolute-form request to origin-form, connects /// upstream, and relays the response using `copy_bidirectional` for streaming. +// Many distinct, non-related context parameters are required for forward proxy +// dispatch; bundling them into a struct would just shift the noise into call sites. +#[allow(clippy::too_many_arguments)] async fn handle_forward_proxy( method: &str, target_uri: &str, @@ -2466,10 +2477,11 @@ async fn handle_forward_proxy( let query_params = match crate::l7::path::canonicalize_request_target(&path, &canonicalize_options) { Ok((canon, query)) => { - let params = match query.as_deref() { - Some(q) => crate::l7::rest::parse_query_params(q).unwrap_or_default(), - None => std::collections::HashMap::new(), - }; + let params = query + .as_deref() + .map_or_else(std::collections::HashMap::new, |q| { + crate::l7::rest::parse_query_params(q).unwrap_or_default() + }); path = canon.path; params } @@ -2525,13 +2537,8 @@ async fn handle_forward_proxy( { let (action_id, disposition_id, severity) = match decision_str { - "allow" => ( - ActionId::Allowed, - DispositionId::Allowed, - SeverityId::Informational, - ), "deny" => (ActionId::Denied, DispositionId::Blocked, SeverityId::Medium), - "audit" => ( + "allow" | "audit" => ( ActionId::Allowed, DispositionId::Allowed, SeverityId::Informational, @@ -2603,6 +2610,9 @@ async fn handle_forward_proxy( raw_allowed_ips = implicit_allowed_ips_for_ip_host(&host); } + // The "non-empty" branch is the explicit-allowlist path; reading it first + // matches the policy decision narrative. + #[allow(clippy::if_not_else)] let addrs = if !raw_allowed_ips.is_empty() { // allowed_ips mode: validate resolved IPs against CIDR allowlist. @@ -2916,6 +2926,12 @@ fn is_benign_relay_error(err: &miette::Report) -> bool { } #[cfg(test)] +#[allow( + clippy::needless_raw_string_hashes, + clippy::iter_on_single_items, + clippy::needless_continue, + reason = "Test code: test fixtures and explicit control-flow markers are idiomatic in tests." +)] mod tests { use super::*; use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}; @@ -4378,6 +4394,8 @@ mod tests { let (_accepted, _) = listener.accept().expect("accept"); let fd = stream.as_raw_fd(); + // libc/syscall FFI requires unsafe + #[allow(unsafe_code)] unsafe { let flags = libc::fcntl(fd, libc::F_GETFD); assert!(flags >= 0, "F_GETFD failed"); @@ -4391,9 +4409,13 @@ mod tests { let sleep_path = CString::new("/bin/sleep").unwrap(); let arg0 = CString::new("sleep").unwrap(); let arg1 = CString::new("30").unwrap(); + // libc/syscall FFI requires unsafe + #[allow(unsafe_code)] let child_pid = unsafe { libc::fork() }; assert!(child_pid >= 0, "fork failed"); if child_pid == 0 { + // libc/syscall FFI requires unsafe + #[allow(unsafe_code)] unsafe { libc::execl( sleep_path.as_ptr(), @@ -4420,8 +4442,27 @@ mod tests { } let cache = BinaryIdentityCache::new(); - let result = resolve_process_identity(std::process::id(), peer_port, &cache); + // Resolve with a brief retry loop — under heavy CI load the child's + // procfs entry can momentarily fail to resolve even though the loop + // above just verified `/proc//exe` pointed at `sleep`. Retry a + // few times before declaring failure so the test is not flaky. + let mut result = resolve_process_identity(std::process::id(), peer_port, &cache); + for _ in 0..5 { + match &result { + Err(err) + if err.reason.contains("No such file or directory") + || err.reason.contains("os error 2") => + { + std::thread::sleep(Duration::from_millis(50)); + result = resolve_process_identity(std::process::id(), peer_port, &cache); + } + _ => break, + } + } + + // libc/syscall FFI requires unsafe + #[allow(unsafe_code)] unsafe { libc::kill(child_pid, libc::SIGKILL); libc::waitpid(child_pid, std::ptr::null_mut(), 0); diff --git a/crates/openshell-sandbox/src/sandbox/linux/landlock.rs b/crates/openshell-sandbox/src/sandbox/linux/landlock.rs index b982c52385..214fc700af 100644 --- a/crates/openshell-sandbox/src/sandbox/linux/landlock.rs +++ b/crates/openshell-sandbox/src/sandbox/linux/landlock.rs @@ -95,7 +95,7 @@ pub struct PreparedRuleset { compatibility: LandlockCompatibility, } -/// Phase 1: Open PathFds and build the Landlock ruleset **as root**. +/// Phase 1: Open `PathFds` and build the Landlock ruleset **as root**. /// /// This must run before `drop_privileges()` so that `PathFd::new()` can open /// paths that are only accessible to root (e.g. mode 700 directories). @@ -272,6 +272,7 @@ pub fn enforce(prepared: PreparedRuleset) -> Result<()> { /// Legacy single-phase apply. Kept for non-Linux platforms and tests. /// On Linux, callers should use [`prepare`] + [`enforce`] for correct /// privilege ordering. +#[allow(dead_code)] // Retained for backward compat; live callers use prepare+enforce. pub fn apply(policy: &SandboxPolicy, workdir: Option<&str>) -> Result<()> { if let Some(prepared) = prepare(policy, workdir)? { enforce(prepared)?; diff --git a/crates/openshell-sandbox/src/sandbox/linux/mod.rs b/crates/openshell-sandbox/src/sandbox/linux/mod.rs index 565ce4f46c..848ab1e3b7 100644 --- a/crates/openshell-sandbox/src/sandbox/linux/mod.rs +++ b/crates/openshell-sandbox/src/sandbox/linux/mod.rs @@ -13,7 +13,7 @@ use std::path::PathBuf; use std::sync::Once; /// Opaque handle to a prepared-but-not-yet-enforced sandbox. -/// Holds the Landlock ruleset with PathFds opened as root. +/// Holds the Landlock ruleset with `PathFds` opened as root. pub struct PreparedSandbox { landlock: Option, policy: SandboxPolicy, @@ -21,7 +21,7 @@ pub struct PreparedSandbox { /// Phase 1: Prepare sandbox restrictions **as root** (before `drop_privileges`). /// -/// Opens Landlock PathFds while the process still has root privileges, +/// Opens Landlock `PathFds` while the process still has root privileges, /// ensuring paths like mode-700 directories are accessible. pub fn prepare(policy: &SandboxPolicy, workdir: Option<&str>) -> Result { let landlock = landlock::prepare(policy, workdir)?; @@ -50,6 +50,7 @@ pub fn apply_supervisor_prelude() -> Result<()> { /// Legacy single-phase apply. Kept for backward compatibility. /// New callers should use [`prepare`] + [`enforce`] for correct privilege ordering. +#[allow(dead_code)] // Retained for backward compat; live callers use prepare+enforce. pub fn apply(policy: &SandboxPolicy, workdir: Option<&str>) -> Result<()> { landlock::apply(policy, workdir)?; seccomp::apply(policy)?; @@ -72,12 +73,12 @@ pub fn log_sandbox_readiness(policy: &SandboxPolicy, workdir: Option<&str>) { let mut read_write = policy.filesystem.read_write.clone(); let read_only = &policy.filesystem.read_only; - if policy.filesystem.include_workdir { - if let Some(dir) = workdir { - let workdir_path = PathBuf::from(dir); - if !read_write.contains(&workdir_path) { - read_write.push(workdir_path); - } + if policy.filesystem.include_workdir + && let Some(dir) = workdir + { + let workdir_path = PathBuf::from(dir); + if !read_write.contains(&workdir_path) { + read_write.push(workdir_path); } } @@ -96,74 +97,71 @@ pub fn log_sandbox_readiness(policy: &SandboxPolicy, workdir: Option<&str>) { } let availability = landlock::probe_availability(); - match &availability { - landlock::LandlockAvailability::Available { abi } => { - openshell_ocsf::ocsf_emit!( - openshell_ocsf::ConfigStateChangeBuilder::new(crate::ocsf_ctx()) - .severity(openshell_ocsf::SeverityId::Informational) - .status(openshell_ocsf::StatusId::Success) - .state(openshell_ocsf::StateId::Enabled, "probed") - .message(format!( - "Landlock filesystem sandbox available \ - [abi:v{abi} compat:{:?} ro:{} rw:{}]", - policy.landlock.compatibility, - read_only.len(), - read_write.len(), - )) - .build() - ); - } - _ => { - // Landlock is NOT available — this is the critical log that was - // previously invisible because it only fired inside pre_exec. - let is_best_effort = matches!( - policy.landlock.compatibility, - crate::policy::LandlockCompatibility::BestEffort - ); - let (desc, msg) = if is_best_effort { - ( - format!( - "Sandbox will run WITHOUT filesystem restrictions: {availability}. \ - Policy requests {total_paths} path rule(s) \ - (ro:{} rw:{}) but Landlock cannot enforce them. \ - Set landlock.compatibility to 'hard_requirement' to make this fatal.", - read_only.len(), - read_write.len(), - ), - format!( - "Landlock filesystem sandbox unavailable (best_effort, degraded): {availability}" - ), - ) - } else { - ( - format!( - "Landlock is unavailable: {availability}. \ - Policy requires {total_paths} path rule(s) \ - (ro:{} rw:{}) with hard_requirement — sandbox startup will fail.", - read_only.len(), - read_write.len(), - ), - format!( - "Landlock filesystem sandbox unavailable (hard_requirement, will fail): {availability}" - ), - ) - }; - openshell_ocsf::ocsf_emit!( - openshell_ocsf::DetectionFindingBuilder::new(crate::ocsf_ctx()) - .activity(openshell_ocsf::ActivityId::Open) - .severity(openshell_ocsf::SeverityId::High) - .confidence(openshell_ocsf::ConfidenceId::High) - .is_alert(true) - .finding_info( - openshell_ocsf::FindingInfo::new( - "landlock-unavailable", - "Landlock Filesystem Sandbox Unavailable", - ) - .with_desc(&desc), + if let landlock::LandlockAvailability::Available { abi } = &availability { + openshell_ocsf::ocsf_emit!( + openshell_ocsf::ConfigStateChangeBuilder::new(crate::ocsf_ctx()) + .severity(openshell_ocsf::SeverityId::Informational) + .status(openshell_ocsf::StatusId::Success) + .state(openshell_ocsf::StateId::Enabled, "probed") + .message(format!( + "Landlock filesystem sandbox available \ + [abi:v{abi} compat:{:?} ro:{} rw:{}]", + policy.landlock.compatibility, + read_only.len(), + read_write.len(), + )) + .build() + ); + } else { + // Landlock is NOT available — this is the critical log that was + // previously invisible because it only fired inside pre_exec. + let is_best_effort = matches!( + policy.landlock.compatibility, + crate::policy::LandlockCompatibility::BestEffort + ); + let (desc, msg) = if is_best_effort { + ( + format!( + "Sandbox will run WITHOUT filesystem restrictions: {availability}. \ + Policy requests {total_paths} path rule(s) \ + (ro:{} rw:{}) but Landlock cannot enforce them. \ + Set landlock.compatibility to 'hard_requirement' to make this fatal.", + read_only.len(), + read_write.len(), + ), + format!( + "Landlock filesystem sandbox unavailable (best_effort, degraded): {availability}" + ), + ) + } else { + ( + format!( + "Landlock is unavailable: {availability}. \ + Policy requires {total_paths} path rule(s) \ + (ro:{} rw:{}) with hard_requirement — sandbox startup will fail.", + read_only.len(), + read_write.len(), + ), + format!( + "Landlock filesystem sandbox unavailable (hard_requirement, will fail): {availability}" + ), + ) + }; + openshell_ocsf::ocsf_emit!( + openshell_ocsf::DetectionFindingBuilder::new(crate::ocsf_ctx()) + .activity(openshell_ocsf::ActivityId::Open) + .severity(openshell_ocsf::SeverityId::High) + .confidence(openshell_ocsf::ConfidenceId::High) + .is_alert(true) + .finding_info( + openshell_ocsf::FindingInfo::new( + "landlock-unavailable", + "Landlock Filesystem Sandbox Unavailable", ) - .message(msg) - .build() - ); - } + .with_desc(&desc), + ) + .message(msg) + .build() + ); } } diff --git a/crates/openshell-sandbox/src/sandbox/linux/netns.rs b/crates/openshell-sandbox/src/sandbox/linux/netns.rs index bbd02255f5..e926335e09 100644 --- a/crates/openshell-sandbox/src/sandbox/linux/netns.rs +++ b/crates/openshell-sandbox/src/sandbox/linux/netns.rs @@ -11,7 +11,7 @@ use miette::{IntoDiagnostic, Result}; use std::net::IpAddr; use std::os::unix::io::RawFd; use std::process::Command; -use tracing::{debug, info, warn}; +use tracing::{debug, warn}; use uuid::Uuid; /// Default subnet for sandbox networking. @@ -211,6 +211,8 @@ impl NetworkNamespace { if let Some(fd) = self.ns_fd { debug!(namespace = %self.name, "Entering network namespace via setns"); // SAFETY: setns is safe to call after fork, before exec + // libc/syscall FFI requires unsafe + #[allow(unsafe_code)] let result = unsafe { libc::setns(fd, libc::CLONE_NEWNET) }; if result != 0 { return Err(miette::miette!( @@ -235,7 +237,7 @@ impl NetworkNamespace { /// Install iptables rules for bypass detection inside the namespace. /// /// Sets up OUTPUT chain rules that: - /// 1. ACCEPT traffic destined for the proxy (host_ip:proxy_port) + /// 1. ACCEPT traffic destined for the proxy (`host_ip:proxy_port`) /// 2. ACCEPT loopback traffic /// 3. ACCEPT established/related connections (response packets) /// 4. LOG + REJECT all other TCP/UDP traffic (bypass attempts) @@ -251,22 +253,19 @@ impl NetworkNamespace { /// diagnostic logging. pub fn install_bypass_rules(&self, proxy_port: u16) -> Result<()> { // Check if iptables is available before attempting to install rules. - let iptables_path = match find_iptables() { - Some(path) => path, - None => { - openshell_ocsf::ocsf_emit!(openshell_ocsf::ConfigStateChangeBuilder::new( - crate::ocsf_ctx() - ) - .severity(openshell_ocsf::SeverityId::Medium) - .status(openshell_ocsf::StatusId::Failure) - .state(openshell_ocsf::StateId::Disabled, "degraded") - .message(format!( - "iptables not found; bypass detection rules will not be installed [ns:{}]", - self.name - )) - .build()); - return Ok(()); - } + let Some(iptables_path) = find_iptables() else { + openshell_ocsf::ocsf_emit!( + openshell_ocsf::ConfigStateChangeBuilder::new(crate::ocsf_ctx()) + .severity(openshell_ocsf::SeverityId::Medium) + .status(openshell_ocsf::StatusId::Failure) + .state(openshell_ocsf::StateId::Disabled, "degraded") + .message(format!( + "iptables not found; bypass detection rules will not be installed [ns:{}]", + self.name + )) + .build() + ); + return Ok(()); }; let host_ip_str = self.host_ip.to_string(); @@ -299,20 +298,20 @@ impl NetworkNamespace { // Install IPv6 rules — best-effort. // Skip the proxy ACCEPT rule for IPv6 since the proxy address is IPv4. - if let Some(ip6_path) = find_ip6tables(&iptables_path) { - if let Err(e) = self.install_bypass_rules_for_v6(&ip6_path, &log_prefix) { - openshell_ocsf::ocsf_emit!(openshell_ocsf::ConfigStateChangeBuilder::new( - crate::ocsf_ctx() - ) - .severity(openshell_ocsf::SeverityId::Low) - .status(openshell_ocsf::StatusId::Failure) - .state(openshell_ocsf::StateId::Other, "degraded") - .message(format!( - "Failed to install IPv6 bypass detection rules (non-fatal) [ns:{}]: {e}", - self.name - )) - .build()); - } + if let Some(ip6_path) = find_ip6tables(&iptables_path) + && let Err(e) = self.install_bypass_rules_for_v6(&ip6_path, &log_prefix) + { + openshell_ocsf::ocsf_emit!( + openshell_ocsf::ConfigStateChangeBuilder::new(crate::ocsf_ctx()) + .severity(openshell_ocsf::SeverityId::Low) + .status(openshell_ocsf::StatusId::Failure) + .state(openshell_ocsf::StateId::Other, "degraded") + .message(format!( + "Failed to install IPv6 bypass detection rules (non-fatal) [ns:{}]: {e}", + self.name + )) + .build() + ); } openshell_ocsf::ocsf_emit!( @@ -755,13 +754,13 @@ fn run_iptables_netns(netns: &str, iptables_cmd: &str, args: &[&str]) -> Result< const IPTABLES_SEARCH_PATHS: &[&str] = &["/usr/sbin/iptables", "/sbin/iptables", "/usr/bin/iptables"]; -/// Returns true if xt extension modules (e.g. xt_comment) cannot be used +/// Returns true if xt extension modules (e.g. `xt_comment`) cannot be used /// via the given iptables binary. /// -/// Some kernels have nf_tables but lack the nft_compat bridge that allows -/// xt extension modules to be used through the nf_tables path (e.g. Jetson +/// Some kernels have `nf_tables` but lack the `nft_compat` bridge that allows +/// xt extension modules to be used through the `nf_tables` path (e.g. Jetson /// Linux 5.15-tegra). This probe detects that condition by attempting to -/// insert a rule using the xt_comment extension. If it fails, xt extensions +/// insert a rule using the `xt_comment` extension. If it fails, xt extensions /// are unavailable and the caller should fall back to iptables-legacy. fn xt_extensions_unavailable(iptables_path: &str) -> bool { // Create a temporary probe chain. If this fails (e.g. no CAP_NET_ADMIN), @@ -769,8 +768,7 @@ fn xt_extensions_unavailable(iptables_path: &str) -> bool { let created = Command::new(iptables_path) .args(["-t", "filter", "-N", "_xt_probe"]) .output() - .map(|o| o.status.success()) - .unwrap_or(false); + .is_ok_and(|o| o.status.success()); if !created { return false; @@ -792,8 +790,7 @@ fn xt_extensions_unavailable(iptables_path: &str) -> bool { "ACCEPT", ]) .output() - .map(|o| o.status.success()) - .unwrap_or(false); + .is_ok_and(|o| o.status.success()); // Clean up — best-effort, ignore failures. let _ = Command::new(iptables_path) diff --git a/crates/openshell-sandbox/src/sandbox/linux/seccomp.rs b/crates/openshell-sandbox/src/sandbox/linux/seccomp.rs index dee96d7ced..f614640234 100644 --- a/crates/openshell-sandbox/src/sandbox/linux/seccomp.rs +++ b/crates/openshell-sandbox/src/sandbox/linux/seccomp.rs @@ -7,10 +7,10 @@ //! //! 1. **Socket domain blocks** -- prevent raw/kernel sockets that bypass the proxy //! 2. **Unconditional syscall blocks** -- block syscalls that enable sandbox escape -//! (fileless exec, ptrace, BPF, cross-process memory access, io_uring, mount) +//! (fileless exec, ptrace, BPF, cross-process memory access, `io_uring`, mount) //! 3. **Conditional syscall blocks** -- block dangerous flag combinations on otherwise -//! needed syscalls (execveat+AT_EMPTY_PATH, unshare+CLONE_NEWUSER, -//! seccomp+SET_MODE_FILTER) +//! needed syscalls (`execveat+AT_EMPTY_PATH`, `unshare+CLONE_NEWUSER`, +//! `seccomp+SET_MODE_FILTER`) use crate::policy::{NetworkMode, SandboxPolicy}; use miette::{IntoDiagnostic, Result}; @@ -90,6 +90,8 @@ fn build_supervisor_prelude_rules() -> BTreeMap> { } fn set_no_new_privs() -> Result<()> { + // libc/syscall FFI requires unsafe + #[allow(unsafe_code)] let rc = unsafe { libc::prctl(libc::PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) }; if rc != 0 { return Err(miette::miette!( @@ -119,7 +121,7 @@ fn compile_filter( /// /// This is a separate filter from the main one because seccomp BPF cannot /// dereference the `struct clone_args *` pointer that clone3 takes as arg 0, -/// so we cannot selectively block CLONE_NEWUSER. We block clone3 +/// so we cannot selectively block `CLONE_NEWUSER`. We block clone3 /// unconditionally with ENOSYS so glibc falls back to the older clone /// syscall (where flags are a direct register argument and CAN be filtered). /// @@ -140,8 +142,8 @@ fn build_clone3_filter() -> Result { /// 2. Install the main filter second. It blocks further seccomp filter /// installation with `EPERM`, preserving the original hardening intent. fn apply_runtime_filters( - main_filter: seccompiler::BpfProgramRef, - clone3_filter: seccompiler::BpfProgramRef, + main_filter: seccompiler::BpfProgramRef<'_>, + clone3_filter: seccompiler::BpfProgramRef<'_>, ) -> Result<()> { apply_filter(clone3_filter).into_diagnostic()?; apply_filter(main_filter).into_diagnostic()?; @@ -283,6 +285,15 @@ fn add_masked_arg_rule( } #[cfg(test)] +// libc/syscall FFI requires unsafe; these tests fork children and exercise +// blocked syscalls, so unsafe blocks/calls are pervasive. +#[allow( + unsafe_code, + unsafe_op_in_unsafe_fn, + unused_unsafe, + clippy::borrow_as_ptr, + trivial_numeric_casts +)] mod tests { use super::*; diff --git a/crates/openshell-sandbox/src/sandbox/mod.rs b/crates/openshell-sandbox/src/sandbox/mod.rs index a153b7b9e5..95aeae492f 100644 --- a/crates/openshell-sandbox/src/sandbox/mod.rs +++ b/crates/openshell-sandbox/src/sandbox/mod.rs @@ -14,6 +14,9 @@ pub mod linux; /// # Errors /// /// Returns an error if the sandbox cannot be applied. +// On Linux the spawn path uses `prepare`+`enforce` directly; this single-phase +// apply is only invoked from the non-Linux spawn_impl. +#[cfg_attr(target_os = "linux", allow(dead_code))] #[cfg_attr(not(target_os = "linux"), allow(clippy::unnecessary_wraps))] pub fn apply(policy: &SandboxPolicy, workdir: Option<&str>) -> Result<()> { #[cfg(target_os = "linux")] diff --git a/crates/openshell-sandbox/src/secrets.rs b/crates/openshell-sandbox/src/secrets.rs index a27537c910..63e253e50e 100644 --- a/crates/openshell-sandbox/src/secrets.rs +++ b/crates/openshell-sandbox/src/secrets.rs @@ -8,7 +8,7 @@ use std::fmt; const PLACEHOLDER_PREFIX: &str = "openshell:resolve:env:"; /// Public access to the placeholder prefix for fail-closed scanning in other modules. -pub(crate) const PLACEHOLDER_PREFIX_PUBLIC: &str = PLACEHOLDER_PREFIX; +pub const PLACEHOLDER_PREFIX_PUBLIC: &str = PLACEHOLDER_PREFIX; /// Characters that are valid in an env var key name (used to extract /// placeholder boundaries within concatenated strings like path segments). @@ -23,7 +23,7 @@ fn is_env_key_char(b: u8) -> bool { /// Error returned when a placeholder cannot be resolved or a resolved secret /// contains prohibited characters. #[derive(Debug)] -pub(crate) struct UnresolvedPlaceholderError { +pub struct UnresolvedPlaceholderError { pub location: &'static str, // "header", "query_param", "path" } @@ -39,18 +39,21 @@ impl fmt::Display for UnresolvedPlaceholderError { /// Result of rewriting an HTTP header block with credential resolution. #[derive(Debug)] -pub(crate) struct RewriteResult { +pub struct RewriteResult { /// The rewritten HTTP bytes (headers + body overflow). pub rewritten: Vec, /// A redacted version of the request target for logging. /// Contains `[CREDENTIAL]` in place of resolved credential values. /// `None` if the target was not modified. + // Kept on the public result struct as part of the API contract; consumed + // selectively by callers that emit redacted logs. + #[allow(dead_code)] pub redacted_target: Option, } /// Result of rewriting a request target for OPA evaluation. #[derive(Debug)] -pub(crate) struct RewriteTargetResult { +pub struct RewriteTargetResult { /// The resolved target (real secrets) — for upstream forwarding only. pub resolved: String, /// The redacted target (`[CREDENTIAL]` in place of secrets) — for OPA + logs. @@ -119,10 +122,9 @@ impl SecretResolver { .strip_prefix("Basic ") .or_else(|| trimmed.strip_prefix("basic ")) .map(str::trim) + && let Some(rewritten) = self.rewrite_basic_auth_token(encoded) { - if let Some(rewritten) = self.rewrite_basic_auth_token(encoded) { - return Some(format!("Basic {rewritten}")); - } + return Some(format!("Basic {rewritten}")); } // Prefixed placeholder: `Bearer openshell:resolve:env:KEY` @@ -172,7 +174,7 @@ impl SecretResolver { } } -pub(crate) fn placeholder_for_env_key(key: &str) -> String { +pub fn placeholder_for_env_key(key: &str) -> String { format!("{PLACEHOLDER_PREFIX}{key}") } @@ -226,12 +228,26 @@ fn percent_encode_path_segment(input: &str) -> String { let mut encoded = String::with_capacity(input.len()); for byte in input.bytes() { match byte { - // unreserved - b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.' | b'_' | b'~' => { - encoded.push(byte as char); - } - // sub-delims + ":" + "@" - b'!' | b'$' | b'&' | b'\'' | b'(' | b')' | b'*' | b'+' | b',' | b';' | b'=' | b':' + // unreserved + sub-delims + ":" + "@" + b'A'..=b'Z' + | b'a'..=b'z' + | b'0'..=b'9' + | b'-' + | b'.' + | b'_' + | b'~' + | b'!' + | b'$' + | b'&' + | b'\'' + | b'(' + | b')' + | b'*' + | b'+' + | b',' + | b';' + | b'=' + | b':' | b'@' => { encoded.push(byte as char); } @@ -254,11 +270,11 @@ fn percent_decode(input: &str) -> String { let lo = bytes.next(); if let (Some(h), Some(l)) = (hi, lo) { let hex = [h, l]; - if let Ok(s) = std::str::from_utf8(&hex) { - if let Ok(val) = u8::from_str_radix(s, 16) { - decoded.push(val); - continue; - } + if let Ok(s) = std::str::from_utf8(&hex) + && let Ok(val) = u8::from_str_radix(s, 16) + { + decoded.push(val); + continue; } // Invalid percent encoding — preserve verbatim decoded.push(b'%'); @@ -318,46 +334,34 @@ struct RewriteLineResult { /// /// Given a request line like `GET /bot{TOKEN}/path?key={APIKEY} HTTP/1.1`, /// resolves placeholders in both path segments and query parameter values. +// `resolver` (the credential resolver) and `resolved` (the resolved string +// output) are intentionally distinct nouns; renaming would obscure intent. +#[allow(clippy::similar_names)] fn rewrite_request_line( line: &str, resolver: &SecretResolver, ) -> Result { // Request line format: METHOD SP REQUEST-URI SP HTTP-VERSION let mut parts = line.splitn(3, ' '); - let method = match parts.next() { - Some(m) => m, - None => { - return Ok(RewriteLineResult { - line: line.to_string(), - redacted_target: None, - }); - } + let unchanged = || { + Ok(RewriteLineResult { + line: line.to_string(), + redacted_target: None, + }) }; - let uri = match parts.next() { - Some(u) => u, - None => { - return Ok(RewriteLineResult { - line: line.to_string(), - redacted_target: None, - }); - } + let Some(method) = parts.next() else { + return unchanged(); }; - let version = match parts.next() { - Some(v) => v, - None => { - return Ok(RewriteLineResult { - line: line.to_string(), - redacted_target: None, - }); - } + let Some(uri) = parts.next() else { + return unchanged(); + }; + let Some(version) = parts.next() else { + return unchanged(); }; // Only rewrite if the URI contains a placeholder if !uri.contains(PLACEHOLDER_PREFIX) { - return Ok(RewriteLineResult { - line: line.to_string(), - redacted_target: None, - }); + return unchanged(); } // Split URI into path and query @@ -382,9 +386,10 @@ fn rewrite_request_line( }; // Reassemble - let resolved_uri = match &resolved_query { - Some(q) => format!("{resolved_path}?{q}"), - None => resolved_path.clone(), + let resolved_uri = if let Some(q) = resolved_query.as_ref() { + format!("{resolved_path}?{q}") + } else { + resolved_path }; let redacted_uri = match &redacted_query { Some(q) => format!("{redacted_path}?{q}"), @@ -404,6 +409,9 @@ fn rewrite_request_line( /// /// Returns `Some((resolved_path, redacted_path))` if any placeholders were found, /// `None` if no placeholders exist in the path. +// `resolver` and `resolved` are intentionally distinct nouns; see comment at +// `rewrite_request_line`. +#[allow(clippy::similar_names)] fn rewrite_uri_path( path: &str, resolver: &SecretResolver, @@ -446,6 +454,9 @@ fn rewrite_uri_path( /// /// Uses the placeholder grammar `openshell:resolve:env:[A-Za-z_][A-Za-z0-9_]*` /// to determine placeholder boundaries within concatenated text. +// `resolver` and `resolved` are intentionally distinct nouns; see comment at +// `rewrite_request_line`. +#[allow(clippy::similar_names)] fn rewrite_path_segment( segment: &str, resolver: &SecretResolver, @@ -561,7 +572,7 @@ fn rewrite_uri_query_params( /// /// Returns `Err` if any placeholder is detected but cannot be resolved /// (fail-closed behavior). -pub(crate) fn rewrite_http_header_block( +pub fn rewrite_http_header_block( raw: &[u8], resolver: Option<&SecretResolver>, ) -> Result { @@ -627,22 +638,25 @@ pub(crate) fn rewrite_http_header_block( }) } -pub(crate) fn rewrite_header_line(line: &str, resolver: &SecretResolver) -> String { +pub fn rewrite_header_line(line: &str, resolver: &SecretResolver) -> String { let Some((name, value)) = line.split_once(':') else { return line.to_string(); }; - match resolver.rewrite_header_value(value.trim()) { - Some(rewritten) => format!("{name}: {rewritten}"), - None => line.to_string(), - } + resolver.rewrite_header_value(value.trim()).map_or_else( + || line.to_string(), + |rewritten| format!("{name}: {rewritten}"), + ) } /// Resolve placeholders in a request target (path + query) for OPA evaluation. /// /// Returns the resolved target (real secrets, for upstream) and a redacted /// version (`[CREDENTIAL]` in place of secrets, for OPA input and logs). -pub(crate) fn rewrite_target_for_eval( +// `resolver` and `resolved` are intentionally distinct nouns; see comment at +// `rewrite_request_line`. +#[allow(clippy::similar_names)] +pub fn rewrite_target_for_eval( target: &str, resolver: &SecretResolver, ) -> Result { @@ -695,6 +709,10 @@ pub(crate) fn rewrite_target_for_eval( // --------------------------------------------------------------------------- #[cfg(test)] +#[allow( + clippy::iter_on_single_items, + reason = "Test code: single-key fixtures are clearer as array literals than std::iter::once." +)] mod tests { use super::*; diff --git a/crates/openshell-sandbox/src/ssh.rs b/crates/openshell-sandbox/src/ssh.rs index 4b2cd1572e..9434d0a16b 100644 --- a/crates/openshell-sandbox/src/ssh.rs +++ b/crates/openshell-sandbox/src/ssh.rs @@ -32,14 +32,16 @@ use tracing::warn; /// Perform SSH server initialization: generate a host key, build the config, /// and bind the Unix socket listener. Extracted so that startup errors can be /// forwarded through the readiness channel rather than being silently logged. -async fn ssh_server_init( - listen_path: &Path, - ca_file_paths: &Option<(PathBuf, PathBuf)>, -) -> Result<( +type SshServerInit = ( UnixListener, Arc, Option>, -)> { +); + +fn ssh_server_init( + listen_path: &Path, + ca_file_paths: &Option<(PathBuf, PathBuf)>, +) -> Result { let mut rng = OsRng; let host_key = PrivateKey::random(&mut rng, Algorithm::Ed25519).into_diagnostic()?; @@ -105,7 +107,7 @@ pub async fn run_ssh_server( ca_file_paths: Option<(PathBuf, PathBuf)>, provider_env: HashMap, ) -> Result<()> { - let (listener, config, ca_paths) = match ssh_server_init(&listen_path, &ca_file_paths).await { + let (listener, config, ca_paths) = match ssh_server_init(&listen_path, &ca_file_paths) { Ok(v) => { // Signal that the SSH server has bound the socket and is ready to // accept connections. The parent task awaits this before spawning @@ -265,8 +267,8 @@ impl russh::server::Handler for SshHandler { /// Clean up per-channel state when the channel is closed. /// /// This is the final cleanup and subsumes `channel_eof` — if `channel_close` - /// fires without a preceding `channel_eof`, all resources (pty_master File, - /// input_sender) are dropped here. + /// fires without a preceding `channel_eof`, all resources (`pty_master` File, + /// `input_sender`) are dropped here. async fn channel_close( &mut self, channel: ChannelId, @@ -317,7 +319,9 @@ impl russh::server::Handler for SshHandler { } let host = host_to_connect.to_string(); - let port = port_to_connect as u16; + // SSH protocol port is bounded by u32 but only u16 is meaningful; + // saturate as a guard for malformed clients. + let port = u16::try_from(port_to_connect).unwrap_or(u16::MAX); let netns_fd = self.netns_fd; tokio::spawn(async move { @@ -652,8 +656,10 @@ fn session_user_and_home(policy: &SandboxPolicy) -> (String, String) { let home = nix::unistd::User::from_name(user) .ok() .flatten() - .map(|u| u.dir.to_string_lossy().into_owned()) - .unwrap_or_else(|| format!("/home/{user}")); + .map_or_else( + || format!("/home/{user}"), + |u| u.dir.to_string_lossy().into_owned(), + ); (user.to_string(), home) } _ => ("sandbox".to_string(), "/sandbox".to_string()), @@ -1039,6 +1045,9 @@ mod unsafe_pty { } #[allow(unsafe_code)] + // `libc::TIOCSCTTY` is `u32` on macOS/BSD and `u64` on Linux; allow the + // cross-platform conversion so the same expression compiles everywhere. + #[allow(clippy::useless_conversion)] fn set_controlling_tty(fd: RawFd) -> std::io::Result<()> { let rc = unsafe { libc::ioctl(fd, libc::TIOCSCTTY.into(), 0) }; if rc != 0 { @@ -1182,6 +1191,11 @@ fn is_loopback_host(host: &str) -> bool { } #[cfg(test)] +#[allow( + clippy::doc_markdown, + unsafe_code, + reason = "Test code: doc text references identifiers and uses libc::winsize zero-init." +)] mod tests { use super::*; use std::process::Stdio; @@ -1487,7 +1501,7 @@ mod tests { None, None, // no netns fd #[cfg(target_os = "linux")] - crate::sandbox::linux::prepare( + sandbox::linux::prepare( &SandboxPolicy { version: 0, filesystem: FilesystemPolicy::default(), diff --git a/crates/openshell-sandbox/tests/websocket_upgrade.rs b/crates/openshell-sandbox/tests/websocket_upgrade.rs index 81eb46e1f2..e4cd232ce1 100644 --- a/crates/openshell-sandbox/tests/websocket_upgrade.rs +++ b/crates/openshell-sandbox/tests/websocket_upgrade.rs @@ -1,6 +1,13 @@ // SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 +#![allow( + clippy::match_same_arms, + clippy::cast_possible_truncation, + clippy::doc_markdown, + reason = "Integration test: dual-arm patterns and small-payload casts are idiomatic in test setup." +)] + //! Integration test: WebSocket upgrade through the L7 relay. //! //! Spins up a dummy WebSocket echo server, connects a client through the diff --git a/crates/openshell-server/src/auth.rs b/crates/openshell-server/src/auth.rs index b896d062c6..e6ff2f0d08 100644 --- a/crates/openshell-server/src/auth.rs +++ b/crates/openshell-server/src/auth.rs @@ -96,32 +96,29 @@ async fn auth_connect( let safe_gateway = html_escape(&gateway_display); - match cf_token { - Some(token) => { - let nonce = uuid::Uuid::new_v4().to_string(); - let csp = format!( - "default-src 'none'; script-src 'nonce-{nonce}'; style-src 'unsafe-inline'; connect-src http://127.0.0.1:*" - ); - ( - [(header::CONTENT_SECURITY_POLICY, csp)], - Html(render_connect_page( - &safe_gateway, - params.callback_port, - &token, - ¶ms.code, - &nonce, - )), - ) - .into_response() - } - None => { - let csp = "default-src 'none'; style-src 'unsafe-inline'".to_string(); - ( - [(header::CONTENT_SECURITY_POLICY, csp)], - Html(render_waiting_page(params.callback_port, ¶ms.code)), - ) - .into_response() - } + if let Some(token) = cf_token { + let nonce = uuid::Uuid::new_v4().to_string(); + let csp = format!( + "default-src 'none'; script-src 'nonce-{nonce}'; style-src 'unsafe-inline'; connect-src http://127.0.0.1:*" + ); + ( + [(header::CONTENT_SECURITY_POLICY, csp)], + Html(render_connect_page( + &safe_gateway, + params.callback_port, + &token, + ¶ms.code, + &nonce, + )), + ) + .into_response() + } else { + let csp = "default-src 'none'; style-src 'unsafe-inline'".to_string(); + ( + [(header::CONTENT_SECURITY_POLICY, csp)], + Html(render_waiting_page(params.callback_port, ¶ms.code)), + ) + .into_response() } } diff --git a/crates/openshell-server/src/cli.rs b/crates/openshell-server/src/cli.rs index 2df0b06f4d..d9d81eed77 100644 --- a/crates/openshell-server/src/cli.rs +++ b/crates/openshell-server/src/cli.rs @@ -79,7 +79,7 @@ struct Args { #[arg(long, env = "OPENSHELL_SANDBOX_IMAGE")] sandbox_image: Option, - /// Kubernetes imagePullPolicy for sandbox pods (Always, IfNotPresent, Never). + /// Kubernetes `imagePullPolicy` for sandbox pods (Always, `IfNotPresent`, Never). #[arg(long, env = "OPENSHELL_SANDBOX_IMAGE_PULL_POLICY")] sandbox_image_pull_policy: Option, diff --git a/crates/openshell-server/src/compute/mod.rs b/crates/openshell-server/src/compute/mod.rs index a218d18a8f..df6f88c77b 100644 --- a/crates/openshell-server/src/compute/mod.rs +++ b/crates/openshell-server/src/compute/mod.rs @@ -81,7 +81,7 @@ trait StartupResume: Send + Sync { #[tonic::async_trait] impl StartupResume for DockerComputeDriver { async fn resume_sandbox(&self, sandbox_id: &str, sandbox_name: &str) -> Result { - DockerComputeDriver::resume_sandbox(self, sandbox_id, sandbox_name) + Self::resume_sandbox(self, sandbox_id, sandbox_name) .await .map_err(|err| err.to_string()) } @@ -97,7 +97,7 @@ const ORPHAN_GRACE_PERIOD: Duration = Duration::from_secs(300); pub use openshell_core::ComputeDriverError as ComputeError; #[derive(Debug)] -pub(crate) struct ManagedDriverProcess { +pub struct ManagedDriverProcess { child: std::sync::Mutex>, socket_path: std::path::PathBuf, } @@ -210,9 +210,7 @@ impl ComputeDriver for RemoteComputeDriver { ) -> Result, Status> { let mut client = self.client(); let response = client.watch_sandboxes(request).await?; - let stream = response - .into_inner() - .map(|item| item.map_err(|status| status)); + let stream = response.into_inner(); Ok(tonic::Response::new(Box::pin(stream))) } } @@ -1132,8 +1130,6 @@ fn driver_sandbox_template_from_public(template: &SandboxTemplate) -> DriverSand fn extract_typed_resources( resources: &Option, ) -> Option { - let s = resources.as_ref()?; - fn get_quantity(s: &prost_types::Struct, section: &str, key: &str) -> String { s.fields .get(section) @@ -1148,6 +1144,8 @@ fn extract_typed_resources( .unwrap_or_default() } + let s = resources.as_ref()?; + let req = DriverResourceRequirements { cpu_request: get_quantity(s, "requests", "cpu"), cpu_limit: get_quantity(s, "limits", "cpu"), @@ -1169,7 +1167,7 @@ fn extract_typed_resources( } /// Build the opaque `platform_config` Struct from platform-specific public -/// template fields (runtime_class_name, annotations, volume_claim_templates) +/// template fields (`runtime_class_name`, annotations, `volume_claim_templates`) /// plus any resource fields beyond CPU/memory. fn build_platform_config(template: &SandboxTemplate) -> Option { use prost_types::{Struct, Value, value::Kind}; @@ -1499,7 +1497,7 @@ fn is_terminal_failure_reason(reason: &str) -> bool { #[cfg(test)] #[derive(Debug, Default)] -pub(crate) struct NoopTestDriver; +pub struct NoopTestDriver; #[cfg(test)] #[tonic::async_trait] @@ -1593,7 +1591,7 @@ impl ComputeDriver for NoopTestDriver { } #[cfg(test)] -pub(crate) async fn new_test_runtime(store: Arc) -> ComputeRuntime { +pub async fn new_test_runtime(store: Arc) -> ComputeRuntime { ComputeRuntime { driver: Arc::new(NoopTestDriver), shutdown_cleanup: None, @@ -1617,6 +1615,7 @@ mod tests { CreateSandboxResponse, DeleteSandboxResponse, GetCapabilitiesResponse, GetSandboxRequest, GetSandboxResponse, StopSandboxRequest, StopSandboxResponse, ValidateSandboxCreateResponse, }; + use std::collections::HashMap; use std::sync::Arc; use tokio::sync::{mpsc, oneshot}; @@ -1790,8 +1789,8 @@ mod tests { metadata: Some(openshell_core::proto::datamodel::v1::ObjectMeta { id: id.to_string(), name: name.to_string(), - created_at_ms: 1000000, - labels: std::collections::HashMap::new(), + created_at_ms: 1_000_000, + labels: HashMap::new(), }), phase: phase as i32, ..Default::default() @@ -1803,8 +1802,8 @@ mod tests { metadata: Some(openshell_core::proto::datamodel::v1::ObjectMeta { id: id.to_string(), name: format!("session-{id}"), - created_at_ms: 1000000, - labels: std::collections::HashMap::new(), + created_at_ms: 1_000_000, + labels: HashMap::new(), }), sandbox_id: sandbox_id.to_string(), token: format!("token-{id}"), @@ -2333,7 +2332,6 @@ mod tests { deleting: false, }), }], - ..Default::default() })) .await; @@ -2391,7 +2389,6 @@ mod tests { last_transition_time: String::new(), })), }], - ..Default::default() })) .await; @@ -2524,8 +2521,8 @@ mod tests { #[derive(Default)] struct RecordingResume { - calls: tokio::sync::Mutex>, - results: tokio::sync::Mutex>>, + calls: Mutex>, + results: Mutex>>, } impl RecordingResume { diff --git a/crates/openshell-server/src/compute/vm.rs b/crates/openshell-server/src/compute/vm.rs index 622bc393e5..844b142409 100644 --- a/crates/openshell-server/src/compute/vm.rs +++ b/crates/openshell-server/src/compute/vm.rs @@ -136,10 +136,10 @@ impl Default for VmComputeConfig { #[cfg(unix)] #[derive(Debug, Clone, PartialEq, Eq)] -pub(crate) struct VmGuestTlsPaths { - pub(crate) ca: PathBuf, - pub(crate) cert: PathBuf, - pub(crate) key: PathBuf, +pub struct VmGuestTlsPaths { + pub ca: PathBuf, + pub cert: PathBuf, + pub key: PathBuf, } /// Resolve the `openshell-driver-vm` binary path. @@ -152,7 +152,7 @@ pub(crate) struct VmGuestTlsPaths { /// `/usr/local/libexec`. /// 3. Sibling of the gateway's own executable (last-resort fallback so /// local development builds still work out of the box). -pub(crate) fn resolve_compute_driver_bin(vm_config: &VmComputeConfig) -> Result { +pub fn resolve_compute_driver_bin(vm_config: &VmComputeConfig) -> Result { let mut searched: Vec = Vec::new(); // 1. Configured driver directory, or the conventional install locations @@ -191,11 +191,10 @@ pub(crate) fn resolve_compute_driver_bin(vm_config: &VmComputeConfig) -> Result< } fn resolve_driver_search_dirs(vm_config: &VmComputeConfig) -> Vec { - if let Some(dir) = vm_config.driver_dir.clone() { - vec![dir] - } else { - VmComputeConfig::default_driver_search_dirs(std::env::var_os("HOME").map(PathBuf::from)) - } + vm_config.driver_dir.clone().map_or_else( + || VmComputeConfig::default_driver_search_dirs(std::env::var_os("HOME").map(PathBuf::from)), + |dir| vec![dir], + ) } fn push_unique_path(paths: &mut Vec, path: PathBuf) { @@ -205,12 +204,12 @@ fn push_unique_path(paths: &mut Vec, path: PathBuf) { } /// Path of the Unix domain socket the driver will listen on. -pub(crate) fn compute_driver_socket_path(vm_config: &VmComputeConfig) -> PathBuf { +pub fn compute_driver_socket_path(vm_config: &VmComputeConfig) -> PathBuf { vm_config.state_dir.join("compute-driver.sock") } #[cfg(unix)] -pub(crate) fn compute_driver_guest_tls_paths( +pub fn compute_driver_guest_tls_paths( config: &Config, vm_config: &VmComputeConfig, ) -> Result> { @@ -261,7 +260,7 @@ pub(crate) fn compute_driver_guest_tls_paths( /// and return a gRPC `Channel` connected to it plus a process handle that /// kills the subprocess and removes the socket on drop. #[cfg(unix)] -pub(crate) async fn spawn( +pub async fn spawn( config: &Config, vm_config: &VmComputeConfig, ) -> Result<(Channel, Arc)> { @@ -333,7 +332,7 @@ pub(crate) async fn spawn( } #[cfg(not(unix))] -pub(crate) async fn spawn( +pub async fn spawn( _config: &Config, _vm_config: &VmComputeConfig, ) -> Result<(Channel, std::sync::Arc)> { @@ -349,9 +348,10 @@ async fn wait_for_compute_driver( ) -> Result { let mut last_error: Option = None; for _ in 0..100 { - if let Some(status) = child.try_wait().map_err(|e| { + let try_wait_result = child.try_wait().map_err(|e| { Error::execution(format!("failed to poll vm compute driver process: {e}")) - })? { + })?; + if let Some(status) = try_wait_result { return Err(Error::execution(format!( "vm compute driver exited before becoming ready with status {status}" ))); diff --git a/crates/openshell-server/src/grpc/mod.rs b/crates/openshell-server/src/grpc/mod.rs index 969204dadd..89e639ac96 100644 --- a/crates/openshell-server/src/grpc/mod.rs +++ b/crates/openshell-server/src/grpc/mod.rs @@ -3,7 +3,7 @@ //! gRPC service implementation. -pub(crate) mod policy; +pub mod policy; mod provider; mod sandbox; mod validation; @@ -119,7 +119,9 @@ fn current_time_ms() -> Result { /// /// This is a crate-level helper that wraps the validation module's implementation. /// Use this from modules outside of `grpc` that need to validate metadata. -pub(crate) fn validate_object_metadata( +// `tonic::Status` is large but is the API surface of gRPC handlers. +#[allow(clippy::result_large_err)] +pub fn validate_object_metadata( metadata: Option<&openshell_core::proto::datamodel::v1::ObjectMeta>, resource_type: &str, ) -> Result<(), Status> { diff --git a/crates/openshell-server/src/grpc/policy.rs b/crates/openshell-server/src/grpc/policy.rs index 8a867d2dad..25a4bd17ce 100644 --- a/crates/openshell-server/src/grpc/policy.rs +++ b/crates/openshell-server/src/grpc/policy.rs @@ -65,7 +65,7 @@ use super::{MAX_PAGE_SIZE, StoredSettingValue, StoredSettings, clamp_limit, curr const GLOBAL_SETTINGS_OBJECT_TYPE: &str = "gateway_settings"; const GLOBAL_SETTINGS_NAME: &str = "global"; /// Internal object type for durable sandbox-scoped settings. -pub(crate) const SANDBOX_SETTINGS_OBJECT_TYPE: &str = "sandbox_settings"; +pub const SANDBOX_SETTINGS_OBJECT_TYPE: &str = "sandbox_settings"; /// Reserved settings key used to store global policy payload. const POLICY_SETTING_KEY: &str = "policy"; /// Sentinel `sandbox_id` used to store global policy revisions. @@ -135,10 +135,10 @@ fn summarize_cli_policy_merge_op(operation: &PolicyMergeOp) -> String { rule_name, host, port, - } => match rule_name { - Some(rule_name) => format!("remove-endpoint {host}:{port} from rule {rule_name}"), - None => format!("remove-endpoint {host}:{port}"), - }, + } => rule_name.as_ref().map_or_else( + || format!("remove-endpoint {host}:{port}"), + |rule_name| format!("remove-endpoint {host}:{port} from rule {rule_name}"), + ), PolicyMergeOp::RemoveRule { rule_name } => format!("remove-rule {rule_name}"), PolicyMergeOp::AddDenyRules { host, @@ -2001,15 +2001,15 @@ fn validate_rule_not_always_blocked(rule: &NetworkPolicyRule) -> Result<(), Stat for ep in &rule.endpoints { // Check if the endpoint host is a literal always-blocked IP. - if let Ok(ip) = ep.host.parse::() { - if is_always_blocked_ip(ip) { - return Err(Status::invalid_argument(format!( - "proposed rule endpoint host '{}' is an always-blocked address \ - (loopback/link-local/unspecified); the proxy will deny traffic \ - to this destination regardless of policy", - ep.host - ))); - } + if let Ok(ip) = ep.host.parse::() + && is_always_blocked_ip(ip) + { + return Err(Status::invalid_argument(format!( + "proposed rule endpoint host '{}' is an always-blocked address \ + (loopback/link-local/unspecified); the proxy will deny traffic \ + to this destination regardless of policy", + ep.host + ))); } let host_lc = ep.host.to_lowercase(); if host_lc == "localhost" || host_lc == "localhost." { @@ -2028,14 +2028,14 @@ fn validate_rule_not_always_blocked(rule: &NetworkPolicyRule) -> Result<(), Stat IpAddr::V6(v6) => ipnet::IpNet::V6(ipnet::Ipv6Net::from(v6)), }) }); - if let Ok(net) = parsed { - if is_always_blocked_net(net) { - return Err(Status::invalid_argument(format!( - "proposed rule contains always-blocked allowed_ips entry '{entry}'; \ - SSRF hardening prevents traffic to these destinations \ - regardless of policy" - ))); - } + if let Ok(net) = parsed + && is_always_blocked_net(net) + { + return Err(Status::invalid_argument(format!( + "proposed rule contains always-blocked `allowed_ips` entry '{entry}'; \ + SSRF hardening prevents traffic to these destinations \ + regardless of policy" + ))); } // Invalid entries are not our concern here — the sandbox's // parse_allowed_ips handles syntax validation. @@ -2603,7 +2603,7 @@ mod tests { metadata: Some(openshell_core::proto::datamodel::v1::ObjectMeta { id: "sb-no-policy".to_string(), name: "no-policy-sandbox".to_string(), - created_at_ms: 1000000, + created_at_ms: 1_000_000, labels: std::collections::HashMap::new(), }), spec: Some(SandboxSpec { @@ -2633,7 +2633,7 @@ mod tests { metadata: Some(openshell_core::proto::datamodel::v1::ObjectMeta { id: "sb-backfill".to_string(), name: "backfill-sandbox".to_string(), - created_at_ms: 1000000, + created_at_ms: 1_000_000, labels: std::collections::HashMap::new(), }), spec: Some(SandboxSpec { @@ -3184,7 +3184,7 @@ mod tests { let sandbox_id = "sb-merge"; let initial_policy = SandboxPolicy { - network_policies: [( + network_policies: std::iter::once(( "test_server".to_string(), NetworkPolicyRule { name: "test_server".to_string(), @@ -3198,8 +3198,7 @@ mod tests { ..Default::default() }], }, - )] - .into_iter() + )) .collect(), ..Default::default() }; @@ -3284,7 +3283,7 @@ mod tests { let sandbox_id = "sb-new"; let initial_policy = SandboxPolicy { - network_policies: [( + network_policies: std::iter::once(( "existing_rule".to_string(), NetworkPolicyRule { name: "existing_rule".to_string(), @@ -3298,8 +3297,7 @@ mod tests { ..Default::default() }], }, - )] - .into_iter() + )) .collect(), ..Default::default() }; @@ -3370,7 +3368,7 @@ mod tests { let sandbox_id = "sb-concurrent-merge"; let initial_policy = SandboxPolicy { - network_policies: [( + network_policies: std::iter::once(( "github".to_string(), NetworkPolicyRule { name: "github".to_string(), @@ -3384,8 +3382,7 @@ mod tests { }], ..Default::default() }, - )] - .into_iter() + )) .collect(), ..Default::default() }; @@ -3601,8 +3598,7 @@ mod tests { let encoded = hex::encode(policy.encode_to_vec()); let global = StoredSettings { revision: 1, - settings: [("policy".to_string(), StoredSettingValue::Bytes(encoded))] - .into_iter() + settings: std::iter::once(("policy".to_string(), StoredSettingValue::Bytes(encoded))) .collect(), }; @@ -3751,20 +3747,18 @@ mod tests { fn merge_effective_settings_policy_key_is_excluded() { let global = StoredSettings { revision: 1, - settings: [( + settings: std::iter::once(( "policy".to_string(), StoredSettingValue::Bytes("deadbeef".to_string()), - )] - .into_iter() + )) .collect(), }; let sandbox = StoredSettings { revision: 1, - settings: [( + settings: std::iter::once(( "policy".to_string(), StoredSettingValue::Bytes("cafebabe".to_string()), - )] - .into_iter() + )) .collect(), }; diff --git a/crates/openshell-server/src/grpc/provider.rs b/crates/openshell-server/src/grpc/provider.rs index 2e3876fb89..a9b18b7ebf 100644 --- a/crates/openshell-server/src/grpc/provider.rs +++ b/crates/openshell-server/src/grpc/provider.rs @@ -426,8 +426,8 @@ mod tests { metadata: Some(openshell_core::proto::datamodel::v1::ObjectMeta { id: String::new(), name: "gitlab-local".to_string(), - created_at_ms: 1000000, - labels: std::collections::HashMap::new(), + created_at_ms: 1_000_000, + labels: HashMap::new(), }), r#type: "gitlab".to_string(), credentials: std::iter::once(( @@ -499,7 +499,7 @@ mod tests { metadata: Some(openshell_core::proto::datamodel::v1::ObjectMeta { id: String::new(), name: "bad-provider".to_string(), - created_at_ms: 1000000, + created_at_ms: 1_000_000, labels: HashMap::new(), }), r#type: String::new(), @@ -523,7 +523,7 @@ mod tests { metadata: Some(openshell_core::proto::datamodel::v1::ObjectMeta { id: String::new(), name: "missing".to_string(), - created_at_ms: 1000000, + created_at_ms: 1_000_000, labels: HashMap::new(), }), r#type: String::new(), @@ -551,7 +551,7 @@ mod tests { metadata: Some(openshell_core::proto::datamodel::v1::ObjectMeta { id: String::new(), name: "noop-test".to_string(), - created_at_ms: 1000000, + created_at_ms: 1_000_000, labels: HashMap::new(), }), r#type: String::new(), @@ -614,13 +614,13 @@ mod tests { updated.credentials.get("API_TOKEN"), Some(&"REDACTED".to_string()) ); - assert!(updated.credentials.get("SECONDARY").is_none()); + assert!(!updated.credentials.contains_key("SECONDARY")); assert_eq!(updated.config.len(), 1); assert_eq!( updated.config.get("endpoint"), Some(&"https://example.com".to_string()) ); - assert!(updated.config.get("region").is_none()); + assert!(!updated.config.contains_key("region")); let stored: Provider = store .get_message_by_name("delete-key-test") .await @@ -631,7 +631,7 @@ mod tests { stored.credentials.get("API_TOKEN"), Some(&"token-123".to_string()) ); - assert!(stored.credentials.get("SECONDARY").is_none()); + assert!(!stored.credentials.contains_key("SECONDARY")); } #[tokio::test] @@ -916,7 +916,7 @@ mod tests { metadata: Some(openshell_core::proto::datamodel::v1::ObjectMeta { id: String::new(), name: "my-claude".to_string(), - created_at_ms: 1000000, + created_at_ms: 1_000_000, labels: HashMap::new(), }), r#type: "claude".to_string(), @@ -935,8 +935,8 @@ mod tests { metadata: Some(openshell_core::proto::datamodel::v1::ObjectMeta { id: "sandbox-001".to_string(), name: "test-sandbox".to_string(), - created_at_ms: 1000000, - labels: std::collections::HashMap::new(), + created_at_ms: 1_000_000, + labels: HashMap::new(), }), spec: Some(SandboxSpec { providers: vec!["my-claude".to_string()], @@ -971,8 +971,8 @@ mod tests { metadata: Some(openshell_core::proto::datamodel::v1::ObjectMeta { id: "sandbox-002".to_string(), name: "empty-sandbox".to_string(), - created_at_ms: 1000000, - labels: std::collections::HashMap::new(), + created_at_ms: 1_000_000, + labels: HashMap::new(), }), spec: Some(SandboxSpec::default()), status: None, diff --git a/crates/openshell-server/src/grpc/sandbox.rs b/crates/openshell-server/src/grpc/sandbox.rs index 60bca2a654..ed1b4cdfc4 100644 --- a/crates/openshell-server/src/grpc/sandbox.rs +++ b/crates/openshell-server/src/grpc/sandbox.rs @@ -46,6 +46,8 @@ pub(super) async fn handle_create_sandbox( state: &Arc, request: Request, ) -> Result, Status> { + use crate::persistence::current_time_ms; + let request = request.into_inner(); let spec = request .spec @@ -91,7 +93,6 @@ pub(super) async fn handle_create_sandbox( request.name.clone() }; - use crate::persistence::current_time_ms; let now_ms = current_time_ms() .map_err(|e| Status::internal(format!("failed to get current time: {e}")))?; @@ -160,8 +161,14 @@ pub(super) async fn handle_list_sandboxes( let request = request.into_inner(); let limit = clamp_limit(request.limit, 100, MAX_PAGE_SIZE); - // If label selector is provided, validate and use filtered list - let records = if !request.label_selector.is_empty() { + // If no label selector is provided, use the unfiltered list path + let records = if request.label_selector.is_empty() { + state + .store + .list(Sandbox::object_type(), limit, request.offset) + .await + .map_err(|e| Status::internal(format!("list sandboxes failed: {e}")))? + } else { crate::grpc::validation::validate_label_selector(&request.label_selector)?; state .store @@ -173,12 +180,6 @@ pub(super) async fn handle_list_sandboxes( ) .await .map_err(|e| Status::internal(format!("list sandboxes with selector failed: {e}")))? - } else { - state - .store - .list(Sandbox::object_type(), limit, request.offset) - .await - .map_err(|e| Status::internal(format!("list sandboxes failed: {e}")))? }; let mut sandboxes = Vec::with_capacity(records.len()); @@ -441,6 +442,8 @@ pub(super) async fn handle_exec_sandbox( state: &Arc, request: Request, ) -> Result>>, Status> { + use openshell_core::ObjectId; + let req = request.into_inner(); if req.sandbox_id.is_empty() { return Err(Status::invalid_argument("sandbox_id is required")); @@ -482,7 +485,6 @@ pub(super) async fn handle_exec_sandbox( let timeout_seconds = req.timeout_seconds; let request_tty = req.tty; - use openshell_core::ObjectId; let sandbox_id = sandbox.object_id().to_string(); let (tx, rx) = mpsc::channel::>(256); @@ -679,7 +681,7 @@ const MAX_COMMAND_STRING_LEN: usize = 256 * 1024; // 256 KiB fn build_remote_exec_command(req: &ExecSandboxRequest) -> Result { let mut parts = Vec::new(); let mut env_entries = req.environment.iter().collect::>(); - env_entries.sort_by(|(a, _), (b, _)| a.cmp(b)); + env_entries.sort_by_key(|(a, _)| *a); for (key, value) in env_entries { parts.push(format!("{key}={}", shell_escape(value)?)); } @@ -781,7 +783,7 @@ async fn stream_exec_over_relay( Ok(()) } -/// Create a localhost SSH proxy that bridges to a relay DuplexStream. +/// Create a localhost SSH proxy that bridges to a relay `DuplexStream`. /// /// The proxy forwards raw SSH bytes between the `russh` client and the relay. /// The supervisor bridges the relay to its Unix-socket SSH daemon; filesystem @@ -999,9 +1001,7 @@ mod tests { "-c".to_string(), "print('ok')".to_string(), ], - environment: [("HOME".to_string(), "/home/user".to_string())] - .into_iter() - .collect(), + environment: std::iter::once(("HOME".to_string(), "/home/user".to_string())).collect(), workdir: "/workspace".to_string(), ..Default::default() }; diff --git a/crates/openshell-server/src/grpc/validation.rs b/crates/openshell-server/src/grpc/validation.rs index 5e4a48cb55..160b7e0312 100644 --- a/crates/openshell-server/src/grpc/validation.rs +++ b/crates/openshell-server/src/grpc/validation.rs @@ -241,11 +241,7 @@ pub(super) fn validate_string_map( /// Validate field sizes on a `Provider` before persisting. pub(super) fn validate_provider_fields(provider: &Provider) -> Result<(), Status> { - let name_len = provider - .metadata - .as_ref() - .map(|m| m.name.len()) - .unwrap_or(0); + let name_len = provider.metadata.as_ref().map_or(0, |m| m.name.len()); if name_len > MAX_NAME_LEN { return Err(Status::invalid_argument(format!( "provider.name exceeds maximum length ({name_len} > {MAX_NAME_LEN})" @@ -291,7 +287,7 @@ pub(super) fn validate_provider_fields(provider: &Provider) -> Result<(), Status /// /// Examples: `app`, `kubernetes.io/app`, `example.com/my-label` /// -/// See: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/ +/// See: pub(super) fn validate_label_key(key: &str) -> Result<(), Status> { if key.is_empty() { return Err(Status::invalid_argument("label key cannot be empty")); @@ -401,7 +397,7 @@ pub(super) fn validate_label_key(key: &str) -> Result<(), Status> { /// - If non-empty, must contain only alphanumeric, hyphens, underscores, and dots /// - If non-empty, must start and end with alphanumeric character /// -/// See: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/ +/// See: pub(super) fn validate_label_value(value: &str) -> Result<(), Status> { // Empty values are allowed in Kubernetes if value.is_empty() { @@ -631,8 +627,7 @@ pub(super) fn level_matches(log_level: &str, min_level: &str) -> bool { let to_num = |s: &str| match s.to_uppercase().as_str() { "ERROR" => 0, "WARN" => 1, - "INFO" => 2, - "OCSF" => 2, + "INFO" | "OCSF" => 2, "DEBUG" => 3, "TRACE" => 4, _ => 5, // unknown levels always pass @@ -877,7 +872,7 @@ mod tests { metadata: Some(openshell_core::proto::datamodel::v1::ObjectMeta { id: String::new(), name: name.to_string(), - created_at_ms: 1000000, + created_at_ms: 1_000_000, labels: HashMap::new(), }), r#type: provider_type.to_string(), diff --git a/crates/openshell-server/src/inference.rs b/crates/openshell-server/src/inference.rs index be983667b9..b52700f0d5 100644 --- a/crates/openshell-server/src/inference.rs +++ b/crates/openshell-server/src/inference.rs @@ -1,6 +1,8 @@ // SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 +#![allow(clippy::result_large_err)] // gRPC handlers return Result, Status> + use openshell_core::proto::{ ClusterInferenceConfig, GetClusterInferenceRequest, GetClusterInferenceResponse, GetInferenceBundleRequest, GetInferenceBundleResponse, InferenceRoute, Provider, ResolvedRoute, @@ -390,10 +392,13 @@ async fn resolve_inference_bundle(store: &Store) -> Result { + () = &mut shutdown => { info!("Shutdown signal received; stopping gateway"); break; } @@ -323,8 +322,8 @@ async fn shutdown_signal() { #[cfg(unix)] { tokio::select! { - _ = ctrl_c_signal() => {} - _ = terminate_signal() => {} + () = ctrl_c_signal() => {} + () = terminate_signal() => {} } } @@ -352,6 +351,9 @@ async fn terminate_signal() { let _ = signal.recv().await; } +// Internal wiring helper: each argument is a distinct piece of runtime state +// that must be passed through, so the count is justified. +#[allow(clippy::too_many_arguments)] async fn build_compute_runtime( config: &Config, vm_config: &VmComputeConfig, @@ -421,8 +423,10 @@ async fn build_compute_runtime( let socket_path = std::env::var("OPENSHELL_PODMAN_SOCKET") .ok() .filter(|s| !s.is_empty()) - .map(std::path::PathBuf::from) - .unwrap_or_else(openshell_driver_podman::PodmanComputeConfig::default_socket_path); + .map_or_else( + openshell_driver_podman::PodmanComputeConfig::default_socket_path, + std::path::PathBuf::from, + ); let network_name = std::env::var("OPENSHELL_NETWORK_NAME") .ok() @@ -472,10 +476,12 @@ fn configured_compute_driver(config: &Config) -> Result { [] => Err(Error::config( "at least one compute driver must be configured", )), - [driver @ ComputeDriverKind::Kubernetes] - | [driver @ ComputeDriverKind::Vm] - | [driver @ ComputeDriverKind::Docker] - | [driver @ ComputeDriverKind::Podman] => Ok(*driver), + [ + driver @ (ComputeDriverKind::Kubernetes + | ComputeDriverKind::Vm + | ComputeDriverKind::Docker + | ComputeDriverKind::Podman), + ] => Ok(*driver), drivers => Err(Error::config(format!( "multiple compute drivers are not supported yet; configured drivers: {}", drivers diff --git a/crates/openshell-server/src/persistence/mod.rs b/crates/openshell-server/src/persistence/mod.rs index 80810c1732..1c926bd4a3 100644 --- a/crates/openshell-server/src/persistence/mod.rs +++ b/crates/openshell-server/src/persistence/mod.rs @@ -13,6 +13,7 @@ pub use openshell_core::proto::{ use openshell_core::{Error as CoreError, Result as CoreResult}; use prost::Message; use rand::Rng; +use std::collections::HashMap; use std::time::{SystemTime, UNIX_EPOCH}; use thiserror::Error; @@ -233,7 +234,7 @@ impl Store { ) -> PersistenceResult<()> { // Serialize labels to JSON let labels_map = message.object_labels(); - let labels_json = if labels_map.as_ref().map_or(true, |m| m.is_empty()) { + let labels_json = if labels_map.as_ref().is_none_or(HashMap::is_empty) { None } else { Some(serde_json::to_string(&labels_map).map_err(|e| { @@ -291,14 +292,14 @@ pub fn current_time_ms() -> PersistenceResult { } fn map_db_error(error: &sqlx::Error) -> PersistenceError { - if let sqlx::Error::Database(db) = error { - if db.is_unique_violation() { - let constraint = db - .constraint() - .map(ToString::to_string) - .or_else(|| infer_sqlite_unique_constraint(db.message())); - return PersistenceError::unique_violation(constraint, Some(db.message().to_string())); - } + if let sqlx::Error::Database(db) = error + && db.is_unique_violation() + { + let constraint = db + .constraint() + .map(ToString::to_string) + .or_else(|| infer_sqlite_unique_constraint(db.message())); + return PersistenceError::unique_violation(constraint, Some(db.message().to_string())); } PersistenceError::Database(error.to_string()) } @@ -323,19 +324,17 @@ fn map_migrate_error(error: &sqlx::migrate::MigrateError) -> PersistenceError { /// Parse a simple label selector string into key-value pairs. /// Format: "key1=value1,key2=value2" -/// Returns a HashMap of label requirements. +/// Returns a `HashMap` of label requirements. /// /// Note: Input validation should be performed at the gRPC layer using /// `grpc::validation::validate_label_selector()` before calling this function. /// Errors returned here indicate unexpected internal errors, not user input errors. -pub fn parse_label_selector( - selector: &str, -) -> PersistenceResult> { +pub fn parse_label_selector(selector: &str) -> PersistenceResult> { if selector.is_empty() { - return Ok(std::collections::HashMap::new()); + return Ok(HashMap::new()); } - let mut labels = std::collections::HashMap::new(); + let mut labels = HashMap::new(); for pair in selector.split(',') { let pair = pair.trim(); if pair.is_empty() { diff --git a/crates/openshell-server/src/persistence/postgres.rs b/crates/openshell-server/src/persistence/postgres.rs index 770f3891a0..2cd6a046f1 100644 --- a/crates/openshell-server/src/persistence/postgres.rs +++ b/crates/openshell-server/src/persistence/postgres.rs @@ -50,7 +50,7 @@ impl PostgresStore { ) -> PersistenceResult<()> { let now_ms = current_time_ms()?; let labels_jsonb: Option = labels - .map(|s| serde_json::from_str(s)) + .map(serde_json::from_str) .transpose() .map_err(|e| super::PersistenceError::Encode(format!("invalid labels JSON: {e}")))?; diff --git a/crates/openshell-server/src/persistence/sqlite.rs b/crates/openshell-server/src/persistence/sqlite.rs index 8fad865bb5..fafb075971 100644 --- a/crates/openshell-server/src/persistence/sqlite.rs +++ b/crates/openshell-server/src/persistence/sqlite.rs @@ -204,7 +204,7 @@ LIMIT ?2 OFFSET ?3 required_labels .iter() - .all(|(key, value)| labels.get(key).map(|v| v == value).unwrap_or(false)) + .all(|(key, value)| labels.get(key).is_some_and(|v| v == value)) }) .skip(offset as usize) .take(limit as usize) diff --git a/crates/openshell-server/src/persistence/tests.rs b/crates/openshell-server/src/persistence/tests.rs index 6ae002e27a..bef95d4b65 100644 --- a/crates/openshell-server/src/persistence/tests.rs +++ b/crates/openshell-server/src/persistence/tests.rs @@ -743,14 +743,14 @@ fn parse_label_selector_accepts_empty_value() { // Kubernetes allows empty label values, so selectors should accept "key=" format let result = super::parse_label_selector("env=").unwrap(); assert_eq!(result.len(), 1); - assert_eq!(result.get("env"), Some(&"".to_string())); + assert_eq!(result.get("env"), Some(&String::new())); } #[test] fn parse_label_selector_multiple_with_empty_value() { let result = super::parse_label_selector("env=,tier=frontend").unwrap(); assert_eq!(result.len(), 2); - assert_eq!(result.get("env"), Some(&"".to_string())); + assert_eq!(result.get("env"), Some(&String::new())); assert_eq!(result.get("tier"), Some(&"frontend".to_string())); } diff --git a/crates/openshell-server/src/policy_store.rs b/crates/openshell-server/src/policy_store.rs index dffa00ca4c..f0a43698e8 100644 --- a/crates/openshell-server/src/policy_store.rs +++ b/crates/openshell-server/src/policy_store.rs @@ -8,7 +8,7 @@ use openshell_core::proto::{ }; use prost::Message; -pub(crate) trait PolicyStoreExt { +pub trait PolicyStoreExt { async fn put_policy_revision( &self, id: &str, @@ -257,7 +257,7 @@ impl PolicyStoreExt for Store { } } -pub(crate) fn policy_payload_from_record(record: &PolicyRecord) -> PersistenceResult> { +pub fn policy_payload_from_record(record: &PolicyRecord) -> PersistenceResult> { let policy = ProtoSandboxPolicy::decode(record.policy_payload.as_slice()).map_err(|e| { crate::persistence::PersistenceError::Decode(format!("decode policy payload failed: {e}")) })?; @@ -270,7 +270,7 @@ pub(crate) fn policy_payload_from_record(record: &PolicyRecord) -> PersistenceRe .encode_to_vec()) } -pub(crate) fn policy_record_from_parts( +pub fn policy_record_from_parts( id: String, sandbox_id: String, version: i64, @@ -301,9 +301,7 @@ pub(crate) fn policy_record_from_parts( }) } -pub(crate) fn draft_chunk_payload_from_record( - chunk: &DraftChunkRecord, -) -> PersistenceResult> { +pub fn draft_chunk_payload_from_record(chunk: &DraftChunkRecord) -> PersistenceResult> { let proposed_rule = if chunk.proposed_rule.is_empty() { None } else { @@ -320,6 +318,7 @@ pub(crate) fn draft_chunk_payload_from_record( proposed_rule, rationale: chunk.rationale.clone(), security_notes: chunk.security_notes.clone(), + #[allow(clippy::cast_possible_truncation)] // f64->f32 for confidence scores confidence: chunk.confidence as f32, decided_at_ms: chunk.decided_at_ms.unwrap_or(0), host: chunk.host.clone(), @@ -330,7 +329,7 @@ pub(crate) fn draft_chunk_payload_from_record( .encode_to_vec()) } -pub(crate) fn draft_chunk_record_from_parts( +pub fn draft_chunk_record_from_parts( id: String, sandbox_id: String, status: String, diff --git a/crates/openshell-server/src/sandbox_watch.rs b/crates/openshell-server/src/sandbox_watch.rs index 73cc4bf266..ac38eba8db 100644 --- a/crates/openshell-server/src/sandbox_watch.rs +++ b/crates/openshell-server/src/sandbox_watch.rs @@ -59,6 +59,16 @@ impl SandboxWatchBus { } } +/// Helper to translate broadcast lag into a gRPC status. +pub fn broadcast_to_status(err: broadcast::error::RecvError) -> Status { + match err { + broadcast::error::RecvError::Closed => Status::cancelled("stream closed"), + broadcast::error::RecvError::Lagged(n) => { + Status::resource_exhausted(format!("watch stream lagged; dropped {n} messages")) + } + } +} + #[cfg(test)] mod tests { use super::*; @@ -105,13 +115,3 @@ mod tests { bus.remove("nonexistent"); } } - -/// Helper to translate broadcast lag into a gRPC status. -pub fn broadcast_to_status(err: broadcast::error::RecvError) -> Status { - match err { - broadcast::error::RecvError::Closed => Status::cancelled("stream closed"), - broadcast::error::RecvError::Lagged(n) => { - Status::resource_exhausted(format!("watch stream lagged; dropped {n} messages")) - } - } -} diff --git a/crates/openshell-server/src/ssh_tunnel.rs b/crates/openshell-server/src/ssh_tunnel.rs index 6b0232fa00..bd317d53fa 100644 --- a/crates/openshell-server/src/ssh_tunnel.rs +++ b/crates/openshell-server/src/ssh_tunnel.rs @@ -106,10 +106,13 @@ async fn ssh_connect( // Check token expiry (0 means no expiry for backward compatibility). if session.expires_at_ms > 0 { - let now_ms = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap_or_default() - .as_millis() as i64; + let now_ms = i64::try_from( + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap_or_default() + .as_millis(), + ) + .unwrap_or(i64::MAX); if now_ms > session.expires_at_ms { return StatusCode::UNAUTHORIZED.into_response(); } @@ -268,10 +271,13 @@ pub fn spawn_session_reaper(store: Arc, interval: Duration) { } async fn reap_expired_sessions(store: &Store) -> Result<(), String> { - let now_ms = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap_or_default() - .as_millis() as i64; + let now_ms = i64::try_from( + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap_or_default() + .as_millis(), + ) + .unwrap_or(i64::MAX); let records = store .list(SshSession::object_type(), 1000, 0) @@ -321,7 +327,7 @@ mod tests { SshSession { metadata: Some(openshell_core::proto::datamodel::v1::ObjectMeta { id: id.to_string(), - name: format!("session-{}", id), + name: format!("session-{id}"), created_at_ms: 1000, labels: HashMap::new(), }), @@ -333,10 +339,13 @@ mod tests { } fn now_ms() -> i64 { - std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap() - .as_millis() as i64 + i64::try_from( + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_millis(), + ) + .unwrap_or(i64::MAX) } // ---- Connection limit tests ---- diff --git a/crates/openshell-server/src/supervisor_session.rs b/crates/openshell-server/src/supervisor_session.rs index cd250459a4..94c352ba53 100644 --- a/crates/openshell-server/src/supervisor_session.rs +++ b/crates/openshell-server/src/supervisor_session.rs @@ -70,9 +70,9 @@ impl openshell_driver_docker::SupervisorReadiness for SupervisorSessionRegistry /// Registry of active supervisor sessions and pending relay channels. #[derive(Default)] pub struct SupervisorSessionRegistry { - /// sandbox_id -> live session handle. + /// `sandbox_id` -> live session handle. sessions: Mutex>, - /// channel_id -> oneshot sender for the reverse CONNECT stream. + /// `channel_id` -> oneshot sender for the reverse CONNECT stream. pending_relays: Mutex>, } @@ -287,10 +287,12 @@ impl SupervisorSessionRegistry { Ok((channel_id, relay_rx)) } - /// Claim a pending relay channel. Called by the /relay/{channel_id} HTTP handler + /// Claim a pending relay channel. Called by the `/relay/{channel_id}` HTTP handler /// when the supervisor's reverse CONNECT arrives. /// - /// Returns the DuplexStream half that the supervisor side should read/write. + /// Returns the `DuplexStream` half that the supervisor side should read/write. + // `tonic::Status` is large but is the API surface of gRPC handlers. + #[allow(clippy::result_large_err)] pub fn claim_relay(&self, channel_id: &str) -> Result { let pending = { let mut map = self.pending_relays.lock().unwrap(); @@ -377,15 +379,16 @@ async fn require_persisted_sandbox( // RelayStream gRPC handler // --------------------------------------------------------------------------- -/// Size of chunks read from the gateway-side DuplexStream when forwarding +/// Size of chunks read from the gateway-side `DuplexStream` when forwarding /// bytes back to the supervisor over the gRPC response stream. const RELAY_STREAM_CHUNK_SIZE: usize = 16 * 1024; -/// Handle a RelayStream RPC from a supervisor. The first inbound `RelayFrame` -/// must carry a `RelayInit` identifying the pending relay; subsequent frames -/// carry raw bytes forward to the gateway-side waiter. Bytes flowing the other -/// way are chunked and sent as `RelayFrame::data` messages back over the -/// response stream. +/// Handle a `RelayStream` RPC from a supervisor. +/// +/// The first inbound `RelayFrame` must carry a `RelayInit` identifying the +/// pending relay; subsequent frames carry raw bytes forward to the +/// gateway-side waiter. Bytes flowing the other way are chunked and sent as +/// `RelayFrame::data` messages back over the response stream. pub async fn handle_relay_stream( registry: &SupervisorSessionRegistry, request: Request>, @@ -456,7 +459,7 @@ pub async fn handle_relay_stream( // Gateway → supervisor: read the DuplexStream and emit RelayFrame::data messages. let (out_tx, out_rx) = mpsc::channel::>(16); - let channel_id_out = channel_id.clone(); + let channel_id_out = channel_id; tokio::spawn(async move { let mut buf = vec![0u8; RELAY_STREAM_CHUNK_SIZE]; loop { @@ -735,8 +738,8 @@ mod tests { metadata: Some(openshell_core::proto::datamodel::v1::ObjectMeta { id: id.to_string(), name: name.to_string(), - created_at_ms: 1000000, - labels: std::collections::HashMap::new(), + created_at_ms: 1_000_000, + labels: HashMap::new(), }), ..Default::default() } @@ -1002,6 +1005,8 @@ mod tests { #[tokio::test] async fn open_relay_uses_newest_session_after_supersede() { + use tokio::sync::mpsc::error::TryRecvError; + let registry = SupervisorSessionRegistry::new(); let (tx_old, mut rx_old) = mpsc::channel::(4); let (tx_new, mut rx_new) = mpsc::channel(4); @@ -1040,7 +1045,6 @@ mod tests { // The old session must have received no messages — the channel is // still open but empty. - use tokio::sync::mpsc::error::TryRecvError; match rx_old.try_recv() { Err(TryRecvError::Empty) => {} other => panic!("expected Empty on superseded session, got {other:?}"), @@ -1191,7 +1195,9 @@ mod tests { PendingRelay { sender: relay_tx, sandbox_id: "sbx-test".to_string(), - created_at: Instant::now() - Duration::from_secs(60), + created_at: Instant::now() + .checked_sub(Duration::from_secs(60)) + .expect("test instant subtraction underflow"), }, ); @@ -1269,7 +1275,9 @@ mod tests { PendingRelay { sender: relay_tx, sandbox_id: "sbx-test".to_string(), - created_at: Instant::now() - Duration::from_secs(60), + created_at: Instant::now() + .checked_sub(Duration::from_secs(60)) + .expect("test instant subtraction underflow"), }, ); diff --git a/crates/openshell-server/tests/edge_tunnel_auth.rs b/crates/openshell-server/tests/edge_tunnel_auth.rs index e8c7e00388..15df2f9d80 100644 --- a/crates/openshell-server/tests/edge_tunnel_auth.rs +++ b/crates/openshell-server/tests/edge_tunnel_auth.rs @@ -12,7 +12,7 @@ //! //! Test matrix: //! -//! | allow_unauthenticated | client cert | bearer auth header | expected | +//! | `allow_unauthenticated` | client cert | bearer auth header | expected | //! |-----------------------|-------------|--------------------|----------| //! | false | valid | — | OK | //! | false | none | — | rejected | @@ -69,7 +69,7 @@ fn install_rustls_provider() { let _ = rustls::crypto::ring::default_provider().install_default(); } -/// Minimal OpenShell implementation for testing. +/// Minimal `OpenShell` implementation for testing. #[derive(Clone, Default)] struct TestOpenShell; @@ -317,15 +317,13 @@ impl OpenShell for TestOpenShell { Err(Status::unimplemented("not implemented in test")) } - type RelayStreamStream = tokio_stream::wrappers::ReceiverStream< - Result, - >; + type RelayStreamStream = ReceiverStream>; async fn relay_stream( &self, _request: tonic::Request>, - ) -> Result, tonic::Status> { - Err(tonic::Status::unimplemented("not implemented in test")) + ) -> Result, Status> { + Err(Status::unimplemented("not implemented in test")) } } @@ -575,7 +573,7 @@ fn https_client_no_cert( // Tests // =========================================================================== -/// Baseline: with allow_unauthenticated=false (default), mTLS connections work. +/// Baseline: with `allow_unauthenticated=false` (default), mTLS connections work. #[tokio::test] async fn baseline_mtls_works_with_mandatory_client_certs() { install_rustls_provider(); @@ -615,7 +613,7 @@ async fn baseline_mtls_works_with_mandatory_client_certs() { server.abort(); } -/// Baseline: with allow_unauthenticated=false, no-client-cert connections are +/// Baseline: with `allow_unauthenticated=false`, no-client-cert connections are /// rejected at the TLS layer. #[tokio::test] async fn baseline_no_cert_rejected_with_mandatory_mtls() { @@ -655,7 +653,7 @@ async fn baseline_no_cert_rejected_with_mandatory_mtls() { server.abort(); } -/// With allow_unauthenticated=true, mTLS connections still work (dual-auth). +/// With `allow_unauthenticated=true`, mTLS connections still work (dual-auth). #[tokio::test] async fn dual_auth_mtls_still_accepted() { install_rustls_provider(); @@ -695,7 +693,7 @@ async fn dual_auth_mtls_still_accepted() { server.abort(); } -/// With allow_unauthenticated=true, no-client-cert connections pass the TLS +/// With `allow_unauthenticated=true`, no-client-cert connections pass the TLS /// handshake. This simulates Cloudflare Tunnel re-originating a connection. /// /// The gRPC health check succeeds because there is no auth middleware yet — @@ -777,7 +775,7 @@ async fn tunnel_mode_cf_authorization_header_reaches_server() { server.abort(); } -/// With allow_unauthenticated=true, a client cert from a rogue CA is still +/// With `allow_unauthenticated=true`, a client cert from a rogue CA is still /// rejected by the TLS layer — the verifier still validates presented certs. #[tokio::test] async fn tunnel_mode_rogue_cert_still_rejected() { diff --git a/crates/openshell-server/tests/multiplex_integration.rs b/crates/openshell-server/tests/multiplex_integration.rs index 561ea2ba72..dd14c63ec1 100644 --- a/crates/openshell-server/tests/multiplex_integration.rs +++ b/crates/openshell-server/tests/multiplex_integration.rs @@ -285,15 +285,13 @@ impl OpenShell for TestOpenShell { Err(Status::unimplemented("not implemented in test")) } - type RelayStreamStream = tokio_stream::wrappers::ReceiverStream< - Result, - >; + type RelayStreamStream = ReceiverStream>; async fn relay_stream( &self, _request: tonic::Request>, - ) -> Result, tonic::Status> { - Err(tonic::Status::unimplemented("not implemented in test")) + ) -> Result, Status> { + Err(Status::unimplemented("not implemented in test")) } } diff --git a/crates/openshell-server/tests/multiplex_tls_integration.rs b/crates/openshell-server/tests/multiplex_tls_integration.rs index dc51e61182..83ba769886 100644 --- a/crates/openshell-server/tests/multiplex_tls_integration.rs +++ b/crates/openshell-server/tests/multiplex_tls_integration.rs @@ -298,15 +298,13 @@ impl OpenShell for TestOpenShell { Err(Status::unimplemented("not implemented in test")) } - type RelayStreamStream = tokio_stream::wrappers::ReceiverStream< - Result, - >; + type RelayStreamStream = ReceiverStream>; async fn relay_stream( &self, _request: tonic::Request>, - ) -> Result, tonic::Status> { - Err(tonic::Status::unimplemented("not implemented in test")) + ) -> Result, Status> { + Err(Status::unimplemented("not implemented in test")) } } diff --git a/crates/openshell-server/tests/ws_tunnel_integration.rs b/crates/openshell-server/tests/ws_tunnel_integration.rs index 584f092818..949c0200a9 100644 --- a/crates/openshell-server/tests/ws_tunnel_integration.rs +++ b/crates/openshell-server/tests/ws_tunnel_integration.rs @@ -311,15 +311,13 @@ impl OpenShell for TestOpenShell { Err(Status::unimplemented("not implemented in test")) } - type RelayStreamStream = tokio_stream::wrappers::ReceiverStream< - Result, - >; + type RelayStreamStream = ReceiverStream>; async fn relay_stream( &self, _request: tonic::Request>, - ) -> Result, tonic::Status> { - Err(tonic::Status::unimplemented("not implemented in test")) + ) -> Result, Status> { + Err(Status::unimplemented("not implemented in test")) } } @@ -440,9 +438,8 @@ where break; } } - Ok(Message::Close(_)) => break, + Ok(Message::Close(_)) | Err(_) => break, Ok(Message::Ping(_) | Message::Pong(_)) => {} - Err(_) => break, } } let _ = writer.shutdown().await; diff --git a/crates/openshell-tui/src/app.rs b/crates/openshell-tui/src/app.rs index f397b1809a..1cab7127c2 100644 --- a/crates/openshell-tui/src/app.rs +++ b/crates/openshell-tui/src/app.rs @@ -359,7 +359,7 @@ pub struct CreateProviderForm { pub is_generic: bool, /// Status message (errors, validation). pub status: Option, - /// Warning shown at top of EnterKey modal (e.g. autodetect failure). + /// Warning shown at top of `EnterKey` modal (e.g. autodetect failure). pub warning: Option, /// Animation start time. pub anim_start: Option, @@ -578,6 +578,7 @@ pub fn format_labels(labels: &HashMap) -> String { } impl App { + #[allow(clippy::large_types_passed_by_value)] // Theme is Copy; one-shot ctor pub fn new( client: OpenShellClient, gateway_name: String, @@ -719,18 +720,18 @@ impl App { self.sandbox_settings = settings::REGISTERED_SETTINGS .iter() .map(|reg| { - let (value, scope) = settings - .get(reg.key) - .map(|es| { - let v = es.value.as_ref().and_then(|sv| sv.value.clone()); - let s = match es.scope { - 1 => SettingScope::Sandbox, - 2 => SettingScope::Global, - _ => SettingScope::Unset, - }; - (v, s) - }) - .unwrap_or((None, SettingScope::Unset)); + let (value, scope) = + settings + .get(reg.key) + .map_or((None, SettingScope::Unset), |es| { + let v = es.value.as_ref().and_then(|sv| sv.value.clone()); + let s = match es.scope { + 1 => SettingScope::Sandbox, + 2 => SettingScope::Global, + _ => SettingScope::Unset, + }; + (v, s) + }); SandboxSettingEntry { key: reg.key.to_string(), kind: reg.kind, @@ -858,11 +859,8 @@ impl App { self.input_mode = InputMode::Command; self.command_input.clear(); } - KeyCode::Char('j') | KeyCode::Down => { - if !self.gateways.is_empty() { - self.gateway_selected = - (self.gateway_selected + 1).min(self.gateways.len() - 1); - } + KeyCode::Char('j') | KeyCode::Down if !self.gateways.is_empty() => { + self.gateway_selected = (self.gateway_selected + 1).min(self.gateways.len() - 1); } KeyCode::Char('k') | KeyCode::Up => { self.gateway_selected = self.gateway_selected.saturating_sub(1); @@ -902,11 +900,8 @@ impl App { self.input_mode = InputMode::Command; self.command_input.clear(); } - KeyCode::Char('j') | KeyCode::Down => { - if self.provider_count > 0 { - self.provider_selected = - (self.provider_selected + 1).min(self.provider_count - 1); - } + KeyCode::Char('j') | KeyCode::Down if self.provider_count > 0 => { + self.provider_selected = (self.provider_selected + 1).min(self.provider_count - 1); } KeyCode::Char('k') | KeyCode::Up => { self.provider_selected = self.provider_selected.saturating_sub(1); @@ -914,22 +909,16 @@ impl App { KeyCode::Char('c') => { self.open_create_provider_form(); } - KeyCode::Enter => { - // Fetch and show provider detail. - if self.provider_count > 0 { - self.pending_provider_get = true; - } + // Fetch and show provider detail. + KeyCode::Enter if self.provider_count > 0 => { + self.pending_provider_get = true; } - KeyCode::Char('u') => { - // Open update form for the selected provider. - if self.provider_count > 0 { - self.open_update_provider_form(); - } + // Open update form for the selected provider. + KeyCode::Char('u') if self.provider_count > 0 => { + self.open_update_provider_form(); } - KeyCode::Char('d') => { - if self.provider_count > 0 { - self.confirm_provider_delete = true; - } + KeyCode::Char('d') if self.provider_count > 0 => { + self.confirm_provider_delete = true; } KeyCode::Char('h' | 'l') | KeyCode::Left | KeyCode::Right => { self.middle_pane_tab = self.middle_pane_tab.next(); @@ -947,11 +936,9 @@ impl App { self.input_mode = InputMode::Command; self.command_input.clear(); } - KeyCode::Char('j') | KeyCode::Down => { - if !self.global_settings.is_empty() { - self.global_settings_selected = - (self.global_settings_selected + 1).min(self.global_settings.len() - 1); - } + KeyCode::Char('j') | KeyCode::Down if !self.global_settings.is_empty() => { + self.global_settings_selected = + (self.global_settings_selected + 1).min(self.global_settings.len() - 1); } KeyCode::Char('k') | KeyCode::Up => { self.global_settings_selected = self.global_settings_selected.saturating_sub(1); @@ -1082,10 +1069,8 @@ impl App { self.input_mode = InputMode::Command; self.command_input.clear(); } - KeyCode::Char('j') | KeyCode::Down => { - if self.sandbox_count > 0 { - self.sandbox_selected = (self.sandbox_selected + 1).min(self.sandbox_count - 1); - } + KeyCode::Char('j') | KeyCode::Down if self.sandbox_count > 0 => { + self.sandbox_selected = (self.sandbox_selected + 1).min(self.sandbox_count - 1); } KeyCode::Char('k') | KeyCode::Up => { self.sandbox_selected = self.sandbox_selected.saturating_sub(1); @@ -1093,13 +1078,11 @@ impl App { KeyCode::Char('c') => { self.open_create_form(); } - KeyCode::Enter => { - if self.sandbox_count > 0 { - self.screen = Screen::Sandbox; - self.focus = Focus::SandboxPolicy; - self.confirm_delete = false; - self.pending_sandbox_detail = true; - } + KeyCode::Enter if self.sandbox_count > 0 => { + self.screen = Screen::Sandbox; + self.focus = Focus::SandboxPolicy; + self.confirm_delete = false; + self.pending_sandbox_detail = true; } KeyCode::Esc => { self.focus = Focus::Providers; @@ -1150,10 +1133,8 @@ impl App { KeyCode::Char('r') => { self.focus = Focus::SandboxDraft; } - KeyCode::Char('s') => { - if self.sandbox_count > 0 { - self.pending_shell_connect = true; - } + KeyCode::Char('s') if self.sandbox_count > 0 => { + self.pending_shell_connect = true; } KeyCode::Char('d') => { self.confirm_delete = true; @@ -1195,11 +1176,9 @@ impl App { // In policy tab, 'l' opens logs. In settings tab, switch tab. self.sandbox_policy_tab = self.sandbox_policy_tab.next(); } - KeyCode::Char('j') | KeyCode::Down => { - if !self.sandbox_settings.is_empty() { - self.sandbox_settings_selected = - (self.sandbox_settings_selected + 1).min(self.sandbox_settings.len() - 1); - } + KeyCode::Char('j') | KeyCode::Down if !self.sandbox_settings.is_empty() => { + self.sandbox_settings_selected = + (self.sandbox_settings_selected + 1).min(self.sandbox_settings.len() - 1); } KeyCode::Char('k') | KeyCode::Up => { self.sandbox_settings_selected = self.sandbox_settings_selected.saturating_sub(1); @@ -1404,10 +1383,8 @@ impl App { self.focus = Focus::SandboxLogs; self.pending_log_fetch = true; } - KeyCode::Enter => { - if !self.draft_chunks.is_empty() { - self.draft_detail_open = true; - } + KeyCode::Enter if !self.draft_chunks.is_empty() => { + self.draft_detail_open = true; } KeyCode::Char('j') | KeyCode::Down => { if total == 0 { @@ -1435,13 +1412,11 @@ impl App { self.draft_scroll = 0; self.draft_selected = 0; } - KeyCode::Char('G') => { - if total > 0 { - let max_scroll = total.saturating_sub(vh.min(total)); - self.draft_scroll = max_scroll; - let visible = total.saturating_sub(self.draft_scroll).min(vh); - self.draft_selected = visible.saturating_sub(1); - } + KeyCode::Char('G') if total > 0 => { + let max_scroll = total.saturating_sub(vh.min(total)); + self.draft_scroll = max_scroll; + let visible = total.saturating_sub(self.draft_scroll).min(vh); + self.draft_selected = visible.saturating_sub(1); } // Approve selected chunk (pending → approved, rejected → approved). KeyCode::Char('a') => { @@ -1586,12 +1561,10 @@ impl App { self.log_autoscroll = false; } } - KeyCode::Enter => { - if filtered_len > 0 && self.log_selection_anchor.is_none() { - let abs = self.sandbox_log_scroll + self.log_cursor; - if abs < filtered_len { - self.log_detail_index = Some(abs); - } + KeyCode::Enter if filtered_len > 0 && self.log_selection_anchor.is_none() => { + let abs = self.sandbox_log_scroll + self.log_cursor; + if abs < filtered_len { + self.log_detail_index = Some(abs); } } KeyCode::Char('j') | KeyCode::Down => { @@ -1655,7 +1628,8 @@ impl App { self.sandbox_log_scroll = self.sandbox_log_scroll.saturating_sub(delta.unsigned_abs()); self.log_autoscroll = false; } else { - self.sandbox_log_scroll = (self.sandbox_log_scroll + delta as usize).min(max_scroll); + self.sandbox_log_scroll = + (self.sandbox_log_scroll + delta.cast_unsigned()).min(max_scroll); } let visible = filtered_len .saturating_sub(self.sandbox_log_scroll) @@ -1723,11 +1697,9 @@ impl App { CreateFormField::Image => Self::handle_text_input(&mut form.image, key), CreateFormField::Command => Self::handle_text_input(&mut form.command, key), CreateFormField::Providers => match key.code { - KeyCode::Char('j') | KeyCode::Down => { - if !form.providers.is_empty() { - form.provider_cursor = - (form.provider_cursor + 1).min(form.providers.len() - 1); - } + KeyCode::Char('j') | KeyCode::Down if !form.providers.is_empty() => { + form.provider_cursor = + (form.provider_cursor + 1).min(form.providers.len() - 1); } KeyCode::Char('k') | KeyCode::Up => { form.provider_cursor = form.provider_cursor.saturating_sub(1); @@ -1826,10 +1798,8 @@ impl App { KeyCode::Esc => { self.create_provider_form = None; } - KeyCode::Char('j') | KeyCode::Down => { - if !form.types.is_empty() { - form.type_cursor = (form.type_cursor + 1).min(form.types.len() - 1); - } + KeyCode::Char('j') | KeyCode::Down if !form.types.is_empty() => { + form.type_cursor = (form.type_cursor + 1).min(form.types.len() - 1); } KeyCode::Char('k') | KeyCode::Up => { form.type_cursor = form.type_cursor.saturating_sub(1); @@ -1870,7 +1840,7 @@ impl App { form.status = None; form.warning = None; } - KeyCode::Char('j') | KeyCode::Down | KeyCode::Char('k') | KeyCode::Up => { + KeyCode::Char('j' | 'k') | KeyCode::Down | KeyCode::Up => { form.method_cursor = 1 - form.method_cursor; } KeyCode::Enter => { @@ -2076,7 +2046,7 @@ impl App { registry .credential_env_vars(&ptype) .first() - .map_or(String::new(), |s| s.to_string()) + .map_or(String::new(), ToString::to_string) } else { cred_key }; diff --git a/crates/openshell-tui/src/event.rs b/crates/openshell-tui/src/event.rs index e73862eb87..4c6eeb8b46 100644 --- a/crates/openshell-tui/src/event.rs +++ b/crates/openshell-tui/src/event.rs @@ -56,7 +56,7 @@ pub enum Event { pub struct EventHandler { rx: mpsc::UnboundedReceiver, // Kept alive so the spawned task's `tx` doesn't see a closed channel. - _keepalive: mpsc::UnboundedSender, + keepalive: mpsc::UnboundedSender, /// When true, the background reader stops polling stdin. paused: Arc, } @@ -85,20 +85,14 @@ impl EventHandler { if event::poll(poll_interval).unwrap_or(false) { match event::read() { - Ok(TermEvent::Key(key)) => { - if tx.send(Event::Key(key)).is_err() { - return; - } + Ok(TermEvent::Key(key)) if tx.send(Event::Key(key)).is_err() => { + return; } - Ok(TermEvent::Mouse(mouse)) => { - if tx.send(Event::Mouse(mouse)).is_err() { - return; - } + Ok(TermEvent::Mouse(mouse)) if tx.send(Event::Mouse(mouse)).is_err() => { + return; } - Ok(TermEvent::Resize(w, h)) => { - if tx.send(Event::Resize(w, h)).is_err() { - return; - } + Ok(TermEvent::Resize(w, h)) if tx.send(Event::Resize(w, h)).is_err() => { + return; } _ => {} } @@ -113,7 +107,7 @@ impl EventHandler { Self { rx, - _keepalive: keepalive, + keepalive, paused, } } @@ -124,7 +118,7 @@ impl EventHandler { /// Get a sender handle for dispatching events from background tasks. pub fn sender(&self) -> mpsc::UnboundedSender { - self._keepalive.clone() + self.keepalive.clone() } /// Pause stdin polling (call before suspending TUI for a child process). diff --git a/crates/openshell-tui/src/lib.rs b/crates/openshell-tui/src/lib.rs index 0a8caf6758..8571ebbe1e 100644 --- a/crates/openshell-tui/src/lib.rs +++ b/crates/openshell-tui/src/lib.rs @@ -7,6 +7,7 @@ mod event; pub mod theme; mod ui; +use std::collections::HashMap; use std::io; use std::path::PathBuf; use std::time::Duration; @@ -33,9 +34,9 @@ const SPLASH_DURATION: Duration = Duration::from_secs(3); // Re-export for use by the CLI crate. pub use theme::ThemeMode; -/// Launch the OpenShell TUI. +/// Launch the `OpenShell` TUI. /// -/// `channel` must be a connected gRPC channel to the OpenShell gateway. +/// `channel` must be a connected gRPC channel to the `OpenShell` gateway. /// `theme_mode` selects the color theme: `Auto` detects the terminal /// background, `Dark`/`Light` forces a specific palette. pub async fn run( @@ -188,11 +189,11 @@ pub async fn run( .next() .cloned() .unwrap_or_default(); - let masked = if let Some(val) = provider.credentials.values().next() { - mask_secret(val) - } else { - "-".to_string() - }; + let masked = provider + .credentials + .values() + .next() + .map_or_else(|| "-".to_string(), |val| mask_secret(val)); app.provider_detail = Some(app::ProviderDetailView { name: provider.object_name().to_string(), provider_type: provider.r#type.clone(), @@ -313,12 +314,11 @@ pub async fn run( }, Some(Event::Tick) => { // Auto-dismiss splash after SPLASH_DURATION. - if app.screen == Screen::Splash { - if let Some(start) = app.splash_start { - if start.elapsed() >= SPLASH_DURATION { - app.dismiss_splash(); - } - } + if app.screen == Screen::Splash + && let Some(start) = app.splash_start + && start.elapsed() >= SPLASH_DURATION + { + app.dismiss_splash(); } refresh_gateway_list(&mut app); @@ -338,90 +338,90 @@ pub async fn run( } Some(Event::Redraw) => { // Check if a buffered sandbox CreateResult is ready to finalize. - if let Some(form) = app.create_form.as_ref() { - if form.create_result.is_some() { - let elapsed = form - .anim_start - .map_or(app::MIN_CREATING_DISPLAY, |s| s.elapsed()); - if elapsed >= app::MIN_CREATING_DISPLAY { - let result = app - .create_form - .as_mut() - .and_then(|f| f.create_result.take()); - if let Some(h) = app.anim_handle.take() { - h.abort(); - } - match result { - Some(Ok(name)) => { - app.create_form = None; - let ports = std::mem::take(&mut app.pending_forward_ports); - let command = std::mem::take(&mut app.pending_exec_command); - let port_info = if ports.is_empty() { - String::new() - } else { - let list = ports - .iter() - .map(|p| p.to_string()) - .collect::>() - .join(", "); - format!(" (forwarding port(s) {list})") - }; - app.status_text = format!("Created sandbox: {name}{port_info}"); - refresh_sandboxes(&mut app).await; - - // If a command was specified, suspend TUI and exec it. - if !command.is_empty() { - handle_exec_command( - &mut app, - &mut terminal, - &events, - &name, - &command, - ) - .await; - } + if let Some(form) = app.create_form.as_ref() + && form.create_result.is_some() + { + let elapsed = form + .anim_start + .map_or(app::MIN_CREATING_DISPLAY, |s| s.elapsed()); + if elapsed >= app::MIN_CREATING_DISPLAY { + let result = app + .create_form + .as_mut() + .and_then(|f| f.create_result.take()); + if let Some(h) = app.anim_handle.take() { + h.abort(); + } + match result { + Some(Ok(name)) => { + app.create_form = None; + let ports = std::mem::take(&mut app.pending_forward_ports); + let command = std::mem::take(&mut app.pending_exec_command); + let port_info = if ports.is_empty() { + String::new() + } else { + let list = ports + .iter() + .map(ToString::to_string) + .collect::>() + .join(", "); + format!(" (forwarding port(s) {list})") + }; + app.status_text = format!("Created sandbox: {name}{port_info}"); + refresh_sandboxes(&mut app).await; + + // If a command was specified, suspend TUI and exec it. + if !command.is_empty() { + handle_exec_command( + &mut app, + &mut terminal, + &events, + &name, + &command, + ) + .await; } - Some(Err(msg)) => { - if let Some(form) = app.create_form.as_mut() { - form.phase = app::CreatePhase::Form; - form.anim_start = None; - form.status = Some(format!("Create failed: {msg}")); - } + } + Some(Err(msg)) => { + if let Some(form) = app.create_form.as_mut() { + form.phase = app::CreatePhase::Form; + form.anim_start = None; + form.status = Some(format!("Create failed: {msg}")); } - None => {} } + None => {} } } } // Check if a buffered provider CreateResult is ready to finalize. - if let Some(form) = app.create_provider_form.as_ref() { - if form.create_result.is_some() { - let elapsed = form - .anim_start - .map_or(app::MIN_CREATING_DISPLAY, |s| s.elapsed()); - if elapsed >= app::MIN_CREATING_DISPLAY { - let result = app - .create_provider_form - .as_mut() - .and_then(|f| f.create_result.take()); - if let Some(h) = app.anim_handle.take() { - h.abort(); + if let Some(form) = app.create_provider_form.as_ref() + && form.create_result.is_some() + { + let elapsed = form + .anim_start + .map_or(app::MIN_CREATING_DISPLAY, |s| s.elapsed()); + if elapsed >= app::MIN_CREATING_DISPLAY { + let result = app + .create_provider_form + .as_mut() + .and_then(|f| f.create_result.take()); + if let Some(h) = app.anim_handle.take() { + h.abort(); + } + match result { + Some(Ok(name)) => { + app.create_provider_form = None; + app.status_text = format!("Created provider: {name}"); + refresh_providers(&mut app).await; } - match result { - Some(Ok(name)) => { - app.create_provider_form = None; - app.status_text = format!("Created provider: {name}"); - refresh_providers(&mut app).await; - } - Some(Err(msg)) => { - if let Some(form) = app.create_provider_form.as_mut() { - form.phase = app::CreateProviderPhase::EnterKey; - form.anim_start = None; - form.status = Some(format!("Create failed: {msg}")); - } + Some(Err(msg)) => { + if let Some(form) = app.create_provider_form.as_mut() { + form.phase = app::CreateProviderPhase::EnterKey; + form.anim_start = None; + form.status = Some(format!("Create failed: {msg}")); } - None => {} } + None => {} } } } @@ -594,7 +594,7 @@ fn spawn_log_stream(app: &mut App, tx: mpsc::UnboundedSender) { source: String::new(), target: String::new(), message: format!("Failed to fetch logs: {}", e.message()), - fields: Default::default(), + fields: HashMap::default(), }])); return; } @@ -605,7 +605,7 @@ fn spawn_log_stream(app: &mut App, tx: mpsc::UnboundedSender) { source: String::new(), target: String::new(), message: "Timed out fetching logs.".into(), - fields: Default::default(), + fields: HashMap::default(), }])); return; } @@ -621,24 +621,20 @@ fn spawn_log_stream(app: &mut App, tx: mpsc::UnboundedSender) { ..Default::default() }; - let resp = - match tokio::time::timeout(Duration::from_secs(5), client.watch_sandbox(req)).await { - Ok(Ok(r)) => r, - Ok(Err(_)) | Err(_) => return, // Silently stop — user can re-enter logs. - }; + // Silently stop — user can re-enter logs. + let Ok(Ok(resp)) = + tokio::time::timeout(Duration::from_secs(5), client.watch_sandbox(req)).await + else { + return; + }; let mut stream = resp.into_inner(); - loop { - match stream.message().await { - Ok(Some(event)) => { - if let Some(openshell_core::proto::sandbox_stream_event::Payload::Log(log)) = - event.payload - { - let line = proto_to_log_line(log); - let _ = tx.send(Event::LogLines(vec![line])); - } - } - _ => break, // Stream ended or error. + while let Ok(Some(event)) = stream.message().await { + if let Some(openshell_core::proto::sandbox_stream_event::Payload::Log(log)) = + event.payload + { + let line = proto_to_log_line(log); + let _ = tx.send(Event::LogLines(vec![line])); } } }); @@ -717,7 +713,7 @@ async fn fetch_sandbox_detail(app: &mut App) { Ok(Ok(resp)) => { if let Some(sandbox) = resp.into_inner().sandbox { if let Some(spec) = &sandbox.spec { - app.sandbox_providers_list = spec.providers.clone(); + app.sandbox_providers_list.clone_from(&spec.providers); } let id = sandbox.object_id().to_string(); if id.is_empty() { None } else { Some(id) } @@ -796,13 +792,14 @@ async fn handle_shell_connect( name: sandbox_name.clone(), }; match tokio::time::timeout(Duration::from_secs(5), app.client.get_sandbox(req)).await { - Ok(Ok(resp)) => match resp.into_inner().sandbox { - Some(s) => s.object_id().to_string(), - None => { + Ok(Ok(resp)) => { + if let Some(s) = resp.into_inner().sandbox { + s.object_id().to_string() + } else { app.status_text = "sandbox not found".to_string(); return; } - }, + } Ok(Err(e)) => { app.status_text = format!("failed to get sandbox: {}", e.message()); return; @@ -945,13 +942,14 @@ async fn handle_exec_command( name: sandbox_name.to_string(), }; match tokio::time::timeout(Duration::from_secs(5), app.client.get_sandbox(req)).await { - Ok(Ok(resp)) => match resp.into_inner().sandbox { - Some(s) => s.object_id().to_string(), - None => { + Ok(Ok(resp)) => { + if let Some(s) = resp.into_inner().sandbox { + s.object_id().to_string() + } else { app.status_text = format!("exec: sandbox {sandbox_name} not found"); return; } - }, + } Ok(Err(e)) => { app.status_text = format!("exec: failed to get sandbox: {}", e.message()); return; @@ -1015,7 +1013,7 @@ async fn handle_exec_command( // remote shell parses it correctly. let command_str = command .split_whitespace() - .map(|word| shell_escape(word)) + .map(shell_escape) .collect::>() .join(" "); let mut ssh = std::process::Command::new("ssh"); @@ -1207,7 +1205,7 @@ fn render_policy_lines( }; lines.push(Line::from(vec![ Span::styled(" Allow: ", t.muted), - Span::styled(format!("{:<6} {}", method, target), t.text), + Span::styled(format!("{method:<6} {target}"), t.text), ])); } } @@ -1275,7 +1273,7 @@ fn spawn_create_sandbox(app: &mut App, tx: mpsc::UnboundedSender) { // Stash command so we can exec after sandbox creation + Ready. app.pending_exec_command = command; // Stash ports so we can include them in the status text. - app.pending_forward_ports = ports.clone(); + app.pending_forward_ports.clone_from(&ports); let endpoint = app.endpoint.clone(); let gateway_name = app.gateway_name.clone(); @@ -1311,7 +1309,7 @@ fn spawn_create_sandbox(app: &mut App, tx: mpsc::UnboundedSender) { policy, ..Default::default() }), - labels: std::collections::HashMap::new(), + labels: HashMap::new(), }; let sandbox_name = @@ -1353,21 +1351,19 @@ fn spawn_create_sandbox(app: &mut App, tx: mpsc::UnboundedSender) { let req = openshell_core::proto::GetSandboxRequest { name: sandbox_name.clone(), }; - match client.get_sandbox(req).await { - Ok(resp) => { - if let Some(sandbox) = resp.into_inner().sandbox { - if sandbox.phase == 2 { - break sandbox.object_id().to_string(); - } - if sandbox.phase == 3 { - let _ = tx.send(Event::CreateResult(Err( - "sandbox entered error state".to_string(), - ))); - return; - } - } + // Retry on transient errors. + if let Ok(resp) = client.get_sandbox(req).await + && let Some(sandbox) = resp.into_inner().sandbox + { + if sandbox.phase == 2 { + break sandbox.object_id().to_string(); + } + if sandbox.phase == 3 { + let _ = tx.send(Event::CreateResult(Err( + "sandbox entered error state".to_string() + ))); + return; } - Err(_) => {} // Retry on transient errors. } }; @@ -1568,11 +1564,11 @@ fn spawn_create_provider(app: &App, tx: mpsc::UnboundedSender) { id: String::new(), name: provider_name.clone(), created_at_ms: 0, - labels: std::collections::HashMap::new(), + labels: HashMap::new(), }), r#type: ptype.clone(), credentials: credentials.clone(), - config: Default::default(), + config: HashMap::default(), }), }; @@ -1649,7 +1645,7 @@ fn spawn_update_provider(app: &App, tx: mpsc::UnboundedSender) { let new_value = form.new_value.clone(); tokio::spawn(async move { - let mut credentials = std::collections::HashMap::new(); + let mut credentials = HashMap::new(); credentials.insert(cred_key, new_value); let req = openshell_core::proto::UpdateProviderRequest { @@ -1658,11 +1654,11 @@ fn spawn_update_provider(app: &App, tx: mpsc::UnboundedSender) { id: String::new(), name: name.clone(), created_at_ms: 0, - labels: std::collections::HashMap::new(), + labels: HashMap::new(), }), r#type: ptype, credentials, - config: Default::default(), + config: HashMap::default(), }), }; @@ -1970,25 +1966,25 @@ fn spawn_set_global_setting(app: &App, tx: mpsc::UnboundedSender) { let value = match kind { openshell_core::settings::SettingValueKind::Bool => { - match openshell_core::settings::parse_bool_like(&raw) { - Some(v) => setting_value::Value::BoolValue(v), - None => { - let _ = tx.send(Event::GlobalSettingSetResult(Err(format!( - "invalid bool value: {raw}" - )))); - return; - } + if let Some(v) = openshell_core::settings::parse_bool_like(&raw) { + setting_value::Value::BoolValue(v) + } else { + let _ = tx.send(Event::GlobalSettingSetResult(Err(format!( + "invalid bool value: {raw}" + )))); + return; } } - openshell_core::settings::SettingValueKind::Int => match raw.parse::() { - Ok(v) => setting_value::Value::IntValue(v), - Err(_) => { + openshell_core::settings::SettingValueKind::Int => { + if let Ok(v) = raw.parse::() { + setting_value::Value::IntValue(v) + } else { let _ = tx.send(Event::GlobalSettingSetResult(Err(format!( "invalid int value: {raw}" )))); return; } - }, + } openshell_core::settings::SettingValueKind::String => { setting_value::Value::StringValue(raw) } @@ -2074,25 +2070,25 @@ fn spawn_set_sandbox_setting(app: &App, tx: mpsc::UnboundedSender) { let value = match kind { openshell_core::settings::SettingValueKind::Bool => { - match openshell_core::settings::parse_bool_like(&raw) { - Some(v) => setting_value::Value::BoolValue(v), - None => { - let _ = tx.send(Event::SandboxSettingSetResult(Err(format!( - "invalid bool value: {raw}" - )))); - return; - } + if let Some(v) = openshell_core::settings::parse_bool_like(&raw) { + setting_value::Value::BoolValue(v) + } else { + let _ = tx.send(Event::SandboxSettingSetResult(Err(format!( + "invalid bool value: {raw}" + )))); + return; } } - openshell_core::settings::SettingValueKind::Int => match raw.parse::() { - Ok(v) => setting_value::Value::IntValue(v), - Err(_) => { + openshell_core::settings::SettingValueKind::Int => { + if let Ok(v) = raw.parse::() { + setting_value::Value::IntValue(v) + } else { let _ = tx.send(Event::SandboxSettingSetResult(Err(format!( "invalid int value: {raw}" )))); return; } - }, + } openshell_core::settings::SettingValueKind::String => { setting_value::Value::StringValue(raw) } @@ -2225,8 +2221,7 @@ async fn refresh_sandboxes(app: &mut App) { .map(|s| { s.metadata .as_ref() - .map(|m| format_age(m.created_at_ms)) - .unwrap_or_else(|| "?".to_string()) + .map_or_else(|| "?".to_string(), |m| format_age(m.created_at_ms)) }) .collect(); app.sandbox_created = sandboxes @@ -2234,8 +2229,7 @@ async fn refresh_sandboxes(app: &mut App) { .map(|s| { s.metadata .as_ref() - .map(|m| format_timestamp(m.created_at_ms)) - .unwrap_or_else(|| "?".to_string()) + .map_or_else(|| "?".to_string(), |m| format_timestamp(m.created_at_ms)) }) .collect(); @@ -2258,7 +2252,7 @@ async fn refresh_sandboxes(app: &mut App) { .map(|s| { s.object_labels() .as_ref() - .map(|labels| app::format_labels(labels)) + .map(app::format_labels) .unwrap_or_default() }) .collect(); diff --git a/crates/openshell-tui/src/theme.rs b/crates/openshell-tui/src/theme.rs index 1be2023671..0c0d9b37c3 100644 --- a/crates/openshell-tui/src/theme.rs +++ b/crates/openshell-tui/src/theme.rs @@ -213,7 +213,7 @@ pub fn detect(mode: ThemeMode) -> Theme { /// /// Uses `terminal-colorsaurus` to send an OSC 11 query to the terminal, /// which returns the actual background RGB color. This works reliably on -/// iTerm2, Terminal.app, WezTerm, Alacritty, and most modern terminals. +/// iTerm2, Terminal.app, `WezTerm`, Alacritty, and most modern terminals. /// /// Falls back to `false` (dark) if the terminal doesn't respond to the /// query (e.g. `TERM=dumb`, piped output, very old terminals). diff --git a/crates/openshell-tui/src/ui/create_provider.rs b/crates/openshell-tui/src/ui/create_provider.rs index c099ea6a57..9f1cc6d832 100644 --- a/crates/openshell-tui/src/ui/create_provider.rs +++ b/crates/openshell-tui/src/ui/create_provider.rs @@ -313,7 +313,6 @@ fn draw_enter_key( chunks[idx], t, ); - idx += 1; } else { // Credential rows — env var name + masked value on the same line. let max_name_len = form @@ -330,7 +329,7 @@ fn draw_enter_key( .map(|(i, (env_name, value))| { let is_focused = form.key_field == ProviderKeyField::Credential && i == form.cred_cursor; - let padded = format!("{:width$}", env_name, width = max_name_len); + let padded = format!("{env_name:max_name_len$}"); let name_style = if is_focused { t.accent_bold } else { t.text }; let mut spans = vec![Span::styled(format!(" {padded}: "), name_style)]; if value.is_empty() { @@ -353,8 +352,8 @@ fn draw_enter_key( }) .collect(); frame.render_widget(Paragraph::new(lines), chunks[idx]); - idx += 1; } + idx += 1; // Spacer. idx += 1; diff --git a/crates/openshell-tui/src/ui/create_sandbox.rs b/crates/openshell-tui/src/ui/create_sandbox.rs index c8411c6184..cb90e244de 100644 --- a/crates/openshell-tui/src/ui/create_sandbox.rs +++ b/crates/openshell-tui/src/ui/create_sandbox.rs @@ -304,7 +304,7 @@ pub fn render_chase( } let frame = (elapsed_ms / 140) as usize; - let mouth_open = frame % 2 == 0; + let mouth_open = frame.is_multiple_of(2); // Characters. let pac = if mouth_open { "ᗧ" } else { "●" }; @@ -360,16 +360,14 @@ pub fn render_chase( let mut current_style = buf[0].1; for &(ch, style) in &buf { - if style == current_style { - current_str.push(ch); - } else { + if style != current_style { if !current_str.is_empty() { spans.push(Span::styled(current_str.clone(), current_style)); current_str.clear(); } current_style = style; - current_str.push(ch); } + current_str.push(ch); } if !current_str.is_empty() { spans.push(Span::styled(current_str, current_style)); diff --git a/crates/openshell-tui/src/ui/global_settings.rs b/crates/openshell-tui/src/ui/global_settings.rs index cac59b0a9f..9e47fd8d62 100644 --- a/crates/openshell-tui/src/ui/global_settings.rs +++ b/crates/openshell-tui/src/ui/global_settings.rs @@ -155,7 +155,7 @@ fn draw_edit_overlay( ])); // content lines + 2 for border - let popup_height = (lines.len() + 2) as u16; + let popup_height = u16::try_from(lines.len() + 2).unwrap_or(u16::MAX); let popup = centered_rect(50, popup_height, area); frame.render_widget(Clear, popup); @@ -239,7 +239,7 @@ fn draw_confirm_delete(frame: &mut Frame<'_>, app: &App, idx: usize, area: Rect) ]; // content lines + 2 for border - let popup_height = (lines.len() + 2) as u16; + let popup_height = u16::try_from(lines.len() + 2).unwrap_or(u16::MAX); let popup = centered_rect(60, popup_height, area); frame.render_widget(Clear, popup); diff --git a/crates/openshell-tui/src/ui/mod.rs b/crates/openshell-tui/src/ui/mod.rs index b920d9cb69..13ac94c10a 100644 --- a/crates/openshell-tui/src/ui/mod.rs +++ b/crates/openshell-tui/src/ui/mod.rs @@ -1,17 +1,17 @@ // SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 -pub(crate) mod create_provider; -pub(crate) mod create_sandbox; +pub mod create_provider; +pub mod create_sandbox; mod dashboard; -pub(crate) mod global_settings; -pub(crate) mod providers; -pub(crate) mod sandbox_detail; +pub mod global_settings; +pub mod providers; +pub mod sandbox_detail; mod sandbox_draft; -pub(crate) mod sandbox_logs; +pub mod sandbox_logs; mod sandbox_policy; -pub(crate) mod sandbox_settings; -pub(crate) mod sandboxes; +pub mod sandbox_settings; +pub mod sandboxes; mod splash; use ratatui::Frame; @@ -89,12 +89,12 @@ fn draw_sandbox_screen(frame: &mut Frame<'_>, app: &mut App, area: Rect) { } // Log detail popup renders over the full frame (not constrained to pane). - if app.focus == Focus::SandboxLogs { - if let Some(detail_idx) = app.log_detail_index { - let filtered: Vec<&app::LogLine> = app.filtered_log_lines(); - if let Some(log) = filtered.get(detail_idx) { - sandbox_logs::draw_detail_popup(frame, log, frame.size(), &app.theme); - } + if app.focus == Focus::SandboxLogs + && let Some(detail_idx) = app.log_detail_index + { + let filtered: Vec<&app::LogLine> = app.filtered_log_lines(); + if let Some(log) = filtered.get(detail_idx) { + sandbox_logs::draw_detail_popup(frame, log, frame.size(), &app.theme); } } @@ -328,8 +328,7 @@ fn draw_nav_bar(frame: &mut Frame<'_>, app: &App, area: Rect) { let selected_status = app .draft_chunks .get(app.draft_scroll + app.draft_selected) - .map(|c| c.status.as_str()) - .unwrap_or(""); + .map_or("", |c| c.status.as_str()); let mut spans = vec![ Span::styled(" ", t.text), Span::styled("[j/k]", t.key_hint), @@ -456,7 +455,7 @@ fn draw_command_bar(frame: &mut Frame<'_>, app: &App, area: Rect) { /// Center a popup rectangle within `area` using percentage-based width and /// an absolute height (in rows). -pub(crate) fn centered_popup(percent_x: u16, height: u16, area: Rect) -> Rect { +pub fn centered_popup(percent_x: u16, height: u16, area: Rect) -> Rect { let vert = Layout::default() .direction(Direction::Vertical) .constraints([ diff --git a/crates/openshell-tui/src/ui/sandbox_detail.rs b/crates/openshell-tui/src/ui/sandbox_detail.rs index 7ab725e9dd..7cdbec8bd0 100644 --- a/crates/openshell-tui/src/ui/sandbox_detail.rs +++ b/crates/openshell-tui/src/ui/sandbox_detail.rs @@ -78,8 +78,7 @@ pub fn draw(frame: &mut Frame<'_>, app: &App, area: Rect) { .sandbox_labels .get(idx) .filter(|s| !s.is_empty()) - .map(String::as_str) - .unwrap_or("none"); + .map_or("none", String::as_str); let row3 = Line::from(vec![ Span::styled(" Labels: ", t.muted), Span::styled(labels_str, t.text), @@ -101,8 +100,7 @@ pub fn draw(frame: &mut Frame<'_>, app: &App, area: Rect) { .sandbox_notes .get(idx) .filter(|s| !s.is_empty()) - .map(String::as_str) - .unwrap_or("none"); + .map_or("none", String::as_str); let row5 = Line::from(vec![ Span::styled(" Forwards: ", t.muted), Span::styled(forwards_str, t.text), diff --git a/crates/openshell-tui/src/ui/sandbox_draft.rs b/crates/openshell-tui/src/ui/sandbox_draft.rs index 528d1c6086..a9cd6b0c90 100644 --- a/crates/openshell-tui/src/ui/sandbox_draft.rs +++ b/crates/openshell-tui/src/ui/sandbox_draft.rs @@ -324,7 +324,7 @@ pub fn draw_approve_all_popup( let count = chunks.len(); // Height: header(1) + blank(1) + chunks(count, capped at 12) + blank(1) + hints(1) + borders(2) + padding(1) let list_lines = count.min(12); - let popup_height = (7 + list_lines) as u16; + let popup_height = u16::try_from(7 + list_lines).unwrap_or(u16::MAX); let popup_height = popup_height.min(area.height.saturating_sub(4)); let popup_width = (area.width * 4 / 5).min(area.width.saturating_sub(4)); let popup_area = centered_rect(popup_width, popup_height, area); diff --git a/crates/openshell-tui/src/ui/sandbox_logs.rs b/crates/openshell-tui/src/ui/sandbox_logs.rs index 2aa793d20e..da20b40c88 100644 --- a/crates/openshell-tui/src/ui/sandbox_logs.rs +++ b/crates/openshell-tui/src/ui/sandbox_logs.rs @@ -335,7 +335,7 @@ const CONNECT_FIELD_ORDER: &[&str] = &[ "reason", ]; -/// Priority field order for L7_REQUEST log lines. +/// Priority field order for `L7_REQUEST` log lines. const L7_FIELD_ORDER: &[&str] = &[ "l7_action", "l7_target", @@ -348,7 +348,7 @@ const L7_FIELD_ORDER: &[&str] = &[ ]; /// Return fields in a smart order based on the log message type. -pub(crate) fn ordered_fields<'a>(log: &'a LogLine) -> Vec<(&'a str, &'a str)> { +pub fn ordered_fields(log: &LogLine) -> Vec<(&str, &str)> { // Matches both "CONNECT" (L4-only decision) and "CONNECT_L7" (tunnel lifecycle for L7 endpoints) let order: Option<&[&str]> = if log.message.starts_with("CONNECT") { Some(CONNECT_FIELD_ORDER) @@ -358,8 +358,18 @@ pub(crate) fn ordered_fields<'a>(log: &'a LogLine) -> Vec<(&'a str, &'a str)> { None }; - match order { - Some(priority) => { + order.map_or_else( + || { + // Default: alphabetical. + let mut pairs: Vec<(&str, &str)> = log + .fields + .iter() + .map(|(k, v)| (k.as_str(), v.as_str())) + .collect(); + pairs.sort_by_key(|(k, _)| *k); + pairs + }, + |priority| { let mut result: Vec<(&str, &str)> = Vec::with_capacity(log.fields.len()); // Add priority fields first (in order). for &key in priority { @@ -377,18 +387,8 @@ pub(crate) fn ordered_fields<'a>(log: &'a LogLine) -> Vec<(&'a str, &'a str)> { remaining.sort_by_key(|(k, _)| *k); result.extend(remaining); result - } - None => { - // Default: alphabetical. - let mut pairs: Vec<(&str, &str)> = log - .fields - .iter() - .map(|(k, v)| (k.as_str(), v.as_str())) - .collect(); - pairs.sort_by_key(|(k, _)| *k); - pairs - } - } + }, + ) } // --------------------------------------------------------------------------- @@ -404,7 +404,7 @@ fn level_style(level: &str, t: &crate::theme::Theme) -> ratatui::style::Style { } } -pub(crate) fn format_short_time(epoch_ms: i64) -> String { +pub fn format_short_time(epoch_ms: i64) -> String { if epoch_ms <= 0 { return String::from("--:--:--"); } @@ -420,7 +420,7 @@ pub(crate) fn format_short_time(epoch_ms: i64) -> String { /// /// Produces the same layout as `render_log_line()` but without styles or /// truncation: `HH:MM:SS {source:<7} {level:<5} {message} [key=value ...]` -pub(crate) fn format_log_line_plain(log: &LogLine) -> String { +pub fn format_log_line_plain(log: &LogLine) -> String { let ts = format_short_time(log.timestamp_ms); let mut s = format!("{ts} {:<7} {:<5} {}", log.source, log.level, log.message); diff --git a/crates/openshell-tui/src/ui/sandbox_settings.rs b/crates/openshell-tui/src/ui/sandbox_settings.rs index c26f4a6632..8ac5eb687e 100644 --- a/crates/openshell-tui/src/ui/sandbox_settings.rs +++ b/crates/openshell-tui/src/ui/sandbox_settings.rs @@ -163,7 +163,7 @@ fn draw_edit_overlay( Span::styled(" Cancel", t.muted), ])); - let popup_height = (lines.len() + 2) as u16; + let popup_height = u16::try_from(lines.len() + 2).unwrap_or(u16::MAX); let popup = centered_rect(50, popup_height, area); frame.render_widget(Clear, popup); @@ -207,7 +207,7 @@ fn draw_confirm_set(frame: &mut Frame<'_>, app: &App, idx: usize, area: Rect) { ]), ]; - let popup_height = (lines.len() + 2) as u16; + let popup_height = u16::try_from(lines.len() + 2).unwrap_or(u16::MAX); let popup = centered_rect(60, popup_height, area); frame.render_widget(Clear, popup); @@ -245,7 +245,7 @@ fn draw_confirm_delete(frame: &mut Frame<'_>, app: &App, idx: usize, area: Rect) ]), ]; - let popup_height = (lines.len() + 2) as u16; + let popup_height = u16::try_from(lines.len() + 2).unwrap_or(u16::MAX); let popup = centered_rect(55, popup_height, area); frame.render_widget(Clear, popup); diff --git a/crates/openshell-vfio/src/lib.rs b/crates/openshell-vfio/src/lib.rs index e7276b3018..bc226a78c4 100644 --- a/crates/openshell-vfio/src/lib.rs +++ b/crates/openshell-vfio/src/lib.rs @@ -591,7 +591,8 @@ fn bind_device_to_vfio(sysfs: &SysfsRoot, bdf: &str) -> Result bdf: bdf.to_string(), reason: format!( "after drivers_probe with {}ms polling, driver is {:?} instead of vfio-pci", - VFIO_BIND_MAX_POLL_ATTEMPTS as u64 * VFIO_BIND_POLL_INTERVAL.as_millis() as u64, + u64::from(VFIO_BIND_MAX_POLL_ATTEMPTS) + * u64::try_from(VFIO_BIND_POLL_INTERVAL.as_millis()).unwrap_or(u64::MAX), current_driver_name(sysfs, bdf) .as_deref() .unwrap_or("") diff --git a/crates/openshell-vm/build.rs b/crates/openshell-vm/build.rs index 33fab9a78c..6351be6e8a 100644 --- a/crates/openshell-vm/build.rs +++ b/crates/openshell-vm/build.rs @@ -12,7 +12,7 @@ //! Environment: //! `OPENSHELL_VM_RUNTIME_COMPRESSED_DIR` - Path to compressed artifacts -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::{env, fs}; fn main() { @@ -97,7 +97,7 @@ fn main() { e ) }); - let size = fs::metadata(&dst_path).map(|m| m.len()).unwrap_or(0); + let size = fs::metadata(&dst_path).map_or(0, |m| m.len()); println!("cargo:warning=Embedded {src_name}: {size} bytes"); } else { println!( @@ -116,7 +116,7 @@ fn main() { /// Generate stub (empty) resource files so the build can complete. /// The embedded module will fail at runtime if these stubs are used. -fn generate_stub_resources(out_dir: &PathBuf) { +fn generate_stub_resources(out_dir: &Path) { let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default(); let (libkrun_name, libkrunfw_name) = match target_os.as_str() { diff --git a/crates/openshell-vm/src/embedded.rs b/crates/openshell-vm/src/embedded.rs index 731f34b10e..537e7d7257 100644 --- a/crates/openshell-vm/src/embedded.rs +++ b/crates/openshell-vm/src/embedded.rs @@ -229,7 +229,8 @@ pub fn cleanup_old_rootfs() -> Result<(), VmError> { } let current_version_dir = base.join(VERSION); - cleanup_old_versions_in_base(&base, ¤t_version_dir) + cleanup_old_versions_in_base(&base, ¤t_version_dir); + Ok(()) } /// Check if the rootfs is embedded (non-empty). @@ -257,12 +258,13 @@ fn runtime_cache_key() -> String { let sample = &chunk[..chunk.len().min(64)]; let mut word: u64 = 0; for (j, &b) in sample.iter().enumerate() { - word ^= (b as u64) << ((j % 8) * 8); + word ^= u64::from(b) << ((j % 8) * 8); } // Mix in resource index so identical resources don't cancel out. - fp ^= word.rotate_left((i as u32) * 13 + 7); + let i_u32 = u32::try_from(i).unwrap_or(u32::MAX); + fp ^= word.rotate_left(i_u32 * 13 + 7); // Also mix in the total length so size changes are detected. - fp ^= (chunk.len() as u64).rotate_left((i as u32) * 17 + 3); + fp ^= (chunk.len() as u64).rotate_left(i_u32 * 17 + 3); } format!("{VERSION}-{fp:016x}") } @@ -286,17 +288,17 @@ fn rootfs_cache_base() -> Result { } fn cleanup_old_versions(current_dir: &Path) -> Result<(), VmError> { - cleanup_old_versions_in_base(&runtime_cache_base()?, current_dir) + cleanup_old_versions_in_base(&runtime_cache_base()?, current_dir); + Ok(()) } -fn cleanup_old_versions_in_base(base: &Path, current_dir: &Path) -> Result<(), VmError> { +fn cleanup_old_versions_in_base(base: &Path, current_dir: &Path) { if !base.exists() { - return Ok(()); + return; } - let entries = match fs::read_dir(base) { - Ok(e) => e, - Err(_) => return Ok(()), // Can't read, skip cleanup + let Ok(entries) = fs::read_dir(base) else { + return; // Can't read, skip cleanup }; for entry in entries.filter_map(Result::ok) { @@ -316,8 +318,6 @@ fn cleanup_old_versions_in_base(base: &Path, current_dir: &Path) -> Result<(), V } } } - - Ok(()) } fn extract_resource(compressed: &[u8], dest: &Path) -> Result<(), VmError> { @@ -417,7 +417,7 @@ fn validate_runtime_dir(dir: &Path) -> Result<(), VmError> { } // Check file is not empty (would indicate a stub) - let size = fs::metadata(path).map(|m| m.len()).unwrap_or(0); + let size = fs::metadata(path).map_or(0, |m| m.len()); if size == 0 { return Err(VmError::HostSetup(format!( "runtime file is empty (stub): {}", diff --git a/crates/openshell-vm/src/exec.rs b/crates/openshell-vm/src/exec.rs index f3198a6bef..63771509e4 100644 --- a/crates/openshell-vm/src/exec.rs +++ b/crates/openshell-vm/src/exec.rs @@ -111,10 +111,10 @@ impl RawModeGuard { let stdin = std::io::stdin(); let fd = stdin.as_fd(); let original = - termios::tcgetattr(&fd).map_err(|e| VmError::Exec(format!("tcgetattr: {e}")))?; + termios::tcgetattr(fd).map_err(|e| VmError::Exec(format!("tcgetattr: {e}")))?; let mut raw = original.clone(); termios::cfmakeraw(&mut raw); - termios::tcsetattr(&fd, SetArg::TCSANOW, &raw) + termios::tcsetattr(fd, SetArg::TCSANOW, &raw) .map_err(|e| VmError::Exec(format!("tcsetattr: {e}")))?; Ok(Self { raw_fd: std::os::unix::io::AsRawFd::as_raw_fd(&stdin), @@ -126,7 +126,7 @@ impl RawModeGuard { impl Drop for RawModeGuard { fn drop(&mut self) { let fd = unsafe { BorrowedFd::borrow_raw(self.raw_fd) }; - let _ = termios::tcsetattr(&fd, SetArg::TCSANOW, &self.original); + let _ = termios::tcsetattr(fd, SetArg::TCSANOW, &self.original); } } @@ -147,25 +147,26 @@ pub fn vm_exec_socket_path(rootfs: &Path) -> PathBuf { // secure_socket_base() when the gvproxy socket dir is created; here // we just compute the path. The parent directory is created (with // permission checks) at launch time via create_dir_all. - let base = if let Some(xdg) = std::env::var_os("XDG_RUNTIME_DIR") { - PathBuf::from(xdg) - } else { - let mut base = PathBuf::from("/tmp"); - if !base.is_dir() { - base = std::env::temp_dir(); - } - base - }; + let base = std::env::var_os("XDG_RUNTIME_DIR").map_or_else( + || { + let mut base = PathBuf::from("/tmp"); + if !base.is_dir() { + base = std::env::temp_dir(); + } + base + }, + PathBuf::from, + ); let dir = base.join("ovm-exec"); let id = hash_path_id(rootfs); dir.join(format!("{id}.sock")) } fn hash_path_id(path: &Path) -> String { - let mut hash: u64 = 0xcbf29ce484222325; + let mut hash: u64 = 0xcbf2_9ce4_8422_2325; for byte in path.to_string_lossy().as_bytes() { hash ^= u64::from(*byte); - hash = hash.wrapping_mul(0x100000001b3); + hash = hash.wrapping_mul(0x0100_0000_01b3); } format!("{:012x}", hash & 0x0000_ffff_ffff_ffff) } @@ -345,15 +346,15 @@ pub fn reset_runtime_state(rootfs: &Path, gateway_name: &str) -> Result<(), VmEr /// This function is a no-op if `state.db` does not exist (e.g. first boot or /// after a full `--reset`). pub fn recover_corrupt_kine_db(rootfs: &Path) -> Result<(), VmError> { + // The SQLite file format begins with a 16-byte magic string. + // Reference: https://www.sqlite.org/fileformat.html#the_database_header + const SQLITE_MAGIC: &[u8] = b"SQLite format 3\x00"; + let db_path = rootfs.join("var/lib/rancher/k3s/server/db/state.db"); if !db_path.exists() { return Ok(()); // Nothing to check — first boot or post-reset. } - // The SQLite file format begins with a 16-byte magic string. - // Reference: https://www.sqlite.org/fileformat.html#the_database_header - const SQLITE_MAGIC: &[u8] = b"SQLite format 3\x00"; - // Read only the first 100 bytes (the minimum valid SQLite header size) // instead of loading the entire database into memory. let has_invalid_header = match File::open(&db_path).and_then(|mut f| { @@ -791,19 +792,18 @@ fn pump_stdin(mut writer: UnixStream, tty: bool) -> Result<(), VmError> { break; } - if tty { - if let Some(size) = get_terminal_size() { - if last_size != Some(size) { - last_size = Some(size); - let _ = send_json_line( - &mut writer, - &ClientFrame::Resize { - cols: size.0, - rows: size.1, - }, - ); - } - } + if tty + && let Some(size) = get_terminal_size() + && last_size != Some(size) + { + last_size = Some(size); + let _ = send_json_line( + &mut writer, + &ClientFrame::Resize { + cols: size.0, + rows: size.1, + }, + ); } let frame = ClientFrame::Stdin { @@ -1015,8 +1015,8 @@ mod tests { rows: u16::MAX, }; let json: serde_json::Value = serde_json::to_value(&frame).unwrap(); - assert_eq!(json["cols"], u16::MAX as u64); - assert_eq!(json["rows"], u16::MAX as u64); + assert_eq!(json["cols"], u64::from(u16::MAX)); + assert_eq!(json["rows"], u64::from(u16::MAX)); } #[test] @@ -1069,9 +1069,7 @@ mod tests { fn stdin_payload_round_trip() { let original = b"echo hello\n"; let encoded = base64::engine::general_purpose::STANDARD.encode(original); - let frame = ClientFrame::Stdin { - data: encoded.clone(), - }; + let frame = ClientFrame::Stdin { data: encoded }; let json = serde_json::to_string(&frame).unwrap(); let parsed: serde_json::Value = serde_json::from_str(&json).unwrap(); let decoded = decode_payload(parsed["data"].as_str().unwrap()).unwrap(); @@ -1102,7 +1100,7 @@ mod tests { }; let v: serde_json::Value = serde_json::to_value(&req).unwrap(); assert!(v["tty"].is_boolean()); - assert_eq!(v["tty"].as_bool().unwrap(), true); + assert!(v["tty"].as_bool().unwrap()); let req_no_tty = ExecRequest { argv: vec!["echo".into()], @@ -1111,7 +1109,7 @@ mod tests { tty: false, }; let v: serde_json::Value = serde_json::to_value(&req_no_tty).unwrap(); - assert_eq!(v["tty"].as_bool().unwrap(), false); + assert!(!v["tty"].as_bool().unwrap()); } #[test] diff --git a/crates/openshell-vm/src/ffi.rs b/crates/openshell-vm/src/ffi.rs index 7500b1c973..66213c6247 100644 --- a/crates/openshell-vm/src/ffi.rs +++ b/crates/openshell-vm/src/ffi.rs @@ -93,6 +93,7 @@ type KrunAddNetUnixstream = unsafe extern "C" fn( flags: u32, ) -> i32; +#[allow(clippy::struct_field_names)] // FFI struct mirrors libkrun's symbol naming pub struct LibKrun { pub krun_init_log: KrunInitLog, pub krun_create_ctx: KrunCreateCtx, @@ -207,7 +208,10 @@ fn preload_runtime_support_libraries(runtime_dir: &Path) -> Result, .is_some_and(|name| { #[cfg(target_os = "macos")] { - name.starts_with("libkrunfw") && name.ends_with(".dylib") + name.starts_with("libkrunfw") + && Path::new(name) + .extension() + .is_some_and(|ext| ext.eq_ignore_ascii_case("dylib")) } #[cfg(not(target_os = "macos"))] { diff --git a/crates/openshell-vm/src/health.rs b/crates/openshell-vm/src/health.rs index 096a35d1f9..c24015bf19 100644 --- a/crates/openshell-vm/src/health.rs +++ b/crates/openshell-vm/src/health.rs @@ -14,8 +14,11 @@ use std::path::PathBuf; use std::time::Duration; use tonic::transport::{Certificate, ClientTlsConfig, Endpoint, Identity}; +/// CA certificate, client certificate, and client key bytes for mTLS. +type MtlsMaterials = (Vec, Vec, Vec); + /// Load mTLS materials from the gateway's cert directory. -fn load_mtls_materials(gateway_name: &str) -> Result<(Vec, Vec, Vec), String> { +fn load_mtls_materials(gateway_name: &str) -> Result { let home = std::env::var("HOME").map_err(|_| "HOME not set")?; let mtls_dir = PathBuf::from(home) .join(".config/openshell/gateways") diff --git a/crates/openshell-vm/src/lib.rs b/crates/openshell-vm/src/lib.rs index 2b78a7669c..b4d8081c1d 100644 --- a/crates/openshell-vm/src/lib.rs +++ b/crates/openshell-vm/src/lib.rs @@ -611,7 +611,7 @@ impl VmContext { Ok(Self { krun, - ctx_id: ctx_id as u32, + ctx_id: ctx_id.cast_unsigned(), }) } @@ -736,8 +736,8 @@ impl VmContext { } fn set_port_map(&self, port_map: &[String]) -> Result<(), VmError> { - let port_strs: Vec<&str> = port_map.iter().map(String::as_str).collect(); - let (_port_owners, port_ptrs) = c_string_array(&port_strs)?; + let port_refs: Vec<&str> = port_map.iter().map(String::as_str).collect(); + let (_port_owners, port_ptrs) = c_string_array(&port_refs)?; unsafe { check( (self.krun.krun_set_port_map)(self.ctx_id, port_ptrs.as_ptr()), @@ -773,10 +773,10 @@ impl VmContext { fn set_exec(&self, exec_path: &str, args: &[String], env: &[String]) -> Result<(), VmError> { let exec_c = CString::new(exec_path)?; - let argv_strs: Vec<&str> = args.iter().map(String::as_str).collect(); - let (_argv_owners, argv_ptrs) = c_string_array(&argv_strs)?; - let env_strs: Vec<&str> = env.iter().map(String::as_str).collect(); - let (_env_owners, env_ptrs) = c_string_array(&env_strs)?; + let argv_refs: Vec<&str> = args.iter().map(String::as_str).collect(); + let (_argv_owners, argv_ptrs) = c_string_array(&argv_refs)?; + let env_refs: Vec<&str> = env.iter().map(String::as_str).collect(); + let (_env_owners, env_ptrs) = c_string_array(&env_refs)?; unsafe { check( @@ -942,7 +942,7 @@ fn kill_stale_gvproxy_by_port(port: u16) { for line in pids.lines() { if let Ok(pid) = line.trim().parse::() { - let pid_i32 = pid as libc::pid_t; + let pid_i32 = pid.cast_signed(); if is_process_named(pid_i32, "gvproxy") { kill_gvproxy_pid(pid); } @@ -951,7 +951,7 @@ fn kill_stale_gvproxy_by_port(port: u16) { } fn kill_gvproxy_pid(gvproxy_pid: u32) { - let pid_i32 = gvproxy_pid as libc::pid_t; + let pid_i32 = gvproxy_pid.cast_signed(); let is_alive = unsafe { libc::kill(pid_i32, 0) } == 0; if is_alive { // Verify the process is actually gvproxy before killing. @@ -998,9 +998,7 @@ fn is_process_named(pid: libc::pid_t, expected: &str) -> bool { #[cfg(target_os = "linux")] fn is_process_named(pid: libc::pid_t, expected: &str) -> bool { let comm_path = format!("/proc/{pid}/comm"); - std::fs::read_to_string(comm_path) - .map(|name| name.trim().contains(expected)) - .unwrap_or(false) + std::fs::read_to_string(comm_path).is_ok_and(|name| name.trim().contains(expected)) } #[cfg(not(any(target_os = "macos", target_os = "linux")))] @@ -1090,10 +1088,10 @@ fn state_disk_sync_mode() -> u32 { } fn hash_path_id(path: &Path) -> String { - let mut hash: u64 = 0xcbf29ce484222325; + let mut hash: u64 = 0xcbf2_9ce4_8422_2325; for byte in path.to_string_lossy().as_bytes() { hash ^= u64::from(*byte); - hash = hash.wrapping_mul(0x100000001b3); + hash = hash.wrapping_mul(0x0100_0000_01b3); } format!("{:012x}", hash & 0x0000_ffff_ffff_ffff) } @@ -1104,15 +1102,16 @@ fn hash_path_id(path: &Path) -> String { /// falls back to `/tmp`. After `create_dir_all`, validates the directory /// is not a symlink and is owned by the current user. fn secure_socket_base(subdir: &str) -> Result { - let base = if let Some(xdg) = std::env::var_os("XDG_RUNTIME_DIR") { - PathBuf::from(xdg) - } else { - let mut base = PathBuf::from("/tmp"); - if !base.is_dir() { - base = std::env::temp_dir(); - } - base - }; + let base = std::env::var_os("XDG_RUNTIME_DIR").map_or_else( + || { + let mut base = PathBuf::from("/tmp"); + if !base.is_dir() { + base = std::env::temp_dir(); + } + base + }, + PathBuf::from, + ); let dir = base.join(subdir); // If the path exists, verify it is not a symlink before using it. @@ -1431,29 +1430,23 @@ pub fn launch(config: &VmConfig) -> Result { // network stack to misroute or drop packets. let mac: [u8; 6] = [0x5a, 0x94, 0xef, 0xe4, 0x0c, 0xee]; - // COMPAT_NET_FEATURES from libkrun.h - const NET_FEATURE_CSUM: u32 = 1 << 0; - const NET_FEATURE_GUEST_CSUM: u32 = 1 << 1; - const NET_FEATURE_GUEST_TSO4: u32 = 1 << 7; - const NET_FEATURE_GUEST_UFO: u32 = 1 << 10; - const NET_FEATURE_HOST_TSO4: u32 = 1 << 11; - const NET_FEATURE_HOST_UFO: u32 = 1 << 14; - const COMPAT_NET_FEATURES: u32 = NET_FEATURE_CSUM - | NET_FEATURE_GUEST_CSUM - | NET_FEATURE_GUEST_TSO4 - | NET_FEATURE_GUEST_UFO - | NET_FEATURE_HOST_TSO4 - | NET_FEATURE_HOST_UFO; + // COMPAT_NET_FEATURES from libkrun.h: + // NET_FEATURE_CSUM (1 << 0) | NET_FEATURE_GUEST_CSUM (1 << 1) + // | NET_FEATURE_GUEST_TSO4 (1 << 7) | NET_FEATURE_GUEST_UFO (1 << 10) + // | NET_FEATURE_HOST_TSO4 (1 << 11) | NET_FEATURE_HOST_UFO (1 << 14). + let compat_net_features: u32 = + (1 << 0) | (1 << 1) | (1 << 7) | (1 << 10) | (1 << 11) | (1 << 14); // On Linux use unixstream (SOCK_STREAM) to connect to gvproxy's // QEMU listener. On macOS use unixgram (SOCK_DGRAM) with the vfkit // magic byte for the vfkit listener. #[cfg(target_os = "linux")] - vm.add_net_unixstream(&net_sock, &mac, COMPAT_NET_FEATURES)?; + vm.add_net_unixstream(&net_sock, &mac, compat_net_features)?; #[cfg(target_os = "macos")] { - const NET_FLAG_VFKIT: u32 = 1 << 0; - vm.add_net_unixgram(&net_sock, &mac, COMPAT_NET_FEATURES, NET_FLAG_VFKIT)?; + // NET_FLAG_VFKIT = 1 << 0 + let net_flag_vfkit: u32 = 1 << 0; + vm.add_net_unixgram(&net_sock, &mac, compat_net_features, net_flag_vfkit)?; } eprintln!( @@ -1761,15 +1754,11 @@ fn bootstrap_gateway(rootfs: &Path, gateway_name: &str, gateway_port: u16) -> Re // drift check and the host already has valid certs. If the agent // isn't reachable we skip silently rather than blocking boot for // 30s. - match fetch_pki_over_exec(&exec_socket, std::time::Duration::from_secs(5)) { - Ok(bundle) => { - if let Err(e) = sync_host_certs_if_stale(gateway_name, &bundle) { - eprintln!("Warning: cert sync check failed: {e}"); - } - } - Err(_) => { - // Expected on warm boot — exec agent not ready yet. - } + // Expected on warm boot — exec agent not ready yet. + if let Ok(bundle) = fetch_pki_over_exec(&exec_socket, std::time::Duration::from_secs(5)) + && let Err(e) = sync_host_certs_if_stale(gateway_name, &bundle) + { + eprintln!("Warning: cert sync check failed: {e}"); } eprintln!( diff --git a/crates/openshell-vm/src/main.rs b/crates/openshell-vm/src/main.rs index bb9d854b12..b2dce993ef 100644 --- a/crates/openshell-vm/src/main.rs +++ b/crates/openshell-vm/src/main.rs @@ -127,31 +127,31 @@ fn main() { // is available and the variable is not already configured. #[cfg(target_os = "macos")] { - if std::env::var_os("__OPENSHELL_VM_REEXEC").is_none() { - if let Ok(runtime_dir) = openshell_vm::configured_runtime_dir() { - let needs_reexec = std::env::var_os("DYLD_LIBRARY_PATH").map_or(true, |v| { - !v.to_string_lossy() - .contains(runtime_dir.to_str().unwrap_or("")) - }); - if needs_reexec { - let mut dyld_paths = vec![runtime_dir]; - if let Some(existing) = std::env::var_os("DYLD_LIBRARY_PATH") { - dyld_paths.extend(std::env::split_paths(&existing)); - } - let joined = std::env::join_paths(&dyld_paths).expect("join DYLD_LIBRARY_PATH"); - let exe = std::env::current_exe().expect("current_exe"); - let args: Vec = std::env::args().skip(1).collect(); - let err = std::process::Command::new(exe) - .args(&args) - .env("DYLD_LIBRARY_PATH", &joined) - .env("__OPENSHELL_VM_REEXEC", "1") - .status(); - match err { - Ok(status) => std::process::exit(status.code().unwrap_or(1)), - Err(e) => { - eprintln!("Error: failed to re-exec with DYLD_LIBRARY_PATH: {e}"); - std::process::exit(1); - } + if std::env::var_os("__OPENSHELL_VM_REEXEC").is_none() + && let Ok(runtime_dir) = openshell_vm::configured_runtime_dir() + { + let needs_reexec = std::env::var_os("DYLD_LIBRARY_PATH").is_none_or(|v| { + !v.to_string_lossy() + .contains(runtime_dir.to_str().unwrap_or("")) + }); + if needs_reexec { + let mut dyld_paths = vec![runtime_dir]; + if let Some(existing) = std::env::var_os("DYLD_LIBRARY_PATH") { + dyld_paths.extend(std::env::split_paths(&existing)); + } + let joined = std::env::join_paths(&dyld_paths).expect("join DYLD_LIBRARY_PATH"); + let exe = std::env::current_exe().expect("current_exe"); + let args: Vec = std::env::args().skip(1).collect(); + let err = std::process::Command::new(exe) + .args(&args) + .env("DYLD_LIBRARY_PATH", &joined) + .env("__OPENSHELL_VM_REEXEC", "1") + .status(); + match err { + Ok(status) => std::process::exit(status.code().unwrap_or(1)), + Err(e) => { + eprintln!("Error: failed to re-exec with DYLD_LIBRARY_PATH: {e}"); + std::process::exit(1); } } } diff --git a/crates/openshell-vm/tests/gateway_integration.rs b/crates/openshell-vm/tests/gateway_integration.rs index 7ababb42f1..fabfd74c82 100644 --- a/crates/openshell-vm/tests/gateway_integration.rs +++ b/crates/openshell-vm/tests/gateway_integration.rs @@ -62,7 +62,7 @@ fn assert_runtime_bundle_staged() { /// Boot the full `OpenShell` gateway and verify the gRPC service becomes /// reachable on port 30051. #[test] -#[ignore] // requires libkrun + rootfs +#[ignore = "requires libkrun + rootfs"] fn gateway_boots_and_service_becomes_reachable() { codesign_if_needed(); assert_runtime_bundle_staged(); @@ -87,7 +87,7 @@ fn gateway_boots_and_service_becomes_reachable() { } // Tear down regardless of result. - let _ = unsafe { libc::kill(child.id() as i32, libc::SIGTERM) }; + let _ = unsafe { libc::kill(child.id().cast_signed(), libc::SIGTERM) }; let _ = child.wait(); assert!( @@ -99,7 +99,7 @@ fn gateway_boots_and_service_becomes_reachable() { /// Run a trivial command inside the VM via `--exec` and verify it exits /// successfully, proving the VM boots and can execute guest processes. #[test] -#[ignore] // requires libkrun + rootfs +#[ignore = "requires libkrun + rootfs"] fn gateway_exec_runs_guest_command() { codesign_if_needed(); assert_runtime_bundle_staged(); @@ -119,7 +119,7 @@ fn gateway_exec_runs_guest_command() { /// Boot the VM, then use `openshell-vm exec` against the running instance. #[test] -#[ignore] // requires libkrun + rootfs +#[ignore = "requires libkrun + rootfs"] fn gateway_exec_attaches_to_running_vm() { codesign_if_needed(); assert_runtime_bundle_staged(); @@ -143,7 +143,7 @@ fn gateway_exec_attaches_to_running_vm() { .output() .expect("failed to run openshell-vm exec"); - let _ = unsafe { libc::kill(child.id() as i32, libc::SIGTERM) }; + let _ = unsafe { libc::kill(child.id().cast_signed(), libc::SIGTERM) }; let _ = child.wait(); assert!( diff --git a/e2e/rust/src/harness/binary.rs b/e2e/rust/src/harness/binary.rs index a9aa785d76..13b8fa4515 100644 --- a/e2e/rust/src/harness/binary.rs +++ b/e2e/rust/src/harness/binary.rs @@ -32,7 +32,8 @@ pub fn openshell_bin() -> PathBuf { let bin = workspace_root().join("target/debug/openshell"); assert!( bin.is_file(), - "openshell binary not found at {bin:?} — run `cargo build -p openshell-cli` first" + "openshell binary not found at {} — run `cargo build -p openshell-cli` first", + bin.display() ); bin } diff --git a/e2e/rust/src/harness/sandbox.rs b/e2e/rust/src/harness/sandbox.rs index 9141f8e2f3..278c16f532 100644 --- a/e2e/rust/src/harness/sandbox.rs +++ b/e2e/rust/src/harness/sandbox.rs @@ -153,10 +153,10 @@ impl SandboxGuard { accumulated.push('\n'); // Try to extract the sandbox name from the header. - if name.is_none() { - if let Some(n) = extract_sandbox_name(&accumulated) { - name = Some(n); - } + if name.is_none() + && let Some(n) = extract_sandbox_name(&accumulated) + { + name = Some(n); } // Check for the ready marker. diff --git a/e2e/rust/tests/cf_auth_smoke.rs b/e2e/rust/tests/cf_auth_smoke.rs index 43a2f96c51..aa46604fb9 100644 --- a/e2e/rust/tests/cf_auth_smoke.rs +++ b/e2e/rust/tests/cf_auth_smoke.rs @@ -147,7 +147,7 @@ async fn gateway_login_help_is_recognized() { // ------------------------------------------------------------------- /// `openshell gateway add ` (cloud gateway) should: -/// - Create cluster metadata with auth_mode = "cloudflare_jwt" +/// - Create cluster metadata with `auth_mode` = `"cloudflare_jwt"` /// - Set the gateway as active /// - Attempt browser authentication (which will fail in CI — non-fatal) #[tokio::test] diff --git a/e2e/rust/tests/docker_preflight.rs b/e2e/rust/tests/docker_preflight.rs index 0b51666e0c..d6e125b4b7 100644 --- a/e2e/rust/tests/docker_preflight.rs +++ b/e2e/rust/tests/docker_preflight.rs @@ -86,7 +86,7 @@ async fn gateway_start_error_mentions_docker() { } /// When Docker is unavailable, the error output should include guidance -/// about DOCKER_HOST since that's the likely fix for non-default runtimes. +/// about `DOCKER_HOST` since that's the likely fix for non-default runtimes. #[tokio::test] async fn gateway_start_error_mentions_docker_host() { let (output, code, _) = run_without_docker(&["gateway", "start"]).await; diff --git a/e2e/rust/tests/sandbox_labels.rs b/e2e/rust/tests/sandbox_labels.rs index 5b682ed267..018a27615f 100644 --- a/e2e/rust/tests/sandbox_labels.rs +++ b/e2e/rust/tests/sandbox_labels.rs @@ -115,6 +115,7 @@ async fn delete_sandbox(name: &str) { } #[tokio::test] +#[allow(clippy::too_many_lines)] // end-to-end test exercises full label lifecycle async fn sandbox_labels_are_stored_and_filterable() { // Create sandboxes with different labels let name1 = create_sandbox_with_labels( @@ -195,7 +196,7 @@ async fn sandbox_labels_are_stored_and_filterable() { assert_eq!( dev_backend_sandboxes .iter() - .filter(|name| [&name1, &name2, &name3, &name4].contains(&name)) + .filter(|name| [&name1, &name2, &name3, &name4].contains(name)) .count(), 1, "env=dev,team=backend filter should return exactly 1 sandbox, got: {dev_backend_sandboxes:?}" diff --git a/e2e/rust/tests/sync.rs b/e2e/rust/tests/sync.rs index 29ffdc00d4..f08f670c51 100644 --- a/e2e/rust/tests/sync.rs +++ b/e2e/rust/tests/sync.rs @@ -261,26 +261,31 @@ async fn upload_respects_gitignore_by_default() { .await .expect("download filtered upload"); + // Filtered uploads of a directory preserve the source basename, so + // contents land under `/repo/...` (matches `openshell sandbox + // upload ` semantics from the unfiltered path). + let uploaded_root = download_dir.join("repo"); + // tracked.txt should be present. - let tracked = fs::read_to_string(download_dir.join("tracked.txt")) + let tracked = fs::read_to_string(uploaded_root.join("tracked.txt")) .expect("tracked.txt should exist after filtered upload"); assert_eq!(tracked, "i-am-tracked", "tracked.txt content mismatch"); // .gitignore itself should be present (it's tracked). assert!( - download_dir.join(".gitignore").exists(), + uploaded_root.join(".gitignore").exists(), ".gitignore should be uploaded (it's a tracked file)" ); // ignored.log should NOT be present. assert!( - !download_dir.join("ignored.log").exists(), + !uploaded_root.join("ignored.log").exists(), "ignored.log should be filtered out by .gitignore" ); // build/ directory should NOT be present. assert!( - !download_dir.join("build").exists(), + !uploaded_root.join("build").exists(), "build/ directory should be filtered out by .gitignore" ); diff --git a/tasks/rust.toml b/tasks/rust.toml index 69214ce7ff..2972957fce 100644 --- a/tasks/rust.toml +++ b/tasks/rust.toml @@ -9,8 +9,11 @@ run = "cargo check --workspace" hide = true ["rust:lint"] -description = "Lint Rust code with Clippy" -run = "cargo clippy --workspace --all-targets" +description = "Lint Rust code with Clippy (deny warnings)" +run = [ + "cargo clippy --workspace --all-targets -- -D warnings", + "cargo clippy --manifest-path e2e/rust/Cargo.toml --all-targets -- -D warnings", +] hide = true ["rust:format"]