-
Notifications
You must be signed in to change notification settings - Fork 15
/
ssh.go
140 lines (123 loc) · 2.79 KB
/
ssh.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
package cmd
import (
"context"
"errors"
"os"
"os/exec"
"os/user"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
"github.com/cybozu-go/cke"
"github.com/cybozu-go/log"
"github.com/cybozu-go/well"
"github.com/spf13/cobra"
)
func detectSSHNode(arg string) string {
nodeName := arg
if strings.Contains(arg, "@") {
nodeName = arg[strings.Index(arg, "@")+1:]
}
return nodeName
}
func writeToFifo(fifo string, data string) {
f, err := os.OpenFile(fifo, os.O_WRONLY, 0600)
if err != nil {
log.Error("failed to open fifo", map[string]interface{}{
log.FnError: err,
"fifo": fifo,
})
return
}
defer f.Close()
_, err = f.WriteString(data)
if err != nil {
log.Error("failed to write to fifo", map[string]interface{}{
log.FnError: err,
"fifo": fifo,
})
}
}
func sshPrivateKey(nodeName string) (string, error) {
usr, err := user.Current()
if err != nil {
return "", err
}
err = os.MkdirAll(filepath.Join(usr.HomeDir, ".ssh"), 0700)
if err != nil {
return "", err
}
fifo := filepath.Join(usr.HomeDir, ".ssh", "ckecli-ssh-key-"+strconv.Itoa(os.Getpid()))
err = syscall.Mkfifo(fifo, 0600)
if err != nil {
return "", err
}
vc, err := inf.Vault()
if err != nil {
return "", err
}
secret, err := vc.Logical().Read(cke.SSHSecret)
if err != nil {
return "", err
}
if secret == nil {
return "", errors.New("no ssh private keys")
}
privKeys := secret.Data
mykey, ok := privKeys[nodeName]
if !ok {
mykey = privKeys[""]
}
if mykey == nil {
return "", errors.New("no ssh private key for " + nodeName)
}
go func() {
writeToFifo(fifo, mykey.(string))
time.Sleep(100 * time.Millisecond)
// OpenSSH reads the private key file twice, it need to write key twice.
writeToFifo(fifo, mykey.(string))
}()
return fifo, nil
}
func ssh(ctx context.Context, args []string) error {
node := detectSSHNode(args[0])
fifo, err := sshPrivateKey(node)
if err != nil {
return err
}
defer os.Remove(fifo)
sshArgs := []string{
"-i", fifo,
"-o", "UserKnownHostsFile=/dev/null",
"-o", "StrictHostKeyChecking=no",
"-o", "ConnectTimeout=60",
}
sshArgs = append(sshArgs, args...)
c := exec.CommandContext(ctx, "ssh", sshArgs...)
c.Stdin = os.Stdin
c.Stdout = os.Stdout
c.Stderr = os.Stderr
return c.Run()
}
// sshCmd represents the ssh command
var sshCmd = &cobra.Command{
Use: "ssh [user@]NODE [COMMAND...]",
Short: "connect to the node via ssh",
Long: `Connect to the node via ssh.
NODE is IP address or hostname of the node to be connected.
If COMMAND is specified, it will be executed on the node.
`,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
well.Go(func(ctx context.Context) error {
return ssh(ctx, args)
})
well.Stop()
return well.Wait()
},
}
func init() {
rootCmd.AddCommand(sshCmd)
}