-
Notifications
You must be signed in to change notification settings - Fork 469
/
AppWorkflowRestHandler.go
393 lines (365 loc) · 13.5 KB
/
AppWorkflowRestHandler.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
/*
* Copyright (c) 2020 Devtron Labs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package restHandler
import (
"encoding/json"
"github.com/devtron-labs/devtron/api/restHandler/common"
"github.com/devtron-labs/devtron/internal/sql/repository/app"
appWorkflow2 "github.com/devtron-labs/devtron/internal/sql/repository/appWorkflow"
"github.com/devtron-labs/devtron/internal/util"
appGroup2 "github.com/devtron-labs/devtron/pkg/appGroup"
"github.com/devtron-labs/devtron/pkg/appWorkflow"
"github.com/devtron-labs/devtron/pkg/bean"
"github.com/devtron-labs/devtron/pkg/pipeline"
"github.com/devtron-labs/devtron/pkg/team"
"github.com/devtron-labs/devtron/pkg/user"
"github.com/devtron-labs/devtron/pkg/user/casbin"
"github.com/devtron-labs/devtron/util/rbac"
"github.com/gorilla/mux"
"go.opentelemetry.io/otel"
"go.uber.org/zap"
"net/http"
"strconv"
"strings"
)
type AppWorkflowRestHandler interface {
CreateAppWorkflow(w http.ResponseWriter, r *http.Request)
FindAppWorkflow(w http.ResponseWriter, r *http.Request)
DeleteAppWorkflow(w http.ResponseWriter, r *http.Request)
FindAllWorkflows(w http.ResponseWriter, r *http.Request)
FindAppWorkflowByEnvironment(w http.ResponseWriter, r *http.Request)
GetWorkflowsViewData(w http.ResponseWriter, r *http.Request)
}
type AppWorkflowRestHandlerImpl struct {
Logger *zap.SugaredLogger
appWorkflowService appWorkflow.AppWorkflowService
userAuthService user.UserService
teamService team.TeamService
enforcer casbin.Enforcer
pipelineBuilder pipeline.PipelineBuilder
appRepository app.AppRepository
enforcerUtil rbac.EnforcerUtil
}
func NewAppWorkflowRestHandlerImpl(Logger *zap.SugaredLogger, userAuthService user.UserService, appWorkflowService appWorkflow.AppWorkflowService,
teamService team.TeamService, enforcer casbin.Enforcer, pipelineBuilder pipeline.PipelineBuilder,
appRepository app.AppRepository, enforcerUtil rbac.EnforcerUtil) *AppWorkflowRestHandlerImpl {
return &AppWorkflowRestHandlerImpl{
Logger: Logger,
appWorkflowService: appWorkflowService,
userAuthService: userAuthService,
teamService: teamService,
enforcer: enforcer,
pipelineBuilder: pipelineBuilder,
appRepository: appRepository,
enforcerUtil: enforcerUtil,
}
}
func (handler AppWorkflowRestHandlerImpl) CreateAppWorkflow(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
userId, err := handler.userAuthService.GetLoggedInUser(r)
handler.Logger.Debugw("request by user", "userId", userId)
if userId == 0 || err != nil {
return
}
var request appWorkflow.AppWorkflowDto
err = decoder.Decode(&request)
if err != nil {
handler.Logger.Errorw("decode err", "err", err)
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
token := r.Header.Get("token")
//rbac block starts from here
resourceName := handler.enforcerUtil.GetAppRBACNameByAppId(request.AppId)
if ok := handler.enforcer.Enforce(token, casbin.ResourceApplications, casbin.ActionCreate, resourceName); !ok {
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusForbidden)
return
}
//rback block ends here
request.UserId = userId
res, err := handler.appWorkflowService.CreateAppWorkflow(request)
if err != nil {
handler.Logger.Errorw("error on creating", "err", err)
common.WriteJsonResp(w, err, []byte("Creation Failed"), http.StatusInternalServerError)
return
}
common.WriteJsonResp(w, err, res, http.StatusOK)
}
func (handler AppWorkflowRestHandlerImpl) DeleteAppWorkflow(w http.ResponseWriter, r *http.Request) {
userId, err := handler.userAuthService.GetLoggedInUser(r)
handler.Logger.Debugw("request by user", "userId", userId)
if userId == 0 || err != nil {
return
}
vars := mux.Vars(r)
appId, err := strconv.Atoi(vars["app-id"])
if err != nil {
handler.Logger.Errorw("bad request", "err", err)
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
appWorkflowId, err := strconv.Atoi(vars["app-wf-id"])
if err != nil {
handler.Logger.Errorw("bad request", "err", err)
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
token := r.Header.Get("token")
//rbac block starts from here
resourceName := handler.enforcerUtil.GetAppRBACNameByAppId(appId)
if ok := handler.enforcer.Enforce(token, casbin.ResourceApplications, casbin.ActionDelete, resourceName); !ok {
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusForbidden)
return
}
//rback block ends here
err = handler.appWorkflowService.DeleteAppWorkflow(appWorkflowId, userId)
if err != nil {
if _, ok := err.(*util.ApiError); ok {
handler.Logger.Warnw("error on deleting", "err", err)
} else {
handler.Logger.Errorw("error on deleting", "err", err)
}
common.WriteJsonResp(w, err, []byte("Creation Failed"), http.StatusInternalServerError)
return
}
common.WriteJsonResp(w, err, nil, http.StatusOK)
}
func (impl AppWorkflowRestHandlerImpl) FindAppWorkflow(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
appId, err := strconv.Atoi(vars["app-id"])
if err != nil {
impl.Logger.Errorw("bad request", "err", err)
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
token := r.Header.Get("token")
app, err := impl.pipelineBuilder.GetApp(appId)
if err != nil {
impl.Logger.Errorw("bad request", "err", err)
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
// RBAC enforcer applying
object := impl.enforcerUtil.GetAppRBACName(app.AppName)
impl.Logger.Debugw("rbac object for other environment list", "object", object)
if ok := impl.enforcer.Enforce(token, casbin.ResourceApplications, casbin.ActionGet, object); !ok {
common.WriteJsonResp(w, err, "unauthorized user", http.StatusForbidden)
return
}
//RBAC enforcer Ends
workflows := make(map[string]interface{})
workflowsList, err := impl.appWorkflowService.FindAppWorkflows(appId)
if err != nil {
impl.Logger.Errorw("error in fetching workflows for app", "err", err)
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
}
workflows["appId"] = app.Id
workflows["appName"] = app.AppName
if len(workflowsList) > 0 {
workflows["workflows"] = workflowsList
} else {
workflows["workflows"] = []appWorkflow.AppWorkflowDto{}
}
common.WriteJsonResp(w, err, workflows, http.StatusOK)
}
func (impl AppWorkflowRestHandlerImpl) FindAllWorkflows(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
appId, err := strconv.Atoi(vars["appId"])
if err != nil {
impl.Logger.Errorw("Bad request", "err", err)
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
token := r.Header.Get("token")
app, err := impl.pipelineBuilder.GetApp(appId)
if err != nil {
impl.Logger.Errorw("Bad request, invalid appId", "err", err)
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
// RBAC enforcer applying
object := impl.enforcerUtil.GetAppRBACName(app.AppName)
if ok := impl.enforcer.Enforce(token, casbin.ResourceApplications, casbin.ActionGet, object); !ok {
common.WriteJsonResp(w, err, "Unauthorized user", http.StatusForbidden)
return
}
//RBAC enforcer Ends
resp, err := impl.appWorkflowService.FindAllWorkflowsComponentDetails(appId)
if err != nil {
impl.Logger.Errorw("error in getting all wf component details by appId", "err", err, "appId", appId)
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
}
common.WriteJsonResp(w, nil, resp, http.StatusOK)
}
func (impl AppWorkflowRestHandlerImpl) FindAppWorkflowByEnvironment(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
userId, err := impl.userAuthService.GetLoggedInUser(r)
if userId == 0 || err != nil {
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
return
}
user, err := impl.userAuthService.GetById(userId)
if userId == 0 || err != nil {
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
return
}
userEmailId := strings.ToLower(user.EmailId)
envId, err := strconv.Atoi(vars["envId"])
if err != nil {
impl.Logger.Errorw("bad request", "err", err)
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
v := r.URL.Query()
appIdsString := v.Get("appIds")
var appIds []int
if len(appIdsString) > 0 {
appIdsSlices := strings.Split(appIdsString, ",")
for _, appId := range appIdsSlices {
id, err := strconv.Atoi(appId)
if err != nil {
common.WriteJsonResp(w, err, "please provide valid appIds", http.StatusBadRequest)
return
}
appIds = append(appIds, id)
}
}
var appGroupId int
appGroupIdStr := v.Get("appGroupId")
if len(appGroupIdStr) > 0 {
appGroupId, err = strconv.Atoi(appGroupIdStr)
if err != nil {
common.WriteJsonResp(w, err, "please provide valid appGroupId", http.StatusBadRequest)
return
}
}
request := appGroup2.AppGroupingRequest{
EnvId: envId,
AppGroupId: appGroupId,
AppIds: appIds,
EmailId: userEmailId,
CheckAuthBatch: impl.checkAuthBatch,
UserId: userId,
Ctx: r.Context(),
}
workflows := make(map[string]interface{})
_, span := otel.Tracer("orchestrator").Start(r.Context(), "ciHandler.FetchAppWorkflowsInAppGrouping")
workflowsList, err := impl.appWorkflowService.FindAppWorkflowsByEnvironmentId(request)
span.End()
if err != nil {
impl.Logger.Errorw("error in fetching workflows for app", "err", err)
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
}
workflows["envId"] = envId
if len(workflowsList) > 0 {
workflows["workflows"] = workflowsList
} else {
workflows["workflows"] = []appWorkflow.AppWorkflowDto{}
}
common.WriteJsonResp(w, err, workflows, http.StatusOK)
}
func (handler *AppWorkflowRestHandlerImpl) GetWorkflowsViewData(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
appId, err := strconv.Atoi(vars["app-id"])
if err != nil {
handler.Logger.Errorw("error in parsing app-id", "appId", appId, "err", err)
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
token := r.Header.Get("token")
app, err := handler.pipelineBuilder.GetApp(appId)
if err != nil {
handler.Logger.Errorw("error in getting app details", "appId", appId, "err", err)
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
// RBAC enforcer applying
object := handler.enforcerUtil.GetAppRBACName(app.AppName)
handler.Logger.Debugw("rbac object for workflows view data", "object", object)
if ok := handler.enforcer.Enforce(token, casbin.ResourceApplications, casbin.ActionGet, object); !ok {
common.WriteJsonResp(w, err, "unauthorized user", http.StatusForbidden)
return
}
//RBAC enforcer Ends
appWorkflows, err := handler.appWorkflowService.FindAppWorkflows(appId)
if err != nil {
handler.Logger.Errorw("error in fetching workflows for app", "appId", appId, "err", err)
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
}
ciPipelineViewData, err := handler.pipelineBuilder.GetTriggerViewCiPipeline(appId)
if err != nil {
if _, ok := err.(*util.ApiError); !ok {
handler.Logger.Errorw("error in fetching trigger view ci pipeline data for app", "appId", appId, "err", err)
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
} else {
err = nil
}
}
cdPipelinesForApp, err := handler.pipelineBuilder.GetTriggerViewCdPipelinesForApp(appId)
if err != nil {
if _, ok := err.(*util.ApiError); !ok {
handler.Logger.Errorw("error in fetching trigger view cd pipeline data for app", "appId", appId, "err", err)
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
} else {
err = nil
}
}
containsExternalCi := handler.containsExternalCi(appWorkflows)
var externalCiData []*bean.ExternalCiConfig
if containsExternalCi {
externalCiData, err = handler.pipelineBuilder.GetExternalCi(appId)
if err != nil {
handler.Logger.Errorw("service err, GetExternalCi", "appId", appId, "err", err)
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
}
}
response := appWorkflow.TriggerViewWorkflowConfig{
Workflows: appWorkflows,
CiConfig: ciPipelineViewData,
CdPipelines: cdPipelinesForApp,
ExternalCiConfig: externalCiData,
}
common.WriteJsonResp(w, err, response, http.StatusOK)
}
func (handler *AppWorkflowRestHandlerImpl) checkAuthBatch(emailId string, appObject []string, envObject []string) (map[string]bool, map[string]bool) {
var appResult map[string]bool
var envResult map[string]bool
if len(appObject) > 0 {
appResult = handler.enforcer.EnforceByEmailInBatch(emailId, casbin.ResourceApplications, casbin.ActionGet, appObject)
}
if len(envObject) > 0 {
envResult = handler.enforcer.EnforceByEmailInBatch(emailId, casbin.ResourceEnvironment, casbin.ActionGet, envObject)
}
return appResult, envResult
}
func (handler AppWorkflowRestHandlerImpl) containsExternalCi(appWorkflows []appWorkflow.AppWorkflowDto) bool {
for _, appWorkflowDto := range appWorkflows {
for _, workflowMappingDto := range appWorkflowDto.AppWorkflowMappingDto {
if workflowMappingDto.Type == appWorkflow2.WEBHOOK {
return true
}
}
}
return false
}