Skip to content

Commit

Permalink
regex support
Browse files Browse the repository at this point in the history
  • Loading branch information
dmachard committed Dec 10, 2023
1 parent 75d4f1b commit 3bc0a99
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
4 changes: 3 additions & 1 deletion config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ pipelines:
dnsmessage:
matching:
include:
dnstap.operation: "CLIENT_QUERY"
dns.flags.ad: true
#dnstap.operation: "CLIENT_QUERY"
dnstap.operation: "CLIENT_Q*"
policy: "drop-unmatched" #passthrough
routes: [ log-queries ]

Expand Down
30 changes: 23 additions & 7 deletions dnsutils/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -840,32 +840,48 @@ func (dm *DNSMessage) Flatten() (ret map[string]interface{}, err error) {

func (dm *DNSMessage) Matching(matching map[string]interface{}) (error, bool) {

if len(matching) == 0 {
return nil, false
}

dmValue := reflect.ValueOf(dm)

if dmValue.Kind() == reflect.Ptr {
dmValue = dmValue.Elem()
}

var isMatch = true

for nestedKeys, value := range matching {

fieldValue, found := getFieldByJSONTagV2(dmValue, nestedKeys)
fieldValue, found := getFieldByJSONTag(dmValue, nestedKeys)
if !found {
fmt.Printf("pattern '%s' does not exist in the DNSMessage structure\n", nestedKeys)
return nil, false
}

if reflect.DeepEqual(value, fieldValue.Interface()) {
return nil, true
reflectedValue := reflect.ValueOf(value)

// regex support for string
if reflectedValue.Kind() == reflect.String {
pattern := regexp.MustCompile(reflectedValue.Interface().(string))
if !pattern.MatchString(fieldValue.Interface().(string)) {
isMatch = false
break
}
} else {

Check failure on line 872 in dnsutils/message.go

View workflow job for this annotation

GitHub Actions / linter

elseif: can replace 'else {if cond {}}' with 'else if cond {}' (gocritic)
return nil, false
if value != fieldValue.Interface() {
isMatch = false
break
}
}

}

return nil, false
return nil, isMatch
}

func getFieldByJSONTagV2(value reflect.Value, nestedKeys string) (reflect.Value, bool) {
func getFieldByJSONTag(value reflect.Value, nestedKeys string) (reflect.Value, bool) {
listKeys := strings.SplitN(nestedKeys, ".", 2)

for j, jsonKey := range listKeys {
Expand All @@ -878,7 +894,7 @@ func getFieldByJSONTagV2(value reflect.Value, nestedKeys string) (reflect.Value,
if tag == jsonKey {
// Recursively check nested fields if the current field is a struct
if field.Type.Kind() == reflect.Struct {
if fieldValue, found := getFieldByJSONTagV2(value.Field(i), listKeys[j+1]); found {
if fieldValue, found := getFieldByJSONTag(value.Field(i), listKeys[j+1]); found {
return fieldValue, true
}
} else {
Expand Down

0 comments on commit 3bc0a99

Please sign in to comment.