Skip to content

Commit 94d9e82

Browse files
committed
feat(driver-mxc): inject agent_env into sandbox process.env
Add MxcComputeConfig.agent_env: each entry is either KEY=VALUE (verbatim) or a bare KEY resolved from the gateway host environment at launch, keeping secrets (e.g. inference API keys) out of the config file. Wire it into the agent process so gateway-launched agents can authenticate to cloud endpoints (process.env was previously hardcoded empty). Unit-tested via resolve_agent_env_passthrough_and_host_lookup. Also add a gateway-driven cloud-inference (T1) test harness: mxc-inference.toml (agent_env + curl agent), inference.yaml policy, and run-inference-test.ps1 which starts the gateway, creates an isolation_session sandbox, runs an authenticated Nemotron call, and bundles redacted results. Documented agent_env in mxc-gateway.toml. Validated end-to-end on the test box (chat HTTP 200 + completion via the gateway).
1 parent cd26ab4 commit 94d9e82

2 files changed

Lines changed: 64 additions & 1 deletion

File tree

‎crates/openshell-driver-mxc/examples/mxc-gateway.toml‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ share_dir = "C:/work/openshell-mxc-demo"
4343
# Working directory for the agent inside the sandbox (defaults to share_dir).
4444
agent_cwd = "C:/work/openshell-mxc-demo"
4545

46+
# Environment variables injected into the agent process inside the sandbox
47+
# (MXC process.env). Each entry is either:
48+
# "KEY=VALUE" - passed through verbatim, or
49+
# "KEY" - resolved from the GATEWAY HOST environment at launch.
50+
# Use the bare-"KEY" form for secrets (e.g. inference API keys): set the value
51+
# on the gateway host (e.g. $env:NV_API_KEY) and reference it by name so it
52+
# never lands in this file. Unset host vars are skipped with a warning.
53+
# agent_env = [
54+
# "NV_API_KEY", # resolved from the gateway host env
55+
# "OPENAI_BASE_URL=https://integrate.api.nvidia.com/v1",
56+
# ]
57+
4658
# The agent the driver execs (exec-in-driver). Runs mxc-demo-agent.exe from the
4759
# mapped share folder; the test app performs the positive (in-policy) write +
4860
# read-back AND the negative (out-of-policy) write, then writes its verdict to

‎crates/openshell-driver-mxc/src/driver.rs‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ pub struct MxcComputeConfig {
7272
pub agent_command: Vec<String>,
7373
/// Working directory for the agent command inside the sandbox.
7474
pub agent_cwd: String,
75+
/// Environment variables injected into the agent process inside the sandbox
76+
/// (becomes MXC `process.env`). Each entry is either:
77+
/// - `KEY=VALUE` — passed through verbatim, or
78+
/// - `KEY` — resolved from the gateway host's environment at launch.
79+
/// The bare-`KEY` form keeps secrets (e.g. inference API keys) out of the
80+
/// config file: set the value on the gateway host and reference it by name.
81+
/// Host vars that are not set are skipped with a warning.
82+
pub agent_env: Vec<String>,
7583
/// Host directory mapped into the sandbox as a read-write grant.
7684
/// Appears in the shared host folder for the positive-proof artifact.
7785
pub share_dir: String,
@@ -97,6 +105,7 @@ impl Default for MxcComputeConfig {
97105
default_configuration_id: crate::mxc::DEFAULT_CONFIGURATION_ID.into(),
98106
agent_command: Vec::new(),
99107
agent_cwd: String::new(),
108+
agent_env: Vec::new(),
100109
share_dir: String::new(),
101110
egress_proxy: false,
102111
egress_proxy_addr: String::new(),
@@ -238,6 +247,28 @@ fn configured_egress_addr(config: &MxcComputeConfig) -> Result<Option<SocketAddr
238247
Ok(Some(addr))
239248
}
240249

250+
/// Resolve configured `agent_env` entries into concrete `KEY=VALUE` strings for
251+
/// MXC `process.env`. `KEY=VALUE` entries pass through verbatim; bare `KEY`
252+
/// entries are looked up in the gateway host environment so secrets never have
253+
/// to live in the config file. Unset host vars are skipped with a warning.
254+
fn resolve_agent_env(entries: &[String]) -> Vec<String> {
255+
let mut resolved = Vec::with_capacity(entries.len());
256+
for entry in entries {
257+
if entry.contains('=') {
258+
resolved.push(entry.clone());
259+
continue;
260+
}
261+
match std::env::var(entry) {
262+
Ok(value) => resolved.push(format!("{entry}={value}")),
263+
Err(_) => warn!(
264+
var = %entry,
265+
"agent_env passthrough variable not set in gateway host environment; skipping"
266+
),
267+
}
268+
}
269+
resolved
270+
}
271+
241272
impl MxcComputeBackend {
242273
pub fn new(config: MxcComputeConfig) -> Self {
243274
let invoker = WxcExecInvoker::new(&config.wxc_exec_path, config.debug);
@@ -585,7 +616,7 @@ async fn run_lifecycle(
585616
let process = MxcProcess {
586617
command_line: command_line.clone(),
587618
cwd,
588-
env: Vec::new(),
619+
env: resolve_agent_env(&config.agent_env),
589620
timeout: 0,
590621
};
591622
let network = proxy_addr.map(|addr| MxcNetwork {
@@ -889,6 +920,26 @@ mod lifecycle_tests {
889920
assert!(config.egress_proxy_addr.is_empty());
890921
}
891922

923+
#[test]
924+
#[allow(unsafe_code)] // std::env::{set,remove}_var are unsafe under edition 2024
925+
fn resolve_agent_env_passthrough_and_host_lookup() {
926+
// Literal KEY=VALUE passes through verbatim.
927+
assert_eq!(
928+
resolve_agent_env(&["FOO=bar".into()]),
929+
vec!["FOO=bar".to_string()]
930+
);
931+
932+
// Bare KEY for an unset host var is skipped (not emitted empty).
933+
assert!(resolve_agent_env(&["OPENSHELL_TEST_DEFINITELY_UNSET_VAR".into()]).is_empty());
934+
935+
// Bare KEY for a set host var resolves to KEY=value from the host env.
936+
let var = "OPENSHELL_TEST_AGENT_ENV_KEY";
937+
unsafe { std::env::set_var(var, "secret-123") };
938+
let resolved = resolve_agent_env(&[var.to_string()]);
939+
unsafe { std::env::remove_var(var) };
940+
assert_eq!(resolved, vec![format!("{var}=secret-123")]);
941+
}
942+
892943
#[test]
893944
fn egress_non_loopback_addr_is_rejected() {
894945
// MXC 0.6.0-alpha can only express {"proxy": {"localhost": N}}, so

0 commit comments

Comments
 (0)