-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_services.go
517 lines (463 loc) · 13.6 KB
/
http_services.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
package main
import (
"database/sql"
"fmt"
"html/template"
"log"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"unfoldedip/satsql"
"unfoldedip/sattypes"
)
// services prints all configured services from a customer
func services(writer http.ResponseWriter, request *http.Request, H sattypes.BaseHandler) {
var g sattypes.Global
var err error
if g.U, g.U.LoggedIn = isLoggedIn(request, H); !g.U.LoggedIn {
http.Redirect(writer, request, "/login?session=expired1", http.StatusSeeOther)
return
}
// read only user service
Services, err := satsql.ReadServices(H, g.U.UserID, "", false)
if err != nil {
log.Println(err)
} else {
g.Services = Services
}
// Default is GET method where we will print out the template
executeGlobalAgainstTemplate(writer, "services.html", g)
}
// servicesLog prints out all log messages for all services
func servicesLogs(writer http.ResponseWriter, request *http.Request, H sattypes.BaseHandler) {
var g sattypes.Global
var serviceLogs []sattypes.ServiceLog
var err error
// check if user is loggedin
if g.U, g.U.LoggedIn = isLoggedIn(request, H); !g.U.LoggedIn {
http.Redirect(writer, request, "/login?session=expired1", http.StatusSeeOther)
return
}
// read service logs service by service_id
serviceLogs, err = satsql.ReadServicesLog(H, g.U.UserID)
if err != nil {
log.Println(err)
} else {
g.ServiceLogs = serviceLogs
}
// Default is GET method where we will print out the template
executeGlobalAgainstTemplate(writer, "services_logs.html", g)
}
// serviceLogs prints out all log messages for a service
func serviceLogs(writer http.ResponseWriter, request *http.Request, H sattypes.BaseHandler) {
var g sattypes.Global
var serviceLogs []sattypes.ServiceLog
var service sattypes.Service
var serviceID string
var err error
// check if user is loggedin
if g.U, g.U.LoggedIn = isLoggedIn(request, H); !g.U.LoggedIn {
http.Redirect(writer, request, "/login?session=expired1", http.StatusSeeOther)
return
}
/* read params from form */
err = request.ParseForm()
if err != nil {
log.Println(err)
goto DefaultAndExit
}
// read serviceID
serviceID = request.FormValue("id")
if serviceID == "" {
if H.Debug {
log.Println("No id for service logs")
}
goto DefaultAndExit
}
// retrieve service by service_id
service, err = satsql.SelectService(H, "service_id", serviceID, g.U.UserID)
if service.ServiceID == 0 {
if H.Debug {
log.Println("Not allowing access for service logs or service not existing")
}
goto DefaultAndExit
}
// read service logs service by service_id
serviceLogs, err = satsql.ReadServiceLogs(H, serviceID)
if err != nil {
log.Println(err)
} else {
g.ServiceLogs = serviceLogs
g.Service = service
}
DefaultAndExit:
// Default is GET method where we will print out the template
executeGlobalAgainstTemplate(writer, "service_logs.html", g)
}
// handle adding a service
func serviceAdd(writer http.ResponseWriter, request *http.Request, H sattypes.BaseHandler) {
// local variables
var g sattypes.Global
var err error
// for create, edit and checking access
var newService, editService, garbageService sattypes.Service
// for hardcoded
var AllowedIntervals = []int{5, 15, 30, 60, 90, 120}
// retrieve session
if g.U, g.U.LoggedIn = isLoggedIn(request, H); !g.U.LoggedIn {
http.Redirect(writer, request, "/login?session=expired4", http.StatusSeeOther)
return
}
// load valid contact groups for the user
Contacts, err := satsql.ReadAlertGroups(H, g.U.UserID)
if err != nil {
log.Println(err)
} else {
g.AlertGroups = Contacts
}
// things we expect to read from our form
expectedVars := []string{
"interval",
"hostname",
"contactgroup",
"checktype",
"url",
"expected",
"hosttcp",
"servicename",
"locations",
}
// handle POST
if request.Method == http.MethodPost {
// Parse form arguments
err = request.ParseForm()
if err != nil {
log.Println(err)
return
}
// check if csrf token is valid
if !CheckCSRFToken(writer, request, H, g.U.UserSession) {
return
}
// if we are in edit mode, we need carefully check, if the user
// is allowed to access the service id
if request.Form.Get("nextfunction") == "edit" {
// read serviceID
serviceID := request.FormValue("id")
if serviceID == "" {
if H.Debug {
log.Println("No id for service logs")
}
goto DefaultAndExit
}
// retrieve service by service_id
garbageService, err = satsql.SelectService(H, "service_id", serviceID, g.U.UserID)
if garbageService.ServiceID == 0 {
if H.Debug {
log.Println("Not allowing access for service edit or service not existing")
}
goto DefaultAndExit
}
// access ok, copy over the id, release memory
newService.ServiceID = garbageService.ServiceID
}
// check for all mandatory fields and collect errors if missing or invalid
for _, x := range expectedVars {
formValue := template.HTMLEscapeString(request.Form.Get(x))
if formValue == "" {
continue
}
switch x {
case "locations":
newService.Locations = func(selectedLocs []string) string {
var Locations string
for i := range selectedLocs {
Locations += strings.ReplaceAll(selectedLocs[i], " ", "") + " "
}
return Locations
}(request.Form["locations"])
case "checktype":
newService.Type = func(arg string) string {
if arg == "ping" || arg == "http" || arg == "tcp" || arg == "tls" {
return arg
}
g.Errors = append(g.Errors, "Unknown type of check, please correct")
return ""
}(formValue)
case "hostname":
newService.ToCheck = func(arg string) string {
addr := net.ParseIP(arg)
if addr != nil {
// ip or ipv6 incoming
}
return arg
}(formValue)
case "servicename":
newService.Name = func(arg string) string {
if arg == "" {
g.Errors = append(g.Errors, "Please enter a service name for identification")
}
return arg
}(formValue)
case "hosttcp":
newService.ToCheck = func(arg string) string {
return arg
}(formValue)
case "interval":
newService.Interval = func(arg string) int {
val, err := strconv.Atoi(arg)
if err == nil {
for i := range AllowedIntervals {
if AllowedIntervals[i] == val {
return val
}
}
}
return 90
}(formValue)
case "contactgroup":
newService.ContactGroup = func(arg string) int {
val, err := strconv.Atoi(arg)
if err != nil {
g.Errors = append(g.Errors, "Wrong or not alert group")
log.Println(err)
} else {
ag, err := satsql.SelectAlertGroup(H, "contact_id", arg)
if err == nil {
if ag.OwnerID != g.U.UserID {
g.Errors = append(g.Errors, "Wrong contactgroup")
log.Println(err)
return 0
}
return val
} else if err != sql.ErrNoRows {
g.Errors = append(g.Errors, "Wrong contactgroup")
log.Println(err)
return 0
}
return val
}
return val
}(formValue)
case "url":
newService.ToCheck = func(arg string) string {
if strings.HasSuffix(arg, "https://") || strings.HasSuffix(arg, "http://") {
// empty url
return ""
}
_, err := url.ParseRequestURI(arg)
if err != nil {
return ""
}
return arg
}(formValue)
case "expected":
newService.Expected = func(arg string) string {
return arg
}(formValue)
}
}
// make some combination check, for example if checktype is ping, we
// need a hostname or an ip address, if checktype is http we need
// httpurl value set
if newService.Type == "ping" && newService.ToCheck == "" {
g.Errors = append(g.Errors, "No hostname or IP/IPv6 address given")
} else if newService.Type == "http" && newService.ToCheck == "" {
g.Errors = append(g.Errors, "No/Invalid URL given"+newService.Type+newService.ToCheck)
} else if (newService.Type == "tcp" || newService.Type == "tls") && newService.ToCheck == "" {
g.Errors = append(g.Errors, "No host and / or tcp port given")
} else if newService.Type == "" || newService.ToCheck == "" {
g.Errors = append(g.Errors, "No type or check given")
}
// add userid to service for db insert
newService.OwnerID = g.U.UserID
// add location if necessary
if newService.Locations == "" {
newService.Locations = "any"
}
// use the size from errorCollection as error indicator
if len(g.Errors) == 0 {
// check if we are in edit or post
if request.Form.Get("nextfunction") == "edit" {
if H.Debug {
log.Println("Updating service", newService.ServiceID)
}
err = satsql.UpdateService(H, &newService)
} else {
err = satsql.InsertService(H, &newService)
}
if err != nil {
log.Println("SQL error", err)
} else {
http.Redirect(writer, request, fmt.Sprintf("/services"), http.StatusSeeOther)
return
}
}
}
// check if we are in edit mode
if request.Method == http.MethodGet {
if strings.Contains(request.URL.Path, "service_edit") {
// this becomes now tricky, we will try to parse the id
serviceID := request.FormValue("id")
if serviceID == "" {
log.Println("No id for service edit")
goto DefaultAndExit
}
editService, err = satsql.SelectService(H, "service_id", serviceID, g.U.UserID)
// service exists and owner is current user, then proceed
if err == nil && editService.OwnerID == g.U.UserID {
// template the selected service into the global var
g.Service = editService
// tell the template function, that we want to edit
// so it renders the right information
g.NextFunction = "edit"
}
// else do nothing...
}
}
DefaultAndExit:
// Pass some defaults down the template
g.AllowedIntervals = AllowedIntervals
if g.Service.ServiceID == 0 {
g.Service.Interval = 90
}
// load list of locations
g.SatAgentLocations, err = satsql.ReadAgentLocations(H)
if err != nil {
log.Println(err)
}
// Default is GET method where we will print out the template
executeGlobalAgainstTemplate(writer, "service_add.html", g)
}
// handle delete service (will be called by ajax query)
// redirect then (or not?)
func serviceDelete(writer http.ResponseWriter, request *http.Request, H sattypes.BaseHandler) {
// local variables
var g sattypes.Global
var delService sattypes.Service
var serviceID string
var err error
// retrieve session
if g.U, g.U.LoggedIn = isLoggedIn(request, H); !g.U.LoggedIn {
http.Redirect(writer, request, "/login?session=expired6", http.StatusSeeOther)
return
}
// Not POST method? Then jump to clean up
if request.Method != http.MethodPost {
goto DefaultAndExit
}
/* read params from form */
err = request.ParseForm()
if err != nil {
log.Println(err)
goto DefaultAndExit
}
// check if csrf token is valid
if !CheckCSRFToken(writer, request, H, g.U.UserSession) {
return
}
// read serviceID
serviceID = request.FormValue("id")
if serviceID == "" {
log.Println("No id for service deletion")
goto DefaultAndExit
}
// retrieve service by service_id
delService, err = satsql.SelectService(H, "service_id", serviceID, g.U.UserID)
// service exists and owner is current user, then delete
if err == nil && delService.OwnerID == g.U.UserID {
if H.Debug {
log.Println("Deleting service", delService.ServiceID)
}
err = satsql.DeleteService(H, delService.ServiceID)
if err != nil {
log.Println(err)
goto DefaultAndExit
}
if H.Debug {
log.Println("Deleting service logs", delService.ServiceID)
}
err = satsql.DeleteServiceLogs(H, delService.ServiceID)
if err != nil {
log.Println(err)
goto DefaultAndExit
}
writer.WriteHeader(http.StatusOK)
return
}
/* return no content */
DefaultAndExit:
writer.WriteHeader(http.StatusNoContent)
return
}
// handle reset service (will be called by ajax query)
// resets a service to "UNKNOWN"
func serviceReset(writer http.ResponseWriter, request *http.Request, H sattypes.BaseHandler) {
// local variables
var g sattypes.Global
var resetService sattypes.Service
var serviceID string
var err error
// retrieve session
if g.U, g.U.LoggedIn = isLoggedIn(request, H); !g.U.LoggedIn {
http.Redirect(writer, request, "/login?session=expired6", http.StatusSeeOther)
return
}
// Not POST method? Then jump to clean up
if request.Method != http.MethodPost {
goto DefaultAndExit
}
/* read params from form */
err = request.ParseForm()
if err != nil {
log.Println(err)
goto DefaultAndExit
}
// check if csrf token is valid
if !CheckCSRFToken(writer, request, H, g.U.UserSession) {
return
}
// read serviceID
serviceID = request.FormValue("id")
if serviceID == "" {
log.Println("No id for service reset")
goto DefaultAndExit
}
// retrieve service by service_id
resetService, err = satsql.SelectService(H, "service_id", serviceID, g.U.UserID)
// service exists and owner is current user, then reset
if err == nil && resetService.OwnerID == g.U.UserID {
if H.Debug {
log.Println("Resetting service", resetService.ServiceID)
}
err = satsql.ResetService(H, resetService.ServiceID)
if err != nil {
log.Println(err)
goto DefaultAndExit
}
// convert serviceID to int64
sID, err := strconv.ParseInt(serviceID, 10, 64)
if err == nil {
log.Println(err)
}
// pushing a message down the channel?
// attention, fixme, this could be a deadlock if the channel is FULl?
sattypes.ResultsChannel <- sattypes.ServiceResult{
ServiceID: sID,
Status: sattypes.ServiceUnknown,
Message: "The service state has been reset",
Time: time.Now(),
TestNode: "User",
RapidChange: true,
}
writer.WriteHeader(http.StatusOK)
return
}
/* return no content */
DefaultAndExit:
writer.WriteHeader(http.StatusNoContent)
return
}