MCPcopy Create free account
hub / github.com/NVIDIA/OpenShell / build_env

Function build_env

crates/openshell-driver-podman/src/container.rs:331–427  ·  view source on GitHub ↗

Merge environment variables from user spec/template with required driver vars. User-supplied vars are inserted first so that the required driver vars always win -- preventing spec/template overrides of security- critical values like `OPENSHELL_ENDPOINT` or `OPENSHELL_SANDBOX_ID`.

(
    sandbox: &DriverSandbox,
    config: &PodmanComputeConfig,
    image: &str,
)

Source from the content-addressed store, hash-verified

329/// vars always win -- preventing spec/template overrides of security-
330/// critical values like `OPENSHELL_ENDPOINT` or `OPENSHELL_SANDBOX_ID`.
331fn build_env(
332 sandbox: &DriverSandbox,
333 config: &PodmanComputeConfig,
334 image: &str,
335) -> BTreeMap<String, String> {
336 let spec = sandbox.spec.as_ref();
337 let template = spec.and_then(|s| s.template.as_ref());
338
339 let mut env: BTreeMap<String, String> = BTreeMap::new();
340
341 // 1. User-supplied environment (lowest priority).
342 // Template vars first, then spec overwrites (spec is user-specified).
343 let mut user_env: BTreeMap<String, String> = BTreeMap::new();
344 if let Some(t) = template {
345 for (k, v) in &t.environment {
346 user_env.insert(k.clone(), v.clone());
347 }
348 }
349 if let Some(s) = spec {
350 if !s.log_level.is_empty() {
351 env.insert(
352 openshell_core::sandbox_env::LOG_LEVEL.into(),
353 s.log_level.clone(),
354 );
355 }
356 for (k, v) in &s.environment {
357 user_env.insert(k.clone(), v.clone());
358 }
359 }
360 env.extend(user_env.clone());
361 if !user_env.is_empty()
362 && let Ok(json) = serde_json::to_string(&user_env)
363 {
364 env.insert(openshell_core::sandbox_env::USER_ENVIRONMENT.into(), json);
365 }
366
367 // 2. Required driver vars (highest priority -- always overwrite).
368 env.insert(
369 openshell_core::sandbox_env::SANDBOX.into(),
370 sandbox.name.clone(),
371 );
372 env.insert(
373 openshell_core::sandbox_env::SANDBOX_ID.into(),
374 sandbox.id.clone(),
375 );
376 env.insert(
377 openshell_core::sandbox_env::ENDPOINT.into(),
378 config.grpc_endpoint.clone(),
379 );
380 env.insert(
381 openshell_core::sandbox_env::SSH_SOCKET_PATH.into(),
382 config.sandbox_ssh_socket_path.clone(),
383 );
384 env.insert("OPENSHELL_CONTAINER_IMAGE".into(), image.to_string());
385 env.insert(
386 openshell_core::sandbox_env::SANDBOX_COMMAND.into(),
387 "sleep infinity".into(),
388 );

Calls 4

enabled_env_valueFunction · 0.85
tls_enabledMethod · 0.80
is_emptyMethod · 0.45
removeMethod · 0.45

Tested by

no test coverage detected