Quellcode durchsuchen

Don't start as root and drop privs, run as step and use sudo to install the CA cert.

Carl Tashian vor 10 Monaten
Ursprung
Commit
d2acca0359
4 geänderte Dateien mit 14 neuen und 28 gelöschten Zeilen
  1. 1 0
      .gitignore
  2. 5 1
      docker/Dockerfile
  3. 4 1
      docker/Dockerfile.hsm
  4. 4 26
      docker/entrypoint.sh

+ 1 - 0
.gitignore

@@ -29,3 +29,4 @@ dist/
 # Packages files
 0x889B19391F774443-Certify.key
 gha-creds-*.json
+.worktrees

+ 5 - 1
docker/Dockerfile

@@ -12,11 +12,15 @@ FROM smallstep/step-kms-plugin:cloud AS kms
 FROM smallstep/step-cli:latest
 
 USER root
-RUN apk add --no-cache su-exec
+RUN apk add --no-cache sudo \
+    && echo "step ALL=(root) NOPASSWD: /bin/tee -a /etc/ssl/certs/ca-certificates.crt" >> /etc/sudoers.d/step \
+    && chmod 0440 /etc/sudoers.d/step
 
 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"
 

+ 4 - 1
docker/Dockerfile.hsm

@@ -18,9 +18,12 @@ 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 gosu
+RUN apt-get install -y --no-install-recommends opensc opensc-pkcs11 pcscd gnutls-bin libpcsclite1 p11-kit yubihsm-pkcs11 sudo
 RUN mkdir -p /run/pcscd
 RUN chown step:step /run/pcscd
+RUN echo "step ALL=(root) NOPASSWD: /usr/bin/tee -a /etc/ssl/certs/ca-certificates.crt" > /etc/sudoers.d/step \
+    && chmod 0440 /etc/sudoers.d/step
+USER step
 
 ENV CONFIGPATH="/home/step/config/ca.json"
 ENV PWDPATH="/home/step/secrets/password"

+ 4 - 26
docker/entrypoint.sh

@@ -7,16 +7,6 @@ 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"
@@ -25,8 +15,8 @@ function trust_root_ca () {
         return 0
     fi
 
-    # Append root CA to system trust bundle
-    cat "${root_ca}" >> /etc/ssl/certs/ca-certificates.crt
+    # Append root CA to system trust bundle (requires sudo)
+    cat "${root_ca}" | sudo tee -a /etc/ssl/certs/ca-certificates.crt > /dev/null
 }
 
 # List of env vars required for step ca init
@@ -81,8 +71,6 @@ 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
@@ -109,12 +97,7 @@ function step_ca_init () {
                        --admin-subject "${DOCKER_STEPCA_INIT_ADMIN_SUBJECT}"
         )
     fi
-    # 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
+    step ca init "${setup_args[@]}"
    	echo ""
     if [ "${DOCKER_STEPCA_INIT_REMOTE_MANAGEMENT}" == "true" ]; then
         echo "👉 Your CA administrative username is: ${DOCKER_STEPCA_INIT_ADMIN_SUBJECT}"
@@ -136,9 +119,4 @@ fi
 # 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
+exec "${@}"