connectOperator Try SSH agent without parsing key files, will succeed if the user has already added a key to the SSH Agent, or if using a configured smartcard. If the initial connection attempt fails fall through to the using the supplied/default private key file DoneFunc should be called by the c
(user string, address string, sshKeyPath string)
| 341 | // the supplied/default private key file |
| 342 | // DoneFunc should be called by the caller to close the SSH connection when done |
| 343 | func connectOperator(user string, address string, sshKeyPath string) (*operator.SSHOperator, DoneFunc, bool, error) { |
| 344 | var sshOperator *operator.SSHOperator |
| 345 | var initialSSHErr error |
| 346 | var closeSSHAgentFunc func() error |
| 347 | |
| 348 | doneFunc := func() { |
| 349 | if sshOperator != nil { |
| 350 | sshOperator.Close() |
| 351 | } |
| 352 | if closeSSHAgentFunc != nil { |
| 353 | closeSSHAgentFunc() |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | if runtime.GOOS != "windows" { |
| 358 | var sshAgentAuthMethod ssh.AuthMethod |
| 359 | sshAgentAuthMethod, initialSSHErr = sshAgentOnly() |
| 360 | if initialSSHErr == nil { |
| 361 | |
| 362 | config := &ssh.ClientConfig{ |
| 363 | User: user, |
| 364 | Auth: []ssh.AuthMethod{sshAgentAuthMethod}, |
| 365 | HostKeyCallback: ssh.InsecureIgnoreHostKey(), |
| 366 | } |
| 367 | |
| 368 | sshOperator, initialSSHErr = operator.NewSSHOperator(address, config) |
| 369 | } |
| 370 | } else { |
| 371 | initialSSHErr = errors.New("ssh-agent unsupported on windows") |
| 372 | } |
| 373 | |
| 374 | if initialSSHErr != nil { |
| 375 | publicKeyFileAuth, closeSSHAgent, err := loadPublickey(sshKeyPath) |
| 376 | if err != nil { |
| 377 | return nil, nil, true, fmt.Errorf("unable to load the ssh key with path %q: %w", sshKeyPath, err) |
| 378 | } |
| 379 | |
| 380 | defer closeSSHAgent() |
| 381 | |
| 382 | config := &ssh.ClientConfig{ |
| 383 | User: user, |
| 384 | Auth: []ssh.AuthMethod{publicKeyFileAuth}, |
| 385 | HostKeyCallback: ssh.InsecureIgnoreHostKey(), |
| 386 | } |
| 387 | |
| 388 | sshOperator, err = operator.NewSSHOperator(address, config) |
| 389 | if err != nil { |
| 390 | return nil, nil, true, fmt.Errorf("unable to connect to %s over ssh: %w", address, err) |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | return sshOperator, doneFunc, false, nil |
| 395 | } |
| 396 | |
| 397 | func sshAgentOnly() (ssh.AuthMethod, error) { |
| 398 | sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")) |
no test coverage detected