-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy patherrors.go
199 lines (165 loc) · 5.91 KB
/
errors.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
package sigma
import (
"errors"
"fmt"
"reflect"
)
// ErrInvalidRegex contextualizes broken regular expressions presented by the user
type ErrInvalidRegex struct {
Pattern string
Err error
}
// Error implements error
func (e ErrInvalidRegex) Error() string {
return fmt.Sprintf("/%s/ %s", e.Pattern, e.Err)
}
// ErrMissingDetection indicates missing detection field
type ErrMissingDetection struct{}
func (e ErrMissingDetection) Error() string { return "sigma rule is missing detection field" }
// ErrMissingConditionItem indicates that identifier in condition is missing in detection map
type ErrMissingConditionItem struct {
Key string
}
func (e ErrMissingConditionItem) Error() string {
return fmt.Sprintf("missing condition identifier %s", e.Key)
}
// ErrEmptyDetection indicates detection field present but empty
type ErrEmptyDetection struct{}
func (e ErrEmptyDetection) Error() string { return "sigma rule has detection but is empty" }
// ErrMissingCondition indicates missing condition field
type ErrMissingCondition struct{}
func (e ErrMissingCondition) Error() string { return "complex sigma rule is missing condition" }
// ErrIncompleteDetection indicates a rule has defined identifiers that are missing in detection map
type ErrIncompleteDetection struct {
Condition string
Keys []string
Msg string
}
func (e ErrIncompleteDetection) Error() string {
return fmt.Sprintf(
"incomplete rule, missing fields from condition. [%s]. Has %+v. %s",
e.Condition,
func() []string {
if e.Keys != nil {
return e.Keys
}
return []string{}
}(),
e.Msg,
)
}
// ErrUnsupportedToken is a parser error indicating lexical token that is not yet supported
// Meant to be used as informational warning, rather than application breaking error
type ErrUnsupportedToken struct{ Msg string }
func (e ErrUnsupportedToken) Error() string { return fmt.Sprintf("UNSUPPORTED TOKEN: %s", e.Msg) }
// ErrWip indicates a rule expression that is currently Work In Progress
// Functions like ErrUnsupportedToken but indicates that feature is under active development
// Non-critical escape hatch while debugging
type ErrWip struct{}
func (e ErrWip) Error() string { return "work in progress" }
// ErrParseYaml indicates YAML parsing error
type ErrParseYaml struct {
Path string
Err error
Count int
}
func (e ErrParseYaml) Error() string {
return fmt.Sprintf("%d - File: %s; Err: %s", e.Count, e.Path, e.Err)
}
// ErrGotBrokenYamlFiles is a bulk error handler for dealing with broken sigma rules
// Some rules are bound to fail, no reason to exit entire application
// Individual errors can be collected and returned at the end
// Called decides if they should be only reported or it warrants full exit
type ErrBulkParseYaml struct {
Errs []ErrParseYaml
}
func (e ErrBulkParseYaml) Error() string {
return fmt.Sprintf("got %d broken yaml files", len(e.Errs))
}
// ErrInvalidTokenSeq indicates expression syntax error from rule writer
// For example, two indents should be separated by a logical AND / OR operator
type ErrInvalidTokenSeq struct {
Prev, Next Item
Collected []Item
}
func (e ErrInvalidTokenSeq) Error() string {
return fmt.Sprintf(`seq error after collecting %d elements.`+
` Invalid token sequence %s -> %s. Values: %s -> %s.`,
len(e.Collected), e.Prev.T, e.Next.T, e.Prev.Val, e.Next.Val)
}
// ErrIncompleteTokenSeq is invoked when lex channel drain does not end with EOF
// thus indicating incomplete lexing sequence
type ErrIncompleteTokenSeq struct {
Expression string
Items []Item
Last Item
}
func (e ErrIncompleteTokenSeq) Error() string {
return fmt.Sprintf("last element should be EOF, got token %s with value %s",
e.Last.T.String(), e.Last.Val)
}
// ErrInvalidKeywordConstruct indicates that parser found a keyword expression
// that did not match any known keyword rule structure
// could be unmarshal issue
type ErrInvalidKeywordConstruct struct {
Msg string
Expr interface{}
}
func (e ErrInvalidKeywordConstruct) Error() string {
return fmt.Sprintf(`invalid type for parsing keyword expression. `+
`Should be slice of strings or a funky one element map where value is slice of strings. `+
`Or other stuff. Got |%+v| with type |%s|`,
e.Expr, reflect.TypeOf(e.Expr).String())
}
// ErrInvalidSelectionConstruct indicates that parser found a selection expression
// that did not match any known selection rule structure
// could be unmarshal issue
type ErrInvalidSelectionConstruct struct {
Msg string
Expr interface{}
}
func (e ErrInvalidSelectionConstruct) Error() string {
return fmt.Sprintf("invalid type for parsing selection expression. Got |%+v| with type |%s|",
e.Expr, reflect.TypeOf(e.Expr).String())
}
// ErrInvalidKind indicates that type switching function received an unsupported
// or unhandled data type
// Contains the type in question, arbitrary error text and keyword/selection indicator
// Critical is used to indicate if this error should cause an exit or can simply
// be handled as a warning for future improvements
type ErrInvalidKind struct {
reflect.Kind
Msg string
T identType
Critical bool
}
func (e ErrInvalidKind) Error() string {
return fmt.Sprintf("%s data type error. %s got %s. %s",
func() string {
if e.Critical {
return "CRITICAL"
}
return "Informative"
}(), e.T, e.Kind, e.Msg)
}
// ErrUnsupportedExpression indicates that rule expression is not yet supported by parser
// mostly a type issue
type ErrUnsupportedExpression struct {
Msg string
T identType
Expr interface{}
Critical bool
}
func (e ErrUnsupportedExpression) Error() string {
return fmt.Sprintf("%s unsupported expression for %s, %s. %+v",
func() string {
if e.Critical {
return "CRITICAL"
}
return "Informative"
}(), e.T, e.Msg, e.Expr)
}
// ErrUnableToReflect indicates that kind reflection could not be done, as
// typeOf returned a nil value
// likely a missing pattern
var ErrUnableToReflect = errors.New("unable to reflect on pattern kind")