|
|
@@ -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
|