generated from brevdev/seed
-
Notifications
You must be signed in to change notification settings - Fork 16
/
ssh.go
545 lines (498 loc) · 15.6 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
// Package ssh exists to provide an api to configure and read from
// an ssh file
//
// brev ssh host file entry format:
//
// Host <workspace-dns-name>
// Hostname 0.0.0.0
// IdentityFile /home//.brev/brev.pem
// User brev
// Port <some-available-port>
//
// also think that file stuff should probably live in files package
// TODO migrate to using dns name for hostname
package ssh
import (
"bytes"
"encoding/xml"
"fmt"
"strconv"
"strings"
"text/template"
"github.com/brevdev/brev-cli/pkg/entity"
breverrors "github.com/brevdev/brev-cli/pkg/errors"
"github.com/kevinburke/ssh_config"
)
const workspaceSSHConfigTemplate = `Host {{ .Host }}
Hostname {{ .Hostname }}
IdentityFile {{ .IdentityFile }}
User brev
Port {{ .Port }}
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
RequestTTY yes
RemoteCommand cd {{ .Dir }}; $SHELL
`
type (
BrevPorts map[string]bool
BrevHostValuesSet map[entity.WorkspaceLocalID]bool
IdentityPortMap map[entity.WorkspaceLocalID]string
workspaceSSHConfig struct {
Host entity.WorkspaceLocalID
Hostname string
User string
IdentityFile string
Port string
Dir string
}
SSHStore interface {
GetUserSSHConfig() (string, error)
WriteUserSSHConfig(config string) error
CreateNewSSHConfigBackup() error
GetPrivateKeyPath() (string, error)
}
Reader interface {
GetBrevPorts() (BrevPorts, error)
GetBrevHostValueSet() BrevHostValuesSet
GetConfiguredWorkspacePort(workspaceIdentifier entity.WorkspaceLocalID) (string, error)
}
Writer interface {
Sync(IdentityPortMap) error
}
SSHConfig struct {
store SSHStore
sshConfig *ssh_config.Config
privateKey string
}
SSHConfigurerStore interface {
WritePrivateKey(pem string) error
}
SSHConfigurer struct {
store SSHConfigurerStore
Reader Reader
Writers []Writer
workspaces []entity.WorkspaceWithMeta
privateKey string
}
JetBrainsGatewayConfigStore interface {
GetJetBrainsConfigPath() (string, error)
GetJetBrainsConfig() (string, error)
WriteJetBrainsConfig(config string) error
GetPrivateKeyPath() (string, error)
}
JetBrainsGatewayConfig struct {
config *JetbrainsGatewayConfigXML
Reader
Writer
store JetBrainsGatewayConfigStore
}
JetbrainsGatewayConfigXMLSSHOption struct {
Name string `xml:"name,attr,omitempty"`
Value string `xml:"value,attr,omitempty"`
}
JetbrainsGatewayConfigXMLSSHConfig struct {
ID string `xml:"id,attr,omitempty"`
CustomName entity.WorkspaceLocalID `xml:"customName,attr,omitempty"`
NameFormat string `xml:"nameFormat,attr,omitempty"`
UseOpenSSHConfig string `xml:"useOpenSSHConfig,attr,omitempty"`
Host string `xml:"host,attr,omitempty"`
Port string `xml:"port,attr,omitempty"`
KeyPath string `xml:"keyPath,attr,omitempty"`
Username string `xml:"username,attr,omitempty"`
ConnectionConfig string `xml:"connectionConfig,attr,omitempty"`
Options []JetbrainsGatewayConfigXMLSSHOption `xml:"option,omitempty"`
}
JetbrainsGatewayConfigXMLConfigs struct {
SSHConfigs []JetbrainsGatewayConfigXMLSSHConfig `xml:"sshConfig"`
}
JetbrainsGatewayConfigXMLComponent struct {
Configs JetbrainsGatewayConfigXMLConfigs `xml:"configs"`
Name string `xml:"name,attr,omitempty"`
}
JetbrainsGatewayConfigXML struct {
XMLName xml.Name `xml:"application"`
Component JetbrainsGatewayConfigXMLComponent `xml:"component"`
}
)
func workspaceIdentifierFromHost(hoststring string) entity.WorkspaceLocalID {
hoststring = strings.TrimSpace(hoststring)
if hoststring == "" {
return ""
}
newLineSplit := strings.Split(hoststring, "\n")
if len(newLineSplit) < 1 {
return ""
}
spaceSplit := strings.Split(newLineSplit[0], " ")
if len(spaceSplit) < 2 {
return ""
}
return entity.WorkspaceLocalID(spaceSplit[1])
}
func checkIfBrevHost(host ssh_config.Host, privateKeyPath string) bool {
for _, node := range host.Nodes {
switch n := node.(type) { //nolint:gocritic // ignoring since want to keep options open for many cases
case *ssh_config.KV:
if strings.Compare(n.Key, "IdentityFile") == 0 {
if strings.Compare(privateKeyPath, n.Value) == 0 {
return true
}
}
}
}
return false
}
func checkIfHostIsActive(hoststring string, activeWorksSpaces []entity.WorkspaceLocalID) bool {
maybeHostname := workspaceIdentifierFromHost(hoststring)
for _, workspaceIdentifier := range activeWorksSpaces {
if workspaceIdentifier == maybeHostname {
return true
}
}
return false
}
// if a host is not a brev entry, it should stay in the config and there
// is nothing for us to do to it.
// if the host is a brev entry, make sure that it's hostname maps to an
// active workspace, otherwise this host should be deleted.
func createConfigEntry(hoststring string, isBrevHost, isActiveHost bool) string {
if !isBrevHost {
return hoststring
}
if isBrevHost && isActiveHost {
return hoststring
}
return ""
}
func sshConfigFromString(config string) (*ssh_config.Config, error) {
sshConfig, err := ssh_config.Decode(strings.NewReader(config))
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
return sshConfig, nil
}
func MakeSSHEntry(workspaceIdentifier entity.WorkspaceLocalID, port string, privateKeyPath string, dir string) (string, error) {
wsc := workspaceSSHConfig{
Host: workspaceIdentifier,
Hostname: "localhost",
User: "brev",
IdentityFile: privateKeyPath,
Port: port,
Dir: dir,
}
tmpl, err := template.New(string(workspaceIdentifier)).Parse(workspaceSSHConfigTemplate)
if err != nil {
return "", breverrors.WrapAndTrace(err)
}
buf := &bytes.Buffer{}
err = tmpl.Execute(buf, wsc)
if err != nil {
return "", breverrors.WrapAndTrace(err)
}
return buf.String(), nil
}
func ParseJetbrainsGatewayXML(config string) (*JetbrainsGatewayConfigXML, error) {
var jetbrainsGatewayConfigXML JetbrainsGatewayConfigXML
if err := xml.Unmarshal([]byte(config), &jetbrainsGatewayConfigXML); err != nil {
return nil, breverrors.WrapAndTrace(err)
}
return &jetbrainsGatewayConfigXML, nil
}
var (
_ Reader = SSHConfig{}
_ Writer = &SSHConfig{}
)
func NewSSHConfig(store SSHStore) (*SSHConfig, error) {
configStr, err := store.GetUserSSHConfig()
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
sshConfig, err := ssh_config.Decode(strings.NewReader(configStr))
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
privateKeyPath, err := store.GetPrivateKeyPath()
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
return &SSHConfig{
store: store,
sshConfig: sshConfig,
privateKey: privateKeyPath,
}, nil
}
func (s *SSHConfig) PruneInactiveWorkspaces(identityPortMap IdentityPortMap) error {
newConfig := ""
var activeWorkspaces []entity.WorkspaceLocalID
for key := range identityPortMap {
activeWorkspaces = append(activeWorkspaces, key)
}
privateKeyPath, err := s.store.GetPrivateKeyPath()
if err != nil {
return breverrors.WrapAndTrace(err)
}
for _, host := range s.sshConfig.Hosts {
hoststring := host.String()
isBrevHost := checkIfBrevHost(*host, privateKeyPath)
isActiveHost := checkIfHostIsActive(hoststring, activeWorkspaces)
newConfig += createConfigEntry(hoststring, isBrevHost, isActiveHost)
}
sshConfig, err := sshConfigFromString(newConfig)
if err != nil {
return breverrors.WrapAndTrace(err)
}
s.sshConfig = sshConfig
return nil
}
// Hostname is a loaded term so using values
func (s SSHConfig) GetBrevHostValues() []entity.WorkspaceLocalID {
privateKeyPath := s.privateKey
var brevHosts []entity.WorkspaceLocalID
for _, host := range s.sshConfig.Hosts {
hostname := workspaceIdentifierFromHost(host.String())
// is this host a brev entry? if not, we don't care, and on to the
// next one
if checkIfBrevHost(*host, privateKeyPath) {
brevHosts = append(brevHosts, hostname)
}
}
return brevHosts
}
func (s *SSHConfig) Sync(identifierPortMapping IdentityPortMap) error {
sshConfigStr := s.sshConfig.String()
brevhosts := s.GetBrevHostValueSet()
for key, value := range identifierPortMapping {
if !brevhosts[key] {
entry, err2 := MakeSSHEntry(key, value, s.privateKey, "/home/brev/workspace")
if err2 != nil {
return breverrors.WrapAndTrace(err2)
}
sshConfigStr += entry
}
}
var err error
s.sshConfig, err = ssh_config.Decode(strings.NewReader(sshConfigStr))
if err != nil {
return breverrors.WrapAndTrace(err)
}
err = s.PruneInactiveWorkspaces(identifierPortMapping)
if err != nil {
return breverrors.WrapAndTrace(err)
}
err = s.store.WriteUserSSHConfig(s.sshConfig.String())
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
}
func (s SSHConfig) GetBrevPorts() (BrevPorts, error) {
portSet := make(BrevPorts)
hostnames := s.GetBrevHostValues()
for _, name := range hostnames {
port, err := s.sshConfig.Get(string(name), "Port")
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
portSet[port] = true
}
return portSet, nil
}
func (s SSHConfig) GetBrevHostValueSet() BrevHostValuesSet {
brevHostValuesSet := make(BrevHostValuesSet)
brevHostValues := s.GetBrevHostValues()
for _, hostValue := range brevHostValues {
brevHostValuesSet[hostValue] = true
}
return brevHostValuesSet
}
func (s SSHConfig) GetConfiguredWorkspacePort(workspaceIdentifier entity.WorkspaceLocalID) (string, error) {
config, err := NewSSHConfig(s.store) // forces load from disk
if err != nil {
return "", breverrors.WrapAndTrace(err)
}
port, err := config.sshConfig.Get(string(workspaceIdentifier), "Port")
if err != nil {
return "", breverrors.WrapAndTrace(err)
}
return port, nil
}
func (s SSHConfig) GetPrivateKeyFilePath() string {
return s.privateKey
}
func NewSSHConfigurer(workspaces []entity.WorkspaceWithMeta, reader Reader, writers []Writer, store SSHConfigurerStore, privateKey string) *SSHConfigurer {
return &SSHConfigurer{
workspaces: workspaces,
Reader: reader,
Writers: writers,
store: store,
privateKey: privateKey,
}
}
func (sshConfigurer SSHConfigurer) GetPrivateKeyPath() (string, error) {
return sshConfigurer.privateKey, nil
}
func (sshConfigurer *SSHConfigurer) GetIdentityPortMap() (IdentityPortMap, error) {
activeWorkspaces := sshConfigurer.GetActiveWorkspaceIdentifiers()
brevHostValuesSet := sshConfigurer.Reader.GetBrevHostValueSet()
ports, err := sshConfigurer.Reader.GetBrevPorts()
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
port := 2222
identifierPortMapping := make(map[entity.WorkspaceLocalID]string)
for _, workspaceIdentifier := range activeWorkspaces {
if !brevHostValuesSet[workspaceIdentifier] {
for ports[fmt.Sprint(port)] {
port++
}
identifierPortMapping[workspaceIdentifier] = strconv.Itoa(port)
ports[fmt.Sprint(port)] = true
} else {
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
p, err := sshConfigurer.GetConfiguredWorkspacePort(workspaceIdentifier)
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
identifierPortMapping[workspaceIdentifier] = p
ports[fmt.Sprint(port)] = true
}
}
return identifierPortMapping, nil
}
func (sshConfigurer *SSHConfigurer) Sync() error {
err := sshConfigurer.store.WritePrivateKey(sshConfigurer.privateKey)
if err != nil {
return breverrors.WrapAndTrace(err)
}
identityPortMap, err := sshConfigurer.GetIdentityPortMap()
if err != nil {
return breverrors.WrapAndTrace(err)
}
for _, writer := range sshConfigurer.Writers {
err := writer.Sync(identityPortMap)
if err != nil {
return breverrors.WrapAndTrace(err)
}
}
return nil
}
func (sshConfigurer *SSHConfigurer) GetActiveWorkspaceIdentifiers() []entity.WorkspaceLocalID {
var workspaceIdentifiers []entity.WorkspaceLocalID
for _, workspace := range sshConfigurer.workspaces {
fmt.Println(workspace.Name)
workspaceIdentifiers = append(workspaceIdentifiers, workspace.GetLocalIdentifier())
}
return workspaceIdentifiers
}
func (sshConfigurer SSHConfigurer) GetConfiguredWorkspacePort(workspaceIdentifier entity.WorkspaceLocalID) (string, error) {
port, err := sshConfigurer.Reader.GetConfiguredWorkspacePort(workspaceIdentifier)
if err != nil {
return "", breverrors.WrapAndTrace(err)
}
return port, nil
}
var (
_ Reader = &JetBrainsGatewayConfig{}
_ Writer = &JetBrainsGatewayConfig{}
)
func NewJetBrainsGatewayConfig(store JetBrainsGatewayConfigStore) (*JetBrainsGatewayConfig, error) {
config, err := store.GetJetBrainsConfig()
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
if config == "" {
return &JetBrainsGatewayConfig{
config: &JetbrainsGatewayConfigXML{},
store: store,
}, nil
}
jetbrainsGatewayConfigXML, err := ParseJetbrainsGatewayXML(config)
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
return &JetBrainsGatewayConfig{
config: jetbrainsGatewayConfigXML,
store: store,
}, nil
}
func (jbgc *JetBrainsGatewayConfig) Sync(identifierPortMapping IdentityPortMap) error {
brevhosts := jbgc.GetBrevHostValueSet()
activeWorkspaces := make(map[entity.WorkspaceLocalID]bool)
privateKeyPath, err := jbgc.store.GetPrivateKeyPath()
if err != nil {
return breverrors.WrapAndTrace(err)
}
for key, value := range identifierPortMapping {
if !brevhosts[key] {
jbgc.config.Component.Configs.SSHConfigs = append(jbgc.config.Component.Configs.SSHConfigs, JetbrainsGatewayConfigXMLSSHConfig{
Host: "localhost",
Port: value,
KeyPath: privateKeyPath,
Username: "brev",
CustomName: (key),
NameFormat: "CUSTOM",
Options: []JetbrainsGatewayConfigXMLSSHOption{
{
Name: "CustomName",
Value: string(key),
},
},
})
}
activeWorkspaces[(key)] = true
}
var sshConfigs []JetbrainsGatewayConfigXMLSSHConfig
for _, conf := range jbgc.config.Component.Configs.SSHConfigs {
isBrevHost := conf.KeyPath == privateKeyPath
isActiveWorkspace := activeWorkspaces[conf.CustomName]
if !isBrevHost {
sshConfigs = append(sshConfigs, conf)
} else if isBrevHost && isActiveWorkspace {
sshConfigs = append(sshConfigs, conf)
}
}
jbgc.config.Component.Configs.SSHConfigs = sshConfigs
output, err := xml.MarshalIndent(jbgc.config, "", " ")
if err != nil {
return breverrors.WrapAndTrace(err)
}
err = jbgc.store.WriteJetBrainsConfig(string(output))
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
}
func (jbgc *JetBrainsGatewayConfig) GetBrevPorts() (BrevPorts, error) {
ports := make(BrevPorts)
for _, sshConf := range jbgc.config.Component.Configs.SSHConfigs {
ports[sshConf.Port] = true
}
return ports, nil
}
func (jbgc *JetBrainsGatewayConfig) GetBrevHostValueSet() BrevHostValuesSet {
brevHostValueSet := make(BrevHostValuesSet)
for _, sshConf := range jbgc.config.Component.Configs.SSHConfigs {
brevHostValueSet[sshConf.CustomName] = true
}
return brevHostValueSet
}
func (jbgc *JetBrainsGatewayConfig) GetConfiguredWorkspacePort(workspaceIdentifier entity.WorkspaceLocalID) (string, error) {
// load config from disk
config, err := jbgc.store.GetJetBrainsConfig()
if err != nil {
return "", breverrors.WrapAndTrace(err)
}
jetbrainsGatewayConfigXML, err := ParseJetbrainsGatewayXML(config)
if err != nil {
return "", breverrors.WrapAndTrace(err)
}
for _, sshConf := range jetbrainsGatewayConfigXML.Component.Configs.SSHConfigs {
if sshConf.CustomName == workspaceIdentifier {
return sshConf.Port, nil
}
}
return "", nil
}