-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
role.go
1768 lines (1557 loc) · 54.1 KB
/
role.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 2020 Gravitational, 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.
*/
package types
import (
"encoding/json"
"fmt"
"time"
"github.com/gogo/protobuf/proto"
"github.com/gravitational/trace"
"golang.org/x/exp/slices"
"github.com/gravitational/teleport/api/constants"
"github.com/gravitational/teleport/api/defaults"
"github.com/gravitational/teleport/api/types/wrappers"
"github.com/gravitational/teleport/api/utils"
"github.com/gravitational/teleport/api/utils/keys"
)
type OnSessionLeaveAction string
const (
// OnSessionLeaveTerminate is a moderated sessions policy constant that terminates
// a session once the require policy is no longer fulfilled.
OnSessionLeaveTerminate OnSessionLeaveAction = "terminate"
// OnSessionLeaveTerminate is a moderated sessions policy constant that pauses
// a session once the require policies is no longer fulfilled. It is resumed
// once the requirements are fulfilled again.
OnSessionLeavePause OnSessionLeaveAction = "pause"
)
// Role contains a set of permissions or settings
type Role interface {
// Resource provides common resource methods.
Resource
// SetMetadata sets role metadata
SetMetadata(meta Metadata)
// GetOptions gets role options.
GetOptions() RoleOptions
// SetOptions sets role options
SetOptions(opt RoleOptions)
// GetCreateDatabaseUserOption returns value of "create_db_user" option.
GetCreateDatabaseUserOption() bool
// GetLogins gets *nix system logins for allow or deny condition.
GetLogins(RoleConditionType) []string
// SetLogins sets *nix system logins for allow or deny condition.
SetLogins(RoleConditionType, []string)
// GetNamespaces gets a list of namespaces this role is allowed or denied access to.
GetNamespaces(RoleConditionType) []string
// SetNamespaces sets a list of namespaces this role is allowed or denied access to.
SetNamespaces(RoleConditionType, []string)
// GetLabelMatchers gets the LabelMatchers that match labels of resources of
// type [kind] this role is allowed or denied access to.
GetLabelMatchers(rct RoleConditionType, kind string) (LabelMatchers, error)
// SetLabelMatchers sets the LabelMatchers that match labels of resources of
// type [kind] this role is allowed or denied access to.
SetLabelMatchers(rct RoleConditionType, kind string, labelMatchers LabelMatchers) error
// GetNodeLabels gets the map of node labels this role is allowed or denied access to.
GetNodeLabels(RoleConditionType) Labels
// SetNodeLabels sets the map of node labels this role is allowed or denied access to.
SetNodeLabels(RoleConditionType, Labels)
// GetAppLabels gets the map of app labels this role is allowed or denied access to.
GetAppLabels(RoleConditionType) Labels
// SetAppLabels sets the map of app labels this role is allowed or denied access to.
SetAppLabels(RoleConditionType, Labels)
// GetClusterLabels gets the map of cluster labels this role is allowed or denied access to.
GetClusterLabels(RoleConditionType) Labels
// SetClusterLabels sets the map of cluster labels this role is allowed or denied access to.
SetClusterLabels(RoleConditionType, Labels)
// GetKubernetesLabels gets the map of kubernetes labels this role is
// allowed or denied access to.
GetKubernetesLabels(RoleConditionType) Labels
// SetKubernetesLabels sets the map of kubernetes labels this role is
// allowed or denied access to.
SetKubernetesLabels(RoleConditionType, Labels)
// GetRules gets all allow or deny rules.
GetRules(rct RoleConditionType) []Rule
// SetRules sets an allow or deny rule.
SetRules(rct RoleConditionType, rules []Rule)
// GetKubeGroups returns kubernetes groups
GetKubeGroups(RoleConditionType) []string
// SetKubeGroups sets kubernetes groups for allow or deny condition.
SetKubeGroups(RoleConditionType, []string)
// GetKubeUsers returns kubernetes users to impersonate
GetKubeUsers(RoleConditionType) []string
// SetKubeUsers sets kubernetes users to impersonate for allow or deny condition.
SetKubeUsers(RoleConditionType, []string)
// GetKubeResources returns the Kubernetes Resources this role grants
// access to.
GetKubeResources(rct RoleConditionType) []KubernetesResource
// SetKubeResources configures the Kubernetes Resources for the RoleConditionType.
SetKubeResources(rct RoleConditionType, pods []KubernetesResource)
// GetAccessRequestConditions gets allow/deny conditions for access requests.
GetAccessRequestConditions(RoleConditionType) AccessRequestConditions
// SetAccessRequestConditions sets allow/deny conditions for access requests.
SetAccessRequestConditions(RoleConditionType, AccessRequestConditions)
// GetAccessReviewConditions gets allow/deny conditions for access review.
GetAccessReviewConditions(RoleConditionType) AccessReviewConditions
// SetAccessReviewConditions sets allow/deny conditions for access review.
SetAccessReviewConditions(RoleConditionType, AccessReviewConditions)
// GetDatabaseLabels gets the map of db labels this role is allowed or denied access to.
GetDatabaseLabels(RoleConditionType) Labels
// SetDatabaseLabels sets the map of db labels this role is allowed or denied access to.
SetDatabaseLabels(RoleConditionType, Labels)
// GetDatabaseNames gets a list of database names this role is allowed or denied access to.
GetDatabaseNames(RoleConditionType) []string
// SetDatabaseNames sets a list of database names this role is allowed or denied access to.
SetDatabaseNames(RoleConditionType, []string)
// GetDatabaseUsers gets a list of database users this role is allowed or denied access to.
GetDatabaseUsers(RoleConditionType) []string
// SetDatabaseUsers sets a list of database users this role is allowed or denied access to.
SetDatabaseUsers(RoleConditionType, []string)
// GetDatabaseRoles gets a list of database roles for auto-provisioned users.
GetDatabaseRoles(RoleConditionType) []string
// SetDatabaseRoles sets a list of database roles for auto-provisioned users.
SetDatabaseRoles(RoleConditionType, []string)
// GetImpersonateConditions returns conditions this role is allowed or denied to impersonate.
GetImpersonateConditions(rct RoleConditionType) ImpersonateConditions
// SetImpersonateConditions sets conditions this role is allowed or denied to impersonate.
SetImpersonateConditions(rct RoleConditionType, cond ImpersonateConditions)
// GetAWSRoleARNs returns a list of AWS role ARNs this role is allowed to assume.
GetAWSRoleARNs(RoleConditionType) []string
// SetAWSRoleARNs sets a list of AWS role ARNs this role is allowed to assume.
SetAWSRoleARNs(RoleConditionType, []string)
// GetAzureIdentities returns a list of Azure identities this role is allowed to assume.
GetAzureIdentities(RoleConditionType) []string
// SetAzureIdentities sets a list of Azure identities this role is allowed to assume.
SetAzureIdentities(RoleConditionType, []string)
// GetGCPServiceAccounts returns a list of GCP service accounts this role is allowed to assume.
GetGCPServiceAccounts(RoleConditionType) []string
// SetGCPServiceAccounts sets a list of GCP service accounts this role is allowed to assume.
SetGCPServiceAccounts(RoleConditionType, []string)
// GetWindowsDesktopLabels gets the Windows desktop labels this role
// is allowed or denied access to.
GetWindowsDesktopLabels(RoleConditionType) Labels
// SetWindowsDesktopLabels sets the Windows desktop labels this role
// is allowed or denied access to.
SetWindowsDesktopLabels(RoleConditionType, Labels)
// GetWindowsLogins gets Windows desktop logins for allow or deny condition.
GetWindowsLogins(RoleConditionType) []string
// SetWindowsLogins sets Windows desktop logins for allow or deny condition.
SetWindowsLogins(RoleConditionType, []string)
// GetSessionRequirePolicies returns the RBAC required policies for a session.
GetSessionRequirePolicies() []*SessionRequirePolicy
// SetSessionRequirePolicies sets the RBAC required policies for a session.
SetSessionRequirePolicies([]*SessionRequirePolicy)
// GetSessionJoinPolicies returns the RBAC join policies for a session.
GetSessionJoinPolicies() []*SessionJoinPolicy
// SetSessionJoinPolicies sets the RBAC join policies for a session.
SetSessionJoinPolicies([]*SessionJoinPolicy)
// GetSessionPolicySet returns the RBAC policy set for a role.
GetSessionPolicySet() SessionTrackerPolicySet
// GetSearchAsRoles returns the list of extra roles which should apply to a
// user while they are searching for resources as part of a Resource Access
// Request, and defines the underlying roles which will be requested as part
// of any Resource Access Request.
GetSearchAsRoles(RoleConditionType) []string
// SetSearchAsRoles sets the list of extra roles which should apply to a
// user while they are searching for resources as part of a Resource Access
// Request, and defines the underlying roles which will be requested as part
// of any Resource Access Request.
SetSearchAsRoles(RoleConditionType, []string)
// GetPreviewAsRoles returns the list of extra roles which should apply to a
// reviewer while they are viewing a Resource Access Request for the
// purposes of viewing details such as the hostname and labels of requested
// resources.
GetPreviewAsRoles(RoleConditionType) []string
// SetPreviewAsRoles sets the list of extra roles which should apply to a
// reviewer while they are viewing a Resource Access Request for the
// purposes of viewing details such as the hostname and labels of requested
// resources.
SetPreviewAsRoles(RoleConditionType, []string)
// GetHostGroups gets the list of groups this role is put in when users are provisioned
GetHostGroups(RoleConditionType) []string
// SetHostGroups sets the list of groups this role is put in when users are provisioned
SetHostGroups(RoleConditionType, []string)
// GetDesktopGroups gets the list of groups this role is put in when desktop users are provisioned
GetDesktopGroups(RoleConditionType) []string
// SetDesktopGroups sets the list of groups this role is put in when desktop users are provisioned
SetDesktopGroups(RoleConditionType, []string)
// GetHostSudoers gets the list of sudoers entries for the role
GetHostSudoers(RoleConditionType) []string
// SetHostSudoers sets the list of sudoers entries for the role
SetHostSudoers(RoleConditionType, []string)
// GetPrivateKeyPolicy returns the private key policy enforced for this role.
GetPrivateKeyPolicy() keys.PrivateKeyPolicy
// GetDatabaseServiceLabels gets the map of db service labels this role is allowed or denied access to.
GetDatabaseServiceLabels(RoleConditionType) Labels
// SetDatabaseServiceLabels sets the map of db service labels this role is allowed or denied access to.
SetDatabaseServiceLabels(RoleConditionType, Labels)
// GetGroupLabels gets the map of group labels this role is allowed or denied access to.
GetGroupLabels(RoleConditionType) Labels
// SetGroupLabels sets the map of group labels this role is allowed or denied access to.
SetGroupLabels(RoleConditionType, Labels)
}
// NewRole constructs new standard V6 role.
// This creates a V6 role with V4+ RBAC semantics.
func NewRole(name string, spec RoleSpecV6) (Role, error) {
role := RoleV6{
Version: V6,
Metadata: Metadata{
Name: name,
},
Spec: spec,
}
if err := role.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
return &role, nil
}
// RoleConditionType specifies if it's an allow rule (true) or deny rule (false).
type RoleConditionType bool
const (
// Allow is the set of conditions that allow access.
Allow RoleConditionType = true
// Deny is the set of conditions that prevent access.
Deny RoleConditionType = false
)
// GetVersion returns resource version
func (r *RoleV6) GetVersion() string {
return r.Version
}
// GetKind returns resource kind
func (r *RoleV6) GetKind() string {
return r.Kind
}
// GetSubKind returns resource sub kind
func (r *RoleV6) GetSubKind() string {
return r.SubKind
}
// SetSubKind sets resource subkind
func (r *RoleV6) SetSubKind(s string) {
r.SubKind = s
}
// GetResourceID returns resource ID
func (r *RoleV6) GetResourceID() int64 {
return r.Metadata.ID
}
// SetResourceID sets resource ID
func (r *RoleV6) SetResourceID(id int64) {
r.Metadata.ID = id
}
// SetExpiry sets expiry time for the object.
func (r *RoleV6) SetExpiry(expires time.Time) {
r.Metadata.SetExpiry(expires)
}
// Expiry returns the expiry time for the object.
func (r *RoleV6) Expiry() time.Time {
return r.Metadata.Expiry()
}
// SetName sets the role name and is a shortcut for SetMetadata().Name.
func (r *RoleV6) SetName(s string) {
r.Metadata.Name = s
}
// GetName gets the role name and is a shortcut for GetMetadata().Name.
func (r *RoleV6) GetName() string {
return r.Metadata.Name
}
// GetMetadata returns role metadata.
func (r *RoleV6) GetMetadata() Metadata {
return r.Metadata
}
// SetMetadata sets role metadata
func (r *RoleV6) SetMetadata(meta Metadata) {
r.Metadata = meta
}
// GetOptions gets role options.
func (r *RoleV6) GetOptions() RoleOptions {
return r.Spec.Options
}
// SetOptions sets role options.
func (r *RoleV6) SetOptions(options RoleOptions) {
r.Spec.Options = options
}
// GetCreateDatabaseUserOption returns value of "create_db_user" option.
func (r *RoleV6) GetCreateDatabaseUserOption() bool {
if r.Spec.Options.CreateDatabaseUser == nil {
return false
}
return r.Spec.Options.CreateDatabaseUser.Value
}
// GetLogins gets system logins for allow or deny condition.
func (r *RoleV6) GetLogins(rct RoleConditionType) []string {
if rct == Allow {
return r.Spec.Allow.Logins
}
return r.Spec.Deny.Logins
}
// SetLogins sets system logins for allow or deny condition.
func (r *RoleV6) SetLogins(rct RoleConditionType, logins []string) {
lcopy := utils.CopyStrings(logins)
if rct == Allow {
r.Spec.Allow.Logins = lcopy
} else {
r.Spec.Deny.Logins = lcopy
}
}
// GetKubeGroups returns kubernetes groups
func (r *RoleV6) GetKubeGroups(rct RoleConditionType) []string {
if rct == Allow {
return r.Spec.Allow.KubeGroups
}
return r.Spec.Deny.KubeGroups
}
// SetKubeGroups sets kubernetes groups for allow or deny condition.
func (r *RoleV6) SetKubeGroups(rct RoleConditionType, groups []string) {
lcopy := utils.CopyStrings(groups)
if rct == Allow {
r.Spec.Allow.KubeGroups = lcopy
} else {
r.Spec.Deny.KubeGroups = lcopy
}
}
// GetKubeResources returns the Kubernetes Resources this role grants
// access to.
func (r *RoleV6) GetKubeResources(rct RoleConditionType) []KubernetesResource {
if rct == Allow {
return r.Spec.Allow.KubernetesResources
}
return r.Spec.Deny.KubernetesResources
}
// SetKubeResources configures the Kubernetes Resources for the RoleConditionType.
func (r *RoleV6) SetKubeResources(rct RoleConditionType, pods []KubernetesResource) {
if rct == Allow {
r.Spec.Allow.KubernetesResources = pods
} else {
r.Spec.Deny.KubernetesResources = pods
}
}
// GetKubeUsers returns kubernetes users
func (r *RoleV6) GetKubeUsers(rct RoleConditionType) []string {
if rct == Allow {
return r.Spec.Allow.KubeUsers
}
return r.Spec.Deny.KubeUsers
}
// SetKubeUsers sets kubernetes user for allow or deny condition.
func (r *RoleV6) SetKubeUsers(rct RoleConditionType, users []string) {
lcopy := utils.CopyStrings(users)
if rct == Allow {
r.Spec.Allow.KubeUsers = lcopy
} else {
r.Spec.Deny.KubeUsers = lcopy
}
}
// GetAccessRequestConditions gets conditions for access requests.
func (r *RoleV6) GetAccessRequestConditions(rct RoleConditionType) AccessRequestConditions {
cond := r.Spec.Deny.Request
if rct == Allow {
cond = r.Spec.Allow.Request
}
if cond == nil {
return AccessRequestConditions{}
}
return *cond
}
// SetAccessRequestConditions sets allow/deny conditions for access requests.
func (r *RoleV6) SetAccessRequestConditions(rct RoleConditionType, cond AccessRequestConditions) {
if rct == Allow {
r.Spec.Allow.Request = &cond
} else {
r.Spec.Deny.Request = &cond
}
}
// GetAccessReviewConditions gets conditions for access reviews.
func (r *RoleV6) GetAccessReviewConditions(rct RoleConditionType) AccessReviewConditions {
cond := r.Spec.Deny.ReviewRequests
if rct == Allow {
cond = r.Spec.Allow.ReviewRequests
}
if cond == nil {
return AccessReviewConditions{}
}
return *cond
}
// SetAccessReviewConditions sets allow/deny conditions for access reviews.
func (r *RoleV6) SetAccessReviewConditions(rct RoleConditionType, cond AccessReviewConditions) {
if rct == Allow {
r.Spec.Allow.ReviewRequests = &cond
} else {
r.Spec.Deny.ReviewRequests = &cond
}
}
// GetNamespaces gets a list of namespaces this role is allowed or denied access to.
func (r *RoleV6) GetNamespaces(rct RoleConditionType) []string {
if rct == Allow {
return r.Spec.Allow.Namespaces
}
return r.Spec.Deny.Namespaces
}
// SetNamespaces sets a list of namespaces this role is allowed or denied access to.
func (r *RoleV6) SetNamespaces(rct RoleConditionType, namespaces []string) {
ncopy := utils.CopyStrings(namespaces)
if rct == Allow {
r.Spec.Allow.Namespaces = ncopy
} else {
r.Spec.Deny.Namespaces = ncopy
}
}
// GetNodeLabels gets the map of node labels this role is allowed or denied access to.
func (r *RoleV6) GetNodeLabels(rct RoleConditionType) Labels {
if rct == Allow {
return r.Spec.Allow.NodeLabels
}
return r.Spec.Deny.NodeLabels
}
// SetNodeLabels sets the map of node labels this role is allowed or denied access to.
func (r *RoleV6) SetNodeLabels(rct RoleConditionType, labels Labels) {
if rct == Allow {
r.Spec.Allow.NodeLabels = labels.Clone()
} else {
r.Spec.Deny.NodeLabels = labels.Clone()
}
}
// GetAppLabels gets the map of app labels this role is allowed or denied access to.
func (r *RoleV6) GetAppLabels(rct RoleConditionType) Labels {
if rct == Allow {
return r.Spec.Allow.AppLabels
}
return r.Spec.Deny.AppLabels
}
// SetAppLabels sets the map of node labels this role is allowed or denied access to.
func (r *RoleV6) SetAppLabels(rct RoleConditionType, labels Labels) {
if rct == Allow {
r.Spec.Allow.AppLabels = labels.Clone()
} else {
r.Spec.Deny.AppLabels = labels.Clone()
}
}
// GetClusterLabels gets the map of cluster labels this role is allowed or denied access to.
func (r *RoleV6) GetClusterLabels(rct RoleConditionType) Labels {
if rct == Allow {
return r.Spec.Allow.ClusterLabels
}
return r.Spec.Deny.ClusterLabels
}
// SetClusterLabels sets the map of cluster labels this role is allowed or denied access to.
func (r *RoleV6) SetClusterLabels(rct RoleConditionType, labels Labels) {
if rct == Allow {
r.Spec.Allow.ClusterLabels = labels.Clone()
} else {
r.Spec.Deny.ClusterLabels = labels.Clone()
}
}
// GetKubernetesLabels gets the map of app labels this role is allowed or denied access to.
func (r *RoleV6) GetKubernetesLabels(rct RoleConditionType) Labels {
if rct == Allow {
return r.Spec.Allow.KubernetesLabels
}
return r.Spec.Deny.KubernetesLabels
}
// SetKubernetesLabels sets the map of node labels this role is allowed or denied access to.
func (r *RoleV6) SetKubernetesLabels(rct RoleConditionType, labels Labels) {
if rct == Allow {
r.Spec.Allow.KubernetesLabels = labels.Clone()
} else {
r.Spec.Deny.KubernetesLabels = labels.Clone()
}
}
// GetDatabaseServiceLabels gets the map of db service labels this role is allowed or denied access to.
func (r *RoleV6) GetDatabaseServiceLabels(rct RoleConditionType) Labels {
if rct == Allow {
return r.Spec.Allow.DatabaseServiceLabels
}
return r.Spec.Deny.DatabaseServiceLabels
}
// SetDatabaseServiceLabels sets the map of db service labels this role is allowed or denied access to.
func (r *RoleV6) SetDatabaseServiceLabels(rct RoleConditionType, labels Labels) {
if rct == Allow {
r.Spec.Allow.DatabaseServiceLabels = labels.Clone()
} else {
r.Spec.Deny.DatabaseServiceLabels = labels.Clone()
}
}
// GetDatabaseLabels gets the map of db labels this role is allowed or denied access to.
func (r *RoleV6) GetDatabaseLabels(rct RoleConditionType) Labels {
if rct == Allow {
return r.Spec.Allow.DatabaseLabels
}
return r.Spec.Deny.DatabaseLabels
}
// SetDatabaseLabels sets the map of db labels this role is allowed or denied access to.
func (r *RoleV6) SetDatabaseLabels(rct RoleConditionType, labels Labels) {
if rct == Allow {
r.Spec.Allow.DatabaseLabels = labels.Clone()
} else {
r.Spec.Deny.DatabaseLabels = labels.Clone()
}
}
// GetDatabaseNames gets a list of database names this role is allowed or denied access to.
func (r *RoleV6) GetDatabaseNames(rct RoleConditionType) []string {
if rct == Allow {
return r.Spec.Allow.DatabaseNames
}
return r.Spec.Deny.DatabaseNames
}
// SetDatabaseNames sets a list of database names this role is allowed or denied access to.
func (r *RoleV6) SetDatabaseNames(rct RoleConditionType, values []string) {
if rct == Allow {
r.Spec.Allow.DatabaseNames = values
} else {
r.Spec.Deny.DatabaseNames = values
}
}
// GetDatabaseUsers gets a list of database users this role is allowed or denied access to.
func (r *RoleV6) GetDatabaseUsers(rct RoleConditionType) []string {
if rct == Allow {
return r.Spec.Allow.DatabaseUsers
}
return r.Spec.Deny.DatabaseUsers
}
// SetDatabaseUsers sets a list of database users this role is allowed or denied access to.
func (r *RoleV6) SetDatabaseUsers(rct RoleConditionType, values []string) {
if rct == Allow {
r.Spec.Allow.DatabaseUsers = values
} else {
r.Spec.Deny.DatabaseUsers = values
}
}
// GetDatabaseRoles gets a list of database roles for auto-provisioned users.
func (r *RoleV6) GetDatabaseRoles(rct RoleConditionType) []string {
if rct == Allow {
return r.Spec.Allow.DatabaseRoles
}
return r.Spec.Deny.DatabaseRoles
}
// SetDatabaseRoles sets a list of database roles for auto-provisioned users.
func (r *RoleV6) SetDatabaseRoles(rct RoleConditionType, values []string) {
if rct == Allow {
r.Spec.Allow.DatabaseRoles = values
} else {
r.Spec.Deny.DatabaseRoles = values
}
}
// GetImpersonateConditions returns conditions this role is allowed or denied to impersonate.
func (r *RoleV6) GetImpersonateConditions(rct RoleConditionType) ImpersonateConditions {
cond := r.Spec.Deny.Impersonate
if rct == Allow {
cond = r.Spec.Allow.Impersonate
}
if cond == nil {
return ImpersonateConditions{}
}
return *cond
}
// SetImpersonateConditions sets conditions this role is allowed or denied to impersonate.
func (r *RoleV6) SetImpersonateConditions(rct RoleConditionType, cond ImpersonateConditions) {
if rct == Allow {
r.Spec.Allow.Impersonate = &cond
} else {
r.Spec.Deny.Impersonate = &cond
}
}
// GetAWSRoleARNs returns a list of AWS role ARNs this role is allowed to impersonate.
func (r *RoleV6) GetAWSRoleARNs(rct RoleConditionType) []string {
if rct == Allow {
return r.Spec.Allow.AWSRoleARNs
}
return r.Spec.Deny.AWSRoleARNs
}
// SetAWSRoleARNs sets a list of AWS role ARNs this role is allowed to impersonate.
func (r *RoleV6) SetAWSRoleARNs(rct RoleConditionType, arns []string) {
if rct == Allow {
r.Spec.Allow.AWSRoleARNs = arns
} else {
r.Spec.Deny.AWSRoleARNs = arns
}
}
// GetAzureIdentities returns a list of Azure identities this role is allowed to assume.
func (r *RoleV6) GetAzureIdentities(rct RoleConditionType) []string {
if rct == Allow {
return r.Spec.Allow.AzureIdentities
}
return r.Spec.Deny.AzureIdentities
}
// SetAzureIdentities sets a list of Azure identities this role is allowed to assume.
func (r *RoleV6) SetAzureIdentities(rct RoleConditionType, identities []string) {
if rct == Allow {
r.Spec.Allow.AzureIdentities = identities
} else {
r.Spec.Deny.AzureIdentities = identities
}
}
// GetGCPServiceAccounts returns a list of GCP service accounts this role is allowed to assume.
func (r *RoleV6) GetGCPServiceAccounts(rct RoleConditionType) []string {
if rct == Allow {
return r.Spec.Allow.GCPServiceAccounts
}
return r.Spec.Deny.GCPServiceAccounts
}
// SetGCPServiceAccounts sets a list of GCP service accounts this role is allowed to assume.
func (r *RoleV6) SetGCPServiceAccounts(rct RoleConditionType, accounts []string) {
if rct == Allow {
r.Spec.Allow.GCPServiceAccounts = accounts
} else {
r.Spec.Deny.GCPServiceAccounts = accounts
}
}
// GetWindowsDesktopLabels gets the desktop labels this role is allowed or denied access to.
func (r *RoleV6) GetWindowsDesktopLabels(rct RoleConditionType) Labels {
if rct == Allow {
return r.Spec.Allow.WindowsDesktopLabels
}
return r.Spec.Deny.WindowsDesktopLabels
}
// SetWindowsDesktopLabels sets the desktop labels this role is allowed or denied access to.
func (r *RoleV6) SetWindowsDesktopLabels(rct RoleConditionType, labels Labels) {
if rct == Allow {
r.Spec.Allow.WindowsDesktopLabels = labels.Clone()
} else {
r.Spec.Deny.WindowsDesktopLabels = labels.Clone()
}
}
// GetWindowsLogins gets Windows desktop logins for the role's allow or deny condition.
func (r *RoleV6) GetWindowsLogins(rct RoleConditionType) []string {
if rct == Allow {
return r.Spec.Allow.WindowsDesktopLogins
}
return r.Spec.Deny.WindowsDesktopLogins
}
// SetWindowsLogins sets Windows desktop logins for the role's allow or deny condition.
func (r *RoleV6) SetWindowsLogins(rct RoleConditionType, logins []string) {
lcopy := utils.CopyStrings(logins)
if rct == Allow {
r.Spec.Allow.WindowsDesktopLogins = lcopy
} else {
r.Spec.Deny.WindowsDesktopLogins = lcopy
}
}
// GetRules gets all allow or deny rules.
func (r *RoleV6) GetRules(rct RoleConditionType) []Rule {
if rct == Allow {
return r.Spec.Allow.Rules
}
return r.Spec.Deny.Rules
}
// SetRules sets an allow or deny rule.
func (r *RoleV6) SetRules(rct RoleConditionType, in []Rule) {
rcopy := CopyRulesSlice(in)
if rct == Allow {
r.Spec.Allow.Rules = rcopy
} else {
r.Spec.Deny.Rules = rcopy
}
}
// GetHostGroups gets all groups for provisioned user
func (r *RoleV6) GetHostGroups(rct RoleConditionType) []string {
if rct == Allow {
return r.Spec.Allow.HostGroups
}
return r.Spec.Deny.HostGroups
}
// SetHostGroups sets all groups for provisioned user
func (r *RoleV6) SetHostGroups(rct RoleConditionType, groups []string) {
ncopy := utils.CopyStrings(groups)
if rct == Allow {
r.Spec.Allow.HostGroups = ncopy
} else {
r.Spec.Deny.HostGroups = ncopy
}
}
// GetDesktopGroups gets all groups for provisioned user
func (r *RoleV6) GetDesktopGroups(rct RoleConditionType) []string {
if rct == Allow {
return r.Spec.Allow.DesktopGroups
}
return r.Spec.Deny.DesktopGroups
}
// SetDesktopGroups sets all groups for provisioned user
func (r *RoleV6) SetDesktopGroups(rct RoleConditionType, groups []string) {
ncopy := utils.CopyStrings(groups)
if rct == Allow {
r.Spec.Allow.DesktopGroups = ncopy
} else {
r.Spec.Deny.DesktopGroups = ncopy
}
}
// GetHostSudoers gets the list of sudoers entries for the role
func (r *RoleV6) GetHostSudoers(rct RoleConditionType) []string {
if rct == Allow {
return r.Spec.Allow.HostSudoers
}
return r.Spec.Deny.HostSudoers
}
// GetHostSudoers sets the list of sudoers entries for the role
func (r *RoleV6) SetHostSudoers(rct RoleConditionType, sudoers []string) {
ncopy := utils.CopyStrings(sudoers)
if rct == Allow {
r.Spec.Allow.HostSudoers = ncopy
} else {
r.Spec.Deny.HostSudoers = ncopy
}
}
// GetPrivateKeyPolicy returns the private key policy enforced for this role.
func (r *RoleV6) GetPrivateKeyPolicy() keys.PrivateKeyPolicy {
switch r.Spec.Options.RequireMFAType {
case RequireMFAType_SESSION_AND_HARDWARE_KEY:
return keys.PrivateKeyPolicyHardwareKey
case RequireMFAType_HARDWARE_KEY_TOUCH:
return keys.PrivateKeyPolicyHardwareKeyTouch
default:
return keys.PrivateKeyPolicyNone
}
}
// setStaticFields sets static resource header and metadata fields.
func (r *RoleV6) setStaticFields() {
r.Kind = KindRole
if r.Version != V3 && r.Version != V4 && r.Version != V5 {
r.Version = V6
}
}
// GetGroupLabels gets the map of group labels this role is allowed or denied access to.
func (r *RoleV6) GetGroupLabels(rct RoleConditionType) Labels {
if rct == Allow {
return r.Spec.Allow.GroupLabels
}
return r.Spec.Deny.GroupLabels
}
// SetGroupLabels sets the map of group labels this role is allowed or denied access to.
func (r *RoleV6) SetGroupLabels(rct RoleConditionType, labels Labels) {
if rct == Allow {
r.Spec.Allow.GroupLabels = labels.Clone()
} else {
r.Spec.Deny.GroupLabels = labels.Clone()
}
}
// CheckAndSetDefaults checks validity of all parameters and sets defaults
func (r *RoleV6) CheckAndSetDefaults() error {
r.setStaticFields()
if err := r.Metadata.CheckAndSetDefaults(); err != nil {
return trace.Wrap(err)
}
// Make sure all fields have defaults.
if r.Spec.Options.CertificateFormat == "" {
r.Spec.Options.CertificateFormat = constants.CertificateFormatStandard
}
if r.Spec.Options.MaxSessionTTL.Value() == 0 {
r.Spec.Options.MaxSessionTTL = NewDuration(defaults.MaxCertDuration)
}
if r.Spec.Options.PortForwarding == nil {
r.Spec.Options.PortForwarding = NewBoolOption(true)
}
if len(r.Spec.Options.BPF) == 0 {
r.Spec.Options.BPF = defaults.EnhancedEvents()
}
if r.Spec.Allow.Namespaces == nil {
r.Spec.Allow.Namespaces = []string{defaults.Namespace}
}
if r.Spec.Options.RecordSession == nil {
r.Spec.Options.RecordSession = &RecordSession{
Desktop: NewBoolOption(true),
Default: constants.SessionRecordingModeBestEffort,
}
}
if r.Spec.Options.DesktopClipboard == nil {
r.Spec.Options.DesktopClipboard = NewBoolOption(true)
}
if r.Spec.Options.DesktopDirectorySharing == nil {
r.Spec.Options.DesktopDirectorySharing = NewBoolOption(true)
}
if r.Spec.Options.CreateDesktopUser == nil {
r.Spec.Options.CreateDesktopUser = NewBoolOption(false)
}
if r.Spec.Options.CreateDatabaseUser == nil {
r.Spec.Options.CreateDatabaseUser = NewBoolOption(false)
}
if r.Spec.Options.SSHFileCopy == nil {
r.Spec.Options.SSHFileCopy = NewBoolOption(true)
}
if r.Spec.Options.IDP == nil {
// By default, allow users to access the IdP.
r.Spec.Options.IDP = &IdPOptions{
SAML: &IdPSAMLOptions{
Enabled: NewBoolOption(true),
},
}
}
if _, ok := CreateHostUserMode_name[int32(r.Spec.Options.CreateHostUserMode)]; !ok {
return trace.BadParameter("invalid host user mode %q, expected one of off, drop or keep", r.Spec.Options.CreateHostUserMode)
}
switch r.Version {
case V3:
if r.Spec.Allow.NodeLabels == nil {
if len(r.Spec.Allow.Logins) == 0 {
// no logins implies no node access
r.Spec.Allow.NodeLabels = Labels{}
} else {
r.Spec.Allow.NodeLabels = Labels{Wildcard: []string{Wildcard}}
}
}
if r.Spec.Allow.AppLabels == nil {
r.Spec.Allow.AppLabels = Labels{Wildcard: []string{Wildcard}}
}
if r.Spec.Allow.KubernetesLabels == nil {
r.Spec.Allow.KubernetesLabels = Labels{Wildcard: []string{Wildcard}}
}
if r.Spec.Allow.DatabaseLabels == nil {
r.Spec.Allow.DatabaseLabels = Labels{Wildcard: []string{Wildcard}}
}
fallthrough
case V4, V5:
// Labels default to nil/empty for v4+ roles
// Allow unrestricted access to all pods.
if len(r.Spec.Allow.KubernetesResources) == 0 && len(r.Spec.Allow.KubernetesLabels) > 0 {
r.Spec.Allow.KubernetesResources = []KubernetesResource{
{
Kind: KindKubePod,
Namespace: Wildcard,
Name: Wildcard,
},
}
}
if err := validateRoleSpecKubeResources(r.Spec); err != nil {
return trace.Wrap(err)
}
case V6:
if err := validateRoleSpecKubeResources(r.Spec); err != nil {
return trace.Wrap(err)
}
default:
return trace.BadParameter("unrecognized role version: %v", r.Version)
}
if r.Spec.Deny.Namespaces == nil {
r.Spec.Deny.Namespaces = []string{defaults.Namespace}
}
// Validate that enhanced recording options are all valid.
for _, opt := range r.Spec.Options.BPF {
if opt == constants.EnhancedRecordingCommand ||
opt == constants.EnhancedRecordingDisk ||
opt == constants.EnhancedRecordingNetwork {
continue
}
return trace.BadParameter("invalid value for role option enhanced_recording: %v", opt)
}
// Validate locking mode.
switch r.Spec.Options.Lock {
case "":
// Missing locking mode implies the cluster-wide default should be used.
case constants.LockingModeBestEffort, constants.LockingModeStrict:
default:
return trace.BadParameter("invalid value for role option lock: %v", r.Spec.Options.Lock)
}
// check and correct the session ttl
if r.Spec.Options.MaxSessionTTL.Value() <= 0 {
r.Spec.Options.MaxSessionTTL = NewDuration(defaults.MaxCertDuration)
}
// restrict wildcards
for _, login := range r.Spec.Allow.Logins {
if login == Wildcard {
return trace.BadParameter("wildcard matcher is not allowed in logins")
}
}
for _, arn := range r.Spec.Allow.AWSRoleARNs {
if arn == Wildcard {
return trace.BadParameter("wildcard matcher is not allowed in aws_role_arns")