forked from neuvector/neuvector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conver.go
329 lines (265 loc) · 8.93 KB
/
conver.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
package rest
import (
"encoding/json"
"io/ioutil"
"net/http"
"strings"
"github.com/julienschmidt/httprouter"
log "github.com/sirupsen/logrus"
"github.com/neuvector/neuvector/controller/access"
"github.com/neuvector/neuvector/controller/api"
"github.com/neuvector/neuvector/controller/rpc"
"github.com/neuvector/neuvector/share"
)
type mcastGraphRPC = func(string, uint16, *share.CLUSGraphOps) error
func handlerConverEndpointList(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
log.WithFields(log.Fields{"URL": r.URL.String()}).Debug("")
defer r.Body.Close()
acc, login := getAccessControl(w, r, "")
if acc == nil {
return
}
query := restParseQuery(r)
var view string
if value, ok := query.pairs[api.QueryKeyView]; ok && value == api.QueryValueViewPod {
view = api.QueryValueViewPod
}
var resp api.RESTConversationEndpointData
resp.Endpoints = make([]*api.RESTConversationEndpoint, 0)
// used for graph now, no sorting or filtering
eps := cacher.GetAllConverEndpoints(view, acc)
// Ignore sorting. Filter
if len(eps) <= query.start {
restRespSuccess(w, r, &resp, acc, login, nil, "Get endpoint list")
return
}
if len(query.filters) > 0 {
var dummy api.RESTConversationEndpoint
rf := restNewFilter(&dummy, query.filters)
for _, ep := range eps[query.start:] {
if !rf.Filter(ep) {
continue
}
resp.Endpoints = append(resp.Endpoints, ep)
if query.limit > 0 && len(resp.Endpoints) >= query.limit {
break
}
}
} else if query.limit == 0 {
resp.Endpoints = eps[query.start:]
} else {
var end int
if query.start+query.limit > len(eps) {
end = len(eps)
} else {
end = query.start + query.limit
}
resp.Endpoints = eps[query.start:end]
}
log.WithFields(log.Fields{"entries": len(resp.Endpoints)}).Debug("Response")
restRespSuccess(w, r, &resp, acc, login, nil, "Get conversation endpoint list")
}
func handlerConverEndpointDelete(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
log.WithFields(log.Fields{"URL": r.URL.String()}).Debug("")
defer r.Body.Close()
acc, login := getAccessControl(w, r, "")
if acc == nil {
return
}
// This endpoint delete function is hidden for now.
// Because deleting one non-workload endpoint will delete converstaion
// from all other namespace to it as well, we will only allow superuser
// to do this operation
if !acc.CanWriteCluster() {
restRespAccessDenied(w, login)
return
}
id := ps.ByName("id")
if !strings.HasPrefix(id, api.LearnedHostPrefix) &&
!strings.HasPrefix(id, api.LearnedWorkloadPrefix) {
restRespErrorMessage(w, http.StatusBadRequest, api.RESTErrInvalidRequest,
"Only host and unmanaged container can be deleted!")
return
}
ops := &share.CLUSGraphOps{Endpoint: id}
mcastAllController(rpc.DeleteEndpoint, ops)
restRespSuccess(w, r, nil, acc, login, nil, "Delete conversation endpoint")
}
func handlerConverEndpointConfig(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
log.WithFields(log.Fields{"URL": r.URL.String()}).Debug("")
defer r.Body.Close()
acc, login := getAccessControl(w, r, "")
if acc == nil {
return
}
id := ps.ByName("id")
// For now, alias is added to graph's endpoint. Without traffic, container's endpoint is not
// in the graph, so it cannot be renamed. Plus, there seems no reason to give a container an alias.
if !strings.HasPrefix(id, api.LearnedHostPrefix) &&
!strings.HasPrefix(id, api.LearnedWorkloadPrefix) {
restRespErrorMessage(w, http.StatusBadRequest, api.RESTErrInvalidRequest,
"Only host and unmanaged container can be modified!")
return
}
ep, err := cacher.GetConverEndpoint(id, acc)
if ep == nil {
restRespNotFoundLogAccessDenied(w, login, err)
return
}
// Read body
body, _ := ioutil.ReadAll(r.Body)
var rconf api.RESTConversationEndpointConfigData
err = json.Unmarshal(body, &rconf)
if err != nil || rconf.Config == nil {
log.WithFields(log.Fields{"error": err}).Error("Request error")
restRespError(w, http.StatusBadRequest, api.RESTErrInvalidRequest)
return
}
rc := rconf.Config
if rc.DisplayName != nil {
ops := &share.CLUSGraphOps{Endpoint: id, Alias: *rc.DisplayName}
mcastAllController(rpc.SetEndpointAlias, ops)
}
restRespSuccess(w, r, nil, acc, login, &rconf, "Configure conversation endpoint")
}
func handlerConverList(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
log.WithFields(log.Fields{"URL": r.URL.String()}).Debug("")
defer r.Body.Close()
acc, login := getAccessControl(w, r, "")
if acc == nil {
return
}
query := restParseQuery(r)
var groupFilter, domainFilter string
for _, f := range query.filters {
if f.tag == api.FilterByGroup && f.op == api.OPeq {
if exist, err := cacher.DoesGroupExist(f.value, acc); !exist {
restRespNotFoundLogAccessDenied(w, login, err)
return
}
groupFilter = f.value
} else if f.tag == api.FilterByDomain && f.op == api.OPeq {
domainFilter = f.value
}
}
convers, endpoints := cacher.GetAllApplicationConvers(groupFilter, domainFilter, acc)
if query.verbose {
// This is not a normal path, will be removed after 2.2
var resp api.RESTConversationsVerboseData
resp.Endpoints = endpoints
epMap := make(map[string]*api.RESTConversationEndpoint, 0)
for _, ep := range endpoints {
epMap[ep.ID] = ep
}
resp.Convers = make([]*api.RESTConversation, len(convers))
for i, conver := range convers {
from, ok := epMap[conver.From]
from_brief := api.RESTWorkloadBrief{ID: conver.From}
to, ok1 := epMap[conver.To]
to_brief := api.RESTWorkloadBrief{ID: conver.To}
if !ok && (ok1 && !to.ServiceMesh) {
continue
}
if !ok1 && (ok && !from.ServiceMesh) {
continue
}
if !ok {
from_brief.DisplayName = "sidecar-proxy"
from = &api.RESTConversationEndpoint{
Kind: api.EndpointKindContainer,
RESTWorkloadBrief: from_brief}
}
if !ok1 {
to_brief.DisplayName = "sidecar-proxy"
to = &api.RESTConversationEndpoint{
Kind: api.EndpointKindContainer,
RESTWorkloadBrief: to_brief}
}
resp.Convers[i] = &api.RESTConversation{
From: from, To: to,
RESTConversationReport: conver.RESTConversationReport,
}
}
log.WithFields(log.Fields{
"conversations": len(resp.Convers), "endpoints": len(resp.Endpoints),
}).Debug("Response")
restRespSuccess(w, r, &resp, acc, login, nil, "Get conversation list")
} else {
var resp api.RESTConversationsData
resp.Convers = convers
resp.Endpoints = endpoints
log.WithFields(log.Fields{
"conversations": len(resp.Convers), "endpoints": len(resp.Endpoints),
}).Debug("Response")
restRespSuccess(w, r, &resp, acc, login, nil, "Get conversation list")
}
}
func handlerConverShow(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
log.WithFields(log.Fields{"URL": r.URL.String()}).Debug("")
defer r.Body.Close()
acc, login := getAccessControl(w, r, "")
if acc == nil {
return
}
from := ps.ByName("from")
to := ps.ByName("to")
body, _ := ioutil.ReadAll(r.Body)
var data api.RESTConversationQueryData
err := json.Unmarshal(body, &data)
var conver *api.RESTConversationDetail
if err != nil || data.Query == nil {
conver, err = cacher.GetApplicationConver(from, to, nil, nil, acc)
} else {
conver, err = cacher.GetApplicationConver(from, to, data.Query.From, data.Query.To, acc)
}
if conver == nil {
restRespNotFoundLogAccessDenied(w, login, err)
return
}
var resp api.RESTConversationsDetailData
resp.Conver = conver
log.WithFields(log.Fields{"entries": len(resp.Conver.Entries)}).Debug("Response")
restRespSuccess(w, r, &resp, acc, login, nil, "Get conversation detail")
}
func handlerConverDeleteAll(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
log.WithFields(log.Fields{"URL": r.URL.String()}).Debug("")
defer r.Body.Close()
acc, login := getAccessControl(w, r, "")
if acc == nil {
return
}
// Only support super user for now.
if !acc.CanWriteCluster() {
restRespAccessDenied(w, login)
return
}
ops := &share.CLUSGraphOps{}
mcastAllController(rpc.DeleteConversation, ops)
restRespSuccess(w, r, nil, acc, login, nil, "Delete all conversations")
}
func handlerConverDelete(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
log.WithFields(log.Fields{"URL": r.URL.String()}).Debug("")
defer r.Body.Close()
acc, login := getAccessControl(w, r, "")
if acc == nil {
return
}
from := ps.ByName("from")
to := ps.ByName("to")
cached, err := cacher.GetApplicationConver(from, to, nil, nil, acc)
if cached == nil {
restRespNotFoundLogAccessDenied(w, login, err)
return
}
ops := &share.CLUSGraphOps{From: from, To: to}
mcastAllController(rpc.DeleteConversation, ops)
restRespSuccess(w, r, nil, acc, login, nil, "Delete conversation")
}
func mcastAllController(f mcastGraphRPC, ops *share.CLUSGraphOps) {
eps := cacher.GetAllControllerRPCEndpoints(access.NewReaderAccessControl())
for _, ep := range eps {
go func(ClusterIP string, RPCServerPort uint16) {
f(ClusterIP, RPCServerPort, ops)
}(ep.ClusterIP, ep.RPCServerPort)
}
}