forked from cert-lv/graphoscope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.go
727 lines (616 loc) · 18.1 KB
/
upload.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"time"
)
var (
// Regex to validate a datetime value
reDatetime = regexp.MustCompile(`^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\dZ$`)
)
/*
* Structure to describe user's uploaded list of indicators
* to be processed in a background
*/
type Upload struct {
// Data source to query
Source string
// Datetime range to search in
StartTime string
EndTime string
// Output format
Format string
// Data source's field to check
// in case is not specified in the uploaded file
Field string
}
/*
* Setup the indicators upload feature
*/
func setupUpload() error {
err := os.MkdirAll(config.Upload.Path+"/queue/", 0755)
if err != nil {
return err
}
err = os.MkdirAll(config.Upload.Path+"/processed/", 0755)
if err != nil {
return err
}
// Process uploaded files again if any process was previously interrupted
go reProcessUploads()
// Delete old/expired files with the processing results
go deleteUploadResults()
return nil
}
/*
* Process '/upload' request to upload a new list of indicators
*/
func uploadHandler(w http.ResponseWriter, r *http.Request) {
// Get client's IP
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
_, e := w.Write([]byte("Can't get IP to upload file: " + err.Error()))
if e != nil {
log.Error().Str("ip", ip).Msg("Can't send error: " + e.Error())
}
log.Error().Msg("Can't get IP to upload file: " + err.Error())
return
}
// Check whether user is signed in
username, err := sessions.exists(w, r)
if err != nil {
_, e := w.Write([]byte("Can't validate user. Check server logs for more info!"))
if e != nil {
log.Error().Str("ip", ip).Msg("Can't send error: " + e.Error())
}
log.Error().
Str("ip", ip).
Msg(err.Error())
return
}
// Get account from the online list
account, ok := online[username]
if !ok {
log.Error().
Str("ip", ip).
Str("username", username).
Msg("Upload attempt from not signed in user")
return
}
// Check whether file is too large
if r.ContentLength > config.Upload.MaxSize {
_, e := w.Write([]byte(fmt.Sprintf("File is too large, max %d Bytes are allowed", config.Upload.MaxSize)))
if e != nil {
log.Error().
Str("ip", ip).
Str("username", username).
Msg("Can't send warning: " + e.Error())
}
log.Error().
Str("ip", ip).
Str("username", username).
Msgf("Upload file is too large, max %d Bytes are allowed", config.Upload.MaxSize)
return
}
file, header, err := r.FormFile("file") // FormFile function takes in the POST input file
if err != nil {
_, e := w.Write([]byte("Can't detect uploaded file: " + err.Error()))
if e != nil {
log.Error().
Str("ip", ip).
Str("username", username).
Msg("Can't send error: " + e.Error())
}
log.Error().
Str("ip", ip).
Str("username", username).
Msg("Can't detect uploaded file: " + err.Error())
return
}
prefix := time.Now().Format("20060102-150405-07")
// Received file's object can't be NULL
if file == nil {
_, e := w.Write([]byte("File was not selected!"))
if e != nil {
log.Error().
Str("ip", ip).
Str("username", username).
Msg("Can't send warning: " + e.Error())
}
log.Error().
Str("ip", ip).
Str("username", username).
Str("filename", header.Filename).
Msg("Upload file was not selected!")
return
}
defer file.Close()
uploaded, err := os.Create(config.Upload.Path + "/queue/" + prefix + "-" + header.Filename)
if err != nil {
_, e := w.Write([]byte("Can't upload file: " + err.Error()))
if e != nil {
log.Error().
Str("ip", ip).
Str("username", username).
Msg("Can't send error: " + e.Error())
}
log.Error().
Str("ip", ip).
Str("username", username).
Str("filename", header.Filename).
Msg("Can't create file for writing: " + err.Error())
return
}
defer uploaded.Close()
// Write the content from POST to the local file
_, err = io.Copy(uploaded, file)
if err != nil {
_, e := w.Write([]byte("Can't upload file: " + err.Error()))
if e != nil {
log.Error().
Str("ip", ip).
Str("username", username).
Msg("Can't send error: " + e.Error())
}
log.Error().
Str("ip", ip).
Str("username", username).
Str("filename", header.Filename).
Msg("Can't store uploaded file: " + err.Error())
return
}
// Get dynamic attributes defined by the user
upload := &Upload{
Source: r.FormValue("source"),
StartTime: r.FormValue("startTime"),
EndTime: r.FormValue("endTime"),
Format: r.FormValue("format"),
Field: r.FormValue("field"),
}
// Add file to the user's queue
account.Uploads.In[prefix+"-"+header.Filename] = upload
err = account.update("uploads.in", account.Uploads.In)
if err != nil {
_, e := w.Write([]byte("Can't add uploaded file to the user's in-queue: " + err.Error()))
if e != nil {
log.Error().
Str("ip", ip).
Str("username", username).
Msg("Can't send error: " + e.Error())
}
log.Error().
Str("ip", ip).
Str("username", username).
Str("filename", header.Filename).
Msg("Can't add uploaded file to the user's in-queue: " + err.Error())
// Clean the environment in case of an error
account.cleanUploads(prefix + "-" + header.Filename)
return
}
// Notify user that upload was successful
_, err = w.Write([]byte("ok"))
if err != nil {
log.Error().
Str("ip", ip).
Str("username", username).
Str("filename", header.Filename).
Msg("Can't inform that upload is successful: " + err.Error())
}
log.Info().
Str("ip", ip).
Str("username", username).
Str("filename", header.Filename).
Msg("File uploaded")
// Notify that the uploaded file is in a queue.
// TODO: Merge "ok" and "upload" responses in just one
account.send("uploaded", prefix+"-"+header.Filename, "")
// Start processing the uploaded file
go func() {
err = account.processUploads(prefix+"-"+header.Filename, upload)
if err != nil {
log.Error().
Str("ip", ip).
Str("username", username).
Str("filename", header.Filename).
Msg(err.Error())
// Notify user about the error
account.addNotification("error", err.Error())
}
// Clean the environment whether the error was nil or not
account.cleanUploads(prefix + "-" + header.Filename)
}()
}
/*
* Start processing the uploaded file
*/
func (a *Account) processUploads(filename string, upload *Upload) error {
// Generated report to send back to the user
report := "Report for the uploaded file: " + filename[19:] + "\n"
// Merged responses from different requests
rRelations := ""
rStats := ""
rDebug := ""
rError := ""
// Validate user input
if err := validUploads("source", upload.Source); err != nil {
rError += "\n - " + "Invalid 'source' value: " + err.Error()
}
if err := validUploads("datetime", upload.StartTime); err != nil {
rError += "\n - " + "Invalid 'start time' value: " + err.Error()
}
if err := validUploads("datetime", upload.EndTime); err != nil {
rError += "\n - " + "Invalid 'end time' value: " + err.Error()
}
if upload.Format != "json" && upload.Format != "table" {
rError += "\n - " + "Invalid 'output format' value: " + upload.Format + ", 'json' or 'table' extected"
}
// Start processing
file, err := os.Open(config.Upload.Path + "/queue/" + filename)
if err != nil {
return fmt.Errorf("Can't open the uploaded file: " + err.Error())
}
field := strings.TrimSpace(upload.Field)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
// Some values shouldn't be quoted
if field != "" {
if valueIsNumeric(line) {
line = field + "=" + line
} else {
line = field + "='" + line + "'"
}
}
// Generate a resulting query
sql := fmt.Sprintf("FROM %s WHERE (%s) AND datetime BETWEEN '%s' AND '%s'",
upload.Source, strings.TrimSpace(line), upload.StartTime, upload.EndTime)
// Query data sources for a new relations data
response := querySources(upload.Source, sql, a.Options.Debug, a.Username)
if len(response.Relations) != 0 {
rRelations += "\n\nIndicator: " + line + "\n\n"
// Format relations data
str, err := formatTo(response.Relations, upload.Format)
if err != nil {
rError += "\n - " + err.Error()
break
} else {
rRelations += str
}
}
if len(response.Stats) != 0 {
rStats += "\n\nIndicator: " + line + "\n\n"
// Format stats
str, err := formatTo(response.Stats, upload.Format)
if err != nil {
rError += "\n - " + err.Error()
break
} else {
rStats += str
}
}
if a.Options.Debug && response.Debug != nil {
rDebug += "\n\nDebug info: " + line + "\n"
for source, section := range response.Debug {
if len(section.(map[string]interface{})) == 0 {
continue
}
// Format debug info
csv, err := formatTo(section, upload.Format)
if err != nil {
rError += "\n - " + err.Error()
break
} else {
rDebug += "\n" + source + "\n" + csv
}
}
}
if response.Error != "" {
// Skip identical errors
if !strings.Contains(rError, response.Error) {
rError += "\n - " + response.Error + ". Query: " + sql
}
}
}
err = scanner.Err()
if err != nil {
return fmt.Errorf("Can't scan the uploaded file: " + err.Error())
}
file.Close()
// Fill report by the relations data
if rRelations != "" {
report += "\n" + rRelations
}
if rStats != "" {
report += "\n\n\nThe amount of relations for some indicators exceeds the limit. Stats info based on limited info:"
report += rStats
}
if rDebug != "" {
report += "\n" + rDebug
}
if rError != "" {
report += "\n\n\nSome errors have occurred during the process:\n"
report += rError
}
// Write results to the file
err = ioutil.WriteFile(config.Upload.Path+"/processed/"+filename, []byte(report), 0600)
if err != nil {
return fmt.Errorf("Can't save processed upload results: " + err.Error())
}
// Notify user that processing is completed
a.send("upload-processed", filename, "")
return nil
}
/*
* Process uploaded files again if any process was interrupted,
* for example service was stopped
*/
func reProcessUploads() {
accounts, err := db.getAccounts()
if err != nil {
log.Error().Msg("Can't get all accounts to restart processing of the uploaded files: " + err.Error())
return
}
for _, account := range accounts {
if len(account.Uploads.In) > 0 {
for filename, params := range account.Uploads.In {
log.Info().
Str("username", account.Username).
Str("filename", filename[19:]).
Msg("Restart processing of the uploaded file")
// Start processing again
err = account.processUploads(filename, params)
if err != nil {
log.Error().
Str("username", account.Username).
Str("filename", filename).
Msg(err.Error())
// Notify user about the error
account.addNotification("error", err.Error())
}
// Clean the environment whether the error was nil or not
account.cleanUploads(filename)
}
}
}
}
/*
* Validate dynamic parameters of the upload
* TODO: call this function only once and return a single combined error
*/
func validUploads(field, value string) error {
// Validate 'source' value
if field == "source" {
if value == "" {
return fmt.Errorf("Can't be empty")
} else if len(value) > 100 {
return fmt.Errorf("Length is too long: %d characters", len(value))
}
}
// Validate 'datetime' value
if field == "datetime" {
if value == "" {
return fmt.Errorf("Can't be empty")
} else if !reDatetime.MatchString(value) {
return fmt.Errorf("Format is incorrect")
}
}
return nil
}
/*
* Clean the environment when the uploaded file is processed
*/
func (a *Account) cleanUploads(filename string) {
err := os.Remove(config.Upload.Path + "/queue/" + filename)
if err != nil {
log.Error().
Str("username", a.Username).
Str("filename", filename[19:]).
Msg("Can't remove the uploaded file: " + err.Error())
} else {
log.Info().
Str("username", a.Username).
Str("filename", filename[19:]).
Msg("Uploads file deleted")
}
// Remove file from the user's in-queue
delete(a.Uploads.In, filename)
err = a.update("uploads.in", a.Uploads.In)
if err != nil {
log.Error().
Str("username", a.Username).
Str("filename", filename).
Msg("Can't remove uploaded file from the in-queue: " + err.Error())
return
}
log.Info().
Str("username", a.Username).
Str("filename", filename).
Msg("Uploaded file removed from the in-queue")
// Add processed file to the downloads list
a.Uploads.Out = append(a.Uploads.Out, filename)
err = a.update("uploads.out", a.Uploads.Out)
if err != nil {
log.Error().
Str("username", a.Username).
Str("filename", filename).
Msg("Can't add processed file to the downloads list: " + err.Error())
// Notify user about the error
a.addNotification("error", "Can't add processed file <span class=\"ui orange text\">"+filename[19:]+"</span> to the downloads list: "+err.Error())
}
log.Info().
Str("username", a.Username).
Str("filename", filename).
Msg("Processed file added to the downloads list")
}
/*
* Process '/download' request to get a file with results
* of the uploaded file processing
*/
func downloadHandler(w http.ResponseWriter, r *http.Request) {
// Get client's IP
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
_, e := w.Write([]byte("Can't get IP to download processed file: " + err.Error()))
if e != nil {
log.Error().Msg("Can't send error: " + e.Error())
}
log.Error().
Str("ip", ip).
Msg("Can't get IP to download processed file: " + err.Error())
return
}
// Check whether user is signed in
username, err := sessions.exists(w, r)
if err != nil {
_, e := w.Write([]byte("Can't validate user. Check server logs for more info!"))
if e != nil {
log.Error().
Str("ip", ip).
Msg("Can't send error: " + e.Error())
}
log.Error().
Str("ip", ip).
Msg(err.Error())
return
}
file := r.FormValue("file")
// Validate file name
if strings.Contains(file, "..") || strings.Contains(file, "/") {
log.Error().
Str("ip", ip).
Str("username", username).
Str("filename", file).
Msg("Unexpected processed file requested to download")
return
}
http.ServeFile(w, r, config.Upload.Path+"/processed/"+file)
log.Info().
Str("ip", ip).
Str("username", username).
Str("filename", file).
Msg("Processed file downloaded")
}
/*
* Delete old files with results of the uploaded files processing
*/
func deleteUploadResults() {
for now := range time.Tick(time.Duration(config.Upload.DeleteInterval) * time.Second) {
log.Debug().Msg("Cleaning users downloads list")
accounts, err := db.getAccounts()
if err != nil {
log.Error().Msg("Can't get all accounts to delete the results of the uploaded files: " + err.Error())
continue
}
// Check all the accounts first
for _, account := range accounts {
if len(account.Uploads.Out) > 0 {
// Do not use 'range' to be able to change 'i'
for i := 0; i < len(account.Uploads.Out); i++ {
filename := account.Uploads.Out[i]
dt, err := time.Parse("20060102-150405-07", filename[:18])
if err != nil {
log.Error().Msg("Can't parse datetime to delete the results of the uploaded file: " + err.Error())
continue
}
if dt.Add(time.Duration(config.Upload.DeleteExpiration) * time.Second).Before(now) {
err := os.Remove(config.Upload.Path + "/processed/" + filename)
if err != nil {
log.Error().
Str("username", account.Username).
Str("filename", filename).
Msg("Can't delete the results of the uploaded file: " + err.Error())
}
// Remove file from the user's downloads list
account.Uploads.Out[i] = account.Uploads.Out[len(account.Uploads.Out)-1]
account.Uploads.Out = account.Uploads.Out[:len(account.Uploads.Out)-1]
err = account.update("uploads.out", account.Uploads.Out)
if err != nil {
log.Error().
Str("username", account.Username).
Str("filename", filename).
Msg("Can't remove file from the downloads list: " + err.Error())
break
}
// Decrease counter to check the new file name again
// after cleaning the slice
i--
log.Info().
Str("username", account.Username).
Str("filename", filename).
Msg("Results of the uploaded file deleted")
}
}
}
}
// Check the file system just in case some old files are stuck
files, _ := ioutil.ReadDir(config.Upload.Path + "/processed/")
for _, file := range files {
if !file.IsDir() {
filename := file.Name()
dt, err := time.Parse("20060102-150405-07", filename[:18])
if err != nil {
log.Error().Msg("Can't parse datetime to delete unassigned results of the uploaded file: " + err.Error())
continue
}
if dt.Add(time.Duration(config.Upload.DeleteExpiration) * time.Second).Before(now) {
err := os.Remove(config.Upload.Path + "/processed/" + filename)
if err != nil {
log.Error().
Str("filename", filename).
Msg("Can't delete unassigned results of the uploaded file: " + err.Error())
}
log.Info().
Str("filename", filename).
Msg("Unassigned results of the uploaded file deleted")
}
}
}
}
}
/*
* Send user's upload queue and download lists
* to be displayed as Web GUI items
*/
func (a *Account) getUploadLists() {
// Use a list instead of a concatenated string
// to avoid possible splitting errors on a JavaScript side
listIn := []string{}
for k := range a.Uploads.In {
listIn = append(listIn, k)
}
bIn, err := json.Marshal(listIn)
if err != nil {
log.Error().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Can't marshal upload queue list: " + err.Error())
a.send("error", "Check server logs for more info!", "Can't get upload queue and download lists.")
return
}
bOut, err := json.Marshal(a.Uploads.Out)
if err != nil {
log.Error().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Can't marshal download list: " + err.Error())
a.send("error", "Check server logs for more info!", "Can't get upload queue and download lists.")
return
}
a.send("upload-lists", string(bIn), string(bOut))
}
/*
* Check whether string value is numeric
*/
func valueIsNumeric(s string) bool {
_, err := strconv.ParseFloat(s, 64)
return err == nil
}