use std::path::{Component, Path, PathBuf};
use std::process::Stdio;
use std::time::Instant;
use async_trait::async_trait;
use tracing::{debug, info, warn};
use crate::host_process;
use crate::{
BackendCapabilities, CodeExecutor, EnvironmentPolicy, ExecutionError, ExecutionIsolation,
ExecutionLanguage, ExecutionPayload, ExecutionRequest, ExecutionResult, ExecutionStatus,
FilesystemPolicy, NetworkPolicy, SandboxPolicy, validate_request,
};
#[derive(Debug, Clone)]
pub struct DockerConfig {
pub image: String,
pub work_dir: String,
pub setup_commands: Vec<String>,
pub environment: Vec<String>,
pub bind_mounts: Vec<String>,
pub network_disabled: bool,
pub container_name_prefix: String,
pub auto_start: bool,
pub auto_remove: bool,
pub drop_capabilities: bool,
pub no_new_privileges: bool,
pub pids_limit: Option<u32>,
}
impl DockerConfig {
pub fn python() -> Self {
Self {
image: "python:3.12-slim".to_string(),
work_dir: "/workspace".to_string(),
setup_commands: vec![],
environment: vec![],
bind_mounts: vec![],
network_disabled: true,
container_name_prefix: "adk-python".to_string(),
auto_start: true,
auto_remove: true,
drop_capabilities: true,
no_new_privileges: true,
pids_limit: Some(DEFAULT_PIDS_LIMIT),
}
}
pub fn node() -> Self {
Self {
image: "node:20-slim".to_string(),
work_dir: "/workspace".to_string(),
setup_commands: vec![],
environment: vec![],
bind_mounts: vec![],
network_disabled: true,
container_name_prefix: "adk-node".to_string(),
auto_start: true,
auto_remove: true,
drop_capabilities: true,
no_new_privileges: true,
pids_limit: Some(DEFAULT_PIDS_LIMIT),
}
}
pub fn custom(image: impl Into<String>) -> Self {
Self {
image: image.into(),
work_dir: "/workspace".to_string(),
setup_commands: vec![],
environment: vec![],
bind_mounts: vec![],
network_disabled: true,
container_name_prefix: "adk-custom".to_string(),
auto_start: true,
auto_remove: true,
drop_capabilities: true,
no_new_privileges: true,
pids_limit: Some(DEFAULT_PIDS_LIMIT),
}
}
pub fn setup_command(mut self, cmd: impl Into<String>) -> Self {
self.setup_commands.push(cmd.into());
self
}
pub fn pip_install(self, packages: &[&str]) -> Self {
self.setup_command(format!("pip install --quiet {}", packages.join(" ")))
}
pub fn npm_install(self, packages: &[&str]) -> Self {
self.setup_command(format!("npm install --silent {}", packages.join(" ")))
}
pub fn with_network(mut self) -> Self {
self.network_disabled = false;
self
}
pub fn bind_mount(mut self, mount: impl Into<String>) -> Self {
self.bind_mounts.push(mount.into());
self
}
pub fn env(mut self, var: impl Into<String>) -> Self {
self.environment.push(var.into());
self
}
pub fn validate_sandbox_policy(&self, policy: &SandboxPolicy) -> Result<(), ExecutionError> {
if matches!(policy.network, NetworkPolicy::Disabled) && !self.network_disabled {
return Err(ExecutionError::UnsupportedPolicy(
"the policy disables network access, but the container was created with network \
access; drop `with_network()` from the DockerConfig or use \
`NetworkPolicy::Enabled`"
.to_string(),
));
}
for mount in &self.bind_mounts {
let parts: Vec<&str> = mount.split(':').collect();
let has_drive_letter = parts.len() > 2
&& parts[0].len() == 1
&& parts[0].chars().all(|c| c.is_ascii_alphabetic());
let (host, rest) = if has_drive_letter {
(&mount[..parts[0].len() + 1 + parts[1].len()], &parts[2..])
} else {
(parts[0], &parts[1..])
};
let read_only = rest
.get(1)
.is_some_and(|options| options.split(',').any(|o| o == "ro" || o == "readonly"));
let host = Path::new(host);
let confined = host.has_root()
&& !host.components().any(|component| matches!(component, Component::ParentDir));
let under = |root: &PathBuf| confined && host.starts_with(root);
let permitted = match &policy.filesystem {
FilesystemPolicy::None => false,
FilesystemPolicy::WorkspaceReadOnly { root } => read_only && under(root),
FilesystemPolicy::WorkspaceReadWrite { root } => under(root),
FilesystemPolicy::Paths { read_only: read_only_paths, read_write } => {
read_write.iter().any(under) || (read_only && read_only_paths.iter().any(under))
}
};
if !permitted {
return Err(ExecutionError::UnsupportedPolicy(format!(
"bind mount `{mount}` exceeds the filesystem policy {:?}; remove the mount \
or grant its host path in the policy",
policy.filesystem
)));
}
}
for entry in &self.environment {
let name = entry.split_once('=').map_or(entry.as_str(), |(name, _)| name);
let allowed = match &policy.environment {
EnvironmentPolicy::None => false,
EnvironmentPolicy::AllowList(names) => names.iter().any(|allowed| allowed == name),
};
if !allowed {
return Err(ExecutionError::UnsupportedPolicy(format!(
"container environment variable `{name}` is not allowed by the environment \
policy; remove it from the DockerConfig or add it to \
`EnvironmentPolicy::AllowList`"
)));
}
}
Ok(())
}
}
impl Default for DockerConfig {
fn default() -> Self {
Self::python()
}
}
#[cfg(feature = "docker")]
mod docker_impl {
use super::*;
use bollard::Docker;
use bollard::container::{
Config, CreateContainerOptions, RemoveContainerOptions, StartContainerOptions,
};
use bollard::exec::{CreateExecOptions, StartExecResults};
use futures::StreamExt;
use rand::Rng;
use tokio::sync::RwLock;
const SETUP_OUTPUT_LIMITS: (usize, usize) = (64 * 1024, 64 * 1024);
#[derive(Debug)]
struct ContainerState {
id: String,
running: bool,
file_counter: u64,
}
pub struct DockerExecutor {
config: DockerConfig,
docker: Docker,
state: RwLock<Option<ContainerState>>,
}
impl std::fmt::Debug for DockerExecutor {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("DockerExecutor").field("config", &self.config).finish()
}
}
impl DockerExecutor {
pub fn new(config: DockerConfig) -> std::result::Result<Self, ExecutionError> {
let docker = Docker::connect_with_local_defaults().map_err(|e| {
ExecutionError::InternalError(format!(
"failed to connect to Docker daemon: {e}. Is Docker installed and running?"
))
})?;
Ok(Self { config, docker, state: RwLock::new(None) })
}
pub fn with_docker(config: DockerConfig, docker: Docker) -> Self {
Self { config, docker, state: RwLock::new(None) }
}
pub async fn cleanup(&self) -> Result<(), ExecutionError> {
let mut state = self.state.write().await;
if let Some(s) = state.take() {
info!(container_id = %s.id, "cleaning up container");
self.docker
.remove_container(
&s.id,
Some(RemoveContainerOptions { force: true, ..Default::default() }),
)
.await
.map_err(|e| {
ExecutionError::ExecutionFailed(format!("failed to remove container: {e}"))
})?;
}
Ok(())
}
fn host_config(&self) -> bollard::models::HostConfig {
let config = &self.config;
bollard::models::HostConfig {
network_mode: config.network_disabled.then(|| "none".to_string()),
binds: (!config.bind_mounts.is_empty()).then(|| config.bind_mounts.clone()),
cap_drop: config.drop_capabilities.then(|| vec!["ALL".to_string()]),
security_opt: config
.no_new_privileges
.then(|| vec!["no-new-privileges".to_string()]),
pids_limit: config.pids_limit.map(i64::from),
..Default::default()
}
}
fn container_name(&self) -> String {
let suffix: u32 = rand::rng().random_range(100_000..999_999);
format!("{}-{suffix}", self.config.container_name_prefix)
}
fn file_extension(lang: &ExecutionLanguage) -> &'static str {
match lang {
ExecutionLanguage::Python => "py",
ExecutionLanguage::JavaScript => "js",
ExecutionLanguage::Rust => "rs",
ExecutionLanguage::Command => "sh",
ExecutionLanguage::Wasm => "wasm",
}
}
fn exec_command(lang: &ExecutionLanguage, filename: &str) -> Vec<String> {
match lang {
ExecutionLanguage::Python => {
vec!["python3".to_string(), filename.to_string()]
}
ExecutionLanguage::JavaScript => {
vec!["node".to_string(), filename.to_string()]
}
ExecutionLanguage::Command => {
vec!["sh".to_string(), filename.to_string()]
}
_ => vec![],
}
}
async fn write_file(
&self,
container_id: &str,
path: &str,
content: &str,
) -> Result<(), ExecutionError> {
let encoded = base64_encode(content.as_bytes());
let cmd = vec![
"sh".to_string(),
"-c".to_string(),
format!("echo '{encoded}' | base64 -d > {path}"),
];
self.exec_in_container(container_id, &cmd, None, None, SETUP_OUTPUT_LIMITS).await?;
Ok(())
}
async fn exec_in_container(
&self,
container_id: &str,
cmd: &[String],
env: Option<Vec<String>>,
timeout: Option<std::time::Duration>,
limits: (usize, usize),
) -> Result<ExecOutput, ExecutionError> {
let pid_file =
timeout.map(|_| format!("/tmp/.adk-exec-{}.pid", super::container_name("pid")));
let cmd = match &pid_file {
Some(pid_file) => with_pid_file(pid_file, cmd),
None => cmd.to_vec(),
};
let exec_output = self.run_exec(container_id, cmd, env, limits);
match (timeout, pid_file) {
(Some(dur), Some(pid_file)) => match tokio::time::timeout(dur, exec_output).await {
Ok(result) => result,
Err(_) => {
self.kill_exec(container_id, &pid_file).await;
Err(ExecutionError::Timeout(dur.as_millis() as u64))
}
},
_ => exec_output.await,
}
}
async fn run_exec(
&self,
container_id: &str,
cmd: Vec<String>,
env: Option<Vec<String>>,
limits: (usize, usize),
) -> Result<ExecOutput, ExecutionError> {
let exec = self
.docker
.create_exec(
container_id,
CreateExecOptions {
cmd: Some(cmd),
env,
attach_stdout: Some(true),
attach_stderr: Some(true),
working_dir: Some(self.config.work_dir.clone()),
..Default::default()
},
)
.await
.map_err(|e| {
ExecutionError::ExecutionFailed(format!("failed to create exec: {e}"))
})?;
{
match self.docker.start_exec(&exec.id, None).await {
Ok(StartExecResults::Attached { mut output, .. }) => {
let mut captured = ExecOutput::default();
while let Some(chunk) = output.next().await {
match chunk {
Ok(bollard::container::LogOutput::StdOut { message }) => {
push_bounded(
&mut captured.stdout,
&mut captured.stdout_truncated,
&message,
limits.0,
);
}
Ok(bollard::container::LogOutput::StdErr { message }) => {
push_bounded(
&mut captured.stderr,
&mut captured.stderr_truncated,
&message,
limits.1,
);
}
Ok(_) => {}
Err(e) => {
return Err(ExecutionError::ExecutionFailed(format!(
"exec stream error: {e}"
)));
}
}
}
let inspect = self.docker.inspect_exec(&exec.id).await.map_err(|e| {
ExecutionError::ExecutionFailed(format!("failed to inspect exec: {e}"))
})?;
captured.exit_code = inspect.exit_code;
Ok(captured)
}
Ok(StartExecResults::Detached) => Ok(ExecOutput::default()),
Err(e) => {
Err(ExecutionError::ExecutionFailed(format!("failed to start exec: {e}")))
}
}
}
}
async fn kill_exec(&self, container_id: &str, pid_file: &str) {
let kill = vec![
"sh".to_string(),
"-c".to_string(),
r#"pid=$(cat "$0") && { kill -KILL -- "-$pid" 2>/dev/null || kill -KILL "$pid"; }"#
.to_string(),
pid_file.to_string(),
];
let killed = tokio::time::timeout(
super::KILL_TIMEOUT,
self.run_exec(container_id, kill, None, (0, 4096)),
)
.await;
match killed {
Ok(Ok(output)) if output.exit_code == Some(0) => {}
Ok(Ok(output)) => warn!(
container_id = %container_id,
exit_code = ?output.exit_code,
stderr = %output.stderr,
"killing the timed-out exec failed"
),
Ok(Err(error)) => {
warn!(container_id = %container_id, %error, "killing the timed-out exec failed")
}
Err(_) => {
warn!(container_id = %container_id, "killing the timed-out exec did not finish")
}
}
}
}
#[derive(Debug, Default)]
struct ExecOutput {
stdout: String,
stderr: String,
exit_code: Option<i64>,
stdout_truncated: bool,
stderr_truncated: bool,
}
fn with_pid_file(pid_file: &str, cmd: &[String]) -> Vec<String> {
let mut wrapped = vec![
"sh".to_string(),
"-c".to_string(),
r#"echo $$ > "$0" && exec "$@""#.to_string(),
pid_file.to_string(),
];
wrapped.extend(cmd.iter().cloned());
wrapped
}
fn push_bounded(buffer: &mut String, truncated: &mut bool, chunk: &[u8], limit: usize) {
if *truncated {
return;
}
let text = String::from_utf8_lossy(chunk);
let room = limit.saturating_sub(buffer.len());
if text.len() <= room {
buffer.push_str(&text);
} else {
buffer.push_str(&text[..text.floor_char_boundary(room)]);
*truncated = true;
}
}
#[async_trait]
impl CodeExecutor for DockerExecutor {
fn name(&self) -> &str {
"docker"
}
fn capabilities(&self) -> BackendCapabilities {
BackendCapabilities {
isolation: ExecutionIsolation::ContainerPersistent,
enforce_network_policy: true,
enforce_filesystem_policy: true,
enforce_environment_policy: true,
enforce_timeout: true,
supports_structured_output: true,
supports_process_execution: true,
supports_persistent_workspace: true,
supports_interactive_sessions: false,
}
}
fn supports_language(&self, lang: &ExecutionLanguage) -> bool {
matches!(
lang,
ExecutionLanguage::Python
| ExecutionLanguage::JavaScript
| ExecutionLanguage::Command
)
}
async fn start(&self) -> Result<(), ExecutionError> {
let mut state = self.state.write().await;
if state.as_ref().is_some_and(|s| s.running) {
return Ok(());
}
let name = self.container_name();
info!(image = %self.config.image, container = %name, "creating container");
let host_config = self.host_config();
let env = if self.config.environment.is_empty() {
None
} else {
Some(self.config.environment.clone())
};
let container_config = Config {
image: Some(self.config.image.clone()),
working_dir: Some(self.config.work_dir.clone()),
env,
host_config: Some(host_config),
cmd: Some(vec!["sleep".to_string(), "infinity".to_string()]),
tty: Some(false),
..Default::default()
};
let create_opts = CreateContainerOptions { name: name.clone(), ..Default::default() };
let response =
self.docker.create_container(Some(create_opts), container_config).await.map_err(
|e| ExecutionError::ExecutionFailed(format!("failed to create container: {e}")),
)?;
let container_id = response.id;
debug!(container_id = %container_id, "container created");
self.docker
.start_container(&container_id, None::<StartContainerOptions<String>>)
.await
.map_err(|e| {
ExecutionError::ExecutionFailed(format!("failed to start container: {e}"))
})?;
info!(container_id = %container_id, "container started");
let mkdir_cmd =
vec!["mkdir".to_string(), "-p".to_string(), self.config.work_dir.clone()];
let _ = self
.exec_in_container(&container_id, &mkdir_cmd, None, None, SETUP_OUTPUT_LIMITS)
.await;
for setup_cmd in &self.config.setup_commands {
info!(cmd = %setup_cmd, "running setup command");
let cmd = vec!["sh".to_string(), "-c".to_string(), setup_cmd.clone()];
let ExecOutput { stderr, exit_code, .. } = self
.exec_in_container(&container_id, &cmd, None, None, SETUP_OUTPUT_LIMITS)
.await?;
if exit_code != Some(0) {
warn!(
exit_code = ?exit_code,
stderr = %stderr,
"setup command failed"
);
let _ = self
.docker
.remove_container(
&container_id,
Some(RemoveContainerOptions { force: true, ..Default::default() }),
)
.await;
return Err(ExecutionError::ExecutionFailed(format!(
"setup command failed: {setup_cmd}\nstderr: {stderr}"
)));
}
}
*state = Some(ContainerState { id: container_id, running: true, file_counter: 0 });
Ok(())
}
async fn stop(&self) -> Result<(), ExecutionError> {
let mut state = self.state.write().await;
if let Some(s) = state.take() {
info!(container_id = %s.id, "stopping container");
let _ = self
.docker
.remove_container(
&s.id,
Some(RemoveContainerOptions { force: true, ..Default::default() }),
)
.await;
}
Ok(())
}
async fn is_running(&self) -> bool {
self.state.read().await.as_ref().is_some_and(|s| s.running)
}
async fn execute(
&self,
request: ExecutionRequest,
) -> Result<ExecutionResult, ExecutionError> {
let supported = [
ExecutionLanguage::Python,
ExecutionLanguage::JavaScript,
ExecutionLanguage::Command,
];
validate_request(&self.capabilities(), &supported, &request)?;
self.config.validate_sandbox_policy(&request.sandbox)?;
let code = match &request.payload {
ExecutionPayload::Source { code } if code.trim().is_empty() => {
return Err(ExecutionError::InvalidRequest("empty source code".to_string()));
}
ExecutionPayload::Source { code } => code.clone(),
ExecutionPayload::GuestModule { .. } => {
return Err(ExecutionError::InvalidRequest(
"DockerExecutor does not support guest modules".to_string(),
));
}
};
if self.config.auto_start && !self.is_running().await {
self.start().await?;
}
let (container_id, filename) = {
let mut state = self.state.write().await;
let s = state.as_mut().ok_or_else(|| {
ExecutionError::ExecutionFailed(
"container not started — call start() first".to_string(),
)
})?;
s.file_counter += 1;
let ext = Self::file_extension(&request.language);
let filename = format!("{}/code_{}.{ext}", self.config.work_dir, s.file_counter);
(s.id.clone(), filename)
};
let start = Instant::now();
self.write_file(&container_id, &filename, &code).await?;
if let Some(ref input) = request.input {
let input_json = serde_json::to_string(input).unwrap_or_default();
let input_path = format!("{}/input.json", self.config.work_dir);
self.write_file(&container_id, &input_path, &input_json).await?;
}
let exec_cmd = Self::exec_command(&request.language, &filename);
if exec_cmd.is_empty() {
return Err(ExecutionError::UnsupportedLanguage(format!("{}", request.language)));
}
debug!(
container_id = %container_id,
language = %request.language,
filename = %filename,
"executing code in container"
);
let exec_env = host_process::allowlisted_host_env(&request.sandbox.environment)
.into_iter()
.map(|(name, value)| format!("{name}={}", value.to_string_lossy()))
.collect::<Vec<_>>();
let ExecOutput { stdout, stderr, exit_code, stdout_truncated, stderr_truncated } = self
.exec_in_container(
&container_id,
&exec_cmd,
Some(exec_env),
Some(request.sandbox.timeout),
(request.sandbox.max_stdout_bytes, request.sandbox.max_stderr_bytes),
)
.await?;
let duration_ms = start.elapsed().as_millis() as u64;
let (structured_output, display_stdout) = extract_structured_output(&stdout);
let status = match exit_code {
Some(0) => ExecutionStatus::Success,
_ => ExecutionStatus::Failed,
};
info!(
exit_code = ?exit_code,
duration_ms,
has_structured_output = structured_output.is_some(),
"container execution completed"
);
Ok(ExecutionResult {
status,
stdout: display_stdout,
stderr,
output: structured_output,
exit_code: exit_code.map(|c| c as i32),
stdout_truncated,
stderr_truncated,
duration_ms,
metadata: None,
})
}
}
impl Drop for DockerExecutor {
fn drop(&mut self) {
if self.config.auto_remove {
if let Some(state) = self.state.get_mut().take() {
let docker = self.docker.clone();
let container_id = state.id;
match tokio::runtime::Handle::try_current() {
Ok(handle) => {
handle.spawn(async move {
let _ = docker
.remove_container(
&container_id,
Some(RemoveContainerOptions {
force: true,
..Default::default()
}),
)
.await;
});
}
Err(_) => {
tracing::warn!(
container_id = %container_id,
"no tokio runtime available during DockerExecutor drop, \
container may leak. Call cleanup() explicitly before dropping."
);
}
}
}
}
}
}
fn base64_encode(data: &[u8]) -> String {
const CHARS: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
let mut result = String::with_capacity(data.len().div_ceil(3) * 4);
for chunk in data.chunks(3) {
let b0 = chunk[0] as u32;
let b1 = chunk.get(1).copied().unwrap_or(0) as u32;
let b2 = chunk.get(2).copied().unwrap_or(0) as u32;
let triple = (b0 << 16) | (b1 << 8) | b2;
result.push(CHARS[((triple >> 18) & 0x3F) as usize] as char);
result.push(CHARS[((triple >> 12) & 0x3F) as usize] as char);
if chunk.len() > 1 {
result.push(CHARS[((triple >> 6) & 0x3F) as usize] as char);
} else {
result.push('=');
}
if chunk.len() > 2 {
result.push(CHARS[(triple & 0x3F) as usize] as char);
} else {
result.push('=');
}
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_container_drops_privileges_and_limits_processes_unless_opted_out() {
let docker = Docker::connect_with_local_defaults().expect("client");
let hardened = DockerExecutor::with_docker(DockerConfig::python(), docker.clone());
let host = hardened.host_config();
assert_eq!(host.cap_drop, Some(vec!["ALL".to_string()]));
assert_eq!(host.security_opt, Some(vec!["no-new-privileges".to_string()]));
assert_eq!(host.pids_limit, Some(i64::from(DEFAULT_PIDS_LIMIT)));
assert_eq!(host.network_mode.as_deref(), Some("none"));
let config = DockerConfig {
drop_capabilities: false,
no_new_privileges: false,
pids_limit: None,
..DockerConfig::python()
};
let host = DockerExecutor::with_docker(config, docker).host_config();
assert_eq!((host.cap_drop, host.security_opt, host.pids_limit), (None, None, None));
}
#[test]
fn exec_commands_record_their_pid_and_keep_it() {
let cmd = with_pid_file("/tmp/x.pid", &["python3".to_string(), "a.py".to_string()]);
assert_eq!(
cmd,
["sh", "-c", r#"echo $$ > "$0" && exec "$@""#, "/tmp/x.pid", "python3", "a.py"]
);
}
#[test]
fn output_past_the_limit_is_discarded_as_it_arrives() {
let (mut buffer, mut truncated) = (String::new(), false);
push_bounded(&mut buffer, &mut truncated, b"abc", 5);
assert_eq!((buffer.as_str(), truncated), ("abc", false));
push_bounded(&mut buffer, &mut truncated, "déf".as_bytes(), 5);
assert_eq!((buffer.as_str(), truncated), ("abcd", true));
push_bounded(&mut buffer, &mut truncated, b"more", 5);
assert_eq!(buffer, "abcd");
}
}
}
#[cfg(feature = "docker")]
pub use docker_impl::DockerExecutor;
#[derive(Debug, Clone)]
pub struct ContainerConfig {
pub runtime: String,
pub default_image: String,
pub extra_flags: Vec<String>,
pub auto_remove: bool,
pub drop_capabilities: bool,
pub no_new_privileges: bool,
pub pids_limit: Option<u32>,
}
pub const DEFAULT_PIDS_LIMIT: u32 = 256;
impl Default for ContainerConfig {
fn default() -> Self {
Self {
runtime: "docker".to_string(),
default_image: "python:3.12-slim".to_string(),
extra_flags: vec![],
auto_remove: true,
drop_capabilities: true,
no_new_privileges: true,
pids_limit: Some(DEFAULT_PIDS_LIMIT),
}
}
}
#[derive(Debug, Clone)]
pub struct ContainerCommandExecutor {
config: ContainerConfig,
}
impl ContainerCommandExecutor {
pub fn new(config: ContainerConfig) -> Self {
Self { config }
}
fn build_run_args(&self, request: &ExecutionRequest, name: &str) -> Vec<String> {
let mut args = vec!["run".to_string()];
if self.config.auto_remove {
args.push("--rm".to_string());
}
args.push("-i".to_string());
args.push("--name".to_string());
args.push(name.to_string());
if self.config.drop_capabilities {
args.push("--cap-drop=ALL".to_string());
}
if self.config.no_new_privileges {
args.push("--security-opt=no-new-privileges".to_string());
}
if let Some(limit) = self.config.pids_limit {
args.push(format!("--pids-limit={limit}"));
}
match request.sandbox.network {
NetworkPolicy::Disabled => {
args.push("--network=none".to_string());
}
NetworkPolicy::Enabled => {}
}
match &request.sandbox.filesystem {
FilesystemPolicy::None => {}
FilesystemPolicy::WorkspaceReadOnly { root } => {
args.push("-v".to_string());
args.push(format!("{}:/workspace:ro", root.display()));
}
FilesystemPolicy::WorkspaceReadWrite { root } => {
args.push("-v".to_string());
args.push(format!("{}:/workspace:rw", root.display()));
}
FilesystemPolicy::Paths { read_only, read_write } => {
for path in read_only {
args.push("-v".to_string());
args.push(format!("{}:{}:ro", path.display(), path.display()));
}
for path in read_write {
args.push("-v".to_string());
args.push(format!("{}:{}:rw", path.display(), path.display()));
}
}
}
if let EnvironmentPolicy::AllowList(vars) = &request.sandbox.environment {
for var in vars {
args.push("--env".to_string());
args.push(var.clone());
}
}
if let Some(ref wd) = request.sandbox.working_directory {
args.push("-w".to_string());
args.push(wd.display().to_string());
}
args.extend(self.config.extra_flags.clone());
args.push(self.config.default_image.clone());
let code = match &request.payload {
ExecutionPayload::Source { code } => code.clone(),
ExecutionPayload::GuestModule { .. } => String::new(),
};
match request.language {
ExecutionLanguage::Python => {
args.push("python3".to_string());
args.push("-c".to_string());
args.push(code);
}
ExecutionLanguage::JavaScript => {
args.push("node".to_string());
args.push("-e".to_string());
args.push(code);
}
ExecutionLanguage::Command => {
args.push("sh".to_string());
args.push("-c".to_string());
args.push(code);
}
_ => {}
}
args.extend(request.argv.clone());
args
}
}
impl Default for ContainerCommandExecutor {
fn default() -> Self {
Self::new(ContainerConfig::default())
}
}
#[async_trait]
impl CodeExecutor for ContainerCommandExecutor {
fn name(&self) -> &str {
"container-command"
}
fn capabilities(&self) -> BackendCapabilities {
BackendCapabilities {
isolation: ExecutionIsolation::ContainerEphemeral,
enforce_network_policy: true,
enforce_filesystem_policy: true,
enforce_environment_policy: true,
enforce_timeout: true,
supports_structured_output: true,
supports_process_execution: true,
supports_persistent_workspace: false,
supports_interactive_sessions: false,
}
}
fn supports_language(&self, lang: &ExecutionLanguage) -> bool {
matches!(
lang,
ExecutionLanguage::Python | ExecutionLanguage::JavaScript | ExecutionLanguage::Command
)
}
async fn execute(&self, request: ExecutionRequest) -> Result<ExecutionResult, ExecutionError> {
let supported =
[ExecutionLanguage::Python, ExecutionLanguage::JavaScript, ExecutionLanguage::Command];
validate_request(&self.capabilities(), &supported, &request)?;
match &request.payload {
ExecutionPayload::Source { code } if code.trim().is_empty() => {
return Err(ExecutionError::InvalidRequest("empty source code".to_string()));
}
ExecutionPayload::Source { .. } => {}
ExecutionPayload::GuestModule { .. } => {
return Err(ExecutionError::InvalidRequest(
"ContainerCommandExecutor does not support guest modules".to_string(),
));
}
}
let start = Instant::now();
let name = container_name("adk-exec");
let run_args = self.build_run_args(&request, &name);
debug!(
runtime = %self.config.runtime,
image = %self.config.default_image,
language = %request.language,
"starting container execution"
);
let stdin = match (&request.input, &request.stdin) {
(Some(input), _) => Some(serde_json::to_vec(input).unwrap_or_default()),
(None, Some(raw)) => Some(raw.clone()),
(None, None) => None,
};
let mut cmd = tokio::process::Command::new(&self.config.runtime);
cmd.args(&run_args)
.stdin(if stdin.is_some() { Stdio::piped() } else { Stdio::null() })
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.kill_on_drop(true);
let child = cmd.spawn().map_err(|e| {
ExecutionError::ExecutionFailed(format!(
"failed to spawn container runtime '{}': {e}",
self.config.runtime
))
})?;
let mut container = RunningContainer::new(&self.config.runtime, &name);
let completed = host_process::run_to_completion(
child,
stdin,
request.sandbox.timeout,
request.sandbox.max_stdout_bytes,
request.sandbox.max_stderr_bytes,
)
.await
.map_err(|e| {
ExecutionError::ExecutionFailed(format!("failed to wait for container: {e}"))
})?;
let Some(output) = completed else {
warn!(container.name = %name, "container execution timed out; killing the container");
container.kill().await;
return Ok(ExecutionResult {
status: ExecutionStatus::Timeout,
stdout: String::new(),
stderr: String::new(),
output: None,
exit_code: None,
stdout_truncated: false,
stderr_truncated: false,
duration_ms: start.elapsed().as_millis() as u64,
metadata: None,
});
};
container.finished();
let duration_ms = start.elapsed().as_millis() as u64;
let raw_stdout = String::from_utf8_lossy(&output.stdout.bytes).into_owned();
let raw_stderr = String::from_utf8_lossy(&output.stderr.bytes).into_owned();
let (stdout, stdout_truncated) =
truncate_output(raw_stdout, request.sandbox.max_stdout_bytes);
let (stderr, stderr_truncated) =
truncate_output(raw_stderr, request.sandbox.max_stderr_bytes);
let stdout_truncated = stdout_truncated || output.stdout.truncated;
let stderr_truncated = stderr_truncated || output.stderr.truncated;
let (structured_output, display_stdout) = extract_structured_output(&stdout);
let status = if output.status.success() {
ExecutionStatus::Success
} else {
ExecutionStatus::Failed
};
info!(
exit_code = output.status.code(),
duration_ms,
has_structured_output = structured_output.is_some(),
"container execution completed"
);
Ok(ExecutionResult {
status,
stdout: display_stdout,
stderr,
output: structured_output,
exit_code: output.status.code(),
stdout_truncated,
stderr_truncated,
duration_ms,
metadata: None,
})
}
}
const KILL_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);
fn container_name(prefix: &str) -> String {
static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
let nanos = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|elapsed| elapsed.subsec_nanos())
.unwrap_or_default();
let counter = NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
format!("{prefix}-{}-{nanos}-{counter}", std::process::id())
}
struct RunningContainer {
runtime: String,
name: String,
running: bool,
}
impl RunningContainer {
fn new(runtime: &str, name: &str) -> Self {
Self { runtime: runtime.to_string(), name: name.to_string(), running: true }
}
fn finished(&mut self) {
self.running = false;
}
async fn kill(&mut self) {
self.running = false;
let kill = tokio::process::Command::new(&self.runtime)
.args(["kill", &self.name])
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.kill_on_drop(true)
.status();
match tokio::time::timeout(KILL_TIMEOUT, kill).await {
Ok(Ok(status)) if status.success() => {}
Ok(Ok(status)) => warn!(container.name = %self.name, ?status, "container kill failed"),
Ok(Err(error)) => warn!(container.name = %self.name, %error, "container kill failed"),
Err(_) => warn!(container.name = %self.name, "container kill did not finish"),
}
}
}
impl Drop for RunningContainer {
fn drop(&mut self) {
if self.running {
let mut kill = std::process::Command::new(&self.runtime);
kill.args(["kill", &self.name])
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null());
std::thread::spawn(move || {
let _ = kill.status();
});
}
}
}
fn truncate_output(output: String, max_bytes: usize) -> (String, bool) {
if output.len() <= max_bytes {
(output, false)
} else {
let truncated = output
.char_indices()
.take_while(|(i, _)| *i < max_bytes)
.map(|(_, c)| c)
.collect::<String>();
(truncated, true)
}
}
fn extract_structured_output(stdout: &str) -> (Option<serde_json::Value>, String) {
let trimmed = stdout.trim_end();
if trimmed.is_empty() {
return (None, String::new());
}
if let Some(last_newline_pos) = trimmed.rfind('\n') {
let last_line = &trimmed[last_newline_pos + 1..];
let before = &trimmed[..last_newline_pos];
if let Ok(value) = serde_json::from_str::<serde_json::Value>(last_line) {
return (Some(value), before.to_string());
}
} else if let Ok(value) = serde_json::from_str::<serde_json::Value>(trimmed) {
return (Some(value), String::new());
}
(None, stdout.to_string())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn capabilities_are_container_ephemeral() {
let executor = ContainerCommandExecutor::default();
let caps = executor.capabilities();
assert_eq!(caps.isolation, ExecutionIsolation::ContainerEphemeral);
assert!(caps.enforce_network_policy);
assert!(caps.enforce_filesystem_policy);
assert!(caps.enforce_environment_policy);
assert!(caps.enforce_timeout);
assert!(caps.supports_structured_output);
assert!(caps.supports_process_execution);
assert!(!caps.supports_persistent_workspace);
assert!(!caps.supports_interactive_sessions);
}
#[test]
fn supports_python_js_command() {
let executor = ContainerCommandExecutor::default();
assert!(executor.supports_language(&ExecutionLanguage::Python));
assert!(executor.supports_language(&ExecutionLanguage::JavaScript));
assert!(executor.supports_language(&ExecutionLanguage::Command));
assert!(!executor.supports_language(&ExecutionLanguage::Rust));
assert!(!executor.supports_language(&ExecutionLanguage::Wasm));
}
#[test]
fn default_config() {
let config = ContainerConfig::default();
assert_eq!(config.runtime, "docker");
assert_eq!(config.default_image, "python:3.12-slim");
assert!(config.extra_flags.is_empty());
assert!(config.auto_remove);
}
#[test]
fn build_run_args_basic_python() {
let executor = ContainerCommandExecutor::default();
let request = ExecutionRequest {
language: ExecutionLanguage::Python,
payload: ExecutionPayload::Source { code: "print('hello')".to_string() },
argv: vec![],
stdin: None,
input: None,
sandbox: crate::SandboxPolicy::strict_rust(),
identity: None,
};
let args = executor.build_run_args(&request, "adk-exec-test");
assert!(args.contains(&"run".to_string()));
assert!(args.contains(&"--rm".to_string()));
assert!(args.contains(&"-i".to_string()));
assert!(args.contains(&"--network=none".to_string()));
assert!(args.contains(&"python3".to_string()));
assert!(args.contains(&"-c".to_string()));
assert!(args.contains(&"print('hello')".to_string()));
}
#[test]
fn build_run_args_with_network_enabled() {
let executor = ContainerCommandExecutor::default();
let mut sandbox = crate::SandboxPolicy::strict_rust();
sandbox.network = NetworkPolicy::Enabled;
let request = ExecutionRequest {
language: ExecutionLanguage::Python,
payload: ExecutionPayload::Source { code: "print('hello')".to_string() },
argv: vec![],
stdin: None,
input: None,
sandbox,
identity: None,
};
let args = executor.build_run_args(&request, "adk-exec-test");
assert!(!args.contains(&"--network=none".to_string()));
}
fn python_request(code: &str, timeout: std::time::Duration) -> ExecutionRequest {
let mut sandbox = crate::SandboxPolicy::strict_rust();
sandbox.timeout = timeout;
ExecutionRequest {
language: ExecutionLanguage::Python,
payload: ExecutionPayload::Source { code: code.to_string() },
argv: vec![],
stdin: None,
input: None,
sandbox,
identity: None,
}
}
#[test]
fn build_run_args_names_and_hardens_the_container_unless_opted_out() {
let request = python_request("print(1)", std::time::Duration::from_secs(5));
let args = ContainerCommandExecutor::default().build_run_args(&request, "adk-exec-test");
let name_at = args.iter().position(|arg| arg == "--name").expect("--name");
assert_eq!(args[name_at + 1], "adk-exec-test");
for flag in ["--cap-drop=ALL", "--security-opt=no-new-privileges", "--pids-limit=256"] {
assert!(args.contains(&flag.to_string()), "{flag} missing from {args:?}");
}
let open = ContainerCommandExecutor::new(ContainerConfig {
drop_capabilities: false,
no_new_privileges: false,
pids_limit: None,
..ContainerConfig::default()
});
let args = open.build_run_args(&request, "adk-exec-test");
assert!(!args.iter().any(|arg| arg.starts_with("--cap-drop")
|| arg.starts_with("--security-opt")
|| arg.starts_with("--pids-limit")));
}
#[cfg(unix)]
fn logging_runtime(dir: &std::path::Path) -> (std::path::PathBuf, std::path::PathBuf) {
use std::os::unix::fs::PermissionsExt;
let log = dir.join("runtime.log");
let runtime = dir.join("runtime");
std::fs::write(
&runtime,
format!(
"#!/bin/sh\necho \"$*\" >> '{}'\ncase \"$1\" in run) exec sleep 30 ;; esac\n",
log.display()
),
)
.unwrap();
std::fs::set_permissions(&runtime, std::fs::Permissions::from_mode(0o755)).unwrap();
std::process::Command::new(&runtime).arg("warm-up").status().unwrap();
(runtime, log)
}
#[cfg(unix)]
fn killed_container(log: &std::path::Path) -> (Option<String>, bool) {
let lines: Vec<String> =
std::fs::read_to_string(log).unwrap_or_default().lines().map(str::to_string).collect();
let name = lines.iter().find(|line| line.starts_with("run ")).and_then(|line| {
let words: Vec<&str> = line.split_whitespace().collect();
words.windows(2).find(|pair| pair[0] == "--name").map(|pair| pair[1].to_string())
});
let killed = name
.as_ref()
.is_some_and(|name| lines.iter().any(|line| *line == format!("kill {name}")));
(name, killed)
}
#[cfg(unix)]
#[tokio::test]
async fn a_timed_out_container_is_killed_by_name() {
let dir = tempfile::tempdir().unwrap();
let (runtime, log) = logging_runtime(dir.path());
let executor = ContainerCommandExecutor::new(ContainerConfig {
runtime: runtime.display().to_string(),
..ContainerConfig::default()
});
let result = executor
.execute(python_request("while True: pass", std::time::Duration::from_millis(1500)))
.await
.unwrap();
assert_eq!(result.status, ExecutionStatus::Timeout);
let (name, killed) = killed_container(&log);
assert!(
name.is_some() && killed,
"{name:?} not killed: {:?}",
std::fs::read_to_string(&log)
);
}
#[cfg(unix)]
#[tokio::test]
async fn a_dropped_execution_kills_its_container() {
let dir = tempfile::tempdir().unwrap();
let (runtime, log) = logging_runtime(dir.path());
let executor = ContainerCommandExecutor::new(ContainerConfig {
runtime: runtime.display().to_string(),
..ContainerConfig::default()
});
let execution = tokio::spawn(async move {
executor
.execute(python_request("while True: pass", std::time::Duration::from_secs(30)))
.await
});
let mut started = false;
for _ in 0..250 {
tokio::time::sleep(std::time::Duration::from_millis(20)).await;
started = killed_container(&log).0.is_some();
if started {
break;
}
}
assert!(started, "the container never started: {:?}", std::fs::read_to_string(&log));
execution.abort();
let mut killed = false;
for _ in 0..250 {
tokio::time::sleep(std::time::Duration::from_millis(20)).await;
killed = killed_container(&log).1;
if killed {
break;
}
}
assert!(killed, "{:?}", std::fs::read_to_string(&log));
}
#[test]
fn docker_config_presets() {
let py = DockerConfig::python();
assert_eq!(py.image, "python:3.12-slim");
assert!(py.network_disabled);
let node = DockerConfig::node();
assert_eq!(node.image, "node:20-slim");
let custom = DockerConfig::custom("ubuntu:24.04");
assert_eq!(custom.image, "ubuntu:24.04");
}
fn policy(
network: NetworkPolicy,
filesystem: FilesystemPolicy,
environment: EnvironmentPolicy,
) -> SandboxPolicy {
SandboxPolicy { network, filesystem, environment, ..SandboxPolicy::strict_rust() }
}
fn assert_rejected(config: &DockerConfig, policy: &SandboxPolicy, mentions: &str) {
match config.validate_sandbox_policy(policy) {
Err(ExecutionError::UnsupportedPolicy(message)) => {
assert!(message.contains(mentions), "{message}");
}
other => panic!("expected UnsupportedPolicy mentioning `{mentions}`, got {other:?}"),
}
}
#[test]
fn docker_policy_default_config_satisfies_strict_policy() {
assert_eq!(
DockerConfig::python().validate_sandbox_policy(&SandboxPolicy::strict_rust()),
Ok(())
);
}
#[test]
fn docker_policy_rejects_disabled_network_on_networked_container() {
let config = DockerConfig::python().with_network();
assert_rejected(&config, &SandboxPolicy::strict_rust(), "network");
let allows_network =
policy(NetworkPolicy::Enabled, FilesystemPolicy::None, EnvironmentPolicy::None);
assert_eq!(config.validate_sandbox_policy(&allows_network), Ok(()));
}
#[test]
fn docker_policy_allows_enabled_network_on_isolated_container() {
let allows_network =
policy(NetworkPolicy::Enabled, FilesystemPolicy::None, EnvironmentPolicy::None);
assert_eq!(DockerConfig::python().validate_sandbox_policy(&allows_network), Ok(()));
}
#[test]
fn docker_policy_rejects_any_mount_without_filesystem_access() {
let config = DockerConfig::python().bind_mount("/srv/data:/data:ro");
assert_rejected(&config, &SandboxPolicy::strict_rust(), "/srv/data:/data:ro");
}
#[test]
fn docker_policy_read_only_workspace_requires_read_only_mounts_under_root() {
let read_only = policy(
NetworkPolicy::Disabled,
FilesystemPolicy::WorkspaceReadOnly { root: PathBuf::from("/srv") },
EnvironmentPolicy::None,
);
let ro_under_root = DockerConfig::python().bind_mount("/srv/data:/data:ro");
assert_eq!(ro_under_root.validate_sandbox_policy(&read_only), Ok(()));
let rw_under_root = DockerConfig::python().bind_mount("/srv/data:/data");
assert_rejected(&rw_under_root, &read_only, "/srv/data:/data");
let ro_outside_root = DockerConfig::python().bind_mount("/home/user/.aws:/aws:ro");
assert_rejected(&ro_outside_root, &read_only, "/home/user/.aws");
let escapes_root = DockerConfig::python().bind_mount("/srv/../etc:/etc-host:ro");
assert_rejected(&escapes_root, &read_only, "/srv/../etc");
let sibling_prefix = DockerConfig::python().bind_mount("/srv-secrets:/s:ro");
assert_rejected(&sibling_prefix, &read_only, "/srv-secrets");
let named_volume = DockerConfig::python().bind_mount("cache:/cache:ro");
assert_rejected(&named_volume, &read_only, "cache:/cache:ro");
}
#[test]
fn docker_policy_read_write_workspace_accepts_any_mode_under_root() {
let read_write = policy(
NetworkPolicy::Disabled,
FilesystemPolicy::WorkspaceReadWrite { root: PathBuf::from("/srv") },
EnvironmentPolicy::None,
);
let config = DockerConfig::python()
.bind_mount("/srv/data:/data")
.bind_mount("/srv/models:/models:ro");
assert_eq!(config.validate_sandbox_policy(&read_write), Ok(()));
}
#[test]
fn docker_policy_explicit_paths_match_mode() {
let paths = policy(
NetworkPolicy::Disabled,
FilesystemPolicy::Paths {
read_only: vec![PathBuf::from("/srv/models")],
read_write: vec![PathBuf::from("/srv/out")],
},
EnvironmentPolicy::None,
);
let config = DockerConfig::python()
.bind_mount("/srv/models:/models:ro,z")
.bind_mount("/srv/out:/out:rw");
assert_eq!(config.validate_sandbox_policy(&paths), Ok(()));
let writable_read_only_path = DockerConfig::python().bind_mount("/srv/models:/models");
assert_rejected(&writable_read_only_path, &paths, "/srv/models:/models");
}
#[test]
fn docker_policy_container_environment_must_be_allowlisted() {
let config = DockerConfig::python().env("API_TOKEN=secret").env("MODE");
assert_rejected(&config, &SandboxPolicy::strict_rust(), "API_TOKEN");
let only_mode = policy(
NetworkPolicy::Disabled,
FilesystemPolicy::None,
EnvironmentPolicy::AllowList(vec!["MODE".to_string()]),
);
assert_rejected(&config, &only_mode, "API_TOKEN");
let both = policy(
NetworkPolicy::Disabled,
FilesystemPolicy::None,
EnvironmentPolicy::AllowList(vec!["API_TOKEN".to_string(), "MODE".to_string()]),
);
assert_eq!(config.validate_sandbox_policy(&both), Ok(()));
}
#[test]
fn docker_config_builder_methods() {
let config = DockerConfig::python()
.pip_install(&["numpy", "pandas"])
.with_network()
.env("MY_VAR=hello")
.bind_mount("/host/data:/data:ro");
assert!(!config.network_disabled);
assert_eq!(config.setup_commands.len(), 1);
assert!(config.setup_commands[0].contains("numpy"));
assert_eq!(config.environment, vec!["MY_VAR=hello"]);
assert_eq!(config.bind_mounts, vec!["/host/data:/data:ro"]);
}
}