diff --git a/cache/cache.go b/cache/cache.go index 074a490..b1c8194 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -5,8 +5,8 @@ import ( ) type Cache interface { - Get(k string) (interface{}, bool) - Set(k string, v interface{}, d time.Duration) + Get(k string) (any, bool) + Set(k string, v any, d time.Duration) Flush() } diff --git a/cache/in_memory.go b/cache/in_memory.go index 0ea1b45..e2331c1 100644 --- a/cache/in_memory.go +++ b/cache/in_memory.go @@ -10,11 +10,11 @@ type inMemory struct { cache *gocache.Cache } -func (i *inMemory) Get(k string) (interface{}, bool) { +func (i *inMemory) Get(k string) (any, bool) { return i.cache.Get(k) } -func (i *inMemory) Set(k string, v interface{}, d time.Duration) { +func (i *inMemory) Set(k string, v any, d time.Duration) { i.cache.Set(k, v, d*time.Minute) } diff --git a/config/common.go b/config/common.go index 63cca04..fd0be77 100644 --- a/config/common.go +++ b/config/common.go @@ -32,7 +32,7 @@ func MustReadFile(fileName string) []byte { return content } -func MustUnmarshalYaml(path string, content []byte, decodedContent interface{}) { +func MustUnmarshalYaml(path string, content []byte, decodedContent any) { err := yaml.Unmarshal(content, decodedContent) if err != nil { panic(fmt.Sprintf("Can't unmarshall the content of %s. Error: %s", path, err)) diff --git a/datafilter/types.go b/datafilter/base.go similarity index 53% rename from datafilter/types.go rename to datafilter/base.go index a26608e..cc37b17 100644 --- a/datafilter/types.go +++ b/datafilter/base.go @@ -1,17 +1,13 @@ package datafilter +import "regexp" + type Validate interface { Validate(data *string) bool ToString() string Disable() bool } -//filter types -const ( - PAN = "pan" - OWASP = "owasp" -) - type pattern struct { Name string `json:"name"` Rule string `json:"rule"` @@ -19,3 +15,16 @@ type pattern struct { Message string `json:"message"` IsDisabled bool `json:"disable"` } + +func (p *pattern) Validate(data *string) bool { + matched, _ := regexp.MatchString(p.Rule, *data) + return matched +} + +func (p *pattern) ToString() string { + return p.Name + " " + p.Message +} + +func (p *pattern) Disable() bool { + return p.IsDisabled +} diff --git a/datafilter/load.go b/datafilter/load.go index e52a769..3f84c99 100644 --- a/datafilter/load.go +++ b/datafilter/load.go @@ -10,6 +10,12 @@ import ( "path/filepath" ) +//filter types +const ( + PAN = "pan" + OWASP = "owasp" +) + type ruleSet struct { Type string `yaml:"type"` Rules []rules `yaml:"rules"` @@ -44,11 +50,11 @@ func Load(dataFilterRuleSetPath string) { switch set.Type { case PAN: for i, v := range patterns { - validateRule[i] = pan{pattern: v} + validateRule[i] = &pan{pattern: v} } case OWASP: for i, v := range patterns { - validateRule[i] = owasp{pattern: v} + validateRule[i] = &owasp{pattern: v} } } cacheIn.Set(rule.Name, validateRule, cache.NoExpiration) diff --git a/datafilter/owasp.go b/datafilter/owasp.go index ffb0412..45a94a0 100644 --- a/datafilter/owasp.go +++ b/datafilter/owasp.go @@ -1,20 +1,5 @@ package datafilter -import "regexp" - type owasp struct { pattern } - -func (o owasp) Validate(data *string) bool { - matched, _ := regexp.MatchString(o.Rule, *data) - return matched -} - -func (o owasp) ToString() string { - return o.Name + " " + o.Message -} - -func (o owasp) Disable() bool { - return o.IsDisabled -} diff --git a/datafilter/pan.go b/datafilter/pan.go index 72e5cfd..6d58967 100644 --- a/datafilter/pan.go +++ b/datafilter/pan.go @@ -9,7 +9,7 @@ type pan struct { pattern } -func (p pan) Validate(data *string) bool { +func (p *pan) Validate(data *string) bool { dataWithoutSpace := strings.ReplaceAll(*data, " ", "") r := regexp.MustCompile(p.Rule) matchList := r.FindAllString(dataWithoutSpace, -1) @@ -21,11 +21,3 @@ func (p pan) Validate(data *string) bool { return false } - -func (p pan) ToString() string { - return p.Name + " " + p.Message -} - -func (p pan) Disable() bool { - return p.IsDisabled -}