forked from loft-sh/devpod
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathssh.go
More file actions
443 lines (385 loc) · 14.2 KB
/
ssh.go
File metadata and controls
443 lines (385 loc) · 14.2 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
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
package ssh
import (
"context"
"math/rand"
"net"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
"github.com/skevetter/devpod/e2e/framework"
)
const osWindows = "windows"
var _ = ginkgo.Describe("devpod ssh test suite", ginkgo.Label("ssh"), ginkgo.Ordered, func() {
var initialDir string
ginkgo.BeforeEach(func() {
var err error
initialDir, err = os.Getwd()
framework.ExpectNoError(err)
})
ginkgo.It(
"should start a new workspace with a docker provider (default) and SSH into it",
func(ctx context.Context) {
tempDir, err := framework.CopyToTempDir("tests/ssh/testdata/local-test")
framework.ExpectNoError(err)
f := framework.NewDefaultFramework(initialDir + "/bin")
_ = f.DevPodProviderAdd(ctx, "docker")
err = f.DevPodProviderUse(ctx, "docker")
framework.ExpectNoError(err)
ginkgo.DeferCleanup(func(cleanupCtx context.Context) {
_ = f.DevPodWorkspaceDelete(cleanupCtx, tempDir)
framework.CleanupTempDir(initialDir, tempDir)
})
// Start up devpod workspace
devpodUpDeadline := time.Now().Add(5 * time.Minute)
devpodUpCtx, cancel := context.WithDeadline(ctx, devpodUpDeadline)
defer cancel()
err = f.DevPodUp(devpodUpCtx, tempDir)
framework.ExpectNoError(err)
devpodSSHDeadline := time.Now().Add(20 * time.Second)
devpodSSHCtx, cancelSSH := context.WithDeadline(ctx, devpodSSHDeadline)
defer cancelSSH()
err = f.DevPodSSHEchoTestString(devpodSSHCtx, tempDir)
framework.ExpectNoError(err)
},
)
// ginkgo.It("should start a new workspace with a docker provider (default) and forward gpg agent into it", func() {
// // skip windows for now
// if runtime.GOOS == osWindows {
// return
// }
//
// tempDir, err := framework.CopyToTempDir("tests/ssh/testdata/gpg-forwarding")
// framework.ExpectNoError(err)
// defer framework.CleanupTempDir(initialDir, tempDir)
//
// f := framework.NewDefaultFramework(initialDir + "/bin")
// _ = f.DevPodProviderAdd(ctx, "docker")
// err = f.DevPodProviderUse(context.Background(), "docker")
// framework.ExpectNoError(err)
//
// ginkgo.DeferCleanup(f.DevPodWorkspaceDelete, context.Background(), tempDir)
//
// out, err := exec.Command("gpg", "-k").Output()
// if err != nil || len(out) == 0 {
// err = f.SetupGPG(tempDir)
// framework.ExpectNoError(err)
// }
//
// // Start up devpod workspace
// devpodUpDeadline := time.Now().Add(5 * time.Minute)
// devpodUpCtx, cancel := context.WithDeadline(context.Background(), devpodUpDeadline)
// defer cancel()
// err = f.DevPodUp(devpodUpCtx, tempDir, "--gpg-agent-forwarding")
// framework.ExpectNoError(err)
//
// devpodSSHDeadline := time.Now().Add(20 * time.Second)
// devpodSSHCtx, cancelSSH := context.WithDeadline(context.Background(), devpodSSHDeadline)
// defer cancelSSH()
//
// // GPG agent might be not ready, let's try 10 times, 1 second each
// retries := 10
// for retries > 0 {
// err = f.DevPodSSHGpgTestKey(devpodSSHCtx, tempDir)
// if err != nil {
// retries--
// time.Sleep(time.Second)
// } else {
// break
// }
// }
// framework.ExpectNoError(err)
// })
ginkgo.It(
"should set up git SSH signature helper and sign a commit",
ginkgo.SpecTimeout(7*time.Minute),
func(ctx ginkgo.SpecContext) {
if runtime.GOOS == osWindows {
ginkgo.Skip("skipping on windows")
}
tempDir, err := framework.CopyToTempDir("tests/ssh/testdata/ssh-signing")
framework.ExpectNoError(err)
f := framework.NewDefaultFramework(initialDir + "/bin")
_ = f.DevPodProviderAdd(ctx, "docker")
err = f.DevPodProviderUse(ctx, "docker")
framework.ExpectNoError(err)
ginkgo.DeferCleanup(func(cleanupCtx context.Context) {
_ = f.DevPodWorkspaceDelete(cleanupCtx, tempDir)
framework.CleanupTempDir(initialDir, tempDir)
})
// Generate a temporary SSH key for signing
sshKeyDir, err := os.MkdirTemp("", "devpod-ssh-signing-test")
framework.ExpectNoError(err)
defer func() { _ = os.RemoveAll(sshKeyDir) }()
keyPath := filepath.Join(sshKeyDir, "id_ed25519")
// #nosec G204 -- test command with controlled arguments
err = exec.Command(
"ssh-keygen", "-t", "ed25519", "-f", keyPath, "-N", "", "-q",
).Run()
framework.ExpectNoError(err)
// Start workspace with git-ssh-signing-key flag
err = f.DevPodUp(ctx, tempDir, "--git-ssh-signing-key", keyPath+".pub")
framework.ExpectNoError(err)
// Verify helper installation, git config, and a signed commit
// in a single SSH session with --start-services so the
// credentials server tunnel is active. The helper is installed
// asynchronously by the credentials server, so retry briefly.
commitCmd := strings.Join([]string{
"for i in $(seq 1 30); do test -x /usr/local/bin/devpod-ssh-signature && break; sleep 1; done",
"test -x /usr/local/bin/devpod-ssh-signature",
"test \"$(git config --global gpg.ssh.program)\" = devpod-ssh-signature",
"test \"$(git config --global gpg.format)\" = ssh",
"cd /tmp",
"git init test-sign-repo",
"cd test-sign-repo",
"git config user.name 'Test User'",
"git config user.email 'test@example.com'",
"git config commit.gpgsign true",
"echo test > testfile",
"git add testfile",
"git commit -m 'signed test commit' 2>&1",
}, " && ")
// The signing key must be passed on each SSH invocation so the
// credentials server can configure the helper inside the container.
sshBase := []string{
"ssh",
"--agent-forwarding",
"--start-services",
"--git-ssh-signing-key", keyPath + ".pub",
tempDir,
}
stdout, stderr, err := f.ExecCommandCapture(ctx,
append(sshBase, "--command", commitCmd),
)
ginkgo.GinkgoWriter.Printf("commit stdout: %s\n", stdout)
ginkgo.GinkgoWriter.Printf("commit stderr: %s\n", stderr)
framework.ExpectNoError(err)
gomega.Expect(stdout).To(
gomega.ContainSubstring("signed test commit"),
"git commit should succeed with the signed test commit message",
)
// Verify the commit is signed with a valid SSH signature.
pubKeyBytes, err := os.ReadFile(
keyPath + ".pub",
) // #nosec G304 -- test file with controlled path
framework.ExpectNoError(err)
pubKey := strings.TrimSpace(string(pubKeyBytes))
verifyCmd := strings.Join([]string{
"cd /tmp/test-sign-repo",
"echo 'test@example.com " + pubKey + "' > /tmp/allowed_signers",
"git config gpg.ssh.allowedSignersFile /tmp/allowed_signers",
"git verify-commit HEAD 2>&1",
}, " && ")
stdout, stderr, err = f.ExecCommandCapture(ctx,
append(sshBase, "--command", verifyCmd),
)
ginkgo.GinkgoWriter.Printf("verify stdout: %s\n", stdout)
ginkgo.GinkgoWriter.Printf("verify stderr: %s\n", stderr)
framework.ExpectNoError(err)
// git verify-commit writes signature details to stderr
combined := stdout + stderr
gomega.Expect(combined).To(
gomega.ContainSubstring("Good"),
"git verify-commit should report a good SSH signature",
)
// And confirm the signature log shows the correct principal
logCmd := "cd /tmp/test-sign-repo && git log --show-signature -1 2>&1"
stdout, stderr, err = f.ExecCommandCapture(ctx,
append(sshBase, "--command", logCmd),
)
ginkgo.GinkgoWriter.Printf("log stdout: %s\n", stdout)
ginkgo.GinkgoWriter.Printf("log stderr: %s\n", stderr)
framework.ExpectNoError(err)
combined = stdout + stderr
gomega.Expect(combined).To(
gomega.ContainSubstring("Good"),
"git log --show-signature should report a good signature",
)
gomega.Expect(combined).To(
gomega.ContainSubstring("test@example.com"),
"signature should be associated with the test email principal",
)
},
)
ginkgo.It(
"should not install git SSH signature helper when signing key is not provided",
ginkgo.SpecTimeout(5*time.Minute),
func(ctx ginkgo.SpecContext) {
if runtime.GOOS == osWindows {
ginkgo.Skip("skipping on windows")
}
tempDir, err := framework.CopyToTempDir("tests/ssh/testdata/ssh-signing")
framework.ExpectNoError(err)
f := framework.NewDefaultFramework(initialDir + "/bin")
_ = f.DevPodProviderAdd(ctx, "docker")
err = f.DevPodProviderUse(ctx, "docker")
framework.ExpectNoError(err)
ginkgo.DeferCleanup(func(cleanupCtx context.Context) {
_ = f.DevPodWorkspaceDelete(cleanupCtx, tempDir)
framework.CleanupTempDir(initialDir, tempDir)
})
// Start workspace WITHOUT --git-ssh-signing-key
err = f.DevPodUp(ctx, tempDir)
framework.ExpectNoError(err)
// Verify the helper script was NOT installed
out, err := f.DevPodSSH(ctx, tempDir,
"test -x /usr/local/bin/devpod-ssh-signature && echo EXISTS || echo MISSING",
)
framework.ExpectNoError(err)
gomega.Expect(strings.TrimSpace(out)).To(
gomega.Equal("MISSING"),
"devpod-ssh-signature helper should not be installed without --git-ssh-signing-key",
)
// Verify git config was NOT set for SSH signing
out, err = f.DevPodSSH(ctx, tempDir,
"git config --global gpg.ssh.program || echo UNSET",
)
framework.ExpectNoError(err)
gomega.Expect(strings.TrimSpace(out)).To(
gomega.Equal("UNSET"),
"gpg.ssh.program should not be configured without --git-ssh-signing-key",
)
},
)
ginkgo.It(
"should surface clear error when SSH signing fails",
ginkgo.SpecTimeout(7*time.Minute),
func(ctx ginkgo.SpecContext) {
if runtime.GOOS == osWindows {
ginkgo.Skip("skipping on windows")
}
tempDir, err := framework.CopyToTempDir("tests/ssh/testdata/ssh-signing")
framework.ExpectNoError(err)
f := framework.NewDefaultFramework(initialDir + "/bin")
_ = f.DevPodProviderAdd(ctx, "docker")
err = f.DevPodProviderUse(ctx, "docker")
framework.ExpectNoError(err)
ginkgo.DeferCleanup(func(cleanupCtx context.Context) {
_ = f.DevPodWorkspaceDelete(cleanupCtx, tempDir)
framework.CleanupTempDir(initialDir, tempDir)
})
// Generate a key but do NOT add it to the ssh-agent so signing will fail
sshKeyDir, err := os.MkdirTemp("", "devpod-ssh-signing-err-test")
framework.ExpectNoError(err)
defer func() { _ = os.RemoveAll(sshKeyDir) }()
keyPath := filepath.Join(sshKeyDir, "id_ed25519")
// #nosec G204 -- test command with controlled arguments
err = exec.Command(
"ssh-keygen", "-t", "ed25519", "-f", keyPath, "-N", "", "-q",
).Run()
framework.ExpectNoError(err)
// Start workspace with signing key
err = f.DevPodUp(ctx, tempDir, "--git-ssh-signing-key", keyPath+".pub")
framework.ExpectNoError(err)
// Attempt a signed commit — this should fail because the key
// is not in the agent, but the error must be human-readable.
commitCmd := strings.Join([]string{
"cd /tmp",
"git init test-sign-err-repo",
"cd test-sign-err-repo",
"git config user.name 'Test User'",
"git config user.email 'test@example.com'",
"git config commit.gpgsign true",
"echo test > testfile",
"git add testfile",
"git commit -m 'signed test commit' 2>&1",
}, " && ")
stdout, stderr, err := f.ExecCommandCapture(ctx, []string{
"ssh",
"--agent-forwarding",
"--start-services",
tempDir,
"--command", commitCmd,
})
ginkgo.GinkgoWriter.Printf("error commit stdout: %s\n", stdout)
ginkgo.GinkgoWriter.Printf("error commit stderr: %s\n", stderr)
// The commit should fail
combined := stdout + stderr
if err != nil {
combined += err.Error()
}
// The error must NOT contain JSON decode artifacts
gomega.Expect(combined).NotTo(
gomega.ContainSubstring("invalid character"),
"error should not contain JSON parse errors — error messages must be human-readable",
)
},
)
ginkgo.It(
"should start a new workspace with a docker provider (default) and forward a port into it",
func(ctx context.Context) {
if runtime.GOOS == osWindows {
ginkgo.Skip("skipping on windows")
}
tempDir, err := framework.CopyToTempDir("tests/ssh/testdata/forward-test")
framework.ExpectNoError(err)
f := framework.NewDefaultFramework(initialDir + "/bin")
_ = f.DevPodProviderAdd(ctx, "docker")
err = f.DevPodProviderUse(ctx, "docker")
framework.ExpectNoError(err)
ginkgo.DeferCleanup(func(cleanupCtx context.Context) {
err = f.DevPodWorkspaceDelete(cleanupCtx, tempDir)
framework.ExpectNoError(err)
framework.CleanupTempDir(initialDir, tempDir)
})
source := rand.NewSource(time.Now().UnixNano())
rng := rand.New(source) // #nosec G404 -- weak random is fine for test port selection
port := rng.Intn(1000) + 50000
devpodUpCtx, cancel := context.WithTimeout(ctx, 5*time.Minute)
defer cancel()
err = f.DevPodUp(devpodUpCtx, tempDir)
framework.ExpectNoError(err)
ginkgo.GinkgoWriter.Println("Starting pong service on port", port)
// Create a cancelable context for the server command
serverCtx, serverCancel := context.WithCancel(ctx)
defer serverCancel()
// #nosec G204 -- test command with controlled arguments
serverCmd := exec.CommandContext(serverCtx, f.DevpodBinDir+"/"+f.DevpodBinName,
"ssh", tempDir, "--command",
"go run /workspaces/"+filepath.Base(tempDir)+"/server.go "+strconv.Itoa(port),
)
err = serverCmd.Start()
framework.ExpectNoError(err)
go func() {
_ = serverCmd.Wait()
}()
ginkgo.GinkgoWriter.Println("Waiting for server to start")
time.Sleep(3 * time.Second)
portForwardCtx, cancelPort := context.WithTimeout(
ctx,
60*time.Second,
)
defer cancelPort()
ginkgo.GinkgoWriter.Println("Starting port forwarding for port", port)
go func() {
_ = f.DevpodPortTest(portForwardCtx, strconv.Itoa(port), tempDir)
}()
ginkgo.GinkgoWriter.Println("Polling for port", port, "to be accessible")
address := net.JoinHostPort("localhost", strconv.Itoa(port))
var out string
gomega.Eventually(func() string {
conn, err := net.DialTimeout("tcp", address, 3*time.Second)
if err == nil {
_ = conn.SetReadDeadline(time.Now().Add(2 * time.Second))
buf := make([]byte, 1024)
n, readErr := conn.Read(buf)
_ = conn.Close()
if readErr == nil && n > 0 {
out = string(buf[:n])
return out
}
}
return ""
}, 60*time.Second, 2*time.Second).Should(
gomega.Equal("PONG\n"),
"Port forwarding failed to establish connection",
)
ginkgo.GinkgoWriter.Println("Port forwarding test successful")
},
)
})