Skip to content

Commit b619e46

Browse files
committed
Fix extended impersonation tests
The RoleBinding proxy needs to check namespaces and allow empty namespaces to be defaulted. We do this by forcibly setting both Namespaces to "" if any is empty and the non-empty one matches the request namespace. Signed-off-by: Simo Sorce <[email protected]>
1 parent 61aea5f commit b619e46

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

pkg/authorization/registry/rolebinding/proxy.go

+28-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
apierrors "k8s.io/apimachinery/pkg/api/errors"
55
metainternal "k8s.io/apimachinery/pkg/apis/meta/internalversion"
66
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
7+
"k8s.io/apimachinery/pkg/conversion"
78
"k8s.io/apimachinery/pkg/runtime"
89
"k8s.io/apimachinery/pkg/runtime/schema"
910
apirequest "k8s.io/apiserver/pkg/endpoints/request"
@@ -104,7 +105,31 @@ func (s *REST) Create(ctx apirequest.Context, obj runtime.Object, _ bool) (runti
104105
return nil, err
105106
}
106107

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)
108133
if err != nil {
109134
return nil, err
110135
}
@@ -173,3 +198,5 @@ func (s *REST) getImpersonatingClient(ctx apirequest.Context) (rbacinternalversi
173198
}
174199
return rbacClient.RoleBindings(namespace), nil
175200
}
201+
202+
var cloner = conversion.NewCloner()

test/extended/templates/templateinstance_impersonation.go

-11
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,6 @@ var _ = g.Describe("[templates] templateinstance impersonation tests", func() {
145145
})
146146
o.Expect(err).NotTo(o.HaveOccurred())
147147

148-
_, err = cli.AdminClient().PolicyBindings(cli.Namespace()).Create(&authorizationapi.PolicyBinding{
149-
ObjectMeta: metav1.ObjectMeta{
150-
Name: authorizationapi.GetPolicyBindingName(cli.Namespace()),
151-
},
152-
PolicyRef: kapi.ObjectReference{
153-
Name: "default",
154-
Namespace: cli.Namespace(),
155-
},
156-
})
157-
o.Expect(err).NotTo(o.HaveOccurred())
158-
159148
_, err = cli.AdminClient().RoleBindings(cli.Namespace()).Create(&authorizationapi.RoleBinding{
160149
ObjectMeta: metav1.ObjectMeta{
161150
Name: "impersonater-binding",

0 commit comments

Comments
 (0)