|
1 | 1 | package resolver
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "github.com/stretchr/testify/require" |
5 | 4 | "math/rand"
|
6 | 5 | "reflect"
|
7 | 6 | "strings"
|
8 | 7 | "testing"
|
9 | 8 | "testing/quick"
|
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + rbacv1 "k8s.io/api/rbac/v1" |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + |
| 15 | + "github.com/operator-framework/api/pkg/operators/v1alpha1" |
10 | 16 | )
|
11 | 17 |
|
12 | 18 | func TestGenerateName(t *testing.T) {
|
@@ -63,3 +69,167 @@ func TestGeneratesWithinRange(t *testing.T) {
|
63 | 69 | }
|
64 | 70 | require.NoError(t, quick.Check(f, nil))
|
65 | 71 | }
|
| 72 | + |
| 73 | +func TestRBACForClusterServiceVersion(t *testing.T) { |
| 74 | + serviceAccount1 := "test-service-account" |
| 75 | + serviceAccount2 := "second-service-account" |
| 76 | + csvName := "test-csv.v1.1.0" |
| 77 | + |
| 78 | + rules := []rbacv1.PolicyRule{ |
| 79 | + { |
| 80 | + Verbs: []string{"get"}, |
| 81 | + APIGroups: []string{""}, |
| 82 | + Resources: []string{"pods"}, |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + // Note: two CSVs have same name and permissions for a cluster role, this is chosen intentionally, |
| 87 | + // to verify that ClusterRole and ClusterRoleBinding have different names when the same CSV is installed |
| 88 | + // twice in the same cluster, but in different namespaces. |
| 89 | + tests := []struct { |
| 90 | + name string |
| 91 | + csv v1alpha1.ClusterServiceVersion |
| 92 | + want map[string]*OperatorPermissions |
| 93 | + }{ |
| 94 | + { |
| 95 | + name: "RoleBindings and one ClusterRoleBinding", |
| 96 | + csv: v1alpha1.ClusterServiceVersion{ |
| 97 | + ObjectMeta: metav1.ObjectMeta{ |
| 98 | + Name: csvName, |
| 99 | + Namespace: "test-namespace", |
| 100 | + }, |
| 101 | + Spec: v1alpha1.ClusterServiceVersionSpec{ |
| 102 | + InstallStrategy: v1alpha1.NamedInstallStrategy{ |
| 103 | + StrategyName: v1alpha1.InstallStrategyNameDeployment, |
| 104 | + StrategySpec: v1alpha1.StrategyDetailsDeployment{ |
| 105 | + Permissions: []v1alpha1.StrategyDeploymentPermissions{ |
| 106 | + { |
| 107 | + ServiceAccountName: serviceAccount1, |
| 108 | + Rules: rules, |
| 109 | + }, |
| 110 | + { |
| 111 | + ServiceAccountName: serviceAccount1, |
| 112 | + Rules: []rbacv1.PolicyRule{ |
| 113 | + { |
| 114 | + Verbs: []string{"get"}, |
| 115 | + APIGroups: []string{""}, |
| 116 | + Resources: []string{"deployments"}, |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + { |
| 121 | + ServiceAccountName: serviceAccount2, |
| 122 | + Rules: rules, |
| 123 | + }, |
| 124 | + }, |
| 125 | + ClusterPermissions: []v1alpha1.StrategyDeploymentPermissions{ |
| 126 | + { |
| 127 | + ServiceAccountName: serviceAccount1, |
| 128 | + Rules: rules, |
| 129 | + }, |
| 130 | + }, |
| 131 | + }, |
| 132 | + }, |
| 133 | + }, |
| 134 | + }, |
| 135 | + want: map[string]*OperatorPermissions{ |
| 136 | + serviceAccount1: { |
| 137 | + RoleBindings: []*rbacv1.RoleBinding{{}, {}}, |
| 138 | + ClusterRoleBindings: []*rbacv1.ClusterRoleBinding{{}}, |
| 139 | + }, |
| 140 | + serviceAccount2: { |
| 141 | + RoleBindings: []*rbacv1.RoleBinding{{}}, |
| 142 | + }, |
| 143 | + }, |
| 144 | + }, |
| 145 | + { |
| 146 | + name: "ClusterRoleBinding", |
| 147 | + csv: v1alpha1.ClusterServiceVersion{ |
| 148 | + ObjectMeta: metav1.ObjectMeta{ |
| 149 | + Name: csvName, |
| 150 | + Namespace: "second-namespace", |
| 151 | + }, |
| 152 | + Spec: v1alpha1.ClusterServiceVersionSpec{ |
| 153 | + InstallStrategy: v1alpha1.NamedInstallStrategy{ |
| 154 | + StrategyName: v1alpha1.InstallStrategyNameDeployment, |
| 155 | + StrategySpec: v1alpha1.StrategyDetailsDeployment{ |
| 156 | + ClusterPermissions: []v1alpha1.StrategyDeploymentPermissions{ |
| 157 | + { |
| 158 | + ServiceAccountName: serviceAccount1, |
| 159 | + Rules: rules, |
| 160 | + }, |
| 161 | + { |
| 162 | + ServiceAccountName: serviceAccount2, |
| 163 | + Rules: rules, |
| 164 | + }, |
| 165 | + }, |
| 166 | + }, |
| 167 | + }, |
| 168 | + }, |
| 169 | + }, |
| 170 | + want: map[string]*OperatorPermissions{ |
| 171 | + serviceAccount1: { |
| 172 | + ClusterRoleBindings: []*rbacv1.ClusterRoleBinding{{}}, |
| 173 | + }, |
| 174 | + serviceAccount2: { |
| 175 | + ClusterRoleBindings: []*rbacv1.ClusterRoleBinding{{}}, |
| 176 | + }, |
| 177 | + }, |
| 178 | + }, |
| 179 | + } |
| 180 | + |
| 181 | + // declared here to verify that names are unique when same csv is install in different namespaces |
| 182 | + clusterRoleBindingNames := map[string]bool{} |
| 183 | + clusterRolesNames := map[string]bool{} |
| 184 | + |
| 185 | + for _, tt := range tests { |
| 186 | + t.Run(tt.name, func(t *testing.T) { |
| 187 | + result, err := RBACForClusterServiceVersion(&tt.csv) |
| 188 | + require.NoError(t, err) |
| 189 | + |
| 190 | + roleBindingNames := map[string]bool{} |
| 191 | + rolesNames := map[string]bool{} |
| 192 | + for serviceAccount, permissions := range tt.want { |
| 193 | + // Check that correct number of bindings is created |
| 194 | + require.Equal(t, len(permissions.RoleBindings), len(result[serviceAccount].RoleBindings)) |
| 195 | + require.Equal(t, len(permissions.ClusterRoleBindings), len(result[serviceAccount].ClusterRoleBindings)) |
| 196 | + |
| 197 | + // Check that testing ServiceAccount is the Subject of RoleBindings |
| 198 | + for _, roleBinding := range result[serviceAccount].RoleBindings { |
| 199 | + require.Len(t, roleBinding.Subjects, 1) |
| 200 | + require.Equal(t, serviceAccount, roleBinding.Subjects[0].Name) |
| 201 | + |
| 202 | + // Check that RoleBindings are created with unique names |
| 203 | + _, rbWithNameExists := roleBindingNames[roleBinding.Name] |
| 204 | + require.False(t, rbWithNameExists, "RoleBinding with the same name already generated") |
| 205 | + roleBindingNames[roleBinding.Name] = true |
| 206 | + } |
| 207 | + |
| 208 | + // Check that testing ServiceAccount is the Subject of ClusterrRoleBindings |
| 209 | + for _, clusterRoleBinding := range result[serviceAccount].ClusterRoleBindings { |
| 210 | + require.Len(t, clusterRoleBinding.Subjects, 1) |
| 211 | + require.Equal(t, serviceAccount, clusterRoleBinding.Subjects[0].Name) |
| 212 | + |
| 213 | + // Check that ClusterRoleBindings are created with unique names |
| 214 | + _, crbWithNameExists := clusterRoleBindingNames[clusterRoleBinding.Name] |
| 215 | + require.False(t, crbWithNameExists, "ClusterRoleBinding with the same name already generated") |
| 216 | + clusterRoleBindingNames[clusterRoleBinding.Name] = true |
| 217 | + } |
| 218 | + |
| 219 | + // Check that Roles are created with unique names |
| 220 | + for _, role := range result[serviceAccount].Roles { |
| 221 | + _, roleWithNameExists := rolesNames[role.Name] |
| 222 | + require.False(t, roleWithNameExists, "Role with the same name already generated") |
| 223 | + rolesNames[role.Name] = true |
| 224 | + } |
| 225 | + |
| 226 | + // Check that ClusterRoles are created with unique names |
| 227 | + for _, clusterRole := range result[serviceAccount].ClusterRoles { |
| 228 | + _, crWithNameExists := clusterRolesNames[clusterRole.Name] |
| 229 | + require.False(t, crWithNameExists, "ClusterRole with the same name already generated") |
| 230 | + clusterRolesNames[clusterRole.Name] = true |
| 231 | + } |
| 232 | + } |
| 233 | + }) |
| 234 | + } |
| 235 | +} |
0 commit comments