forked from cert-lv/graphoscope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.go
539 lines (448 loc) · 13.5 KB
/
admin.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
package main
import (
"net"
"net/http"
"regexp"
"strconv"
"strings"
)
var (
// Regex to validate Hex color definition
reColor = regexp.MustCompile(`^#(?:[0-9a-fA-F]{3}){1,2}$`)
)
/*
* Configurable graph UI settings.
* Accessible to admins only at Web GUI settings -> "Administration".
*
* More details at:
* https://visjs.github.io/vis-network/docs/network/nodes.html
* https://visjs.github.io/vis-network/docs/network/edges.html
* https://visjs.github.io/vis-network/docs/network/interaction.html
*/
type GraphSettings struct {
ID string `bson:"_id" json:"-"`
NodeSize int `bson:"nodeSize"`
BorderWidth int `bson:"borderWidth"`
BGcolor string `bson:"bgColor"`
BorderColor string `bson:"borderColor"`
NodeFontSize int `bson:"nodeFontSize"`
EdgeWidth int `bson:"edgeWidth"`
EdgeColor string `bson:"edgeColor"`
EdgeFontSize int `bson:"edgeFontSize"`
EdgeFontColor string `bson:"edgeFontColor"`
Shadow bool `bson:"shadow"`
Arrow bool `bson:"arrow"`
Smooth bool `bson:"smooth"`
Hover bool `bson:"hover"`
MultiSelect bool `bson:"multiSelect"`
HideEdgesOnDrag bool `bson:"hideEdgesOnDrag"`
}
/*
* Serve '/admin' page
*/
func adminHandler(w http.ResponseWriter, r *http.Request) {
// Get client's IP
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
log.Error().Msg("Can't get IP for admin page: " + err.Error())
return
}
// Check existing session
username, err := sessions.exists(w, r)
if err != nil {
log.Error().
Str("ip", ip).
Msg(err.Error())
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
}
// Get account from DB
account, err := db.getAccount(username)
if err != nil {
log.Error().
Str("ip", ip).
Str("username", username).
Msg("Can't GetAccount for admin page: " + err.Error())
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
}
// All are admins in service's DEV mode
if config.Environment != "prod" {
account.Admin = true
}
// Drop non-admin users in prod. mode
if !account.Admin && config.Environment == "prod" {
log.Info().
Str("ip", ip).
Str("username", username).
Msg("Not admin requested admin page")
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
// Error message to display in case it exists
msg := ""
settings, err := db.getGraphSettings()
if err != nil {
log.Error().
Str("ip", ip).
Str("username", username).
Msg("Can't get graph UI settings: " + err.Error())
msg = err.Error()
}
accounts, err := db.getAccounts()
if err != nil {
log.Error().
Str("ip", ip).
Str("username", username).
Msg("Can't get accounts: " + err.Error())
msg = err.Error()
}
templateData := &TemplateData{
Account: account,
Accounts: accounts,
GraphSettings: settings,
Error: msg,
}
// Render HTML and send it to the client
renderTemplate(w, "admin", templateData, nil)
log.Info().
Str("ip", ip).
Str("username", username).
Msg("Admin page requested")
}
/*
* Handle 'settings' websocket command to save global graph settings
*/
func (a *Account) settingsHandler(settings string) {
// Drop non-admin users request
if !a.Admin && config.Environment == "prod" {
log.Info().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Not enough rights to set UI settings")
a.send("error", "Not enough rights to set UI settings.", "Can't save!")
return
}
parts := strings.Split(settings, ",")
// Get and validate all received settings
nodesize, err := strconv.Atoi(parts[0])
if err != nil {
log.Error().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Can't parse node size value: " + err.Error())
a.send("error", "Invalid <strong>node size</strong> value given, integer expected.", "Can't save!")
return
}
borderwidth, err := strconv.Atoi(parts[1])
if err != nil {
log.Error().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Can't parse node border width value: " + err.Error())
a.send("error", "Invalid <strong>node border width</strong> value given, integer expected.", "Can't save!")
return
}
bgcolor := parts[2]
if !reColor.MatchString(bgcolor) {
log.Error().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Can't parse node background color value: " + bgcolor)
a.send("error", "Invalid <strong>node background color</strong> value given, <strong>#000</strong> or <strong>#000000</strong> format expected.", "Can't save!")
return
}
bordercolor := parts[3]
if !reColor.MatchString(bordercolor) {
log.Error().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Can't parse node border color value: " + bordercolor)
a.send("error", "Invalid <strong>node border color</strong> value given, <strong>#000</strong> or <strong>#000000</strong> format expected.", "Can't save!")
return
}
nodefontsize, err := strconv.Atoi(parts[4])
if err != nil {
log.Error().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Can't parse node font size value: " + err.Error())
a.send("error", "Invalid <strong>node font size</strong> value given, integer expected.", "Can't save!")
return
}
shadow, err := strconv.ParseBool(parts[5])
if err != nil {
log.Error().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Can't parse shadow value: " + err.Error())
a.send("error", "Invalid <strong>node shadow</strong> value given, boolean expected.", "Can't save!")
return
}
edgewidth, err := strconv.Atoi(parts[6])
if err != nil {
log.Error().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Can't parse edge width value: " + err.Error())
a.send("error", "Invalid <strong>edge width</strong> value given, integer expected.", "Can't save!")
return
}
edgecolor := parts[7]
if !reColor.MatchString(edgecolor) {
log.Error().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Can't parse edge color value: " + edgecolor)
a.send("error", "Invalid <strong>edge color</strong> value given, <strong>#000</strong> or <strong>#000000</strong> format expected.", "Can't save!")
return
}
edgefontsize, err := strconv.Atoi(parts[8])
if err != nil {
log.Error().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Can't parse edge font size value: " + err.Error())
a.send("error", "Invalid <strong>edge font size</strong> value given, integer expected.", "Can't save!")
return
}
edgefontcolor := parts[9]
if !reColor.MatchString(edgefontcolor) {
log.Error().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Can't parse edge font color value: " + edgefontcolor)
a.send("error", "Invalid <strong>edge font color</strong> value given, <strong>#000</strong> or <strong>#000000</strong> format expected.", "Can't save!")
return
}
arrow, err := strconv.ParseBool(parts[10])
if err != nil {
log.Error().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Can't parse edge arrow value: " + err.Error())
a.send("error", "Invalid <strong>edge arrow</strong> value given, boolean expected.", "Can't save!")
return
}
smooth, err := strconv.ParseBool(parts[11])
if err != nil {
log.Error().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Can't parse edge smooth value: " + err.Error())
a.send("error", "Invalid <strong>edge smooth</strong> value given, boolean expected.", "Can't save!")
return
}
hover, err := strconv.ParseBool(parts[12])
if err != nil {
log.Error().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Can't parse hover value: " + err.Error())
a.send("error", "Invalid <strong>hover</strong> value given, boolean expected.", "Can't save!")
return
}
multiselect, err := strconv.ParseBool(parts[13])
if err != nil {
log.Error().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Can't parse multiselect value: " + err.Error())
a.send("error", "Invalid <strong>multiselect</strong> value given, boolean expected.", "Can't save!")
return
}
hideedgesondrag, err := strconv.ParseBool(parts[14])
if err != nil {
log.Error().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Can't parse hideedgesondrag value: " + err.Error())
a.send("error", "Invalid <strong>hide edges on drag</strong> value given, boolean expected.", "Can't save!")
return
}
// Update graph settings
opt := &GraphSettings{
ID: "graph",
NodeSize: nodesize,
BorderWidth: borderwidth,
BGcolor: bgcolor,
BorderColor: bordercolor,
NodeFontSize: nodefontsize,
Shadow: shadow,
EdgeWidth: edgewidth,
EdgeColor: edgecolor,
EdgeFontSize: edgefontsize,
EdgeFontColor: edgefontcolor,
Arrow: arrow,
Smooth: smooth,
Hover: hover,
MultiSelect: multiselect,
HideEdgesOnDrag: hideedgesondrag,
}
err = db.setGraphSettings(opt)
if err != nil {
log.Error().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Can't update graph UI settings: " + err.Error())
a.send("error", "Can't update graph UI settings: "+err.Error(), "Can't save!")
return
}
a.send("ok", "", "")
log.Info().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Graph UI settings updated")
}
/*
* Handle 'users' websocket command to manage users.
*
* Acceptable commands:
* reset-password: drop user's password, so he can renew it
* delete: delete specific user
* admin-true: give admin rights
* admin-false: remove admin rights
*/
func (a *Account) usersHandler(data string) {
// Drop non-admin users request
if !a.Admin && config.Environment == "prod" {
log.Info().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Not enough rights to set users options")
a.send("error", "Not enough rights to set users options.", "Error!")
return
}
parts := strings.Split(data, ",")
// Get action & user to modify
user := parts[0]
action := parts[1]
// Get an account to modify from a database
userAccount, err := db.getAccount(user)
if err != nil {
log.Error().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Can't GetAccount to get user to modify: " + err.Error())
a.send("error", "Can't validate requested user: "+err.Error(), "Error!")
return
}
switch action {
case "reset-password":
err = userAccount.update("password", "")
if err != nil {
log.Error().
Str("ip", a.Session.IP).
Str("username", a.Username).
Str("user-to-reset", user).
Msg("Can't reset password: " + err.Error())
a.send("error", "Can't reset password: "+err.Error(), "Error!")
return
}
a.send("account-reset", "", "")
log.Info().
Str("ip", a.Session.IP).
Str("username", a.Username).
Str("user-to-mod", user).
Msg("Password was reset")
case "delete":
err = db.deleteAccount(user)
if err != nil {
log.Error().
Str("ip", a.Session.IP).
Str("username", a.Username).
Str("user-to-delete", user).
Msg("Can't deleteAccount: " + err.Error())
a.send("error", "Can't delete account: "+err.Error(), "Error!")
return
}
// Delete session if exists
err = db.deleteSession(user, nil, nil)
if err != nil && err != errSessionNotExists {
log.Error().
Str("username", a.Username).
Str("user-to-delete", user).
Msg("Can't delete user session: " + err.Error())
a.send("error", "Can't delete user session: "+err.Error(), "Error!")
return
}
a.send("account-deleted", user, "")
log.Info().
Str("ip", a.Session.IP).
Str("username", a.Username).
Str("user-to-mod", user).
Msg("User deleted")
case "admin-true":
err = userAccount.update("admin", true)
if err != nil {
log.Error().
Str("ip", a.Session.IP).
Str("username", a.Username).
Str("user-to-make-admin", user).
Msg("Can't set admin rights: " + err.Error())
a.send("error", "Can't set admin rights: "+err.Error(), "Error!")
return
}
log.Info().
Str("ip", a.Session.IP).
Str("username", a.Username).
Str("user-to-mod", user).
Msg("User got admin rights")
case "admin-false":
err = userAccount.update("admin", false)
if err != nil {
log.Error().
Str("ip", a.Session.IP).
Str("username", a.Username).
Str("user-to-remove-admin", user).
Msg("Can't unset admin rights: " + err.Error())
a.send("error", "Can't unset admin rights: "+err.Error(), "Error!")
return
}
log.Info().
Str("ip", a.Session.IP).
Str("username", a.Username).
Str("user-to-mod", user).
Msg("User lost admin rights")
default:
log.Error().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Unknown user mod. action requested: " + action)
a.send("error", "Unknown user mod. action requested: "+action, "Error!")
return
}
}
/*
* Reload collectors and processors.
*
* This allows:
* - To reload definition files
* - To recreate dropped connections
* - To refresh the list of fields to query for the Web GUI autocomplete
*/
func (a *Account) reloadHandler() {
err := setupCollectors()
if err != nil {
a.send("error", "Can't reload collectors: "+err.Error(), "Error!")
log.Info().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Can't reload collectors: " + err.Error())
return
}
err = setupProcessors()
if err != nil {
a.send("error", "Can't reload processors: "+err.Error(), "Error!")
log.Info().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Can't reload processors: " + err.Error())
return
}
a.send("ok", "", "")
log.Info().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Collectors and processors reloaded")
}