Using environmental variable substitution with containerized environments
export MY_UID="SOAsfj_lIg0VenDr83gjDw"
export MY_USER="keeper://${MY_UID}/field/login"
export MY_PASS="keeper://${MY_UID}/field/password"DOCKER_BUILDKIT=1 ksm exec -- \
docker build \
--secret id=my_user,env=MY_USER \
--secret id=my_password,env=MY_PASS \
-t my_image .FROM my_base_image
RUN --mount=type=secret,id=my_user,dst=/my_secrets/my_user \
--mount=type=secret,id=my_password,dst=/my_secrets/my_password \
useradd "$(cat /my_secrets/my_user)" && \
echo "$(cat /my_secrets/my_user)":"$(cat /my_secrets/my_password)" | chpasswdexport MY_UID="SOAsfj_lIg0VenDr83gjDw"
export MY_USER="keeper://${MY_UID}/field/login"
export MY_PASS="keeper://${MY_UID}/field/password"ksm exec --inline -- \
docker build \
--build-arg "BUILD_MY_USER=${MY_USER}" \
--build-arg "BUILD_MY_PASSWORD=${MY_PASSWORD}" \
-t my_image .FROM my_base_image
​ARG BUILD_MY_USER
ARG BUILD_MY_PASSWORD
RUN useradd "$(printenv --null BUILD_MY_USER)" && \
echo "$(printenv --null BUILD_MY_USER)":"$(printenv --null BUILD_MY_PASSWORD)" | chpasswd---
version: "3"
services:
my_app:
build:
content: "."ksm exec --inline -- \
docker-compose build \
--build-arg "BUILD_MY_USER=${MY_USER}" \
--build-arg "BUILD_MY_PASSWORD=${MY_PASSWORD}" \
-t my_image .FROM my_base_image
​ARG BUILD_MY_USER
ARG BUILD_MY_PASSWORD
RUN useradd "$(printenv --null BUILD_MY_USER)" && \
echo "$(printenv --null BUILD_MY_USER)":"$(printenv --null BUILD_MY_PASSWORD)" | chpasswdFROM tomcat:10-jdk16
​
ARG BUILD_KSM_INI_CONFIG
ARG BUILD_KSM_SERVER_UID
# The names of our files in our secret record
ARG BUILD_SERVER_CONFIG="server.xml"
ARG BUILD_KEYSTORE="localhost-rsa.jks"
# Temporarily install Python3
RUN apt-get update -y && \
apt-get install -y \
python3 \
python3-pip \
python3-venv
​
# Install modules in a known place so we can remove them later.
ENV VIRTUAL_ENV /venv
RUN python3 -m pip install --upgrade pip && \
python3 -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
​
# Upgrade pip since the distro's python might be old enough that it doesn't like to install newer modules.
RUN pip3 install --upgrade pip
RUN pip3 install keeper_secrets_manager_cli
​
RUN echo ${BUILD_KSM_INI_CONFIG}
​
# Import the KSM Client Device configuration, decode it, and store it a place where ksm can find it.
RUN ksm profile import $(printenv --null BUILD_KSM_INI_CONFIG)
​
# Download the server.xml and keystore into the Tomcat conf directory
RUN ksm secret download -u ${BUILD_KSM_SERVER_UID} --name ${BUILD_SERVER_CONFIG} --file-output /usr/local/tomcat/conf/server.xml
RUN ksm secret download -u ${BUILD_KSM_SERVER_UID} --name ${BUILD_KEYSTORE} --file-output /usr/local/tomcat/conf/localhost-rsa.jks
​
# We no longer need ksm. Remove it, python, and Debian apt to make a smaller Docker image.
RUN rm -rf /venv keeper.ini
RUN apt-get purge -y \
python3 \
python3-pip \
python3-venv && \
apt-get clean autoclean && \
apt-get autoremove -y && \
rm -rf /var/lib/{apt,dpkg,cache,log}/
​
# Expose port 8443 for SSL
EXPOSE 8443#!/bin/sh
# Export the KSM profile as a string
export CF=$(ksm profile export _default)
# Execute the docker build, passing in the Record UID
# that contains the secret files
docker build \
--build-arg "BUILD_KSM_INI_CONFIG=${CF}" \
--build-arg "BUILD_KSM_SERVER_UID=LdRkidFLPF7vDaogwJ7etQ" \
-t ksm_tomcat .

