/
access_request.go
894 lines (773 loc) · 26.7 KB
/
access_request.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
/*
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 contains all types and logic required by the Teleport API.
package types
import (
"fmt"
"reflect"
"slices"
"sort"
"time"
"github.com/gravitational/trace"
"github.com/gravitational/teleport/api/constants"
"github.com/gravitational/teleport/api/utils"
)
// AccessRequest is a request for temporarily granted roles
type AccessRequest interface {
ResourceWithLabels
// GetUser gets the name of the requesting user
GetUser() string
// GetRoles gets the roles being requested by the user
GetRoles() []string
// SetRoles overrides the roles being requested by the user
SetRoles([]string)
// GetState gets the current state of the request
GetState() RequestState
// SetState sets the approval state of the request
SetState(RequestState) error
// GetCreationTime gets the time at which the request was
// originally registered with the auth server.
GetCreationTime() time.Time
// SetCreationTime sets the creation time of the request.
SetCreationTime(time.Time)
// GetAccessExpiry gets the expiration time for the elevated certificate
// that will be issued if the Access Request is approved.
GetAccessExpiry() time.Time
// GetAssumeStartTime gets the time the roles can be assumed
// if the Access Request is approved.
GetAssumeStartTime() *time.Time
// SetAssumeStartTime sets the time the roles can be assumed
// if the Access Request is approved.
SetAssumeStartTime(time.Time)
// SetAccessExpiry sets the expiration time for the elevated certificate
// that will be issued if the Access Request is approved.
SetAccessExpiry(time.Time)
// GetSessionTLL gets the session TTL for generated certificates.
GetSessionTLL() time.Time
// SetSessionTLL sets the session TTL for generated certificates.
SetSessionTLL(time.Time)
// GetRequestReason gets the reason for the request's creation.
GetRequestReason() string
// SetRequestReason sets the reason for the request's creation.
SetRequestReason(string)
// GetResolveReason gets the reason for the request's resolution.
GetResolveReason() string
// SetResolveReason sets the reason for the request's resolution.
SetResolveReason(string)
// GetResolveAnnotations gets the annotations associated with
// the request's resolution.
GetResolveAnnotations() map[string][]string
// SetResolveAnnotations sets the annotations associated with
// the request's resolution.
SetResolveAnnotations(map[string][]string)
// GetSystemAnnotations gets the teleport-applied annotations.
GetSystemAnnotations() map[string][]string
// SetSystemAnnotations sets the teleport-applied annotations.
SetSystemAnnotations(map[string][]string)
// GetOriginalRoles gets the original (pre-override) role list.
GetOriginalRoles() []string
// GetThresholds gets the review thresholds.
GetThresholds() []AccessReviewThreshold
// SetThresholds sets the review thresholds (internal use only).
SetThresholds([]AccessReviewThreshold)
// GetRoleThresholdMapping gets the rtm. See documentation of the
// AccessRequestSpecV3.RoleThresholdMapping field for details.
GetRoleThresholdMapping() map[string]ThresholdIndexSets
// SetRoleThresholdMapping sets the rtm (internal use only). See documentation
// of the AccessRequestSpecV3.RoleThresholdMapping field for details.
SetRoleThresholdMapping(map[string]ThresholdIndexSets)
// GetReviews gets the list of currently applied access reviews.
GetReviews() []AccessReview
// SetReviews sets the list of currently applied access reviews (internal use only).
SetReviews([]AccessReview)
// GetPromotedAccessListName returns the access list name that this access request
// was promoted to.
GetPromotedAccessListName() string
// SetPromotedAccessListName sets the access list name that this access request
// was promoted to.
SetPromotedAccessListName(name string)
// GetPromotedAccessListTitle returns the access list title that this access request
// was promoted to.
GetPromotedAccessListTitle() string
// SetPromotedAccessListTitle sets the access list title that this access request
// was promoted to.
SetPromotedAccessListTitle(string)
// GetSuggestedReviewers gets the suggested reviewer list.
GetSuggestedReviewers() []string
// SetSuggestedReviewers sets the suggested reviewer list.
SetSuggestedReviewers([]string)
// GetRequestedResourceIDs gets the resource IDs to which access is being requested.
GetRequestedResourceIDs() []ResourceID
// SetRequestedResourceIDs sets the resource IDs to which access is being requested.
SetRequestedResourceIDs([]ResourceID)
// GetLoginHint gets the requested login hint.
GetLoginHint() string
// SetLoginHint sets the requested login hint.
SetLoginHint(string)
// GetMaxDuration gets the maximum time at which the access should be approved for.
GetMaxDuration() time.Time
// SetMaxDuration sets the maximum time at which the access should be approved for.
SetMaxDuration(time.Time)
// GetDryRun returns true if this request should not be created and is only
// a dry run to validate request capabilities.
GetDryRun() bool
// SetDryRun sets the dry run flag on the request.
SetDryRun(bool)
// Copy returns a copy of the access request resource.
Copy() AccessRequest
}
// NewAccessRequest assembles an AccessRequest resource.
func NewAccessRequest(name string, user string, roles ...string) (AccessRequest, error) {
return NewAccessRequestWithResources(name, user, roles, []ResourceID{})
}
// NewAccessRequestWithResources assembles an AccessRequest resource with
// requested resources.
func NewAccessRequestWithResources(name string, user string, roles []string, resourceIDs []ResourceID) (AccessRequest, error) {
req := AccessRequestV3{
Metadata: Metadata{
Name: name,
},
Spec: AccessRequestSpecV3{
User: user,
Roles: utils.CopyStrings(roles),
RequestedResourceIDs: append([]ResourceID{}, resourceIDs...),
},
}
if err := req.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
return &req, nil
}
// GetUser gets User
func (r *AccessRequestV3) GetUser() string {
return r.Spec.User
}
// GetRoles gets Roles
func (r *AccessRequestV3) GetRoles() []string {
return r.Spec.Roles
}
// SetRoles sets Roles
func (r *AccessRequestV3) SetRoles(roles []string) {
r.Spec.Roles = roles
}
// GetState gets State
func (r *AccessRequestV3) GetState() RequestState {
return r.Spec.State
}
// SetState sets State
func (r *AccessRequestV3) SetState(state RequestState) error {
if r.Spec.State.IsDenied() {
if state.IsDenied() {
return nil
}
return trace.BadParameter("cannot set request-state %q (already denied)", state.String())
}
r.Spec.State = state
return nil
}
// GetCreationTime gets CreationTime
func (r *AccessRequestV3) GetCreationTime() time.Time {
return r.Spec.Created
}
// SetCreationTime sets CreationTime
func (r *AccessRequestV3) SetCreationTime(t time.Time) {
r.Spec.Created = t.UTC()
}
// GetAccessExpiry gets AccessExpiry
func (r *AccessRequestV3) GetAccessExpiry() time.Time {
return r.Spec.Expires
}
// GetAssumeStartTime gets AssumeStartTime
func (r *AccessRequestV3) GetAssumeStartTime() *time.Time {
return r.Spec.AssumeStartTime
}
// SetAssumeStartTime sets AssumeStartTime
func (r *AccessRequestV3) SetAssumeStartTime(t time.Time) {
r.Spec.AssumeStartTime = &t
}
// SetAccessExpiry sets AccessExpiry
func (r *AccessRequestV3) SetAccessExpiry(expiry time.Time) {
r.Spec.Expires = expiry.UTC()
}
// GetSessionTLL gets SessionTLL
func (r *AccessRequestV3) GetSessionTLL() time.Time {
return r.Spec.SessionTTL
}
// SetSessionTLL sets SessionTLL
func (r *AccessRequestV3) SetSessionTLL(t time.Time) {
r.Spec.SessionTTL = t.UTC()
}
// GetRequestReason gets RequestReason
func (r *AccessRequestV3) GetRequestReason() string {
return r.Spec.RequestReason
}
// SetRequestReason sets RequestReason
func (r *AccessRequestV3) SetRequestReason(reason string) {
r.Spec.RequestReason = reason
}
// GetResolveReason gets ResolveReason
func (r *AccessRequestV3) GetResolveReason() string {
return r.Spec.ResolveReason
}
// SetResolveReason sets ResolveReason
func (r *AccessRequestV3) SetResolveReason(reason string) {
r.Spec.ResolveReason = reason
}
// GetResolveAnnotations gets ResolveAnnotations
func (r *AccessRequestV3) GetResolveAnnotations() map[string][]string {
return r.Spec.ResolveAnnotations
}
// SetResolveAnnotations sets ResolveAnnotations
func (r *AccessRequestV3) SetResolveAnnotations(annotations map[string][]string) {
r.Spec.ResolveAnnotations = annotations
}
// GetSystemAnnotations gets SystemAnnotations
func (r *AccessRequestV3) GetSystemAnnotations() map[string][]string {
return r.Spec.SystemAnnotations
}
// SetSystemAnnotations sets SystemAnnotations
func (r *AccessRequestV3) SetSystemAnnotations(annotations map[string][]string) {
r.Spec.SystemAnnotations = annotations
}
func (r *AccessRequestV3) GetOriginalRoles() []string {
if l := len(r.Spec.RoleThresholdMapping); l == 0 || l == len(r.Spec.Roles) {
// rtm is unspecified or original role list is unmodified. since the rtm
// keys and role list are identical until role subselection is applied,
// we can return the role list directly.
return r.Spec.Roles
}
// role subselection has been applied. calculate original roles
// by collecting the keys of the rtm.
roles := make([]string, 0, len(r.Spec.RoleThresholdMapping))
for role := range r.Spec.RoleThresholdMapping {
roles = append(roles, role)
}
sort.Strings(roles)
return roles
}
// GetThresholds gets the review thresholds.
func (r *AccessRequestV3) GetThresholds() []AccessReviewThreshold {
return r.Spec.Thresholds
}
// SetThresholds sets the review thresholds.
func (r *AccessRequestV3) SetThresholds(thresholds []AccessReviewThreshold) {
r.Spec.Thresholds = thresholds
}
// GetRoleThresholdMapping gets the rtm.
func (r *AccessRequestV3) GetRoleThresholdMapping() map[string]ThresholdIndexSets {
return r.Spec.RoleThresholdMapping
}
// SetRoleThresholdMapping sets the rtm (internal use only).
func (r *AccessRequestV3) SetRoleThresholdMapping(rtm map[string]ThresholdIndexSets) {
r.Spec.RoleThresholdMapping = rtm
}
// SetReviews sets the list of currently applied access reviews.
func (r *AccessRequestV3) SetReviews(revs []AccessReview) {
utcRevs := make([]AccessReview, len(revs))
for i, rev := range revs {
utcRevs[i] = rev
utcRevs[i].Created = rev.Created.UTC()
}
r.Spec.Reviews = utcRevs
}
// GetReviews gets the list of currently applied access reviews.
func (r *AccessRequestV3) GetReviews() []AccessReview {
return r.Spec.Reviews
}
// GetSuggestedReviewers gets the suggested reviewer list.
func (r *AccessRequestV3) GetSuggestedReviewers() []string {
return r.Spec.SuggestedReviewers
}
// SetSuggestedReviewers sets the suggested reviewer list.
func (r *AccessRequestV3) SetSuggestedReviewers(reviewers []string) {
r.Spec.SuggestedReviewers = reviewers
}
// GetPromotedAccessListName returns PromotedAccessListName.
func (r *AccessRequestV3) GetPromotedAccessListName() string {
if r.Spec.AccessList == nil {
return ""
}
return r.Spec.AccessList.Name
}
// SetPromotedAccessListName sets PromotedAccessListName.
func (r *AccessRequestV3) SetPromotedAccessListName(name string) {
if r.Spec.AccessList == nil {
r.Spec.AccessList = &PromotedAccessList{}
}
r.Spec.AccessList.Name = name
}
// GetPromotedAccessListTitle returns PromotedAccessListTitle.
func (r *AccessRequestV3) GetPromotedAccessListTitle() string {
if r.Spec.AccessList == nil {
return ""
}
return r.Spec.AccessList.Title
}
// SetPromotedAccessListTitle sets PromotedAccessListTitle.
func (r *AccessRequestV3) SetPromotedAccessListTitle(title string) {
if r.Spec.AccessList == nil {
r.Spec.AccessList = &PromotedAccessList{}
}
r.Spec.AccessList.Title = title
}
// setStaticFields sets static resource header and metadata fields.
func (r *AccessRequestV3) setStaticFields() {
r.Kind = KindAccessRequest
r.Version = V3
}
// CheckAndSetDefaults validates set values and sets default values
func (r *AccessRequestV3) CheckAndSetDefaults() error {
r.setStaticFields()
if err := r.Metadata.CheckAndSetDefaults(); err != nil {
return trace.Wrap(err)
}
if r.Spec.State.IsNone() {
r.Spec.State = RequestState_PENDING
}
if r.GetState().IsPending() {
if r.GetResolveReason() != "" {
return trace.BadParameter("pending requests cannot include resolve reason")
}
if len(r.GetResolveAnnotations()) != 0 {
return trace.BadParameter("pending requests cannot include resolve annotations")
}
}
if r.GetUser() == "" {
return trace.BadParameter("access request user name not set")
}
if r.Spec.Roles == nil {
r.Spec.Roles = []string{}
}
if r.Spec.RequestedResourceIDs == nil {
r.Spec.RequestedResourceIDs = []ResourceID{}
}
if len(r.GetRoles()) == 0 && len(r.GetRequestedResourceIDs()) == 0 {
return trace.BadParameter("access request does not specify any roles or resources")
}
// dedupe and sort roles to simplify comparing role lists
r.Spec.Roles = utils.Deduplicate(r.Spec.Roles)
sort.Strings(r.Spec.Roles)
return nil
}
// GetKind gets Kind
func (r *AccessRequestV3) GetKind() string {
return r.Kind
}
// GetSubKind gets SubKind
func (r *AccessRequestV3) GetSubKind() string {
return r.SubKind
}
// SetSubKind sets SubKind
func (r *AccessRequestV3) SetSubKind(subKind string) {
r.SubKind = subKind
}
// GetVersion gets Version
func (r *AccessRequestV3) GetVersion() string {
return r.Version
}
// GetName gets Name
func (r *AccessRequestV3) GetName() string {
return r.Metadata.Name
}
// SetName sets Name
func (r *AccessRequestV3) SetName(name string) {
r.Metadata.Name = name
}
// Expiry gets Expiry
func (r *AccessRequestV3) Expiry() time.Time {
return r.Metadata.Expiry()
}
// SetExpiry sets Expiry
func (r *AccessRequestV3) SetExpiry(expiry time.Time) {
r.Metadata.SetExpiry(expiry.UTC())
}
// GetMetadata gets Metadata
func (r *AccessRequestV3) GetMetadata() Metadata {
return r.Metadata
}
// GetResourceID gets ResourceID
func (r *AccessRequestV3) GetResourceID() int64 {
return r.Metadata.GetID()
}
// SetResourceID sets ResourceID
func (r *AccessRequestV3) SetResourceID(id int64) {
r.Metadata.SetID(id)
}
// GetRevision returns the revision
func (r *AccessRequestV3) GetRevision() string {
return r.Metadata.GetRevision()
}
// SetRevision sets the revision
func (r *AccessRequestV3) SetRevision(rev string) {
r.Metadata.SetRevision(rev)
}
// GetRequestedResourceIDs gets the resource IDs to which access is being requested.
func (r *AccessRequestV3) GetRequestedResourceIDs() []ResourceID {
return append([]ResourceID{}, r.Spec.RequestedResourceIDs...)
}
// SetRequestedResourceIDs sets the resource IDs to which access is being requested.
func (r *AccessRequestV3) SetRequestedResourceIDs(ids []ResourceID) {
r.Spec.RequestedResourceIDs = append([]ResourceID{}, ids...)
}
// GetLoginHint gets the requested login hint.
func (r *AccessRequestV3) GetLoginHint() string {
return r.Spec.LoginHint
}
// SetLoginHint sets the requested login hint.
func (r *AccessRequestV3) SetLoginHint(login string) {
r.Spec.LoginHint = login
}
// GetDryRun returns true if this request should not be created and is only
// a dry run to validate request capabilities.
func (r *AccessRequestV3) GetDryRun() bool {
return r.Spec.DryRun
}
// GetMaxDuration gets the maximum time at which the access should be approved for.
func (r *AccessRequestV3) GetMaxDuration() time.Time {
return r.Spec.MaxDuration
}
// SetMaxDuration sets the maximum time at which the access should be approved for.
func (r *AccessRequestV3) SetMaxDuration(t time.Time) {
r.Spec.MaxDuration = t
}
// SetDryRun sets the dry run flag on the request.
func (r *AccessRequestV3) SetDryRun(dryRun bool) {
r.Spec.DryRun = dryRun
}
// Copy returns a copy of the access request resource.
func (r *AccessRequestV3) Copy() AccessRequest {
return utils.CloneProtoMsg(r)
}
// GetLabel retrieves the label with the provided key. If not found
// value will be empty and ok will be false.
func (r *AccessRequestV3) GetLabel(key string) (value string, ok bool) {
v, ok := r.Metadata.Labels[key]
return v, ok
}
// GetStaticLabels returns the access request static labels.
func (r *AccessRequestV3) GetStaticLabels() map[string]string {
return r.Metadata.Labels
}
// SetStaticLabels sets the access request static labels.
func (r *AccessRequestV3) SetStaticLabels(sl map[string]string) {
r.Metadata.Labels = sl
}
// GetAllLabels returns the access request static labels.
func (r *AccessRequestV3) GetAllLabels() map[string]string {
return r.Metadata.Labels
}
// MatchSearch goes through select field values and tries to
// match against the list of search values.
func (r *AccessRequestV3) MatchSearch(values []string) bool {
fieldVals := append(utils.MapToStrings(r.GetAllLabels()), r.GetName(), r.GetUser())
fieldVals = append(fieldVals, r.GetRoles()...)
for _, resource := range r.GetRequestedResourceIDs() {
fieldVals = append(fieldVals, resource.Name)
}
return MatchSearch(fieldVals, values, nil)
}
// Origin returns the origin value of the resource.
func (r *AccessRequestV3) Origin() string {
return r.Metadata.Origin()
}
// SetOrigin sets the origin value of the resource.
func (r *AccessRequestV3) SetOrigin(origin string) {
r.Metadata.SetOrigin(origin)
}
// String returns a text representation of this AccessRequest
func (r *AccessRequestV3) String() string {
return fmt.Sprintf("AccessRequest(user=%v,roles=%+v)", r.Spec.User, r.Spec.Roles)
}
func (c AccessReviewConditions) IsZero() bool {
return reflect.ValueOf(c).IsZero()
}
func (s AccessReviewSubmission) Check() error {
if s.RequestID == "" {
return trace.BadParameter("missing request ID")
}
return s.Review.Check()
}
func (s AccessReview) Check() error {
if s.Author == "" {
return trace.BadParameter("missing review author")
}
return nil
}
// GetAccessListName returns the access list name used for the promotion.
func (s AccessReview) GetAccessListName() string {
if s.AccessList == nil {
return ""
}
return s.AccessList.Name
}
// GetAccessListTitle returns the access list title used for the promotion.
func (s AccessReview) GetAccessListTitle() string {
if s.AccessList == nil {
return ""
}
return s.AccessList.Title
}
// AccessRequestUpdate encompasses the parameters of a
// SetAccessRequestState call.
type AccessRequestUpdate struct {
// RequestID is the ID of the request to be updated.
RequestID string
// State is the state that the target request
// should resolve to.
State RequestState
// Reason is an optional description of *why* the
// the request is being resolved.
Reason string
// Annotations supplies extra data associated with
// the resolution; primarily for audit purposes.
Annotations map[string][]string
// Roles, if non-empty declares a list of roles
// that should override the role list of the request.
// This parameter is only accepted on approvals
// and must be a subset of the role list originally
// present on the request.
Roles []string
// AssumeStartTime sets the time the requestor can assume
// the requested roles.
AssumeStartTime *time.Time
}
// Check validates the request's fields
func (u *AccessRequestUpdate) Check() error {
if u.RequestID == "" {
return trace.BadParameter("missing request id")
}
if u.State.IsNone() {
return trace.BadParameter("missing request state")
}
if len(u.Roles) > 0 && !u.State.IsApproved() {
return trace.BadParameter("cannot override roles when setting state: %s", u.State)
}
return nil
}
// RequestStrategy is an indicator of how access requests
// should be handled for holders of a given role.
type RequestStrategy string
const (
// RequestStrategyOptional is the default request strategy,
// indicating that no special actions/requirements exist.
RequestStrategyOptional RequestStrategy = "optional"
// RequestStrategyReason indicates that client implementations
// should automatically generate wildcard requests on login, and
// users should be prompted for a reason.
RequestStrategyReason RequestStrategy = "reason"
// RequestStrategyAlways indicates that client implementations
// should automatically generate wildcard requests on login, but
// that reasons are not required.
RequestStrategyAlways RequestStrategy = "always"
)
// ShouldAutoRequest checks if the request strategy
// indicates that a request should be automatically
// generated on login.
func (s RequestStrategy) ShouldAutoRequest() bool {
switch s {
case RequestStrategyReason, RequestStrategyAlways:
return true
default:
return false
}
}
// RequireReason checks if the request strategy
// is one that requires users to always supply
// reasons with their requests.
func (s RequestStrategy) RequireReason() bool {
return s == RequestStrategyReason
}
// stateVariants allows iteration of the expected variants
// of RequestState.
var stateVariants = [5]RequestState{
RequestState_NONE,
RequestState_PENDING,
RequestState_APPROVED,
RequestState_DENIED,
RequestState_PROMOTED,
}
// Parse attempts to interpret a value as a string representation
// of a RequestState.
func (s *RequestState) Parse(val string) error {
for _, state := range stateVariants {
if state.String() == val {
*s = state
return nil
}
}
return trace.BadParameter("unknown request state: %q", val)
}
// IsNone request state
func (s RequestState) IsNone() bool {
return s == RequestState_NONE
}
// IsPending request state
func (s RequestState) IsPending() bool {
return s == RequestState_PENDING
}
// IsApproved request state
func (s RequestState) IsApproved() bool {
return s == RequestState_APPROVED
}
// IsDenied request state
func (s RequestState) IsDenied() bool {
return s == RequestState_DENIED
}
// IsPromoted returns true is the request in the PROMOTED state.
func (s RequestState) IsPromoted() bool {
return s == RequestState_PROMOTED
}
// IsResolved request state
func (s RequestState) IsResolved() bool {
return s.IsApproved() || s.IsDenied() || s.IsPromoted()
}
// key values for map encoding of request filter
const (
keyID = "id"
keyUser = "user"
keyState = "state"
)
// IntoMap copies AccessRequestFilter values into a map
func (f *AccessRequestFilter) IntoMap() map[string]string {
m := make(map[string]string)
if f.ID != "" {
m[keyID] = f.ID
}
if f.User != "" {
m[keyUser] = f.User
}
if !f.State.IsNone() {
m[keyState] = f.State.String()
}
return m
}
// FromMap copies values from a map into this AccessRequestFilter value
func (f *AccessRequestFilter) FromMap(m map[string]string) error {
for key, val := range m {
switch key {
case keyID:
f.ID = val
case keyUser:
f.User = val
case keyState:
if err := f.State.Parse(val); err != nil {
return trace.Wrap(err)
}
default:
return trace.BadParameter("unknown filter key %s", key)
}
}
return nil
}
func hasReviewed(req AccessRequest, author string) bool {
reviews := req.GetReviews()
var reviewers []string
for _, review := range reviews {
reviewers = append(reviewers, review.Author)
}
return slices.Contains(reviewers, author)
}
// Match checks if a given access request matches this filter.
func (f *AccessRequestFilter) Match(req AccessRequest) bool {
// only return if the request was made by the api requester
if f.Scope == AccessRequestScope_MY_REQUESTS && req.GetUser() != f.Requester {
return false
}
// a user cannot review their own requests
if f.Scope == AccessRequestScope_NEEDS_REVIEW {
if req.GetUser() == f.Requester {
return false
}
if req.GetState() != RequestState_PENDING {
return false
}
if hasReviewed(req, f.Requester) {
return false
}
}
// only match if the api requester has submit a review
if f.Scope == AccessRequestScope_REVIEWED {
// users cant review their own requests so we can early return
if req.GetUser() == f.Requester {
return false
}
if !hasReviewed(req, f.Requester) {
return false
}
}
if !req.MatchSearch(f.SearchKeywords) {
return false
}
if f.ID != "" && req.GetName() != f.ID {
return false
}
if f.User != "" && req.GetUser() != f.User {
return false
}
if !f.State.IsNone() && req.GetState() != f.State {
return false
}
return true
}
// AccessRequests is a list of AccessRequest resources.
type AccessRequests []AccessRequest
// ToMap returns these access requests as a map keyed by access request name.
func (a AccessRequests) ToMap() map[string]AccessRequest {
m := make(map[string]AccessRequest)
for _, accessRequest := range a {
m[accessRequest.GetName()] = accessRequest
}
return m
}
// AsResources returns these access requests as resources with labels.
func (a AccessRequests) AsResources() (resources ResourcesWithLabels) {
for _, accessRequest := range a {
resources = append(resources, accessRequest)
}
return resources
}
// Len returns the slice length.
func (a AccessRequests) Len() int { return len(a) }
// Less compares access requests by name.
func (a AccessRequests) Less(i, j int) bool { return a[i].GetName() < a[j].GetName() }
// Swap swaps two access requests.
func (a AccessRequests) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
// NewAccessRequestAllowedPromotions returns a new AccessRequestAllowedPromotions resource.
func NewAccessRequestAllowedPromotions(promotions []*AccessRequestAllowedPromotion) *AccessRequestAllowedPromotions {
if promotions == nil {
promotions = make([]*AccessRequestAllowedPromotion, 0)
}
return &AccessRequestAllowedPromotions{
Promotions: promotions,
}
}
// ValidateAssumeStartTime returns error if start time is in an invalid range.
func ValidateAssumeStartTime(assumeStartTime time.Time, accessExpiry time.Time, creationTime time.Time) error {
// Guard against requesting a start time before the request creation time.
if assumeStartTime.Before(creationTime) {
return trace.BadParameter("assume start time has to be after %v", creationTime.Format(time.RFC3339))
}
// Guard against requesting a start time after access expiry.
if assumeStartTime.After(accessExpiry) || assumeStartTime.Equal(accessExpiry) {
return trace.BadParameter("assume start time must be prior to access expiry time at %v",
accessExpiry.Format(time.RFC3339))
}
// Access expiry can be greater than constants.MaxAssumeStartDuration, but start time
// should be on or before constants.MaxAssumeStartDuration.
maxAssumableStartTime := creationTime.Add(constants.MaxAssumeStartDuration)
if maxAssumableStartTime.Before(accessExpiry) && assumeStartTime.After(maxAssumableStartTime) {
return trace.BadParameter("assume start time is too far in the future, latest time allowed is %v",
maxAssumableStartTime.Format(time.RFC3339))
}
return nil
}