|
4 | 4 | apierrors "k8s.io/apimachinery/pkg/api/errors"
|
5 | 5 | metainternal "k8s.io/apimachinery/pkg/apis/meta/internalversion"
|
6 | 6 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
| 7 | + "k8s.io/apimachinery/pkg/conversion" |
7 | 8 | "k8s.io/apimachinery/pkg/runtime"
|
8 | 9 | "k8s.io/apimachinery/pkg/runtime/schema"
|
9 | 10 | apirequest "k8s.io/apiserver/pkg/endpoints/request"
|
@@ -104,7 +105,31 @@ func (s *REST) Create(ctx apirequest.Context, obj runtime.Object, _ bool) (runti
|
104 | 105 | return nil, err
|
105 | 106 | }
|
106 | 107 |
|
107 |
| - convertedObj, err := util.RoleBindingToRBAC(obj.(*authorizationapi.RoleBinding)) |
| 108 | + /* Validate Namespaces on creation. This is needed because there is some |
| 109 | + * namespacing defaulting allowed in the API but the conversion function are |
| 110 | + * strict in what they allow */ |
| 111 | + rbObj := obj.(*authorizationapi.RoleBinding) |
| 112 | + if len(rbObj.Namespace) != 0 || len(rbObj.RoleRef.Namespace) != 0 { |
| 113 | + ns := apirequest.NamespaceValue(ctx) |
| 114 | + if len(rbObj.Namespace) != 0 && rbObj.Namespace != ns { |
| 115 | + return nil, apierrors.NewBadRequest("The namespace used in the object does not match the namespace of the request") |
| 116 | + } |
| 117 | + if len(rbObj.RoleRef.Namespace) != 0 && rbObj.RoleRef.Namespace != ns { |
| 118 | + return nil, apierrors.NewBadRequest("The namespace used in the object does not match the namespace of the request") |
| 119 | + } |
| 120 | + |
| 121 | + /* If either Namespace is "" then reset both and let the RBAC api set |
| 122 | + * the right namesapces on the objects during validation */ |
| 123 | + deepcopiedObj := &authorizationapi.RoleBinding{} |
| 124 | + if err := authorizationapi.DeepCopy_authorization_RoleBinding(rbObj, deepcopiedObj, cloner); err != nil { |
| 125 | + return nil, err |
| 126 | + } |
| 127 | + deepcopiedObj.Namespace = "" |
| 128 | + deepcopiedObj.RoleRef.Namespace = "" |
| 129 | + rbObj = deepcopiedObj |
| 130 | + } |
| 131 | + |
| 132 | + convertedObj, err := util.RoleBindingToRBAC(rbObj) |
108 | 133 | if err != nil {
|
109 | 134 | return nil, err
|
110 | 135 | }
|
@@ -173,3 +198,5 @@ func (s *REST) getImpersonatingClient(ctx apirequest.Context) (rbacinternalversi
|
173 | 198 | }
|
174 | 199 | return rbacClient.RoleBindings(namespace), nil
|
175 | 200 | }
|
| 201 | + |
| 202 | +var cloner = conversion.NewCloner() |
0 commit comments