-
Notifications
You must be signed in to change notification settings - Fork 4
/
fp_rule.go
73 lines (64 loc) · 1.92 KB
/
fp_rule.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
package limacharlie
import (
"encoding/json"
"fmt"
"net/http"
)
type FPRuleOptions struct {
// Replace rule if it already exists with this name.
IsReplace bool
}
type FPRuleName = string
type FPRule struct {
Detection Dict `json:"data" yaml:"data"`
OID string `json:"oid" yaml:"oid"`
Name FPRuleName `json:"name,omitempty" yaml:"name,omitempty"`
}
// FPRules get all false positive rules from a LC organization.
func (org Organization) FPRules() (map[FPRuleName]FPRule, error) {
resp := map[FPRuleName]FPRule{}
request := makeDefaultRequest(&resp)
if err := org.client.reliableRequest(http.MethodGet, fmt.Sprintf("fp/%s", org.client.options.OID), request); err != nil {
return map[FPRuleName]FPRule{}, err
}
return resp, nil
}
type fpAddRuleRequest struct {
IsReplace bool `json:"is_replace,string"`
Name FPRuleName `json:"name"`
Rule string `json:"rule"`
}
// FPRuleAdd add a false positive rule to a LC organization
func (org Organization) FPRuleAdd(name FPRuleName, detection interface{}, opts ...FPRuleOptions) error {
reqOpt := FPRuleOptions{
IsReplace: false,
}
for _, o := range opts {
reqOpt = o
}
ruleBytes, err := json.Marshal(detection)
if err != nil {
return err
}
resp := Dict{}
request := makeDefaultRequest(&resp).withFormData(fpAddRuleRequest{
IsReplace: reqOpt.IsReplace,
Name: name,
Rule: string(ruleBytes),
})
if err := org.client.reliableRequest(http.MethodPost, fmt.Sprintf("fp/%s", org.client.options.OID), request); err != nil {
return err
}
return nil
}
// FPRuleDelete delete a false positive rule from a LC organization
func (org Organization) FPRuleDelete(name FPRuleName) error {
resp := Dict{}
request := makeDefaultRequest(&resp).withFormData(Dict{
"name": name,
})
if err := org.client.reliableRequest(http.MethodDelete, fmt.Sprintf("fp/%s", org.client.options.OID), request); err != nil {
return err
}
return nil
}