-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathevents_handler.go
More file actions
59 lines (55 loc) · 1.42 KB
/
events_handler.go
File metadata and controls
59 lines (55 loc) · 1.42 KB
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
// Copyright 2015 CodeIgnition. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package main
import (
"encoding/json"
"net/http"
"time"
"github.com/codeignition/recon/policy"
"gopkg.in/mgo.v2/bson"
)
func eventsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.Method {
case "GET":
uid := r.FormValue("uid")
if uid == "" {
http.Error(w, "uid query string required", http.StatusInternalServerError)
return
}
policyName := r.FormValue("policy_name")
if policyName == "" {
http.Error(w, "policy_name query string required", http.StatusInternalServerError)
return
}
t := r.FormValue("t")
if t == "" {
t = "5m"
}
d, err := time.ParseDuration(t)
if err != nil {
http.Error(w, "malformed time duration", http.StatusInternalServerError)
return
}
var events []policy.Event
err = eventsC.Find(bson.M{
"agent_uid": uid,
"policy_name": policyName,
"time": bson.M{"$gt": time.Now().Add(-d)},
}).All(&events)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
enc := json.NewEncoder(w)
if err := enc.Encode(events); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
default:
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
}