File size: 5,330 Bytes
8628c98 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | """
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
|