fix: pass connect command
parent
f42e8b5901
commit
0ee5b71b64
66
sshkeys.bash
66
sshkeys.bash
|
|
@ -9,7 +9,10 @@ PASS_DIR="$PASSWORD_STORE_DIR"
|
|||
VERBOSE=0
|
||||
|
||||
# Helper functions
|
||||
die() { echo "Error: $*" >&2; exit 1; }
|
||||
die() {
|
||||
echo "Error: $*" >&2
|
||||
exit 1
|
||||
}
|
||||
debug() { [[ $VERBOSE -eq 1 ]] && echo "DEBUG: $*" >&2; }
|
||||
yesno() {
|
||||
local answer
|
||||
|
|
@ -95,7 +98,8 @@ cmd_import_with_deps() {
|
|||
local identity_files=()
|
||||
local identity_paths=()
|
||||
for line in "${host_block[@]}"; do
|
||||
if [[ "$line" =~ ^[Ii][Dd][Ee][Nn][Tt][Ii][Tt][Yy][Ff][Ii][Ll][Ee][[:space:]]+([^[:space:]]+) ]]; then
|
||||
debug "Checking line for IdentityFile: $line"
|
||||
if [[ "$line" =~ ^[[:space:]]*[Ii][Dd][Ee][Nn][Tt][Ii][Tt][Yy][Ff][Ii][Ll][Ee][[:space:]]+([^[:space:]]+) ]]; then
|
||||
identity_file="${BASH_REMATCH[1]}"
|
||||
debug "Found IdentityFile: $identity_file"
|
||||
identity_files+=("$identity_file")
|
||||
|
|
@ -243,7 +247,7 @@ cmd_export() {
|
|||
# Export IdentityFiles
|
||||
local identity_files=()
|
||||
while IFS= read -r line; do
|
||||
if [[ "$line" =~ ^[Ii][Dd][Ee][Nn][Tt][Ii][Tt][Yy][Ff][Ii][Ll][Ee][[:space:]]+([^[:space:]]+) ]]; then
|
||||
if [[ "$line" =~ ^[[:space:]]*[Ii][Dd][Ee][Nn][Tt][Ii][Tt][Yy][Ff][Ii][Ll][Ee][[:space:]]+([^[:space:]]+) ]]; then
|
||||
identity_files+=("${BASH_REMATCH[1]}")
|
||||
fi
|
||||
done <<<"$host_block"
|
||||
|
|
@ -315,8 +319,14 @@ cmd_connect() {
|
|||
|
||||
# Create temporary directory for keys
|
||||
local tmp_dir=$(mktemp -d)
|
||||
debug "Created temporary directory: $tmp_dir"
|
||||
trap 'rm -rf "$tmp_dir"' EXIT
|
||||
|
||||
# Create empty temporary SSH config
|
||||
local tmp_config="$tmp_dir/config"
|
||||
touch "$tmp_config"
|
||||
debug "Created temporary config: $tmp_config"
|
||||
|
||||
# Function to process a host and its ProxyJump dependencies
|
||||
process_host() {
|
||||
local host="$1"
|
||||
|
|
@ -326,12 +336,15 @@ cmd_connect() {
|
|||
local config_store="ssh/$host/config"
|
||||
local host_block
|
||||
host_block=$(pass show "$config_store" 2>/dev/null) || die "No config found for $host"
|
||||
debug "Retrieved host block from $config_store:"
|
||||
debug "$host_block"
|
||||
|
||||
# Append to temporary SSH config
|
||||
echo "$host_block" >>"$tmp_config"
|
||||
|
||||
# Extract and restore keys
|
||||
while IFS= read -r line; do
|
||||
debug "Processing config line: $line"
|
||||
if [[ "$line" =~ ^[[:space:]]*[Pp][Rr][Oo][Xx][Yy][Jj][Uu][Mm][Pp][[:space:]]+([^[:space:]]+) ]]; then
|
||||
debug "Found ProxyJump: ${BASH_REMATCH[1]}"
|
||||
IFS=',' read -ra proxy_hosts <<<"${BASH_REMATCH[1]}"
|
||||
|
|
@ -344,43 +357,55 @@ cmd_connect() {
|
|||
process_host "$proxy"
|
||||
fi
|
||||
done
|
||||
elif [[ "$line" =~ ^[Ii][Dd][Ee][Nn][Tt][Ii][Tt][Yy][Ff][Ii][Ll][Ee][[:space:]]+([^[:space:]]+) ]]; then
|
||||
elif [[ "$line" =~ ^[[:space:]]*[Ii][Dd][Ee][Nn][Tt][Ii][Tt][Yy][Ff][Ii][Ll][Ee][[:space:]]+([^[:space:]]+) ]]; then
|
||||
local identity_file="${BASH_REMATCH[1]}"
|
||||
debug "Found IdentityFile: $identity_file"
|
||||
|
||||
local expanded_path="${identity_file/#\~/$HOME}"
|
||||
expanded_path=$(realpath -m "$expanded_path")
|
||||
debug "Expanded path: $expanded_path"
|
||||
|
||||
# Resolve relative to SSH_DIR if needed
|
||||
if [[ "$expanded_path" != "$SSH_DIR"/* ]]; then
|
||||
debug "Path not under SSH_DIR, adjusting"
|
||||
expanded_path="$SSH_DIR/$identity_file"
|
||||
fi
|
||||
debug "Final expanded path: $expanded_path"
|
||||
|
||||
local rel_path="${expanded_path#$SSH_DIR/}"
|
||||
rel_path="${rel_path//../_dotdot_}"
|
||||
local store_path="ssh/$host/$rel_path"
|
||||
local tmp_key="$tmp_dir/$(basename "$identity_file")"
|
||||
debug "Store path: $store_path"
|
||||
debug "Temporary key path: $tmp_key"
|
||||
|
||||
# Restore key to temporary location
|
||||
if pass show "$store_path" >"$tmp_key" 2>/dev/null; then
|
||||
chmod 600 "$tmp_key"
|
||||
debug "Restored key $store_path to $tmp_key"
|
||||
# Update config to use temporary key
|
||||
debug "Updating config to use temporary key"
|
||||
debug "Replacing: $identity_file"
|
||||
debug "With: $tmp_key"
|
||||
sed -i "s|${identity_file}|${tmp_key}|g" "$tmp_config"
|
||||
else
|
||||
debug "Failed to retrieve key from $store_path"
|
||||
echo "Warning: Key $store_path not found in pass"
|
||||
fi
|
||||
fi
|
||||
done <<<"$host_block"
|
||||
}
|
||||
|
||||
# Create empty temporary SSH config
|
||||
local tmp_config="$tmp_dir/config"
|
||||
touch "$tmp_config"
|
||||
debug "Finished processing host: $host"
|
||||
debug "Current temporary config contents:"
|
||||
debug "$(cat "$tmp_config")"
|
||||
}
|
||||
|
||||
# Process the main host and its dependencies
|
||||
process_host "$hostname"
|
||||
|
||||
# Execute SSH command with temporary config
|
||||
echo "Connecting to $hostname..."
|
||||
debug "Running: ssh -F \"$tmp_config\" \"$hostname\""
|
||||
ssh -F "$tmp_config" "$hostname"
|
||||
}
|
||||
|
||||
|
|
@ -394,10 +419,25 @@ case "$1" in
|
|||
esac
|
||||
|
||||
case "$1" in
|
||||
import) shift; cmd_import_with_deps "$@" ;;
|
||||
import-all) shift; cmd_import_all ;;
|
||||
export) shift; cmd_export "$@" ;;
|
||||
export-all) shift; cmd_export_all ;;
|
||||
connect) shift; cmd_connect "$@" ;;
|
||||
import)
|
||||
shift
|
||||
cmd_import_with_deps "$@"
|
||||
;;
|
||||
import-all)
|
||||
shift
|
||||
cmd_import_all
|
||||
;;
|
||||
export)
|
||||
shift
|
||||
cmd_export "$@"
|
||||
;;
|
||||
export-all)
|
||||
shift
|
||||
cmd_export_all
|
||||
;;
|
||||
connect)
|
||||
shift
|
||||
cmd_connect "$@"
|
||||
;;
|
||||
*) die "Usage: pass ssh [-v|--verbose] import|import-all|export|export-all|connect [hostname]" ;;
|
||||
esac
|
||||
|
|
|
|||
Loading…
Reference in New Issue