Skip to content

Commit a9ebca3

Browse files
adding TokenReview.auth.k8s.io/v1 webhook support
Signed-off-by: Chris Hein <[email protected]>
1 parent b5065bd commit a9ebca3

File tree

13 files changed

+1170
-0
lines changed

13 files changed

+1170
-0
lines changed

examples/tokenreview/main.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"os"
21+
22+
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
23+
"sigs.k8s.io/controller-runtime/pkg/client/config"
24+
"sigs.k8s.io/controller-runtime/pkg/log"
25+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
26+
"sigs.k8s.io/controller-runtime/pkg/manager"
27+
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
28+
"sigs.k8s.io/controller-runtime/pkg/webhook"
29+
)
30+
31+
func init() {
32+
log.SetLogger(zap.New())
33+
}
34+
35+
func main() {
36+
entryLog := log.Log.WithName("entrypoint")
37+
38+
// Setup a Manager
39+
entryLog.Info("setting up manager")
40+
mgr, err := manager.New(config.GetConfigOrDie(), manager.Options{})
41+
if err != nil {
42+
entryLog.Error(err, "unable to set up overall controller manager")
43+
os.Exit(1)
44+
}
45+
46+
// Setup webhooks
47+
entryLog.Info("setting up webhook server")
48+
hookServer := mgr.GetWebhookServer()
49+
50+
entryLog.Info("registering webhooks to the webhook server")
51+
hookServer.Register("/validate-v1-tokenreview", &webhook.Authentication{Handler: &authenticator{Client: mgr.GetClient()}})
52+
53+
entryLog.Info("starting manager")
54+
if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
55+
entryLog.Error(err, "unable to run manager")
56+
os.Exit(1)
57+
}
58+
}

examples/tokenreview/tokenreview.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"context"
21+
22+
v1 "k8s.io/api/authentication/v1"
23+
"sigs.k8s.io/controller-runtime/pkg/client"
24+
25+
"sigs.k8s.io/controller-runtime/pkg/webhook/authentication"
26+
)
27+
28+
// authenticator validates tokenreviews
29+
type authenticator struct {
30+
Client client.Client
31+
decoder *authentication.Decoder
32+
}
33+
34+
// authenticator admits a request by the token.
35+
func (v *authenticator) Handle(ctx context.Context, req authentication.Request) authentication.Response {
36+
if req.Spec.Token == "invalid" {
37+
return authentication.Unauthenticated("invalid is an invalid token", v1.UserInfo{})
38+
}
39+
return authentication.Authenticated("", v1.UserInfo{})
40+
}
41+
42+
// authenticator implements authentication.DecoderInjector.
43+
// A decoder will be automatically injected.
44+
45+
// InjectDecoder injects the decoder.
46+
func (v *authenticator) InjectDecoder(d *authentication.Decoder) error {
47+
v.decoder = d
48+
return nil
49+
}

pkg/webhook/alias.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package webhook
1919
import (
2020
"gomodules.xyz/jsonpatch/v2"
2121
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
22+
"sigs.k8s.io/controller-runtime/pkg/webhook/authentication"
2223
)
2324

2425
// define some aliases for common bits of the webhook functionality
@@ -54,6 +55,20 @@ type AdmissionHandler = admission.Handler
5455
// AdmissionDecoder knows how to decode objects from admission requests.
5556
type AdmissionDecoder = admission.Decoder
5657

58+
// AuthenticationRequest defines the input for an authentication handler.
59+
// It contains the token & audiences from the client.
60+
type AuthenticationRequest = authentication.Request
61+
62+
// AuthenticationResponse is the output of an authentication handler.
63+
// It contains a response indicating if a given operation is allowed.
64+
type AuthenticationResponse = authentication.Response
65+
66+
// Authentication is webhook suitable for registration with the server
67+
type Authentication = authentication.Webhook
68+
69+
// AuthenticationHandler knows how to process authentication requests.
70+
type AuthenticationHandler = authentication.Handler
71+
5772
// JSONPatchOp represents a single JSONPatch patch operation.
5873
type JSONPatchOp = jsonpatch.Operation
5974

@@ -70,4 +85,13 @@ var (
7085

7186
// Errored indicates that an error occurred in the admission request.
7287
Errored = admission.Errored
88+
89+
// Authenticated indicates that the token review should be allowed for the given reason.
90+
Authenticated = authentication.Authenticated
91+
92+
// Unauthenticated indicates that the token review should be unauthorized for the given reson.
93+
Unauthenticated = authentication.Unauthenticated
94+
95+
// AuthenticationErrored indicates that an error occurred in the token review
96+
AuthenticationErrored = authentication.Errored
7397
)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package authentication
18+
19+
import (
20+
"testing"
21+
22+
. "github.com/onsi/ginkgo"
23+
. "github.com/onsi/gomega"
24+
25+
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
26+
logf "sigs.k8s.io/controller-runtime/pkg/log"
27+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
28+
)
29+
30+
func TestAuthenticationWebhook(t *testing.T) {
31+
RegisterFailHandler(Fail)
32+
suiteName := "Authentication Webhook Suite"
33+
RunSpecsWithDefaultAndCustomReporters(t, suiteName, []Reporter{printer.NewlineReporter{}, printer.NewProwReporter(suiteName)})
34+
}
35+
36+
var _ = BeforeSuite(func(done Done) {
37+
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
38+
39+
close(done)
40+
}, 60)

pkg/webhook/authentication/decoder.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package authentication
18+
19+
import (
20+
"k8s.io/apimachinery/pkg/runtime"
21+
"k8s.io/apimachinery/pkg/runtime/serializer"
22+
)
23+
24+
// Decoder knows how to decode the contents of a CRD version conversion
25+
// request into a concrete object.
26+
// TODO(droot): consider reusing decoder from admission pkg for this.
27+
type Decoder struct {
28+
codecs serializer.CodecFactory
29+
}
30+
31+
// NewDecoder creates a Decoder given the runtime.Scheme
32+
func NewDecoder(scheme *runtime.Scheme) (*Decoder, error) {
33+
return &Decoder{codecs: serializer.NewCodecFactory(scheme)}, nil
34+
}
35+
36+
// DecodeInto decodes the inlined object in the into the passed-in runtime.Object.
37+
func (d *Decoder) DecodeInto(content []byte, into runtime.Object) error {
38+
deserializer := d.codecs.UniversalDeserializer()
39+
return runtime.DecodeInto(deserializer, content, into)
40+
}

pkg/webhook/authentication/doc.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
/*
18+
Package authentication provides implementation for authentication webhook and methods to implement authentication webhook handlers.
19+
20+
See examples/authentication.go for an example of authentication webhooks.
21+
*/
22+
package authentication
23+
24+
import (
25+
logf "sigs.k8s.io/controller-runtime/pkg/internal/log"
26+
)
27+
28+
var log = logf.RuntimeLog.WithName("authentication")

0 commit comments

Comments
 (0)