# syntax=docker/dockerfile:1.3

ARG BASE_IMAGE=debian:stable-slim

FROM ${BASE_IMAGE} AS builder
ARG ARCH=x86_64
ARG HTTPS_CRYPTO=rustls
ARG ALIYUN=true
ARG TPM_PCA_PLUGIN=true

WORKDIR /usr/src/kbs
COPY . .

RUN apt update && apt install -y protobuf-compiler git libudev-dev curl make gcc openssl libssl-dev clang openssh-client pkg-config

# Install Rust
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
ENV PATH="/root/.cargo/bin:${PATH}"
RUN rustup component add rustfmt clippy

# Build and Install KBS
RUN cd kbs && make AS_FEATURE=coco-as-grpc ALIYUN=${ALIYUN} TPM_PCA_PLUGIN=${TPM_PCA_PLUGIN} && \
    make install-kbs

FROM ${BASE_IMAGE}

RUN apt update && apt install -y openssl curl pkg-config

COPY --from=builder /usr/local/bin/kbs /usr/local/bin/kbs
COPY --from=builder /usr/src/kbs/kbs/kbs-init/kbs-init /usr/local/bin/coco-kbs-init
COPY --from=builder /usr/src/kbs/kbs/kbs-init/kbs-watcher /usr/local/bin/coco-kbs-secret-watcher