-
Notifications
You must be signed in to change notification settings - Fork 523
/
Copy pathUserAuthRepository.go
1161 lines (1089 loc) · 39.2 KB
/
UserAuthRepository.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2020-2024. Devtron Inc.
*
* 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.
*/
/*
@description: user authentication and authorization
*/
package repository
import (
"encoding/json"
"fmt"
bean3 "github.com/devtron-labs/devtron/pkg/auth/authorisation/casbin/bean"
"github.com/devtron-labs/devtron/pkg/auth/user/adapter"
bean4 "github.com/devtron-labs/devtron/pkg/auth/user/repository/bean"
"strings"
"time"
"github.com/devtron-labs/devtron/api/bean"
casbin2 "github.com/devtron-labs/devtron/pkg/auth/authorisation/casbin"
bean2 "github.com/devtron-labs/devtron/pkg/auth/user/bean"
"github.com/devtron-labs/devtron/pkg/sql"
"github.com/devtron-labs/devtron/util"
"github.com/go-pg/pg"
"go.uber.org/zap"
)
type UserAuthRepository interface {
CreateRole(role *RoleModel) (*RoleModel, error)
CreateRoleWithTxn(userModel *RoleModel, tx *pg.Tx) (*RoleModel, error)
GetRoleById(id int) (*RoleModel, error)
GetRolesByIds(ids []int) ([]*RoleModel, error)
GetRoleByRoles(roles []string) ([]RoleModel, error)
GetRolesByUserId(userId int32) ([]*RoleModel, error)
GetRolesByGroupId(userId int32) ([]*RoleModel, error)
GetAllRole() ([]RoleModel, error)
GetRolesByActionAndAccessType(action string, accessType string) ([]RoleModel, error)
GetRoleByFilterForAllTypes(roleFieldDto *bean4.RoleModelFieldsDto) (RoleModel, error)
CreateUserRoleMapping(userRoleModel *UserRoleModel, tx *pg.Tx) (*UserRoleModel, error)
GetUserRoleMappingByUserId(userId int32) ([]*UserRoleModel, error)
GetUserRoleMappingIdsByUserId(userId int32) ([]int, error)
GetUserRoleMappingIdsByUserIds(userIds []int32) ([]int, error)
DeleteUserRoleMapping(userRoleModel *UserRoleModel, tx *pg.Tx) (bool, error)
DeleteUserRoleMappingByIds(urmIds []int, tx *pg.Tx) error
DeleteUserRoleByRoleId(roleId int, tx *pg.Tx) error
DeleteUserRoleByRoleIds(roleIds []int, tx *pg.Tx) error
CreateDefaultPoliciesForAllTypes(team, entityName, env, entity, cluster, namespace, group, kind, resource, actionType, accessType string, UserId int32) (bool, error, []bean3.Policy)
CreateRoleForSuperAdminIfNotExists(tx *pg.Tx, UserId int32) (bool, error)
SyncOrchestratorToCasbin(team string, entityName string, env string, tx *pg.Tx) (bool, error)
UpdateTriggerPolicyForTerminalAccess() error
GetRolesForEnvironment(envName, envIdentifier string) ([]*RoleModel, error)
GetRolesForProject(teamName string) ([]*RoleModel, error)
GetRolesForApp(appName string) ([]*RoleModel, error)
GetRolesForChartGroup(chartGroupName string) ([]*RoleModel, error)
DeleteRole(role *RoleModel, tx *pg.Tx) error
DeleteRolesByIds(roleIds []int, tx *pg.Tx) error
//GetRoleByFilterForClusterEntity(cluster, namespace, group, kind, resource, action string) (RoleModel, error)
GetRolesByUserIdAndEntityType(userId int32, entityType string) ([]*RoleModel, error)
CreateRolesWithAccessTypeAndEntity(team, entityName, env, entity, cluster, namespace, group, kind, resource, actionType, accessType string, UserId int32, role string) (bool, error)
GetRolesForWorkflow(workflow, entityName string) ([]*RoleModel, error)
GetRoleForClusterEntity(cluster, namespace, group, kind, resource, action string) (RoleModel, error)
GetRoleForJobsEntity(entity, team, app, env, act string, workflow string) (RoleModel, error)
GetRoleForOtherEntity(team, app, env, act, accessType string, oldValues bool) (RoleModel, error)
GetRoleForChartGroupEntity(entity, app, act, accessType string) (RoleModel, error)
}
type UserAuthRepositoryImpl struct {
dbConnection *pg.DB
Logger *zap.SugaredLogger
defaultAuthPolicyRepository DefaultAuthPolicyRepository
defaultAuthRoleRepository DefaultAuthRoleRepository
}
func NewUserAuthRepositoryImpl(dbConnection *pg.DB, Logger *zap.SugaredLogger,
defaultAuthPolicyRepository DefaultAuthPolicyRepository,
defaultAuthRoleRepository DefaultAuthRoleRepository) *UserAuthRepositoryImpl {
return &UserAuthRepositoryImpl{
dbConnection: dbConnection,
Logger: Logger,
defaultAuthPolicyRepository: defaultAuthPolicyRepository,
defaultAuthRoleRepository: defaultAuthRoleRepository,
}
}
type RoleModel struct {
TableName struct{} `sql:"roles" pg:",discard_unknown_columns"`
Id int `sql:"id,pk"`
Role string `sql:"role,notnull"`
Entity string `sql:"entity"`
Team string `sql:"team"`
EntityName string `sql:"entity_name"`
Environment string `sql:"environment"`
Action string `sql:"action"`
AccessType string `sql:"access_type"`
Cluster string `sql:"cluster"`
Namespace string `sql:"namespace"`
Group string `sql:"group"`
Kind string `sql:"kind"`
Resource string `sql:"resource"`
Workflow string `sql:"workflow"`
sql.AuditLog
}
type RolePolicyDetails struct {
Team string
Env string
App string
TeamObj string
EnvObj string
AppObj string
Entity string
EntityName string
Cluster string
Namespace string
Group string
Kind string
Resource string
ClusterObj string
NamespaceObj string
GroupObj string
KindObj string
ResourceObj string
Approver bool
}
type ClusterRolePolicyDetails struct {
Entity string
Cluster string
Namespace string
Group string
Kind string
Resource string
ClusterObj string
NamespaceObj string
GroupObj string
KindObj string
ResourceObj string
}
func (r RoleModel) GetTeam() string { return r.Team }
func (r RoleModel) GetEntity() string { return r.Entity }
func (r RoleModel) GetAction() string { return r.Action }
func (r RoleModel) GetAccessType() string { return r.AccessType }
func (r RoleModel) GetEnvironment() string { return r.Environment }
func (r RoleModel) GetCluster() string { return r.Cluster }
func (r RoleModel) GetGroup() string { return r.Group }
func (r RoleModel) GetKind() string { return r.Kind }
func (r RoleModel) GetEntityName() string { return r.EntityName }
func (r RoleModel) GetResource() string { return r.Resource }
func (r RoleModel) GetWorkflow() string { return r.Workflow }
func (r RoleModel) GetNamespace() string { return r.Namespace }
func (impl UserAuthRepositoryImpl) CreateRole(role *RoleModel) (*RoleModel, error) {
err := impl.dbConnection.Insert(role)
if err != nil {
impl.Logger.Error("error in creating role", "err", err, "role", role)
return role, err
}
return role, nil
}
func (impl UserAuthRepositoryImpl) CreateRoleWithTxn(userModel *RoleModel, tx *pg.Tx) (*RoleModel, error) {
err := tx.Insert(userModel)
if err != nil {
impl.Logger.Error(err)
return userModel, err
}
return userModel, nil
}
func (impl UserAuthRepositoryImpl) GetRoleById(id int) (*RoleModel, error) {
var model RoleModel
err := impl.dbConnection.Model(&model).Where("id = ?", id).Select()
if err != nil {
impl.Logger.Error(err)
return &model, err
}
return &model, nil
}
func (impl UserAuthRepositoryImpl) GetRolesByIds(ids []int) ([]*RoleModel, error) {
var model []*RoleModel
err := impl.dbConnection.Model(&model).Where("id IN (?)", pg.In(ids)).Select()
if err != nil {
impl.Logger.Error(err)
return model, err
}
return model, nil
}
func (impl UserAuthRepositoryImpl) GetRoleByRoles(roles []string) ([]RoleModel, error) {
var model []RoleModel
err := impl.dbConnection.Model(&model).Where("role IN (?)", pg.In(roles)).Select()
if err != nil {
impl.Logger.Error(err)
return model, err
}
return model, nil
}
func (impl UserAuthRepositoryImpl) GetRolesByUserId(userId int32) ([]*RoleModel, error) {
var models []*RoleModel
err := impl.dbConnection.Model(&models).
Column("role_model.*").
Join("INNER JOIN user_roles ur on ur.role_id=role_model.id").
Where("ur.user_id = ?", userId).Select()
if err != nil {
impl.Logger.Error(err)
return models, err
}
return models, nil
}
func (impl UserAuthRepositoryImpl) GetRolesByGroupId(roleGroupId int32) ([]*RoleModel, error) {
var models []*RoleModel
err := impl.dbConnection.Model(&models).
Column("role_model.*").
Join("INNER JOIN role_group_role_mapping rgrm on rgrm.role_id=role_model.id").
Join("INNER JOIN role_group rg on rg.id=rgrm.role_group_id").
Where("rg.id = ?", roleGroupId).Select()
if err != nil {
impl.Logger.Error(err)
return models, err
}
return models, nil
}
func (impl UserAuthRepositoryImpl) GetRole(role string) (*RoleModel, error) {
var model RoleModel
err := impl.dbConnection.Model(&model).Where("role = ?", role).Select()
if err != nil {
impl.Logger.Error(err)
return &model, err
}
return &model, nil
}
func (impl UserAuthRepositoryImpl) GetAllRole() ([]RoleModel, error) {
var models []RoleModel
err := impl.dbConnection.Model(&models).Select()
if err != nil {
impl.Logger.Error(err)
return models, err
}
return models, nil
}
func (impl UserAuthRepositoryImpl) GetRolesByActionAndAccessType(action string, accessType string) ([]RoleModel, error) {
var models []RoleModel
var err error
if accessType == "" {
err = impl.dbConnection.Model(&models).Where("action = ?", action).
Where("access_type is NULL").
Select()
} else {
err = impl.dbConnection.Model(&models).Where("action = ?", action).
Where("access_type = ?", accessType).
Select()
}
if err != nil {
impl.Logger.Error("err in getting role by action", "err", err, "action", action, "accessType", accessType)
return models, err
}
return models, nil
}
func (impl UserAuthRepositoryImpl) GetRoleByFilterForAllTypes(roleFieldDto *bean4.RoleModelFieldsDto) (RoleModel, error) {
entity := roleFieldDto.Entity
action := roleFieldDto.Action
switch entity {
case bean2.CLUSTER_ENTITIY:
{
cluster, namespace, group, kind, resource := roleFieldDto.Cluster, roleFieldDto.Namespace, roleFieldDto.Group, roleFieldDto.Kind, roleFieldDto.Resource
return impl.GetRoleForClusterEntity(cluster, namespace, group, kind, resource, action)
}
case bean2.CHART_GROUP_ENTITY:
{
app, accessType := roleFieldDto.App, roleFieldDto.AccessType
return impl.GetRoleForChartGroupEntity(entity, app, action, accessType)
}
case bean2.EntityJobs:
{
team, app, env, workflow := roleFieldDto.Team, roleFieldDto.App, roleFieldDto.Env, roleFieldDto.Workflow
return impl.GetRoleForJobsEntity(entity, team, app, env, action, workflow)
}
default:
{
team, app, env, accessType, oldValues := roleFieldDto.Team, roleFieldDto.App, roleFieldDto.Env, roleFieldDto.AccessType, roleFieldDto.OldValues
return impl.GetRoleForOtherEntity(team, app, env, action, accessType, oldValues)
}
}
return RoleModel{}, nil
}
func (impl UserAuthRepositoryImpl) CreateUserRoleMapping(userRoleModel *UserRoleModel, tx *pg.Tx) (*UserRoleModel, error) {
err := tx.Insert(userRoleModel)
if err != nil {
impl.Logger.Error(err)
return userRoleModel, err
}
return userRoleModel, nil
}
func (impl UserAuthRepositoryImpl) GetUserRoleMappingByUserId(userId int32) ([]*UserRoleModel, error) {
var userRoleModels []*UserRoleModel
err := impl.dbConnection.Model(&userRoleModels).Where("user_id = ?", userId).Select()
if err != nil {
impl.Logger.Error(err)
return userRoleModels, err
}
return userRoleModels, nil
}
func (impl UserAuthRepositoryImpl) GetUserRoleMappingIdsByUserId(userId int32) ([]int, error) {
var Id []int
err := impl.dbConnection.Model().
Table("user_roles").
Column("user_roles.id").
Where("user_id = ?", userId).Select(&Id)
if err != nil {
impl.Logger.Errorw("error in GetUserRoleMappingIdsByUserId", "userId", userId, "err", err)
return nil, err
}
return Id, nil
}
func (impl UserAuthRepositoryImpl) GetUserRoleMappingIdsByUserIds(userIds []int32) ([]int, error) {
var Id []int
err := impl.dbConnection.Model().
Table("user_roles").
Column("user_roles.id").
Where("user_id in (?)", pg.In(userIds)).Select(&Id)
if err != nil {
impl.Logger.Errorw("error in GetUserRoleMappingsForUserIds", "userIds", userIds, "err", err)
return nil, err
}
return Id, nil
}
func (impl UserAuthRepositoryImpl) DeleteUserRoleMapping(userRoleModel *UserRoleModel, tx *pg.Tx) (bool, error) {
err := tx.Delete(userRoleModel)
if err != nil {
impl.Logger.Error(err)
return false, err
}
return true, nil
}
func (impl UserAuthRepositoryImpl) DeleteUserRoleMappingByIds(urmIds []int, tx *pg.Tx) error {
var userRoleModel *UserRoleModel
_, err := tx.Model(userRoleModel).Where("id in (?)", pg.In(urmIds)).Delete()
if err != nil {
impl.Logger.Error("err encountered in DeleteUserRoleMappingByIds", "urmIds", urmIds, "err", err)
return err
}
return nil
}
func (impl UserAuthRepositoryImpl) DeleteUserRoleByRoleId(roleId int, tx *pg.Tx) error {
var userRoleModel *UserRoleModel
_, err := tx.Model(userRoleModel).
Where("role_id = ?", roleId).Delete()
if err != nil {
impl.Logger.Error("err in deleting user role by role id", "err", err, "roleId", roleId)
return err
}
return nil
}
func (impl UserAuthRepositoryImpl) DeleteUserRoleByRoleIds(roleIds []int, tx *pg.Tx) error {
var userRoleModel *UserRoleModel
_, err := tx.Model(userRoleModel).
Where("role_id in (?)", pg.In(roleIds)).Delete()
if err != nil {
impl.Logger.Error("err in deleting user role by role id", "err", err, "roleIds", roleIds)
return err
}
return nil
}
func (impl UserAuthRepositoryImpl) CreateDefaultPoliciesForAllTypes(team, entityName, env, entity, cluster, namespace, group, kind, resource, actionType, accessType string, UserId int32) (bool, error, []bean3.Policy) {
//not using txn from parent caller because of conflicts in fetching of transactional save
dbConnection := impl.dbConnection
tx, err := dbConnection.Begin()
var policiesToBeAdded []bean3.Policy
if err != nil {
return false, err, policiesToBeAdded
}
// Rollback tx on error.
defer tx.Rollback()
//for START in Casbin Object
teamObj := team
envObj := env
appObj := entityName
if teamObj == "" {
teamObj = "*"
}
if envObj == "" {
envObj = "*"
}
if appObj == "" {
appObj = "*"
}
clusterObj := cluster
namespaceObj := namespace
groupObj := group
kindObj := kind
resourceObj := resource
if cluster == "" {
clusterObj = "*"
}
if namespace == "" {
namespaceObj = "*"
}
if group == "" {
groupObj = "*"
}
if kind == "" {
kindObj = "*"
}
if resource == "" {
resourceObj = "*"
}
rolePolicyDetails := RolePolicyDetails{
Team: team,
App: entityName,
Env: env,
TeamObj: teamObj,
EnvObj: envObj,
AppObj: appObj,
Entity: entity,
EntityName: entityName,
Cluster: cluster,
Namespace: namespace,
Group: group,
Kind: kind,
Resource: resource,
ClusterObj: clusterObj,
NamespaceObj: namespaceObj,
GroupObj: groupObj,
KindObj: kindObj,
ResourceObj: resourceObj,
}
//getting policies from db
PoliciesDb, err := impl.defaultAuthPolicyRepository.GetPolicyByRoleTypeAndEntity(bean2.RoleType(actionType), accessType, entity)
if err != nil {
return false, err, policiesToBeAdded
}
//getting updated policies
Policies, err := util.Tprintf(PoliciesDb, rolePolicyDetails)
if err != nil {
impl.Logger.Errorw("error in getting updated policies", "err", err, "roleType", bean2.RoleType(actionType), accessType)
return false, err, policiesToBeAdded
}
//for START in Casbin Object Ends Here
var policies bean.PolicyRequest
err = json.Unmarshal([]byte(Policies), &policies)
if err != nil {
impl.Logger.Errorw("decode err", "err", err)
return false, err, policiesToBeAdded
}
impl.Logger.Debugw("add policy request", "policies", policies)
policiesToBeAdded = append(policiesToBeAdded, policies.Data...)
//Creating ROLES
//getting roles from db
roleDb, err := impl.defaultAuthRoleRepository.GetRoleByRoleTypeAndEntityType(bean2.RoleType(actionType), accessType, entity)
if err != nil {
return false, err, nil
}
role, err := util.Tprintf(roleDb, rolePolicyDetails)
if err != nil {
impl.Logger.Errorw("error in getting updated role", "err", err, "roleType", bean2.RoleType(actionType))
return false, err, nil
}
//getting updated role
var roleData bean2.RoleData
err = json.Unmarshal([]byte(role), &roleData)
if err != nil {
impl.Logger.Errorw("decode err", "err", err)
return false, err, nil
}
_, err = impl.createRole(&roleData, UserId)
if err != nil && strings.Contains(err.Error(), "duplicate key value violates unique constraint") {
return false, err, nil
}
err = tx.Commit()
if err != nil {
return false, err, nil
}
return true, nil, policiesToBeAdded
}
func (impl UserAuthRepositoryImpl) CreateRolesWithAccessTypeAndEntity(team, entityName, env, entity, cluster, namespace, group, kind, resource, actionType, accessType string, UserId int32, role string) (bool, error) {
roleData := bean2.RoleData{
Role: role,
Entity: entity,
Team: team,
EntityName: entityName,
Environment: env,
Action: actionType,
AccessType: accessType,
Cluster: cluster,
Namespace: namespace,
Group: group,
Kind: kind,
Resource: resource,
}
_, err := impl.createRole(&roleData, UserId)
if err != nil && strings.Contains(err.Error(), "duplicate key value violates unique constraint") {
return false, err
}
return true, nil
}
func (impl UserAuthRepositoryImpl) CreateRoleForSuperAdminIfNotExists(tx *pg.Tx, UserId int32) (bool, error) {
transaction, err := impl.dbConnection.Begin()
if err != nil {
return false, err
}
//Creating ROLES
roleModel, err := impl.GetRoleByFilterForAllTypes(adapter.BuildSuperAdminRoleFieldsDto())
if err != nil && err != pg.ErrNoRows {
return false, err
}
if roleModel.Id == 0 || err == pg.ErrNoRows {
roleManager := "{\r\n \"role\": \"role:super-admin___\",\r\n \"casbinSubjects\": [\r\n \"role:super-admin___\"\r\n ],\r\n \"team\": \"\",\r\n \"entityName\": \"\",\r\n \"environment\": \"\",\r\n \"action\": \"super-admin\"\r\n}"
var roleManagerData bean2.RoleData
err = json.Unmarshal([]byte(roleManager), &roleManagerData)
if err != nil {
impl.Logger.Errorw("decode err", "err", err)
return false, err
}
_, err = impl.createRole(&roleManagerData, UserId)
if err != nil && strings.Contains(err.Error(), "duplicate key value violates unique constraint") {
return false, err
}
}
err = transaction.Commit()
if err != nil {
return false, err
}
return true, nil
}
func (impl UserAuthRepositoryImpl) createRole(roleData *bean2.RoleData, UserId int32) (bool, error) {
roleModel := &RoleModel{
Role: roleData.Role,
Entity: roleData.Entity,
Team: roleData.Team,
EntityName: roleData.EntityName,
Environment: roleData.Environment,
Action: roleData.Action,
AccessType: roleData.AccessType,
Cluster: roleData.Cluster,
Namespace: roleData.Namespace,
Group: roleData.Group,
Kind: roleData.Kind,
Resource: roleData.Resource,
AuditLog: sql.AuditLog{
CreatedBy: UserId,
CreatedOn: time.Now(),
UpdatedBy: UserId,
UpdatedOn: time.Now(),
},
}
roleModel, err := impl.CreateRole(roleModel)
if err != nil || roleModel == nil {
return false, err
}
return true, nil
}
func (impl UserAuthRepositoryImpl) SyncOrchestratorToCasbin(team string, entityName string, env string, tx *pg.Tx) (bool, error) {
//getting policies from db
triggerPoliciesDb, err := impl.defaultAuthPolicyRepository.GetPolicyByRoleTypeAndEntity(bean2.TRIGGER_TYPE, bean2.DEVTRON_APP, bean2.ENTITY_APPS)
if err != nil {
return false, err
}
viewPoliciesDb, err := impl.defaultAuthPolicyRepository.GetPolicyByRoleTypeAndEntity(bean2.VIEW_TYPE, bean2.DEVTRON_APP, bean2.ENTITY_APPS)
if err != nil {
return false, err
}
//for START in Casbin Object
teamObj := team
envObj := env
appObj := entityName
if teamObj == "" {
teamObj = "*"
}
if envObj == "" {
envObj = "*"
}
if appObj == "" {
appObj = "*"
}
policyDetails := RolePolicyDetails{
Team: team,
App: entityName,
Env: env,
TeamObj: teamObj,
EnvObj: envObj,
AppObj: appObj,
}
//getting updated trigger policies
triggerPolicies, err := util.Tprintf(triggerPoliciesDb, policyDetails)
if err != nil {
impl.Logger.Errorw("error in getting updated policies", "err", err, "roleType", bean2.TRIGGER_TYPE)
return false, err
}
//getting updated view policies
viewPolicies, err := util.Tprintf(viewPoliciesDb, policyDetails)
if err != nil {
impl.Logger.Errorw("error in getting updated policies", "err", err, "roleType", bean2.VIEW_TYPE)
return false, err
}
//for START in Casbin Object Ends Here
var policies []bean3.Policy
var policiesTrigger bean.PolicyRequest
err = json.Unmarshal([]byte(triggerPolicies), &policiesTrigger)
if err != nil {
impl.Logger.Errorw("decode err", "err", err)
return false, err
}
impl.Logger.Debugw("add policy request", "policies", policiesTrigger)
policies = append(policies, policiesTrigger.Data...)
var policiesView bean.PolicyRequest
err = json.Unmarshal([]byte(viewPolicies), &policiesView)
if err != nil {
impl.Logger.Errorw("decode err", "err", err)
return false, err
}
impl.Logger.Debugw("add policy request", "policies", policiesView)
policies = append(policies, policiesView.Data...)
casbin2.AddPolicy(policies)
return true, nil
}
func (impl UserAuthRepositoryImpl) UpdateTriggerPolicyForTerminalAccess() (err error) {
newTriggerPolicy := `{
"data": [
{
"type": "p",
"sub": "role:trigger_{{.Team}}_{{.Env}}_{{.App}}",
"res": "applications",
"act": "get",
"obj": "{{.TeamObj}}/{{.AppObj}}"
},
{
"type": "p",
"sub": "role:trigger_{{.Team}}_{{.Env}}_{{.App}}",
"res": "applications",
"act": "trigger",
"obj": "{{.TeamObj}}/{{.AppObj}}"
},
{
"type": "p",
"sub": "role:trigger_{{.Team}}_{{.Env}}_{{.App}}",
"res": "environment",
"act": "trigger",
"obj": "{{.EnvObj}}/{{.AppObj}}"
},
{
"type": "p",
"sub": "role:trigger_{{.Team}}_{{.Env}}_{{.App}}",
"res": "environment",
"act": "get",
"obj": "{{.EnvObj}}/{{.AppObj}}"
},
{
"type": "p",
"sub": "role:trigger_{{.Team}}_{{.Env}}_{{.App}}",
"res": "global-environment",
"act": "get",
"obj": "{{.EnvObj}}"
},
{
"type": "p",
"sub": "role:trigger_{{.Team}}_{{.Env}}_{{.App}}",
"res": "team",
"act": "get",
"obj": "{{.TeamObj}}"
},
{
"type": "p",
"sub": "role:trigger_{{.Team}}_{{.Env}}_{{.App}}",
"res": "terminal",
"act": "exec",
"obj": "{{.TeamObj}}/{{.EnvObj}}/{{.AppObj}}"
}
]
}`
err = impl.UpdateDefaultPolicyByRoleType(newTriggerPolicy, bean2.TRIGGER_TYPE)
if err != nil {
impl.Logger.Errorw("error in updating default policy for trigger role", "err", err)
return err
}
return nil
}
func (impl UserAuthRepositoryImpl) GetDefaultPolicyByRoleType(roleType bean2.RoleType) (policy string, err error) {
policy, err = impl.defaultAuthPolicyRepository.GetPolicyByRoleTypeAndEntity(roleType, bean2.DEVTRON_APP, bean2.ENTITY_APPS)
if err != nil {
return "", err
}
return policy, nil
}
func (impl UserAuthRepositoryImpl) UpdateDefaultPolicyByRoleType(newPolicy string, roleType bean2.RoleType) (err error) {
//getting all roles by role type
roles, err := impl.GetRolesByActionAndAccessType(string(roleType), "")
if err != nil {
impl.Logger.Errorw("error in getting roles for trigger action", "err", err)
return err
}
oldPolicy, err := impl.defaultAuthPolicyRepository.GetPolicyByRoleTypeAndEntity(roleType, bean2.DEVTRON_APP, bean2.ENTITY_APPS)
if err != nil {
return err
}
//updating new policy in db
_, err = impl.defaultAuthPolicyRepository.UpdatePolicyByRoleType(newPolicy, roleType)
if err != nil {
return err
}
//getting diff between new and old policy(policies deleted/added)
addedPolicies, deletedPolicies, err := impl.GetDiffBetweenPolicies(oldPolicy, newPolicy)
if err != nil {
impl.Logger.Errorw("error in getting diff between old and new policy", "err", err)
return err
}
var addedPolicyFinal bean.PolicyRequest
var deletedPolicyFinal bean.PolicyRequest
for _, role := range roles {
teamObj := role.Team
envObj := role.Environment
appObj := role.EntityName
if teamObj == "" {
teamObj = "*"
}
if envObj == "" {
envObj = "*"
}
if appObj == "" {
appObj = "*"
}
rolePolicyDetails := RolePolicyDetails{
Team: role.Team,
Env: role.Environment,
App: role.EntityName,
TeamObj: teamObj,
EnvObj: envObj,
AppObj: appObj,
}
if len(addedPolicies) > 0 {
addedPolicyReq, err := impl.GetUpdatedAddedOrDeletedPolicies(addedPolicies, rolePolicyDetails)
if err != nil {
impl.Logger.Errorw("error in getting updated added policies", "err", err)
return err
}
addedPolicyFinal.Data = append(addedPolicyFinal.Data, addedPolicyReq.Data...)
}
if len(deletedPolicies) > 0 {
deletedPolicyReq, err := impl.GetUpdatedAddedOrDeletedPolicies(deletedPolicies, rolePolicyDetails)
if err != nil {
impl.Logger.Errorw("error in getting updated deleted policies", "err", err)
return err
}
deletedPolicyFinal.Data = append(deletedPolicyFinal.Data, deletedPolicyReq.Data...)
}
}
//loading policy for safety
casbin2.LoadPolicy()
//updating all policies(for all roles) in casbin
if len(addedPolicyFinal.Data) > 0 {
casbin2.AddPolicy(addedPolicyFinal.Data)
}
if len(deletedPolicyFinal.Data) > 0 {
casbin2.RemovePolicy(deletedPolicyFinal.Data)
}
//loading policy for syncing orchestrator to casbin with newly added policies
casbin2.LoadPolicy()
return nil
}
func (impl UserAuthRepositoryImpl) GetDiffBetweenPolicies(oldPolicy string, newPolicy string) (addedPolicies []bean3.Policy, deletedPolicies []bean3.Policy, err error) {
var oldPolicyObj bean.PolicyRequest
err = json.Unmarshal([]byte(oldPolicy), &oldPolicyObj)
if err != nil {
impl.Logger.Errorw("error in un-marshaling old policy", "err", err)
return addedPolicies, deletedPolicies, err
}
var newPolicyObj bean.PolicyRequest
err = json.Unmarshal([]byte(newPolicy), &newPolicyObj)
if err != nil {
impl.Logger.Errorw("error in un-marshaling new policy", "err", err)
return addedPolicies, deletedPolicies, err
}
oldPolicyMap := make(map[string]bool)
for _, oldPolicyData := range oldPolicyObj.Data {
//converting all fields of data to a string
data := fmt.Sprintf("type:%s,sub:%s,res:%s,act:%s,obj:%s", oldPolicyData.Type, oldPolicyData.Sub, oldPolicyData.Res, oldPolicyData.Act, oldPolicyData.Obj)
//creating entry for data, keeping false because if present in new policy
//then will be set to true and will not be included in deletedPolicies
oldPolicyMap[data] = false
}
for _, newPolicyData := range newPolicyObj.Data {
//converting all fields of data to a string
data := fmt.Sprintf("type:%s,sub:%s,res:%s,act:%s,obj:%s", newPolicyData.Type, newPolicyData.Sub, newPolicyData.Res, newPolicyData.Act, newPolicyData.Obj)
if _, ok := oldPolicyMap[data]; !ok {
//data not present in old policy, to be included in addedPolicies
addedPolicies = append(addedPolicies, newPolicyData)
} else {
//data present in old policy; set old policy to true, so it does not get included in deletedPolicies
oldPolicyMap[data] = true
}
}
//check oldPolicies for updating deletedPolicies
for _, oldPolicyData := range oldPolicyObj.Data {
data := fmt.Sprintf("type:%s,sub:%s,res:%s,act:%s,obj:%s", oldPolicyData.Type, oldPolicyData.Sub, oldPolicyData.Res, oldPolicyData.Act, oldPolicyData.Obj)
if presentInNew := oldPolicyMap[data]; !presentInNew {
//data not present in old policy, to be included in addedPolicies
deletedPolicies = append(deletedPolicies, oldPolicyData)
}
}
return addedPolicies, deletedPolicies, nil
}
func (impl UserAuthRepositoryImpl) GetUpdatedAddedOrDeletedPolicies(policies []bean3.Policy, rolePolicyDetails RolePolicyDetails) (bean.PolicyRequest, error) {
var policyResp bean.PolicyRequest
var policyReq bean.PolicyRequest
policyReq.Data = policies
policy, err := json.Marshal(policyReq)
if err != nil {
impl.Logger.Errorw("error in marshaling policy", "err", err)
return policyResp, err
}
//getting updated policy
updatedPolicy, err := util.Tprintf(string(policy), rolePolicyDetails)
if err != nil {
impl.Logger.Errorw("error in getting updated policy", "err", err)
return policyResp, err
}
err = json.Unmarshal([]byte(updatedPolicy), &policyResp)
if err != nil {
impl.Logger.Errorw("error in un-marshaling policy", "err", err)
return policyResp, err
}
return policyResp, nil
}
func (impl UserAuthRepositoryImpl) GetRolesForEnvironment(envName, envIdentifier string) ([]*RoleModel, error) {
var roles []*RoleModel
err := impl.dbConnection.Model(&roles).WhereOr("environment = ?", envName).
WhereOr("environment = ?", envIdentifier).Select()
if err != nil {
impl.Logger.Errorw("error in getting roles for environment", "err", err, "envName", envName, "envIdentifier", envIdentifier)
return nil, err
}
return roles, nil
}
func (impl UserAuthRepositoryImpl) GetRolesForProject(teamName string) ([]*RoleModel, error) {
var roles []*RoleModel
err := impl.dbConnection.Model(&roles).Where("team = ?", teamName).Select()
if err != nil {
impl.Logger.Errorw("error in getting roles for team", "err", err, "teamName", teamName)
return nil, err
}
return roles, nil
}
func (impl UserAuthRepositoryImpl) GetRolesForApp(appName string) ([]*RoleModel, error) {
var roles []*RoleModel
err := impl.dbConnection.Model(&roles).
Where("(entity is NULL) OR (entity = ? AND access_type = ?) OR (entity = ?)", bean2.ENTITY_APPS, bean2.DEVTRON_APP, bean2.EntityJobs).
Where("entity_name = ?", appName).
Select()
if err != nil {
impl.Logger.Errorw("error in getting roles for app", "err", err, "appName", appName)
return nil, err
}
return roles, nil
}
func (impl UserAuthRepositoryImpl) GetRolesForChartGroup(chartGroupName string) ([]*RoleModel, error) {
var roles []*RoleModel
err := impl.dbConnection.Model(&roles).Where("entity = ?", bean2.CHART_GROUP_TYPE).
Where("entity_name = ?", chartGroupName).Select()
if err != nil {
impl.Logger.Errorw("error in getting roles for chart group", "err", err, "chartGroupName", chartGroupName)
return nil, err
}
return roles, nil
}
func (impl UserAuthRepositoryImpl) DeleteRole(role *RoleModel, tx *pg.Tx) error {
err := tx.Delete(role)
if err != nil {
impl.Logger.Errorw("error in deleting role", "err", err, "role", role)
return err
}
return nil
}
func (impl UserAuthRepositoryImpl) DeleteRolesByIds(roleIds []int, tx *pg.Tx) error {
var models []RoleModel
_, err := tx.Model(&models).Where("id in (?)", pg.In(roleIds)).Delete()
if err != nil {
impl.Logger.Errorw("error in deleting roles by roleIds", "err", err, "roles", roleIds)
return err
}
return nil
}
func (impl UserAuthRepositoryImpl) GetRolesByUserIdAndEntityType(userId int32, entityType string) ([]*RoleModel, error) {
var models []*RoleModel
err := impl.dbConnection.Model(&models).
Column("role_model.*").
Join("INNER JOIN user_roles ur on ur.role_id=role_model.id").
Where("role_model.entity = ?", entityType).
Where("ur.user_id = ?", userId).Select()
if err != nil {
impl.Logger.Error(err)
return models, err
}
return models, nil
}
func (impl UserAuthRepositoryImpl) GetRolesForWorkflow(workflow, entityName string) ([]*RoleModel, error) {
var roles []*RoleModel
err := impl.dbConnection.Model(&roles).Where("workflow = ?", workflow).
Where("entity_name = ?", entityName).
Select()
if err != nil {
impl.Logger.Errorw("error in getting roles for team", "err", err, "workflow", workflow)
return nil, err
}
return roles, nil
}
func (impl UserAuthRepositoryImpl) GetRoleForClusterEntity(cluster, namespace, group, kind, resource, action string) (RoleModel, error) {
var model RoleModel
var queryParams []interface{}
query := "SELECT * FROM roles WHERE entity = ? "
queryParams = append(queryParams, bean2.CLUSTER_ENTITIY)
var err error
if len(cluster) > 0 {
query += " and cluster = ? "
queryParams = append(queryParams, cluster)
} else {
query += " and cluster IS NULL "
}
if len(namespace) > 0 {
query += " and namespace = ? "
queryParams = append(queryParams, namespace)
} else {
query += " and namespace IS NULL "
}
if len(group) > 0 {
query += " and \"group\"= ? "
queryParams = append(queryParams, group)
} else {
query += " and \"group\" IS NULL "
}
if len(kind) > 0 {
query += " and kind = ? "
queryParams = append(queryParams, kind)
} else {
query += " and kind IS NULL "
}
if len(resource) > 0 {
query += " and resource = ? "