/
pastelyzer.go
378 lines (303 loc) · 9.45 KB
/
pastelyzer.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package main
import (
"bytes"
"encoding/json"
"fmt"
"mime/multipart"
"net/http"
"regexp"
"strings"
"sync"
"github.com/blastrain/vitess-sqlparser/sqlparser"
"github.com/cert-lv/graphoscope/pdk"
"github.com/umpc/go-sortedmap"
"github.com/umpc/go-sortedmap/desc"
)
/*
* Check "pdk/plugin.go" for the built-in plugin functions description
*/
func (p *plugin) Conf() *pdk.Source {
return p.source
}
func (p *plugin) Setup(source *pdk.Source, limit int) error {
// Validate necessary parameters
if source.Access["url"] == "" {
return fmt.Errorf("'access.url' is not defined")
} else if source.Access["url"][0:4] != "http" {
return fmt.Errorf("'access.url' must start with 'http[s]://'")
}
// Store settings
p.source = source
p.limit = limit
p.url = source.Access["url"]
// Set possible variable type & searching fields
for _, relation := range source.Relations {
for _, types := range relation.From.VarTypes {
types.RegexCompiled = regexp.MustCompile(types.Regex)
}
for _, types := range relation.To.VarTypes {
types.RegexCompiled = regexp.MustCompile(types.Regex)
}
}
// fmt.Printf("Pastelyzer %s: %#v\n\n", source.Name, p)
return nil
}
func (p *plugin) Fields() ([]string, error) {
return p.source.QueryFields, nil
}
func (p *plugin) Search(stmt *sqlparser.Select) ([]map[string]interface{}, map[string]interface{}, map[string]interface{}, error) {
// Storage for the results to return
results := []map[string]interface{}{}
// Convert SQL statement
searchFields, err := p.convert(stmt)
if err != nil {
return nil, nil, nil, err
}
var body *bytes.Buffer
var debug map[string]interface{}
/*
* Get all artefacts of the requested paste ID
*/
if searchFields[0][0] == "source" {
body, debug, err = p.getArtefacts(searchFields[0][1])
if err != nil {
return nil, nil, debug, err
}
} else {
/*
* Send indicators to get related paste IDs
*/
body, debug, err = p.getPastes(searchFields)
if err != nil {
return nil, nil, debug, err
}
}
/*
* Receive hits and deserialize them
*/
mx := sync.Mutex{}
umx := sync.Mutex{}
unique := make(map[string]bool)
counter := 0
// Struct to store statistics data
// when the amount of returned entries is too large
stats := pdk.NewStats()
for _, field := range p.source.StatsFields {
stats.Fields[field] = sortedmap.New(10, desc.Int)
}
var entries []map[string]interface{}
err = json.NewDecoder(body).Decode(&entries)
if err != nil {
return nil, nil, debug, err
}
// Process results
for _, entry := range entries {
// Stop when results count is too big
if counter >= p.limit {
top, err := stats.ToJSON(p.source.Name)
if err != nil {
return nil, nil, debug, err
}
return nil, top, debug, nil
}
// Update stats
for _, field := range p.source.StatsFields {
stats.Update(entry, field)
}
// Go through all the predefined relations and collect unique entries
for _, relation := range p.source.Relations {
if entry[relation.From.ID] != nil && entry[relation.To.ID] != nil {
umx.Lock()
// Use "Printf(...%v..." instead of "entry[relation.From.ID].(string)"
// as the value can be not a string only
if _, exists := unique[fmt.Sprintf("%v-%v-%v-%v", relation.From.ID, entry[relation.From.ID], relation.To.ID, entry[relation.To.ID])]; exists {
if pdk.ResultsContain(results, entry, relation) {
umx.Unlock()
continue
}
}
counter++
unique[fmt.Sprintf("%v-%v-%v-%v", relation.From.ID, entry[relation.From.ID], relation.To.ID, entry[relation.To.ID])] = true
umx.Unlock()
/*
* FROM node with attributes
*/
from := map[string]interface{}{
"id": entry[relation.From.ID],
"group": relation.From.Group,
"search": relation.From.Search,
}
// Check FROM type & searching fields
if len(relation.From.VarTypes) > 0 {
for _, t := range relation.From.VarTypes {
if t.RegexCompiled.MatchString(fmt.Sprintf("%v", entry[relation.From.ID])) {
from["group"] = t.Group
from["search"] = t.Search
break
}
}
}
if len(relation.From.Attributes) > 0 {
from["attributes"] = make(map[string]interface{})
pdk.CopyPresentValues(entry, from["attributes"].(map[string]interface{}), relation.From.Attributes)
}
// Manually add link to the paste's content
if relation.From.ID == "source" {
// Show ID only, not full URL
from["id"] = strings.Split(from["id"].(string), "/")[4]
// Append additional param,
// which is no available by default
if _, ok := from["attributes"]; !ok {
from["attributes"] = make(map[string]interface{})
}
from["attributes"].(map[string]interface{})["content"] = entry["source"].(string) + "body"
}
/*
* TO node
*/
to := map[string]interface{}{
"id": entry[relation.To.ID],
"group": relation.To.Group,
"search": relation.To.Search,
}
// Check FROM type & searching fields
if len(relation.To.VarTypes) > 0 {
for _, t := range relation.To.VarTypes {
if t.RegexCompiled.MatchString(fmt.Sprintf("%v", entry[relation.To.ID])) {
to["group"] = t.Group
to["search"] = t.Search
break
}
}
}
if len(relation.To.Attributes) > 0 {
to["attributes"] = make(map[string]interface{})
pdk.CopyPresentValues(entry, to["attributes"].(map[string]interface{}), relation.To.Attributes)
}
// Manually add link to the paste's content
if relation.To.ID == "source" {
// Show ID only, not full URL
to["id"] = strings.Split(to["id"].(string), "/")[4]
// Append additional param,
// which is no available by default
if _, ok := to["attributes"]; !ok {
to["attributes"] = make(map[string]interface{})
}
to["attributes"].(map[string]interface{})["content"] = entry["source"].(string) + "body"
}
// Resulting graph entry to return
result := make(map[string]interface{})
/*
* Edge between FROM and TO
*/
if relation.Edge != nil && (relation.Edge.Label != "" || len(relation.Edge.Attributes) > 0) {
result["edge"] = make(map[string]interface{})
if relation.Edge.Label != "" {
result["edge"].(map[string]interface{})["label"] = relation.Edge.Label
}
if len(relation.Edge.Attributes) > 0 {
result["edge"].(map[string]interface{})["attributes"] = make(map[string]interface{})
pdk.CopyPresentValues(entry, result["edge"].(map[string]interface{})["attributes"].(map[string]interface{}), relation.Edge.Attributes)
}
}
/*
* Put it together
*/
result["from"] = from
result["to"] = to
result["source"] = p.source.Name
//fmt.Println("Edge:", from, to, p.name)
/*
* Add current entry to the list to return
*/
mx.Lock()
results = append(results, result)
mx.Unlock()
}
}
}
return results, nil, debug, nil
}
/*
* Return all artefacts of the given paste ID
*/
func (p *plugin) getArtefacts(id string) (*bytes.Buffer, map[string]interface{}, error) {
// Debug info
debug := make(map[string]interface{})
query := p.url + "/content/" + id + "/artefacts/typed"
debug["query"] = query
// Create the POST request to the URL with all fields mounted
req, err := http.NewRequest("GET", query, nil)
if err != nil {
return nil, debug, fmt.Errorf("Error on request creation: %s", err.Error())
}
// Declare an HTTP client to execute the request
client := http.Client{Timeout: p.source.Timeout}
// Finally send a POST HTTP request
resp, err := client.Do(req)
if err != nil {
return nil, debug, fmt.Errorf("Error on request execution: %s", err.Error())
}
body := &bytes.Buffer{}
_, err = body.ReadFrom(resp.Body)
if err != nil {
resp.Body.Close()
return nil, debug, err
}
resp.Body.Close()
// Check the response
if resp.StatusCode != http.StatusOK {
return nil, debug, fmt.Errorf("Bad response StatusCode: %s", resp.Status)
}
return body, debug, nil
}
/*
* Return pastes ID where given indicators were found
*/
func (p *plugin) getPastes(searchFields [][2]string) (*bytes.Buffer, map[string]interface{}, error) {
// Create buffer
buf := new(bytes.Buffer)
// Create multipart Writer for that buffer
w := multipart.NewWriter(buf)
for _, field := range searchFields {
if err := w.WriteField(field[0], field[1]); err != nil {
return nil, nil, err
}
}
// IMPORTANT: Close multipart writer before using it
w.Close()
// Debug info
debug := make(map[string]interface{})
debug["query"] = p.url + "/artefacts/typed"
debug["POST_payload"] = buf.String()
// Create the POST request to the URL with all fields mounted
req, err := http.NewRequest("POST", p.url+"/artefacts/typed", buf)
if err != nil {
return nil, debug, fmt.Errorf("Error on request creation: %s", err.Error())
}
// Set the header of the request to send
req.Header.Set("Content-Type", w.FormDataContentType())
// Declare our HTTP client to execute the request
client := http.Client{Timeout: p.source.Timeout}
// Finally send our POST HTTP request
resp, err := client.Do(req)
if err != nil {
return nil, debug, fmt.Errorf("Error on request execution: %s", err.Error())
}
body := &bytes.Buffer{}
_, err = body.ReadFrom(resp.Body)
if err != nil {
resp.Body.Close()
return nil, debug, err
}
resp.Body.Close()
// Check the response
if resp.StatusCode != http.StatusOK {
return nil, debug, fmt.Errorf("Bad response StatusCode: %s", resp.Status)
}
return body, debug, nil
}
func (p *plugin) Stop() error {
// No error to check, so return nil
return nil
}