FROM 086679231553.dkr.ecr.us-east-1.amazonaws.com/docker-parent-images:python311 as base

USER root

# Run updates
RUN apt-get -y update \
  && apt-get -y install git \
  && apt-get -y upgrade \
  && apt-get -y clean \
  && rm -rf /var/lib/apt/lists/*

# Configure environment.
USER worker
WORKDIR /app
ENV PATH="/app/.virtualenv/bin:${PATH}"

# Create SSH directory
RUN mkdir -p /home/worker/.ssh

# Create virtualenv.
RUN python -m venv /app/.virtualenv

# Install dependencies.
COPY requirements.txt ./
RUN pip install --upgrade pip setuptools \
  && pip install -r ./requirements.txt

# Deploy application.
COPY policies/ ./policies
COPY terraform-scanner/ ./terraform-scanner
COPY checkov.yaml entrypoint.sh ./

# Configure environment variables.
ENV REPO_DIR="/repository" \
  GITHUB_API_KEY="" \
  GITHUB_REPO_NAME="" \
  GITHUB_PR_NUM=""

CMD ["/app/entrypoint.sh"]

FROM base as unit-test

USER root

RUN apt-get -y update \
  && apt-get -y install jq \
  && apt-get -y clean \
  && rm -rf /var/lib/apt/lists/* 

USER worker

COPY tests/ ./tests
COPY run-tests.sh ./

CMD ["/app/run-tests.sh"]

FROM base as module-integration-tests

USER root

RUN apt-get -y update \
  && apt-get -y install git \
  && apt-get -y clean \
  && rm -rf /var/lib/apt/lists/* 

USER worker

COPY download-modules.sh ./
RUN --mount=type=secret,id=GITHUB_APP_CLIENT_ID,uid=1010 \
    --mount=type=secret,id=GITHUB_APP_INSTALLATION_ID,uid=1010 \
    --mount=type=secret,id=GITHUB_APP_PRIVATE_KEY,uid=1010 \
    export GITHUB_APP_CLIENT_ID=$(cat /run/secrets/GITHUB_APP_CLIENT_ID) && \
    export GITHUB_APP_INSTALLATION_ID=$(cat /run/secrets/GITHUB_APP_INSTALLATION_ID) && \
    export GITHUB_APP_PRIVATE_KEY=$(cat /run/secrets/GITHUB_APP_PRIVATE_KEY) && \
    ./download-modules.sh
COPY run-module-integration-tests.sh ./

CMD ["/app/run-module-integration-tests.sh"]
