Skip to content

Commit 90fd199

Browse files
committed
Improve tests. Update configuration examples
1 parent f1382b7 commit 90fd199

File tree

5 files changed

+410
-74
lines changed

5 files changed

+410
-74
lines changed

examples/config/global_config.json

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,35 @@
1515
"info"
1616
],
1717
"metrics": false,
18-
"output": [
19-
"json"
20-
],
18+
"output": {
19+
"destinations": [
20+
{
21+
"name": "default_destination",
22+
"type": "file",
23+
"path": "stdout",
24+
"url": "",
25+
"format": "table"
26+
}
27+
],
28+
"streams": [
29+
{
30+
"name": "default_stream",
31+
"destinations": [
32+
"default_destination"
33+
],
34+
"filters": {
35+
"events": [],
36+
"policies": []
37+
},
38+
"buffer": {
39+
"mode": "block",
40+
"size": 1024
41+
}
42+
}
43+
]
44+
},
2145
"perf-buffer-size": 1024,
2246
"pprof": false,
2347
"pyroscope": false,
2448
"signatures-dir": ""
25-
}
49+
}

examples/config/global_config.yaml

Lines changed: 19 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -112,56 +112,25 @@ scope:
112112
metrics: false
113113

114114
output:
115-
json:
116-
files:
117-
- stdout
118-
119-
# table:
120-
# files:
121-
# - /path/to/table1.out
122-
# - /path/to/table2.out
123-
124-
# table-verbose:
125-
# files:
126-
# - stdout
127-
128-
# gotemplate:
129-
# template: /path/to/my_template1.tmpl
130-
# files:
131-
# - /path/to/output1.out
132-
# - /path/to/output2.out
133-
134-
# forward:
135-
# - forward1:
136-
# protocol: tcp
137-
# user: user
138-
# password: pass
139-
# host: 127.0.0.1
140-
# port: 24224
141-
# tag: tracee1
142-
# - forward2:
143-
# protocol: udp
144-
# user: user
145-
# password: pass
146-
# host: 127.0.0.1
147-
# port: 24225
148-
# tag: tracee2
149-
150-
# webhook:
151-
# - webhook1:
152-
# protocol: http
153-
# host: localhost
154-
# port: 8000
155-
# timeout: 5s
156-
# gotemplate: /path/to/template/test.tmpl
157-
# content-type: application/json
158-
# - webhook2:
159-
# protocol: http
160-
# host: localhost
161-
# port: 9000
162-
# timeout: 3s
163-
# gotemplate: /path/to/template/test.tmpl
164-
# content-type: application/json
115+
# destinations:
116+
# - name: destination_name
117+
# type: file | webhook | forward
118+
# path: path in case of type == file
119+
# url: url in case of type == webhook or type == forward
120+
# format: json | gotemplate=/path/to/template.tpl | table | table-verbose
121+
122+
# streams:
123+
# - name: stream_name
124+
# destinations:
125+
# - destination_name
126+
# filters:
127+
# events:
128+
# - event_to_filter
129+
# policies:
130+
# - policy_to_filter
131+
# buffer:
132+
# mode: drop
133+
# size: 1024
165134

166135
# options:
167136
# none: false

pkg/cmd/cobra/cobra.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ func GetTraceeRunner(c *cobra.Command, version string) (cmd.Runner, error) {
285285

286286
cfg.Output = output.TraceeConfig
287287
} else {
288-
outputConfig, err := getStructuredOutputConfig()
288+
outputConfig, err := GetStructuredOutputConfig()
289289
if err != nil {
290290
return runner, err
291291
}

pkg/cmd/cobra/config.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cobra
33
import (
44
"fmt"
55
"os"
6+
"strings"
67

78
"github.com/spf13/viper"
89

@@ -354,24 +355,33 @@ func isStructured(key string) bool {
354355
return ok
355356
}
356357

357-
func getStructuredOutputConfig() (*OutputConfig, error) {
358+
// GetStructuredOutputConfig gets the output from config file
359+
// and returns an error if the validation goes wrong. Exported for testing
360+
func GetStructuredOutputConfig() (*OutputConfig, error) {
358361
var output OutputConfig
359362
err := viper.UnmarshalKey("output", &output)
360363
if err != nil {
361364
return nil, err
362365
}
363366

364-
// validation
367+
if err := output.ValidateOrDefaults(); err != nil {
368+
return nil, err
369+
}
370+
371+
return &output, nil
372+
}
373+
374+
func (output *OutputConfig) ValidateOrDefaults() error {
365375
for _, stream := range output.Streams {
366376
if stream.Name == "" {
367-
return nil, errfmt.Errorf("validaiton error: name is mandatory for streams")
377+
return errfmt.Errorf("validation error: name is mandatory for streams")
368378
}
369379
}
370380

371381
for dIdx := range output.Destinations {
372382
destination := &output.Destinations[dIdx]
373383
if destination.Name == "" {
374-
return nil, errfmt.Errorf("validaiton error: name is mandatory for destinations")
384+
return errfmt.Errorf("validation error: name is mandatory for destinations")
375385
}
376386

377387
if destination.Type == "" {
@@ -384,11 +394,22 @@ func getStructuredOutputConfig() (*OutputConfig, error) {
384394

385395
if (destination.Type == "webhook" || destination.Type == "forward") &&
386396
destination.Url == "" {
387-
return nil, errfmt.Errorf("validaiton error: url is mandatory in destination for %s", destination.Type)
397+
return errfmt.Errorf("validation error: url is mandatory in destination for %s", destination.Type)
398+
}
399+
400+
if destination.Format != "json" && destination.Format != "table" &&
401+
destination.Format != "table-verbose" && !strings.HasPrefix(destination.Format, "gotemplate=") {
402+
return errfmt.Errorf("validation error: destination format %s not valid for destination %s",
403+
destination.Format, destination.Name)
404+
}
405+
406+
if destination.Type != "file" && destination.Type != "webhook" && destination.Type != "forward" {
407+
return errfmt.Errorf("validation error: destination type %s not valid for destination %s",
408+
destination.Type, destination.Name)
388409
}
389410
}
390411

391-
return &output, nil
412+
return nil
392413
}
393414

394415
// The following structures are very similar to the ones defined in pkg/config/config.go.

0 commit comments

Comments
 (0)