@@ -22,11 +22,12 @@ type http struct {
22
22
usernameClaimField string
23
23
ignoredImpersonationGroups []string
24
24
impersonationGroupsRegexp * regexp.Regexp
25
+ skipImpersonationReview bool
25
26
client client.Writer
26
27
}
27
28
28
- func NewHTTP (request * h.Request , authTypes []AuthType , usernameClaimField string , client client.Writer , ignoredImpersonationGroups []string , impersonationGroupsRegexp * regexp.Regexp ) Request {
29
- return & http {Request : request , authTypes : authTypes , usernameClaimField : usernameClaimField , client : client , ignoredImpersonationGroups : ignoredImpersonationGroups , impersonationGroupsRegexp : impersonationGroupsRegexp }
29
+ func NewHTTP (request * h.Request , authTypes []AuthType , usernameClaimField string , client client.Writer , ignoredImpersonationGroups []string , impersonationGroupsRegexp * regexp.Regexp , skipImpersonationReview bool ) Request {
30
+ return & http {Request : request , authTypes : authTypes , usernameClaimField : usernameClaimField , client : client , ignoredImpersonationGroups : ignoredImpersonationGroups , impersonationGroupsRegexp : impersonationGroupsRegexp , skipImpersonationReview : skipImpersonationReview }
30
31
}
31
32
32
33
func (h http ) GetHTTPRequest () * h.Request {
@@ -49,14 +50,45 @@ func (h http) GetUserAndGroups() (username string, groups []string, err error) {
49
50
50
51
// In case the requester is asking for impersonation, we have to be sure that's allowed by creating a
51
52
// SubjectAccessReview with the requested data, before proceeding.
53
+ //nolint:nestif
52
54
if impersonateGroups := GetImpersonatingGroups (h .Request , h .ignoredImpersonationGroups , h .impersonationGroupsRegexp ); len (impersonateGroups ) > 0 {
53
- for _ , impersonateGroup := range impersonateGroups {
55
+ if ! h .skipImpersonationReview {
56
+ for _ , impersonateGroup := range impersonateGroups {
57
+ ac := & authorizationv1.SubjectAccessReview {
58
+ Spec : authorizationv1.SubjectAccessReviewSpec {
59
+ ResourceAttributes : & authorizationv1.ResourceAttributes {
60
+ Verb : "impersonate" ,
61
+ Resource : "groups" ,
62
+ Name : impersonateGroup ,
63
+ },
64
+ User : username ,
65
+ Groups : groups ,
66
+ },
67
+ }
68
+ if err = h .client .Create (h .Request .Context (), ac ); err != nil {
69
+ return "" , nil , err
70
+ }
71
+
72
+ if ! ac .Status .Allowed {
73
+ return "" , nil , NewErrUnauthorized (fmt .Sprintf ("the current user %s cannot impersonate the group %s" , username , impersonateGroup ))
74
+ }
75
+ }
76
+ }
77
+
78
+ defer func () {
79
+ groups = impersonateGroups
80
+ }()
81
+ }
82
+
83
+ //nolint:nestif
84
+ if impersonateUser := GetImpersonatingUser (h .Request ); len (impersonateUser ) > 0 {
85
+ if ! h .skipImpersonationReview {
54
86
ac := & authorizationv1.SubjectAccessReview {
55
87
Spec : authorizationv1.SubjectAccessReviewSpec {
56
88
ResourceAttributes : & authorizationv1.ResourceAttributes {
57
89
Verb : "impersonate" ,
58
- Resource : "groups " ,
59
- Name : impersonateGroup ,
90
+ Resource : "users " ,
91
+ Name : impersonateUser ,
60
92
},
61
93
User : username ,
62
94
Groups : groups ,
@@ -67,35 +99,10 @@ func (h http) GetUserAndGroups() (username string, groups []string, err error) {
67
99
}
68
100
69
101
if ! ac .Status .Allowed {
70
- return "" , nil , NewErrUnauthorized (fmt .Sprintf ("the current user %s cannot impersonate the group %s" , username , impersonateGroup ))
102
+ return "" , nil , NewErrUnauthorized (fmt .Sprintf ("the current user %s cannot impersonate the user %s" , username , impersonateUser ))
71
103
}
72
104
}
73
105
74
- defer func () {
75
- groups = impersonateGroups
76
- }()
77
- }
78
-
79
- if impersonateUser := GetImpersonatingUser (h .Request ); len (impersonateUser ) > 0 {
80
- ac := & authorizationv1.SubjectAccessReview {
81
- Spec : authorizationv1.SubjectAccessReviewSpec {
82
- ResourceAttributes : & authorizationv1.ResourceAttributes {
83
- Verb : "impersonate" ,
84
- Resource : "users" ,
85
- Name : impersonateUser ,
86
- },
87
- User : username ,
88
- Groups : groups ,
89
- },
90
- }
91
- if err = h .client .Create (h .Request .Context (), ac ); err != nil {
92
- return "" , nil , err
93
- }
94
-
95
- if ! ac .Status .Allowed {
96
- return "" , nil , NewErrUnauthorized (fmt .Sprintf ("the current user %s cannot impersonate the user %s" , username , impersonateUser ))
97
- }
98
-
99
106
// Assign impersonate user after group impersonation with current user
100
107
// As defer func works in LIFO, if user is also impersonating groups, they will be set to correct value in the previous defer func.
101
108
// Otherwise, groups will be set to nil, meaning we are checking just user permissions.
0 commit comments