瀏覽代碼

Move grok_pattern.go away from pkg/types to trim bouncer dependencies (#2269)

mmetc 2 年之前
父節點
當前提交
6096cb3c9b
共有 6 個文件被更改,包括 16 次插入19 次删除
  1. 1 2
      cmd/crowdsec-cli/lapi.go
  2. 1 2
      pkg/hubtest/hubtest_item.go
  3. 1 1
      pkg/parser/grok_pattern.go
  4. 3 3
      pkg/parser/node.go
  5. 8 9
      pkg/parser/node_test.go
  6. 2 2
      pkg/parser/runtime.go

+ 1 - 2
cmd/crowdsec-cli/lapi.go

@@ -24,7 +24,6 @@ import (
 	"github.com/crowdsecurity/crowdsec/pkg/exprhelpers"
 	"github.com/crowdsecurity/crowdsec/pkg/exprhelpers"
 	"github.com/crowdsecurity/crowdsec/pkg/models"
 	"github.com/crowdsecurity/crowdsec/pkg/models"
 	"github.com/crowdsecurity/crowdsec/pkg/parser"
 	"github.com/crowdsecurity/crowdsec/pkg/parser"
-	"github.com/crowdsecurity/crowdsec/pkg/types"
 )
 )
 
 
 var LAPIURLPrefix string = "v1"
 var LAPIURLPrefix string = "v1"
@@ -440,7 +439,7 @@ cscli lapi context delete --value evt.Line.Src
 	return cmdContext
 	return cmdContext
 }
 }
 
 
-func detectStaticField(GrokStatics []types.ExtraField) []string {
+func detectStaticField(GrokStatics []parser.ExtraField) []string {
 	ret := make([]string, 0)
 	ret := make([]string, 0)
 	for _, static := range GrokStatics {
 	for _, static := range GrokStatics {
 		if static.Parsed != "" {
 		if static.Parsed != "" {

+ 1 - 2
pkg/hubtest/hubtest_item.go

@@ -10,7 +10,6 @@ import (
 	"github.com/crowdsecurity/crowdsec/pkg/csconfig"
 	"github.com/crowdsecurity/crowdsec/pkg/csconfig"
 	"github.com/crowdsecurity/crowdsec/pkg/cwhub"
 	"github.com/crowdsecurity/crowdsec/pkg/cwhub"
 	"github.com/crowdsecurity/crowdsec/pkg/parser"
 	"github.com/crowdsecurity/crowdsec/pkg/parser"
-	"github.com/crowdsecurity/crowdsec/pkg/types"
 	log "github.com/sirupsen/logrus"
 	log "github.com/sirupsen/logrus"
 	"gopkg.in/yaml.v2"
 	"gopkg.in/yaml.v2"
 )
 )
@@ -23,7 +22,7 @@ type HubTestItemConfig struct {
 	LogType         string             `yaml:"log_type"`
 	LogType         string             `yaml:"log_type"`
 	Labels          map[string]string  `yaml:"labels"`
 	Labels          map[string]string  `yaml:"labels"`
 	IgnoreParsers   bool               `yaml:"ignore_parsers"`   // if we test a scenario, we don't want to assert on Parser
 	IgnoreParsers   bool               `yaml:"ignore_parsers"`   // if we test a scenario, we don't want to assert on Parser
-	OverrideStatics []types.ExtraField `yaml:"override_statics"` //Allow to override statics. Executed before s00
+	OverrideStatics []parser.ExtraField `yaml:"override_statics"` //Allow to override statics. Executed before s00
 }
 }
 
 
 type HubIndex struct {
 type HubIndex struct {

+ 1 - 1
pkg/types/grok_pattern.go → pkg/parser/grok_pattern.go

@@ -1,4 +1,4 @@
-package types
+package parser
 
 
 import (
 import (
 	"time"
 	"time"

+ 3 - 3
pkg/parser/node.go

@@ -56,11 +56,11 @@ type Node struct {
 	SubGroks yaml.MapSlice `yaml:"pattern_syntax,omitempty"`
 	SubGroks yaml.MapSlice `yaml:"pattern_syntax,omitempty"`
 
 
 	//Holds a grok pattern
 	//Holds a grok pattern
-	Grok types.GrokPattern `yaml:"grok,omitempty"`
+	Grok GrokPattern `yaml:"grok,omitempty"`
 	//Statics can be present in any type of node and is executed last
 	//Statics can be present in any type of node and is executed last
-	Statics []types.ExtraField `yaml:"statics,omitempty"`
+	Statics []ExtraField `yaml:"statics,omitempty"`
 	//Stash allows to capture data from the log line and store it in an accessible cache
 	//Stash allows to capture data from the log line and store it in an accessible cache
-	Stash []types.DataCapture `yaml:"stash,omitempty"`
+	Stash []DataCapture `yaml:"stash,omitempty"`
 	//Whitelists
 	//Whitelists
 	Whitelist Whitelist           `yaml:"whitelist,omitempty"`
 	Whitelist Whitelist           `yaml:"whitelist,omitempty"`
 	Data      []*types.DataSource `yaml:"data,omitempty"`
 	Data      []*types.DataSource `yaml:"data,omitempty"`

+ 8 - 9
pkg/parser/node_test.go

@@ -3,7 +3,6 @@ package parser
 import (
 import (
 	"testing"
 	"testing"
 
 
-	"github.com/crowdsecurity/crowdsec/pkg/types"
 	yaml "gopkg.in/yaml.v2"
 	yaml "gopkg.in/yaml.v2"
 )
 )
 
 
@@ -20,7 +19,7 @@ func TestParserConfigs(t *testing.T) {
 		Valid    bool
 		Valid    bool
 	}{
 	}{
 		//valid node with grok pattern
 		//valid node with grok pattern
-		{&Node{Debug: true, Stage: "s00", Grok: types.GrokPattern{RegexpValue: "^x%{DATA:extr}$", TargetField: "t"}}, true, true},
+		{&Node{Debug: true, Stage: "s00", Grok: GrokPattern{RegexpValue: "^x%{DATA:extr}$", TargetField: "t"}}, true, true},
 		//bad filter
 		//bad filter
 		{&Node{Debug: true, Stage: "s00", Filter: "ratata"}, false, false},
 		{&Node{Debug: true, Stage: "s00", Filter: "ratata"}, false, false},
 		//empty node
 		//empty node
@@ -28,25 +27,25 @@ func TestParserConfigs(t *testing.T) {
 		//bad subgrok
 		//bad subgrok
 		{&Node{Debug: true, Stage: "s00", SubGroks: yaml.MapSlice{{Key: string("FOOBAR"), Value: string("[a-$")}}}, false, true},
 		{&Node{Debug: true, Stage: "s00", SubGroks: yaml.MapSlice{{Key: string("FOOBAR"), Value: string("[a-$")}}}, false, true},
 		//valid node with grok pattern
 		//valid node with grok pattern
-		{&Node{Debug: true, Stage: "s00", SubGroks: yaml.MapSlice{{Key: string("FOOBAR"), Value: string("[a-z]")}}, Grok: types.GrokPattern{RegexpValue: "^x%{FOOBAR:extr}$", TargetField: "t"}}, true, true},
+		{&Node{Debug: true, Stage: "s00", SubGroks: yaml.MapSlice{{Key: string("FOOBAR"), Value: string("[a-z]")}}, Grok: GrokPattern{RegexpValue: "^x%{FOOBAR:extr}$", TargetField: "t"}}, true, true},
 		//bad node success
 		//bad node success
-		{&Node{Debug: true, Stage: "s00", OnSuccess: "ratat", Grok: types.GrokPattern{RegexpValue: "^x%{DATA:extr}$", TargetField: "t"}}, false, false},
+		{&Node{Debug: true, Stage: "s00", OnSuccess: "ratat", Grok: GrokPattern{RegexpValue: "^x%{DATA:extr}$", TargetField: "t"}}, false, false},
 		//ok node success
 		//ok node success
-		{&Node{Debug: true, Stage: "s00", OnSuccess: "continue", Grok: types.GrokPattern{RegexpValue: "^x%{DATA:extr}$", TargetField: "t"}}, true, true},
+		{&Node{Debug: true, Stage: "s00", OnSuccess: "continue", Grok: GrokPattern{RegexpValue: "^x%{DATA:extr}$", TargetField: "t"}}, true, true},
 		//valid node with grok sub-pattern used by name
 		//valid node with grok sub-pattern used by name
-		{&Node{Debug: true, Stage: "s00", SubGroks: yaml.MapSlice{{Key: string("FOOBARx"), Value: string("[a-z] %{DATA:lol}$")}}, Grok: types.GrokPattern{RegexpName: "FOOBARx", TargetField: "t"}}, true, true},
+		{&Node{Debug: true, Stage: "s00", SubGroks: yaml.MapSlice{{Key: string("FOOBARx"), Value: string("[a-z] %{DATA:lol}$")}}, Grok: GrokPattern{RegexpName: "FOOBARx", TargetField: "t"}}, true, true},
 		//node with unexisting grok pattern
 		//node with unexisting grok pattern
-		{&Node{Debug: true, Stage: "s00", Grok: types.GrokPattern{RegexpName: "RATATA", TargetField: "t"}}, false, true},
+		{&Node{Debug: true, Stage: "s00", Grok: GrokPattern{RegexpName: "RATATA", TargetField: "t"}}, false, true},
 		//node with grok pattern dependencies
 		//node with grok pattern dependencies
 		{&Node{Debug: true, Stage: "s00", SubGroks: yaml.MapSlice{
 		{&Node{Debug: true, Stage: "s00", SubGroks: yaml.MapSlice{
 			{Key: string("SUBGROK"), Value: string("[a-z]")},
 			{Key: string("SUBGROK"), Value: string("[a-z]")},
 			{Key: string("MYGROK"), Value: string("[a-z]%{SUBGROK}")},
 			{Key: string("MYGROK"), Value: string("[a-z]%{SUBGROK}")},
-		}, Grok: types.GrokPattern{RegexpValue: "^x%{MYGROK:extr}$", TargetField: "t"}}, true, true},
+		}, Grok: GrokPattern{RegexpValue: "^x%{MYGROK:extr}$", TargetField: "t"}}, true, true},
 		//node with broken grok pattern dependencies
 		//node with broken grok pattern dependencies
 		{&Node{Debug: true, Stage: "s00", SubGroks: yaml.MapSlice{
 		{&Node{Debug: true, Stage: "s00", SubGroks: yaml.MapSlice{
 			{Key: string("SUBGROKBIS"), Value: string("[a-z]%{MYGROKBIS}")},
 			{Key: string("SUBGROKBIS"), Value: string("[a-z]%{MYGROKBIS}")},
 			{Key: string("MYGROKBIS"), Value: string("[a-z]")},
 			{Key: string("MYGROKBIS"), Value: string("[a-z]")},
-		}, Grok: types.GrokPattern{RegexpValue: "^x%{MYGROKBIS:extr}$", TargetField: "t"}}, false, true},
+		}, Grok: GrokPattern{RegexpValue: "^x%{MYGROKBIS:extr}$", TargetField: "t"}}, false, true},
 	}
 	}
 	for idx := range CfgTests {
 	for idx := range CfgTests {
 		err := CfgTests[idx].NodeCfg.compile(pctx, EnricherCtx{})
 		err := CfgTests[idx].NodeCfg.compile(pctx, EnricherCtx{})

+ 2 - 2
pkg/parser/runtime.go

@@ -94,7 +94,7 @@ func SetTargetByName(target string, value string, evt *types.Event) bool {
 	return true
 	return true
 }
 }
 
 
-func printStaticTarget(static types.ExtraField) string {
+func printStaticTarget(static ExtraField) string {
 
 
 	if static.Method != "" {
 	if static.Method != "" {
 		return static.Method
 		return static.Method
@@ -111,7 +111,7 @@ func printStaticTarget(static types.ExtraField) string {
 	}
 	}
 }
 }
 
 
-func (n *Node) ProcessStatics(statics []types.ExtraField, event *types.Event) error {
+func (n *Node) ProcessStatics(statics []ExtraField, event *types.Event) error {
 	//we have a few cases :
 	//we have a few cases :
 	//(meta||key) + (static||reference||expr)
 	//(meta||key) + (static||reference||expr)
 	var value string
 	var value string