This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 525
/
ssh.go
51 lines (45 loc) · 1.45 KB
/
ssh.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
package helpers
import (
"fmt"
"io/ioutil"
"github.com/pkg/errors"
"golang.org/x/crypto/ssh"
)
func SSHClient(jumpboxHost, jumpboxPort, hostname string, jumpboxConfig, nodeConfig *ssh.ClientConfig) (*ssh.Client, error) {
lbConn, err := ssh.Dial("tcp", fmt.Sprintf("%s:%s", jumpboxHost, jumpboxPort), jumpboxConfig)
if err != nil {
return nil, errors.Wrapf(err, "dialing jumpbox (%s)", jumpboxHost)
}
conn, err := lbConn.Dial("tcp", fmt.Sprintf("%s:22", hostname))
if err != nil {
return nil, errors.Wrapf(err, "dialing host (%s)", hostname)
}
ncc, chans, reqs, err := ssh.NewClientConn(conn, hostname, nodeConfig)
if err != nil {
return nil, errors.Wrapf(err, "starting new client connection to host (%s)", hostname)
}
return ssh.NewClient(ncc, chans, reqs), nil
}
func SSHClientConfig(user string, auth ssh.AuthMethod) *ssh.ClientConfig {
return &ssh.ClientConfig{
// FixedHostKey instead?
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
User: user,
Auth: []ssh.AuthMethod{
auth,
},
}
}
func PublicKeyAuth(sshPrivateKeyPath string) (ssh.AuthMethod, error) {
b, err := ioutil.ReadFile(sshPrivateKeyPath)
if err != nil {
return nil, errors.Wrap(err, "reading ssh private key file")
}
k, err := ssh.ParsePrivateKey(b)
if err != nil {
return nil, errors.Wrap(err, "parsing ssh private key content")
}
return ssh.PublicKeys(k), nil
}