-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathdiagnostics_test.go
698 lines (659 loc) · 20 KB
/
diagnostics_test.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
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License 2.0;
// you may not use this file except in compliance with the Elastic License 2.0.
package diagnostics
import (
"archive/zip"
"bytes"
"compress/gzip"
"context"
"encoding/hex"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
"github.com/google/pprof/profile"
agentclient "github.com/elastic/elastic-agent-client/v7/pkg/client"
"github.com/elastic/elastic-agent-libs/mapstr"
"github.com/elastic/elastic-agent/internal/pkg/agent/application/paths"
agentruntime "github.com/elastic/elastic-agent/pkg/component/runtime"
"github.com/elastic/elastic-agent/pkg/control/v2/client"
"github.com/elastic/elastic-agent/version"
)
func TestRedactResults(t *testing.T) {
privKey := `-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAACFwAAAAdzc2gtcn
NhAAAAAwEAAQAAAgEAnomdLTF3Vp52cT55PealM+qwSQVEkEBsKB3dSXEIvCqv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": mapstr.M{
"passphrase": "unredacted",
"nested1": mapstr.M{
"certificate": "unredacted",
"nested2": mapstr.M{
"passphrase": "unredacted",
"password": "unredacted",
"nested3": mapstr.M{
"token": "unredacted",
"key": "unredacted",
},
"ssl": mapstr.M{ // ssh-keygen -f ~/test-key -t rsa -b 4096
"key": privKey,
},
},
},
},
}
formatted, err := yaml.Marshal(exampleConfig)
require.NoError(t, err)
errOut := strings.Builder{}
outWriter := strings.Builder{}
res := client.DiagnosticFileResult{Content: formatted, ContentType: "application/yaml"}
err = writeRedacted(&errOut, &outWriter, "test/path", res)
require.NoError(t, err)
require.Empty(t, errOut.String())
require.NotContains(t, outWriter.String(), "unredacted")
require.NotContains(t, outWriter.String(), privKey)
}
func TestRedactComplexKeys(t *testing.T) {
// taken directly from the yaml spec: https://yaml.org/spec/1.1/#c-mapping-key
// This test mostly serves to document that part of the YAML library doesn't work properly
t.Skip("YAML library currently can't do this, come back to see if the library works.")
testComplexKey := `
sequence:
- one
- two
mapping:
? sky
: blue
? sea : green`
errOut := strings.Builder{}
outWriter := strings.Builder{}
res := client.DiagnosticFileResult{Content: []byte(testComplexKey), ContentType: "application/yaml"}
err := writeRedacted(&errOut, &outWriter, "test/path", res)
require.NoError(t, err)
require.Empty(t, errOut.String())
}
func TestRedactWithSecretPaths(t *testing.T) {
tests := []struct {
name string
input []byte
expect string
}{{
name: "no secret paths",
input: []byte(`id: test-policy
inputs:
- type: test_input
redactKey: secretValue
outputs:
default:
type: elasticsearch
api_key: secretKey
redactOtherKey: secretOutputValue
`),
expect: `id: test-policy
inputs:
- redactKey: secretValue
type: test_input
outputs:
default:
api_key: <REDACTED>
redactOtherKey: <REDACTED>
type: elasticsearch
`,
}, {
name: "uppercase fields are redacted",
input: []byte(`id: test-policy
inputs:
- type: test_input
outputs:
default:
type: elasticsearch
api_key: secretKey
Certificate: secretCert
PassPhrase: secretPassphrase
PASSWORD: secretPassword
tOkEn: secretToken
`),
expect: `id: test-policy
inputs:
- type: test_input
outputs:
default:
Certificate: <REDACTED>
PASSWORD: <REDACTED>
PassPhrase: <REDACTED>
api_key: <REDACTED>
tOkEn: <REDACTED>
type: elasticsearch
`,
}, {
name: "secret_paths are redacted",
input: []byte(`id: test-policy
secret_paths:
- inputs.0.redactKey
- outputs.default.redactOtherKey
inputs:
- type: test_input
redactKey: secretValue
outputs:
default:
type: elasticsearch
api_key: secretKey
redactOtherKey: secretOutputValue
`),
expect: `id: test-policy
inputs:
- redactKey: <REDACTED>
type: test_input
outputs:
default:
api_key: <REDACTED>
redactOtherKey: <REDACTED>
type: elasticsearch
secret_paths:
- inputs.0.redactKey
- outputs.default.redactOtherKey
`,
}, {
name: "secret_paths contains extra keys",
input: []byte(`id: test-policy
secret_paths:
- inputs.0.redactKey
- inputs.1.missingKey
- outputs.default.redactOtherKey
inputs:
- type: test_input
redactKey: secretValue
outputs:
default:
type: elasticsearch
api_key: secretKey
`),
expect: `id: test-policy
inputs:
- redactKey: <REDACTED>
type: test_input
outputs:
default:
api_key: <REDACTED>
type: elasticsearch
secret_paths:
- inputs.0.redactKey
- inputs.1.missingKey
- outputs.default.redactOtherKey
`,
}, {
name: "path in nested list",
input: []byte(`id: test-policy
inputs:
- type: httpjson
data_stream:
namespace: default
streams:
- config_version: "2"
request.transforms:
- set:
target: header.Authorization
value: SSWS this-should-be-redacted
- set:
target: url.params.limit
value: "1000"
secret_paths:
- inputs.0.streams.0.request.transforms.0.set.value
`),
expect: `id: test-policy
inputs:
- data_stream:
namespace: default
streams:
- config_version: "2"
request:
transforms:
- set:
target: header.Authorization
value: <REDACTED>
- set:
target: url.params.limit
value: "1000"
type: httpjson
secret_paths:
- inputs.0.streams.0.request.transforms.0.set.value
`,
}}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
file := client.DiagnosticFileResult{Content: tc.input, ContentType: "application/yaml"}
var out bytes.Buffer
var errOut bytes.Buffer
err := writeRedacted(&errOut, &out, "testPath", file)
require.NoError(t, err)
t.Logf("Error output: %s", errOut.String())
assert.Equal(t, tc.expect, out.String())
})
}
}
func TestUnitAndStateMapping(t *testing.T) {
// this structure causes problems due to the compound agentruntime.ComponentUnitKey map key
exampleState := agentruntime.ComponentState{
State: agentclient.UnitStateStarting,
Message: "test",
Units: map[agentruntime.ComponentUnitKey]agentruntime.ComponentUnitState{
{UnitType: agentclient.UnitTypeInput, UnitID: "test-unit"}: {Message: "test unit"},
{UnitType: agentclient.UnitTypeOutput, UnitID: "test-unit-2"}: {Message: "test unit 2"},
},
VersionInfo: agentruntime.ComponentVersionInfo{Name: "test-component"},
}
formatted, err := yaml.Marshal(exampleState)
require.NoError(t, err)
t.Logf("%s", formatted)
errOut := strings.Builder{}
outWriter := strings.Builder{}
res := client.DiagnosticFileResult{Content: formatted, ContentType: "application/yaml"}
err = writeRedacted(&errOut, &outWriter, "test/path", res)
require.NoError(t, err)
require.Empty(t, errOut.String())
}
type zippedItem struct {
Name string
IsDir bool
}
func TestZipLogs(t *testing.T) {
topPath := t.TempDir()
dir := filepath.Join(paths.HomeFrom(topPath), "logs", "sub-dir")
require.NoError(t, os.MkdirAll(dir, 0o700))
require.NoError(t, os.WriteFile(filepath.Join(dir, "log.ndjson"), []byte(".\n"), 0o600))
eventLogs := filepath.Join(paths.HomeFrom(topPath), "logs", "events")
require.NoError(t, os.MkdirAll(eventLogs, 0o700))
require.NoError(t, os.WriteFile(filepath.Join(eventLogs, "elastic-agent-events-log.ndjson"), []byte(".\n"), 0o600))
testCases := []struct {
name string
excludeEventsLog bool
expectedItems []zippedItem
}{
{
name: "include events logs",
excludeEventsLog: false,
expectedItems: []zippedItem{
{"logs/", true},
{"logs/elastic-agent-unknow/", true},
{"logs/elastic-agent-unknow/events/", true},
{"logs/elastic-agent-unknow/events/elastic-agent-events-log.ndjson", false},
{"logs/elastic-agent-unknow/sub-dir/", true},
{"logs/elastic-agent-unknow/sub-dir/log.ndjson", false},
},
},
{
name: "exclude events logs",
excludeEventsLog: true,
expectedItems: []zippedItem{
{"logs/", true},
{"logs/elastic-agent-unknow/", true},
{"logs/elastic-agent-unknow/sub-dir/", true},
{"logs/elastic-agent-unknow/sub-dir/log.ndjson", false},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
zipLogsAndAssertFiles(t, topPath, tc.excludeEventsLog, tc.expectedItems)
})
}
}
func zipLogsAndAssertFiles(t *testing.T, topPath string, excludeEvents bool, expected []zippedItem) {
t.Helper()
// Zip the logs directory.
buf := new(bytes.Buffer)
w := zip.NewWriter(buf)
require.NoError(t, zipLogs(w, time.Now(), topPath, excludeEvents))
require.NoError(t, w.Close())
// Read back the contents.
r, err := zip.NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len()))
require.NoError(t, err)
var observed []zippedItem
for _, f := range r.File {
observed = append(observed, zippedItem{Name: f.Name, IsDir: f.FileInfo().IsDir()})
}
assert.Equal(t, expected, observed)
if t.Failed() {
t.Log("Expected")
for _, f := range expected {
t.Logf("name: %s, dir? %t", f.Name, f.IsDir)
}
t.Log("Got")
for _, f := range observed {
t.Logf("name: %s, dir? %t", f.Name, f.IsDir)
}
}
}
func TestGlobalHooks(t *testing.T) {
testPkgVer := "1.2.3-test"
setupPkgVersion(t, testPkgVer, 0o644)
hooks := GlobalHooks()
assert.NotEmpty(t, hooks, "multiple hooks should be returned")
deadline, _ := t.Deadline()
ctx, cancel := context.WithDeadline(context.Background(), deadline)
t.Cleanup(func() { cancel() })
for _, h := range hooks {
output := h.Hook(ctx)
assert.NotEmpty(t, h, "hook should produce output")
var ok bool
var err error
switch h.Name {
case "version":
ok, err = isVersion(output)
assert.Truef(t, ok, "hook %q returned incompatible data: %q", h.Name, hex.EncodeToString(output))
assert.NoErrorf(t, err, "hook %q validation error: %v", err)
case "package version":
assert.Equal(t, testPkgVer, string(output), "hook package version does not match")
default:
ok, err = isPprof(output)
assert.Truef(t, ok, "hook %q returned incompatible data: %q", h.Name, hex.EncodeToString(output))
assert.NoErrorf(t, err, "hook %q validation error: %v", err)
}
}
}
func TestPackageVersionHook(t *testing.T) {
for _, h := range GlobalHooks() {
if h.Name == "package version" {
testPackageVersionHook(t, h)
return
}
}
t.Fatal("package version hook not returned by GlobalHooks()")
}
func testPackageVersionHook(t *testing.T, pkgVersHook Hook) {
deadline, _ := t.Deadline()
ctx, cancel := context.WithDeadline(context.Background(), deadline)
defer cancel()
t.Run("package version hook returns an error if no package version file is found", func(t *testing.T) {
output := pkgVersHook.Hook(ctx)
assert.True(t, strings.HasPrefix(string(output), "error: "))
})
t.Run("package version hook returns an error if package version file is not readable", func(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("windows does not support non-readable permissions on files")
}
testPkgVer := "1.2.3-test"
setupPkgVersion(t, testPkgVer, 0o222)
output := pkgVersHook.Hook(ctx)
assert.True(t, strings.HasPrefix(string(output), "error: "))
assert.True(t, strings.HasSuffix(string(output), "permission denied\""))
})
t.Run("package version hook returns all the bytes in file including spaces", func(t *testing.T) {
testPkgVer := "\r\n 1.2.3-test \n\t"
setupPkgVersion(t, testPkgVer, 0o444)
output := pkgVersHook.Hook(ctx)
assert.Equal(t, testPkgVer, string(output))
})
}
func setupPkgVersion(t *testing.T, pkgVer string, fileMode os.FileMode) {
// setup a fake package version to test the package version hook
pkgVersPath, err := version.GetAgentPackageVersionFilePath()
require.NoError(t, err)
t.Cleanup(func() { os.Remove(pkgVersPath) })
err = os.WriteFile(pkgVersPath, []byte(pkgVer), fileMode)
require.NoError(t, err)
}
func isVersion(input []byte) (bool, error) {
return strings.Contains(string(input), "version:"), nil
}
func isPprof(input []byte) (bool, error) {
gz, err := gzip.NewReader(bytes.NewBuffer(input))
if err != nil {
return false, err
}
uncompressed, err := io.ReadAll(gz)
if err != nil {
return false, err
}
_, err = profile.ParseUncompressed(uncompressed)
if err != nil {
return false, err
}
return true, nil
}
func TestRedactSecretPaths(t *testing.T) {
tests := []struct {
name string
input map[string]interface{}
expect map[string]interface{}
}{{
name: "no secret_paths",
input: map[string]interface{}{
"outputs": map[string]interface{}{
"default": map[string]interface{}{
"type": "elasticsearch",
"api_key": "apikeyvalue",
},
},
"inputs": []interface{}{
map[string]interface{}{
"type": "example",
"secret": "secretvalue",
},
},
},
expect: map[string]interface{}{
"outputs": map[string]interface{}{
"default": map[string]interface{}{
"type": "elasticsearch",
"api_key": "apikeyvalue",
},
},
"inputs": []interface{}{
map[string]interface{}{
"type": "example",
"secret": "secretvalue",
},
},
},
}, {
name: "secret paths is not an array",
input: map[string]interface{}{
"secret_paths": "inputs.0.secret,outputs.default.api_key",
"outputs": map[string]interface{}{
"default": map[string]interface{}{
"type": "elasticsearch",
"api_key": "apikeyvalue",
},
},
"inputs": []interface{}{
map[string]interface{}{
"type": "example",
"secret": "secretvalue",
},
},
},
expect: map[string]interface{}{
"secret_paths": "inputs.0.secret,outputs.default.api_key",
"outputs": map[string]interface{}{
"default": map[string]interface{}{
"type": "elasticsearch",
"api_key": "apikeyvalue",
},
},
"inputs": []interface{}{
map[string]interface{}{
"type": "example",
"secret": "secretvalue",
},
},
},
}, {
name: "secret_paths are redacted",
input: map[string]interface{}{
"secret_paths": []interface{}{
"inputs.0.secret",
"outputs.default.api_key",
},
"outputs": map[string]interface{}{
"default": map[string]interface{}{
"type": "elasticsearch",
"api_key": "apikeyvalue",
},
},
"inputs": []interface{}{
map[string]interface{}{
"type": "example",
"secret": "secretvalue",
},
},
},
expect: map[string]interface{}{
"secret_paths": []interface{}{
"inputs.0.secret",
"outputs.default.api_key",
},
"outputs": map[string]interface{}{
"default": map[string]interface{}{
"type": "elasticsearch",
"api_key": REDACTED,
},
},
"inputs": []interface{}{
map[string]interface{}{
"type": "example",
"secret": REDACTED,
},
},
},
}, {
name: "secret_paths contains extra keys",
input: map[string]interface{}{
"secret_paths": []interface{}{
"inputs.0.secret",
"outputs.default.api_key",
"inputs.1.secret",
},
"outputs": map[string]interface{}{
"default": map[string]interface{}{
"type": "elasticsearch",
"api_key": "apikeyvalue",
},
},
"inputs": []interface{}{
map[string]interface{}{
"type": "example",
"secret": "secretvalue",
},
},
},
expect: map[string]interface{}{
"secret_paths": []interface{}{
"inputs.0.secret",
"outputs.default.api_key",
"inputs.1.secret",
},
"outputs": map[string]interface{}{
"default": map[string]interface{}{
"type": "elasticsearch",
"api_key": REDACTED,
},
},
"inputs": []interface{}{
map[string]interface{}{
"type": "example",
"secret": REDACTED,
},
},
},
}, {
name: "secret_paths contains non string key",
input: map[string]interface{}{
"secret_paths": []interface{}{
"inputs.0.secret",
"outputs.default.api_key",
2,
},
"outputs": map[string]interface{}{
"default": map[string]interface{}{
"type": "elasticsearch",
"api_key": "apikeyvalue",
},
},
"inputs": []interface{}{
map[string]interface{}{
"type": "example",
"secret": "secretvalue",
},
},
},
expect: map[string]interface{}{
"secret_paths": []interface{}{
"inputs.0.secret",
"outputs.default.api_key",
uint64(2), // go-ucfg serializing/deserializing flattens types
},
"outputs": map[string]interface{}{
"default": map[string]interface{}{
"type": "elasticsearch",
"api_key": REDACTED,
},
},
"inputs": []interface{}{
map[string]interface{}{
"type": "example",
"secret": REDACTED,
},
},
},
}}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := RedactSecretPaths(tc.input, io.Discard)
assert.Equal(t, tc.expect, result)
})
}
}