Skip to content

Commit

Permalink
implemented custom configuration files to allow user to create own se…
Browse files Browse the repository at this point in the history
…ttings
  • Loading branch information
kaanaktas committed Jul 15, 2022
1 parent 01e2056 commit 9500cb9
Show file tree
Hide file tree
Showing 20 changed files with 362 additions and 186 deletions.
4 changes: 2 additions & 2 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"github.com/kaanaktas/go-slm/config"
"github.com/kaanaktas/go-slm/datafilter"
"github.com/kaanaktas/go-slm/executor"
"io/ioutil"
"log"
"testing"
Expand All @@ -25,6 +25,6 @@ func Benchmark(b *testing.B) {
serviceName := "test"

for i := 0; i < b.N; i++ {
datafilter.Execute(data, serviceName)
executor.Execute(data, serviceName, config.Request)
}
}
4 changes: 2 additions & 2 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
type Cache interface {
Get(k string) (interface{}, bool)
Set(k string, v interface{}, d time.Duration) error
Flush()
}

const (
NoExpiration time.Duration = -1
DefaultExpiration time.Duration = 0
NoExpiration time.Duration = -1
)
4 changes: 4 additions & 0 deletions cache/in_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func (i *inMemory) Set(k string, v interface{}, d time.Duration) error {
return nil
}

func (i *inMemory) Flush() {
i.cache.Flush()
}

var onceInMem sync.Once

func (i *inMemory) initiateInMemory() {
Expand Down
28 changes: 22 additions & 6 deletions config/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@ package config
import (
"log"
"os"
"runtime/debug"
"time"
)

const NumberOfWorker = 5

//policy rule set directions
const (
Request = "request"
Response = "response"
)

var RootDirectory, _ = os.Getwd()

func ReadFile(fileName string) ([]byte, error) {
content, err := os.ReadFile(fileName)
if err != nil {
log.Printf("error reading the file %q: %v\n", fileName, err)
return nil, err
}

Expand All @@ -30,8 +38,16 @@ func PolicyKey(serviceName, direction string) string {
return serviceName + "_" + direction
}

//policy rule set directions
const (
Request = "request"
Response = "response"
)
func IsModuleImported(currentModuleName string) bool {
if currentModuleName == "" {
currentModuleName = "github.com/kaanaktas/go-slm"
}

bi, ok := debug.ReadBuildInfo()
if !ok {
log.Println("Failed to read build info")
return false
}

return !(currentModuleName == bi.Path)
}
107 changes: 0 additions & 107 deletions datafilter/executor.go

This file was deleted.

64 changes: 56 additions & 8 deletions datafilter/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@ package datafilter

import (
"embed"
_ "embed"
"encoding/json"
"fmt"
"github.com/kaanaktas/go-slm/cache"
"github.com/kaanaktas/go-slm/config"
"log"
"path/filepath"
)

func init() {
loadRules()
}

type ruleSet struct {
Type string `json:"type"`
Rules []rules `json:"rules"`
Expand All @@ -23,20 +20,71 @@ type rules struct {
Path string `json:"path"`
}

var cacheIn = cache.NewInMemory()

//go:embed datafilter_rule_set.json
var dataFilterRuleSet []byte

//go:embed rules/*
var ruleFs embed.FS

func loadRules() {
var ruleSet []ruleSet
func indexOfRuleSet(ruleSet []ruleSet, ruleType string) int {
for i, set := range ruleSet {
if ruleType == set.Type {
return i
}
}
return -1
}

func indexOfRule(rules []rules, ruleName string) int {
for i, rule := range rules {
if ruleName == rule.Name {
return i
}
}
return -1
}

func Load(dataFilterRuleSetPath string) {
var ruleSet, customRuleSet []ruleSet
err := json.Unmarshal(dataFilterRuleSet, &ruleSet)
if err != nil {
msg := fmt.Sprintf("Can't unmarshall the content of datafilter_rule_set.json. Error: %s", err)
panic(msg)
}

if dataFilterRuleSetPath != "" {
content, err := config.ReadFile(filepath.Join(config.RootDirectory, dataFilterRuleSetPath))
if err != nil {
msg := fmt.Sprintf("Error while reading %s. Error: %s", dataFilterRuleSetPath, err)
panic(msg)
}
err = json.Unmarshal(content, &customRuleSet)
if err != nil {
msg := fmt.Sprintf("Can't unmarshall the content of datafilter_rule_set.json. Error: %s", err)
panic(msg)
}

for i := 0; i < len(customRuleSet); i++ {
ruleType := customRuleSet[i].Type
rsIndex := indexOfRuleSet(ruleSet, ruleType)
if rsIndex == -1 {
ruleSet = append(ruleSet, customRuleSet[i])
} else {
customRules := customRuleSet[i].Rules
for k := 0; k < len(customRules); k++ {
index := indexOfRule(ruleSet[rsIndex].Rules, customRules[k].Name)
if index == -1 {
ruleSet[rsIndex].Rules = append(ruleSet[rsIndex].Rules, customRules[k])
} else {
(ruleSet[rsIndex]).Rules[index].Path = customRules[k].Path
}
}
}
}
}

for _, set := range ruleSet {
for _, rule := range set.Rules {
content, err := ruleFs.ReadFile(rule.Path)
Expand All @@ -51,7 +99,7 @@ func loadRules() {
panic(msg)
}

validateRule := make([]validate, len(patterns))
validateRule := make([]Validate, len(patterns))
switch set.Type {
case PAN:
for i, v := range patterns {
Expand Down
8 changes: 4 additions & 4 deletions datafilter/owasp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ type owasp struct {
pattern
}

func (o owasp) validate(data *string) bool {
func (o owasp) Validate(data *string) bool {
matched, _ := regexp.MatchString(o.Rule, *data)
return matched
}

func (o owasp) toString() string {
func (o owasp) ToString() string {
return o.Name + " " + o.Message
}

func (o owasp) disable() bool {
return o.Disable
func (o owasp) Disable() bool {
return o.IsDisabled
}
8 changes: 4 additions & 4 deletions datafilter/pan.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type pan struct {
pattern
}

func (p pan) validate(data *string) bool {
func (p pan) Validate(data *string) bool {
r := regexp.MustCompile(p.Rule)
matchList := r.FindAllString(*data, -1)
for _, v := range matchList {
Expand All @@ -18,10 +18,10 @@ func (p pan) validate(data *string) bool {
return false
}

func (p pan) toString() string {
func (p pan) ToString() string {
return p.Name + " " + p.Message
}

func (p pan) disable() bool {
return p.Disable
func (p pan) Disable() bool {
return p.IsDisabled
}
24 changes: 24 additions & 0 deletions datafilter/testdata/datafilter_rule_set.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[
{
"type": "pan",
"rules": [
{
"name": "test_pan_process",
"path": "rules/pan_process.json"
}
]
},
{
"type": "owasp",
"rules": [
{
"name": "sqli",
"path": "rules/owasp_attack_sqli.json"
},
{
"name": "xss",
"path": "rules/owasp_attack_xss.json"
}
]
}
]
18 changes: 9 additions & 9 deletions datafilter/types.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package datafilter

type validate interface {
validate(data *string) bool
toString() string
disable() bool
type Validate interface {
Validate(data *string) bool
ToString() string
Disable() bool
}

//filter types
Expand All @@ -13,9 +13,9 @@ const (
)

type pattern struct {
Name string `json:"name"`
Rule string `json:"rule"`
Sample string `json:"sample"`
Message string `json:"message"`
Disable bool `json:"disable"`
Name string `json:"name"`
Rule string `json:"rule"`
Sample string `json:"sample"`
Message string `json:"message"`
IsDisabled bool `json:"disable"`
}
Loading

0 comments on commit 9500cb9

Please sign in to comment.