forked from rancher/rke
/
tunnel.go
168 lines (147 loc) · 4.85 KB
/
tunnel.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package hosts
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"net"
"github.com/docker/docker/client"
"github.com/rancher/rke/docker"
"github.com/rancher/rke/log"
"github.com/rancher/rke/util"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)
const (
DockerAPIVersion = "1.24"
)
func (h *Host) TunnelUp(ctx context.Context, dialerFactory DialerFactory, clusterPrefixPath string, clusterVersion string) error {
if h.DClient != nil {
return nil
}
log.Infof(ctx, "[dialer] Setup tunnel for host [%s]", h.Address)
httpClient, err := h.newHTTPClient(dialerFactory)
if err != nil {
return fmt.Errorf("Can't establish dialer connection: %v", err)
}
// set Docker client
logrus.Debugf("Connecting to Docker API for host [%s]", h.Address)
h.DClient, err = client.NewClient("unix:///var/run/docker.sock", DockerAPIVersion, httpClient, nil)
if err != nil {
return fmt.Errorf("Can't initiate NewClient: %v", err)
}
if err := checkDockerVersion(ctx, h, clusterVersion); err != nil {
return err
}
h.PrefixPath = GetPrefixPath(h.DockerInfo.OperatingSystem, clusterPrefixPath)
return nil
}
func (h *Host) TunnelUpLocal(ctx context.Context, clusterVersion string) error {
var err error
if h.DClient != nil {
return nil
}
// set Docker client
logrus.Debugf("Connecting to Docker API for host [%s]", h.Address)
h.DClient, err = client.NewEnvClient()
if err != nil {
return fmt.Errorf("Can't initiate NewClient: %v", err)
}
return checkDockerVersion(ctx, h, clusterVersion)
}
func checkDockerVersion(ctx context.Context, h *Host, clusterVersion string) error {
info, err := h.DClient.Info(ctx)
if err != nil {
return fmt.Errorf("Can't retrieve Docker Info: %v", err)
}
logrus.Debugf("Docker Info found: %#v", info)
h.DockerInfo = info
K8sSemVer, err := util.StrToSemVer(clusterVersion)
if err != nil {
return fmt.Errorf("Error while parsing cluster version [%s]: %v", clusterVersion, err)
}
K8sVersion := fmt.Sprintf("%d.%d", K8sSemVer.Major, K8sSemVer.Minor)
isvalid, err := docker.IsSupportedDockerVersion(info, K8sVersion)
if err != nil {
return fmt.Errorf("Error while determining supported Docker version [%s]: %v", info.ServerVersion, err)
}
if !isvalid && !h.IgnoreDockerVersion {
return fmt.Errorf("Unsupported Docker version found [%s], supported versions are %v", info.ServerVersion, docker.K8sDockerVersions[K8sVersion])
} else if !isvalid {
log.Warnf(ctx, "Unsupported Docker version found [%s], supported versions are %v", info.ServerVersion, docker.K8sDockerVersions[K8sVersion])
}
return nil
}
func parsePrivateKey(keyBuff string) (ssh.Signer, error) {
return ssh.ParsePrivateKey([]byte(keyBuff))
}
func getSSHConfig(username, sshPrivateKeyString string, sshCertificateString string, useAgentAuth bool) (*ssh.ClientConfig, error) {
config := &ssh.ClientConfig{
User: username,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
// Kind of a double check now.
if useAgentAuth {
if sshAgentSock := os.Getenv("SSH_AUTH_SOCK"); sshAgentSock != "" {
sshAgent, err := net.Dial("unix", sshAgentSock)
if err != nil {
return config, fmt.Errorf("Cannot connect to SSH Auth socket %q: %s", sshAgentSock, err)
}
config.Auth = append(config.Auth, ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers))
logrus.Debugf("using %q SSH_AUTH_SOCK", sshAgentSock)
return config, nil
}
}
signer, err := parsePrivateKey(sshPrivateKeyString)
if err != nil {
return config, err
}
if len(sshCertificateString) > 0 {
key, _, _, _, err := ssh.ParseAuthorizedKey([]byte(sshCertificateString))
if err != nil {
return config, fmt.Errorf("Unable to parse SSH certificate: %v", err)
}
if _, ok := key.(*ssh.Certificate); !ok {
return config, fmt.Errorf("Unable to cast public key to SSH Certificate")
}
signer, err = ssh.NewCertSigner(key.(*ssh.Certificate), signer)
if err != nil {
return config, err
}
}
config.Auth = append(config.Auth, ssh.PublicKeys(signer))
return config, nil
}
func privateKeyPath(sshKeyPath string) (string, error) {
if sshKeyPath[:2] == "~/" {
sshKeyPath = filepath.Join(userHome(), sshKeyPath[2:])
}
buff, err := ioutil.ReadFile(sshKeyPath)
if err != nil {
return "", fmt.Errorf("Error while reading SSH key file: %v", err)
}
return string(buff), nil
}
func certificatePath(sshCertPath string) (string, error) {
if sshCertPath[:2] == "~/" {
sshCertPath = filepath.Join(userHome(), sshCertPath[2:])
}
buff, err := ioutil.ReadFile(sshCertPath)
if err != nil {
return "", fmt.Errorf("Error while reading SSH certificate file: %v", err)
}
return string(buff), nil
}
func userHome() string {
if home := os.Getenv("HOME"); home != "" {
return home
}
homeDrive := os.Getenv("HOMEDRIVE")
homePath := os.Getenv("HOMEPATH")
if homeDrive != "" && homePath != "" {
return homeDrive + homePath
}
return os.Getenv("USERPROFILE")
}