Skip to content

Commit feb756f

Browse files
matskivopenshift-cherrypick-robot
authored andcommitted
Bug 1855088: generate unique (Cluster)RoleBinding names
When same permission rules are defined for different ServiceAccounts, OLM would generate same name for (Cluster)RoleBinding related to this rules. With this change, ServiceAccount name will be also passed to generateName(), resulting in a unqiue binding name for each SA.
1 parent 922f6ad commit feb756f

File tree

2 files changed

+141
-3
lines changed

2 files changed

+141
-3
lines changed

pkg/controller/registry/resolver/rbac.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func RBACForClusterServiceVersion(csv *v1alpha1.ClusterServiceVersion) (map[stri
105105
// Create RoleBinding
106106
roleBinding := &rbacv1.RoleBinding{
107107
ObjectMeta: metav1.ObjectMeta{
108-
Name: generateName(role.GetName(), role),
108+
Name: generateName(role.GetName(), permission),
109109
Namespace: csv.GetNamespace(),
110110
OwnerReferences: []metav1.OwnerReference{ownerutil.NonBlockingOwner(csv)},
111111
Labels: ownerutil.OwnerLabel(csv, v1alpha1.ClusterServiceVersionKind),
@@ -147,7 +147,7 @@ func RBACForClusterServiceVersion(csv *v1alpha1.ClusterServiceVersion) (map[stri
147147
// Create ClusterRoleBinding
148148
roleBinding := &rbacv1.ClusterRoleBinding{
149149
ObjectMeta: metav1.ObjectMeta{
150-
Name: generateName(role.GetName(), role),
150+
Name: generateName(role.GetName(), permission),
151151
Namespace: csv.GetNamespace(),
152152
Labels: ownerutil.OwnerLabel(csv, v1alpha1.ClusterServiceVersionKind),
153153
},

pkg/controller/registry/resolver/rbac_test.go

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package resolver
22

33
import (
4-
"github.com/stretchr/testify/require"
54
"math/rand"
65
"reflect"
76
"strings"
87
"testing"
98
"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"
1016
)
1117

1218
func TestGenerateName(t *testing.T) {
@@ -63,3 +69,135 @@ func TestGeneratesWithinRange(t *testing.T) {
6369
}
6470
require.NoError(t, quick.Check(f, nil))
6571
}
72+
73+
func TestRBACBindings(t *testing.T) {
74+
serviceAccount1 := "test-service-account"
75+
serviceAccount2 := "second-account"
76+
77+
rules := []rbacv1.PolicyRule{
78+
{
79+
Verbs: []string{"get"},
80+
APIGroups: []string{""},
81+
Resources: []string{"pods"},
82+
},
83+
}
84+
85+
tests := []struct {
86+
name string
87+
csv v1alpha1.ClusterServiceVersion
88+
want map[string]*OperatorPermissions
89+
}{
90+
{
91+
name: "RoleBinding",
92+
csv: v1alpha1.ClusterServiceVersion{
93+
ObjectMeta: metav1.ObjectMeta{
94+
Name: "test-csv-1.1.0",
95+
},
96+
Spec: v1alpha1.ClusterServiceVersionSpec{
97+
InstallStrategy: v1alpha1.NamedInstallStrategy{
98+
StrategyName: v1alpha1.InstallStrategyNameDeployment,
99+
StrategySpec: v1alpha1.StrategyDetailsDeployment{
100+
Permissions: []v1alpha1.StrategyDeploymentPermissions{
101+
{
102+
ServiceAccountName: serviceAccount1,
103+
Rules: rules,
104+
},
105+
{
106+
ServiceAccountName: serviceAccount1,
107+
Rules: []rbacv1.PolicyRule{
108+
{
109+
Verbs: []string{"get"},
110+
APIGroups: []string{""},
111+
Resources: []string{"deployments"},
112+
},
113+
},
114+
},
115+
{
116+
ServiceAccountName: serviceAccount2,
117+
Rules: rules,
118+
},
119+
},
120+
},
121+
},
122+
},
123+
},
124+
want: map[string]*OperatorPermissions{
125+
serviceAccount1: {
126+
RoleBindings: []*rbacv1.RoleBinding{{}, {}},
127+
},
128+
serviceAccount2: {
129+
RoleBindings: []*rbacv1.RoleBinding{{}},
130+
},
131+
},
132+
},
133+
{
134+
name: "ClusterRoleBinding",
135+
csv: v1alpha1.ClusterServiceVersion{
136+
ObjectMeta: metav1.ObjectMeta{
137+
Name: "second-csv-1.1.0",
138+
},
139+
Spec: v1alpha1.ClusterServiceVersionSpec{
140+
InstallStrategy: v1alpha1.NamedInstallStrategy{
141+
StrategyName: v1alpha1.InstallStrategyNameDeployment,
142+
StrategySpec: v1alpha1.StrategyDetailsDeployment{
143+
ClusterPermissions: []v1alpha1.StrategyDeploymentPermissions{
144+
{
145+
ServiceAccountName: serviceAccount1,
146+
Rules: rules,
147+
},
148+
{
149+
ServiceAccountName: serviceAccount2,
150+
Rules: rules,
151+
},
152+
},
153+
},
154+
},
155+
},
156+
},
157+
want: map[string]*OperatorPermissions{
158+
serviceAccount1: {
159+
ClusterRoleBindings: []*rbacv1.ClusterRoleBinding{{}},
160+
},
161+
serviceAccount2: {
162+
ClusterRoleBindings: []*rbacv1.ClusterRoleBinding{{}},
163+
},
164+
},
165+
},
166+
}
167+
for _, tt := range tests {
168+
t.Run(tt.name, func(t *testing.T) {
169+
result, err := RBACForClusterServiceVersion(&tt.csv)
170+
require.NoError(t, err)
171+
172+
roleBindingNames := map[string]bool{}
173+
clusterRoleBindingNames := map[string]bool{}
174+
for serviceAccount, permissions := range tt.want {
175+
// Check that correct number of bindings is created
176+
require.Equal(t, len(permissions.RoleBindings), len(result[serviceAccount].RoleBindings))
177+
require.Equal(t, len(permissions.ClusterRoleBindings), len(result[serviceAccount].ClusterRoleBindings))
178+
179+
// Check that testing ServiceAccount is the Subject of RoleBindings
180+
for _, roleBinding := range result[serviceAccount].RoleBindings {
181+
require.Len(t, roleBinding.Subjects, 1)
182+
require.Equal(t, serviceAccount, roleBinding.Subjects[0].Name)
183+
184+
// Check that RoleBindings are created with unique names
185+
_, rbWithNameExists := roleBindingNames[roleBinding.Name]
186+
require.False(t, rbWithNameExists, "RoleBinding with the same name already generated")
187+
roleBindingNames[roleBinding.Name] = true
188+
}
189+
190+
// Check that testing ServiceAccount is the Subject of ClusterrRoleBindings
191+
for _, clusterRoleBinding := range result[serviceAccount].ClusterRoleBindings {
192+
require.Len(t, clusterRoleBinding.Subjects, 1)
193+
require.Equal(t, serviceAccount, clusterRoleBinding.Subjects[0].Name)
194+
195+
// Check that ClusterRoleBindings are created with unique names
196+
_, crbWithNameExists := clusterRoleBindingNames[clusterRoleBinding.Name]
197+
require.False(t, crbWithNameExists, "ClusterRoleBinding with the same name already generated")
198+
clusterRoleBindingNames[clusterRoleBinding.Name] = true
199+
}
200+
}
201+
})
202+
}
203+
}

0 commit comments

Comments
 (0)