Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions src/seclab_taskflows/mcp_servers/container_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import atexit
import logging
import os
import re
import subprocess
import uuid
from typing import Annotated
Expand All @@ -26,10 +27,27 @@
CONTAINER_IMAGE = os.environ.get("CONTAINER_IMAGE", "")
CONTAINER_WORKSPACE = os.environ.get("CONTAINER_WORKSPACE", "")
CONTAINER_TIMEOUT = int(os.environ.get("CONTAINER_TIMEOUT", "30"))
CONTAINER_PERSIST = os.environ.get("CONTAINER_PERSIST", "").lower() in ("1", "true", "yes")

_DEFAULT_WORKDIR = "/workspace"


def _persistent_name() -> str:
"""Derive a deterministic container name from the image for reuse across tasks."""
slug = re.sub(r"[^a-zA-Z0-9]", "-", CONTAINER_IMAGE).strip("-")[:40]
return f"seclab-persist-{slug}"
Comment thread
anticomputer marked this conversation as resolved.
Outdated


def _is_running(name: str) -> bool:
"""Check if a container with the given name is already running."""
result = subprocess.run(
["docker", "inspect", "-f", "{{.State.Running}}", name],
capture_output=True,
text=True,
)
return result.returncode == 0 and result.stdout.strip() == "true"


def _start_container() -> str:
"""Start the Docker container and return its name."""
if not CONTAINER_IMAGE:
Expand All @@ -38,8 +56,24 @@ def _start_container() -> str:
if CONTAINER_WORKSPACE and ":" in CONTAINER_WORKSPACE:
msg = f"CONTAINER_WORKSPACE must not contain a colon: {CONTAINER_WORKSPACE!r}"
raise RuntimeError(msg)
name = f"seclab-shell-{uuid.uuid4().hex[:8]}"
cmd = ["docker", "run", "-d", "--rm", "--name", name]

if CONTAINER_PERSIST:
name = _persistent_name()
if _is_running(name):
logging.debug(f"Reusing persistent container: {name}")
return name
Comment thread
anticomputer marked this conversation as resolved.
# Remove stopped leftover with the same name (ignore errors)
subprocess.run(
["docker", "rm", "-f", name],
capture_output=True,
text=True,
)
Comment thread
anticomputer marked this conversation as resolved.
Outdated
else:
name = f"seclab-shell-{uuid.uuid4().hex[:8]}"

cmd = ["docker", "run", "-d", "--name", name]
if not CONTAINER_PERSIST:
cmd.append("--rm")
if CONTAINER_WORKSPACE:
cmd += ["-v", f"{CONTAINER_WORKSPACE}:/workspace"]
cmd += [CONTAINER_IMAGE, "tail", "-f", "/dev/null"]
Expand All @@ -48,15 +82,19 @@ def _start_container() -> str:
if result.returncode != 0:
msg = f"docker run failed: {result.stderr.strip()}"
raise RuntimeError(msg)
Comment thread
anticomputer marked this conversation as resolved.
logging.debug(f"Container started: {name}")
logging.debug(f"Container started: {name} (persist={CONTAINER_PERSIST})")
return name


def _stop_container() -> None:
"""Stop the running container."""
"""Stop the running container (skipped for persistent containers)."""
global _container_name
if _container_name is None:
return
if CONTAINER_PERSIST:
logging.debug(f"Leaving persistent container running: {_container_name}")
_container_name = None
return
logging.debug(f"Stopping container: {_container_name}")
result = subprocess.run(
["docker", "stop", "--time", "5", _container_name],
Expand Down
1 change: 1 addition & 0 deletions src/seclab_taskflows/toolboxes/container_shell_base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ server_params:
CONTAINER_IMAGE: "seclab-shell-base:latest"
CONTAINER_WORKSPACE: "{{ env('CONTAINER_WORKSPACE', required=False) }}"
CONTAINER_TIMEOUT: "{{ env('CONTAINER_TIMEOUT', '30') }}"
CONTAINER_PERSIST: "{{ env('CONTAINER_PERSIST', required=False) }}"
LOG_DIR: "{{ env('LOG_DIR') }}"

confirm:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ server_params:
CONTAINER_IMAGE: "seclab-shell-malware-analysis:latest"
CONTAINER_WORKSPACE: "{{ env('CONTAINER_WORKSPACE', required=False) }}"
CONTAINER_TIMEOUT: "{{ env('CONTAINER_TIMEOUT', '60') }}"
CONTAINER_PERSIST: "{{ env('CONTAINER_PERSIST', required=False) }}"
LOG_DIR: "{{ env('LOG_DIR') }}"

confirm:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ server_params:
CONTAINER_IMAGE: "seclab-shell-network-analysis:latest"
CONTAINER_WORKSPACE: "{{ env('CONTAINER_WORKSPACE', required=False) }}"
CONTAINER_TIMEOUT: "{{ env('CONTAINER_TIMEOUT', '30') }}"
CONTAINER_PERSIST: "{{ env('CONTAINER_PERSIST', required=False) }}"
LOG_DIR: "{{ env('LOG_DIR') }}"

confirm:
Expand Down
1 change: 1 addition & 0 deletions src/seclab_taskflows/toolboxes/container_shell_sast.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ server_params:
CONTAINER_IMAGE: "seclab-shell-sast:latest"
CONTAINER_WORKSPACE: "{{ env('CONTAINER_WORKSPACE', required=False) }}"
CONTAINER_TIMEOUT: "{{ env('CONTAINER_TIMEOUT', '60') }}"
CONTAINER_PERSIST: "{{ env('CONTAINER_PERSIST', required=False) }}"
LOG_DIR: "{{ env('LOG_DIR') }}"

confirm:
Expand Down
Loading