forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rest.go
52 lines (43 loc) · 2.07 KB
/
rest.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
package localresourceaccessreview
import (
"fmt"
kapi "k8s.io/kubernetes/pkg/api"
kapierrors "k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util/validation/field"
authorizationapi "github.com/openshift/origin/pkg/authorization/api"
authorizationvalidation "github.com/openshift/origin/pkg/authorization/api/validation"
"github.com/openshift/origin/pkg/authorization/registry/resourceaccessreview"
)
// REST implements the RESTStorage interface in terms of an Registry.
type REST struct {
clusterRARRegistry resourceaccessreview.Registry
}
func NewREST(clusterRARRegistry resourceaccessreview.Registry) *REST {
return &REST{clusterRARRegistry}
}
func (r *REST) New() runtime.Object {
return &authorizationapi.LocalResourceAccessReview{}
}
// Create transforms a LocalRAR into an ClusterRAR that is requesting a namespace. That collapses the code paths.
// LocalResourceAccessReview exists to allow clean expression of policy.
func (r *REST) Create(ctx kapi.Context, obj runtime.Object) (runtime.Object, error) {
localRAR, ok := obj.(*authorizationapi.LocalResourceAccessReview)
if !ok {
return nil, kapierrors.NewBadRequest(fmt.Sprintf("not a localResourceAccessReview: %#v", obj))
}
if errs := authorizationvalidation.ValidateLocalResourceAccessReview(localRAR); len(errs) > 0 {
return nil, kapierrors.NewInvalid(authorizationapi.Kind(localRAR.Kind), "", errs)
}
if namespace := kapi.NamespaceValue(ctx); len(namespace) == 0 {
return nil, kapierrors.NewBadRequest(fmt.Sprintf("namespace is required on this type: %v", namespace))
} else if (len(localRAR.Action.Namespace) > 0) && (namespace != localRAR.Action.Namespace) {
return nil, field.Invalid(field.NewPath("namespace"), localRAR.Action.Namespace, fmt.Sprintf("namespace must be: %v", namespace))
}
// transform this into a ResourceAccessReview
clusterRAR := &authorizationapi.ResourceAccessReview{
Action: localRAR.Action,
}
clusterRAR.Action.Namespace = kapi.NamespaceValue(ctx)
return r.clusterRARRegistry.CreateResourceAccessReview(kapi.WithNamespace(ctx, ""), clusterRAR)
}