Explorar el Código

Add local CA trust to Docker containers

Carl Tashian hace 10 meses
padre
commit
78093ad276
Se han modificado 3 ficheros con 43 adiciones y 6 borrados
  1. 3 2
      docker/Dockerfile
  2. 1 2
      docker/Dockerfile.hsm
  3. 39 2
      docker/entrypoint.sh

+ 3 - 2
docker/Dockerfile

@@ -11,11 +11,12 @@ FROM smallstep/step-kms-plugin:cloud AS kms
 
 FROM smallstep/step-cli:latest
 
+USER root
+RUN apk add --no-cache su-exec
+
 COPY --from=builder /src/bin/step-ca /usr/local/bin/step-ca
 COPY --from=kms /usr/local/bin/step-kms-plugin /usr/local/bin/step-kms-plugin
 
-USER step
-
 ENV CONFIGPATH="/home/step/config/ca.json"
 ENV PWDPATH="/home/step/secrets/password"
 

+ 1 - 2
docker/Dockerfile.hsm

@@ -18,10 +18,9 @@ COPY --from=kms /usr/local/bin/step-kms-plugin /usr/local/bin/step-kms-plugin
 
 USER root
 RUN apt-get update
-RUN apt-get install -y --no-install-recommends opensc opensc-pkcs11 pcscd gnutls-bin libpcsclite1 p11-kit yubihsm-pkcs11
+RUN apt-get install -y --no-install-recommends opensc opensc-pkcs11 pcscd gnutls-bin libpcsclite1 p11-kit yubihsm-pkcs11 gosu
 RUN mkdir -p /run/pcscd
 RUN chown step:step /run/pcscd
-USER step
 
 ENV CONFIGPATH="/home/step/config/ca.json"
 ENV PWDPATH="/home/step/secrets/password"

+ 39 - 2
docker/entrypoint.sh

@@ -7,6 +7,28 @@ set -eo pipefail
 
 export STEPPATH=$(step path)
 
+# Determine which tool to use for dropping privileges
+if command -v gosu &> /dev/null; then
+    DROP_PRIV="gosu step"
+elif command -v su-exec &> /dev/null; then
+    DROP_PRIV="su-exec step"
+else
+    echo "Warning: Neither gosu nor su-exec found, running as current user"
+    DROP_PRIV=""
+fi
+
+# Install the CA root certificate to the system trust store
+function trust_root_ca () {
+    local root_ca="${STEPPATH}/certs/root_ca.crt"
+    if [ ! -f "${root_ca}" ]; then
+        echo "Warning: Root CA certificate not found at ${root_ca}, skipping OS trust installation"
+        return 0
+    fi
+
+    # Append root CA to system trust bundle
+    cat "${root_ca}" >> /etc/ssl/certs/ca-certificates.crt
+}
+
 # List of env vars required for step ca init
 declare -ra REQUIRED_INIT_VARS=(DOCKER_STEPCA_INIT_NAME DOCKER_STEPCA_INIT_DNS_NAMES)
 
@@ -59,6 +81,8 @@ function step_ca_init () {
         generate_password > "${STEPPATH}/password"
         generate_password > "${STEPPATH}/provisioner_password"
     fi
+    # Ensure password files are owned by step user
+    chown step:step "${STEPPATH}/password" "${STEPPATH}/provisioner_password"
     if [ -f "${DOCKER_STEPCA_INIT_ROOT_FILE}" ]; then
         setup_args=("${setup_args[@]}" --root "${DOCKER_STEPCA_INIT_ROOT_FILE}")
     fi
@@ -85,7 +109,12 @@ function step_ca_init () {
                        --admin-subject "${DOCKER_STEPCA_INIT_ADMIN_SUBJECT}"
         )
     fi
-    step ca init "${setup_args[@]}"
+    # Run step ca init as the step user to ensure correct file ownership
+    if [ -n "${DROP_PRIV}" ]; then
+        ${DROP_PRIV} step ca init "${setup_args[@]}"
+    else
+        step ca init "${setup_args[@]}"
+    fi
    	echo ""
     if [ "${DOCKER_STEPCA_INIT_REMOTE_MANAGEMENT}" == "true" ]; then
         echo "👉 Your CA administrative username is: ${DOCKER_STEPCA_INIT_ADMIN_SUBJECT}"
@@ -104,4 +133,12 @@ if [ ! -f "${STEPPATH}/config/ca.json" ]; then
     init_if_possible
 fi
 
-exec "${@}"
+# Install CA root certificate to system trust store so the CA trusts itself
+trust_root_ca
+
+# Drop privileges and exec the command
+if [ -n "${DROP_PRIV}" ]; then
+    exec ${DROP_PRIV} "${@}"
+else
+    exec "${@}"
+fi