-
Notifications
You must be signed in to change notification settings - Fork 2
/
policy_handler.go
119 lines (114 loc) · 3.48 KB
/
policy_handler.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
// 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"
"gopkg.in/mgo.v2/bson"
"github.com/codeignition/recon/policy"
)
func policyHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.Method {
case "POST":
var p policy.Policy
defer r.Body.Close()
dec := json.NewDecoder(r.Body)
if err := dec.Decode(&p); err != nil {
http.Error(w, "unable to decode json", http.StatusBadRequest)
return
}
if p.AgentUID == "" {
http.Error(w, "UID can't be empty", http.StatusBadRequest)
return
}
var a Agent
err := agentsC.Find(bson.M{"uid": p.AgentUID}).One(&a)
if err != nil {
http.Error(w, "recond agent unknown: check the agent UID and try again", http.StatusInternalServerError)
return
}
if a.Status() == "offline" {
http.Error(w, "recond agent offline: restart the agent and try again", http.StatusInternalServerError)
return
}
var reply string
if err := natsEncConn.Request(p.AgentUID+"_add_policy", &p, &reply, 5*time.Second); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if reply != "add_policy_ack" {
http.Error(w, reply, http.StatusInternalServerError)
return
}
// TODO: if the policy is successfully sent to the agent, store the agent's PolicyConfig in the master server's DB as well.
return
case "PATCH":
var p policy.Policy
defer r.Body.Close()
dec := json.NewDecoder(r.Body)
if err := dec.Decode(&p); err != nil {
http.Error(w, "unable to decode json", http.StatusBadRequest)
return
}
if p.AgentUID == "" {
http.Error(w, "UID can't be empty", http.StatusBadRequest)
return
}
var a Agent
err := agentsC.Find(bson.M{"uid": p.AgentUID}).One(&a)
if err != nil {
http.Error(w, "recond agent unknown: check the agent UID and try again", http.StatusInternalServerError)
return
}
if a.Status() == "offline" {
http.Error(w, "recond agent offline: restart the agent and try again", http.StatusInternalServerError)
return
}
var reply string
if err := natsEncConn.Request(p.AgentUID+"_modify_policy", &p, &reply, 5*time.Second); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if reply != "modify_policy_ack" {
http.Error(w, reply, http.StatusInternalServerError)
return
}
case "DELETE":
var p policy.Policy
defer r.Body.Close()
dec := json.NewDecoder(r.Body)
if err := dec.Decode(&p); err != nil {
http.Error(w, "unable to decode json", http.StatusBadRequest)
return
}
if p.AgentUID == "" {
http.Error(w, "UID can't be empty", http.StatusBadRequest)
return
}
var a Agent
err := agentsC.Find(bson.M{"uid": p.AgentUID}).One(&a)
if err != nil {
http.Error(w, "recond agent unknown: check the agent UID and try again", http.StatusInternalServerError)
return
}
if a.Status() == "offline" {
http.Error(w, "recond agent offline: restart the agent and try again", http.StatusInternalServerError)
return
}
var reply string
if err := natsEncConn.Request(p.AgentUID+"_delete_policy", &p, &reply, 5*time.Second); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if reply != "delete_policy_ack" {
http.Error(w, reply, http.StatusInternalServerError)
return
}
default:
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
}