-
Notifications
You must be signed in to change notification settings - Fork 10
/
archive.go
953 lines (801 loc) · 23.5 KB
/
archive.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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package flare
import (
"bufio"
"bytes"
"context"
"crypto/rand"
"encoding/hex"
"encoding/json"
"expvar"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"time"
"github.com/n9e/n9e-agentd/staging/datadog-agent/pkg/api/security"
apiutil "github.com/n9e/n9e-agentd/staging/datadog-agent/pkg/api/util"
"github.com/n9e/n9e-agentd/pkg/config"
"github.com/n9e/n9e-agentd/staging/datadog-agent/pkg/diagnose"
"github.com/n9e/n9e-agentd/staging/datadog-agent/pkg/secrets"
"github.com/n9e/n9e-agentd/staging/datadog-agent/pkg/status"
"github.com/n9e/n9e-agentd/staging/datadog-agent/pkg/status/health"
"github.com/n9e/n9e-agentd/staging/datadog-agent/pkg/util"
"k8s.io/klog/v2"
"github.com/mholt/archiver/v3"
"gopkg.in/yaml.v2"
)
const (
routineDumpFilename = "go-routine-dump.log"
// Maximum size for the root directory name
directoryNameMaxSize = 32
)
var (
pprofURL = fmt.Sprintf("http://127.0.0.1:%s/debug/pprof/goroutine?debug=2",
config.Datadog.GetString("expvar_port"))
telemetryURL = fmt.Sprintf("http://127.0.0.1:%s/telemetry",
config.Datadog.GetString("expvar_port"))
// Match .yaml and .yml to ship configuration files in the flare.
cnfFileExtRx = regexp.MustCompile(`(?i)\.ya?ml`)
// Filter to clean the directory name from invalid file name characters
directoryNameFilter = regexp.MustCompile(`[^a-zA-Z0-9_-]+`)
// Match other services api keys
// It is a best effort to match the api key field without matching our
// own already redacted (we don't want to match: **************************abcde)
// Basically we allow many special chars while forbidding *
otherAPIKeysRx = regexp.MustCompile(`api_key\s*:\s*[a-zA-Z0-9\\\/\^\]\[\(\){}!|%:;"~><=#@$_\-\+]+`)
otherAPIKeysReplacer = log.Replacer{
Regex: otherAPIKeysRx,
ReplFunc: func(b []byte) []byte {
return []byte("api_key: ********")
},
}
)
// SearchPaths is just an alias for a map of strings
type SearchPaths map[string]string
// permissionsInfos holds permissions info about the files shipped
// in the flare.
// The key is the filepath of the file.
type permissionsInfos map[string]filePermsInfo
type filePermsInfo struct {
mode os.FileMode
owner string
group string
}
// ProfileData maps (pprof) profile names to the profile data.
type ProfileData map[string][]byte
// CreatePerformanceProfile adds a set of heap and CPU profiles into target, using cpusec as the CPU
// profile duration, debugURL as the target URL for fetching the profiles and prefix as a prefix for
// naming them inside target.
//
// It is accepted to pass a nil target.
func CreatePerformanceProfile(prefix, debugURL string, cpusec int, target *ProfileData) error {
c := apiutil.GetClient(false)
if *target == nil {
*target = make(ProfileData)
}
for _, prof := range []struct{ Name, URL string }{
{
// 1st heap profile
Name: prefix + "-1st-heap.pprof",
URL: debugURL + "/heap",
},
{
// CPU profile
Name: prefix + "-cpu.pprof",
URL: fmt.Sprintf("%s/profile?seconds=%d", debugURL, cpusec),
},
{
// 2nd heap profile
Name: prefix + "-2nd-heap.pprof",
URL: debugURL + "/heap",
},
} {
b, err := apiutil.DoGet(c, prof.URL)
if err != nil {
return err
}
(*target)[prof.Name] = b
}
return nil
}
// CreateArchive packages up the files
func CreateArchive(local bool, distPath, pyChecksPath string, logFilePaths []string, pdata ProfileData) (string, error) {
zipFilePath := getArchivePath()
confSearchPaths := SearchPaths{
"": config.Datadog.GetString("confd_path"),
"dist": filepath.Join(distPath, "conf.d"),
"checksd": pyChecksPath,
}
return createArchive(confSearchPaths, local, zipFilePath, logFilePaths, pdata)
}
func createArchive(confSearchPaths SearchPaths, local bool, zipFilePath string, logFilePaths []string, pdata ProfileData) (string, error) {
tempDir, err := createTempDir()
if err != nil {
return "", err
}
defer os.RemoveAll(tempDir)
// Get hostname, if there's an error in getting the hostname,
// set the hostname to unknown
hostname, err := util.GetHostname()
if err != nil {
hostname = "unknown"
}
hostname = cleanDirectoryName(hostname)
permsInfos := make(permissionsInfos)
if local {
err = writeLocal(tempDir, hostname)
if err != nil {
return "", err
}
// Can't reach the agent, mention it in those two files
err = writeStatusFile(tempDir, hostname, []byte("unable to get the status of the agent, is it running?"))
if err != nil {
return "", err
}
err = writeConfigCheck(tempDir, hostname, []byte("unable to get loaded checks config, is the agent running?"))
if err != nil {
return "", err
}
} else {
// Status informations are available, zip them up as the agent is running.
err = zipStatusFile(tempDir, hostname)
if err != nil {
klog.Errorf("Could not zip status: %s", err)
}
err = zipConfigCheck(tempDir, hostname)
if err != nil {
klog.Errorf("Could not zip config check: %s", err)
}
err = zipTaggerList(tempDir, hostname)
if err != nil {
klog.Errorf("Could not zip tagger list: %s", err)
}
}
// auth token permissions info (only if existing)
if _, err = os.Stat(security.GetAuthTokenFilepath()); err == nil && !os.IsNotExist(err) {
permsInfos.add(security.GetAuthTokenFilepath())
}
err = zipConfigFiles(tempDir, hostname, confSearchPaths, permsInfos)
if err != nil {
klog.Errorf("Could not zip config: %s", err)
}
err = zipExpVar(tempDir, hostname)
if err != nil {
klog.Errorf("Could not zip exp var: %s", err)
}
if config.Datadog.GetBool("system_probe_config.enabled") {
err = zipSystemProbeStats(tempDir, hostname)
if err != nil {
klog.Errorf("Could not zip system probe exp var stats: %s", err)
}
}
err = zipDiagnose(tempDir, hostname)
if err != nil {
klog.Errorf("Could not zip diagnose: %s", err)
}
err = zipRegistryJSON(tempDir, hostname)
if err != nil {
klog.Warningf("Could not zip registry.json: %s", err)
}
err = zipVersionHistory(tempDir, hostname)
if err != nil {
klog.Errorf("Could not zip version-history.json: %s", err)
}
err = zipSecrets(tempDir, hostname)
if err != nil {
klog.Errorf("Could not zip secrets: %s", err)
}
err = zipEnvvars(tempDir, hostname)
if err != nil {
klog.Errorf("Could not zip env vars: %s", err)
}
err = zipHealth(tempDir, hostname)
if err != nil {
klog.Errorf("Could not zip health check: %s", err)
}
if config.Datadog.GetBool("telemetry.enabled") {
err = zipTelemetry(tempDir, hostname)
if err != nil {
klog.Errorf("Could not collect telemetry metrics: %s", err)
}
}
err = zipStackTraces(tempDir, hostname)
if err != nil {
klog.Errorf("Could not collect go routine stack traces: %s", err)
}
if config.IsContainerized() {
err = zipDockerSelfInspect(tempDir, hostname)
if err != nil {
klog.Errorf("Could not zip docker inspect: %s", err)
}
}
err = zipDockerPs(tempDir, hostname)
if err != nil {
klog.Errorf("Could not zip docker ps: %s", err)
}
err = zipTypeperfData(tempDir, hostname)
if err != nil {
klog.Errorf("Could not write typeperf data: %s", err)
}
err = zipLodctrOutput(tempDir, hostname)
if err != nil {
klog.Errorf("Could not write lodctr data: %s", err)
}
err = zipCounterStrings(tempDir, hostname)
if err != nil {
klog.Errorf("Could not write counter strings: %s", err)
}
err = zipWindowsEventLogs(tempDir, hostname)
if err != nil {
klog.Errorf("Could not export Windows event logs: %s", err)
}
// force a log flush before zipping them
log.Flush()
for _, logFilePath := range logFilePaths {
err = zipLogFiles(tempDir, hostname, logFilePath, permsInfos)
if err != nil {
klog.Errorf("Could not zip logs: %s", err)
}
}
err = zipInstallInfo(tempDir, hostname)
if err != nil {
klog.Errorf("Could not zip install_info: %s", err)
}
if pdata != nil {
err = zipPerformanceProfile(tempDir, hostname, pdata)
if err != nil {
klog.Errorf("Could not zip performance profile: %s", err)
}
}
// gets files infos and write the permissions.log file
if err := permsInfos.commit(tempDir, hostname, os.ModePerm); err != nil {
klog.Errorf("Could not write permissions.log file: %s", err)
}
// File format is determined based on `zipFilePath` extension
err = archiver.Archive([]string{filepath.Join(tempDir, hostname)}, zipFilePath)
if err != nil {
return "", err
}
return zipFilePath, nil
}
func createTempDir() (string, error) {
b := make([]byte, 10)
_, err := rand.Read(b)
if err != nil {
return "", err
}
dirName := hex.EncodeToString(b)
return ioutil.TempDir("", dirName)
}
func zipStatusFile(tempDir, hostname string) error {
// Grab the status
s, err := status.GetAndFormatStatus()
if err != nil {
return err
}
return writeStatusFile(tempDir, hostname, s)
}
func writeStatusFile(tempDir, hostname string, data []byte) error {
f := filepath.Join(tempDir, hostname, "status.log")
err := ensureParentDirsExist(f)
if err != nil {
return err
}
w, err := newRedactingWriter(f, os.ModePerm, true)
if err != nil {
return err
}
defer w.Close()
_, err = w.Write(data)
return err
}
func addParentPerms(dirPath string, permsInfos permissionsInfos) {
parent := filepath.Dir(dirPath)
// We do not enter the loop when `filepath.Dir` returns ".", meaning an empty directory was passed.
for parent != "." {
if len(filepath.Dir(parent)) == len(parent) {
permsInfos.add(parent)
break
}
permsInfos.add(parent)
parent = filepath.Dir(parent)
}
}
func zipLogFiles(tempDir, hostname, logFilePath string, permsInfos permissionsInfos) error {
logFileDir := filepath.Dir(logFilePath)
err := filepath.Walk(logFileDir, func(src string, f os.FileInfo, err error) error {
if f == nil {
return nil
}
if f.IsDir() {
return nil
}
if filepath.Ext(f.Name()) == ".log" || getFirstSuffix(f.Name()) == ".log" {
dst := filepath.Join(tempDir, hostname, "logs", f.Name())
if permsInfos != nil {
permsInfos.add(src)
}
return util.CopyFileAll(src, dst)
}
return nil
})
// The permsInfos map is empty when we cannot read the auth token.
if len(permsInfos) != 0 {
// Force path to be absolute for getting parent permissions.
absPath, err := filepath.Abs(logFileDir)
if err != nil {
klog.Errorf("Error while getting absolute file path for parent directory: %v", err)
}
addParentPerms(absPath, permsInfos)
}
return err
}
func zipExpVar(tempDir, hostname string) error {
var variables = make(map[string]interface{})
expvar.Do(func(kv expvar.KeyValue) {
var variable = make(map[string]interface{})
json.Unmarshal([]byte(kv.Value.String()), &variable) //nolint:errcheck
variables[kv.Key] = variable
})
// The callback above cannot return an error.
// In order to properly ensure error checking,
// it needs to be done in its own loop
for key, value := range variables {
yamlValue, err := yaml.Marshal(value)
if err != nil {
return err
}
f := filepath.Join(tempDir, hostname, "expvar", key)
err = ensureParentDirsExist(f)
if err != nil {
return err
}
w, err := newRedactingWriter(f, os.ModePerm, true)
if err != nil {
return err
}
defer w.Close()
_, err = w.Write(yamlValue)
if err != nil {
return err
}
}
apmPort := "8126"
if config.Datadog.IsSet("apm_config.receiver_port") {
apmPort = config.Datadog.GetString("apm_config.receiver_port")
}
f := filepath.Join(tempDir, hostname, "expvar", "trace-agent")
w, err := newRedactingWriter(f, os.ModePerm, true)
if err != nil {
return err
}
defer w.Close()
resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:%s/debug/vars", apmPort))
if err != nil {
_, err := w.Write([]byte(fmt.Sprintf("Error retrieving vars: %v", err)))
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
slurp, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
_, err = w.Write([]byte(fmt.Sprintf("Got response %s from /debug/vars:\n%s", resp.Status, string(slurp))))
return err
}
var all map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&all); err != nil {
return fmt.Errorf("error decoding trace-agent /debug/vars response: %v", err)
}
v, err := yaml.Marshal(all)
if err != nil {
return err
}
_, err = w.Write(v)
return err
}
func zipSystemProbeStats(tempDir, hostname string) error {
sysProbeStats := status.GetSystemProbeStats(config.Datadog.GetString("system_probe_config.sysprobe_socket"))
sysProbeFile := filepath.Join(tempDir, hostname, "expvar", "system-probe")
sysProbeWriter, err := newRedactingWriter(sysProbeFile, os.ModePerm, true)
if err != nil {
return err
}
defer sysProbeWriter.Close()
sysProbeBuf, err := yaml.Marshal(sysProbeStats)
if err != nil {
return err
}
_, err = sysProbeWriter.Write(sysProbeBuf)
return err
}
func zipConfigFiles(tempDir, hostname string, confSearchPaths SearchPaths, permsInfos permissionsInfos) error {
c, err := yaml.Marshal(config.Datadog.AllSettings())
if err != nil {
return err
}
f := filepath.Join(tempDir, hostname, "runtime_config_dump.yaml")
err = ensureParentDirsExist(f)
if err != nil {
return err
}
w, err := newRedactingWriter(f, os.ModePerm, true)
if err != nil {
return err
}
defer w.Close()
_, err = w.Write(c)
if err != nil {
return err
}
err = walkConfigFilePaths(tempDir, hostname, confSearchPaths, permsInfos)
if err != nil {
return err
}
if config.Datadog.ConfigFileUsed() != "" {
// zip up the config file that was actually used, if one exists
filePath := config.Datadog.ConfigFileUsed()
if err = createConfigFiles(filePath, tempDir, hostname, permsInfos); err != nil {
return err
}
// figure out system-probe file path based on main config path,
// and use best effort to include system-probe.yaml to the flare
systemProbePath := getSystemProbePath(filePath)
if systemErr := createConfigFiles(systemProbePath, tempDir, hostname, permsInfos); systemErr != nil {
klog.Warningf("could not zip system-probe.yaml, system-probe might not be configured, or is in a different directory with datadog.yaml: %s", systemErr)
}
}
return err
}
func zipSecrets(tempDir, hostname string) error {
var b bytes.Buffer
writer := bufio.NewWriter(&b)
info, err := secrets.GetDebugInfo()
if err != nil {
fmt.Fprintf(writer, "%s", err)
} else {
info.Print(writer)
}
writer.Flush()
f := filepath.Join(tempDir, hostname, "secrets.log")
err = ensureParentDirsExist(f)
if err != nil {
return err
}
w, err := newRedactingWriter(f, os.ModePerm, true)
if err != nil {
return err
}
defer w.Close()
_, err = w.Write(b.Bytes())
return err
}
func zipDiagnose(tempDir, hostname string) error {
var b bytes.Buffer
writer := bufio.NewWriter(&b)
diagnose.RunAll(writer) //nolint:errcheck
writer.Flush()
f := filepath.Join(tempDir, hostname, "diagnose.log")
err := ensureParentDirsExist(f)
if err != nil {
return err
}
w, err := newRedactingWriter(f, os.ModePerm, true)
if err != nil {
return err
}
defer w.Close()
_, err = w.Write(b.Bytes())
return err
}
func zipRegistryJSON(tempDir, hostname string) error {
originalPath := filepath.Join(config.Datadog.GetString("logs_config.run_path"), "registry.json")
original, err := os.Open(originalPath)
if err != nil {
return err
}
defer original.Close()
zippedPath := filepath.Join(tempDir, hostname, "registry.json")
err = ensureParentDirsExist(zippedPath)
if err != nil {
return err
}
zipped, err := os.OpenFile(zippedPath, os.O_RDWR|os.O_CREATE, os.ModePerm)
if err != nil {
return err
}
defer zipped.Close()
_, err = io.Copy(zipped, original)
return err
}
func zipVersionHistory(tempDir, hostname string) error {
originalPath := filepath.Join(config.Datadog.GetString("run_path"), "version-history.json")
original, err := os.Open(originalPath)
if err != nil {
return err
}
defer original.Close()
zippedPath := filepath.Join(tempDir, hostname, "version-history.json")
err = ensureParentDirsExist(zippedPath)
if err != nil {
return err
}
zipped, err := os.OpenFile(zippedPath, os.O_RDWR|os.O_CREATE, os.ModePerm)
if err != nil {
return err
}
defer zipped.Close()
_, err = io.Copy(zipped, original)
return err
}
func zipConfigCheck(tempDir, hostname string) error {
var b bytes.Buffer
writer := bufio.NewWriter(&b)
GetConfigCheck(writer, true) //nolint:errcheck
writer.Flush()
return writeConfigCheck(tempDir, hostname, b.Bytes())
}
func writeConfigCheck(tempDir, hostname string, data []byte) error {
f := filepath.Join(tempDir, hostname, "config-check.log")
err := ensureParentDirsExist(f)
if err != nil {
return err
}
w, err := newRedactingWriter(f, os.ModePerm, true)
if err != nil {
return err
}
defer w.Close()
_, err = w.Write(data)
return err
}
// Used for testing mock HTTP server
var taggerListURL string
func zipTaggerList(tempDir, hostname string) error {
f := filepath.Join(tempDir, hostname, "tagger-list.json")
err := ensureParentDirsExist(f)
if err != nil {
return err
}
w, err := newRedactingWriter(f, os.ModePerm, true)
if err != nil {
return err
}
defer w.Close()
ipcAddress, err := config.GetIPCAddress()
if err != nil {
return err
}
if taggerListURL == "" {
taggerListURL = fmt.Sprintf("https://%v:%v/agent/tagger-list", ipcAddress, config.Datadog.GetInt("cmd_port"))
}
c := apiutil.GetClient(false) // FIX: get certificates right then make this true
r, err := apiutil.DoGet(c, taggerListURL)
if err != nil {
return err
}
// Pretty print JSON output
var b bytes.Buffer
writer := bufio.NewWriter(&b)
err = json.Indent(&b, r, "", "\t")
if err != nil {
_, err = w.Write(r)
return err
}
writer.Flush()
_, err = w.Write(b.Bytes())
return err
}
func zipHealth(tempDir, hostname string) error {
s := health.GetReady()
sort.Strings(s.Healthy)
sort.Strings(s.Unhealthy)
yamlValue, err := yaml.Marshal(s)
if err != nil {
return err
}
f := filepath.Join(tempDir, hostname, "health.yaml")
err = ensureParentDirsExist(f)
if err != nil {
return err
}
w, err := newRedactingWriter(f, os.ModePerm, true)
if err != nil {
return err
}
defer w.Close()
_, err = w.Write(yamlValue)
return err
}
func zipInstallInfo(tempDir, hostname string) error {
originalPath := filepath.Join(config.FileUsedDir(), "install_info")
original, err := os.Open(originalPath)
if err != nil {
return err
}
defer original.Close()
zippedPath := filepath.Join(tempDir, hostname, "install_info")
err = ensureParentDirsExist(zippedPath)
if err != nil {
return err
}
zipped, err := os.OpenFile(zippedPath, os.O_RDWR|os.O_CREATE, os.ModePerm)
if err != nil {
return err
}
defer zipped.Close()
_, err = io.Copy(zipped, original)
return err
}
func zipTelemetry(tempDir, hostname string) error {
return zipHTTPCallContent(tempDir, hostname, "telemetry.log", telemetryURL)
}
func zipStackTraces(tempDir, hostname string) error {
return zipHTTPCallContent(tempDir, hostname, routineDumpFilename, pprofURL)
}
// zipHTTPCallContent does a GET HTTP call to the given url and
// writes the content of the HTTP response in the given file, ready
// to be shipped in a flare.
func zipHTTPCallContent(tempDir, hostname, filename, url string) error {
ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second)
defer cancel()
client := http.Client{}
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return err
}
resp, err := client.Do(req.WithContext(ctx))
if err != nil {
return err
}
defer resp.Body.Close()
f := filepath.Join(tempDir, hostname, filename)
err = ensureParentDirsExist(f)
if err != nil {
return err
}
w, err := newRedactingWriter(f, os.ModePerm, true)
if err != nil {
return err
}
defer w.Close()
_, err = io.Copy(w, resp.Body)
return err
}
func zipPerformanceProfile(tempDir, hostname string, pdata ProfileData) error {
dir := filepath.Join(tempDir, hostname, "profiles")
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return err
}
for name, data := range pdata {
fullpath := filepath.Join(dir, name)
if err := ioutil.WriteFile(fullpath, data, os.ModePerm); err != nil {
return err
}
}
return nil
}
func walkConfigFilePaths(tempDir, hostname string, confSearchPaths SearchPaths, permsInfos permissionsInfos) error {
for prefix, filePath := range confSearchPaths {
err := filepath.Walk(filePath, func(src string, f os.FileInfo, err error) error {
if f == nil {
return nil
}
if f.IsDir() {
return nil
}
if filepath.Ext(f.Name()) == ".example" {
return nil
}
firstSuffix := getFirstSuffix(f.Name())
ext := filepath.Ext(f.Name())
if cnfFileExtRx.Match([]byte(firstSuffix)) || cnfFileExtRx.Match([]byte(ext)) {
baseName := strings.Replace(src, filePath, "", 1)
f := filepath.Join(tempDir, hostname, "etc", "confd", prefix, baseName)
err := ensureParentDirsExist(f)
if err != nil {
return err
}
w, err := newRedactingWriter(f, os.ModePerm, true)
if err != nil {
return err
}
defer w.Close()
if _, err = w.WriteFromFile(src); err != nil {
return err
}
if permsInfos != nil {
permsInfos.add(src)
if len(permsInfos) != 0 {
absPath, err := filepath.Abs(filePath)
if err != nil {
klog.Errorf("Error while getting absolute file path for parent directory: %v", err)
}
addParentPerms(absPath, permsInfos)
}
}
}
return nil
})
if err != nil {
return err
}
}
return nil
}
func newRedactingWriter(f string, p os.FileMode, buffered bool) (*RedactingWriter, error) {
w, err := NewRedactingWriter(f, os.ModePerm, true)
if err != nil {
return nil, err
}
// The original RedactingWriter use the log/strip.go implementation
// to scrub some credentials.
// It doesn't deal with api keys of other services, for example powerDNS
// which has an "api_key" field in its YAML configuration.
// We add this replacer to scrub even those credentials.
w.RegisterReplacer(otherAPIKeysReplacer)
return w, nil
}
func ensureParentDirsExist(p string) error {
return os.MkdirAll(filepath.Dir(p), os.ModePerm)
}
func getFirstSuffix(s string) string {
return filepath.Ext(strings.TrimSuffix(s, filepath.Ext(s)))
}
func getArchivePath() string {
dir := os.TempDir()
t := time.Now().UTC()
timeString := strings.ReplaceAll(t.Format(time.RFC3339), ":", "-")
fileName := strings.Join([]string{"datadog", "agent", timeString}, "-")
fileName = strings.Join([]string{fileName, "zip"}, ".")
filePath := filepath.Join(dir, fileName)
return filePath
}
func cleanDirectoryName(name string) string {
filteredName := directoryNameFilter.ReplaceAllString(name, "_")
if len(filteredName) > directoryNameMaxSize {
return filteredName[:directoryNameMaxSize]
}
return filteredName
}
// createConfigFiles takes the content of config files that need to be included in the flare and
// put them in the directory waiting to be archived
func createConfigFiles(filePath, tempDir, hostname string, permsInfos permissionsInfos) error {
// Check if the file exists
_, err := os.Stat(filePath)
if err == nil {
f := filepath.Join(tempDir, hostname, "etc", filepath.Base(filePath))
err := ensureParentDirsExist(f)
if err != nil {
return err
}
w, err := newRedactingWriter(f, os.ModePerm, true)
if err != nil {
return err
}
defer w.Close()
_, err = w.WriteFromFile(filePath)
if err != nil {
return err
}
if permsInfos != nil {
permsInfos.add(filePath)
}
}
return err
}
// getSystemProbePath would take the path to datadog.yaml and replace the file name with system-probe.yaml
func getSystemProbePath(ddCfgFilePath string) string {
path := filepath.Dir(ddCfgFilePath)
return filepath.Join(path, "system-probe.yaml")
}