-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Bram van Dartel
committed
Apr 14, 2024
1 parent
c26080c
commit 2cd0cd8
Showing
8 changed files
with
162 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 0 additions & 56 deletions
56
docker/entrypoint/docker-entrypoint.d/.env-from-docker-secrets
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
rootfs/etc/s6-overlay/s6-rc.d/docker-entrypoint/docker-entrypoint.d/.env-from-docker-secrets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#!/usr/bin/env bash | ||
|
||
# EXPANDING VARIABLES FROM DOCKER SECRETS | ||
: "${ENV_SECRETS_DIR:=/run/secrets}" | ||
|
||
# Function to print debug messages for environment secrets | ||
env_secret_debug() { | ||
if [[ -n "$ENV_SECRETS_DEBUG" ]]; then | ||
printf "\033[1m%s\033[0m\n" "$@" | ||
fi | ||
} | ||
|
||
# This function populates environment variables from variables containing file paths | ||
populate_file_variables() { | ||
for env_var in $(env | grep '_FILE=' | cut -d '=' -f 1); do | ||
var_name="${env_var%_FILE}" # Remove the '_FILE' suffix | ||
file_path=$(eval echo "\$$env_var") # Get the file path from the environment variable | ||
if [[ -s "$file_path" ]]; then | ||
val=$(cat "$file_path") # Read the file contents into the environment variable | ||
export "$var_name"="$val" | ||
env_secret_debug "Populated Docker secret variable: $var_name" | ||
else | ||
env_secret_debug "Docker secret file is empty or does not exist: $file_path" | ||
fi | ||
done | ||
} | ||
|
||
# This function expands variables from docker secrets | ||
expand_docker_secrets() { | ||
# Using env to avoid issues with variable names containing special characters | ||
while IFS='=' read -r env_var var_value; do | ||
# Match the pattern directly with Bash regex | ||
if [[ "$var_value" =~ DOCKER-SECRET-\>([^}]+)$ ]]; then | ||
secret_key="${BASH_REMATCH[1]}" | ||
secret="${ENV_SECRETS_DIR}/${secret_key}" | ||
if [[ -f "$secret" ]]; then | ||
# Read the content of the secret file safely | ||
val=$(<"$secret") | ||
export "$env_var"="$val" # Set the variable with the secret value | ||
env_secret_debug "Expanded Docker secret variable: $env_var" | ||
else | ||
env_secret_debug "Docker secret file does not exist! $secret" | ||
fi | ||
fi | ||
done < <(env) # Redirect the output of env to the while loop | ||
} | ||
|
||
# Populate environment variables from variables containing file paths | ||
# Conditionally expand variables from docker secrets | ||
if [[ -n "$ENV_SECRETS_DIR" ]]; then | ||
populate_file_variables | ||
expand_docker_secrets | ||
else | ||
env_secret_debug "No Docker secret found in /run/secrets" | ||
fi | ||
|
||
# Execute the command provided as arguments to the script | ||
exec "$@" |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters