Skip to content

Commit caf293d

Browse files
committed
Merge branch 'main' into refchart
2 parents 2906be5 + 18431d0 commit caf293d

File tree

11 files changed

+488
-452
lines changed

11 files changed

+488
-452
lines changed

api/auth/user/UserRestHandler.go

Lines changed: 280 additions & 152 deletions
Large diffs are not rendered by default.

api/bean/UserRequest.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ const (
118118
USER_TYPE_API_TOKEN = "apiToken"
119119
CHART_GROUP_ENTITY = "chart-group"
120120
CLUSTER_ENTITIY = "cluster"
121+
ACTION_SUPERADMIN = "super-admin"
121122
)
122123

123124
type UserListingResponse struct {

api/helm-app/service/HelmAppService.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ type HelmAppService interface {
8686
UpdateApplicationWithChartInfoWithExtraValues(ctx context.Context, appIdentifier *helmBean.AppIdentifier, chartRepository *gRPC.ChartRepository, extraValues map[string]interface{}, extraValuesYamlUrl string, useLatestChartVersion bool) (*openapi.UpdateReleaseResponse, error)
8787
TemplateChart(ctx context.Context, templateChartRequest *openapi2.TemplateChartRequest) (*openapi2.TemplateChartResponse, error)
8888
GetNotes(ctx context.Context, request *gRPC.InstallReleaseRequest) (string, error)
89-
ValidateOCIRegistry(ctx context.Context, OCIRegistryRequest *gRPC.RegistryCredential) bool
89+
ValidateOCIRegistry(ctx context.Context, OCIRegistryRequest *gRPC.RegistryCredential) (bool, error)
9090
GetRevisionHistoryMaxValue(appType bean.SourceAppType) int32
9191
GetResourceTreeForExternalResources(ctx context.Context, clusterId int, clusterConfig *gRPC.ClusterConfig, resources []*gRPC.ExternalResourceDetail) (*gRPC.ResourceTreeResponse, error)
9292
CheckIfNsExistsForClusterIds(clusterIdToNsMap map[int]string) error
@@ -1022,13 +1022,13 @@ func (impl *HelmAppServiceImpl) GetNotes(ctx context.Context, request *gRPC.Inst
10221022
return notesTxt, err
10231023
}
10241024

1025-
func (impl *HelmAppServiceImpl) ValidateOCIRegistry(ctx context.Context, OCIRegistryRequest *gRPC.RegistryCredential) bool {
1025+
func (impl *HelmAppServiceImpl) ValidateOCIRegistry(ctx context.Context, OCIRegistryRequest *gRPC.RegistryCredential) (bool, error) {
10261026
response, err := impl.helmAppClient.ValidateOCIRegistry(ctx, OCIRegistryRequest)
10271027
if err != nil {
10281028
impl.logger.Errorw("error in fetching chart", "err", err)
1029-
return false
1029+
return false, err
10301030
}
1031-
return response.IsLoggedIn
1031+
return response.IsLoggedIn, nil
10321032
}
10331033

10341034
func (impl *HelmAppServiceImpl) DecodeAppId(appId string) (*helmBean.AppIdentifier, error) {

api/restHandler/DockerRegRestHandler.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424

2525
"github.com/devtron-labs/devtron/api/restHandler/common"
2626
repository "github.com/devtron-labs/devtron/internal/sql/repository/dockerRegistry"
27-
"github.com/devtron-labs/devtron/internal/util"
2827
chartProviderService "github.com/devtron-labs/devtron/pkg/appStore/chartProvider"
2928
"github.com/devtron-labs/devtron/pkg/auth/authorisation/casbin"
3029
"github.com/devtron-labs/devtron/pkg/auth/user"
@@ -233,13 +232,8 @@ func (impl DockerRegRestHandlerImpl) SaveDockerRegistryConfig(w http.ResponseWri
233232
//RBAC enforcer Ends
234233

235234
// valid registry credentials from kubelink
236-
if isValid := impl.dockerRegistryConfig.ValidateRegistryCredentials(&bean); !isValid {
237-
impl.logger.Errorw("registry credentials validation err, SaveDockerRegistryConfig", "err", err, "payload", bean)
238-
err = &util.ApiError{
239-
HttpStatusCode: http.StatusBadRequest,
240-
InternalMessage: "Invalid authentication credentials. Please verify.",
241-
UserMessage: "Invalid authentication credentials. Please verify.",
242-
}
235+
if err = impl.dockerRegistryConfig.ValidateRegistryCredentials(&bean); err != nil {
236+
impl.logger.Errorw("registry credentials validation err, SaveDockerRegistryConfig", "err", err)
243237
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
244238
return
245239
}
@@ -349,13 +343,8 @@ func (impl DockerRegRestHandlerImpl) ValidateDockerRegistryConfig(w http.Respons
349343
bean.Cert = existingStore.Cert
350344
}
351345
// valid registry credentials from kubelink
352-
if isValid := impl.dockerRegistryConfig.ValidateRegistryCredentials(&bean); !isValid {
353-
impl.logger.Errorw("registry credentials validation err, SaveDockerRegistryConfig", "err", err, "payload", bean)
354-
err = &util.ApiError{
355-
HttpStatusCode: http.StatusBadRequest,
356-
InternalMessage: "Invalid authentication credentials. Please verify.",
357-
UserMessage: "Invalid authentication credentials. Please verify.",
358-
}
346+
if err = impl.dockerRegistryConfig.ValidateRegistryCredentials(&bean); err != nil {
347+
impl.logger.Errorw("registry credentials validation err, SaveDockerRegistryConfig", "err", err)
359348
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
360349
return
361350
}

cmd/external-app/wire_gen.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apiToken/ApiTokenService.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ func (impl ApiTokenServiceImpl) CreateApiToken(request *openapi.CreateApiTokenRe
205205
EmailId: email,
206206
UserType: bean.USER_TYPE_API_TOKEN,
207207
}
208-
createUserResponse, _, err := impl.userService.CreateUser(&createUserRequest, token, managerAuth)
208+
createUserResponse, err := impl.userService.CreateUser(&createUserRequest)
209209
if err != nil {
210210
impl.logger.Errorw("error while creating user for api-token", "email", email, "error", err)
211211
return nil, err

pkg/auth/user/RoleGroupService.go

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"errors"
2121
"fmt"
2222
"github.com/devtron-labs/devtron/pkg/auth/user/repository/helper"
23+
"net/http"
2324
"strings"
2425
"time"
2526

@@ -37,7 +38,8 @@ import (
3738

3839
type RoleGroupService interface {
3940
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)
4143
FetchDetailedRoleGroups(req *bean.ListingRequest) ([]*bean.RoleGroup, error)
4244
FetchRoleGroupsById(id int32) (*bean.RoleGroup, error)
4345
FetchRoleGroups() ([]*bean.RoleGroup, error)
@@ -136,21 +138,21 @@ func (impl RoleGroupServiceImpl) CreateRoleGroup(request *bean.RoleGroup) (*bean
136138
for index, roleFilter := range request.RoleFilters {
137139
entity := roleFilter.Entity
138140
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])
140142
policies = append(policies, policiesToBeAdded...)
141143
if err != nil {
142144
// making it non-blocking as it is being done for multiple Role filters and does not want this to be blocking.
143145
impl.logger.Errorw("error in creating updating role group for cluster entity", "err", err, "roleFilter", roleFilter)
144146
}
145147
} 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])
147149
policies = append(policies, policiesToBeAdded...)
148150
if err != nil {
149151
// making it non-blocking as it is being done for multiple Role filters and does not want this to be blocking.
150152
impl.logger.Errorw("error in creating updating role group for jobs entity", "err", err, "roleFilter", roleFilter)
151153
}
152154
} 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])
154156
policies = append(policies, policiesToBeAdded...)
155157
if err != nil {
156158
// 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
199201
return request, nil
200202
}
201203

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) {
203205
//var policiesToBeAdded []casbin2.Policy
204206
namespaces := strings.Split(roleFilter.Namespace, ",")
205207
groups := strings.Split(roleFilter.Group, ",")
@@ -213,12 +215,6 @@ func (impl RoleGroupServiceImpl) CreateOrUpdateRoleGroupForClusterEntity(roleFil
213215
for _, group := range groups {
214216
for _, kind := range kinds {
215217
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-
}
222218
roleModel, err := impl.userAuthRepository.GetRoleByFilterForAllTypes(entity, "", "", "", "", accessType, roleFilter.Cluster, namespace, group, kind, resource, actionType, false, "")
223219
if err != nil {
224220
impl.logger.Errorw("error in getting new role model by filter")
@@ -263,7 +259,7 @@ func (impl RoleGroupServiceImpl) CreateOrUpdateRoleGroupForClusterEntity(roleFil
263259
return policiesToBeAdded, nil
264260
}
265261

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) {
267263
actionType := roleFilter.Action
268264
accessType := roleFilter.AccessType
269265
entity := roleFilter.Entity
@@ -319,7 +315,7 @@ func (impl RoleGroupServiceImpl) CreateOrUpdateRoleGroupForOtherEntity(roleFilte
319315
return policiesToBeAdded, nil
320316
}
321317

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) {
323319
actionType := roleFilter.Action
324320
accessType := roleFilter.AccessType
325321
entity := roleFilter.Entity
@@ -372,7 +368,8 @@ func (impl RoleGroupServiceImpl) CreateOrUpdateRoleGroupForJobsEntity(roleFilter
372368
return policiesToBeAdded, nil
373369
}
374370

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) {
376373
dbConnection := impl.roleGroupRepository.GetConnection()
377374
tx, err := dbConnection.Begin()
378375
if err != nil {
@@ -404,6 +401,8 @@ func (impl RoleGroupServiceImpl) UpdateRoleGroup(request *bean.RoleGroup, token
404401
var eliminatedPolicies []casbin2.Policy
405402
capacity, mapping := impl.userCommonService.GetCapacityForRoleFilter(request.RoleFilters)
406403
var policies = make([]casbin2.Policy, 0, capacity)
404+
var eliminatedRoleModels []*repository.RoleModel
405+
var items []casbin2.Policy
407406
if request.SuperAdmin == false {
408407
roleGroupMappingModels, err := impl.roleGroupRepository.GetRoleGroupRoleMappingByRoleGroupId(roleGroup.Id)
409408
if err != nil {
@@ -417,7 +416,7 @@ func (impl RoleGroupServiceImpl) UpdateRoleGroup(request *bean.RoleGroup, token
417416

418417
// DELETE PROCESS STARTS
419418

420-
items, err := impl.userCommonService.RemoveRolesAndReturnEliminatedPoliciesForGroups(request, existingRoles, eliminatedRoles, tx, token, managerAuth)
419+
items, eliminatedRoleModels, err = impl.userCommonService.RemoveRolesAndReturnEliminatedPoliciesForGroups(request, existingRoles, eliminatedRoles, tx)
421420
if err != nil {
422421
return nil, err
423422
}
@@ -427,32 +426,24 @@ func (impl RoleGroupServiceImpl) UpdateRoleGroup(request *bean.RoleGroup, token
427426
//Adding New Policies
428427
for index, roleFilter := range request.RoleFilters {
429428
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])
431430
policies = append(policies, policiesToBeAdded...)
432431
if err != nil {
433432
impl.logger.Errorw("error in creating updating role group for cluster entity", "err", err, "roleFilter", roleFilter)
434433
}
435434
} 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-
}
444435
switch roleFilter.Entity {
445436
case bean2.EntityJobs:
446437
{
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])
448439
policies = append(policies, policiesToBeAdded...)
449440
if err != nil {
450441
impl.logger.Errorw("error in creating updating role group for jobs entity", "err", err, "roleFilter", roleFilter)
451442
}
452443
}
453444
default:
454445
{
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])
456447
policies = append(policies, policiesToBeAdded...)
457448
if err != nil {
458449
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
486477
policies = append(policies, casbin2.Policy{Type: "g", Sub: casbin2.Subject(roleGroup.CasbinName), Obj: casbin2.Object(roleModel.Role)})
487478
}
488479
}
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+
489496
//deleting policies from casbin
490497
impl.logger.Debugw("eliminated policies", "eliminatedPolicies", eliminatedPolicies)
491498
if len(eliminatedPolicies) > 0 {

0 commit comments

Comments
 (0)