forked from k0sproject/rig
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
603 lines (491 loc) · 19.4 KB
/
Copy pathclient_test.go
File metadata and controls
603 lines (491 loc) · 19.4 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
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
package rig_test
import (
"context"
"errors"
"io"
"strings"
"sync"
"testing"
"github.com/k0sproject/rig/v2"
"github.com/k0sproject/rig/v2/cmd"
"github.com/k0sproject/rig/v2/os"
"github.com/k0sproject/rig/v2/protocol"
"github.com/k0sproject/rig/v2/packagemanager"
"github.com/k0sproject/rig/v2/remotefs"
"github.com/k0sproject/rig/v2/rigtest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)
func TestClientWithConnectionFactory(t *testing.T) {
cc := &rig.CompositeConfig{
Localhost: true,
}
conn, err := rig.NewClient(
rig.WithConnectionFactory(cc),
)
require.NoError(t, err)
require.NotNil(t, conn)
require.NoError(t, conn.Connect(context.Background()))
out, err := conn.ExecOutput("echo hello")
require.NoError(t, err)
require.Equal(t, "hello", out)
}
func TestClient(t *testing.T) {
conn := rigtest.NewMockConnection()
conn.AddCommandOutput(rigtest.Match("echo hello"), "hello")
client, err := rig.NewClient(rig.WithConnection(conn))
require.NoError(t, err)
require.NoError(t, client.Connect(context.Background()))
out, err := client.ExecOutput("echo hello")
require.NoError(t, err)
require.Equal(t, "hello", out)
}
func TestClientLogging(t *testing.T) {
conn := rigtest.NewMockConnection()
conn.AddCommandOutput(rigtest.Match("echo hello"), "hello")
logger := &rigtest.MockLogger{}
client, err := rig.NewClient(rig.WithConnection(conn), rig.WithLogger(logger))
require.NoError(t, err)
require.NoError(t, client.Connect(context.Background()))
_, _ = client.ExecOutput("echo hello")
t.Log(logger.Messages())
}
func TestClientFSErrorFallback(t *testing.T) {
conn := rigtest.NewMockConnection()
mockErr := errors.New("mock fs error")
client, err := rig.NewClient(
rig.WithConnection(conn),
rig.WithRemoteFSProvider(func(_ cmd.Runner) (remotefs.FS, error) {
return nil, mockErr
}),
)
require.NoError(t, err)
require.NoError(t, client.Connect(context.Background()))
fs := client.FS()
require.NotNil(t, fs)
_, err = fs.Open("test")
require.Error(t, err)
_, err = client.RemoteFSProvider.FS()
require.ErrorIs(t, err, mockErr)
}
func TestClientPackageManagerErrorFallback(t *testing.T) {
conn := rigtest.NewMockConnection()
mockErr := errors.New("mock pm error")
client, err := rig.NewClient(
rig.WithConnection(conn),
rig.WithPackageManagerProvider(func(_ cmd.ContextRunner) (packagemanager.PackageManager, error) {
return nil, mockErr
}),
)
require.NoError(t, err)
require.NoError(t, client.Connect(context.Background()))
pm := client.PackageManager()
require.NotNil(t, pm)
err = pm.Install(context.Background(), "test-package")
require.ErrorIs(t, err, mockErr)
}
func TestClientReconnect(t *testing.T) {
conn := rigtest.NewMockConnection()
conn.AddCommandOutput(rigtest.Match("echo hello"), "hello")
client, err := rig.NewClient(rig.WithConnection(conn))
require.NoError(t, err)
require.NoError(t, client.Connect(context.Background()))
require.True(t, client.IsConnected())
out, err := client.ExecOutput("echo hello")
require.NoError(t, err)
require.Equal(t, "hello", out)
client.Disconnect()
require.False(t, client.IsConnected())
require.NoError(t, client.Connect(context.Background()))
require.True(t, client.IsConnected())
out, err = client.ExecOutput("echo hello")
require.NoError(t, err)
require.Equal(t, "hello", out)
}
func TestClientProtocolName(t *testing.T) {
conn := rigtest.NewMockConnection()
client, err := rig.NewClient(rig.WithConnection(conn))
require.NoError(t, err)
require.Equal(t, "mock", client.Protocol())
require.Equal(t, "mock", client.ProtocolName())
}
type testConfig struct {
Hosts []*testHost `yaml:"hosts"`
}
type testHost struct {
ClientConfig rig.CompositeConfig `yaml:"-,inline"`
*rig.Client
}
func (th *testHost) UnmarshalYAML(unmarshal func(interface{}) error) error {
type rawTestHost testHost
h := (*rawTestHost)(th)
if err := unmarshal(h); err != nil {
return err
}
conn, err := rig.NewClient(rig.WithConnectionFactory(&h.ClientConfig))
if err != nil {
return err
}
h.Client = conn
return nil
}
func TestConnectionUnmarshal(t *testing.T) {
hostConfig := map[string]any{
"localhost": true,
}
mainConfig := map[string]any{
"hosts": []map[string]any{hostConfig},
}
yamlContent, err := yaml.Marshal(mainConfig)
require.NoError(t, err)
testConfig := &testConfig{}
require.NoError(t, yaml.Unmarshal(yamlContent, testConfig))
require.Len(t, testConfig.Hosts, 1)
conn := testConfig.Hosts[0]
require.NoError(t, conn.Connect(context.Background()))
require.Equal(t, "Local", conn.Protocol())
require.NoError(t, conn.Connect(context.Background()))
out, err := conn.ExecOutput("echo hello")
require.NoError(t, err)
require.Equal(t, "hello", out)
}
type testConfigConfigured struct {
Hosts []*testHostConfigured `yaml:"hosts"`
}
type testHostConfigured struct {
rig.ClientWithConfig `yaml:"-,inline"`
}
func TestConfiguredConnectionUnmarshal(t *testing.T) {
hostConfig := map[string]any{
"localhost": true,
}
mainConfig := map[string]any{
"hosts": []map[string]any{hostConfig},
}
yamlContent, err := yaml.Marshal(mainConfig)
require.NoError(t, err)
testConfig := &testConfigConfigured{}
require.NoError(t, yaml.Unmarshal(yamlContent, testConfig))
require.Len(t, testConfig.Hosts, 1)
conn := testConfig.Hosts[0]
require.NoError(t, conn.Connect(context.Background()))
require.Equal(t, "Local", conn.Protocol())
require.NoError(t, conn.Connect(context.Background()))
out, err := conn.ExecOutput("echo hello")
require.NoError(t, err)
require.Equal(t, "hello", out)
}
func TestWithOSIDOverride(t *testing.T) {
detectedRelease := &os.Release{
ID: "detected-os",
Name: "Detected OS",
IDLike: []string{"family"},
}
releaseProvider := func(_ cmd.SimpleRunner) (*os.Release, error) {
return detectedRelease, nil
}
t.Run("override after provider", func(t *testing.T) {
conn := rigtest.NewMockConnection()
client, err := rig.NewClient(
rig.WithConnection(conn),
rig.WithOSReleaseProvider(releaseProvider),
rig.WithOSIDOverride("override-id"),
)
require.NoError(t, err)
require.NoError(t, client.Connect(context.Background()))
release, err := client.OS()
require.NoError(t, err)
require.Equal(t, "override-id", release.ID)
require.Equal(t, "Detected OS", release.Name)
require.Equal(t, []string{"family"}, release.IDLike)
})
t.Run("override before provider", func(t *testing.T) {
conn := rigtest.NewMockConnection()
client, err := rig.NewClient(
rig.WithConnection(conn),
rig.WithOSIDOverride("override-id"),
rig.WithOSReleaseProvider(releaseProvider),
)
require.NoError(t, err)
require.NoError(t, client.Connect(context.Background()))
release, err := client.OS()
require.NoError(t, err)
require.Equal(t, "override-id", release.ID)
require.Equal(t, "Detected OS", release.Name)
require.Equal(t, []string{"family"}, release.IDLike)
})
t.Run("nil release from provider", func(t *testing.T) {
conn := rigtest.NewMockConnection()
client, err := rig.NewClient(
rig.WithConnection(conn),
rig.WithOSReleaseProvider(func(_ cmd.SimpleRunner) (*os.Release, error) {
return nil, nil
}),
rig.WithOSIDOverride("override-id"),
)
require.NoError(t, err)
require.NoError(t, client.Connect(context.Background()))
_, err = client.OS()
require.Error(t, err)
})
}
// TestConfiguredConnectionConnectOptsApplied is a regression test verifying that
// options passed to Connect() after YAML unmarshal are actually applied.
// Previously, UnmarshalYAML called Setup() eagerly, causing a subsequent
// Connect(ctx, opts...) to skip Setup() and silently ignore those options.
func TestConfiguredConnectionConnectOptsApplied(t *testing.T) {
hostConfig := map[string]any{
"localhost": true,
}
mainConfig := map[string]any{
"hosts": []map[string]any{hostConfig},
}
yamlContent, err := yaml.Marshal(mainConfig)
require.NoError(t, err)
testConfig := &testConfigConfigured{}
require.NoError(t, yaml.Unmarshal(yamlContent, testConfig))
require.Len(t, testConfig.Hosts, 1)
conn := testConfig.Hosts[0]
mock := rigtest.NewMockRunner()
mock.AddCommand(rigtest.Equal("echo hello"), func(a *rigtest.A) error { return nil })
require.NoError(t, conn.Connect(context.Background(), rig.WithRunner(mock)))
_, err = conn.ExecOutput("echo hello")
require.NoError(t, err)
rigtest.ReceivedEqual(t, mock, "echo hello", "WithRunner option passed to Connect was not applied")
}
func TestClientRebootNilConnection(t *testing.T) {
conn := rigtest.NewMockConnection()
client, err := rig.NewClient(rig.WithConnection(conn))
require.NoError(t, err)
require.NoError(t, client.Connect(context.Background()))
// Clone with a nil connection forces an uninitialized connection state.
uninitialized := client.Clone(rig.WithConnection(nil))
err = uninitialized.Reboot(context.Background())
require.Error(t, err)
require.ErrorIs(t, err, protocol.ErrNonRetryable)
}
func TestClientRebootFSErrorWrapped(t *testing.T) {
mockErr := errors.New("reboot failed on remote")
conn := rigtest.NewMockConnection()
client, err := rig.NewClient(
rig.WithConnection(conn),
rig.WithRemoteFSProvider(func(_ cmd.Runner) (remotefs.FS, error) {
return nil, mockErr
}),
)
require.NoError(t, err)
require.NoError(t, client.Connect(context.Background()))
err = client.Reboot(context.Background())
require.Error(t, err)
require.ErrorIs(t, err, mockErr)
require.Contains(t, err.Error(), "reboot:")
}
// interactiveConn embeds MockConnection and additionally implements InteractiveExecer
// so that Client.ExecInteractive forwards through to it.
type interactiveConn struct {
*rigtest.MockConnection
receivedCtx context.Context
receivedCmd string
returnErr error
}
func (c *interactiveConn) ExecInteractive(ctx context.Context, cmd string, _ io.Reader, _ io.Writer, _ io.Writer) error {
c.receivedCtx = ctx
c.receivedCmd = cmd
return c.returnErr
}
func TestClientExecInteractiveForwardsContext(t *testing.T) {
conn := &interactiveConn{MockConnection: rigtest.NewMockConnection()}
client, err := rig.NewClient(rig.WithConnection(conn))
require.NoError(t, err)
require.NoError(t, client.Connect(context.Background()))
type ctxKey struct{}
ctx := context.WithValue(context.Background(), ctxKey{}, "marker")
require.NoError(t, client.ExecInteractive(ctx, "echo hello", nil, nil, nil))
require.Equal(t, "echo hello", conn.receivedCmd)
require.Equal(t, "marker", conn.receivedCtx.Value(ctxKey{}))
}
func TestClientExecInteractiveCancelledContext(t *testing.T) {
conn := &interactiveConn{
MockConnection: rigtest.NewMockConnection(),
returnErr: context.Canceled,
}
client, err := rig.NewClient(rig.WithConnection(conn))
require.NoError(t, err)
require.NoError(t, client.Connect(context.Background()))
ctx, cancel := context.WithCancel(context.Background())
cancel()
err = client.ExecInteractive(ctx, "sleep 60", nil, nil, nil)
require.Error(t, err)
require.ErrorIs(t, err, context.Canceled)
}
func TestClientExecInteractiveNotSupported(t *testing.T) {
// MockConnection does not implement InteractiveExecer, so the client should
// return an error indicating that interactive exec is unsupported.
conn := rigtest.NewMockConnection()
client, err := rig.NewClient(rig.WithConnection(conn))
require.NoError(t, err)
require.NoError(t, client.Connect(context.Background()))
err = client.ExecInteractive(context.Background(), "sh", nil, nil, nil)
require.Error(t, err)
require.ErrorContains(t, err, "interactive")
}
var errNotRoot = errors.New("not root")
// forceSudo makes the sudo registry deterministically select the "sudo"
// decorator against a mock connection. The mock otherwise succeeds on every
// command, which the root check would read as "already root" and both the sudo
// and doas probes would pass (leaving the choice to registry iteration order).
// It fails the UID0/root probe and the doas probe so only sudo qualifies.
func forceSudo(conn *rigtest.MockConnection) {
conn.AddCommand(rigtest.Contains("id -u"), func(_ *rigtest.A) error { return errNotRoot })
conn.AddCommand(rigtest.Contains("doas -n"), func(_ *rigtest.A) error { return errNotRoot })
}
// gateRecorder records the commands presented to a CommandGate and can be
// configured to allow or deny them.
type gateRecorder struct {
mu sync.Mutex
seen []string
allow bool
}
func (g *gateRecorder) confirm(_, command string) bool {
g.mu.Lock()
defer g.mu.Unlock()
g.seen = append(g.seen, command)
return g.allow
}
func (g *gateRecorder) commands() []string {
g.mu.Lock()
defer g.mu.Unlock()
return append([]string(nil), g.seen...)
}
func TestConfirmFuncGatesSudoCommandOnce(t *testing.T) {
conn := rigtest.NewMockConnection()
forceSudo(conn)
conn.AddCommand(rigtest.Contains("systemctl restart nginx"), func(_ *rigtest.A) error { return nil })
rec := &gateRecorder{allow: true}
client, err := rig.NewClient(rig.WithConnection(conn), rig.WithConfirmFunc(rec.confirm))
require.NoError(t, err)
require.NoError(t, client.Connect(context.Background()))
require.NoError(t, client.Sudo().ExecContext(context.Background(), "systemctl restart nginx"))
seen := rec.commands()
// Exactly one prompt: the sudo-availability probe is Ungated and must not
// be presented, and the sudo wrapping must not cause a double prompt.
require.Len(t, seen, 1, "expected exactly one gated command, got %v", seen)
assert.Contains(t, seen[0], "systemctl restart nginx")
assert.Contains(t, seen[0], "sudo -n", "gate must see the fully sudo-wrapped command")
}
func TestConfirmFuncSudoProbeIsUngated(t *testing.T) {
conn := rigtest.NewMockConnection()
forceSudo(conn)
rec := &gateRecorder{allow: false} // deny everything
client, err := rig.NewClient(rig.WithConnection(conn), rig.WithConfirmFunc(rec.confirm))
require.NoError(t, err)
require.NoError(t, client.Connect(context.Background()))
// Denying the gate must not disable sudo: the probe runs Ungated, so the
// sudo runner is a real sudo executor and rejection surfaces on the actual
// command rather than silently downgrading to a non-sudo runner.
err = client.Sudo().ExecContext(context.Background(), "id")
require.Error(t, err)
assert.ErrorIs(t, err, cmd.ErrCommandRejected)
// All privilege probes (sudo/doas availability, UID-0) are Ungated, so the
// only command the gate should have seen is the real, sudo-wrapped "id".
// A leaked probe would show up as an extra entry here.
seen := rec.commands()
require.Len(t, seen, 1, "gate must see only the real command, not the ungated probes; got %v", seen)
assert.Contains(t, seen[0], "id", "gate must see the real command")
assert.NotContains(t, seen[0], "true", "the sudo probe (true) must not be presented to the gate")
}
func TestConfirmFuncRejectionBlocksExecution(t *testing.T) {
conn := rigtest.NewMockConnection()
conn.AddCommand(rigtest.Match("."), func(_ *rigtest.A) error { return nil })
rec := &gateRecorder{allow: false}
client, err := rig.NewClient(rig.WithConnection(conn), rig.WithConfirmFunc(rec.confirm))
require.NoError(t, err)
require.NoError(t, client.Connect(context.Background()))
err = client.ExecContext(context.Background(), "rm -rf /data")
require.Error(t, err)
assert.ErrorIs(t, err, cmd.ErrCommandRejected)
assert.NoError(t, conn.NotReceived(rigtest.Contains("rm -rf /data")), "rejected command must not reach the host")
}
func TestCommandGateContextErrorNotRejection(t *testing.T) {
conn := rigtest.NewMockConnection()
conn.AddCommand(rigtest.Match("."), func(_ *rigtest.A) error { return nil })
// A gate that returns a context error (e.g. it honored cancellation while
// prompting) must surface as cancellation, not as an explicit rejection.
gate := cmd.CommandGateFunc(func(_ context.Context, _, _ string) error {
return context.Canceled
})
client, err := rig.NewClient(rig.WithConnection(conn), rig.WithCommandGate(gate))
require.NoError(t, err)
require.NoError(t, client.Connect(context.Background()))
err = client.ExecContext(context.Background(), "uptime")
require.Error(t, err)
assert.ErrorIs(t, err, context.Canceled, "context error must pass through")
assert.NotErrorIs(t, err, cmd.ErrCommandRejected, "cancellation must not look like a rejection")
assert.NoError(t, conn.NotReceived(rigtest.Contains("uptime")), "command must not reach the host")
}
func TestConfirmFuncNilDisablesGating(t *testing.T) {
conn := rigtest.NewMockConnection()
conn.AddCommand(rigtest.Match("."), func(_ *rigtest.A) error { return nil })
client, err := rig.NewClient(rig.WithConnection(conn), rig.WithConfirmFunc(nil))
require.NoError(t, err)
require.NoError(t, client.Connect(context.Background()))
// A nil confirm func must disable gating rather than panic on invocation.
require.NoError(t, client.ExecContext(context.Background(), "echo hello"))
require.NoError(t, conn.Received(rigtest.Contains("echo hello")))
}
func TestConfirmFuncGatesExecInteractive(t *testing.T) {
conn := &interactiveConn{MockConnection: rigtest.NewMockConnection()}
rec := &gateRecorder{allow: true}
client, err := rig.NewClient(rig.WithConnection(conn), rig.WithConfirmFunc(rec.confirm))
require.NoError(t, err)
require.NoError(t, client.Connect(context.Background()))
require.NoError(t, client.ExecInteractive(context.Background(), "top", nil, nil, nil))
assert.Equal(t, "top", conn.receivedCmd, "allowed interactive command must start the session")
assert.Contains(t, rec.commands(), "top", "the interactive command must be presented to the gate")
}
func TestConfirmFuncRejectsExecInteractive(t *testing.T) {
conn := &interactiveConn{MockConnection: rigtest.NewMockConnection()}
rec := &gateRecorder{allow: false}
client, err := rig.NewClient(rig.WithConnection(conn), rig.WithConfirmFunc(rec.confirm))
require.NoError(t, err)
require.NoError(t, client.Connect(context.Background()))
err = client.ExecInteractive(context.Background(), "top", nil, nil, nil)
require.Error(t, err)
assert.ErrorIs(t, err, cmd.ErrCommandRejected)
assert.Empty(t, conn.receivedCmd, "rejected interactive command must not start the session")
}
func TestExecInteractiveCancelledContextSkipsGate(t *testing.T) {
conn := &interactiveConn{MockConnection: rigtest.NewMockConnection()}
rec := &gateRecorder{allow: true}
client, err := rig.NewClient(rig.WithConnection(conn), rig.WithConfirmFunc(rec.confirm))
require.NoError(t, err)
require.NoError(t, client.Connect(context.Background()))
ctx, cancel := context.WithCancel(context.Background())
cancel()
err = client.ExecInteractive(ctx, "top", nil, nil, nil)
require.Error(t, err)
assert.ErrorIs(t, err, context.Canceled, "cancelled context must propagate")
assert.NotErrorIs(t, err, cmd.ErrCommandRejected, "cancellation must not look like a rejection")
assert.Empty(t, rec.commands(), "gate must not be consulted for a cancelled session")
assert.Empty(t, conn.receivedCmd, "cancelled session must not start")
}
func TestConfirmFuncGatesFilesystemOps(t *testing.T) {
conn := rigtest.NewMockConnection()
forceSudo(conn)
rec := &gateRecorder{allow: true}
client, err := rig.NewClient(rig.WithConnection(conn), rig.WithConfirmFunc(rec.confirm))
require.NoError(t, err)
require.NoError(t, client.Connect(context.Background()))
// Touch the filesystem service on the sudo client; its lazy detection runs
// commands through the sudo runner, proving the gate propagates to FS.
_, _ = client.Sudo().FS().Stat("/etc/hostname")
var sawSudoWrapped bool
for _, c := range rec.commands() {
if strings.Contains(c, "sudo -n") {
sawSudoWrapped = true
break
}
}
assert.True(t, sawSudoWrapped, "filesystem operations on a sudo client must be gated, got %v", rec.commands())
}