Files
dotfiles/bin/.bin/load_keys

43 lines
1.1 KiB
Plaintext
Raw Normal View History

2025-12-25 23:28:40 -05:00
#!/usr/bin/env bash
set -euo pipefail
2026-01-12 12:45:48 -05:00
HOST="${HOST:-$(hostname -s 2>/dev/null || hostname)}"
2025-12-25 23:28:40 -05:00
PASS_BASE="personal/ssh"
STORE_ROOT="${PASSWORD_STORE_DIR:-$HOME/.password-store}"
ABS_BASE_PATH="${STORE_ROOT}/${PASS_BASE}"
# Ensure ssh-agent is running
if [[ -z "${SSH_AUTH_SOCK:-}" ]]; then
eval "$(ssh-agent -s)"
fi
# Verify the base path exists
if [[ ! -d "$ABS_BASE_PATH" ]]; then
echo "ERROR: Base path not found in pass: $PASS_BASE" >&2
exit 1
fi
2026-01-12 12:45:48 -05:00
2025-12-25 23:28:40 -05:00
# Loop through each identity subdirectory
2026-01-12 12:45:48 -05:00
echo -n "Loading ssh keys for host: "
2025-12-25 23:28:40 -05:00
while IFS= read -r dir; do
IDENTITY=$(basename "$dir")
2026-01-12 12:45:48 -05:00
# Find the latest .gpg file by name (ISO sort) and hostname
2026-01-05 10:40:58 -05:00
LATEST_FILE=$(find "$dir" -maxdepth 1 -name "*.gpg" -exec basename {} \; \
2025-12-25 23:28:40 -05:00
| sed 's/\.gpg$//' \
| sort -r \
| head -n 1)
if [[ -z "$LATEST_FILE" ]]; then
continue
fi
2026-01-12 12:45:48 -05:00
echo -n "$HOST"
2025-12-25 23:28:40 -05:00
# Decrypt and pipe directly to ssh-add
# The '-' tells ssh-add to read the key from standard input (stdin)
pass show "${PASS_BASE}/${IDENTITY}/${LATEST_FILE}" | ssh-add - >/dev/null 2>&1
2026-01-12 12:45:48 -05:00
done < <(find "$ABS_BASE_PATH" -mindepth 1 -maxdepth 1 -type d -name "*${HOST}*")