-
Notifications
You must be signed in to change notification settings - Fork 66
/
msghandling.go
170 lines (146 loc) · 4.95 KB
/
msghandling.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
166
167
168
169
170
package msgservice
import (
"encoding/json"
"log"
"strings"
"time"
"github.com/aquasecurity/postee/v2/actions"
"github.com/aquasecurity/postee/v2/data"
"github.com/aquasecurity/postee/v2/dbservice"
"github.com/aquasecurity/postee/v2/regoservice"
"github.com/aquasecurity/postee/v2/routes"
)
type MsgService struct {
}
func (scan *MsgService) MsgHandling(input []byte, output actions.Action, route *routes.InputRoute, inpteval data.Inpteval, AquaServer *string) {
if output == nil {
return
}
in := map[string]interface{}{}
if err := json.Unmarshal(input, &in); err != nil {
prnInputLogs("json.Unmarshal error for %q: %v", input, err)
return
}
//TODO move logic below somewhere close to Jira action implementation
owners := ""
applicationScopeOwnersObj, ok := in["application_scope_owners"]
if ok {
applicationScopeOwners := make([]string, 0)
for _, owner := range applicationScopeOwnersObj.([]interface{}) {
applicationScopeOwners = append(applicationScopeOwners, owner.(string))
}
if len(applicationScopeOwners) > 0 {
owners = strings.Join(applicationScopeOwners, ";")
}
}
if route.Plugins.UniqueMessageProps != nil && len(route.Plugins.UniqueMessageProps) > 0 {
msgKey := GetMessageUniqueId(in, route.Plugins.UniqueMessageProps)
expired := calculateExpired(route.Plugins.UniqueMessageTimeoutSeconds)
wasStored, err := dbservice.MayBeStoreMessage(input, msgKey, expired)
if err != nil {
log.Printf("Error while storing input: %v", err)
return
}
if !wasStored {
log.Printf("The same message was received before: %s", msgKey)
return
}
}
posteeOpts := map[string]string{
"AquaServer": *AquaServer,
}
in["postee"] = posteeOpts
content, err := inpteval.Eval(in, *AquaServer)
if err != nil {
log.Printf("Error while evaluating input: %v", err)
return
}
if owners != "" {
content["owners"] = owners
}
if route.Plugins.AggregateMessageNumber > 0 && inpteval.IsAggregationSupported() {
aggregated := AggregateScanAndGetQueue(route.Name, content, route.Plugins.AggregateMessageNumber, false)
if len(aggregated) > 0 {
content, err = inpteval.BuildAggregatedContent(aggregated)
if err != nil {
log.Printf("Error while building aggregated content: %v", err)
return
}
if route.SerializeActions {
send(output, content)
} else {
go send(output, content)
}
}
} else if route.Plugins.AggregateTimeoutSeconds > 0 && inpteval.IsAggregationSupported() {
AggregateScanAndGetQueue(route.Name, content, 0, true)
if !route.IsSchedulerRun() { //TODO route shouldn't have any associated logic
log.Printf("about to schedule %s\n", route.Name)
RunScheduler(route, send, AggregateScanAndGetQueue, inpteval, &route.Name, output)
} else {
log.Printf("%s is already scheduled\n", route.Name)
}
} else {
if route.SerializeActions {
send(output, content)
} else {
go send(output, content)
}
}
}
// EvaluateRegoRule returns true in case the given input ([]byte) matches the input of the given route
func (scan *MsgService) EvaluateRegoRule(r *routes.InputRoute, input []byte) bool {
in := map[string]interface{}{}
if err := json.Unmarshal(input, &in); err != nil {
prnInputLogs("json.Unmarshal error for %q: %v", input, err)
return false
}
// input-files will override input, if both are provided
if ok, err := regoservice.DoesMatchRegoCriteria(in, r.InputFiles, r.Input); err != nil {
if !regoservice.IsUsedRegoFiles(r.InputFiles) {
prnInputLogs("Error while evaluating rego rule %s :%v for the input %s", r.Input, err, input)
} else {
prnInputLogs("Error while evaluating rego rule for input files :%v for the input %s", err, input)
}
return false
} else if !ok {
if !regoservice.IsUsedRegoFiles(r.InputFiles) {
prnInputLogs("Input %s... doesn't match a REGO rule: %s", input, r.Input)
} else {
prnInputLogs("Input %s... doesn't match a REGO input files rule", input)
}
return false
}
return true
}
func send(otpt actions.Action, cnt map[string]string) {
err := otpt.Send(cnt)
if err != nil {
log.Printf("Error while sending event: %v", err)
}
err = dbservice.RegisterPlgnInvctn(otpt.GetName())
if err != nil {
log.Printf("Error while building aggregated content: %v", err)
return
}
}
func calculateExpired(UniqueMessageTimeoutSeconds int) *time.Time {
if UniqueMessageTimeoutSeconds == 0 {
return nil
}
timeToExpire := time.Duration(UniqueMessageTimeoutSeconds) * time.Second
expired := time.Now().UTC().Add(timeToExpire)
return &expired
}
var AggregateScanAndGetQueue = func(outputName string, currentContent map[string]string, counts int, ignoreLength bool) []map[string]string {
aggregatedScans, err := dbservice.AggregateScans(outputName, currentContent, counts, ignoreLength)
if err != nil {
log.Printf("AggregateScans Error: %v", err)
return aggregatedScans
}
if len(currentContent) != 0 && len(aggregatedScans) == 0 {
log.Printf("New scan was added to the queue of %q without sending.", outputName)
return nil
}
return aggregatedScans
}