Skip to content

Commit 07675e2

Browse files
chore: Add support for userInfo.extra in validating webhook
1 parent 374536a commit 07675e2

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

pkg/webhook/validate_auth.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import (
2323

2424
"github.com/go-logr/logr"
2525
"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"
2728
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2829
"k8s.io/apimachinery/pkg/runtime"
2930
authorizationv1 "k8s.io/client-go/kubernetes/typed/authorization/v1"
@@ -103,7 +104,7 @@ func (v *AuthValidator) Handle(ctx context.Context, req admission.Request) admis
103104
requireClusterPrivileges, affectedNamespaces := affectedNamespaces(chaos)
104105

105106
if requireClusterPrivileges {
106-
allow, err := v.auth(username, groups, "", requestKind)
107+
allow, err := v.auth(req.UserInfo, "", requestKind)
107108
if err != nil {
108109
return admission.Errored(http.StatusBadRequest, err)
109110
}
@@ -116,7 +117,7 @@ func (v *AuthValidator) Handle(ctx context.Context, req admission.Request) admis
116117
v.logger.Info("start validating user", "user", username, "groups", groups, "namespace", affectedNamespaces)
117118

118119
for namespace := range affectedNamespaces {
119-
allow, err := v.auth(username, groups, namespace, requestKind)
120+
allow, err := v.auth(req.UserInfo, namespace, requestKind)
120121
if err != nil {
121122
return admission.Errored(http.StatusBadRequest, err)
122123
}
@@ -132,21 +133,23 @@ func (v *AuthValidator) Handle(ctx context.Context, req admission.Request) admis
132133
return admission.Allowed("")
133134
}
134135

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) {
136137
resourceName, err := v.resourceFor(chaosKind)
137138
if err != nil {
138139
return false, err
139140
}
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{
143145
Namespace: namespace,
144146
Verb: "create",
145147
Group: "chaos-mesh.org",
146148
Resource: resourceName,
147149
},
148-
User: username,
149-
Groups: groups,
150+
User: userInfo.Username,
151+
Groups: userInfo.Groups,
152+
Extra: convertExtra(userInfo.Extra),
150153
},
151154
}
152155

@@ -172,3 +175,12 @@ func contains(arr []string, target string) bool {
172175
}
173176
return false
174177
}
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

Comments
 (0)