load_keys 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. while IFS= read -r dir; do
  17. IDENTITY=$(basename "$dir")
  18. # Find the latest .gpg file by name (ISO sort)
  19. LATEST_FILE=$(find "$dir" -maxdepth 1 -name "*.gpg" -printf "%f\n" \
  20. | sed 's/\.gpg$//' \
  21. | sort -r \
  22. | head -n 1)
  23. if [[ -z "$LATEST_FILE" ]]; then
  24. continue
  25. fi
  26. echo "Injecting $IDENTITY ($LATEST_FILE) into ssh-agent..."
  27. # Decrypt and pipe directly to ssh-add
  28. # The '-' tells ssh-add to read the key from standard input (stdin)
  29. pass show "${PASS_BASE}/${IDENTITY}/${LATEST_FILE}" | ssh-add - >/dev/null 2>&1
  30. done < <(find "$ABS_BASE_PATH" -mindepth 1 -maxdepth 1 -type d)
  31. echo "Done. All latest keys injected into agent."