GetSSHClient returns a ssh client.
(ds *storepb.DataSource)
| 15 | |
| 16 | // GetSSHClient returns a ssh client. |
| 17 | func GetSSHClient(ds *storepb.DataSource) (*ssh.Client, error) { |
| 18 | sshConfig := &ssh.ClientConfig{ |
| 19 | User: ds.GetSshUser(), |
| 20 | Auth: []ssh.AuthMethod{}, |
| 21 | HostKeyCallback: ssh.InsecureIgnoreHostKey(), |
| 22 | } |
| 23 | if ds.GetSshPrivateKey() != "" { |
| 24 | signer, err := ssh.ParsePrivateKey([]byte(ds.GetSshPrivateKey())) |
| 25 | if err != nil { |
| 26 | return nil, err |
| 27 | } |
| 28 | sshConfig.Auth = append(sshConfig.Auth, ssh.PublicKeys(signer)) |
| 29 | } else { |
| 30 | // Users may use ssh-agent to store the private key with passphrase, |
| 31 | // we will try to connect to the ssh-agent to get the private key. |
| 32 | if conn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil { |
| 33 | defer conn.Close() |
| 34 | agentClient := agent.NewClient(conn) |
| 35 | sshConfig.Auth = append(sshConfig.Auth, ssh.PublicKeysCallback(agentClient.Signers)) |
| 36 | } |
| 37 | } |
| 38 | // When there's a non empty password add the password AuthMethod. |
| 39 | if ds.GetSshPassword() != "" { |
| 40 | sshConfig.Auth = append(sshConfig.Auth, ssh.PasswordCallback(func() (string, error) { |
| 41 | return ds.GetSshPassword(), nil |
| 42 | })) |
| 43 | } |
| 44 | // Connect to the SSH Server |
| 45 | sshConn, err := ssh.Dial("tcp", fmt.Sprintf("%s:%s", ds.GetSshHost(), ds.GetSshPort()), sshConfig) |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | return sshConn, nil |
| 50 | } |
| 51 | |
| 52 | const sshPortSize = 100 |
| 53 |
no test coverage detected