Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add comparison in do_if #605

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ ozon*
dist/
tests-offsets
testdata

.idea/
4 changes: 2 additions & 2 deletions Insanedocfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ extractors:
fn-list: '"fn-list" #4 /Plugin\)\s(.+)\s{/'
match-modes: '"match-modes" /MatchMode(.*),/ /\"(.*)\"/'
do-if-node: '"do-if-node" /DoIfNode(\w+)\s/'
do-if-field-op: '"do-if-field-op" /doIfField(\w+)OpBytes\s/'
do-if-logical-op: '"do-if-logical-op" /doIfLogical(\w+)Bytes\s/'
do-if-field-op: '"do-if-field-op" /doIfField(\w+)OpName\s/'
do-if-logical-op: '"do-if-logical-op" /doIfLogical(\w+)Name\s/'
decorators:
config-params: '_ _ /*`%s`* / /*`default=%s`* / /*`%s`* / /*`options=%s`* /'
fn-list: '_ _ /`%s`/'
Expand Down
21 changes: 15 additions & 6 deletions fd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,12 @@ var (
"or": struct{}{},
}
doIfFieldOpNodes = map[string]struct{}{
"equal": struct{}{},
"contains": struct{}{},
"prefix": struct{}{},
"suffix": struct{}{},
"regex": struct{}{},
"equal": struct{}{},
"contains": struct{}{},
"prefix": struct{}{},
"suffix": struct{}{},
"regex": struct{}{},
"bytes_len_cmp": struct{}{},
}
)

Expand Down Expand Up @@ -234,13 +235,21 @@ func extractFieldOpNode(opName string, jsonNode *simplejson.Json) (pipeline.DoIf
var result pipeline.DoIfNode
var err error
fieldPath := jsonNode.Get("field").MustString()

caseSensitiveNode, has := jsonNode.CheckGet("case_sensitive")
caseSensitive := true
if has {
caseSensitive = caseSensitiveNode.MustBool()
}

cmpOpNode, has := jsonNode.CheckGet("cmp_op")
cmpOp := ""
if has {
cmpOp = cmpOpNode.MustString()
}

vals := extractFieldOpVals(jsonNode)
result, err = pipeline.NewFieldOpNode(opName, fieldPath, caseSensitive, vals)
result, err = pipeline.NewFieldOpNode(opName, fieldPath, caseSensitive, cmpOp, vals)
if err != nil {
return nil, fmt.Errorf("failed to init field op: %w", err)
}
Expand Down
36 changes: 36 additions & 0 deletions fd/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type doIfTreeNode struct {
fieldOp string
fieldName string
caseSensitive bool
cmpOp string
values [][]byte

logicalOp string
Expand All @@ -55,6 +56,7 @@ func buildDoIfTree(node *doIfTreeNode) (pipeline.DoIfNode, error) {
node.fieldOp,
node.fieldName,
node.caseSensitive,
node.cmpOp,
node.values,
)
} else if node.logicalOp != "" {
Expand Down Expand Up @@ -125,6 +127,12 @@ func Test_extractDoIfChecker(t *testing.T) {
"op": "regex",
"field": "message",
"values": ["test-\\d+", "test-msg-\\d+"]
},
{
"op": "bytes_len_cmp",
"field": "message",
"cmp_op": "lt",
"values": ["100"]
}
]
}
Expand Down Expand Up @@ -173,6 +181,13 @@ func Test_extractDoIfChecker(t *testing.T) {
values: [][]byte{[]byte(`test-\d+`), []byte(`test-msg-\d+`)},
caseSensitive: true,
},
{
fieldOp: "bytes_len_cmp",
fieldName: "message",
cmpOp: "lt",
values: [][]byte{[]byte("100")},
caseSensitive: true,
},
},
},
},
Expand Down Expand Up @@ -259,6 +274,27 @@ func Test_extractDoIfChecker(t *testing.T) {
},
wantErr: true,
},
{
name: "error_no_cmp_value",
args: args{
cfgStr: `{"op": "bytes_len_cmp", "field": "message", "values": []}`,
},
wantErr: true,
},
{
name: "error_too_many_cmp_values",
args: args{
cfgStr: `{"op": "bytes_len_cmp", "field": "message", "values": ["1", "2", "3"]}`,
},
wantErr: true,
},
{
name: "error_invalid_cmp_value",
args: args{
cfgStr: `{"op": "bytes_len_cmp", "field": "message", "values": ["can't parse it"]}`,
},
wantErr: true,
},
}
for _, tt := range tests {
tt := tt
Expand Down
27 changes: 27 additions & 0 deletions pipeline/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,33 @@ result:

<br>

**`BytesLengthCmp`** compares field length in bytes with certain value

Example:
```yaml
pipelines:
test:
actions:
- type: discard
do_if:
op: bytes_len_cmp
field: pod_id
cmp_op: lt
values: [5]
```

result:
```
{"pod_id":""} # discarded
{"pod_id":123} # discarded
{"pod_id":12345} # not discarded
{"pod_id":123456} # not discarded
```

Possible comparison operations (values of field 'cmp_op'): `lt`, `le`, `gt`, `ge`, `eq`, `ne`.

<br>


### Logical op node
DoIf logical op node is a node considered to be the root or an edge between nodes.
Expand Down
Loading
Loading