load_keys 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. PASS_BASE="personal/ssh"
  4. STORE_ROOT="${PASSWORD_STORE_DIR:-$HOME/.password-store}"
  5. ABS_BASE_PATH="${STORE_ROOT}/${PASS_BASE}"
  6. # Ensure ssh-agent is running
  7. if [[ -z "${SSH_AUTH_SOCK:-}" ]]; then
  8. eval "$(ssh-agent -s)"
  9. fi
  10. # Verify the base path exists
  11. if [[ ! -d "$ABS_BASE_PATH" ]]; then
  12. echo "ERROR: Base path not found in pass: $PASS_BASE" >&2
  13. exit 1
  14. fi
  15. # Loop through each identity subdirectory
  16. echo -n "Injecting ssh keys "
  17. while IFS= read -r dir; do
  18. IDENTITY=$(basename "$dir")
  19. # Find the latest .gpg file by name (ISO sort)
  20. LATEST_FILE=$(find "$dir" -maxdepth 1 -name "*.gpg" -exec basename {} \; \
  21. | sed 's/\.gpg$//' \
  22. | sort -r \
  23. | head -n 1)
  24. if [[ -z "$LATEST_FILE" ]]; then
  25. continue
  26. fi
  27. echo -n "."
  28. # Decrypt and pipe directly to ssh-add
  29. # The '-' tells ssh-add to read the key from standard input (stdin)
  30. pass show "${PASS_BASE}/${IDENTITY}/${LATEST_FILE}" | ssh-add - >/dev/null 2>&1
  31. done < <(find "$ABS_BASE_PATH" -mindepth 1 -maxdepth 1 -type d)