# syntax=docker/dockerfile:1
FROM python:3.12-slim

# WARNING: Never set ANTHROPIC_API_KEY or other secrets here.
# Inject secrets at runtime via --env or orchestrator secret management.

ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1

WORKDIR /app

# Install system dependencies
# ripgrep backs the worker's search_code tool (warp/tools.py / security_spec.md §11.2);
# without it search_code fails closed with a "ripgrep not installed" ToolPolicyError.
# iptables/ip6tables + ipset + dnsmasq back the per-tier container egress firewall
# (security_spec.md §4 / §11.4) the entrypoint installs before dropping privileges;
# util-linux provides `setpriv`, used to drop from the root firewall stage to the
# unprivileged worker user so the worker cannot tamper with the firewall.
RUN apt-get update && apt-get install -y --no-install-recommends \
    git \
    ripgrep \
    iptables \
    ipset \
    dnsmasq \
    util-linux \
  && rm -rf /var/lib/apt/lists/* \
  && git --version \
  && rg --version \
  && dnsmasq --version \
  && setpriv --help >/dev/null \
  && command -v ipset >/dev/null \
  && command -v iptables >/dev/null
# NOTE: do not run `ipset --version` / `iptables -L` here — they probe the kernel
# netlink, which is unavailable during `docker build` (no NET_ADMIN). Presence is
# checked with `command -v`; functional use happens at runtime where the
# dispatcher grants NET_ADMIN.

# Create non-root user
RUN groupadd --gid 1001 warp && \
    useradd --uid 1001 --gid warp --shell /bin/bash --create-home warp

# Copy source and install (hatchling requires source present for pip install .)
COPY pyproject.toml ./
COPY warp/ ./warp/
RUN pip install --no-cache-dir .

# Copy and configure entrypoints.
# docker-egress-setup.sh (ENTRYPOINT) installs the egress firewall as root then
# drops privileges; docker-entrypoint.sh (CMD) is the unprivileged worker stage.
COPY docker-egress-setup.sh ./docker-egress-setup.sh
COPY docker-entrypoint.sh ./docker-entrypoint.sh
RUN sed -i 's/\r//' /app/docker-egress-setup.sh /app/docker-entrypoint.sh && \
    chmod +x /app/docker-egress-setup.sh /app/docker-entrypoint.sh && \
    chown -R warp:warp /app

# NOTE: the container intentionally starts as root (no `USER warp`). The ENTRYPOINT
# wrapper needs root + NET_ADMIN to install the egress firewall (security_spec.md
# §4), then drops to the `warp` user (uid 1001) via setpriv before exec'ing the
# worker. The dispatcher runs the container with `--user 0 --cap-add=NET_ADMIN`;
# the worker process ends up unprivileged regardless. Do not re-add `USER warp`
# here — it would deny the firewall stage the privileges it needs and the
# entrypoint would fail closed.
ENTRYPOINT ["/app/docker-egress-setup.sh"]
CMD ["/app/docker-entrypoint.sh"]
