feat: implement import/export functionality for SSH keys and configs

main
Malar Invention 2025-07-18 01:16:06 +05:30
parent f362ffa12b
commit 8c12edb583
6 changed files with 61 additions and 6 deletions

View File

@ -1,6 +1,6 @@
install:
@echo "Installing pass-cli plugin (sshkeys.bash)..."
@mkdir -p ~/.password-store/.extensions/
@cp sshkeys.bash ~/.password-store/.extensions/
@cp extension/sshkeys.bash ~/.password-store/.extensions/
@chmod +x ~/.password-store/.extensions/sshkeys.bash
@echo "Installation complete. You can now use 'pass-sshkeys'."

View File

@ -53,7 +53,7 @@ alias pass='PASSWORD_STORE_ENABLE_EXTENSIONS=true pass'
Import a single host:
```bash
pass ssh import hostname
pass sshkeys import hostname
```
When importing a host, the extension automatically detects and handles ProxyJump configurations:
@ -65,7 +65,7 @@ When importing a host, the extension automatically detects and handles ProxyJump
Import all hosts from SSH config:
```bash
pass ssh import-all
pass sshkeys import-all
```
### Export SSH Keys and Config
@ -73,13 +73,13 @@ pass ssh import-all
Export a single host:
```bash
pass ssh export hostname
pass sshkeys export hostname
```
Export all stored hosts:
```bash
pass ssh export-all
pass sshkeys export-all
```
### Direct Connection
@ -87,7 +87,7 @@ pass ssh export-all
Connect to a host using stored keys without importing:
```bash
pass ssh connect hostname
pass sshkeys connect hostname
```
The connect command:

38
completion/pass-sshkeys Normal file
View File

@ -0,0 +1,38 @@
PASSWORD_STORE_EXTENSION_COMMANDS+=(sshkeys)
__password_store_extension_complete_sshkeys() {
local cur="${COMP_WORDS[COMP_CWORD]}"
local subcommand="${COMP_WORDS[2]}"
# If we are completing the subcommand itself (at index 2)
if [[ $COMP_CWORD -eq 2 ]]; then
COMPREPLY=($(compgen -W "import import-all export export-all connect -v --verbose -h --help" -- "$cur"))
return
fi
# If -v or --verbose is at index 2, we might be completing the subcommand at index 3
if [[ "${COMP_WORDS[2]}" == "-v" || "${COMP_WORDS[2]}" == "--verbose" ]]; then
if [[ $COMP_CWORD -eq 3 ]]; then
COMPREPLY=($(compgen -W "import import-all export export-all connect -h --help" -- "$cur"))
return
fi
# The actual subcommand is at index 3
subcommand="${COMP_WORDS[3]}"
fi
# We are completing an argument for a subcommand
case "$subcommand" in
import)
# Suggest hosts from ~/.ssh/config
if [[ -f "$HOME/.ssh/config" ]]; then
local hosts=$(awk '/^[Hh][Oo][Ss][Tt][[:space:]]/{for(i=2;i<=NF;i++){if($i~/#/){break};if($i!="*"){print $i}}}' "$HOME/.ssh/config" | sort -u)
COMPREPLY=($(compgen -W "$hosts" -- "$cur"))
fi
;;
export|connect)
# Suggest hosts from the password store
local hosts=$(pass ls ssh 2>/dev/null | sed -e 's,^ssh/,,g' -e 's,/.*,,g' | sort -u)
COMPREPLY=($(compgen -W "$hosts" -- "$cur"))
;;
esac
}

View File

@ -442,6 +442,20 @@ cmd_connect() {
ssh -F "$tmp_config" "$hostname"
}
# Show help
cmd_help() {
cat <<-_EOF
Usage: pass ssh [-v|--verbose] import|import-all|export|export-all|connect [hostname]
Commands:
import - Import a host and its dependencies from ~/.ssh/config.
import-all - Import all hosts from ~/.ssh/config.
export - Export a host and its dependencies to ~/.ssh/config.
export-all - Export all hosts to ~/.ssh/config.
connect - Connect to a host using the stored keys.
_EOF
}
# Main command handler
case "$1" in
-v | --verbose)
@ -472,5 +486,8 @@ connect)
shift
cmd_connect "$@"
;;
-h|--help|help)
cmd_help
;;
*) die "Usage: pass ssh [-v|--verbose] import|import-all|export|export-all|connect [hostname]" ;;
esac