Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

CodeBuild — a security_opt hook for the agent container (MINISTACK_CODEBUILD_DOCKER_FLAGS) (#1612)

mm-salesqueze41aaf28594d7
diff --git a/ministack/services/codebuild.py b/ministack/services/codebuild.py
index c71b8b7b..2d158f95 100644
--- a/ministack/services/codebuild.py
+++ b/ministack/services/codebuild.py
@@ -194,6 +194,20 @@ AGENT_IMAGE = "public.ecr.aws/codebuild/local-builds:latest"
# agent container; not an AWS concept, so not configurable.
WORKSPACE = "/tmp/ministack-codebuild"
+# Extra `docker run` flags for the AGENT container, with the same syntax and the
+# same parser as LAMBDA_DOCKER_FLAGS.
+#
+# The agent's entire job is to drive the Docker socket it is handed, so on an
+# SELinux-enforcing host it needs `--security-opt label=disable` to function at
+# all: it is otherwise denied both the socket connect and its own env-file
+# mount, and `:z` does not help because relabelling the socket file is a
+# different permission from connecting to it.
+#
+# Without this hook there is no configuration that makes CodeBuild work under
+# SELinux, and the failure is silent — the agent exits 0 after starting nothing,
+# and _execute_build reads that exit code as SUCCEEDED.
+CODEBUILD_DOCKER_FLAGS = os.environ.get("MINISTACK_CODEBUILD_DOCKER_FLAGS", "")
+
_PHASE_COMPLETE_RE = re.compile(r"Phase complete: ([A-Z_]+) State: ([A-Z_]+)")
_docker = None
@@ -391,8 +405,7 @@ def _execute_build(build_id, project):
_record_phase(build, "QUEUED", "SUCCEEDED")
build["buildStatus"] = "IN_PROGRESS"
- container = client.containers.run(
- AGENT_IMAGE,
+ run_kwargs = dict(
detach=True,
environment={
"LOCAL_AGENT_IMAGE_NAME": AGENT_IMAGE,
@@ -411,6 +424,21 @@ def _execute_build(build_id, project):
},
labels=container_reaper.own_labels("codebuild", **{"ministack.codebuild.build": build_id}),
)
+
+ if CODEBUILD_DOCKER_FLAGS:
+ # Imported here rather than at module scope: the services package
+ # loads its modules eagerly, and a top-level cross-service import
+ # would order codebuild after lambda.
+ from ministack.services.lambda_svc import _parse_docker_flags
+
+ extra = _parse_docker_flags(CODEBUILD_DOCKER_FLAGS)
+ # The volumes above are what the agent contract requires; a --volume
+ # in the flags would silently replace them, so it is dropped.
+ extra.pop("mounts", None)
+ run_kwargs["environment"].update(extra.pop("environment", {}))
+ run_kwargs.update(extra)
+
+ container = client.containers.run(AGENT_IMAGE, **run_kwargs)
except Exception:
logger.exception("Failed to start build %s", build_id)
_finish_build(build, "FAULT")
diff --git a/ministack/services/lambda_svc.py b/ministack/services/lambda_svc.py
index 160d7e99..191535ee 100644
--- a/ministack/services/lambda_svc.py
+++ b/ministack/services/lambda_svc.py
@@ -3831,6 +3831,7 @@ def _parse_docker_flags(flags: str) -> dict:
parser.add_argument("--shm-size")
parser.add_argument("--tmpfs", action="append", default=[])
parser.add_argument("--add-host", action="append", default=[])
+ parser.add_argument("--security-opt", action="append", default=[])
parser.add_argument("--privileged", action="store_true")
parser.add_argument("--read-only", action="store_true")
args, _ = parser.parse_known_args(shlex.split(flags))
@@ -3864,6 +3865,8 @@ def _parse_docker_flags(flags: str) -> dict:
kwargs["mem_limit"] = args.memory
if args.shm_size:
kwargs["shm_size"] = args.shm_size
+ if args.security_opt:
+ kwargs["security_opt"] = args.security_opt
if args.privileged:
kwargs["privileged"] = True
if args.read_only: