@@ -20,6 +20,7 @@ import (
20
20
"errors"
21
21
"fmt"
22
22
"github.com/devtron-labs/devtron/pkg/auth/user/repository/helper"
23
+ "net/http"
23
24
"strings"
24
25
"time"
25
26
@@ -37,7 +38,8 @@ import (
37
38
38
39
type RoleGroupService interface {
39
40
CreateRoleGroup (request * bean.RoleGroup ) (* bean.RoleGroup , error )
40
- UpdateRoleGroup (request * bean.RoleGroup , token string , managerAuth func (resource , token string , object string ) bool ) (* bean.RoleGroup , error )
41
+ UpdateRoleGroup (request * bean.RoleGroup , token string , checkRBACForGroupUpdate func (token string , groupInfo * bean.RoleGroup ,
42
+ eliminatedRoleFilters []* repository.RoleModel ) (isAuthorised bool , err error )) (* bean.RoleGroup , error )
41
43
FetchDetailedRoleGroups (req * bean.ListingRequest ) ([]* bean.RoleGroup , error )
42
44
FetchRoleGroupsById (id int32 ) (* bean.RoleGroup , error )
43
45
FetchRoleGroups () ([]* bean.RoleGroup , error )
@@ -136,21 +138,21 @@ func (impl RoleGroupServiceImpl) CreateRoleGroup(request *bean.RoleGroup) (*bean
136
138
for index , roleFilter := range request .RoleFilters {
137
139
entity := roleFilter .Entity
138
140
if entity == bean .CLUSTER_ENTITIY {
139
- policiesToBeAdded , err := impl .CreateOrUpdateRoleGroupForClusterEntity (roleFilter , request .UserId , model , nil , "" , nil , tx , mapping [index ])
141
+ policiesToBeAdded , err := impl .CreateOrUpdateRoleGroupForClusterEntity (roleFilter , request .UserId , model , nil , tx , mapping [index ])
140
142
policies = append (policies , policiesToBeAdded ... )
141
143
if err != nil {
142
144
// making it non-blocking as it is being done for multiple Role filters and does not want this to be blocking.
143
145
impl .logger .Errorw ("error in creating updating role group for cluster entity" , "err" , err , "roleFilter" , roleFilter )
144
146
}
145
147
} else if entity == bean2 .EntityJobs {
146
- policiesToBeAdded , err := impl .CreateOrUpdateRoleGroupForJobsEntity (roleFilter , request .UserId , model , nil , "" , nil , tx , mapping [index ])
148
+ policiesToBeAdded , err := impl .CreateOrUpdateRoleGroupForJobsEntity (roleFilter , request .UserId , model , nil , tx , mapping [index ])
147
149
policies = append (policies , policiesToBeAdded ... )
148
150
if err != nil {
149
151
// making it non-blocking as it is being done for multiple Role filters and does not want this to be blocking.
150
152
impl .logger .Errorw ("error in creating updating role group for jobs entity" , "err" , err , "roleFilter" , roleFilter )
151
153
}
152
154
} else {
153
- policiesToBeAdded , err := impl .CreateOrUpdateRoleGroupForOtherEntity (roleFilter , request , model , nil , "" , nil , tx , mapping [index ])
155
+ policiesToBeAdded , err := impl .CreateOrUpdateRoleGroupForOtherEntity (roleFilter , request , model , nil , tx , mapping [index ])
154
156
policies = append (policies , policiesToBeAdded ... )
155
157
if err != nil {
156
158
// making it non-blocking as it is being done for multiple Role filters and does not want this to be blocking.
@@ -199,7 +201,7 @@ func (impl RoleGroupServiceImpl) CreateRoleGroup(request *bean.RoleGroup) (*bean
199
201
return request , nil
200
202
}
201
203
202
- func (impl RoleGroupServiceImpl ) CreateOrUpdateRoleGroupForClusterEntity (roleFilter bean.RoleFilter , userId int32 , model * repository.RoleGroup , existingRoles map [int ]* repository.RoleGroupRoleMapping , token string , managerAuth func ( resource string , token string , object string ) bool , tx * pg.Tx , capacity int ) ([]casbin2.Policy , error ) {
204
+ func (impl RoleGroupServiceImpl ) CreateOrUpdateRoleGroupForClusterEntity (roleFilter bean.RoleFilter , userId int32 , model * repository.RoleGroup , existingRoles map [int ]* repository.RoleGroupRoleMapping , tx * pg.Tx , capacity int ) ([]casbin2.Policy , error ) {
203
205
//var policiesToBeAdded []casbin2.Policy
204
206
namespaces := strings .Split (roleFilter .Namespace , "," )
205
207
groups := strings .Split (roleFilter .Group , "," )
@@ -213,12 +215,6 @@ func (impl RoleGroupServiceImpl) CreateOrUpdateRoleGroupForClusterEntity(roleFil
213
215
for _ , group := range groups {
214
216
for _ , kind := range kinds {
215
217
for _ , resource := range resources {
216
- if managerAuth != nil {
217
- isValidAuth := impl .userCommonService .CheckRbacForClusterEntity (roleFilter .Cluster , namespace , group , kind , resource , token , managerAuth )
218
- if ! isValidAuth {
219
- continue
220
- }
221
- }
222
218
roleModel , err := impl .userAuthRepository .GetRoleByFilterForAllTypes (entity , "" , "" , "" , "" , accessType , roleFilter .Cluster , namespace , group , kind , resource , actionType , false , "" )
223
219
if err != nil {
224
220
impl .logger .Errorw ("error in getting new role model by filter" )
@@ -263,7 +259,7 @@ func (impl RoleGroupServiceImpl) CreateOrUpdateRoleGroupForClusterEntity(roleFil
263
259
return policiesToBeAdded , nil
264
260
}
265
261
266
- func (impl RoleGroupServiceImpl ) CreateOrUpdateRoleGroupForOtherEntity (roleFilter bean.RoleFilter , request * bean.RoleGroup , model * repository.RoleGroup , existingRoles map [int ]* repository.RoleGroupRoleMapping , token string , managerAuth func ( resource string , token string , object string ) bool , tx * pg.Tx , capacity int ) ([]casbin2.Policy , error ) {
262
+ func (impl RoleGroupServiceImpl ) CreateOrUpdateRoleGroupForOtherEntity (roleFilter bean.RoleFilter , request * bean.RoleGroup , model * repository.RoleGroup , existingRoles map [int ]* repository.RoleGroupRoleMapping , tx * pg.Tx , capacity int ) ([]casbin2.Policy , error ) {
267
263
actionType := roleFilter .Action
268
264
accessType := roleFilter .AccessType
269
265
entity := roleFilter .Entity
@@ -319,7 +315,7 @@ func (impl RoleGroupServiceImpl) CreateOrUpdateRoleGroupForOtherEntity(roleFilte
319
315
return policiesToBeAdded , nil
320
316
}
321
317
322
- func (impl RoleGroupServiceImpl ) CreateOrUpdateRoleGroupForJobsEntity (roleFilter bean.RoleFilter , userId int32 , model * repository.RoleGroup , existingRoles map [int ]* repository.RoleGroupRoleMapping , token string , managerAuth func ( resource string , token string , object string ) bool , tx * pg.Tx , capacity int ) ([]casbin2.Policy , error ) {
318
+ func (impl RoleGroupServiceImpl ) CreateOrUpdateRoleGroupForJobsEntity (roleFilter bean.RoleFilter , userId int32 , model * repository.RoleGroup , existingRoles map [int ]* repository.RoleGroupRoleMapping , tx * pg.Tx , capacity int ) ([]casbin2.Policy , error ) {
323
319
actionType := roleFilter .Action
324
320
accessType := roleFilter .AccessType
325
321
entity := roleFilter .Entity
@@ -372,7 +368,8 @@ func (impl RoleGroupServiceImpl) CreateOrUpdateRoleGroupForJobsEntity(roleFilter
372
368
return policiesToBeAdded , nil
373
369
}
374
370
375
- func (impl RoleGroupServiceImpl ) UpdateRoleGroup (request * bean.RoleGroup , token string , managerAuth func (resource , token string , object string ) bool ) (* bean.RoleGroup , error ) {
371
+ func (impl RoleGroupServiceImpl ) UpdateRoleGroup (request * bean.RoleGroup , token string , checkRBACForGroupUpdate func (token string , groupInfo * bean.RoleGroup ,
372
+ eliminatedRoleFilters []* repository.RoleModel ) (isAuthorised bool , err error )) (* bean.RoleGroup , error ) {
376
373
dbConnection := impl .roleGroupRepository .GetConnection ()
377
374
tx , err := dbConnection .Begin ()
378
375
if err != nil {
@@ -404,6 +401,8 @@ func (impl RoleGroupServiceImpl) UpdateRoleGroup(request *bean.RoleGroup, token
404
401
var eliminatedPolicies []casbin2.Policy
405
402
capacity , mapping := impl .userCommonService .GetCapacityForRoleFilter (request .RoleFilters )
406
403
var policies = make ([]casbin2.Policy , 0 , capacity )
404
+ var eliminatedRoleModels []* repository.RoleModel
405
+ var items []casbin2.Policy
407
406
if request .SuperAdmin == false {
408
407
roleGroupMappingModels , err := impl .roleGroupRepository .GetRoleGroupRoleMappingByRoleGroupId (roleGroup .Id )
409
408
if err != nil {
@@ -417,7 +416,7 @@ func (impl RoleGroupServiceImpl) UpdateRoleGroup(request *bean.RoleGroup, token
417
416
418
417
// DELETE PROCESS STARTS
419
418
420
- items , err : = impl .userCommonService .RemoveRolesAndReturnEliminatedPoliciesForGroups (request , existingRoles , eliminatedRoles , tx , token , managerAuth )
419
+ items , eliminatedRoleModels , err = impl .userCommonService .RemoveRolesAndReturnEliminatedPoliciesForGroups (request , existingRoles , eliminatedRoles , tx )
421
420
if err != nil {
422
421
return nil , err
423
422
}
@@ -427,32 +426,24 @@ func (impl RoleGroupServiceImpl) UpdateRoleGroup(request *bean.RoleGroup, token
427
426
//Adding New Policies
428
427
for index , roleFilter := range request .RoleFilters {
429
428
if roleFilter .Entity == bean .CLUSTER_ENTITIY {
430
- policiesToBeAdded , err := impl .CreateOrUpdateRoleGroupForClusterEntity (roleFilter , request .UserId , roleGroup , existingRoles , token , managerAuth , tx , mapping [index ])
429
+ policiesToBeAdded , err := impl .CreateOrUpdateRoleGroupForClusterEntity (roleFilter , request .UserId , roleGroup , existingRoles , tx , mapping [index ])
431
430
policies = append (policies , policiesToBeAdded ... )
432
431
if err != nil {
433
432
impl .logger .Errorw ("error in creating updating role group for cluster entity" , "err" , err , "roleFilter" , roleFilter )
434
433
}
435
434
} else {
436
- if len (roleFilter .Team ) > 0 {
437
- // check auth only for apps permission, skip for chart group
438
- rbacObject := fmt .Sprintf ("%s" , roleFilter .Team )
439
- isValidAuth := managerAuth (casbin2 .ResourceUser , token , rbacObject )
440
- if ! isValidAuth {
441
- continue
442
- }
443
- }
444
435
switch roleFilter .Entity {
445
436
case bean2 .EntityJobs :
446
437
{
447
- policiesToBeAdded , err := impl .CreateOrUpdateRoleGroupForJobsEntity (roleFilter , request .UserId , roleGroup , existingRoles , token , managerAuth , tx , mapping [index ])
438
+ policiesToBeAdded , err := impl .CreateOrUpdateRoleGroupForJobsEntity (roleFilter , request .UserId , roleGroup , existingRoles , tx , mapping [index ])
448
439
policies = append (policies , policiesToBeAdded ... )
449
440
if err != nil {
450
441
impl .logger .Errorw ("error in creating updating role group for jobs entity" , "err" , err , "roleFilter" , roleFilter )
451
442
}
452
443
}
453
444
default :
454
445
{
455
- policiesToBeAdded , err := impl .CreateOrUpdateRoleGroupForOtherEntity (roleFilter , request , roleGroup , existingRoles , token , managerAuth , tx , mapping [index ])
446
+ policiesToBeAdded , err := impl .CreateOrUpdateRoleGroupForOtherEntity (roleFilter , request , roleGroup , existingRoles , tx , mapping [index ])
456
447
policies = append (policies , policiesToBeAdded ... )
457
448
if err != nil {
458
449
impl .logger .Errorw ("error in creating updating role group for other entity" , "err" , err , "roleFilter" , roleFilter )
@@ -486,6 +477,22 @@ func (impl RoleGroupServiceImpl) UpdateRoleGroup(request *bean.RoleGroup, token
486
477
policies = append (policies , casbin2.Policy {Type : "g" , Sub : casbin2 .Subject (roleGroup .CasbinName ), Obj : casbin2 .Object (roleModel .Role )})
487
478
}
488
479
}
480
+
481
+ if checkRBACForGroupUpdate != nil {
482
+ isAuthorised , err := checkRBACForGroupUpdate (token , request , eliminatedRoleModels )
483
+ if err != nil {
484
+ impl .logger .Errorw ("error in checking RBAC for role group update" , "err" , err , "request" , request )
485
+ return nil , err
486
+ } else if ! isAuthorised {
487
+ impl .logger .Errorw ("rbac check failed for role group update" , "request" , request )
488
+ return nil , & util.ApiError {
489
+ Code : "403" ,
490
+ HttpStatusCode : http .StatusForbidden ,
491
+ UserMessage : "unauthorized" ,
492
+ }
493
+ }
494
+ }
495
+
489
496
//deleting policies from casbin
490
497
impl .logger .Debugw ("eliminated policies" , "eliminatedPolicies" , eliminatedPolicies )
491
498
if len (eliminatedPolicies ) > 0 {
0 commit comments