2017-12-11 18:28:08 +00:00
|
|
|
package hosts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/docker/docker/client"
|
|
|
|
"github.com/rancher/rke/docker"
|
2018-01-09 22:10:56 +00:00
|
|
|
"github.com/rancher/rke/log"
|
2017-12-11 18:28:08 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
DockerAPIVersion = "1.24"
|
|
|
|
K8sVersion = "1.8"
|
|
|
|
)
|
|
|
|
|
2018-01-09 22:10:56 +00:00
|
|
|
func (h *Host) TunnelUp(ctx context.Context, dialerFactory DialerFactory) error {
|
2017-12-11 18:28:08 +00:00
|
|
|
if h.DClient != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Infof(ctx, "[dialer] Setup tunnel for host [%s]", h.Address)
|
2017-12-16 03:38:15 +00:00
|
|
|
httpClient, err := h.newHTTPClient(dialerFactory)
|
2017-12-11 18:28:08 +00:00
|
|
|
if err != nil {
|
2017-12-16 03:38:15 +00:00
|
|
|
return fmt.Errorf("Can't establish dialer connection: %v", err)
|
2017-12-11 18:28:08 +00:00
|
|
|
}
|
|
|
|
// 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)
|
|
|
|
}
|
2017-12-22 01:01:53 +00:00
|
|
|
return checkDockerVersion(ctx, h)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Host) TunnelUpLocal(ctx context.Context) 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkDockerVersion(ctx context.Context, h *Host) error {
|
2018-01-09 22:10:56 +00:00
|
|
|
info, err := h.DClient.Info(ctx)
|
2017-12-11 18:28:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Can't retrieve Docker Info: %v", err)
|
|
|
|
}
|
|
|
|
logrus.Debugf("Docker Info found: %#v", info)
|
|
|
|
isvalid, err := docker.IsSupportedDockerVersion(info, K8sVersion)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error while determining supported Docker version [%s]: %v", info.ServerVersion, err)
|
|
|
|
}
|
|
|
|
|
2018-01-08 20:22:20 +00:00
|
|
|
if !isvalid && h.EnforceDockerVersion {
|
2017-12-11 18:28:08 +00:00
|
|
|
return fmt.Errorf("Unsupported Docker version found [%s], supported versions are %v", info.ServerVersion, docker.K8sDockerVersions[K8sVersion])
|
2017-12-15 03:02:44 +00:00
|
|
|
} else if !isvalid {
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Warnf(ctx, "Unsupported Docker version found [%s], supported versions are %v", info.ServerVersion, docker.K8sDockerVersions[K8sVersion])
|
2017-12-11 18:28:08 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parsePrivateKey(keyBuff string) (ssh.Signer, error) {
|
|
|
|
return ssh.ParsePrivateKey([]byte(keyBuff))
|
|
|
|
}
|
|
|
|
|
|
|
|
func parsePrivateKeyWithPassPhrase(keyBuff string, passphrase []byte) (ssh.Signer, error) {
|
|
|
|
return ssh.ParsePrivateKeyWithPassphrase([]byte(keyBuff), passphrase)
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeSSHConfig(user string, signer ssh.Signer) (*ssh.ClientConfig, error) {
|
|
|
|
config := ssh.ClientConfig{
|
|
|
|
User: user,
|
|
|
|
Auth: []ssh.AuthMethod{
|
|
|
|
ssh.PublicKeys(signer),
|
|
|
|
},
|
|
|
|
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
|
|
|
}
|
|
|
|
|
|
|
|
return &config, nil
|
|
|
|
}
|
|
|
|
|
2018-01-09 19:50:10 +00:00
|
|
|
func (h *Host) checkEncryptedKey() (ssh.Signer, error) {
|
2017-12-11 18:28:08 +00:00
|
|
|
logrus.Debugf("[ssh] Checking private key")
|
|
|
|
var err error
|
|
|
|
var key ssh.Signer
|
2018-01-09 19:50:10 +00:00
|
|
|
if len(h.SSHKey) > 0 {
|
|
|
|
key, err = parsePrivateKey(h.SSHKey)
|
2017-12-11 18:28:08 +00:00
|
|
|
} else {
|
2018-01-09 19:50:10 +00:00
|
|
|
key, err = parsePrivateKey(privateKeyPath(h.SSHKeyPath))
|
2017-12-11 18:28:08 +00:00
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse encrypted key
|
|
|
|
if strings.Contains(err.Error(), "decode encrypted private keys") {
|
2018-01-09 19:50:10 +00:00
|
|
|
var passphrase []byte
|
|
|
|
if len(h.SavedKeyPhrase) == 0 {
|
|
|
|
fmt.Printf("Passphrase for Private SSH Key: ")
|
|
|
|
passphrase, err = terminal.ReadPassword(int(syscall.Stdin))
|
|
|
|
fmt.Printf("\n")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
h.SavedKeyPhrase = string(passphrase)
|
|
|
|
} else {
|
|
|
|
passphrase = []byte(h.SavedKeyPhrase)
|
2017-12-11 18:28:08 +00:00
|
|
|
}
|
2018-01-09 19:50:10 +00:00
|
|
|
|
|
|
|
if len(h.SSHKey) > 0 {
|
|
|
|
key, err = parsePrivateKeyWithPassPhrase(h.SSHKey, passphrase)
|
2017-12-11 18:28:08 +00:00
|
|
|
} else {
|
2018-01-09 19:50:10 +00:00
|
|
|
key, err = parsePrivateKeyWithPassPhrase(privateKeyPath(h.SSHKeyPath), passphrase)
|
2017-12-11 18:28:08 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return key, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func privateKeyPath(sshKeyPath string) string {
|
|
|
|
if sshKeyPath[:2] == "~/" {
|
|
|
|
sshKeyPath = filepath.Join(os.Getenv("HOME"), sshKeyPath[2:])
|
|
|
|
}
|
|
|
|
buff, _ := ioutil.ReadFile(sshKeyPath)
|
|
|
|
return string(buff)
|
|
|
|
}
|