-
Notifications
You must be signed in to change notification settings - Fork 0
/
security_rule_model.go
executable file
·165 lines (158 loc) · 4.59 KB
/
security_rule_model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package security_rule
import (
"context"
"encoding/json"
"errors"
"fmt"
logs "github.com/eru-tech/eru/eru-logs/eru-logs"
"github.com/eru-tech/eru/eru-templates/gotemplate"
"strings"
)
type CustomRule struct {
AND []CustomRuleDetails `json:",omitempty"`
OR []CustomRuleDetails `json:",omitempty"`
}
type CustomRuleDetails struct {
DataType string `json:",omitempty"`
Variable1 string `json:",omitempty"`
Variable2 string `json:",omitempty"`
Operator string `json:",omitempty"`
ErrorMsg string `json:",omitempty"`
AND []CustomRuleDetails `json:",omitempty"`
OR []CustomRuleDetails `json:",omitempty"`
}
type SecurityRule struct {
RuleType string
CustomRule CustomRule
}
func (sr SecurityRule) Stringify(ctx context.Context, vars map[string]interface{}, ignoreIfNotFound bool) (str string, err error) {
logs.WithContext(ctx).Debug("Stringify - Start")
if len(sr.CustomRule.AND) > 0 {
str, err = processRuleClause(ctx, sr.CustomRule.AND, "and", vars, ignoreIfNotFound)
return
}
if len(sr.CustomRule.OR) > 0 {
str, err = processRuleClause(ctx, sr.CustomRule.OR, "or", vars, ignoreIfNotFound)
return
}
return
}
func processRuleClause(ctx context.Context, rules []CustomRuleDetails, conditionType string, vars map[string]interface{}, ignoreIfNotFound bool) (ruleOutput string, err error) {
logs.WithContext(ctx).Debug("processRuleClause - Start")
var strArray []string
str := ""
for _, v := range rules {
if len(v.AND) > 0 {
str, err = processRuleClause(ctx, v.AND, "and", vars, ignoreIfNotFound)
} else if len(v.OR) > 0 {
str, err = processRuleClause(ctx, v.OR, "or", vars, ignoreIfNotFound)
} else {
str, err = stringifyRule(ctx, v, conditionType, vars, ignoreIfNotFound)
}
if str != "" {
strArray = append(strArray, str)
}
}
if len(strArray) > 0 {
conditionType = fmt.Sprint(" ", conditionType, " ")
ruleOutput = fmt.Sprint("( ", strings.Join(strArray, conditionType), " )")
}
return
}
func stringifyRule(ctx context.Context, cd CustomRuleDetails, conditionType string, vars map[string]interface{}, ignoreIfNotFound bool) (str string, err error) {
logs.WithContext(ctx).Debug("stringifyRule - Start")
op := ""
valPrefix := ""
valSuffix := ""
if cd.DataType == "string" {
valPrefix = "'"
valSuffix = "'"
}
if cd.Operator == "like" {
valPrefix = "'%"
valSuffix = "%'"
}
switch cd.Operator {
case "btw":
op = " between "
break
case "gte":
op = " >= "
break
case "lte":
op = " <= "
break
case "gt":
op = " > "
break
case "lt":
op = " < "
break
case "eq":
op = " = "
break
case "ne":
op = " <> "
break
case "in":
op = " in "
break
case "nin":
op = " not in "
break
case "like":
op = " like "
break
case "eq_null":
op = " is null "
break
case "neq_null":
op = " is not null "
break
default:
//do nothing
break
}
logs.WithContext(ctx).Info(fmt.Sprint("cd.Variable1 = ", cd.Variable1))
var1Bytes, err := processTemplate(ctx, "customrule", cd.Variable1, vars, "string")
if err == nil {
cd.Variable1 = fmt.Sprint(valPrefix, string(var1Bytes), valSuffix)
} else if ignoreIfNotFound && err.Error() != "no variable prefix found" {
return "", nil
}
logs.WithContext(ctx).Info(fmt.Sprint("cd.Variable2 = ", cd.Variable2))
var2Bytes, err := processTemplate(ctx, "customrule", cd.Variable2, vars, "string")
if err == nil {
cd.Variable2 = fmt.Sprint(valPrefix, string(var2Bytes), valSuffix)
} else if ignoreIfNotFound && err.Error() != "no variable prefix found" {
return "", nil
}
return fmt.Sprint(cd.Variable1, op, cd.Variable2), nil
}
func processTemplate(ctx context.Context, templateName string, templateString string, vars map[string]interface{}, outputType string) (output []byte, err error) {
logs.WithContext(ctx).Debug("processTemplate - Start")
logs.WithContext(ctx).Info(fmt.Sprint("templateString = ", templateString))
ruleValue := strings.SplitN(templateString, ".", 2)
if ruleValue[0] == "token" {
templateStr := fmt.Sprint("{{ .", ruleValue[1], " }}")
goTmpl := gotemplate.GoTemplate{templateName, templateStr}
outputObj, err := goTmpl.Execute(ctx, vars["token"], outputType)
if err != nil {
logs.WithContext(ctx).Error(err.Error())
return nil, err
} else if outputType == "string" {
return []byte(outputObj.(string)), nil
} else {
output, err = json.Marshal(outputObj)
if err != nil {
logs.WithContext(ctx).Error(err.Error())
return nil, err
}
}
} else {
err = errors.New("no variable prefix found")
logs.WithContext(ctx).Error(err.Error())
}
//todo - to add if prefix is not token
return
}