@@ -23,7 +23,8 @@ import (
23
23
24
24
"github.com/go-logr/logr"
25
25
"github.com/pkg/errors"
26
- authv1 "k8s.io/api/authorization/v1"
26
+ authnv1 "k8s.io/api/authentication/v1"
27
+ authzv1 "k8s.io/api/authorization/v1"
27
28
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28
29
"k8s.io/apimachinery/pkg/runtime"
29
30
authorizationv1 "k8s.io/client-go/kubernetes/typed/authorization/v1"
@@ -103,7 +104,7 @@ func (v *AuthValidator) Handle(ctx context.Context, req admission.Request) admis
103
104
requireClusterPrivileges , affectedNamespaces := affectedNamespaces (chaos )
104
105
105
106
if requireClusterPrivileges {
106
- allow , err := v .auth (username , groups , "" , requestKind )
107
+ allow , err := v .auth (req . UserInfo , "" , requestKind )
107
108
if err != nil {
108
109
return admission .Errored (http .StatusBadRequest , err )
109
110
}
@@ -116,7 +117,7 @@ func (v *AuthValidator) Handle(ctx context.Context, req admission.Request) admis
116
117
v .logger .Info ("start validating user" , "user" , username , "groups" , groups , "namespace" , affectedNamespaces )
117
118
118
119
for namespace := range affectedNamespaces {
119
- allow , err := v .auth (username , groups , namespace , requestKind )
120
+ allow , err := v .auth (req . UserInfo , namespace , requestKind )
120
121
if err != nil {
121
122
return admission .Errored (http .StatusBadRequest , err )
122
123
}
@@ -132,21 +133,23 @@ func (v *AuthValidator) Handle(ctx context.Context, req admission.Request) admis
132
133
return admission .Allowed ("" )
133
134
}
134
135
135
- func (v * AuthValidator ) auth (username string , groups [] string , namespace string , chaosKind string ) (bool , error ) {
136
+ func (v * AuthValidator ) auth (userInfo authnv1. UserInfo , namespace string , chaosKind string ) (bool , error ) {
136
137
resourceName , err := v .resourceFor (chaosKind )
137
138
if err != nil {
138
139
return false , err
139
140
}
140
- sar := authv1.SubjectAccessReview {
141
- Spec : authv1.SubjectAccessReviewSpec {
142
- ResourceAttributes : & authv1.ResourceAttributes {
141
+
142
+ sar := authzv1.SubjectAccessReview {
143
+ Spec : authzv1.SubjectAccessReviewSpec {
144
+ ResourceAttributes : & authzv1.ResourceAttributes {
143
145
Namespace : namespace ,
144
146
Verb : "create" ,
145
147
Group : "chaos-mesh.org" ,
146
148
Resource : resourceName ,
147
149
},
148
- User : username ,
149
- Groups : groups ,
150
+ User : userInfo .Username ,
151
+ Groups : userInfo .Groups ,
152
+ Extra : convertExtra (userInfo .Extra ),
150
153
},
151
154
}
152
155
@@ -172,3 +175,12 @@ func contains(arr []string, target string) bool {
172
175
}
173
176
return false
174
177
}
178
+
179
+ func convertExtra (in map [string ]authnv1.ExtraValue ) map [string ]authzv1.ExtraValue {
180
+ // map from authentication and authorization types
181
+ extra := make (map [string ]authzv1.ExtraValue )
182
+ for key , value := range in {
183
+ extra [key ] = authzv1 .ExtraValue (value )
184
+ }
185
+ return extra
186
+ }
0 commit comments