Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kaanaktas committed Oct 4, 2022
1 parent dd59185 commit c0b541e
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 37 deletions.
4 changes: 2 additions & 2 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
4 changes: 2 additions & 2 deletions cache/in_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion config/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
21 changes: 15 additions & 6 deletions datafilter/types.go → datafilter/base.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
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"`
Sample string `json:"sample"`
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
}
10 changes: 8 additions & 2 deletions datafilter/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 0 additions & 15 deletions datafilter/owasp.go
Original file line number Diff line number Diff line change
@@ -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
}
10 changes: 1 addition & 9 deletions datafilter/pan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}

0 comments on commit c0b541e

Please sign in to comment.