forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrestrictusers.go
218 lines (181 loc) · 6.6 KB
/
restrictusers.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package restrictusers
import (
"errors"
"fmt"
"io"
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
"github.com/golang/glog"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apiserver/pkg/admission"
"k8s.io/kubernetes/pkg/apis/rbac"
kadmission "k8s.io/kubernetes/pkg/kubeapiserver/admission"
authorizationclient "github.com/openshift/origin/pkg/authorization/generated/internalclientset"
authorizationtypedclient "github.com/openshift/origin/pkg/authorization/generated/internalclientset/typed/authorization/internalversion"
oadmission "github.com/openshift/origin/pkg/cmd/server/admission"
userapi "github.com/openshift/origin/pkg/user/apis/user"
usercache "github.com/openshift/origin/pkg/user/cache"
userinformer "github.com/openshift/origin/pkg/user/generated/informers/internalversion"
userclient "github.com/openshift/origin/pkg/user/generated/internalclientset"
)
func Register(plugins *admission.Plugins) {
plugins.Register("openshift.io/RestrictSubjectBindings",
func(config io.Reader) (admission.Interface, error) {
return NewRestrictUsersAdmission()
})
}
type GroupCache interface {
GroupsFor(string) ([]*userapi.Group, error)
}
// restrictUsersAdmission implements admission.Interface and enforces
// restrictions on adding rolebindings in a project to permit only designated
// subjects.
type restrictUsersAdmission struct {
*admission.Handler
roleBindingRestrictionsGetter authorizationtypedclient.RoleBindingRestrictionsGetter
userClient userclient.Interface
kclient kclientset.Interface
groupCache GroupCache
}
var _ = oadmission.WantsOpenshiftInternalAuthorizationClient(&restrictUsersAdmission{})
var _ = oadmission.WantsOpenshiftInternalUserClient(&restrictUsersAdmission{})
var _ = oadmission.WantsUserInformer(&restrictUsersAdmission{})
var _ = kadmission.WantsInternalKubeClientSet(&restrictUsersAdmission{})
// NewRestrictUsersAdmission configures an admission plugin that enforces
// restrictions on adding role bindings in a project.
func NewRestrictUsersAdmission() (admission.Interface, error) {
return &restrictUsersAdmission{
Handler: admission.NewHandler(admission.Create, admission.Update),
}, nil
}
func (q *restrictUsersAdmission) SetInternalKubeClientSet(c kclientset.Interface) {
q.kclient = c
}
func (q *restrictUsersAdmission) SetOpenshiftInternalAuthorizationClient(roleBindingRestrictionsGetter authorizationclient.Interface) {
q.roleBindingRestrictionsGetter = roleBindingRestrictionsGetter.Authorization()
}
func (q *restrictUsersAdmission) SetOpenshiftInternalUserClient(userClient userclient.Interface) {
q.userClient = userClient
}
func (q *restrictUsersAdmission) SetUserInformer(userInformers userinformer.SharedInformerFactory) {
q.groupCache = usercache.NewGroupCache(userInformers.User().InternalVersion().Groups())
}
// subjectsDelta returns the relative complement of elementsToIgnore in
// elements (i.e., elements∖elementsToIgnore).
func subjectsDelta(elementsToIgnore, elements []rbac.Subject) []rbac.Subject {
result := []rbac.Subject{}
for _, el := range elements {
keep := true
for _, skipEl := range elementsToIgnore {
if el == skipEl {
keep = false
break
}
}
if keep {
result = append(result, el)
}
}
return result
}
// Admit makes admission decisions that enforce restrictions on adding
// project-scoped role-bindings. In order for a role binding to be permitted,
// each subject in the binding must be matched by some rolebinding restriction
// in the namespace.
func (q *restrictUsersAdmission) Admit(a admission.Attributes) (err error) {
// We only care about rolebindings
if a.GetResource().GroupResource() != rbac.Resource("rolebindings") {
return nil
}
// Ignore all operations that correspond to subresource actions.
if len(a.GetSubresource()) != 0 {
return nil
}
ns := a.GetNamespace()
// Ignore cluster-level resources.
if len(ns) == 0 {
return nil
}
var oldSubjects []rbac.Subject
obj, oldObj := a.GetObject(), a.GetOldObject()
rolebinding, ok := obj.(*rbac.RoleBinding)
if !ok {
return admission.NewForbidden(a,
fmt.Errorf("wrong object type for new rolebinding: %T", obj))
}
if len(rolebinding.Subjects) == 0 {
glog.V(4).Infof("No new subjects; admitting")
return nil
}
if oldObj != nil {
oldrolebinding, ok := oldObj.(*rbac.RoleBinding)
if !ok {
return admission.NewForbidden(a,
fmt.Errorf("wrong object type for old rolebinding: %T", oldObj))
}
oldSubjects = oldrolebinding.Subjects
}
glog.V(4).Infof("Handling rolebinding %s/%s",
rolebinding.Namespace, rolebinding.Name)
newSubjects := subjectsDelta(oldSubjects, rolebinding.Subjects)
if len(newSubjects) == 0 {
glog.V(4).Infof("No new subjects; admitting")
return nil
}
// TODO: Cache rolebinding restrictions.
roleBindingRestrictionList, err := q.roleBindingRestrictionsGetter.RoleBindingRestrictions(ns).
List(metav1.ListOptions{})
if err != nil {
return admission.NewForbidden(a, err)
}
if len(roleBindingRestrictionList.Items) == 0 {
glog.V(4).Infof("No rolebinding restrictions specified; admitting")
return nil
}
checkers := []SubjectChecker{}
for _, rbr := range roleBindingRestrictionList.Items {
checker, err := NewSubjectChecker(&rbr.Spec)
if err != nil {
return admission.NewForbidden(a, err)
}
checkers = append(checkers, checker)
}
roleBindingRestrictionContext, err := NewRoleBindingRestrictionContext(ns,
q.kclient, q.userClient.User(), q.groupCache)
if err != nil {
return admission.NewForbidden(a, err)
}
checker := NewUnionSubjectChecker(checkers)
errs := []error{}
for _, subject := range newSubjects {
allowed, err := checker.Allowed(subject, roleBindingRestrictionContext)
if err != nil {
errs = append(errs, err)
}
if !allowed {
errs = append(errs,
fmt.Errorf("rolebindings to %s %q are not allowed in project %q",
subject.Kind, subject.Name, ns))
}
}
if len(errs) != 0 {
return admission.NewForbidden(a, kerrors.NewAggregate(errs))
}
glog.V(4).Infof("All new subjects are allowed; admitting")
return nil
}
func (q *restrictUsersAdmission) Validate() error {
if q.kclient == nil {
return errors.New("RestrictUsersAdmission plugin requires a Kubernetes client")
}
if q.roleBindingRestrictionsGetter == nil {
return errors.New("RestrictUsersAdmission plugin requires an OpenShift client")
}
if q.userClient == nil {
return errors.New("RestrictUsersAdmission plugin requires an OpenShift user client")
}
if q.groupCache == nil {
return errors.New("RestrictUsersAdmission plugin requires a group cache")
}
return nil
}