2016-07-27 14:29:31 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-09-23 19:10:47 +00:00
|
|
|
package rest
|
2016-07-27 14:29:31 +00:00
|
|
|
|
|
|
|
import (
|
2016-08-26 15:06:27 +00:00
|
|
|
"fmt"
|
2016-07-27 14:29:31 +00:00
|
|
|
"sync"
|
2016-12-22 13:19:11 +00:00
|
|
|
"time"
|
2016-07-27 14:29:31 +00:00
|
|
|
|
2016-08-26 15:06:27 +00:00
|
|
|
"github.com/golang/glog"
|
|
|
|
|
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2016-07-27 14:29:31 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/rest"
|
|
|
|
"k8s.io/kubernetes/pkg/apis/rbac"
|
|
|
|
rbacapiv1alpha1 "k8s.io/kubernetes/pkg/apis/rbac/v1alpha1"
|
|
|
|
rbacvalidation "k8s.io/kubernetes/pkg/apis/rbac/validation"
|
2016-10-21 22:24:05 +00:00
|
|
|
rbacclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/rbac/internalversion"
|
2016-07-27 14:29:31 +00:00
|
|
|
"k8s.io/kubernetes/pkg/genericapiserver"
|
2016-12-01 02:09:56 +00:00
|
|
|
"k8s.io/kubernetes/pkg/registry/generic"
|
2016-09-15 17:41:48 +00:00
|
|
|
"k8s.io/kubernetes/pkg/registry/rbac/clusterrole"
|
|
|
|
clusterroleetcd "k8s.io/kubernetes/pkg/registry/rbac/clusterrole/etcd"
|
|
|
|
clusterrolepolicybased "k8s.io/kubernetes/pkg/registry/rbac/clusterrole/policybased"
|
|
|
|
"k8s.io/kubernetes/pkg/registry/rbac/clusterrolebinding"
|
|
|
|
clusterrolebindingetcd "k8s.io/kubernetes/pkg/registry/rbac/clusterrolebinding/etcd"
|
|
|
|
clusterrolebindingpolicybased "k8s.io/kubernetes/pkg/registry/rbac/clusterrolebinding/policybased"
|
|
|
|
"k8s.io/kubernetes/pkg/registry/rbac/role"
|
|
|
|
roleetcd "k8s.io/kubernetes/pkg/registry/rbac/role/etcd"
|
|
|
|
rolepolicybased "k8s.io/kubernetes/pkg/registry/rbac/role/policybased"
|
|
|
|
"k8s.io/kubernetes/pkg/registry/rbac/rolebinding"
|
|
|
|
rolebindingetcd "k8s.io/kubernetes/pkg/registry/rbac/rolebinding/etcd"
|
|
|
|
rolebindingpolicybased "k8s.io/kubernetes/pkg/registry/rbac/rolebinding/policybased"
|
2016-08-26 15:06:27 +00:00
|
|
|
utilruntime "k8s.io/kubernetes/pkg/util/runtime"
|
2016-12-22 13:19:11 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/wait"
|
2016-08-26 15:06:27 +00:00
|
|
|
"k8s.io/kubernetes/plugin/pkg/auth/authorizer/rbac/bootstrappolicy"
|
2016-07-27 14:29:31 +00:00
|
|
|
)
|
|
|
|
|
2016-12-05 16:28:59 +00:00
|
|
|
type RESTStorageProvider struct{}
|
2016-07-27 14:29:31 +00:00
|
|
|
|
2016-10-27 18:24:11 +00:00
|
|
|
var _ genericapiserver.PostStartHookProvider = RESTStorageProvider{}
|
2016-07-27 14:29:31 +00:00
|
|
|
|
2016-12-01 02:09:56 +00:00
|
|
|
func (p RESTStorageProvider) NewRESTStorage(apiResourceConfigSource genericapiserver.APIResourceConfigSource, restOptionsGetter generic.RESTOptionsGetter) (genericapiserver.APIGroupInfo, bool) {
|
2016-07-27 14:29:31 +00:00
|
|
|
apiGroupInfo := genericapiserver.NewDefaultAPIGroupInfo(rbac.GroupName)
|
|
|
|
|
|
|
|
if apiResourceConfigSource.AnyResourcesForVersionEnabled(rbacapiv1alpha1.SchemeGroupVersion) {
|
|
|
|
apiGroupInfo.VersionedResourcesStorageMap[rbacapiv1alpha1.SchemeGroupVersion.Version] = p.v1alpha1Storage(apiResourceConfigSource, restOptionsGetter)
|
|
|
|
apiGroupInfo.GroupMeta.GroupVersion = rbacapiv1alpha1.SchemeGroupVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
return apiGroupInfo, true
|
|
|
|
}
|
|
|
|
|
2016-12-01 02:09:56 +00:00
|
|
|
func (p RESTStorageProvider) v1alpha1Storage(apiResourceConfigSource genericapiserver.APIResourceConfigSource, restOptionsGetter generic.RESTOptionsGetter) map[string]rest.Storage {
|
2016-07-27 14:29:31 +00:00
|
|
|
version := rbacapiv1alpha1.SchemeGroupVersion
|
|
|
|
|
|
|
|
once := new(sync.Once)
|
2016-12-01 02:09:56 +00:00
|
|
|
var (
|
|
|
|
authorizationRuleResolver rbacvalidation.AuthorizationRuleResolver
|
|
|
|
rolesStorage rest.StandardStorage
|
|
|
|
roleBindingsStorage rest.StandardStorage
|
|
|
|
clusterRolesStorage rest.StandardStorage
|
|
|
|
clusterRoleBindingsStorage rest.StandardStorage
|
|
|
|
)
|
|
|
|
|
|
|
|
initializeStorage := func() {
|
2016-07-27 14:29:31 +00:00
|
|
|
once.Do(func() {
|
2016-12-01 02:09:56 +00:00
|
|
|
rolesStorage = roleetcd.NewREST(restOptionsGetter)
|
|
|
|
roleBindingsStorage = rolebindingetcd.NewREST(restOptionsGetter)
|
|
|
|
clusterRolesStorage = clusterroleetcd.NewREST(restOptionsGetter)
|
|
|
|
clusterRoleBindingsStorage = clusterrolebindingetcd.NewREST(restOptionsGetter)
|
|
|
|
|
2016-07-27 14:29:31 +00:00
|
|
|
authorizationRuleResolver = rbacvalidation.NewDefaultRuleResolver(
|
2016-12-01 02:09:56 +00:00
|
|
|
role.AuthorizerAdapter{Registry: role.NewRegistry(rolesStorage)},
|
|
|
|
rolebinding.AuthorizerAdapter{Registry: rolebinding.NewRegistry(roleBindingsStorage)},
|
|
|
|
clusterrole.AuthorizerAdapter{Registry: clusterrole.NewRegistry(clusterRolesStorage)},
|
|
|
|
clusterrolebinding.AuthorizerAdapter{Registry: clusterrolebinding.NewRegistry(clusterRoleBindingsStorage)},
|
2016-07-27 14:29:31 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
storage := map[string]rest.Storage{}
|
|
|
|
if apiResourceConfigSource.ResourceEnabled(version.WithResource("roles")) {
|
2016-12-01 02:09:56 +00:00
|
|
|
initializeStorage()
|
|
|
|
storage["roles"] = rolepolicybased.NewStorage(rolesStorage, authorizationRuleResolver)
|
2016-07-27 14:29:31 +00:00
|
|
|
}
|
|
|
|
if apiResourceConfigSource.ResourceEnabled(version.WithResource("rolebindings")) {
|
2016-12-01 02:09:56 +00:00
|
|
|
initializeStorage()
|
|
|
|
storage["rolebindings"] = rolebindingpolicybased.NewStorage(roleBindingsStorage, authorizationRuleResolver)
|
2016-07-27 14:29:31 +00:00
|
|
|
}
|
|
|
|
if apiResourceConfigSource.ResourceEnabled(version.WithResource("clusterroles")) {
|
2016-12-01 02:09:56 +00:00
|
|
|
initializeStorage()
|
|
|
|
storage["clusterroles"] = clusterrolepolicybased.NewStorage(clusterRolesStorage, authorizationRuleResolver)
|
2016-07-27 14:29:31 +00:00
|
|
|
}
|
|
|
|
if apiResourceConfigSource.ResourceEnabled(version.WithResource("clusterrolebindings")) {
|
2016-12-01 02:09:56 +00:00
|
|
|
initializeStorage()
|
|
|
|
storage["clusterrolebindings"] = clusterrolebindingpolicybased.NewStorage(clusterRoleBindingsStorage, authorizationRuleResolver)
|
2016-07-27 14:29:31 +00:00
|
|
|
}
|
|
|
|
return storage
|
|
|
|
}
|
2016-08-26 15:06:27 +00:00
|
|
|
|
2016-10-27 18:24:11 +00:00
|
|
|
func (p RESTStorageProvider) PostStartHook() (string, genericapiserver.PostStartHookFunc, error) {
|
2016-09-29 19:10:04 +00:00
|
|
|
return "rbac/bootstrap-roles", PostStartHook, nil
|
2016-08-26 15:06:27 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 19:10:04 +00:00
|
|
|
func PostStartHook(hookContext genericapiserver.PostStartHookContext) error {
|
2016-12-22 13:19:11 +00:00
|
|
|
// intializing roles is really important. On some e2e runs, we've seen cases where etcd is down when the server
|
|
|
|
// starts, the roles don't initialize, and nothing works.
|
|
|
|
err := wait.Poll(1*time.Second, 30*time.Second, func() (done bool, err error) {
|
|
|
|
clientset, err := rbacclient.NewForConfig(hookContext.LoopbackClientConfig)
|
|
|
|
if err != nil {
|
2016-09-29 19:10:04 +00:00
|
|
|
utilruntime.HandleError(fmt.Errorf("unable to initialize clusterroles: %v", err))
|
2016-12-22 13:19:11 +00:00
|
|
|
return false, nil
|
2016-08-26 15:06:27 +00:00
|
|
|
}
|
2016-09-29 19:10:04 +00:00
|
|
|
|
2016-12-22 13:19:11 +00:00
|
|
|
existingClusterRoles, err := clientset.ClusterRoles().List(api.ListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("unable to initialize clusterroles: %v", err))
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
// only initialized on empty etcd
|
|
|
|
if len(existingClusterRoles.Items) == 0 {
|
|
|
|
for _, clusterRole := range append(bootstrappolicy.ClusterRoles(), bootstrappolicy.ControllerRoles()...) {
|
|
|
|
if _, err := clientset.ClusterRoles().Create(&clusterRole); err != nil {
|
|
|
|
// don't fail on failures, try to create as many as you can
|
|
|
|
utilruntime.HandleError(fmt.Errorf("unable to initialize clusterroles: %v", err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
glog.Infof("Created clusterrole.%s/%s", rbac.GroupName, clusterRole.Name)
|
|
|
|
}
|
|
|
|
}
|
2016-10-04 14:34:01 +00:00
|
|
|
|
2016-12-22 13:19:11 +00:00
|
|
|
existingClusterRoleBindings, err := clientset.ClusterRoleBindings().List(api.ListOptions{})
|
|
|
|
if err != nil {
|
2016-10-04 14:34:01 +00:00
|
|
|
utilruntime.HandleError(fmt.Errorf("unable to initialize clusterrolebindings: %v", err))
|
2016-12-22 13:19:11 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
// only initialized on empty etcd
|
|
|
|
if len(existingClusterRoleBindings.Items) == 0 {
|
|
|
|
for _, clusterRoleBinding := range append(bootstrappolicy.ClusterRoleBindings(), bootstrappolicy.ControllerRoleBindings()...) {
|
|
|
|
if _, err := clientset.ClusterRoleBindings().Create(&clusterRoleBinding); err != nil {
|
|
|
|
// don't fail on failures, try to create as many as you can
|
|
|
|
utilruntime.HandleError(fmt.Errorf("unable to initialize clusterrolebindings: %v", err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
glog.Infof("Created clusterrolebinding.%s/%s", rbac.GroupName, clusterRoleBinding.Name)
|
|
|
|
}
|
2016-10-04 14:34:01 +00:00
|
|
|
}
|
2016-12-22 13:19:11 +00:00
|
|
|
|
|
|
|
return true, nil
|
|
|
|
})
|
|
|
|
// if we're never able to make it through intialization, kill the API server
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to initialize roles: %v", err)
|
2016-10-04 14:34:01 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 19:10:04 +00:00
|
|
|
return nil
|
2016-08-26 15:06:27 +00:00
|
|
|
}
|
2016-10-27 18:24:11 +00:00
|
|
|
|
|
|
|
func (p RESTStorageProvider) GroupName() string {
|
|
|
|
return rbac.GroupName
|
|
|
|
}
|