mirror of
https://github.com/MaksTinyWorkshop/_Assistant_Lead_Tech
synced 2026-04-06 13:31:43 +02:00
90 lines
2.2 KiB
Bash
Executable File
90 lines
2.2 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
_load_global_secrets() {
|
|
local _env_paths
|
|
if [ -f "$HOME/AI_RULES/Auto_scripts/env_paths.sh" ]; then
|
|
_env_paths="$HOME/AI_RULES/Auto_scripts/env_paths.sh"
|
|
elif [ -f "/srv/shared/scripts/env_paths.sh" ]; then
|
|
_env_paths="/srv/shared/scripts/env_paths.sh"
|
|
else
|
|
echo "env_paths.sh introuvable" >&2
|
|
return 1
|
|
fi
|
|
source "$_env_paths" || return 1
|
|
|
|
if [ ! -f "$SECRETS_KDBX" ]; then
|
|
echo "Coffre introuvable : $SECRETS_KDBX" >&2
|
|
return 1
|
|
fi
|
|
|
|
if ! command -v keepassxc-cli >/dev/null 2>&1; then
|
|
echo "keepassxc-cli introuvable" >&2
|
|
return 1
|
|
fi
|
|
|
|
if ! command -v expect >/dev/null 2>&1; then
|
|
echo "expect introuvable" >&2
|
|
return 1
|
|
fi
|
|
|
|
if [ -z "${KDBX_PASSWORD:-}" ]; then
|
|
printf "Mot de passe KeePassXC : " >&2
|
|
stty -echo
|
|
IFS= read -r KDBX_PASSWORD
|
|
stty echo
|
|
printf '\n' >&2
|
|
fi
|
|
|
|
echo "Chargement des secrets globaux..." >&2
|
|
|
|
# Export CSV complet — une seule ouverture du coffre
|
|
local csv
|
|
csv=$(KDBX_PASSWORD="$KDBX_PASSWORD" SECRETS_KDBX="$SECRETS_KDBX" expect <<'EOF'
|
|
log_user 0
|
|
set timeout 30
|
|
spawn keepassxc-cli export --format csv $env(SECRETS_KDBX)
|
|
expect "Saisir le mot de passe pour déverrouiller*"
|
|
send -- "$env(KDBX_PASSWORD)\r"
|
|
expect eof
|
|
catch wait result
|
|
puts -nonewline $expect_out(buffer)
|
|
exit [lindex $result 3]
|
|
EOF
|
|
) || {
|
|
echo "Impossible d'exporter le coffre." >&2
|
|
return 1
|
|
}
|
|
|
|
local loaded=0
|
|
|
|
while IFS=',' read -r group title username password rest; do
|
|
group="${group//\"/}"
|
|
title="${title//\"/}"
|
|
password="${password//\"/}"
|
|
|
|
[[ "$group" != "Racine/global" && "$group" != "Racine/global/"* ]] && continue
|
|
|
|
local var_name="$title"
|
|
if ! printf '%s' "$var_name" | grep -Eq '^[A-Z_][A-Z0-9_]*$'; then
|
|
echo "Nom invalide ignoré : $var_name" >&2
|
|
continue
|
|
fi
|
|
|
|
[ -z "$password" ] && { echo "Valeur vide ignorée : $var_name" >&2; continue; }
|
|
|
|
export "$var_name=$password"
|
|
loaded=$((loaded + 1))
|
|
|
|
done <<< "$csv"
|
|
|
|
if [ "$loaded" -eq 0 ]; then
|
|
echo "Aucun secret global chargé." >&2
|
|
return 1
|
|
fi
|
|
|
|
echo "Secrets chargés : $loaded"
|
|
}
|
|
|
|
_load_global_secrets
|
|
unset -f _load_global_secrets
|