Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions internal/hetzner/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,16 +286,26 @@ func SSHConnect(ip string) (*ssh.Client, error) {
var authMethods []ssh.AuthMethod
var diagErrors []string

// Try SSH agent first (handles passphrase-protected keys)
// Try SSH agent first (handles passphrase-protected keys).
// The agent connection must stay open through ssh.Dial because
// PublicKeysCallback calls Signers() lazily during the handshake.
var agentConn net.Conn
if sock := os.Getenv("SSH_AUTH_SOCK"); sock != "" {
conn, err := net.Dial("unix", sock)
if err != nil {
diagErrors = append(diagErrors, fmt.Sprintf("SSH agent dial failed: %v", err))
} else {
agentConn = conn
agentClient := agent.NewClient(conn)
authMethods = append(authMethods, ssh.PublicKeysCallback(agentClient.Signers))
signers, err := agentClient.Signers()
if err != nil {
diagErrors = append(diagErrors, fmt.Sprintf("SSH agent signers failed: %v", err))
conn.Close()
} else if len(signers) == 0 {
conn.Close()
} else {
agentConn = conn
authMethods = append(authMethods, ssh.PublicKeysCallback(agentClient.Signers))
}
}
}

Expand All @@ -314,7 +324,7 @@ func SSHConnect(ip string) (*ssh.Client, error) {
if err != nil {
var passErr *ssh.PassphraseMissingError
if errors.As(err, &passErr) {
continue // passphrase-protected, skip agent handles these
continue // passphrase-protected, skip - agent handles these
}
diagErrors = append(diagErrors, fmt.Sprintf("failed to parse %s: %v", keyPath, err))
continue
Expand All @@ -339,10 +349,10 @@ func SSHConnect(ip string) (*ssh.Client, error) {
}

client, err := ssh.Dial("tcp", ip+":22", config)
if agentConn != nil {
agentConn.Close()
}
if err != nil {
if agentConn != nil {
agentConn.Close()
}
return nil, err
}
return client, nil
Expand Down
Loading