# norcaster-envoy: the Norcaster filter for Envoy (an ext_proc gRPC service), run beside Envoy.
# Red Hat Universal Base Image 9, as Norcaster's own images: free to redistribute while only UBI
# content is added. Every dependency installs from a hash-pinned wheel (docker/lock.sh).
#
# Build from the repository root, which holds the Norcaster SDK the filter is built on (sdk/python):
#   docker build -f packages/norcaster-envoy/Dockerfile -t norcaster-envoy .
FROM registry.access.redhat.com/ubi9/python-312-minimal:9.8-1792840006@sha256:d4a17b4e76d039c59c9517544a12da25522914321abaf0b309dc677ebd0471ff

# The base image runs as 1001; the build steps run as root, the image as 10001 (below). Its group is
# 0: OpenShift runs an image as an arbitrary uid in group 0.
USER 0
RUN useradd --system --uid 10001 --gid 0 --home-dir /home/norcaster --create-home \
       --shell /usr/sbin/nologin norcaster \
    && chgrp -R 0 /home/norcaster && chmod -R g=u /home/norcaster
WORKDIR /app
COPY packages/norcaster-envoy/docker/requirements.txt /tmp/requirements.txt
RUN pip install --no-cache-dir --require-hashes -r /tmp/requirements.txt && rm /tmp/requirements.txt
# The Norcaster SDK (`norcaster`) and the filter, from this repository's source. A release
# (.github/workflows/release-envoy.yml) first checks that these SDK files are the published `norcaster`
# release, byte for byte. Nothing else of the repository goes into the image.
COPY sdk/python/src/norcaster/__init__.py sdk/python/src/norcaster/_media.py sdk/python/src/norcaster/check.py sdk/python/src/norcaster/client.py ./norcaster/
COPY packages/norcaster-envoy/src/norcaster_envoy ./norcaster_envoy
COPY packages/norcaster-envoy/LICENSE /licenses/
COPY sdk/python/LICENSE /licenses/norcaster-sdk-LICENSE

# Nothing is written at run time: the image works on a read-only root filesystem. HOME, USER and
# LOGNAME are set so nothing needs a passwd entry for an arbitrary uid. In a pod beside Envoy the
# filter listens on loopback (127.0.0.1:9002, the default); set NORCASTER_ENVOY_ADDRESS to listen
# elsewhere, on a private network only: the port has no TLS and receives whole prompts.
ENV PYTHONPATH=/app \
    PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    HOME=/home/norcaster \
    USER=norcaster \
    LOGNAME=norcaster

# Red Hat's required labels, replacing the base image's.
ARG NORCASTER_VERSION=dev
ARG NORCASTER_RELEASE=0
LABEL name="norcaster-envoy" \
      vendor="Navego AB" \
      version="${NORCASTER_VERSION}" \
      release="${NORCASTER_RELEASE}" \
      summary="Norcaster filter for Envoy" \
      description="Run beside Envoy: before Envoy calls the AI provider, it asks Norcaster for a decision (allow, hide or block) and Norcaster keeps a record of every answer. An Envoy external processor (ext_proc) on gRPC port 9002." \
      maintainer="Navego AB <security@norcaster.com>" \
      url="https://www.norcaster.com" \
      io.k8s.display-name="Norcaster filter for Envoy" \
      io.k8s.description="Envoy external processor that asks Norcaster before the AI provider is called." \
      io.openshift.tags="norcaster,envoy" \
      org.opencontainers.image.licenses="Apache-2.0" \
      usage=""

EXPOSE 9002
USER 10001
ENTRYPOINT ["python", "-m", "norcaster_envoy"]
