Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 adding SubjectAccessReview.authorization.k8s.io/v1 webhook support #2664

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions examples/subjectaccessreview/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Copyright 2021 The Kubernetes Authors.

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 main

import (
"os"

_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
"sigs.k8s.io/controller-runtime/pkg/webhook/authorization"
)

func init() {
log.SetLogger(zap.New())
}

func main() {
entryLog := log.Log.WithName("entrypoint")

// Setup a Manager
entryLog.Info("setting up manager")
mgr, err := manager.New(config.GetConfigOrDie(), manager.Options{})
if err != nil {
entryLog.Error(err, "unable to set up overall controller manager")
os.Exit(1)
}

// Setup webhooks
entryLog.Info("setting up webhook server")
hookServer := mgr.GetWebhookServer()

entryLog.Info("registering webhooks to the webhook server")
hookServer.Register("/validate-v1-subjectaccessreview", &authorization.Webhook{Handler: &authorizer{}})

entryLog.Info("starting manager")
if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
entryLog.Error(err, "unable to run manager")
os.Exit(1)
}
}
38 changes: 38 additions & 0 deletions examples/subjectaccessreview/subjectaccessreview.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2021 The Kubernetes Authors.

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 main

import (
"context"

"sigs.k8s.io/controller-runtime/pkg/webhook/authorization"
)

// authorizer validates subjectaccessreviews
type authorizer struct {
}

// authorizer admits a request by the token.
func (a *authorizer) Handle(ctx context.Context, req authorization.Request) authorization.Response {
if req.Spec.User == "system:anonymous" {
return authorization.Denied("anonymous users are not allowed")
}
if req.Spec.User == "foo" {
return authorization.NoOpinion("I don't care if foo is authorized or not")
}
return authorization.Allowed()
}
37 changes: 37 additions & 0 deletions pkg/webhook/authorization/authorization_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright 2021 The Kubernetes Authors.

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 authorization

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)

func TestAuthorizationWebhook(t *testing.T) {
RegisterFailHandler(Fail)
}

var _ = BeforeSuite(func(done Done) {
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))

close(done)
})
23 changes: 23 additions & 0 deletions pkg/webhook/authorization/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright 2021 The Kubernetes Authors.

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 authorization provides implementation for authorization webhook and
methods to implement authorization webhook handlers.

See examples/subjectaccessreview/ for an example of authorization webhooks.
*/
package authorization
155 changes: 155 additions & 0 deletions pkg/webhook/authorization/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
Copyright 2021 The Kubernetes Authors.

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 authorization

import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"

authorizationv1 "k8s.io/api/authorization/v1"
authorizationv1beta1 "k8s.io/api/authorization/v1beta1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
)

var authorizationScheme = runtime.NewScheme()
var authorizationCodecs = serializer.NewCodecFactory(authorizationScheme)

func init() {
utilruntime.Must(authorizationv1.AddToScheme(authorizationScheme))
utilruntime.Must(authorizationv1beta1.AddToScheme(authorizationScheme))
}

var _ http.Handler = &Webhook{}

func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if wh.WithContextFunc != nil {
ctx = wh.WithContextFunc(ctx, r)
}

if r.Body == nil || r.Body == http.NoBody {
err := errors.New("request body is empty")
wh.getLogger(nil).Error(err, "bad request")
wh.writeResponse(w, Errored(err))
return
}

defer r.Body.Close()
body, err := io.ReadAll(r.Body)
if err != nil {
wh.getLogger(nil).Error(err, "unable to read the body from the incoming request")
wh.writeResponse(w, Errored(err))
return
}

// verify the content type is accurate
if contentType := r.Header.Get("Content-Type"); contentType != "application/json" {
err = fmt.Errorf("contentType=%s, expected application/json", contentType)
wh.getLogger(nil).Error(err, "unable to process a request with unknown content type")
wh.writeResponse(w, Errored(err))
return
}

// Decode request body into authorizationv1.SubjectAccessReviewSpec structure
sar, actualTokRevGVK, err := wh.decodeRequestBody(body)
if err != nil {
wh.getLogger(nil).Error(err, "unable to decode the request")
wh.writeResponse(w, Errored(err))
return
}
req := Request{}
req.SubjectAccessReview = sar.SubjectAccessReview
wh.getLogger(&req).V(5).Info("received request")

wh.writeResponseTyped(w, wh.Handle(ctx, req), actualTokRevGVK)
}

// writeResponse writes response to w generically, i.e. without encoding GVK information.
func (wh *Webhook) writeResponse(w io.Writer, response Response) {
wh.writeSubjectAccessReviewResponse(w, response.SubjectAccessReview)
}

// writeResponseTyped writes response to w with GVK set to subjRevGVK, which is necessary
// if multiple SubjectAccessReview versions are permitted by the webhook.
func (wh *Webhook) writeResponseTyped(w io.Writer, response Response, subjRevGVK *schema.GroupVersionKind) {
ar := response.SubjectAccessReview

// Default to a v1 SubjectAccessReview, otherwise the API server may not recognize the request
// if multiple SubjectAccessReview versions are permitted by the webhook config.
if subjRevGVK == nil || *subjRevGVK == (schema.GroupVersionKind{}) {
ar.SetGroupVersionKind(authorizationv1.SchemeGroupVersion.WithKind("SubjectAccessReview"))
} else {
ar.SetGroupVersionKind(*subjRevGVK)
}
wh.writeSubjectAccessReviewResponse(w, ar)
}

// writeSubjectAccessReviewResponse writes ar to w.
func (wh *Webhook) writeSubjectAccessReviewResponse(w io.Writer, ar authorizationv1.SubjectAccessReview) {
if err := json.NewEncoder(w).Encode(ar); err != nil {
wh.getLogger(nil).Error(err, "unable to encode the response")
wh.writeResponse(w, Errored(err))
}
res := ar
wh.getLogger(nil).V(5).Info("wrote response", "authorized", res.Status.Allowed)
}

func (wh *Webhook) decodeRequestBody(body []byte) (unversionedSubjectAccessReview, *schema.GroupVersionKind, error) {
// v1 and v1beta1 SubjectAccessReview types are almost exactly the same (the only difference is the JSON key for the
// 'Groups' field).The v1beta1 api is deprecated as of 1.19 and will be removed in authorization as of v1.22. We
// decode the object into a v1 type and "manually" convert the 'Groups' field (see below).
// However, the runtime codec's decoder guesses which type to decode into by type name if an Object's TypeMeta
// isn't set. By setting TypeMeta of an unregistered type to the v1 GVK, the decoder will coerce a v1beta1
// SubjectAccessReview to v1.
var obj unversionedSubjectAccessReview
obj.SetGroupVersionKind(authorizationv1.SchemeGroupVersion.WithKind("SubjectAccessReview"))

_, gvk, err := authorizationCodecs.UniversalDeserializer().Decode(body, nil, &obj)
if err != nil {
return obj, nil, err
}
if gvk == nil {
return obj, nil, fmt.Errorf("could not determine GVK for object in the request body")
}

// The only difference in v1beta1 is that the JSON key name of the 'Groups' field is different. Hence, when we
// detect that v1beta1 was sent, we decode it once again into the "correct" type and manually "convert" the 'Groups'
// information.
switch *gvk {
case authorizationv1beta1.SchemeGroupVersion.WithKind("SubjectAccessReview"):
var tmp authorizationv1beta1.SubjectAccessReview
if _, _, err := authorizationCodecs.UniversalDeserializer().Decode(body, nil, &tmp); err != nil {
return obj, gvk, err
}
obj.Spec.Groups = tmp.Spec.Groups
}

return obj, gvk, nil
}

// unversionedSubjectAccessReview is used to decode both v1 and v1beta1 SubjectAccessReview types.
type unversionedSubjectAccessReview struct {
authorizationv1.SubjectAccessReview
}

var _ runtime.Object = &unversionedSubjectAccessReview{}
Loading
Loading