-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
dump.go
161 lines (138 loc) · 4.67 KB
/
dump.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
// 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.
//go:generate go run golang.org/x/tools/cmd/stringer -type=StorageFormat,StorageType -linecomment -output enum_string.go
// Package config holds config related files
package config
import (
"fmt"
"path"
"strings"
"github.com/DataDog/datadog-agent/pkg/security/proto/api"
)
// StorageRequest is used to request a type of storage for a dump
type StorageRequest struct {
Type StorageType
Format StorageFormat
Compression bool
// LocalStorage specific parameters
OutputDirectory string
}
// NewStorageRequest returns a new StorageRequest instance
func NewStorageRequest(storageType StorageType, format StorageFormat, compression bool, outputDirectory string) StorageRequest {
return StorageRequest{
Type: storageType,
Format: format,
Compression: compression,
OutputDirectory: outputDirectory,
}
}
// ParseStorageRequests parses storage requests from a gRPC call
func ParseStorageRequests(requests *api.StorageRequestParams) ([]StorageRequest, error) {
parsedRequests := make([]StorageRequest, 0, len(requests.GetRemoteStorageFormats())+len(requests.GetLocalStorageFormats()))
formats, err := ParseStorageFormats(requests.GetLocalStorageFormats())
if err != nil {
return nil, err
}
for _, format := range formats {
parsedRequests = append(parsedRequests, NewStorageRequest(
LocalStorage,
format,
requests.GetLocalStorageCompression(),
requests.GetLocalStorageDirectory(),
))
}
// add remote storage requests
formats, err = ParseStorageFormats(requests.GetRemoteStorageFormats())
if err != nil {
return nil, err
}
for _, format := range formats {
parsedRequests = append(parsedRequests, NewStorageRequest(
RemoteStorage,
format,
requests.GetRemoteStorageCompression(),
"",
))
}
return parsedRequests, nil
}
// ToStorageRequestMessage returns an api.StorageRequestMessage from the StorageRequest
func (sr *StorageRequest) ToStorageRequestMessage(filename string) *api.StorageRequestMessage {
return &api.StorageRequestMessage{
Compression: sr.Compression,
Type: sr.Type.String(),
Format: sr.Format.String(),
File: sr.GetOutputPath(filename),
}
}
// GetOutputPath returns the output path to the file in the storage
func (sr *StorageRequest) GetOutputPath(filename string) string {
var compressionSuffix string
if sr.Compression {
compressionSuffix = ".gz"
}
return path.Join(sr.OutputDirectory, filename) + "." + sr.Format.String() + compressionSuffix
}
// StorageFormat is used to define the format of a dump
type StorageFormat int
const (
// JSON is used to request the JSON format
JSON StorageFormat = iota // json
// Protobuf is used to request the protobuf format
Protobuf // protobuf
// Dot is used to request the dot format
Dot // dot
// Profile is used to request the generation of a profile
Profile // profile
)
// AllStorageFormats returns the list of supported formats
func AllStorageFormats() []StorageFormat {
return []StorageFormat{JSON, Protobuf, Dot, Profile}
}
// ParseStorageFormat returns a storage format from a string input
func ParseStorageFormat(input string) (StorageFormat, error) {
if len(input) > 0 && input[0] == '.' {
input = input[1:]
}
for _, s := range AllStorageFormats() {
if strings.ToLower(s.String()) == input {
return s, nil
}
}
return -1, fmt.Errorf("%s: unknown storage format, available options are %v", input, AllStorageFormats())
}
// ParseStorageFormats returns a list of storage formats from a list of strings
func ParseStorageFormats(input []string) ([]StorageFormat, error) {
output := make([]StorageFormat, 0, len(input))
for _, in := range input {
format, err := ParseStorageFormat(in)
if err != nil {
return nil, err
}
output = append(output, format)
}
return output, nil
}
// StorageType is used to define the type of storage
type StorageType int
const (
// LocalStorage is used to request a local storage
LocalStorage StorageType = iota // local_storage
// RemoteStorage is used to request a remote storage
RemoteStorage // remote_storage
)
// AllStorageTypes returns the list of supported storage types
func AllStorageTypes() []StorageType {
return []StorageType{LocalStorage, RemoteStorage}
}
// ParseStorageType returns a storage type from its string representation
func ParseStorageType(input string) (StorageType, error) {
for _, st := range AllStorageTypes() {
if st.String() == input {
return st, nil
}
}
return -1, fmt.Errorf("unknown storage type [%s]", input)
}