# syntax=docker/dockerfile:1
#
# Urutau 2: static binaries (no CGO, standard library only), two images.
#   --target hub    the hub: dashboard, API, probe ingestion (Alpine), cmd/urutau
#   --target probe  the probe only (FROM scratch), cmd/urutau-probe: links just the
#                   probe agent, the tailer and the wire protocol (~7 MB binary)
# The default (last) stage is the hub, so a plain `docker build .` is the hub.

# Build stage.
FROM golang:1.26.11-alpine3.24 AS build
WORKDIR /src
COPY go.mod ./
COPY cmd cmd
COPY internal internal
RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /out/urutau ./cmd/urutau \
 && v=$(sed -n 's/^var Version = "\(.*\)"/\1/p' internal/app/version.go) \
 && CGO_ENABLED=0 go build -trimpath -ldflags="-s -w -X main.version=$v" -o /out/urutau-probe ./cmd/urutau-probe

# Files the scratch image needs: CA certificates (HTTPS to the hub), zoneinfo
# (TZ must match the host's fail2ban log) and an owned empty /state.
FROM alpine:3.24 AS probe-files
RUN apk add --no-cache ca-certificates tzdata \
 && mkdir -p /rootfs/state \
 && chown 10001:10001 /rootfs/state

# Probe image: no shell, no package manager, no listening port. It reads the
# host logs (read-only mounts), sends them to the hub over HTTPS and appends the
# hub's ban decisions to a file. The HEALTHCHECK is the binary itself
# (`urutau probe --check` reads the liveness mark in the state volume).
# Runs unprivileged (uid 10001); the compose file adds the host group adm
# (`group_add: ["4"]`) to read /var/log.
FROM scratch AS probe
COPY --from=probe-files /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=probe-files /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=probe-files --chown=10001:10001 /rootfs/state /state
COPY --from=build /out/urutau-probe /urutau
LABEL org.opencontainers.image.title="Urutau probe" \
      org.opencontainers.image.description="Ultra-light Urutau probe: ships host logs to an Urutau hub" \
      org.opencontainers.image.source="https://git.lucasrguerra.dev.br/ciberfauna/urutau" \
      org.opencontainers.image.licenses="MIT OR Apache-2.0"
ENV URUTAU_STATE_DIR=/state \
    SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt \
    ZONEINFO=/usr/share/zoneinfo
USER 10001:10001
VOLUME /state
HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \
  CMD ["/urutau", "probe", "--check"]
ENTRYPOINT ["/urutau", "probe"]

# Hub image: CA certificates (hub-to-hub downloads: geo database, Tor lists) and
# tzdata. The HEALTHCHECK is the binary itself (`urutau healthcheck`): no curl in
# the image. The hub reads no log file: the probes bring the lines.
FROM alpine:3.24 AS hub
RUN apk add --no-cache ca-certificates tzdata \
 && addgroup -S -g 10001 urutau \
 && adduser -S -u 10001 -G urutau -h /data -s /sbin/nologin urutau \
 && mkdir -p /data \
 && chown urutau:urutau /data
COPY --from=build /out/urutau /usr/local/bin/urutau
LABEL org.opencontainers.image.title="Urutau" \
      org.opencontainers.image.description="Lightweight, always-on threat intelligence for self-hosted servers" \
      org.opencontainers.image.source="https://git.lucasrguerra.dev.br/ciberfauna/urutau" \
      org.opencontainers.image.licenses="MIT OR Apache-2.0"
ENV URUTAU_BIND=0.0.0.0:8790 \
    URUTAU_DATA_DIR=/data
USER urutau
WORKDIR /data
VOLUME /data
EXPOSE 8790
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
  CMD ["urutau", "healthcheck"]
ENTRYPOINT ["urutau"]
CMD ["serve"]
