-
Notifications
You must be signed in to change notification settings - Fork 18
/
arguments.go
124 lines (95 loc) · 2.88 KB
/
arguments.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
/*
SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Gardener contributors
SPDX-License-Identifier: Apache-2.0
*/
package ssh
import (
"fmt"
"strings"
"github.com/gardener/gardenctl-v2/internal/util"
)
type arguments struct {
list []argument
}
var _ fmt.Stringer = (*arguments)(nil)
type argument struct {
value string
shellEscapeDisabled bool
}
// String returns a shell escaped arguments string.
func (a *arguments) String() string {
var sb strings.Builder
for i, arg := range a.list {
if i > 0 {
sb.WriteString(" ")
}
value := arg.value
if !arg.shellEscapeDisabled {
value = util.ShellEscape(value)
}
sb.WriteString(value)
}
return sb.String()
}
func userKnownHostsFilesArgument(userKnownHostsFiles []string) *argument {
if len(userKnownHostsFiles) == 0 {
return nil
}
var escapedHostsFiles []string
for _, path := range userKnownHostsFiles {
escapedHostsFiles = append(escapedHostsFiles, util.ShellEscape(path))
}
userKnownHostsFilesValue := strings.Join(escapedHostsFiles, " ")
return &argument{value: fmt.Sprintf("-oUserKnownHostsFile=%s", userKnownHostsFilesValue)}
}
func sshCommandArguments(
bastionHost string,
bastionPort string,
sshPrivateKeyFile PrivateKeyFile,
bastionUserKnownHostsFiles []string,
nodeHostname string,
nodePrivateKeyFiles []PrivateKeyFile,
user string,
) arguments {
bastionUserKnownHostsFilesArg := userKnownHostsFilesArgument(bastionUserKnownHostsFiles)
proxyCmdArgs := sshProxyCmdArguments(
bastionHost,
bastionPort,
sshPrivateKeyFile,
bastionUserKnownHostsFilesArg,
)
args := []argument{
{value: "-oStrictHostKeyChecking=no", shellEscapeDisabled: true},
{value: "-oIdentitiesOnly=yes", shellEscapeDisabled: true},
}
for _, file := range nodePrivateKeyFiles {
args = append(args, argument{value: fmt.Sprintf("-i%s", file)})
}
args = append(args, argument{value: fmt.Sprintf("-oProxyCommand=%s", proxyCmdArgs.String())})
args = append(args, argument{value: fmt.Sprintf("%s@%s", user, nodeHostname)})
return arguments{list: args}
}
func sshProxyCmdArguments(
bastionHost string,
bastionPort string,
sshPrivateKeyFile PrivateKeyFile,
userKnownHostsFileArg *argument,
) arguments {
args := []argument{
{value: "ssh", shellEscapeDisabled: true},
{value: "-W%h:%p", shellEscapeDisabled: true},
{value: "-oStrictHostKeyChecking=no", shellEscapeDisabled: true},
}
if sshPrivateKeyFile != "" {
args = append(args, argument{value: "-oIdentitiesOnly=yes", shellEscapeDisabled: true})
args = append(args, argument{value: fmt.Sprintf("-i%s", sshPrivateKeyFile)})
}
if userKnownHostsFileArg != nil {
args = append(args, *userKnownHostsFileArg)
}
args = append(args, argument{value: fmt.Sprintf("%s@%s", SSHBastionUsername, bastionHost)})
if bastionPort != "" {
args = append(args, argument{value: fmt.Sprintf("-p%s", bastionPort)})
}
return arguments{list: args}
}