auto / runtime_environment /process_utils.py
rsnarsna
feat(alli-backend): Wave-1 standardization, standalone sys.env node & vault crypto, and DEC-014 security hardening
8628c98
Raw History Blame Contribute Delete
5.33 kB
"""
ALLI Runtime Environment Process & Environment Isolation Utilities.
Provides ExecutionResult, credential-scrubbed environments, package cache resolution,
and recursive OS process-tree teardown.
"""
from dataclasses import dataclass
import os
import subprocess
import sys
from typing import Dict, Optional, Union
from language_core.workspace_env_contracts import (
build_allowlisted_host_env,
validate_env_key,
validate_env_value,
)
from runtime_environment.env_injection import get_active_run_env
DEFAULT_PACKAGE_CACHE_DIR = os.path.abspath(
os.path.join(os.path.dirname(__file__), "..", "storage", "package_cache")
)
@dataclass
class ExecutionResult:
success: bool
exit_code: int
stdout: str
stderr: str
duration_ms: float
mode: str
memory_limit: Optional[str] = None
cpu_limit: Optional[str] = None
def to_dict(self) -> Dict:
return {
"success": self.success,
"exit_code": self.exit_code,
"stdout": self.stdout,
"stderr": self.stderr,
"duration_ms": round(self.duration_ms, 2),
"mode": self.mode,
"memory_limit": self.memory_limit,
"cpu_limit": self.cpu_limit,
}
def resolve_package_cache_dir(explicit_dir: Optional[str] = None) -> Optional[str]:
"""Return an existing non-empty package cache directory for PYTHONPATH mounting, if available."""
candidate = explicit_dir or os.environ.get("ALLI_PACKAGE_CACHE_DIR") or DEFAULT_PACKAGE_CACHE_DIR
if candidate and os.path.isdir(candidate):
try:
if explicit_dir or os.environ.get("ALLI_PACKAGE_CACHE_DIR") or os.listdir(candidate):
return os.path.realpath(candidate)
except OSError:
pass
return None
def get_sanitized_env(
package_cache_dir: Optional[str] = None,
workspace_env: Optional[Dict[str, str]] = None,
) -> Dict[str, str]:
"""
Construct an explicit-allowlist execution environment with zero leaked host secrets,
optionally overlaying validated workspace env vars and cached package path.
"""
clean_env = build_allowlisted_host_env(os.environ)
env_overlay = workspace_env
if env_overlay is None:
active_ctx = get_active_run_env()
if active_ctx and not active_ctx.is_viewer:
env_overlay = active_ctx.get_effective_vars()
if env_overlay:
for raw_k, raw_v in env_overlay.items():
ok_k, clean_k, _ = validate_env_key(raw_k)
ok_v, clean_v, _ = validate_env_value(raw_v)
if ok_k and ok_v:
clean_env[clean_k] = clean_v
clean_env["PYTHONUNBUFFERED"] = "1"
clean_env["PYTHONDONTWRITEBYTECODE"] = "1"
pkg_dir = resolve_package_cache_dir(package_cache_dir)
if pkg_dir:
existing_pp = clean_env.get("PYTHONPATH", "")
clean_env["PYTHONPATH"] = f"{pkg_dir}{os.pathsep}{existing_pp}" if existing_pp else pkg_dir
return clean_env
def kill_process_tree(proc: Optional[Union[subprocess.Popen, int]]) -> None:
"""Terminates the process and all of its descendant children to prevent orphan processes."""
if proc is None:
return
pid = proc if isinstance(proc, int) else getattr(proc, "pid", None)
if not pid:
return
killed_via_psutil = False
try:
import psutil
try:
parent = psutil.Process(pid)
for child in parent.children(recursive=True):
try:
child.kill()
except Exception:
pass
parent.kill()
killed_via_psutil = True
except psutil.NoSuchProcess:
killed_via_psutil = True
except Exception:
pass
if not killed_via_psutil and sys.platform == "win32":
try:
subprocess.run(["taskkill", "/F", "/T", "/PID", str(pid)], capture_output=True, timeout=1.0)
except Exception:
pass
elif not killed_via_psutil:
try:
import signal
os.killpg(os.getpgid(pid), signal.SIGKILL)
except Exception:
try:
if hasattr(proc, "kill"):
proc.kill()
except Exception:
pass
def build_docker_run_command(
temp_dir: str,
container_name: str,
image_name: str,
memory_limit: str,
cpu_limit: str,
package_cache_dir: Optional[str] = None,
env_file: Optional[str] = None,
) -> list[str]:
"""Construct air-gapped Docker command with least-privilege hardening and optional external --env-file."""
cmd = [
"docker", "run", "--rm",
"--name", container_name,
"--network", "none",
"--cap-drop=ALL",
"--security-opt=no-new-privileges:true",
"--pids-limit=64",
"--read-only",
"--tmpfs", "/tmp:rw,noexec,nosuid,size=16m",
f"--memory={memory_limit}",
f"--cpus={cpu_limit}",
"-v", f"{temp_dir}:/app:ro",
]
if env_file:
cmd.extend(["--env-file", env_file])
pkg_cache = resolve_package_cache_dir(package_cache_dir)
if pkg_cache:
cmd.extend(["-v", f"{pkg_cache}:/opt/packages:ro", "-e", "PYTHONPATH=/opt/packages:/app"])
cmd.extend([image_name, "python", "/app/script.py"])
return cmd