# syntax=docker/dockerfile:1.3

# Copyright (c) 2023 by Alibaba.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

FROM openanolis/anolisos:latest as builder
ARG ARCH=x86_64

WORKDIR /usr/src/attestation-service
COPY . .

RUN cd /tmp && \
    curl https://download.01.org/intel-sgx/sgx-dcap/1.21/linux/distro/Anolis86/sgx_rpm_local_repo.tgz --output /tmp/sgx_rpm_local_repo.tgz && \
    tar zxvf /tmp/sgx_rpm_local_repo.tgz && \
    yum -y install yum-utils && yum-config-manager --add-repo file:///tmp/sgx_rpm_local_repo && \
    yum -y install epel-release && \
    yum install -y --setopt=install_weak_deps=False --nogpgcheck libsgx-dcap-quote-verify-devel perl wget curl clang openssl-devel protobuf-devel git tpm2-tss tpm2-tss-devel libudev-devel && \
    yum clean all && \
    rm -rf /tmp/*

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

# Build and Install gRPC attestation-service
RUN cargo install --path attestation-service --bin grpc-as --features grpc-bin --locked


FROM openanolis/anolisos:latest
ARG ARCH=x86_64

RUN cd /tmp && \
    curl https://download.01.org/intel-sgx/sgx-dcap/1.21/linux/distro/Anolis86/sgx_rpm_local_repo.tgz --output /tmp/sgx_rpm_local_repo.tgz && \
    tar zxvf /tmp/sgx_rpm_local_repo.tgz && \
    yum -y install yum-utils && yum-config-manager --add-repo file:///tmp/sgx_rpm_local_repo && \
    yum -y install epel-release && \
    yum install -y --setopt=install_weak_deps=False --nogpgcheck libsgx-dcap-default-qpl libsgx-dcap-quote-verify tpm2-tss tpm2-tss-devel \
        curl gnupg openssl && \
    yum clean all && \
    rm -rf /tmp/*

COPY --from=builder /root/.cargo/bin/grpc-as /usr/local/bin/grpc-as

VOLUME /opt/confidential-containers/attestation-service

CMD ["grpc-as", "--socket", "0.0.0.0:50004"]

EXPOSE 50004