-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathtypes.go
408 lines (338 loc) · 13.3 KB
/
types.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
// Copyright (c) F5, Inc.
//
// This source code is licensed under the Apache License, Version 2.0 license found in the
// LICENSE file in the root directory of this source tree.
package config
import (
"errors"
"fmt"
"path/filepath"
"strings"
"time"
"github.com/google/uuid"
)
type ServerType int
const (
Grpc ServerType = iota + 1
)
var serverTypes = map[string]ServerType{
"grpc": Grpc,
}
func parseServerType(str string) (ServerType, bool) {
c, ok := serverTypes[strings.ToLower(str)]
return c, ok
}
type (
Config struct {
Command *Command `yaml:"-" mapstructure:"command"`
Log *Log `yaml:"-" mapstructure:"log"`
DataPlaneConfig *DataPlaneConfig `yaml:"-" mapstructure:"data_plane_config"`
Client *Client `yaml:"-" mapstructure:"client"`
Collector *Collector `yaml:"-" mapstructure:"collector"`
Watchers *Watchers `yaml:"-"`
Labels map[string]any `yaml:"-" mapstructure:"labels"`
Version string `yaml:"-"`
Path string `yaml:"-"`
UUID string `yaml:"-"`
AllowedDirectories []string `yaml:"-" mapstructure:"allowed_directories"`
Features []string `yaml:"-"`
}
Log struct {
Level string `yaml:"-" mapstructure:"level"`
Path string `yaml:"-" mapstructure:"path"`
}
DataPlaneConfig struct {
Nginx *NginxDataPlaneConfig `yaml:"-" mapstructure:"nginx"`
}
NginxDataPlaneConfig struct {
ExcludeLogs []string `yaml:"-" mapstructure:"exclude_logs"`
ReloadMonitoringPeriod time.Duration `yaml:"-" mapstructure:"reload_monitoring_period"`
TreatWarningsAsErrors bool `yaml:"-" mapstructure:"treat_warnings_as_errors"`
}
Client struct {
HTTP *HTTP `yaml:"http" mapstructure:"http"`
Grpc *GRPC `yaml:"grpc" mapstructure:"grpc"`
Backoff *BackOff `yaml:"backoff" mapstructure:"backoff"`
}
HTTP struct {
Timeout time.Duration `yaml:"-" mapstructure:"timeout"`
}
BackOff struct {
InitialInterval time.Duration `yaml:"-" mapstructure:"initial_interval"`
MaxInterval time.Duration `yaml:"-" mapstructure:"max_interval"`
MaxElapsedTime time.Duration `yaml:"-" mapstructure:"max_elapsed_time"`
RandomizationFactor float64 `yaml:"-" mapstructure:"randomization_factor"`
Multiplier float64 `yaml:"-" mapstructure:"multiplier"`
}
GRPC struct {
KeepAlive *KeepAlive `yaml:"-" mapstructure:"target"`
// if MaxMessageSize is size set then we use that value,
// otherwise MaxMessageRecieveSize and MaxMessageSendSize for individual settings
MaxMessageSize int `yaml:"-" mapstructure:"max_message_size"`
MaxMessageReceiveSize int `yaml:"-" mapstructure:"max_message_receive_size"`
MaxMessageSendSize int `yaml:"-" mapstructure:"max_message_send_size"`
}
KeepAlive struct {
Timeout time.Duration `yaml:"-" mapstructure:"timeout"`
Time time.Duration `yaml:"-" mapstructure:"time"`
PermitWithoutStream bool `yaml:"-" mapstructure:"permit_without_stream"`
}
Collector struct {
ConfigPath string `yaml:"-" mapstructure:"config_path"`
Log *Log `yaml:"-" mapstructure:"log"`
Exporters Exporters `yaml:"-" mapstructure:"exporters"`
Extensions Extensions `yaml:"-" mapstructure:"extensions"`
Processors Processors `yaml:"-" mapstructure:"processors"`
Receivers Receivers `yaml:"-" mapstructure:"receivers"`
}
Exporters struct {
Debug *DebugExporter `yaml:"-" mapstructure:"debug"`
PrometheusExporter *PrometheusExporter `yaml:"-" mapstructure:"prometheus_exporter"`
OtlpExporters []OtlpExporter `yaml:"-" mapstructure:"otlp_exporters"`
}
OtlpExporter struct {
Server *ServerConfig `yaml:"-" mapstructure:"server"`
TLS *TLSConfig `yaml:"-" mapstructure:"tls"`
Compression string `yaml:"-" mapstructure:"compression"`
Authenticator string `yaml:"-" mapstructure:"authenticator"`
}
Extensions struct {
Health *Health `yaml:"-" mapstructure:"health"`
HeadersSetter *HeadersSetter `yaml:"-" mapstructure:"headers_setter"`
}
Health struct {
Server *ServerConfig `yaml:"-" mapstructure:"server"`
TLS *TLSConfig `yaml:"-" mapstructure:"tls"`
Path string `yaml:"-" mapstructure:"path"`
}
HeadersSetter struct {
Headers []Header `yaml:"-" mapstructure:"headers"`
}
Header struct {
Action string `yaml:"-" mapstructure:"action"`
Key string `yaml:"-" mapstructure:"key"`
Value string `yaml:"-" mapstructure:"value"`
DefaultValue string `yaml:"-" mapstructure:"default_value"`
FromContext string `yaml:"-" mapstructure:"from_context"`
}
DebugExporter struct{}
PrometheusExporter struct {
Server *ServerConfig `yaml:"-" mapstructure:"server"`
TLS *TLSConfig `yaml:"-" mapstructure:"tls"`
}
// OTel Collector Processors configuration.
Processors struct {
Attribute *Attribute `yaml:"-" mapstructure:"attribute"`
Resource *Resource `yaml:"-" mapstructure:"resource"`
Batch *Batch `yaml:"-" mapstructure:"batch"`
}
Attribute struct {
Actions []Action `yaml:"-" mapstructure:"actions"`
}
Action struct {
Key string `yaml:"key" mapstructure:"key"`
Action string `yaml:"action" mapstructure:"action"`
Value string `yaml:"value" mapstructure:"value"`
}
Resource struct {
Attributes []ResourceAttribute `yaml:"-" mapstructure:"attributes"`
}
ResourceAttribute struct {
Key string `yaml:"key" mapstructure:"key"`
Action string `yaml:"action" mapstructure:"action"`
Value string `yaml:"value" mapstructure:"value"`
}
Batch struct {
SendBatchSize uint32 `yaml:"-" mapstructure:"send_batch_size"`
SendBatchMaxSize uint32 `yaml:"-" mapstructure:"send_batch_max_size"`
Timeout time.Duration `yaml:"-" mapstructure:"timeout"`
}
// OTel Collector Receiver configuration.
Receivers struct {
HostMetrics *HostMetrics `yaml:"-" mapstructure:"host_metrics"`
OtlpReceivers []OtlpReceiver `yaml:"-" mapstructure:"otlp_receivers"`
NginxReceivers []NginxReceiver `yaml:"-" mapstructure:"nginx_receivers"`
NginxPlusReceivers []NginxPlusReceiver `yaml:"-" mapstructure:"nginx_plus_receivers"`
TcplogReceivers []TcplogReceiver `yaml:"-" mapstructure:"tcplog_receivers"`
}
OtlpReceiver struct {
Server *ServerConfig `yaml:"-" mapstructure:"server"`
Auth *AuthConfig `yaml:"-" mapstructure:"auth"`
OtlpTLSConfig *OtlpTLSConfig `yaml:"-" mapstructure:"tls"`
}
TcplogReceiver struct {
ListenAddress string `yaml:"-" mapstructure:"listen_address"`
Operators []Operator `yaml:"-" mapstructure:"operators"`
}
// There are many types of operators with different field names so we use a generic map to store the fields.
// See here for more info:
// https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/pkg/stanza/docs/operators/README.md
Operator struct {
Fields map[string]string `yaml:"-" mapstructure:"fields"`
Type string `yaml:"-" mapstructure:"type"`
}
NginxReceiver struct {
InstanceID string `yaml:"-" mapstructure:"instance_id"`
StubStatus APIDetails `yaml:"-" mapstructure:"api_details"`
AccessLogs []AccessLog `yaml:"-" mapstructure:"access_logs"`
}
APIDetails struct {
URL string `yaml:"-" mapstructure:"url"`
Listen string `yaml:"-" mapstructure:"listen"`
Location string `yaml:"-" mapstructure:"location"`
}
AccessLog struct {
FilePath string `yaml:"-" mapstructure:"file_path"`
LogFormat string `yaml:"-" mapstructure:"log_format"`
}
NginxPlusReceiver struct {
InstanceID string `yaml:"-" mapstructure:"instance_id"`
PlusAPI APIDetails `yaml:"-" mapstructure:"api_details"`
}
HostMetrics struct {
Scrapers *HostMetricsScrapers `yaml:"-" mapstructure:"scrapers"`
CollectionInterval time.Duration `yaml:"-" mapstructure:"collection_interval"`
InitialDelay time.Duration `yaml:"-" mapstructure:"initial_delay"`
}
HostMetricsScrapers struct {
CPU *CPUScraper `yaml:"-" mapstructure:"cpu"`
Disk *DiskScraper `yaml:"-" mapstructure:"disk"`
Filesystem *FilesystemScraper `yaml:"-" mapstructure:"filesystem"`
Memory *MemoryScraper `yaml:"-" mapstructure:"memory"`
Network *NetworkScraper `yaml:"-" mapstructure:"network"`
}
CPUScraper struct{}
DiskScraper struct{}
FilesystemScraper struct{}
MemoryScraper struct{}
NetworkScraper struct{}
Command struct {
Server *ServerConfig `yaml:"-" mapstructure:"server"`
Auth *AuthConfig `yaml:"-" mapstructure:"auth"`
TLS *TLSConfig `yaml:"-" mapstructure:"tls"`
}
ServerConfig struct {
Host string `yaml:"-" mapstructure:"host"`
Port int `yaml:"-" mapstructure:"port"`
Type ServerType `yaml:"-" mapstructure:"type"`
}
AuthConfig struct {
Token string `yaml:"-" mapstructure:"token"`
TokenPath string `yaml:"-" mapstructure:"tokenpath"`
}
TLSConfig struct {
Cert string `yaml:"-" mapstructure:"cert"`
Key string `yaml:"-" mapstructure:"key"`
Ca string `yaml:"-" mapstructure:"ca"`
ServerName string `yaml:"-" mapstructure:"server_name"`
SkipVerify bool `yaml:"-" mapstructure:"skip_verify"`
}
// Specialized TLS configuration for OtlpReceiver with self-signed cert generation.
OtlpTLSConfig struct {
Cert string `yaml:"-" mapstructure:"cert"`
Key string `yaml:"-" mapstructure:"key"`
Ca string `yaml:"-" mapstructure:"ca"`
ServerName string `yaml:"-" mapstructure:"server_name"`
ExistingCert bool `yaml:"-"`
SkipVerify bool `yaml:"-" mapstructure:"skip_verify"`
GenerateSelfSignedCert bool `yaml:"-" mapstructure:"generate_self_signed_cert"`
}
Watchers struct {
FileWatcher FileWatcher `yaml:"-" mapstructure:"file_watcher"`
InstanceWatcher InstanceWatcher `yaml:"-" mapstructure:"instance_watcher"`
InstanceHealthWatcher InstanceHealthWatcher `yaml:"-" mapstructure:"instance_health_watcher"`
}
InstanceWatcher struct {
MonitoringFrequency time.Duration `yaml:"-" mapstructure:"monitoring_frequency"`
}
InstanceHealthWatcher struct {
MonitoringFrequency time.Duration `yaml:"-" mapstructure:"monitoring_frequency"`
}
FileWatcher struct {
ExcludeFiles []string `yaml:"-" mapstructure:"exclude_files"`
MonitoringFrequency time.Duration `yaml:"-" mapstructure:"monitoring_frequency"`
}
)
func (col *Collector) Validate(allowedDirectories []string) error {
var err error
cleaned := filepath.Clean(col.ConfigPath)
if !isAllowedDir(cleaned, allowedDirectories) {
err = errors.Join(err, fmt.Errorf("collector path %s not allowed", col.ConfigPath))
}
for _, nginxReceiver := range col.Receivers.NginxReceivers {
err = errors.Join(err, nginxReceiver.Validate(allowedDirectories))
}
return err
}
func (nr *NginxReceiver) Validate(allowedDirectories []string) error {
var err error
if _, uuidErr := uuid.Parse(nr.InstanceID); uuidErr != nil {
err = errors.Join(err, errors.New("invalid nginx receiver instance ID"))
}
for _, al := range nr.AccessLogs {
if !isAllowedDir(al.FilePath, allowedDirectories) {
err = errors.Join(err, fmt.Errorf("invalid nginx receiver access log path: %s", al.FilePath))
}
if len(al.FilePath) != 0 {
// The log format's double quotes must be escaped so that
// valid YAML is produced when executing the Go template.
al.LogFormat = strings.ReplaceAll(al.LogFormat, `"`, `\"`)
}
}
return err
}
func (c *Config) IsDirectoryAllowed(directory string) bool {
return isAllowedDir(directory, c.AllowedDirectories)
}
func (c *Config) IsGrpcClientConfigured() bool {
return c.Command != nil &&
c.Command.Server != nil &&
c.Command.Server.Host != "" &&
c.Command.Server.Port != 0 &&
c.Command.Server.Type == Grpc
}
func (c *Config) IsAuthConfigured() bool {
return c.Command.Auth != nil &&
(c.Command.Auth.Token != "" || c.Command.Auth.TokenPath != "")
}
func (c *Config) IsTLSConfigured() bool {
return c.Command.TLS != nil
}
func (c *Config) IsFeatureEnabled(feature string) bool {
for _, enabledFeature := range c.Features {
if enabledFeature == feature {
return true
}
}
return false
}
func (c *Config) IsACollectorExporterConfigured() bool {
if c.Collector == nil {
return false
}
return c.Collector.Exporters.PrometheusExporter != nil ||
c.Collector.Exporters.OtlpExporters != nil ||
c.Collector.Exporters.Debug != nil
}
func (c *Config) AreReceiversConfigured() bool {
if c.Collector == nil {
return false
}
return c.Collector.Receivers.NginxPlusReceivers != nil ||
len(c.Collector.Receivers.NginxPlusReceivers) > 0 ||
c.Collector.Receivers.OtlpReceivers != nil ||
len(c.Collector.Receivers.OtlpReceivers) > 0 ||
c.Collector.Receivers.NginxReceivers != nil ||
len(c.Collector.Receivers.NginxReceivers) > 0 ||
c.Collector.Receivers.HostMetrics != nil ||
c.Collector.Receivers.TcplogReceivers != nil ||
len(c.Collector.Receivers.TcplogReceivers) > 0
}
func isAllowedDir(dir string, allowedDirs []string) bool {
for _, allowedDirectory := range allowedDirs {
if strings.HasPrefix(dir, allowedDirectory) {
return true
}
}
return false
}