mirror of
https://github.com/rancher/rke.git
synced 2025-09-01 06:56:29 +00:00
Fix encrypted key issue
This commit is contained in:
@@ -2,28 +2,37 @@ package cluster
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"github.com/rancher/rke/hosts"
|
||||
"github.com/rancher/rke/pki"
|
||||
"github.com/rancher/rke/services"
|
||||
"github.com/sirupsen/logrus"
|
||||
"golang.org/x/crypto/ssh"
|
||||
"golang.org/x/crypto/ssh/terminal"
|
||||
)
|
||||
|
||||
func (c *Cluster) TunnelHosts() error {
|
||||
key, err := checkEncryptedKey()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to parse the private key: %v", err)
|
||||
}
|
||||
for i := range c.EtcdHosts {
|
||||
err := c.EtcdHosts[i].TunnelUp()
|
||||
err := c.EtcdHosts[i].TunnelUp(key)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to set up SSH tunneling for Etcd hosts: %v", err)
|
||||
}
|
||||
}
|
||||
for i := range c.ControlPlaneHosts {
|
||||
err := c.ControlPlaneHosts[i].TunnelUp()
|
||||
err := c.ControlPlaneHosts[i].TunnelUp(key)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to set up SSH tunneling for Control hosts: %v", err)
|
||||
}
|
||||
}
|
||||
for i := range c.WorkerHosts {
|
||||
err := c.WorkerHosts[i].TunnelUp()
|
||||
err := c.WorkerHosts[i].TunnelUp(key)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to set up SSH tunneling for Worker hosts: %v", err)
|
||||
}
|
||||
@@ -75,3 +84,29 @@ func (c *Cluster) SetUpHosts() error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkEncryptedKey() (ssh.Signer, error) {
|
||||
logrus.Infof("[ssh] Checking private key")
|
||||
key, err := hosts.ParsePrivateKey(privateKeyPath())
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "decode encrypted private keys") {
|
||||
fmt.Printf("Passphrase for Private SSH Key: ")
|
||||
passphrase, err := terminal.ReadPassword(int(syscall.Stdin))
|
||||
fmt.Printf("\n")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
key, err = hosts.ParsePrivateKeyWithPassPhrase(privateKeyPath(), passphrase)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return key, nil
|
||||
}
|
||||
|
||||
func privateKeyPath() string {
|
||||
return os.Getenv("HOME") + "/.ssh/id_rsa"
|
||||
}
|
||||
|
Reference in New Issue
Block a user