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

USER       root

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

WORKDIR    /opt/terraform

RUN        curl -Lso awscli.zip https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip \
           && unzip awscli.zip \
           && ./aws/install \
           && rm -r aws awscli.zip

ENV        TFSWITCH_VERSION='0.13.1275'
ENV        TF_VERSION='0.13.7'
RUN        curl -Lso tfswitch.tar.gz "https://github.com/warrensbox/terraform-switcher/releases/download/${TFSWITCH_VERSION}/terraform-switcher_${TFSWITCH_VERSION}_linux_amd64.tar.gz" \
           && mkdir tfswitch \
           && tar -xvf tfswitch.tar.gz -C tfswitch \
           && mv tfswitch/tfswitch /usr/local/bin/tfswitch \
           && chmod 755 /usr/local/bin/tfswitch \
           && rm -r tfswitch tfswitch.tar.gz

COPY       terraform_secrets_import.sh /opt/terraform/
COPY       state_file_cleaner.py /opt/terraform/
RUN        chmod +x /opt/terraform/terraform_secrets_import.sh
RUN        useradd -m terraform -s /bin/bash
RUN        chown -R terraform:terraform /opt/terraform
RUN        python3 -m venv venv
ENV        PATH=/opt/terraform/venv/bin:$PATH:/home/terraform/bin
RUN        pip3 install boto3

USER       terraform
RUN        mkdir /home/terraform/.ssh
RUN        ssh-keyscan github.com >> /home/terraform/.ssh/known_hosts
ENTRYPOINT [ "/opt/terraform/terraform_secrets_import.sh" ]
