Not create rbac roles on k8s cluster that have rbac disabled.

Fixes https://github.com/rook/rook/issues/1221
This commit is contained in:
Steve Leon committed 2017-12-11 14:55:11 -08:00
1 parent bc529bba63
commit 735d2e7155
4 files changed
+110

No files matched your search

+21
View File
@@ -22,6 +22,7 @@ package k8sutil
import (
"fmt"
"os"
"k8s.io/api/core/v1"
"k8s.io/api/rbac/v1beta1"
@@ -30,6 +31,10 @@ import (
"k8s.io/client-go/kubernetes"
)
const (
enableRBACEnv = "RBAC_ENABLED"
)
func MakeRole(clientset kubernetes.Interface, namespace, name string, rules []v1beta1.PolicyRule) error {
err := makeServiceAccount(clientset, namespace, name)
@@ -37,6 +42,10 @@ func MakeRole(clientset kubernetes.Interface, namespace, name string, rules []v1
return err
}
if !isRBACEnabled() {
return nil
}
// Create the role if it doesn't yet exist.
// If the role already exists we have to update it. Otherwise if the permissions change during an upgrade,
// the create will fail with an error that we're changing the permissions.
@@ -72,6 +81,10 @@ func MakeClusterRole(clientset kubernetes.Interface, namespace, name string, rul
return err
}
if !isRBACEnabled() {
return nil
}
// Create the cluster scoped role if it doesn't yet exist.
// If the role already exists we have to update it. Otherwise if the permissions change during an upgrade,
// the create will fail with an error that we're changing the permissions.
@@ -110,3 +123,11 @@ func makeServiceAccount(clientset kubernetes.Interface, namespace, name string)
}
return nil
}
func isRBACEnabled() bool {
r := os.Getenv(enableRBACEnv)
if r == "false" {
return false
}
return true
}
+82
View File
@@ -21,11 +21,13 @@ which also has the apache 2.0 license.
package k8sutil
import (
"os"
"testing"
"github.com/rook/rook/pkg/operator/test"
"github.com/stretchr/testify/assert"
"k8s.io/api/rbac/v1beta1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
@@ -146,3 +148,83 @@ func TestMakeClusterRole(t *testing.T) {
assert.Equal(t, 1, len(role.Rules[0].Resources))
assert.Equal(t, 2, len(role.Rules[0].Verbs))
}
func TestMakeRoleRBACDisabled(t *testing.T) {
os.Setenv(enableRBACEnv, "false")
defer os.Unsetenv(enableRBACEnv)
clientset := test.New(1)
namespace := "myns"
name := "myapp"
rules := []v1beta1.PolicyRule{
{
APIGroups: []string{""},
Resources: []string{"namespaces", "secrets", "pods", "services", "nodes", "configmaps", "events"},
Verbs: []string{"get", "list", "watch", "create", "update"},
},
{
APIGroups: []string{"extensions"},
Resources: []string{"thirdpartyresources", "deployments", "daemonsets", "replicasets"},
Verbs: []string{"get", "list", "create", "delete"},
},
{
APIGroups: []string{"apiextensions.k8s.io"},
Resources: []string{"customresourcedefinitions"},
Verbs: []string{"get", "list", "create"},
},
{
APIGroups: []string{"storage.k8s.io"},
Resources: []string{"storageclasses"},
Verbs: []string{"get", "list"},
},
}
err := MakeRole(clientset, namespace, name, rules)
_, err = clientset.RbacV1beta1().Roles(namespace).Get(name, metav1.GetOptions{})
assert.NotNil(t, err)
assert.True(t, errors.IsNotFound(err))
account, err := clientset.CoreV1().ServiceAccounts(namespace).Get(name, metav1.GetOptions{})
assert.Nil(t, err)
assert.Equal(t, namespace, account.Namespace)
_, err = clientset.RbacV1beta1().RoleBindings(namespace).Get(name, metav1.GetOptions{})
assert.NotNil(t, err)
assert.True(t, errors.IsNotFound(err))
}
func TestMakeClusterRoleRBACDisabled(t *testing.T) {
os.Setenv(enableRBACEnv, "false")
defer os.Unsetenv(enableRBACEnv)
clientset := test.New(1)
namespace := "myns"
name := "myapp"
rules := []v1beta1.PolicyRule{
{
APIGroups: []string{""},
Resources: []string{"pods", "secrets", "configmaps", "persistentvolumes", "nodes/proxy"},
Verbs: []string{"get", "list"},
},
{
APIGroups: []string{"storage.k8s.io"},
Resources: []string{"storageclasses"},
Verbs: []string{"get"},
},
}
err := MakeClusterRole(clientset, namespace, name, rules)
assert.Nil(t, err)
_, err = clientset.RbacV1beta1().ClusterRoles().Get(name, metav1.GetOptions{})
assert.NotNil(t, err)
assert.True(t, errors.IsNotFound(err))
account, err := clientset.CoreV1().ServiceAccounts(namespace).Get(name, metav1.GetOptions{})
assert.Nil(t, err)
assert.Equal(t, namespace, account.Namespace)
_, err = clientset.RbacV1beta1().ClusterRoleBindings().Get(name, metav1.GetOptions{})
assert.NotNil(t, err)
assert.True(t, errors.IsNotFound(err))
}