MCPcopy Create free account
hub / github.com/bytebase/bytebase / GetSSHClient

Function GetSSHClient

backend/plugin/db/util/ssh.go:17–50  ·  view source on GitHub ↗

GetSSHClient returns a ssh client.

(ds *storepb.DataSource)

Source from the content-addressed store, hash-verified

15
16// GetSSHClient returns a ssh client.
17func 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
52const sshPortSize = 100
53

Callers 8

OpenMethod · 0.92
getMySQLConnectionMethod · 0.92
OpenMethod · 0.92
OpenMethod · 0.92
OpenMethod · 0.92
OpenMethod · 0.92
getTiDBConnectionMethod · 0.92
config.goFile · 0.92

Calls 7

DialMethod · 0.65
CloseMethod · 0.65
GetSshUserMethod · 0.45
GetSshPrivateKeyMethod · 0.45
GetSshPasswordMethod · 0.45
GetSshHostMethod · 0.45
GetSshPortMethod · 0.45

Tested by

no test coverage detected