Merge dev to main: Security improvements and comprehensive test infrastructure #39
@@ -4,11 +4,20 @@ from config import DOCKER_HOST
|
|||||||
from rbac import require_admin
|
from rbac import require_admin
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
client = docker.DockerClient(base_url=DOCKER_HOST)
|
|
||||||
|
_client = None
|
||||||
|
|
||||||
|
def get_docker_client():
|
||||||
|
"""Get or create Docker client (lazy initialization)."""
|
||||||
|
global _client
|
||||||
|
if _client is None:
|
||||||
|
_client = docker.DockerClient(base_url=DOCKER_HOST)
|
||||||
|
return _client
|
||||||
|
|
||||||
|
|
||||||
@router.get("/containers")
|
@router.get("/containers")
|
||||||
def list_containers():
|
def list_containers():
|
||||||
|
client = get_docker_client()
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
"id": c.short_id,
|
"id": c.short_id,
|
||||||
@@ -26,6 +35,7 @@ def list_containers():
|
|||||||
def container_action(container_id: str, action: str):
|
def container_action(container_id: str, action: str):
|
||||||
if action not in ("start", "stop", "restart"):
|
if action not in ("start", "stop", "restart"):
|
||||||
return {"error": "invalid action"}
|
return {"error": "invalid action"}
|
||||||
|
client = get_docker_client()
|
||||||
c = client.containers.get(container_id)
|
c = client.containers.get(container_id)
|
||||||
getattr(c, action)()
|
getattr(c, action)()
|
||||||
return {"ok": True}
|
return {"ok": True}
|
||||||
@@ -33,5 +43,6 @@ def container_action(container_id: str, action: str):
|
|||||||
|
|
||||||
@router.get("/containers/{container_id}/logs")
|
@router.get("/containers/{container_id}/logs")
|
||||||
def container_logs(container_id: str, tail: int = Query(200, le=10000)):
|
def container_logs(container_id: str, tail: int = Query(200, le=10000)):
|
||||||
|
client = get_docker_client()
|
||||||
c = client.containers.get(container_id)
|
c = client.containers.get(container_id)
|
||||||
return {"logs": c.logs(tail=tail, timestamps=True).decode(errors="replace")}
|
return {"logs": c.logs(tail=tail, timestamps=True).decode(errors="replace")}
|
||||||
|
|||||||
Reference in New Issue
Block a user