# =============================================================================
# Platform apache — Multi-distro (Rocky Linux 9 par défaut)
#
# Architecture :
#   traffic-gen ─HTTPS→ Apache HTTPD (TLS, HTTP/2) → ClickHouse
#                            ↑
#                         ja4ebpf (uprobe httpd/libssl + hook TC ingress)
#
# ja4ebpf attache ses uprobes sur /usr/sbin/httpd qui lie OpenSSL.
# Le hook TC ingress capture TCP SYN + TLS ClientHello sur eth0.
# =============================================================================

# ── Stage 1 : build ja4ebpf ──────────────────────────────────────────────────
FROM golang:1.24-bookworm AS go-builder

RUN apt-get update && apt-get install -y clang llvm libbpf-dev && rm -rf /var/lib/apt/lists/*

WORKDIR /build
COPY go.work go.work.sum* ./
COPY shared/go/ja4common/go.mod shared/go/ja4common/go.sum* ./shared/go/ja4common/
COPY services/ja4ebpf/go.mod services/ja4ebpf/go.sum* ./services/ja4ebpf/
RUN cd services/ja4ebpf && go mod download 2>/dev/null; true

COPY shared/go/ja4common/ ./shared/go/ja4common/
COPY services/ja4ebpf/ ./services/ja4ebpf/

WORKDIR /build/services/ja4ebpf
RUN GOWORK=off go generate ./internal/loader/ && \
    GOWORK=off CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
    go build -ldflags="-s -w" -o /out/ja4ebpf ./cmd/ja4ebpf/

# ── Stage 2 : runtime Apache HTTPD + ja4ebpf ─────────────────────────────────
ARG BASE_IMAGE=rockylinux:9
FROM ${BASE_IMAGE}

RUN dnf install -y epel-release 2>/dev/null; \
    dnf install -y httpd mod_ssl mod_http2 openssl curl && \
    dnf clean all

COPY --from=go-builder /out/ja4ebpf /usr/local/bin/ja4ebpf

# Certificat TLS auto-signé pour Apache
RUN openssl req -x509 -nodes -days 365 \
        -subj "/CN=platform.test" \
        -newkey rsa:2048 \
        -keyout /etc/pki/tls/private/apache.key \
        -out /etc/pki/tls/certs/apache.crt && \
    mkdir -p /var/www/html /var/log/httpd && \
    echo '{"status":"ok","stack":"apache"}' > /var/www/html/health

COPY tests/integration/apache/platform/httpd-ssl.conf /etc/httpd/conf.d/ssl.conf
COPY tests/integration/apache/platform/ja4ebpf.yml    /etc/ja4ebpf/config.yml
COPY tests/integration/apache/platform/entrypoint.sh  /entrypoint.sh
RUN chmod +x /entrypoint.sh

EXPOSE 80 443
CMD ["/entrypoint.sh"]
