From 0f6d88b1a1ab92d897b139881e23f0da9dea2ff7 Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar <30073382+mighty840@users.noreply.github.com> Date: Mon, 21 Sep 2026 18:56:45 +0200 Subject: [PATCH] feat(platform): harden the container images (#261) - Agent, dashboard and MCP run as an unprivileged user (uid 10001). The agent writes only to its home (tool caches), /data/compliance-scanner (uploaded artifacts, the OSCAL snapshot; mount a volume there) and /tmp (git checkouts). If a volume created by an older root image is not writable, the entrypoint stops with the chown command that fixes it. - Docker HEALTHCHECKs: agent /api/v1/health, MCP /health, and a new dashboard /health route outside /api (no session needed). #262 makes the probes check dependencies. - Pinned tools: Rust 1.98.1 (installed system-wide, per-user CARGO_HOME), cargo-audit 0.22.2 (`--locked`, compiled in its own stage so the runtime has no C toolchain), syft 1.52.0 and gitleaks 8.21.2 (sha256-verified), semgrep 1.177.0 and ruff 0.16.8. They are recorded in /opt/pruefwerk/tool-versions.json for the evidence pack. - Removed Nix (store bootstrap, nix.conf, entrypoint seeding) and runtimes nothing invokes (Go, PHP, Composer, Ruby, Bundler), and install without recommended packages. The runtime layers shrink from about 1.19 GB to about 0.5 GB compressed. - .dockerignore keeps target/ (24 GB locally), node_modules, .git and .env out of the build context. - docker-compose mounts a data volume instead of a root-owned clone volume. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p --- .dockerignore | 12 ++ Dockerfile.agent | 129 +++++++++++------- Dockerfile.dashboard | 12 +- Dockerfile.mcp | 13 +- .../src/infrastructure/server.rs | 3 + docker-compose.yml | 6 +- docker/agent-entrypoint.sh | 46 +++---- docs/guide/scanning.md | 5 +- 8 files changed, 144 insertions(+), 82 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..a1257eb --- /dev/null +++ b/.dockerignore @@ -0,0 +1,12 @@ +# Build context: sources only. Keeps local builds from sending gigabytes of +# build output to the daemon, and keeps local secrets (.env) out of it. +.git +.claude +target +**/node_modules +docs/.vitepress/dist +docs/.vitepress/cache +.env +.env.* +!.env.example +*.log diff --git a/Dockerfile.agent b/Dockerfile.agent index 2272542..748a74e 100644 --- a/Dockerfile.agent +++ b/Dockerfile.agent @@ -1,3 +1,15 @@ +# Pinned versions of every tool the agent runs. They are also written to +# /opt/pruefwerk/tool-versions.json, so evidence can name the tool that +# produced a finding. Bump a version together with its checksum. +ARG RUST_VERSION=1.98.1 +ARG CARGO_AUDIT_VERSION=0.22.2 +ARG SYFT_VERSION=1.52.0 +ARG SYFT_SHA256=caeedb81fb0491615f1ebd1761e4145d41ee86dd2cc7bf80669f9f5ad9d6133d +ARG GITLEAKS_VERSION=8.21.2 +ARG GITLEAKS_SHA256=5bc41815076e6ed6ef8fbecc9d9b75bcae31f39029ceb55da08086315316e3ba +ARG SEMGREP_VERSION=1.177.0 +ARG RUFF_VERSION=0.16.8 + FROM rust:1.94-bookworm AS builder WORKDIR /app @@ -13,75 +25,94 @@ RUN --mount=type=secret,id=git_token \ fi && \ CARGO_NET_GIT_FETCH_WITH_CLI=true cargo build --release -p compliance-agent -# A throwaway stage that packs a real nix store (store paths + the validity DB) -# into a compressed bootstrap tarball. Only the tarball is copied into the final -# image, so we don't carry a raw /nix copy layer. -FROM nixos/nix:latest AS nixseed -RUN tar -C / -czf /nix-bootstrap.tar.gz nix +# cargo-audit is compiled here, where a C toolchain exists, so the runtime image +# needs none. +FROM rust:1.94-bookworm AS cargo-audit +ARG CARGO_AUDIT_VERSION +RUN cargo install cargo-audit --version "${CARGO_AUDIT_VERSION}" --locked -FROM debian:bookworm-slim -RUN apt-get update && apt-get install -y ca-certificates libssl3 git curl python3 python3-pip npm golang-go php-cli && rm -rf /var/lib/apt/lists/* +# The runtime without the agent binary: every scanner, pinned. Buildable on its +# own (`--target tools`), which is how the tool installs are tested. +FROM debian:bookworm-slim AS tools +ARG RUST_VERSION +ARG CARGO_AUDIT_VERSION +ARG SYFT_VERSION +ARG SYFT_SHA256 +ARG GITLEAKS_VERSION +ARG GITLEAKS_SHA256 +ARG SEMGREP_VERSION +ARG RUFF_VERSION -# Install Cargo (minimal, for cargo metadata / cargo audit / generate-lockfile) -RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain stable --profile minimal -ENV PATH="/root/.cargo/bin:${PATH}" -RUN cargo install cargo-audit +# No recommended packages: python3-pip would otherwise pull in a full C +# toolchain, which nothing at runtime needs. +RUN apt-get update \ + && apt-get install -y --no-install-recommends \ + ca-certificates libssl3 git curl python3 python3-pip npm \ + && rm -rf /var/lib/apt/lists/* -# Install Composer for PHP dependency resolution -RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer +# Cargo resolves Rust lockfiles and metadata for the SBOM stage. Installed +# system-wide so the unprivileged user can run it; its caches go to the user's +# CARGO_HOME below. +ENV RUSTUP_HOME=/usr/local/rustup \ + PATH=/usr/local/cargo/bin:${PATH} +RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs \ + | CARGO_HOME=/usr/local/cargo sh -s -- -y --no-modify-path --profile minimal \ + --default-toolchain "${RUST_VERSION}" -# Install Bundler for Ruby dependency resolution -RUN apt-get update && apt-get install -y ruby && rm -rf /var/lib/apt/lists/* && gem install bundler +RUN curl -sSfL -o /tmp/syft.tar.gz \ + "https://github.com/anchore/syft/releases/download/v${SYFT_VERSION}/syft_${SYFT_VERSION}_linux_amd64.tar.gz" \ + && echo "${SYFT_SHA256} /tmp/syft.tar.gz" | sha256sum -c - \ + && tar -xzf /tmp/syft.tar.gz -C /usr/local/bin syft \ + && rm /tmp/syft.tar.gz -# Install syft for SBOM generation -RUN curl -sSfL https://raw.githubusercontent.com/anchore/syft/main/install.sh | sh -s -- -b /usr/local/bin +RUN curl -sSfL -o /tmp/gitleaks.tar.gz \ + "https://github.com/gitleaks/gitleaks/releases/download/v${GITLEAKS_VERSION}/gitleaks_${GITLEAKS_VERSION}_linux_x64.tar.gz" \ + && echo "${GITLEAKS_SHA256} /tmp/gitleaks.tar.gz" | sha256sum -c - \ + && tar -xzf /tmp/gitleaks.tar.gz -C /usr/local/bin gitleaks \ + && rm /tmp/gitleaks.tar.gz -# Install gitleaks for secret detection -RUN curl -sSfL https://github.com/gitleaks/gitleaks/releases/download/v8.21.2/gitleaks_8.21.2_linux_x64.tar.gz \ - | tar -xz -C /usr/local/bin gitleaks +RUN pip3 install --break-system-packages --no-cache-dir \ + "semgrep==${SEMGREP_VERSION}" "ruff==${RUFF_VERSION}" -# Install semgrep for static analysis -RUN pip3 install --break-system-packages semgrep +RUN mkdir -p /opt/pruefwerk \ + && printf '{"rust":"%s","cargo-audit":"%s","syft":"%s","gitleaks":"%s","semgrep":"%s","ruff":"%s"}\n' \ + "${RUST_VERSION}" "${CARGO_AUDIT_VERSION}" "${SYFT_VERSION}" "${GITLEAKS_VERSION}" \ + "${SEMGREP_VERSION}" "${RUFF_VERSION}" \ + > /opt/pruefwerk/tool-versions.json -# Install ruff for Python linting -RUN pip3 install --break-system-packages ruff - -# Real nix for the firmwerk reproducible-build firmware SBOM. -# -# nix-portable's proot fallback can't run here: user namespaces are blocked by -# the container's default seccomp/apparmor profile, and orca exposes no way to -# relax it. So ship a *real* nix and disable its build sandbox -# (`sandbox = false`) — a plain gcc/make firmware build needs no user namespace, -# so it runs fine under the locked-down profile with no proot involved. -# -# The store is shipped as a bootstrap tarball and seeded onto /nix at first -# start (see docker/agent-entrypoint.sh), so a persistent /nix volume survives -# redeploys. A missing/broken nix just falls back to the analysis-only SBOM. -COPY --from=nixseed /nix-bootstrap.tar.gz /opt/nix-bootstrap.tar.gz -ENV PATH="/nix/var/nix/profiles/default/bin:${PATH}" -RUN mkdir -p /etc/nix && printf '%s\n' \ - 'experimental-features = nix-command flakes' \ - 'sandbox = false' \ - 'build-users-group =' \ - 'substituters = https://cache.nixos.org' \ - 'trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=' \ - > /etc/nix/nix.conf +# The agent runs unprivileged. It writes only to its home (tool caches), the +# data root (uploaded artifacts, the OSCAL snapshot; mount a volume there) and +# /tmp (git checkouts), so the rest of the filesystem can be read-only. +RUN useradd --uid 10001 --user-group --create-home --home-dir /home/app --shell /usr/sbin/nologin app \ + && mkdir -p /data/compliance-scanner/artifacts /data/compliance-scanner/oscal \ + && chown -R app:app /data/compliance-scanner +ENV HOME=/home/app \ + CARGO_HOME=/home/app/.cargo \ + ARTIFACT_STORE_BASE_PATH=/data/compliance-scanner/artifacts \ + BREAKPILOT_SNAPSHOT_DIR=/data/compliance-scanner/oscal +FROM tools +COPY --from=cargo-audit /usr/local/cargo/bin/cargo-audit /usr/local/cargo/bin/cargo-audit COPY --from=builder /app/target/release/compliance-agent /usr/local/bin/compliance-agent -COPY docker/agent-entrypoint.sh /usr/local/bin/agent-entrypoint.sh +COPY --chmod=0755 docker/agent-entrypoint.sh /usr/local/bin/agent-entrypoint.sh # OSCAL control-catalog snapshot: lets control mapping run without the # breakpilot-compliance backend. The entrypoint seeds it into # BREAKPILOT_SNAPSHOT_DIR on start (copy-if-absent). COPY --from=builder /app/compliance-agent/controls/snapshot /opt/breakpilot/oscal-snapshot -RUN chmod +x /usr/local/bin/agent-entrypoint.sh # Copy documentation for the help chat assistant COPY --from=builder /app/README.md /app/README.md COPY --from=builder /app/docs /app/docs ENV HELP_DOCS_PATH=/app +USER app EXPOSE 3001 3002 -# Seeds /nix (fresh volume) from the bootstrap tarball, then runs the agent. -ENTRYPOINT ["/usr/local/bin/agent-entrypoint.sh"] +# Liveness from the agent's own health endpoint (#262 makes it report its +# dependencies too). +HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \ + CMD curl -fsS "http://127.0.0.1:${AGENT_PORT:-3001}/api/v1/health" || exit 1 +# Checks the data directories are writable, seeds the OSCAL snapshot, then runs +# the agent. +ENTRYPOINT ["/usr/local/bin/agent-entrypoint.sh"] diff --git a/Dockerfile.dashboard b/Dockerfile.dashboard index 150581a..463558d 100644 --- a/Dockerfile.dashboard +++ b/Dockerfile.dashboard @@ -19,14 +19,22 @@ RUN --mount=type=secret,id=git_token \ CARGO_NET_GIT_FETCH_WITH_CLI=true dx build --release --package compliance-dashboard FROM debian:bookworm-slim -RUN apt-get update && apt-get install -y ca-certificates libssl3 && rm -rf /var/lib/apt/lists/* +# curl only serves the healthcheck. +RUN apt-get update \ + && apt-get install -y --no-install-recommends ca-certificates libssl3 curl \ + && rm -rf /var/lib/apt/lists/* \ + && useradd --uid 10001 --user-group --no-create-home --shell /usr/sbin/nologin app WORKDIR /app COPY --from=builder /app/target/dx/compliance-dashboard/release/web/compliance-dashboard /app/compliance-dashboard COPY --from=builder /app/target/dx/compliance-dashboard/release/web/public /app/public ENV IP=0.0.0.0 +# Sessions live in Mongo, so the dashboard writes nothing to its filesystem. +USER app EXPOSE 8080 -ENTRYPOINT ["./compliance-dashboard"] +HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \ + CMD curl -fsS "http://127.0.0.1:${PORT:-8080}/health" || exit 1 +ENTRYPOINT ["./compliance-dashboard"] diff --git a/Dockerfile.mcp b/Dockerfile.mcp index 1ea3036..8a6d133 100644 --- a/Dockerfile.mcp +++ b/Dockerfile.mcp @@ -14,13 +14,20 @@ RUN --mount=type=secret,id=git_token \ CARGO_NET_GIT_FETCH_WITH_CLI=true cargo build --release -p compliance-mcp FROM debian:bookworm-slim -RUN apt-get update && apt-get install -y ca-certificates libssl3 && rm -rf /var/lib/apt/lists/* +# curl only serves the healthcheck. +RUN apt-get update \ + && apt-get install -y --no-install-recommends ca-certificates libssl3 curl \ + && rm -rf /var/lib/apt/lists/* \ + && useradd --uid 10001 --user-group --no-create-home --shell /usr/sbin/nologin app COPY --from=builder /app/target/release/compliance-mcp /usr/local/bin/compliance-mcp +ENV MCP_PORT=8090 +# The MCP server writes nothing to its filesystem. +USER app EXPOSE 8090 -ENV MCP_PORT=8090 +HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \ + CMD curl -fsS "http://127.0.0.1:${MCP_PORT}/health" || exit 1 ENTRYPOINT ["compliance-mcp"] - diff --git a/compliance-dashboard/src/infrastructure/server.rs b/compliance-dashboard/src/infrastructure/server.rs index eb61d81..f3135a0 100644 --- a/compliance-dashboard/src/infrastructure/server.rs +++ b/compliance-dashboard/src/infrastructure/server.rs @@ -87,6 +87,9 @@ pub fn server_start(app: fn() -> Element) -> Result<(), DashboardError> { tracing::info!("Dashboard server listening on {addr}"); let router = axum::Router::new() + // Liveness for the container healthcheck; outside `/api/`, so + // it needs no session. + .route("/health", get(|| async { "ok" })) .route("/auth", get(auth_login)) .route("/auth/callback", get(auth_callback)) .route("/logout", get(logout)) diff --git a/docker-compose.yml b/docker-compose.yml index 3b8ce50..b32179a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -23,7 +23,9 @@ services: depends_on: - mongo volumes: - - repos_data:/tmp/compliance-scanner/repos + # Uploaded artifacts and the OSCAL snapshot. A fresh named volume takes + # its ownership from the image, so the unprivileged agent can write it. + - agent_data:/data/compliance-scanner dashboard: build: @@ -60,4 +62,4 @@ services: volumes: mongo_data: - repos_data: + agent_data: diff --git a/docker/agent-entrypoint.sh b/docker/agent-entrypoint.sh index 3e79eff..b925573 100644 --- a/docker/agent-entrypoint.sh +++ b/docker/agent-entrypoint.sh @@ -1,34 +1,30 @@ #!/usr/bin/env bash -# Seed the nix store on first start, then run the agent. -# -# The firmware-SBOM pipeline drives a real `nix` build (Firmwerk NixBackend). -# The image ships the store as a bootstrap tarball rather than baking /nix, so a -# persistent /nix volume (mounted empty on first deploy) gets populated once and -# then survives redeploys. Seeding is best-effort: if it fails, the agent still -# starts and firmware SBOMs fall back to analysis-only. -if [ ! -e /nix/store ]; then - echo "agent-entrypoint: seeding /nix store from image bootstrap..." - mkdir -p /nix - if tar -C / -xzf /opt/nix-bootstrap.tar.gz; then - echo "agent-entrypoint: /nix store seeded." - else - echo "agent-entrypoint: WARN nix seed failed; firmware SBOM will use analysis-only fallback." +# Check the agent can write where it keeps data, seed the OSCAL snapshot, then +# run the agent. +set -euo pipefail + +# The agent runs as an unprivileged user. A volume mounted over a data +# directory keeps the ownership it was created with, so a volume made by an +# older image that ran as root is not writable. Say how to fix it instead of +# failing on the first upload. +for dir in "${ARTIFACT_STORE_BASE_PATH:?}" "${BREAKPILOT_SNAPSHOT_DIR:?}"; do + if ! mkdir -p "$dir" 2>/dev/null || [ ! -w "$dir" ]; then + echo "agent-entrypoint: $dir is not writable by uid $(id -u)." >&2 + echo "agent-entrypoint: fix the volume once with: chown -R $(id -u):$(id -g) " >&2 + exit 1 fi -fi +done # Seed the OSCAL control-catalog snapshot. The image ships a catalog snapshot so # control mapping works without the breakpilot-compliance backend # (BREAKPILOT_BASE_URL unset). Copy-if-absent, so a snapshot refreshed from a # live backend (or a mounted volume) always wins. -SNAP_DIR="${BREAKPILOT_SNAPSHOT_DIR:-/data/compliance-scanner/oscal}" -if [ -d /opt/breakpilot/oscal-snapshot ]; then - mkdir -p "$SNAP_DIR" - for f in /opt/breakpilot/oscal-snapshot/*.json; do - [ -e "$f" ] || continue - if [ ! -e "$SNAP_DIR/$(basename "$f")" ]; then - cp "$f" "$SNAP_DIR/" && echo "agent-entrypoint: seeded $(basename "$f") into $SNAP_DIR" - fi - done -fi +for f in /opt/breakpilot/oscal-snapshot/*.json; do + [ -e "$f" ] || continue + if [ ! -e "$BREAKPILOT_SNAPSHOT_DIR/$(basename "$f")" ]; then + cp "$f" "$BREAKPILOT_SNAPSHOT_DIR/" \ + && echo "agent-entrypoint: seeded $(basename "$f") into $BREAKPILOT_SNAPSHOT_DIR" + fi +done exec compliance-agent "$@" diff --git a/docs/guide/scanning.md b/docs/guide/scanning.md index d01ab86..b4ed22f 100644 --- a/docs/guide/scanning.md +++ b/docs/guide/scanning.md @@ -115,7 +115,10 @@ complete firmware SBOM, build with Firmwerk on your side and import the bundle. A single-tenant or sandboxed deployment can turn them on with `PW_ALLOW_UNTRUSTED_CODE_EXEC=1`; a hardened deployment ignores it. The stages -return later as sandboxed Werkbank jobs. +return later as sandboxed Werkbank jobs. The agent image does not ship Nix, so +the firmware build only runs where the agent itself runs next to a Nix +installation; everywhere else firmware SBOMs stop at analysis unless you +import a Firmwerk bundle. Every external tool also runs with an empty environment apart from a short allowlist (`PATH`, `HOME`, `TMPDIR`, and its own cache variables), so the -- 2.54.1