forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelegated.go
278 lines (242 loc) · 10.7 KB
/
delegated.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package delegated
import (
"errors"
"fmt"
"strings"
"github.com/golang/glog"
kapierror "k8s.io/apimachinery/pkg/api/errors"
metainternal "k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
apirequest "k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/pkg/registry/rest"
restclient "k8s.io/client-go/rest"
kapi "k8s.io/kubernetes/pkg/api"
authorizationapi "k8s.io/kubernetes/pkg/apis/authorization"
"k8s.io/kubernetes/pkg/apis/rbac"
authorizationclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/authorization/internalversion"
rbaclisters "k8s.io/kubernetes/pkg/client/listers/rbac/internalversion"
"k8s.io/kubernetes/pkg/client/retry"
"k8s.io/kubernetes/pkg/kubectl/resource"
osauthorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
authorizationutil "github.com/openshift/origin/pkg/authorization/util"
configcmd "github.com/openshift/origin/pkg/config/cmd"
projectapi "github.com/openshift/origin/pkg/project/apis/project"
projectclientinternal "github.com/openshift/origin/pkg/project/generated/internalclientset/typed/project/internalversion"
projectrequestregistry "github.com/openshift/origin/pkg/project/registry/projectrequest"
templateapi "github.com/openshift/origin/pkg/template/apis/template"
templateinternalclient "github.com/openshift/origin/pkg/template/client/internalversion"
templateclient "github.com/openshift/origin/pkg/template/generated/internalclientset"
restutil "github.com/openshift/origin/pkg/util/rest"
)
type REST struct {
message string
templateNamespace string
templateName string
sarClient authorizationclient.SubjectAccessReviewInterface
projectGetter projectclientinternal.ProjectsGetter
templateClient templateclient.Interface
restConfig *restclient.Config
// policyBindings is an auth cache that is shared with the authorizer for the API server.
// we use this cache to detect when the authorizer has observed the change for the auth rules
roleBindings rbaclisters.RoleBindingLister
}
var _ rest.Lister = &REST{}
var _ rest.Creater = &REST{}
func NewREST(message, templateNamespace, templateName string, projectClient projectclientinternal.ProjectsGetter, templateClient templateclient.Interface, sarClient authorizationclient.SubjectAccessReviewInterface, restConfig *restclient.Config, roleBindings rbaclisters.RoleBindingLister) *REST {
return &REST{
message: message,
templateNamespace: templateNamespace,
templateName: templateName,
projectGetter: projectClient,
templateClient: templateClient,
sarClient: sarClient,
restConfig: restConfig,
roleBindings: roleBindings,
}
}
func (r *REST) New() runtime.Object {
return &projectapi.ProjectRequest{}
}
func (r *REST) NewList() runtime.Object {
return &metav1.Status{}
}
var _ = rest.Creater(&REST{})
var (
ForbiddenNames = []string{"openshift", "kubernetes", "kube"}
ForbiddenPrefixes = []string{"openshift-", "kubernetes-", "kube-"}
)
func (r *REST) Create(ctx apirequest.Context, obj runtime.Object, includeUninitialized bool) (runtime.Object, error) {
if err := rest.BeforeCreate(projectrequestregistry.Strategy, ctx, obj); err != nil {
return nil, err
}
projectRequest := obj.(*projectapi.ProjectRequest)
for _, s := range ForbiddenNames {
if projectRequest.Name == s {
return nil, kapierror.NewForbidden(projectapi.Resource("project"), projectRequest.Name, fmt.Errorf("cannot request a project with the name %q", s))
}
}
for _, s := range ForbiddenPrefixes {
if strings.HasPrefix(projectRequest.Name, s) {
return nil, kapierror.NewForbidden(projectapi.Resource("project"), projectRequest.Name, fmt.Errorf("cannot request a project starting with %q", s))
}
}
if _, err := r.projectGetter.Projects().Get(projectRequest.Name, metav1.GetOptions{}); err == nil {
return nil, kapierror.NewAlreadyExists(projectapi.Resource("project"), projectRequest.Name)
}
projectName := projectRequest.Name
projectAdmin := ""
projectRequester := ""
if userInfo, exists := apirequest.UserFrom(ctx); exists {
projectAdmin = userInfo.GetName()
projectRequester = userInfo.GetName()
}
template, err := r.getTemplate()
if err != nil {
return nil, err
}
for i := range template.Parameters {
switch template.Parameters[i].Name {
case ProjectAdminUserParam:
template.Parameters[i].Value = projectAdmin
case ProjectDescriptionParam:
template.Parameters[i].Value = projectRequest.Description
case ProjectDisplayNameParam:
template.Parameters[i].Value = projectRequest.DisplayName
case ProjectNameParam:
template.Parameters[i].Value = projectName
case ProjectRequesterParam:
template.Parameters[i].Value = projectRequester
}
}
tc := templateinternalclient.NewTemplateProcessorClient(r.templateClient.Template().RESTClient(), metav1.NamespaceDefault)
list, err := tc.Process(template)
if err != nil {
return nil, err
}
if err := utilerrors.NewAggregate(runtime.DecodeList(list.Objects, kapi.Codecs.UniversalDecoder())); err != nil {
return nil, kapierror.NewInternalError(err)
}
// one of the items in this list should be the project. We are going to locate it, remove it from the list, create it separately
var projectFromTemplate *projectapi.Project
lastRoleBindingName := ""
objectsToCreate := &kapi.List{}
for i := range list.Objects {
switch t := list.Objects[i].(type) {
case *projectapi.Project:
if projectFromTemplate != nil {
return nil, kapierror.NewInternalError(fmt.Errorf("the project template (%s/%s) is not correctly configured: must contain only one project resource", r.templateNamespace, r.templateName))
}
projectFromTemplate = t
// don't add this to the list to create. We'll create the project separately.
continue
case *rbac.RoleBinding:
lastRoleBindingName = t.Name
case *osauthorizationapi.RoleBinding:
lastRoleBindingName = t.Name
default:
// noop, we care only for special handling projects and roles
}
// use list.Objects[i] in append to avoid range memory address reuse
objectsToCreate.Items = append(objectsToCreate.Items, list.Objects[i])
}
if projectFromTemplate == nil {
return nil, kapierror.NewInternalError(fmt.Errorf("the project template (%s/%s) is not correctly configured: must contain a project resource", r.templateNamespace, r.templateName))
}
// we split out project creation separately so that in a case of racers for the same project, only one will win and create the rest of their template objects
createdProject, err := r.projectGetter.Projects().Create(projectFromTemplate)
if err != nil {
// log errors other than AlreadyExists and Forbidden
if !kapierror.IsAlreadyExists(err) && !kapierror.IsForbidden(err) {
utilruntime.HandleError(fmt.Errorf("error creating requested project %#v: %v", projectFromTemplate, err))
}
return nil, err
}
// Stop on the first error, since we have to delete the whole project if any item in the template fails
stopOnErr := configcmd.AfterFunc(func(_ *resource.Info, err error) bool {
return err != nil
})
bulk := configcmd.Bulk{
Mapper: &resource.Mapper{
RESTMapper: restutil.DefaultMultiRESTMapper(),
ObjectTyper: kapi.Scheme,
ClientMapper: configcmd.ClientMapperFromConfig(r.restConfig),
},
After: stopOnErr,
Op: configcmd.Create,
}
if err := utilerrors.NewAggregate(bulk.Run(objectsToCreate, createdProject.Name)); err != nil {
utilruntime.HandleError(fmt.Errorf("error creating items in requested project %q: %v", createdProject.Name, err))
// We have to clean up the project if any part of the project request template fails
if deleteErr := r.projectGetter.Projects().Delete(createdProject.Name, &metav1.DeleteOptions{}); deleteErr != nil {
utilruntime.HandleError(fmt.Errorf("error cleaning up requested project %q: %v", createdProject.Name, deleteErr))
}
return nil, kapierror.NewInternalError(err)
}
// wait for a rolebinding if we created one
if len(lastRoleBindingName) != 0 {
r.waitForRoleBinding(createdProject.Name, lastRoleBindingName)
}
return r.projectGetter.Projects().Get(createdProject.Name, metav1.GetOptions{})
}
func (r *REST) waitForRoleBinding(namespace, name string) {
// we have a rolebinding, the we check the cache we have to see if its been updated with this rolebinding
// if you share a cache with our authorizer (you should), then this will let you know when the authorizer is ready.
// doesn't matter if this failed. When the call returns, return. If we have access great. If not, oh well.
backoff := retry.DefaultBackoff
backoff.Steps = 6 // this effectively waits for 6-ish seconds
err := wait.ExponentialBackoff(backoff, func() (bool, error) {
_, err := r.roleBindings.RoleBindings(namespace).Get(name)
return err == nil, nil
})
if err != nil {
glog.V(4).Infof("authorization cache failed to update for %v %v: %v", namespace, name, err)
}
}
func (r *REST) getTemplate() (*templateapi.Template, error) {
if len(r.templateNamespace) == 0 || len(r.templateName) == 0 {
return DefaultTemplate(), nil
}
return r.templateClient.Template().Templates(r.templateNamespace).Get(r.templateName, metav1.GetOptions{})
}
var _ = rest.Lister(&REST{})
func (r *REST) List(ctx apirequest.Context, options *metainternal.ListOptions) (runtime.Object, error) {
userInfo, exists := apirequest.UserFrom(ctx)
if !exists {
return nil, errors.New("a user must be provided")
}
// the caller might not have permission to run a subject access review (he has it by default, but it could have been removed).
// So we'll escalate for the subject access review to determine rights
accessReview := authorizationutil.AddUserToSAR(userInfo, &authorizationapi.SubjectAccessReview{
Spec: authorizationapi.SubjectAccessReviewSpec{
ResourceAttributes: &authorizationapi.ResourceAttributes{
Verb: "create",
Group: projectapi.GroupName,
Resource: "projectrequests",
},
},
})
accessReviewResponse, err := r.sarClient.Create(accessReview)
if err != nil {
return nil, err
}
if accessReviewResponse.Status.Allowed {
return &metav1.Status{Status: metav1.StatusSuccess}, nil
}
forbiddenError := kapierror.NewForbidden(projectapi.Resource("projectrequest"), "", errors.New("you may not request a new project via this API."))
if len(r.message) > 0 {
forbiddenError.ErrStatus.Message = r.message
forbiddenError.ErrStatus.Details = &metav1.StatusDetails{
Kind: "ProjectRequest",
Causes: []metav1.StatusCause{
{Message: r.message},
},
}
} else {
forbiddenError.ErrStatus.Message = "You may not request a new project via this API."
}
return nil, forbiddenError
}