/
ssh.go
193 lines (161 loc) · 4.65 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
181
182
183
184
185
186
187
188
189
190
191
192
193
package cmd
import (
"errors"
bihttpagent "github.com/cloudfoundry/bosh-agent/agentclient/http"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshdir "github.com/cloudfoundry/bosh-cli/v7/director"
boshssh "github.com/cloudfoundry/bosh-cli/v7/ssh"
boshui "github.com/cloudfoundry/bosh-cli/v7/ui"
. "github.com/cloudfoundry/bosh-cli/v7/cmd/opts"
)
type SSHCmd struct {
deployment boshdir.Deployment
intSSHRunner boshssh.Runner
nonIntSSHRunner boshssh.Runner
resultsSSHRunner boshssh.Runner
ui boshui.UI
hostBuilder boshssh.HostBuilder
}
func NewSSHCmd(
intSSHRunner boshssh.Runner,
nonIntSSHRunner boshssh.Runner,
resultsSSHRunner boshssh.Runner,
ui boshui.UI,
hostBuilder boshssh.HostBuilder,
) SSHCmd {
return SSHCmd{
intSSHRunner: intSSHRunner,
nonIntSSHRunner: nonIntSSHRunner,
resultsSSHRunner: resultsSSHRunner,
ui: ui,
hostBuilder: hostBuilder,
}
}
func (c SSHCmd) Run(opts SSHOpts, deploymentFetcher boshssh.DeploymentFetcher) error {
if opts.Results || !c.ui.IsInteractive() {
if len(opts.Command) == 0 {
return bosherr.Errorf("Non-interactive SSH requires non-empty command")
}
}
sshOpts, connOpts, err := opts.GatewayFlags.AsSSHOpts()
if err != nil {
return err
}
connOpts.RawOpts = opts.RawOpts.AsStrings()
var result boshdir.SSHResult
if opts.PrivateKey.Bytes == nil {
c.deployment, err = deploymentFetcher()
if err != nil {
return err
}
// host key will be returned by agent over NATS
connOpts.RawOpts = append(connOpts.RawOpts, "-o", "StrictHostKeyChecking=yes")
result, err = c.deployment.SetUpSSH(opts.Args.Slug, sshOpts)
if err != nil {
return err
}
defer func() {
_ = c.deployment.CleanUpSSH(opts.Args.Slug, sshOpts)
}()
} else {
// no automatic source of host key
connOpts.RawOpts = append(connOpts.RawOpts, "-o", "StrictHostKeyChecking=no")
connOpts.PrivateKey = string(opts.PrivateKey.Bytes)
host, err := c.hostBuilder.BuildHost(opts.Args.Slug, opts.Username, deploymentFetcher)
if err != nil {
return err
}
result = boshdir.SSHResult{
Hosts: []boshdir.Host{
host,
},
GatewayUsername: connOpts.GatewayUsername,
GatewayHost: connOpts.GatewayHost,
}
}
var runner boshssh.Runner
if opts.Results {
runner = c.resultsSSHRunner
} else if !c.ui.IsInteractive() || len(opts.Command) > 0 {
runner = c.nonIntSSHRunner
} else {
runner = c.intSSHRunner
}
err = runner.Run(connOpts, result, opts.Command)
if err != nil {
return bosherr.WrapErrorf(err, "Running SSH")
}
return nil
}
type EnvSSHCmd struct {
agentClientFactory bihttpagent.AgentClientFactory
intSSHRunner boshssh.Runner
nonIntSSHRunner boshssh.Runner
resultsSSHRunner boshssh.Runner
ui boshui.UI
}
func NewEnvSSHCmd(
agentClientFactory bihttpagent.AgentClientFactory,
intSSHRunner boshssh.Runner,
nonIntSSHRunner boshssh.Runner,
resultsSSHRunner boshssh.Runner,
ui boshui.UI,
) EnvSSHCmd {
return EnvSSHCmd{
agentClientFactory: agentClientFactory,
intSSHRunner: intSSHRunner,
nonIntSSHRunner: nonIntSSHRunner,
resultsSSHRunner: resultsSSHRunner,
ui: ui,
}
}
func (c EnvSSHCmd) Run(opts SSHOpts) error {
if opts.PrivateKey.Bytes != nil {
return errors.New("the --private-key flag is not supported in combination with the --director flag")
}
if opts.Endpoint == "" || opts.Certificate == "" {
return errors.New("the --director flag requires both the --agent-endpoint and --agent-certificate flags to be set")
}
agentClient, err := c.agentClientFactory.NewAgentClient("bosh-cli", opts.Endpoint, opts.Certificate)
if err != nil {
return err
}
sshOpts, connOpts, err := opts.GatewayFlags.AsSSHOpts()
if err != nil {
return err
}
connOpts.RawOpts = opts.RawOpts.AsStrings()
agentResult, err := agentClient.SetUpSSH(sshOpts.Username, sshOpts.PublicKey)
if err != nil {
return err
}
result := boshdir.SSHResult{
Hosts: []boshdir.Host{
{
Username: sshOpts.Username,
Host: agentResult.Ip,
HostPublicKey: agentResult.HostPublicKey,
Job: "create-env-vm",
IndexOrID: "0",
},
},
}
defer func() {
_, _ = agentClient.CleanUpSSH(sshOpts.Username)
}()
// host key will be returned by agent over HTTPS
connOpts.RawOpts = append(connOpts.RawOpts, "-o", "StrictHostKeyChecking=yes")
var runner boshssh.Runner
if opts.Results {
runner = c.resultsSSHRunner
} else if !c.ui.IsInteractive() || len(opts.Command) > 0 {
runner = c.nonIntSSHRunner
} else {
runner = c.intSSHRunner
}
err = runner.Run(connOpts, result, opts.Command)
if err != nil {
return bosherr.WrapErrorf(err, "Running SSH")
}
return nil
}