Skip to content
Merged
Changes from 1 commit
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
18 changes: 11 additions & 7 deletions internal/hetzner/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,15 +287,22 @@ func SSHConnect(ip string) (*ssh.Client, error) {
var diagErrors []string

// Try SSH agent first (handles passphrase-protected keys)
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 {
authMethods = append(authMethods, ssh.PublicKeysCallback(agentClient.Signers))
conn.Close()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Keep agent socket open for PublicKeysCallback auth

ssh.PublicKeysCallback(agentClient.Signers) is lazy and runs during ssh.Dial, but this change closes conn immediately afterward, so the callback has no live agent transport when authentication actually starts. In environments that rely on agent-backed keys (for example passphrase-protected keys that cannot be parsed from ~/.ssh/id_*), agent auth now fails deterministically even when keys are loaded, which regresses the main SSH path this function is meant to support.

Useful? React with 👍 / 👎.

}
}
}

Expand All @@ -314,7 +321,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 @@ -340,9 +347,6 @@ func SSHConnect(ip string) (*ssh.Client, error) {

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