This repository was archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 605
Expand file tree
/
Copy pathssh.go
More file actions
93 lines (73 loc) · 2.28 KB
/
Copy pathssh.go
File metadata and controls
93 lines (73 loc) · 2.28 KB
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
package remote
import (
"errors"
"fmt"
"time"
"github.com/koding/kite"
"github.com/koding/logging"
"koding/klient/remote/req"
"koding/klient/sshkeys"
)
// standardTimeout is a timeout value to use for basic communications with remote
// machines. Do not use this for file transfers, or anything long running!
const standardTimeout = 30 * time.Second
// SSHKeyAddHandler adds the specified public SSH key to remote machine via the klient
// on the remote machine.
func (r *Remote) SSHKeyAddHandler(kreq *kite.Request) (interface{}, error) {
log := r.log.New("remote.sshKeyAdd")
var params req.SSHKeyAdd
if kreq.Args == nil {
return nil, errors.New("Required arguments were not passed.")
}
if err := kreq.Args.One().Unmarshal(¶ms); err != nil {
err = fmt.Errorf(
"remote.sshKeyAdd: Error '%s' while unmarshalling request '%s'\n",
err, kreq.Args.One(),
)
r.log.Error(err.Error())
return nil, err
}
if params.Debug {
log.SetLevel(logging.DEBUG)
}
switch {
case params.Name == "":
return nil, errors.New("Missing required argument `name`.")
case len(params.Key) == 0:
return nil, errors.New("Missing required argument `key`.")
}
log = log.New(
"mountName", params.Name,
// Using len to avoid printing the whole key.
"keyLength", len(params.Key),
)
remoteMachine, err := r.GetDialedMachine(params.Name)
if err != nil {
log.Error("Error getting dialed, valid machine. err:%s", err)
return nil, err
}
username := params.Username
// If the username is empty, default it to the current user on the remote side.
if params.Username == "" {
log.Debug("Username empty, looking up default.")
res, err := remoteMachine.TellWithTimeout("os.currentUsername", standardTimeout)
if err != nil {
log.Debug("Error from remote machine os.currentUsername method. err:%s", err)
return nil, err
}
if err := res.Unmarshal(&username); err != nil {
log.Debug("Failed to unmarshal username string")
return nil, err
}
}
log.Debug("Adding key for username %q", username)
var sshReq = sshkeys.AddOptions{
Username: username,
Keys: []string{string(params.Key)},
}
if _, err := remoteMachine.Tell("sshkeys.add", sshReq); err != nil {
log.Debug("Error from remote machine sshkeys.add method. err:%s", err)
return nil, err
}
return nil, nil
}