-
Notifications
You must be signed in to change notification settings - Fork 63
/
audit_log_query.go
212 lines (201 loc) · 5.03 KB
/
audit_log_query.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package service
import (
"bytes"
"encoding/json"
"fmt"
"strings"
v1 "github.com/paralus/paralus/proto/rpc/audit"
"google.golang.org/protobuf/types/known/structpb"
)
type AuditLogService struct {
auditQuery ElasticSearchQuery
}
func NewAuditLogService(url string, auditPattern string, logPrefix string) (*AuditLogService, error) {
auditQuery, err := NewElasticSearchQuery(url, auditPattern, logPrefix)
if err != nil {
return nil, err
}
return &AuditLogService{auditQuery: auditQuery}, nil
}
func (a *AuditLogService) GetAuditLog(req *v1.GetAuditLogSearchRequest) (res *v1.GetAuditLogSearchResponse, err error) {
if err != nil {
return nil, err
}
project, err := getPrjectFromUrlScope(req.GetMetadata().UrlScope)
if err != nil {
return nil, err
}
req.Filter.Projects = []string{project}
return a.GetAuditLogByProjects(req)
}
func validateQueryString(queryString string) error {
if strings.Contains(queryString, "*") {
return fmt.Errorf("'*' is not supported in search query")
}
if len(queryString) > 0 && len(queryString) < 3 {
return fmt.Errorf("search string has to be atleast 3 characters")
}
return nil
}
func getPrjectFromUrlScope(urlScope string) (string, error) {
s := strings.Split(urlScope, "/")
if len(s) != 2 {
_log.Errorw("Unable to retrieve project from urlScope", "urlScope", urlScope)
return "", fmt.Errorf("unable to retrieve project from urlScope")
}
return s[1], nil
}
func (a *AuditLogService) GetAuditLogByProjects(req *v1.GetAuditLogSearchRequest) (res *v1.GetAuditLogSearchResponse, err error) {
err = validateQueryString(req.GetFilter().QueryString)
if err != nil {
return nil, err
}
res = &v1.GetAuditLogSearchResponse{
Result: &structpb.Struct{},
}
var buf bytes.Buffer
var r map[string]interface{}
query := map[string]interface{}{
"_source": []string{"json"},
"size": 500,
"query": map[string]interface{}{
"bool": map[string]interface{}{
"must": []map[string]interface{}{
{
"term": map[string]interface{}{
"json.category": "AUDIT",
},
},
},
},
},
"sort": map[string]interface{}{
"json.timestamp": map[string]interface{}{
"order": "desc",
},
},
"aggs": map[string]interface{}{
"group_by_username": map[string]interface{}{
"terms": map[string]interface{}{
"field": "json.actor.account.username",
},
},
"group_by_type": map[string]interface{}{
"terms": map[string]interface{}{
"field": "json.type",
},
},
},
}
//aggregations
agg, _ := query["aggs"].(map[string]interface{})
if req.GetFilter().DashboardData {
agg["group_by_project"] = map[string]interface{}{
"terms": map[string]interface{}{
"field": "json.project",
"size": 1000,
},
"aggs": map[string]interface{}{
"group_by_username": map[string]interface{}{
"terms": map[string]interface{}{
"field": "json.actor.account.username",
"size": 1000,
},
},
"group_by_type": map[string]interface{}{
"terms": map[string]interface{}{
"field": "json.type",
"size": 1000,
},
},
},
}
}
// Filters
q, _ := query["query"].(map[string]interface{})
b, _ := q["bool"].(map[string]interface{})
m, _ := b["must"].([]map[string]interface{})
//Results not required in case of dashboard - only aggregations required
if req.GetFilter().DashboardData {
query["size"] = 0
}
// Add timefrom filter
if req.GetFilter().Timefrom != "" {
b["filter"] = map[string]interface{}{
"range": map[string]interface{}{
"json.timestamp": map[string]interface{}{
"gte": req.GetFilter().Timefrom,
"lt": "now",
},
},
}
}
// Add type
if req.GetFilter().Type != "" {
t := map[string]interface{}{
"term": map[string]interface{}{
"json.type": req.GetFilter().Type,
},
}
m = append(m, t)
}
// Add user
if req.GetFilter().User != "" {
t := map[string]interface{}{
"term": map[string]interface{}{
"json.actor.account.username": req.GetFilter().User,
},
}
m = append(m, t)
}
// Add user
if req.GetFilter().Client != "" {
t := map[string]interface{}{
"term": map[string]interface{}{
"json.client.type": req.GetFilter().Client,
},
}
m = append(m, t)
}
// Project
if len(req.GetFilter().Projects) > 0 {
t := map[string]interface{}{
"terms": map[string]interface{}{
"json.project": req.GetFilter().Projects,
},
}
m = append(m, t)
}
// query string
if req.GetFilter().QueryString != "" {
q := map[string]interface{}{
"query_string": map[string]interface{}{
"query": req.GetFilter().QueryString,
},
}
m = append(m, q)
}
b["must"] = m
if err := json.NewEncoder(&buf).Encode(query); err != nil {
_log.Errorw("Error encoding query:", " err", err)
return res, err
}
_log.Debug("Executing Query: ", q)
r, err = a.auditQuery.Handle(buf)
if err != nil {
return res, err
}
if r == nil {
return res, nil
}
// raw, err := json.Marshal(r)
// if err != nil {
// return res, err
// }
raw, err := structpb.NewStruct(r)
if err != nil {
return res, err
}
res = &v1.GetAuditLogSearchResponse{Result: raw}
return res, nil
}