FROM golang:bookworm AS builder

WORKDIR /src
COPY . .

RUN apt-get update
RUN apt-get install -y --no-install-recommends \
        gcc pkgconf libpcsclite-dev libcap2-bin
RUN make V=1 GO_ENVS="CGO_ENABLED=1" bin/step-ca
RUN setcap CAP_NET_BIND_SERVICE=+eip bin/step-ca

FROM smallstep/step-kms-plugin:bookworm AS kms

FROM smallstep/step-cli:bookworm

COPY --from=builder /src/bin/step-ca /usr/local/bin/step-ca
COPY --from=kms /usr/local/bin/step-kms-plugin /usr/local/bin/step-kms-plugin

USER root
RUN apt-get update
RUN apt-get install -y --no-install-recommends pcscd libpcsclite1
RUN mkdir -p /run/pcscd
RUN chown step:step /run/pcscd
USER step

ENV CONFIGPATH="/home/step/config/ca.json"
ENV PWDPATH="/home/step/secrets/password"

VOLUME ["/home/step"]
STOPSIGNAL SIGTERM
HEALTHCHECK CMD step ca health 2>/dev/null | grep "^ok" >/dev/null

COPY docker/entrypoint.sh /entrypoint.sh

ENTRYPOINT ["/bin/bash", "/entrypoint.sh"]
CMD exec /usr/local/bin/step-ca --password-file $PWDPATH $CONFIGPATH
