Skip to content

Commit 65b39cc

Browse files
committed
Add example program for SubjectAccessReviews
1 parent 7cc2d12 commit 65b39cc

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

examples/subjectaccessreview/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/authorization"
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-subjectaccessreview", &authorization.Webhook{Handler: &authorizer{}})
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+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
"sigs.k8s.io/controller-runtime/pkg/webhook/authorization"
23+
)
24+
25+
// authorizer validates subjectaccessreviews
26+
type authorizer struct {
27+
}
28+
29+
// authorizer admits a request by the token.
30+
func (a *authorizer) Handle(ctx context.Context, req authorization.Request) authorization.Response {
31+
if req.Spec.User == "system:anonymous" {
32+
return authorization.Denied("anonymous users are not allowed")
33+
}
34+
if req.Spec.User == "foo" {
35+
return authorization.NoOpinion("I don't care if foo is authorized or not")
36+
}
37+
return authorization.Allowed()
38+
}

0 commit comments

Comments
 (0)