Skip to content

Commit 7d38d53

Browse files
Nilesh Trivedikaralabe
authored andcommitted
cmd/puppeth: accept ssh identity in the server string (#17407)
* cmd/puppeth: Accept identityfile in the server string with fallback to id_rsa * cmd/puppeth: code polishes + fix heath check double ports
1 parent 1de9ada commit 7d38d53

2 files changed

Lines changed: 34 additions & 26 deletions

File tree

‎cmd/puppeth/ssh.go‎

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,33 +45,44 @@ type sshClient struct {
4545

4646
// dial establishes an SSH connection to a remote node using the current user and
4747
// the user's configured private RSA key. If that fails, password authentication
48-
// is fallen back to. The caller may override the login user via user@server:port.
48+
// is fallen back to. server can be a string like user:identity@server:port.
4949
func dial(server string, pubkey []byte) (*sshClient, error) {
50-
// Figure out a label for the server and a logger
51-
label := server
52-
if strings.Contains(label, ":") {
53-
label = label[:strings.Index(label, ":")]
54-
}
55-
login := ""
50+
// Figure out username, identity, hostname and port
51+
hostname := ""
52+
hostport := server
53+
username := ""
54+
identity := "id_rsa" // default
55+
5656
if strings.Contains(server, "@") {
57-
login = label[:strings.Index(label, "@")]
58-
label = label[strings.Index(label, "@")+1:]
59-
server = server[strings.Index(server, "@")+1:]
57+
prefix := server[:strings.Index(server, "@")]
58+
if strings.Contains(prefix, ":") {
59+
username = prefix[:strings.Index(prefix, ":")]
60+
identity = prefix[strings.Index(prefix, ":")+1:]
61+
} else {
62+
username = prefix
63+
}
64+
hostport = server[strings.Index(server, "@")+1:]
6065
}
61-
logger := log.New("server", label)
66+
if strings.Contains(hostport, ":") {
67+
hostname = hostport[:strings.Index(hostport, ":")]
68+
} else {
69+
hostname = hostport
70+
hostport += ":22"
71+
}
72+
logger := log.New("server", server)
6273
logger.Debug("Attempting to establish SSH connection")
6374

6475
user, err := user.Current()
6576
if err != nil {
6677
return nil, err
6778
}
68-
if login == "" {
69-
login = user.Username
79+
if username == "" {
80+
username = user.Username
7081
}
7182
// Configure the supported authentication methods (private key and password)
7283
var auths []ssh.AuthMethod
7384

74-
path := filepath.Join(user.HomeDir, ".ssh", "id_rsa")
85+
path := filepath.Join(user.HomeDir, ".ssh", identity)
7586
if buf, err := ioutil.ReadFile(path); err != nil {
7687
log.Warn("No SSH key, falling back to passwords", "path", path, "err", err)
7788
} else {
@@ -94,25 +105,22 @@ func dial(server string, pubkey []byte) (*sshClient, error) {
94105
}
95106
}
96107
auths = append(auths, ssh.PasswordCallback(func() (string, error) {
97-
fmt.Printf("What's the login password for %s at %s? (won't be echoed)\n> ", login, server)
108+
fmt.Printf("What's the login password for %s at %s? (won't be echoed)\n> ", username, server)
98109
blob, err := terminal.ReadPassword(int(os.Stdin.Fd()))
99110

100111
fmt.Println()
101112
return string(blob), err
102113
}))
103114
// Resolve the IP address of the remote server
104-
addr, err := net.LookupHost(label)
115+
addr, err := net.LookupHost(hostname)
105116
if err != nil {
106117
return nil, err
107118
}
108119
if len(addr) == 0 {
109120
return nil, errors.New("no IPs associated with domain")
110121
}
111122
// Try to dial in to the remote server
112-
logger.Trace("Dialing remote SSH server", "user", login)
113-
if !strings.Contains(server, ":") {
114-
server += ":22"
115-
}
123+
logger.Trace("Dialing remote SSH server", "user", username)
116124
keycheck := func(hostname string, remote net.Addr, key ssh.PublicKey) error {
117125
// If no public key is known for SSH, ask the user to confirm
118126
if pubkey == nil {
@@ -139,13 +147,13 @@ func dial(server string, pubkey []byte) (*sshClient, error) {
139147
// We have a mismatch, forbid connecting
140148
return errors.New("ssh key mismatch, readd the machine to update")
141149
}
142-
client, err := ssh.Dial("tcp", server, &ssh.ClientConfig{User: login, Auth: auths, HostKeyCallback: keycheck})
150+
client, err := ssh.Dial("tcp", hostport, &ssh.ClientConfig{User: username, Auth: auths, HostKeyCallback: keycheck})
143151
if err != nil {
144152
return nil, err
145153
}
146154
// Connection established, return our utility wrapper
147155
c := &sshClient{
148-
server: label,
156+
server: hostname,
149157
address: addr[0],
150158
pubkey: pubkey,
151159
client: client,

‎cmd/puppeth/wizard_network.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ func (w *wizard) manageServers() {
6262
}
6363
}
6464

65-
// makeServer reads a single line from stdin and interprets it as a hostname to
66-
// connect to. It tries to establish a new SSH session and also executing some
67-
// baseline validations.
65+
// makeServer reads a single line from stdin and interprets it as
66+
// username:identity@hostname to connect to. It tries to establish a
67+
// new SSH session and also executing some baseline validations.
6868
//
6969
// If connection succeeds, the server is added to the wizards configs!
7070
func (w *wizard) makeServer() string {
7171
fmt.Println()
72-
fmt.Println("Please enter remote server's address:")
72+
fmt.Println("What is the remote server's address ([username[:identity]@]hostname[:port])?")
7373

7474
// Read and dial the server to ensure docker is present
7575
input := w.readString()

0 commit comments

Comments
 (0)