-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathssh_test.go
More file actions
279 lines (255 loc) · 7.85 KB
/
ssh_test.go
File metadata and controls
279 lines (255 loc) · 7.85 KB
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
package ssh
import (
"os"
"strings"
"testing"
)
func TestConsole_SSHNotFound(t *testing.T) {
t.Setenv("PATH", t.TempDir()) // empty dir, no ssh binary
err := Console(ConnConfig{Host: "10.0.0.1", User: "pixel"}, "")
if err == nil {
t.Fatal("expected error when ssh is not on PATH")
}
if got := err.Error(); !strings.Contains(got, "ssh binary not found") {
t.Errorf("error = %q, want it to contain %q", got, "ssh binary not found")
}
}
func TestSSHArgs(t *testing.T) {
t.Run("with key", func(t *testing.T) {
args := Args(ConnConfig{Host: "10.0.0.1", User: "pixel", KeyPath: "/tmp/key"})
wantSuffix := []string{"-i", "/tmp/key", "pixel@10.0.0.1"}
got := args[len(args)-3:]
for i, w := range wantSuffix {
if got[i] != w {
t.Errorf("args[%d] = %q, want %q", len(args)-3+i, got[i], w)
}
}
})
t.Run("always uses accept-new even without explicit KnownHostsPath", func(t *testing.T) {
args := Args(ConnConfig{Host: "10.0.0.1", User: "pixel"})
foundAcceptNew := false
for _, a := range args {
if a == "StrictHostKeyChecking=accept-new" {
foundAcceptNew = true
}
if a == "StrictHostKeyChecking=no" {
t.Error("should never use StrictHostKeyChecking=no")
}
if strings.Contains(a, os.DevNull) {
t.Errorf("should never use DevNull for known hosts, got %q", a)
}
}
if !foundAcceptNew {
t.Errorf("expected StrictHostKeyChecking=accept-new, got %v", args)
}
})
t.Run("accept-new with KnownHostsPath", func(t *testing.T) {
khFile := "/tmp/pixels-test-known-hosts"
args := Args(ConnConfig{Host: "10.0.0.1", User: "pixel", KnownHostsPath: khFile})
// Should use accept-new instead of no.
foundAcceptNew := false
for _, a := range args {
if a == "StrictHostKeyChecking=accept-new" {
foundAcceptNew = true
}
if a == "StrictHostKeyChecking=no" {
t.Error("should not use StrictHostKeyChecking=no when KnownHostsPath is set")
}
}
if !foundAcceptNew {
t.Errorf("expected StrictHostKeyChecking=accept-new, got %v", args)
}
// Should use the provided known hosts file.
want := "UserKnownHostsFile=" + khFile
found := false
for _, a := range args {
if a == want {
found = true
}
}
if !found {
t.Errorf("expected %q, got %v", want, args)
}
})
t.Run("without key", func(t *testing.T) {
args := Args(ConnConfig{Host: "10.0.0.1", User: "pixel"})
last := args[len(args)-1]
if last != "pixel@10.0.0.1" {
t.Errorf("last arg = %q, want %q", last, "pixel@10.0.0.1")
}
for _, a := range args {
if a == "-i" {
t.Error("should not include -i when keyPath is empty")
}
}
})
t.Run("SetEnv with env vars", func(t *testing.T) {
cc := ConnConfig{
Host: "10.0.0.1",
User: "pixel",
Env: map[string]string{
"GITHUB_TOKEN": "ghp_abc123",
"API_KEY": "sk-secret",
},
}
args := Args(cc)
// All vars should be in a single SetEnv directive (space-separated,
// sorted by key), preceded by -o. Multiple -o SetEnv flags don't
// stack in OpenSSH — only the first takes effect.
want := "SetEnv=API_KEY=sk-secret GITHUB_TOKEN=ghp_abc123"
var found bool
for i, a := range args {
if strings.HasPrefix(a, "SetEnv=") {
if i == 0 || args[i-1] != "-o" {
t.Errorf("SetEnv arg %q not preceded by -o", a)
}
if a != want {
t.Errorf("SetEnv = %q, want %q", a, want)
}
found = true
}
}
if !found {
t.Error("SetEnv not found in args")
}
// user@host should still be last.
if last := args[len(args)-1]; last != "pixel@10.0.0.1" {
t.Errorf("last arg = %q, want %q", last, "pixel@10.0.0.1")
}
})
t.Run("nil env produces no SetEnv", func(t *testing.T) {
args := Args(ConnConfig{Host: "10.0.0.1", User: "pixel"})
for _, a := range args {
if strings.HasPrefix(a, "SetEnv=") {
t.Errorf("unexpected SetEnv arg %q with nil env", a)
}
}
})
t.Run("empty env produces no SetEnv", func(t *testing.T) {
args := Args(ConnConfig{Host: "10.0.0.1", User: "pixel", Env: map[string]string{}})
for _, a := range args {
if strings.HasPrefix(a, "SetEnv=") {
t.Errorf("unexpected SetEnv arg %q with empty env", a)
}
}
})
}
func TestRemoveKnownHost(t *testing.T) {
t.Run("no-op when file is empty string", func(t *testing.T) {
if err := RemoveKnownHost("", "10.0.0.1"); err != nil {
t.Errorf("expected no error, got %v", err)
}
})
t.Run("no-op when file does not exist", func(t *testing.T) {
if err := RemoveKnownHost("/tmp/nonexistent-known-hosts-file", "10.0.0.1"); err != nil {
t.Errorf("expected no error for missing file, got %v", err)
}
})
t.Run("removes entry from existing file", func(t *testing.T) {
dir := t.TempDir()
khFile := dir + "/known_hosts"
// Use valid ssh-ed25519 key data (32 bytes base64-encoded with key type prefix).
key1 := "10.0.0.1 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBVlGh5YxGBMp/DO3OjAHsMR0DVQS2DJnpOqaGP2MkNl\n"
key2 := "10.0.0.2 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKxvLhGmlN1sdag3FISwEVfAGwC+v3+x0v6qIFNyGmNd\n"
if err := os.WriteFile(khFile, []byte(key1+key2), 0o600); err != nil {
t.Fatal(err)
}
if err := RemoveKnownHost(khFile, "10.0.0.1"); err != nil {
t.Fatalf("RemoveKnownHost: %v", err)
}
data, err := os.ReadFile(khFile)
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(data), "10.0.0.1") {
t.Errorf("expected 10.0.0.1 to be removed, file contains: %s", data)
}
if !strings.Contains(string(data), "10.0.0.2") {
t.Errorf("expected 10.0.0.2 to remain, file contains: %s", data)
}
})
}
func TestConsoleArgs(t *testing.T) {
t.Run("no remote command", func(t *testing.T) {
cc := ConnConfig{Host: "10.0.0.1", User: "pixel", KeyPath: "/tmp/key"}
args := consoleArgs(cc, "")
sshA := Args(cc)
if len(args) != len(sshA) {
t.Fatalf("len(consoleArgs) = %d, want %d (same as sshArgs)", len(args), len(sshA))
}
for i := range args {
if args[i] != sshA[i] {
t.Errorf("args[%d] = %q, want %q", i, args[i], sshA[i])
}
}
for _, a := range args {
if a == "-t" {
t.Error("should not include -t when remoteCmd is empty")
}
}
})
t.Run("no key with remote command", func(t *testing.T) {
cc := ConnConfig{Host: "10.0.0.1", User: "pixel"}
args := consoleArgs(cc, "zmx attach console")
last3 := args[len(args)-3:]
if last3[0] != "-t" {
t.Errorf("expected -t before user@host, got %q", last3[0])
}
if last3[1] != "pixel@10.0.0.1" {
t.Errorf("expected user@host, got %q", last3[1])
}
if last3[2] != "zmx attach console" {
t.Errorf("expected remote command, got %q", last3[2])
}
for _, a := range args {
if a == "-i" {
t.Error("should not include -i when keyPath is empty")
}
}
})
t.Run("with remote command", func(t *testing.T) {
cc := ConnConfig{Host: "10.0.0.1", User: "pixel", KeyPath: "/tmp/key"}
args := consoleArgs(cc, "zmx attach console")
// Should have -t before user@host and command after
last3 := args[len(args)-3:]
if last3[0] != "-t" {
t.Errorf("expected -t before user@host, got %q", last3[0])
}
if last3[1] != "pixel@10.0.0.1" {
t.Errorf("expected user@host, got %q", last3[1])
}
if last3[2] != "zmx attach console" {
t.Errorf("expected remote command, got %q", last3[2])
}
})
t.Run("with env and remote command", func(t *testing.T) {
cc := ConnConfig{
Host: "10.0.0.1",
User: "pixel",
KeyPath: "/tmp/key",
Env: map[string]string{"FOO": "bar"},
}
args := consoleArgs(cc, "zmx attach build")
// Verify SetEnv is present.
var foundSetEnv bool
for _, a := range args {
if strings.HasPrefix(a, "SetEnv=") {
foundSetEnv = true
}
}
if !foundSetEnv {
t.Error("SetEnv not found in args")
}
// Verify -t and command at end.
last3 := args[len(args)-3:]
if last3[0] != "-t" {
t.Errorf("expected -t, got %q", last3[0])
}
if last3[1] != "pixel@10.0.0.1" {
t.Errorf("expected user@host, got %q", last3[1])
}
if last3[2] != "zmx attach build" {
t.Errorf("expected remote command, got %q", last3[2])
}
})
}