forked from goodrain/rainbond
-
Notifications
You must be signed in to change notification settings - Fork 2
/
middleware.go
396 lines (363 loc) · 12.7 KB
/
middleware.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
// Copyright (C) 2014-2018 Wutong Co., Ltd.
// WUTONG, Application Management Platform
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Wutong,
// one or multiple Commercial Licenses authorized by Wutong Co., Ltd.
// must be obtained first.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package middleware
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"strings"
"github.com/go-chi/chi"
"github.com/jinzhu/gorm"
"github.com/sirupsen/logrus"
"github.com/wutong-paas/wutong/api/client/kube"
"github.com/wutong-paas/wutong/api/handler"
"github.com/wutong-paas/wutong/api/util"
ctxutil "github.com/wutong-paas/wutong/api/util/ctx"
"github.com/wutong-paas/wutong/db"
dbmodel "github.com/wutong-paas/wutong/db/model"
"github.com/wutong-paas/wutong/event"
httputil "github.com/wutong-paas/wutong/util/http"
)
var pool []string
func init() {
pool = []string{
"services_status",
}
}
// InitTenantEnv 实现中间件
func InitTenantEnv(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
debugRequestBody(r)
tenantName := chi.URLParam(r, "tenant_name")
tenantEnvName := chi.URLParam(r, "tenant_env_name")
if tenantEnvName == "" {
httputil.ReturnError(r, w, 404, "cant find tenant env")
return
}
tenantEnv, err := db.GetManager().TenantEnvDao().GetTenantEnvIDByName(tenantName, tenantEnvName)
if err != nil {
logrus.Errorf("get tenant env by tenant env name error: %s %v", tenantEnvName, err)
if err.Error() == gorm.ErrRecordNotFound.Error() {
httputil.ReturnError(r, w, 404, "cant find tenantEnv")
return
}
httputil.ReturnError(r, w, 500, "get assign tenant env uuid failed")
return
}
ctx := context.WithValue(r.Context(), ctxutil.ContextKey("tenant_env_name"), tenantEnvName)
ctx = context.WithValue(ctx, ctxutil.ContextKey("tenant_env_id"), tenantEnv.UUID)
ctx = context.WithValue(ctx, ctxutil.ContextKey("tenant_env"), tenantEnv)
next.ServeHTTP(w, r.WithContext(ctx))
}
return http.HandlerFunc(fn)
}
// InitService 实现serviceinit中间件
func InitService(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
serviceAlias := chi.URLParam(r, "service_alias")
if serviceAlias == "" {
httputil.ReturnError(r, w, 404, "cant find service alias")
return
}
tenantEnvID := r.Context().Value(ctxutil.ContextKey("tenant_env_id"))
service, err := db.GetManager().TenantEnvServiceDao().GetServiceByTenantEnvIDAndServiceAlias(tenantEnvID.(string), serviceAlias)
if err != nil {
if err.Error() == gorm.ErrRecordNotFound.Error() {
httputil.ReturnError(r, w, 404, "cant find service")
return
}
logrus.Errorf("get service by tenant env & service alias error, %v", err)
httputil.ReturnError(r, w, 500, "get service id error")
return
}
serviceID := service.ServiceID
ctx := context.WithValue(r.Context(), ctxutil.ContextKey("service_alias"), serviceAlias)
ctx = context.WithValue(ctx, ctxutil.ContextKey("service_id"), serviceID)
ctx = context.WithValue(ctx, ctxutil.ContextKey("service"), service)
next.ServeHTTP(w, r.WithContext(ctx))
}
return http.HandlerFunc(fn)
}
// InitApplication -
func InitApplication(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
appID := chi.URLParam(r, "app_id")
tenantEnvApp, err := handler.GetApplicationHandler().GetAppByID(appID)
if err != nil {
httputil.ReturnBcodeError(r, w, err)
return
}
ctx := context.WithValue(r.Context(), ctxutil.ContextKey("app_id"), tenantEnvApp.AppID)
ctx = context.WithValue(ctx, ctxutil.ContextKey("application"), tenantEnvApp)
next.ServeHTTP(w, r.WithContext(ctx))
}
return http.HandlerFunc(fn)
}
// InitVeleroBackupOrRestore
func InitVeleroBackupOrRestore(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
if ok := kube.IsVeleroInstalled(kube.RegionClientset(), kube.RegionAPIExtClientset()); !ok {
httputil.ReturnError(r, w, 500, "集群中未检测到 Velero 服务,使用该功能请联系管理员安装 Velero 服务!")
return
}
next.ServeHTTP(w, r.WithContext(r.Context()))
}
return http.HandlerFunc(fn)
}
// InitVM -
func InitVM(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
if ok := kube.IsKubevirtInstalled(kube.RegionClientset(), kube.RegionAPIExtClientset()); !ok {
httputil.ReturnError(r, w, 500, "集群中未检测到 Kubevirt 服务,使用该功能请联系管理员安装 Kubevirt 服务!")
return
}
next.ServeHTTP(w, r.WithContext(r.Context()))
}
return http.HandlerFunc(fn)
}
// InitVMID -
func InitVMID(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
if ok := kube.IsKubevirtInstalled(kube.RegionClientset(), kube.RegionAPIExtClientset()); !ok {
httputil.ReturnError(r, w, 500, "集群中未检测到 Kubevirt 服务,使用该功能请联系管理员安装 Kubevirt 服务!")
return
}
vmID := chi.URLParam(r, "vm_id")
if vmID == "" {
httputil.ReturnError(r, w, 404, "need vm id")
return
}
ctx := context.WithValue(r.Context(), ctxutil.ContextKey("vm_id"), vmID)
next.ServeHTTP(w, r.WithContext(ctx))
}
return http.HandlerFunc(fn)
}
// InitPlugin 实现plugin init中间件
func InitPlugin(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
debugRequestBody(r)
pluginID := chi.URLParam(r, "plugin_id")
tenantEnvID := r.Context().Value(ctxutil.ContextKey("tenant_env_id")).(string)
if pluginID == "" {
httputil.ReturnError(r, w, 404, "need plugin id")
return
}
_, err := db.GetManager().TenantEnvPluginDao().GetPluginByID(pluginID, tenantEnvID)
if err != nil {
if err.Error() == gorm.ErrRecordNotFound.Error() {
httputil.ReturnError(r, w, 404, "cant find plugin")
return
}
logrus.Errorf("get plugin error, %v", err)
httputil.ReturnError(r, w, 500, "get plugin error")
return
}
ctx := context.WithValue(r.Context(), ctxutil.ContextKey("plugin_id"), pluginID)
next.ServeHTTP(w, r.WithContext(ctx))
}
return http.HandlerFunc(fn)
}
// SetLog SetLog
func SetLog(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
eventID := chi.URLParam(r, "event_id")
if eventID != "" {
logger := event.GetManager().GetLogger(eventID)
ctx := context.WithValue(r.Context(), ctxutil.ContextKey("logger"), logger)
next.ServeHTTP(w, r.WithContext(ctx))
}
}
return http.HandlerFunc(fn)
}
// Proxy 反向代理中间件
func Proxy(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.RequestURI, "/v2/nodes") {
handler.GetNodeProxy().Proxy(w, r)
return
}
if strings.HasPrefix(r.RequestURI, "/v2/cluster/service-health") {
handler.GetNodeProxy().Proxy(w, r)
return
}
if strings.HasPrefix(r.RequestURI, "/v2/builder") {
handler.GetBuilderProxy().Proxy(w, r)
return
}
if strings.HasPrefix(r.RequestURI, "/v2/tasks") {
handler.GetNodeProxy().Proxy(w, r)
return
}
if strings.HasPrefix(r.RequestURI, "/v2/tasktemps") {
handler.GetNodeProxy().Proxy(w, r)
return
}
if strings.HasPrefix(r.RequestURI, "/v2/taskgroups") {
handler.GetNodeProxy().Proxy(w, r)
return
}
if strings.HasPrefix(r.RequestURI, "/v2/configs") {
handler.GetNodeProxy().Proxy(w, r)
return
}
if strings.HasPrefix(r.RequestURI, "/v2/rules") {
handler.GetMonitorProxy().Proxy(w, r)
return
}
if strings.HasPrefix(r.RequestURI, "/console/filebrowser") {
paths := strings.Split(r.URL.Path, "/")
if len(paths) > 3 {
serviceID := paths[3]
proxy := handler.GetFileBrowserProxy(serviceID)
r.URL.Path = strings.Replace(r.URL.Path, "/console/filebrowser/"+serviceID, "", 1)
proxy.Proxy(w, r)
return
}
}
if strings.HasPrefix(r.RequestURI, "/console/dbgate") {
paths := strings.Split(r.URL.Path, "/")
if len(paths) > 3 {
serviceID := paths[3]
proxy := handler.GetDbgateProxy(serviceID)
proxy.Proxy(w, r)
return
}
}
if strings.HasPrefix(r.RequestURI, "/obsmimir") {
r.URL.Path = strings.Replace(r.URL.Path, "/obsmimir", "", 1)
handler.GetObsMimirProxy().Proxy(w, r)
return
}
if strings.HasPrefix(r.RequestURI, "/obstempo") {
r.URL.Path = strings.Replace(r.URL.Path, "/obstempo", "", 1)
handler.GetObsTempoProxy().Proxy(w, r)
return
}
if strings.HasPrefix(r.RequestURI, "/obsloki") {
r.URL.Path = strings.Replace(r.URL.Path, "/obsloki", "", 1)
handler.GetObsLokiProxy().Proxy(w, r)
return
}
if strings.HasPrefix(r.RequestURI, "/obs") {
r.URL.Path = strings.Replace(r.URL.Path, "/obs", "", 1)
handler.GetObsProxy().Proxy(w, r)
return
}
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
func apiExclude(r *http.Request) bool {
if r.Method == "GET" {
return true
}
for _, item := range pool {
if strings.Contains(r.RequestURI, item) {
return true
}
}
return false
}
type resWriter struct {
origWriter http.ResponseWriter
statusCode int
}
func (w *resWriter) Header() http.Header {
return w.origWriter.Header()
}
func (w *resWriter) Write(p []byte) (int, error) {
return w.origWriter.Write(p)
}
func (w *resWriter) WriteHeader(statusCode int) {
w.statusCode = statusCode
w.origWriter.WriteHeader(statusCode)
}
// WrapEL wrap eventlog, handle event log before and after process
func WrapEL(f http.HandlerFunc, target, optType string, synType int) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var serviceKind string
serviceObj := r.Context().Value(ctxutil.ContextKey("service"))
if serviceObj != nil {
service := serviceObj.(*dbmodel.TenantEnvServices)
serviceKind = service.Kind
}
if r.Method != "GET" {
body, err := io.ReadAll(r.Body)
if err != nil {
logrus.Warningf("error reading request body: %v", err)
} else {
logrus.Debugf("method: %s; uri: %s; body: %s", r.Method, r.RequestURI, string(body))
}
// set a new body, which will simulate the same data we read
r.Body = io.NopCloser(bytes.NewBuffer(body))
var targetID string
var ok bool
if targetID, ok = r.Context().Value(ctxutil.ContextKey("service_id")).(string); !ok {
var reqDataMap map[string]interface{}
if err = json.Unmarshal(body, &reqDataMap); err != nil {
httputil.ReturnError(r, w, 400, "操作对象未指定")
return
}
if targetID, ok = reqDataMap["service_id"].(string); !ok {
httputil.ReturnError(r, w, 400, "操作对象未指定")
return
}
}
//eventLog check the latest event
if !util.CanDoEvent(optType, synType, target, targetID, serviceKind) {
logrus.Errorf("operation too frequently. uri: %s; target: %s; target id: %s", r.RequestURI, target, targetID)
httputil.ReturnError(r, w, 409, "操作过于频繁,请稍后再试") // status code 409 conflict
return
}
// handle operator
var operator string
var reqData map[string]interface{}
if err = json.Unmarshal(body, &reqData); err == nil {
if operatorI := reqData["operator"]; operatorI != nil {
operator = operatorI.(string)
}
}
// tenantEnvID can not null
tenantEnvID := r.Context().Value(ctxutil.ContextKey("tenant_env_id")).(string)
var ctx context.Context
event, err := util.CreateEvent(target, optType, targetID, tenantEnvID, string(body), operator, synType)
if err != nil {
logrus.Error("create event error : ", err)
httputil.ReturnError(r, w, 500, "操作失败")
return
}
ctx = context.WithValue(r.Context(), ctxutil.ContextKey("event"), event)
ctx = context.WithValue(ctx, ctxutil.ContextKey("event_id"), event.EventID)
rw := &resWriter{origWriter: w}
f(rw, r.WithContext(ctx))
if synType == dbmodel.SYNEVENTTYPE || (synType == dbmodel.ASYNEVENTTYPE && rw.statusCode >= 400) { // status code 2XX/3XX all equal to success
util.UpdateEvent(event.EventID, rw.statusCode)
}
}
}
}
func debugRequestBody(r *http.Request) {
if !apiExclude(r) {
body, err := io.ReadAll(r.Body)
if err != nil {
logrus.Warningf("error reading request body: %v", err)
}
logrus.Debugf("method: %s; uri: %s; body: %s", r.Method, r.RequestURI, string(body))
// set a new body, which will simulate the same data we read
r.Body = io.NopCloser(bytes.NewBuffer(body))
}
}