# agentspm — UBI9-minimal container image (B10, reference deployment).
#
# The PRIMARY deployment mode remains the wheel on a bastion host
# (docs/ARCHITECTURE.md, "Evidence & deployment"); this image is the container
# reference for it and for the CronJob example in deploy/.
#
# Build (network access to the package index and the OPA download host is
# required at BUILD time only; the resulting image needs no index access):
#   podman build -f Containerfile -t agentspm:0.1.0 .
#
# Notes:
# * Multi-stage: the wheel and its dependency wheels are built/collected in
#   the builder stage; the runtime stage installs strictly --no-index from
#   that wheelhouse.
# * OPA is a pinned static binary (version + sha256) — `agentspm evaluate`
#   re-verifies the sha256 pin at runtime via AGENTSPM_OPA_SHA256.
# * Runs as a non-root user. No HEALTHCHECK: this is a CLI image, not a
#   long-running service (there is nothing meaningful to probe).
# * The age binary (needed only by `agentspm export`) is NOT bundled: there
#   is no age package in UBI9 base repos, and baking in an unpinned download
#   would undercut the supply-chain posture. Run exports on the host, or
#   extend the builder stage with a sha256-pinned age release of your choice.

# Base je pripnuty TAGEM I DIGESTEM. Duvod obojiho: tag se hybe (dnes je
# ubi-minimal:9.8 totozny s :latest), takze samotny tag znamena, ze se obsah
# image muze zmenit beze zmeny v repu; samotny digest zas neni citelny.
# Digest nize = tag 9.8, overeno 18. 10. 2026 (`docker buildx imagetools inspect`).
# Pri bumpu menit OBOJI zaroven.
#
# Proc to tady vubec resim: base byl pripnuty na 9.4, zatimco opravy jsou v 9.8.x.
# Sken image v CI na tom nasel 40 HIGH CVE v OS balickach, vsechny s dostupnou
# opravou. Pinovani na starou minor verzi neni stabilita, je to odklad patchu.
ARG UBI_TAG=9.8
ARG UBI_DIGEST=sha256:c4c285d42941ebb2bd4da878479008e1e4536bb599aad1dbe568f89de9053661

# --- builder: build the project wheel + collect dependency wheels ------------
FROM registry.access.redhat.com/ubi9/ubi-minimal:${UBI_TAG}@${UBI_DIGEST} AS builder

ARG OPA_VERSION=1.21.4
ARG OPA_SHA256=2ab571f367f293bb19523b76f92f1945bbf245003392c7f392903f95c2b9b630

RUN microdnf install -y python3.12 python3.12-pip \
    && microdnf clean all

WORKDIR /src
COPY pyproject.toml uv.lock README.md LICENSE ./
COPY src/ src/
COPY schema/ schema/

# Build the agentspm wheel and collect the runtime dependency wheels into one
# wheelhouse (the runtime stage installs offline from it). Dependencies are
# resolved from uv.lock (hash-checked), not from loose pyproject ranges — the
# image matches the audited lockfile. uv itself is version-pinned.
RUN python3.12 -m pip install --no-cache-dir uv==0.13.0 \
    && uv export --locked --no-emit-project --no-dev \
         --format requirements.txt -o /tmp/requirements.txt \
    && python3.12 -m pip wheel --wheel-dir /wheels -r /tmp/requirements.txt \
    && python3.12 -m pip wheel --wheel-dir /wheels --no-deps .

# Pinned static OPA binary (sha256 verified at build; re-verified at runtime).
RUN curl -sSfL -o /opa \
      "https://openpolicyagent.org/downloads/v${OPA_VERSION}/opa_linux_amd64_static" \
    && echo "${OPA_SHA256}  /opa" | sha256sum --check \
    && chmod 0755 /opa

# --- runtime ------------------------------------------------------------------
FROM registry.access.redhat.com/ubi9/ubi-minimal:${UBI_TAG}@${UBI_DIGEST}

ARG OPA_SHA256=2ab571f367f293bb19523b76f92f1945bbf245003392c7f392903f95c2b9b630

# `microdnf update` pred instalaci: mezi dvema rebuildy base image se objevi nove
# opravy a bez tohoto kroku by image nesl znamou a opravitelnou CVE jen proto, ze
# digest je starsi nez oprava. PRIZNANY KOMPROMIS: tim prestava byt image bajtove
# reprodukovatelny -- stejny vstup muze dat jiny vystup podle toho, co je zrovna
# v repozitarich. Volim patchovanou verzi; reprodukovatelnost nese digest base
# a lockfile pro Python cast, ne zmrazene systemove baliky.
RUN microdnf update -y \
    && microdnf install -y python3.12 python3.12-pip shadow-utils \
    && useradd --uid 1001 --create-home --home-dir /app \
         --shell /sbin/nologin agentspm \
    && mkdir -p /data && chown 1001:0 /data \
    && microdnf remove -y shadow-utils \
    && microdnf clean all

# Offline install from the builder's wheelhouse — no index access needed here.
# (plain COPY --from keeps the file compatible with non-BuildKit builders)
COPY --from=builder /wheels /tmp/wheels
RUN python3.12 -m pip install --no-cache-dir --no-index --find-links /tmp/wheels agentspm \
    && rm -rf /tmp/wheels

COPY --from=builder /opa /usr/local/bin/opa
# Policy pack ships in the image; override with AGENTSPM_POLICIES_DIR.
COPY policies/ /opt/agentspm/policies/

ENV AGENTSPM_OPA_PATH=/usr/local/bin/opa \
    AGENTSPM_OPA_SHA256=${OPA_SHA256} \
    AGENTSPM_POLICIES_DIR=/opt/agentspm/policies/v0.1 \
    AGENTSPM_EVIDENCE_STORE=/data/evidence \
    AGENTSPM_DB=/data/agentspm.sqlite

# Evidence lives on a mounted volume (encrypted at the platform level).
VOLUME ["/data"]

USER 1001
WORKDIR /app

ENTRYPOINT ["agentspm"]
CMD ["--help"]
