/
autoextract.go
214 lines (195 loc) · 5 KB
/
autoextract.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
/*************************************************************************
* Copyright 2021 Gravwell, Inc. All rights reserved.
* Contact: <legal@gravwell.io>
*
* This software may be modified and distributed under the terms of the
* BSD 2-clause license. See the LICENSE file for details.
**************************************************************************/
package types
import (
"encoding/json"
"errors"
"fmt"
"io"
"sort"
"strings"
"time"
"github.com/google/uuid"
"github.com/gravwell/gravwell/v3/ingest"
)
var (
ErrMissingModule error = errors.New("extraction module name is missing")
ErrMissingParams error = errors.New("extraction parameters missing")
ErrMissingTag error = errors.New("extraction tag assignment missing")
)
// AutoExtractor object. When setting an AutoExtractor, only Name, Module,
// Params, and Tag must be set.
type AXDefinition struct {
Name string `toml:"name,omitempty" json:",omitempty"`
Desc string `toml:"desc,omitempty" json:",omitempty"`
Module string `toml:"module"`
Params string `toml:"params" json:",omitempty"`
Args string `toml:"args,omitempty" json:",omitempty"`
Tag string `toml:"tag"`
Tags []string `toml:"tags"` // AXs can support multiple tags. For backwards compatibility, we leave Tag and add Tags
Labels []string `toml:"-"`
UID int32 `toml:"-"`
GIDs []int32 `toml:"-"`
Global bool `toml:"-"`
UUID uuid.UUID `toml:"-"`
Synced bool `toml:"-" json:"-"`
LastUpdated time.Time `toml:"-"`
}
// Verify all required fields in an AXDefinition object are valid.
func (dc *AXDefinition) Validate() error {
if dc.Module == `` {
return ErrMissingModule
}
if len(dc.GetTags()) == 0 {
return ErrMissingTag
}
for _, t := range dc.GetTags() {
if err := ingest.CheckTag(t); err != nil {
return err
}
}
dc.Name = sanitizeValue(dc.Name)
dc.Desc = sanitizeValue(dc.Desc)
dc.Module = sanitizeValue(dc.Module)
dc.Params = sanitizeValue(dc.Params)
dc.Tag = sanitizeValue(dc.Tag)
for i, t := range dc.Tags {
dc.Tags[i] = sanitizeValue(t)
}
dc.Args = sanitizeValue(dc.Args)
collisions := make(map[string]bool)
for _, t := range dc.GetTags() {
if _, ok := collisions[t]; ok {
return fmt.Errorf("Tag %v already defined", t)
}
collisions[t] = true
}
return nil
}
func (dc *AXDefinition) GetTags() []string {
if dc.Tag == "" {
return dc.Tags
}
return append(dc.Tags, dc.Tag)
}
func sanitizeValue(v string) string {
trim := func(r rune) rune {
switch r {
case '\n':
return ' '
case '\r':
return ' '
}
return r
}
return strings.Map(trim, v)
}
// Encode the "config file" styled AX definition to the given io.Writer. hdr is
// an optional header comment.
func (dc AXDefinition) Encode(fout io.Writer, hdr string) (err error) {
if err = dc.Validate(); err != nil {
return
}
//write header comment if it exists
if hdr != `` {
if _, err = fmt.Fprintf(fout, "# %s\n", hdr); err != nil {
return
}
}
//write actual header
if _, err = fmt.Fprintf(fout, "[[extraction]]\n"); err != nil {
return
}
//write required parameters
for _, t := range dc.GetTags() {
if err = GenLine(fout, `tag`, t); err != nil {
return
}
}
if err = GenLine(fout, `module`, dc.Module); err != nil {
return
}
if err = GenLine(fout, `params`, dc.Params); err != nil {
return
}
//write optional parameters
if err = GenLine(fout, `args`, dc.Args); err != nil {
return
}
if err = GenLine(fout, `name`, dc.Name); err != nil {
return
}
if err = GenLine(fout, `desc`, dc.Desc); err != nil {
return
}
return
}
func (dc AXDefinition) JSONMetadata() (ro json.RawMessage, err error) {
x := &struct {
Name string `json:"name,omitempty"`
Desc string `json:"desc,omitempty"`
Module string `json:"module"`
Tag string `json:"tag"`
Tags []string `json:"tags"`
}{
Name: dc.Name,
Desc: dc.Desc,
Module: dc.Module,
Tag: dc.Tag,
Tags: dc.GetTags(),
}
if x.Desc == `` {
x.Desc = fmt.Sprintf("%s extractor for tags %v", x.Module, dc.GetTags())
}
b, err := json.Marshal(x)
return json.RawMessage(b), err
}
func GenLine(wtr io.Writer, name, line string) (err error) {
if len(line) == 0 {
return
}
_, err = fmt.Fprintf(wtr, " %s = '%s'\n", name, line)
return
}
func (axd AXDefinition) Equal(v AXDefinition) bool {
if axd.Name != v.Name || axd.Desc != v.Desc || axd.Module != v.Module || axd.UUID != v.UUID {
return false
}
if axd.Params != v.Params || axd.Args != v.Args {
return false
}
t1 := axd.GetTags()
t2 := v.GetTags()
sort.Strings(t1)
sort.Strings(t2)
if len(t1) != len(t2) {
return false
}
for i, t := range t1 {
if t != t2[i] {
return false
}
}
if axd.UID != v.UID || axd.Global != v.Global {
return false
}
if len(axd.Labels) != len(v.Labels) || len(axd.GIDs) != len(v.GIDs) {
return false
}
for i, l := range axd.Labels {
if v.Labels[i] != l {
return false
}
}
for i, g := range axd.GIDs {
if v.GIDs[i] != g {
return false
}
}
return true
}