-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
restrictusers.go
241 lines (201 loc) · 6.74 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
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"
kapi "k8s.io/kubernetes/pkg/api"
kadmission "k8s.io/kubernetes/pkg/kubeapiserver/admission"
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
oclient "github.com/openshift/origin/pkg/client"
oadmission "github.com/openshift/origin/pkg/cmd/server/admission"
usercache "github.com/openshift/origin/pkg/user/cache"
)
func Register(plugins *admission.Plugins) {
plugins.Register("openshift.io/RestrictSubjectBindings",
func(config io.Reader) (admission.Interface, error) {
return NewRestrictUsersAdmission()
})
}
// restrictUsersAdmission implements admission.Interface and enforces
// restrictions on adding rolebindings in a project to permit only designated
// subjects.
type restrictUsersAdmission struct {
*admission.Handler
oclient oclient.Interface
kclient kclientset.Interface
groupCache *usercache.GroupCache
}
var _ = oadmission.WantsOpenshiftClient(&restrictUsersAdmission{})
var _ = oadmission.WantsGroupCache(&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) SetOpenshiftClient(c oclient.Interface) {
q.oclient = c
}
func (q *restrictUsersAdmission) SetGroupCache(c *usercache.GroupCache) {
q.groupCache = c
}
// objectReferenceDelta returns the relative complement of
// []ObjectReference elementsToIgnore in []ObjectReference elements
// (i.e., elements∖elementsToIgnore).
func objectReferenceDelta(elementsToIgnore, elements []kapi.ObjectReference) []kapi.ObjectReference {
result := []kapi.ObjectReference{}
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 and policybindings; ignore anything else.
gr := a.GetResource().GroupResource()
switch {
case authorizationapi.IsResourceOrLegacy("policybindings", gr), authorizationapi.IsResourceOrLegacy("rolebindings", gr):
default:
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 subjects, oldSubjects []kapi.ObjectReference
obj, oldObj := a.GetObject(), a.GetOldObject()
switch {
case authorizationapi.IsResourceOrLegacy("rolebindings", gr):
rolebinding, ok := obj.(*authorizationapi.RoleBinding)
if !ok {
return admission.NewForbidden(a,
fmt.Errorf("wrong object type for new rolebinding: %T", obj))
}
subjects = rolebinding.Subjects
if len(subjects) == 0 {
return nil
}
if oldObj != nil {
oldrolebinding, ok := oldObj.(*authorizationapi.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)
case authorizationapi.IsResourceOrLegacy("policybindings", gr):
policybinding, ok := obj.(*authorizationapi.PolicyBinding)
if !ok {
return admission.NewForbidden(a,
fmt.Errorf("wrong object type for new policybinding: %T", obj))
}
for _, rolebinding := range policybinding.RoleBindings {
subjects = append(subjects, rolebinding.Subjects...)
}
if len(subjects) == 0 {
return nil
}
if oldObj != nil {
oldpolicybinding, ok := oldObj.(*authorizationapi.PolicyBinding)
if !ok {
return admission.NewForbidden(a,
fmt.Errorf("wrong object type for old policybinding: %T", oldObj))
}
for _, rolebinding := range oldpolicybinding.RoleBindings {
oldSubjects = append(oldSubjects, rolebinding.Subjects...)
}
}
glog.V(4).Infof("Handling policybinding %s/%s",
policybinding.Namespace, policybinding.Name)
}
newSubjects := objectReferenceDelta(oldSubjects, subjects)
if len(newSubjects) == 0 {
glog.V(4).Infof("No new subjects; admitting")
return nil
}
// TODO: Cache rolebinding restrictions.
roleBindingRestrictionList, err := q.oclient.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
}
if !q.groupCache.Running() {
return admission.NewForbidden(a, errors.New("groupCache not running"))
}
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.oclient, 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.oclient == nil {
return errors.New("RestrictUsersAdmission plugin requires an OpenShift client")
}
if q.groupCache == nil {
return errors.New("RestrictUsersAdmission plugin requires a group cache")
}
return nil
}