forked from openshift/origin
/
admission.go
139 lines (116 loc) · 4.12 KB
/
admission.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
Copyright 2017 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 authsarcheck
import (
"fmt"
"io"
"github.com/golang/glog"
"github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog"
authorizationapi "k8s.io/api/authorization/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apiserver/pkg/admission"
kubeclientset "k8s.io/client-go/kubernetes"
scadmission "github.com/kubernetes-incubator/service-catalog/pkg/apiserver/admission"
)
const (
pluginName = "BrokerAuthSarCheck"
)
// Register registers a plugin
func Register(plugins *admission.Plugins) {
plugins.Register(pluginName, func(io.Reader) (admission.Interface, error) {
return NewSARCheck()
})
}
// sarcheck is an implementation of admission.Interface.
// It enforces the creator of a broker has proper access to the auth credentials
type sarcheck struct {
*admission.Handler
client kubeclientset.Interface
}
var _ = scadmission.WantsKubeClientSet(&sarcheck{})
func convertToSARExtra(extra map[string][]string) map[string]authorizationapi.ExtraValue {
if extra == nil {
return nil
}
ret := map[string]authorizationapi.ExtraValue{}
for k, v := range extra {
ret[k] = authorizationapi.ExtraValue(v)
}
return ret
}
func (s *sarcheck) Admit(a admission.Attributes) error {
// need to wait for our caches to warm
if !s.WaitForReady() {
return admission.NewForbidden(a, fmt.Errorf("not yet ready to handle request"))
}
// only care about brokers
if a.GetResource().Group != servicecatalog.GroupName || a.GetResource().GroupResource() != servicecatalog.Resource("clusterservicebrokers") {
return nil
}
clusterServiceBroker, ok := a.GetObject().(*servicecatalog.ClusterServiceBroker)
if !ok {
return errors.NewBadRequest("Resource was marked with kind ClusterServiceBroker, but was unable to be converted")
}
if clusterServiceBroker.Spec.AuthInfo == nil {
// no auth secret to check
return nil
}
var secretRef *servicecatalog.ObjectReference
if clusterServiceBroker.Spec.AuthInfo.Basic != nil {
secretRef = clusterServiceBroker.Spec.AuthInfo.Basic.SecretRef
} else if clusterServiceBroker.Spec.AuthInfo.Bearer != nil {
secretRef = clusterServiceBroker.Spec.AuthInfo.Bearer.SecretRef
}
glog.V(5).Infof("ClusterServiceBroker %q: evaluating auth secret ref", clusterServiceBroker)
userInfo := a.GetUserInfo()
sar := &authorizationapi.SubjectAccessReview{
Spec: authorizationapi.SubjectAccessReviewSpec{
ResourceAttributes: &authorizationapi.ResourceAttributes{
Namespace: secretRef.Namespace,
Verb: "get",
Group: corev1.SchemeGroupVersion.Group,
Version: corev1.SchemeGroupVersion.Version,
Resource: corev1.ResourceSecrets.String(),
Name: secretRef.Name,
},
User: userInfo.GetName(),
Groups: userInfo.GetGroups(),
Extra: convertToSARExtra(userInfo.GetExtra()),
UID: userInfo.GetUID(),
},
}
sar, err := s.client.AuthorizationV1().SubjectAccessReviews().Create(sar)
if err != nil {
return err
}
if !sar.Status.Allowed {
return admission.NewForbidden(a, fmt.Errorf("broker forbidden access to auth secret (%s): Reason: %s, EvaluationError: %s", secretRef.Name, sar.Status.Reason, sar.Status.EvaluationError))
}
return nil
}
// NewSARCheck creates a new subject access review check admission control handler
func NewSARCheck() (admission.Interface, error) {
return &sarcheck{
Handler: admission.NewHandler(admission.Create, admission.Update),
}, nil
}
func (s *sarcheck) SetKubeClientSet(client kubeclientset.Interface) {
s.client = client
}
func (s *sarcheck) Validate() error {
if s.client == nil {
return fmt.Errorf("missing client")
}
return nil
}