forked from aquasecurity/tracee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.go
177 lines (150 loc) · 4.21 KB
/
input.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
package main
import (
"bufio"
"encoding/gob"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"os"
"strings"
tracee "github.com/aquasecurity/tracee/tracee-ebpf/tracee/external"
"github.com/aquasecurity/tracee/tracee-rules/types"
)
var errHelp = errors.New("user has requested help text")
type inputFormat uint8
const (
invalidInputFormat inputFormat = iota
jsonInputFormat
gobInputFormat
)
type traceeInputOptions struct {
inputFile *os.File
inputFormat inputFormat
}
func setupTraceeInputSource(opts *traceeInputOptions) (chan types.Event, error) {
if opts.inputFormat == jsonInputFormat {
return setupTraceeJSONInputSource(opts)
}
if opts.inputFormat == gobInputFormat {
return setupTraceeGobInputSource(opts)
}
return nil, errors.New("could not set up input source")
}
func setupTraceeGobInputSource(opts *traceeInputOptions) (chan types.Event, error) {
dec := gob.NewDecoder(opts.inputFile)
res := make(chan types.Event)
go func() {
for {
var event tracee.Event
err := dec.Decode(&event)
if err != nil {
if err == io.EOF {
break
} else {
log.Printf("Error while decoding event: %v", err)
}
} else {
res <- event
}
}
opts.inputFile.Close()
close(res)
}()
return res, nil
}
func setupTraceeJSONInputSource(opts *traceeInputOptions) (chan types.Event, error) {
res := make(chan types.Event)
scanner := bufio.NewScanner(opts.inputFile)
go func() {
for scanner.Scan() {
event := scanner.Bytes()
var e tracee.Event
err := json.Unmarshal(event, &e)
if err != nil {
log.Printf("invalid json in %s: %v", string(event), err)
}
res <- e
}
opts.inputFile.Close()
close(res)
}()
return res, nil
}
func parseTraceeInputOptions(inputOptions []string) (*traceeInputOptions, error) {
var (
inputSourceOptions traceeInputOptions
err error
)
if len(inputOptions) == 0 {
return nil, errors.New("no tracee input options specified")
}
for i := range inputOptions {
if inputOptions[i] == "help" {
return nil, errHelp
}
kv := strings.Split(inputOptions[i], ":")
if len(kv) != 2 {
return nil, fmt.Errorf("invalid input-tracee option: %s", inputOptions[i])
}
if kv[0] == "" || kv[1] == "" {
return nil, fmt.Errorf("empty key or value passed: key: >%s< value: >%s<", kv[0], kv[1])
}
if kv[0] == "file" {
err = parseTraceeInputFile(&inputSourceOptions, kv[1])
if err != nil {
return nil, err
}
} else if kv[0] == "format" {
err = parseTraceeInputFormat(&inputSourceOptions, kv[1])
if err != nil {
return nil, err
}
} else {
return nil, fmt.Errorf("invalid input-tracee option key: %s", kv[0])
}
}
return &inputSourceOptions, nil
}
func parseTraceeInputFile(option *traceeInputOptions, fileOpt string) error {
if fileOpt == "stdin" {
option.inputFile = os.Stdin
return nil
}
_, err := os.Stat(fileOpt)
if err != nil {
return fmt.Errorf("invalid Tracee input file: %s", fileOpt)
}
f, err := os.Open(fileOpt)
if err != nil {
return fmt.Errorf("invalid file: %s", fileOpt)
}
option.inputFile = f
return nil
}
func parseTraceeInputFormat(option *traceeInputOptions, formatString string) error {
formatString = strings.ToUpper(formatString)
if formatString == "JSON" {
option.inputFormat = jsonInputFormat
} else if formatString == "GOB" {
option.inputFormat = gobInputFormat
} else {
option.inputFormat = invalidInputFormat
return fmt.Errorf("invalid tracee input format specified: %s", formatString)
}
return nil
}
func printHelp() {
traceeInputHelp := `
tracee-rules --input-tracee <key:value>,<key:value> --input-tracee <key:value>
Specify various key value pairs for input options tracee-ebpf. The following key options are available:
'file' - Input file source. You can specify a relative or absolute path. You may also specify 'stdin' for standard input.
'format' - Input format. Options currently include 'JSON' and 'GOB'. Both can be specified as output formats from tracee-ebpf.
Examples:
'tracee-rules --input-tracee file:./events.json --input-tracee format:json'
'tracee-rules --input-tracee file:./events.gob --input-tracee format:gob'
'sudo tracee-ebpf -o format:gob | tracee-rules --input-tracee file:stdin --input-tracee format:gob'
`
fmt.Println(traceeInputHelp)
}