forked from cloudfoundry/bosh-bootloader
/
ssh.go
180 lines (152 loc) · 4.25 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package commands
import (
"errors"
"fmt"
"os/exec"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/cloudfoundry/bosh-bootloader/fileio"
"github.com/cloudfoundry/bosh-bootloader/flags"
"github.com/cloudfoundry/bosh-bootloader/storage"
)
type SSH struct {
logger logger
cli sshCLI
keyGetter sshKeyGetter
pathFinder pathFinder
tempDirWriter tempDirWriter
randomPort randomPort
}
type sshCLI interface {
Run([]string) error
Start([]string) (*exec.Cmd, error)
}
type pathFinder interface {
CommandExists(string) bool
}
type randomPort interface {
GetPort() (string, error)
}
type tempDirWriter interface {
fileio.FileWriter
fileio.TempDirer
}
func NewSSH(logger logger, sshCLI sshCLI, sshKeyGetter sshKeyGetter, pathFinder pathFinder, tempDirWriter tempDirWriter, randomPort randomPort) SSH {
return SSH{
logger: logger,
cli: sshCLI,
keyGetter: sshKeyGetter,
pathFinder: pathFinder,
tempDirWriter: tempDirWriter,
randomPort: randomPort,
}
}
func (s SSH) CheckFastFails(subcommandFlags []string, state storage.State) error {
if len(state.Jumpbox.URL) == 0 {
return errors.New("Invalid bbl state for bbl ssh.")
}
return nil
}
func (s SSH) Execute(args []string, state storage.State) error {
var (
jumpbox bool
director bool
cmd string
)
sshFlags := flags.New("ssh")
sshFlags.Bool(&jumpbox, "jumpbox")
sshFlags.Bool(&director, "director")
sshFlags.String(&cmd, "cmd", "")
err := sshFlags.Parse(args)
if err != nil {
return err
}
if !jumpbox && !director {
return fmt.Errorf("This command requires the --jumpbox or --director flag.")
}
if jumpbox && len(cmd) > 0 {
return fmt.Errorf("Executing commands on jumpbox not supported (only on director).")
}
tempDir, err := s.tempDirWriter.TempDir("", "")
if err != nil {
return fmt.Errorf("Create temp directory: %s", err)
}
jumpboxKey, err := s.keyGetter.Get("jumpbox")
if err != nil {
return fmt.Errorf("Get jumpbox private key: %s", err)
}
jumpboxKeyPath := filepath.Join(tempDir, "jumpbox-private-key")
err = s.tempDirWriter.WriteFile(jumpboxKeyPath, []byte(jumpboxKey), 0600)
if err != nil {
return fmt.Errorf("Write private key file: %s", err)
}
jumpboxURL := strings.Split(state.Jumpbox.URL, ":")[0]
if jumpbox {
return s.cli.Run([]string{
"-tt",
"-o", "ServerAliveInterval=300",
fmt.Sprintf("jumpbox@%s", jumpboxURL),
"-i", jumpboxKeyPath,
})
}
directorPrivateKey, err := s.keyGetter.Get("director")
if err != nil {
return fmt.Errorf("Get director private key: %s", err)
}
directorKeyPath := filepath.Join(tempDir, "director-private-key")
err = s.tempDirWriter.WriteFile(directorKeyPath, []byte(directorPrivateKey), 0600)
if err != nil {
return fmt.Errorf("Write private key file: %s", err)
}
port, err := s.randomPort.GetPort()
if err != nil {
return fmt.Errorf("Open proxy port: %s", err)
}
s.logger.Println("checking host key")
err = s.cli.Run([]string{
"-T",
fmt.Sprintf("jumpbox@%s", jumpboxURL),
"-i", jumpboxKeyPath,
"echo", "host key confirmed",
})
if err != nil {
return fmt.Errorf("unable to verify host key fingerprint: %s", err)
}
s.logger.Println("opening a tunnel through your jumpbox")
backgroundTunnel, err := s.cli.Start([]string{
"-4",
"-D", port,
"-nNC",
fmt.Sprintf("jumpbox@%s", jumpboxURL),
"-i", jumpboxKeyPath,
})
if err != nil {
return fmt.Errorf("Open tunnel to jumpbox: %s", err)
}
defer func() {
if backgroundTunnel != nil {
backgroundTunnel.Process.Signal(syscall.SIGINT)
} // removing this will break the acceptance test
}()
proxyCommandPrefix := "nc -x"
if s.pathFinder.CommandExists("connect-proxy") {
proxyCommandPrefix = "connect-proxy -S"
}
ip := strings.Split(strings.TrimPrefix(state.BOSH.DirectorAddress, "https://"), ":")[0]
toExecute := []string{
"-tt",
"-o", "StrictHostKeyChecking=no",
"-o", "ServerAliveInterval=300",
"-o", fmt.Sprintf("ProxyCommand=%s localhost:%s %%h %%p", proxyCommandPrefix, port),
"-i", directorKeyPath,
fmt.Sprintf("jumpbox@%s", ip),
}
if len(cmd) > 0 {
toExecute = append(toExecute, cmd)
s.logger.Printf("executing command on director:\n%s\n", cmd)
}
time.Sleep(2 * time.Second) // make sure we give that tunnel a moment to open
return s.cli.Run(toExecute)
}