Skip to content

Commit 064864b

Browse files
committed
fix(rbac): reconcile Role when ObjectStore spec changes
When an ObjectStore's credentials change (e.g., secret rename), the RBAC Role granting the Cluster's ServiceAccount access to those secrets was not updated because nothing triggered a Cluster reconciliation. Implement the ObjectStore controller's Reconcile to detect referencing Clusters and update their Roles directly. Extract ensureRole into a shared rbac.EnsureRole function used by both the Pre hook and the ObjectStore controller. The shared EnsureRole accepts optional beforeCreate callbacks so the Pre hook can set owner references on Role creation, while the ObjectStore controller path skips ownership (it only updates existing Roles). Handle deleted ObjectStores gracefully by skipping NotFound errors during Role reconciliation, and filter status-only changes with GenerationChangedPredicate. Add RBAC permission for the plugin to list/watch Clusters so the ObjectStore controller can find affected Clusters. Regenerate config/rbac/role.yaml. Signed-off-by: Armando Ruocco <armando.ruocco@enterprisedb.com>
1 parent 376e178 commit 064864b

7 files changed

Lines changed: 694 additions & 109 deletions

File tree

config/rbac/role.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ rules:
4444
- postgresql.cnpg.io
4545
resources:
4646
- backups
47+
- clusters
4748
verbs:
4849
- get
4950
- list
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
Copyright © contributors to CloudNativePG, established as
3+
CloudNativePG a Series of LF Projects, LLC.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
SPDX-License-Identifier: Apache-2.0
18+
*/
19+
20+
// Package rbac contains utilities to reconcile RBAC resources
21+
// for the barman-cloud plugin.
22+
package rbac
23+
24+
import (
25+
"context"
26+
27+
cnpgv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
28+
"github.com/cloudnative-pg/machinery/pkg/log"
29+
rbacv1 "k8s.io/api/rbac/v1"
30+
"k8s.io/apimachinery/pkg/api/equality"
31+
apierrs "k8s.io/apimachinery/pkg/api/errors"
32+
"sigs.k8s.io/controller-runtime/pkg/client"
33+
34+
barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1"
35+
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/operator/specs"
36+
)
37+
38+
// EnsureRole ensures the RBAC Role for the given Cluster matches
39+
// the desired state derived from the given ObjectStores.
40+
// Optional beforeCreate callbacks are applied to the Role before creation
41+
// (e.g. to set owner references). They are not called on updates.
42+
func EnsureRole(
43+
ctx context.Context,
44+
c client.Client,
45+
cluster *cnpgv1.Cluster,
46+
barmanObjects []barmancloudv1.ObjectStore,
47+
beforeCreate ...func(*rbacv1.Role) error,
48+
) error {
49+
contextLogger := log.FromContext(ctx)
50+
newRole := specs.BuildRole(cluster, barmanObjects)
51+
52+
var role rbacv1.Role
53+
if err := c.Get(ctx, client.ObjectKey{
54+
Namespace: newRole.Namespace,
55+
Name: newRole.Name,
56+
}, &role); err != nil {
57+
if !apierrs.IsNotFound(err) {
58+
return err
59+
}
60+
61+
contextLogger.Info(
62+
"Creating role",
63+
"name", newRole.Name,
64+
"namespace", newRole.Namespace,
65+
)
66+
67+
for _, fn := range beforeCreate {
68+
if err := fn(newRole); err != nil {
69+
return err
70+
}
71+
}
72+
73+
return c.Create(ctx, newRole)
74+
}
75+
76+
if equality.Semantic.DeepEqual(newRole.Rules, role.Rules) {
77+
return nil
78+
}
79+
80+
contextLogger.Info(
81+
"Patching role",
82+
"name", newRole.Name,
83+
"namespace", newRole.Namespace,
84+
"rules", newRole.Rules,
85+
)
86+
87+
oldRole := role.DeepCopy()
88+
role.Rules = newRole.Rules
89+
90+
return c.Patch(ctx, &role, client.MergeFrom(oldRole))
91+
}
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
/*
2+
Copyright © contributors to CloudNativePG, established as
3+
CloudNativePG a Series of LF Projects, LLC.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
SPDX-License-Identifier: Apache-2.0
18+
*/
19+
20+
package rbac_test
21+
22+
import (
23+
"context"
24+
"fmt"
25+
26+
cnpgv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
27+
. "github.com/onsi/ginkgo/v2"
28+
. "github.com/onsi/gomega"
29+
barmanapi "github.com/cloudnative-pg/barman-cloud/pkg/api"
30+
machineryapi "github.com/cloudnative-pg/machinery/pkg/api"
31+
rbacv1 "k8s.io/api/rbac/v1"
32+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33+
"k8s.io/apimachinery/pkg/runtime"
34+
"sigs.k8s.io/controller-runtime/pkg/client"
35+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
36+
37+
barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1"
38+
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/operator/rbac"
39+
)
40+
41+
func newScheme() *runtime.Scheme {
42+
s := runtime.NewScheme()
43+
_ = rbacv1.AddToScheme(s)
44+
_ = cnpgv1.AddToScheme(s)
45+
_ = barmancloudv1.AddToScheme(s)
46+
return s
47+
}
48+
49+
func newCluster(name, namespace string) *cnpgv1.Cluster {
50+
return &cnpgv1.Cluster{
51+
ObjectMeta: metav1.ObjectMeta{
52+
Name: name,
53+
Namespace: namespace,
54+
},
55+
}
56+
}
57+
58+
func newObjectStore(name, namespace, secretName string) barmancloudv1.ObjectStore {
59+
return barmancloudv1.ObjectStore{
60+
ObjectMeta: metav1.ObjectMeta{
61+
Name: name,
62+
Namespace: namespace,
63+
},
64+
Spec: barmancloudv1.ObjectStoreSpec{
65+
Configuration: barmanapi.BarmanObjectStoreConfiguration{
66+
DestinationPath: "s3://bucket/path",
67+
BarmanCredentials: barmanapi.BarmanCredentials{
68+
AWS: &barmanapi.S3Credentials{
69+
AccessKeyIDReference: &machineryapi.SecretKeySelector{
70+
LocalObjectReference: machineryapi.LocalObjectReference{
71+
Name: secretName,
72+
},
73+
Key: "ACCESS_KEY_ID",
74+
},
75+
},
76+
},
77+
},
78+
},
79+
}
80+
}
81+
82+
var _ = Describe("EnsureRole", func() {
83+
var (
84+
ctx context.Context
85+
cluster *cnpgv1.Cluster
86+
objects []barmancloudv1.ObjectStore
87+
fakeClient client.Client
88+
)
89+
90+
BeforeEach(func() {
91+
ctx = context.Background()
92+
cluster = newCluster("test-cluster", "default")
93+
objects = []barmancloudv1.ObjectStore{
94+
newObjectStore("my-store", "default", "aws-creds"),
95+
}
96+
})
97+
98+
Context("when the Role does not exist", func() {
99+
BeforeEach(func() {
100+
fakeClient = fake.NewClientBuilder().WithScheme(newScheme()).Build()
101+
})
102+
103+
It("should create the Role", func() {
104+
err := rbac.EnsureRole(ctx, fakeClient, cluster, objects)
105+
Expect(err).NotTo(HaveOccurred())
106+
107+
var role rbacv1.Role
108+
err = fakeClient.Get(ctx, client.ObjectKey{
109+
Namespace: "default",
110+
Name: "test-cluster-barman-cloud",
111+
}, &role)
112+
Expect(err).NotTo(HaveOccurred())
113+
Expect(role.Rules).To(HaveLen(3))
114+
})
115+
116+
It("should apply beforeCreate callbacks", func() {
117+
called := false
118+
err := rbac.EnsureRole(ctx, fakeClient, cluster, objects, func(role *rbacv1.Role) error {
119+
called = true
120+
role.Labels = map[string]string{"owner": "test-cluster"}
121+
return nil
122+
})
123+
Expect(err).NotTo(HaveOccurred())
124+
Expect(called).To(BeTrue())
125+
126+
var role rbacv1.Role
127+
err = fakeClient.Get(ctx, client.ObjectKey{
128+
Namespace: "default",
129+
Name: "test-cluster-barman-cloud",
130+
}, &role)
131+
Expect(err).NotTo(HaveOccurred())
132+
Expect(role.Labels).To(HaveKeyWithValue("owner", "test-cluster"))
133+
})
134+
135+
It("should return error if beforeCreate callback fails", func() {
136+
err := rbac.EnsureRole(ctx, fakeClient, cluster, objects, func(_ *rbacv1.Role) error {
137+
return fmt.Errorf("callback error")
138+
})
139+
Expect(err).To(MatchError("callback error"))
140+
})
141+
})
142+
143+
Context("when the Role exists with matching rules", func() {
144+
BeforeEach(func() {
145+
fakeClient = fake.NewClientBuilder().WithScheme(newScheme()).Build()
146+
Expect(rbac.EnsureRole(ctx, fakeClient, cluster, objects)).To(Succeed())
147+
})
148+
149+
It("should not patch the Role", func() {
150+
var before rbacv1.Role
151+
Expect(fakeClient.Get(ctx, client.ObjectKey{
152+
Namespace: "default",
153+
Name: "test-cluster-barman-cloud",
154+
}, &before)).To(Succeed())
155+
156+
err := rbac.EnsureRole(ctx, fakeClient, cluster, objects)
157+
Expect(err).NotTo(HaveOccurred())
158+
159+
var after rbacv1.Role
160+
Expect(fakeClient.Get(ctx, client.ObjectKey{
161+
Namespace: "default",
162+
Name: "test-cluster-barman-cloud",
163+
}, &after)).To(Succeed())
164+
165+
Expect(after.ResourceVersion).To(Equal(before.ResourceVersion))
166+
})
167+
})
168+
169+
Context("when the Role exists with different rules", func() {
170+
BeforeEach(func() {
171+
fakeClient = fake.NewClientBuilder().WithScheme(newScheme()).Build()
172+
// Create with old secret
173+
oldObjects := []barmancloudv1.ObjectStore{
174+
newObjectStore("my-store", "default", "old-secret"),
175+
}
176+
Expect(rbac.EnsureRole(ctx, fakeClient, cluster, oldObjects)).To(Succeed())
177+
})
178+
179+
It("should patch the Role with new rules", func() {
180+
// Now call with new secret name
181+
err := rbac.EnsureRole(ctx, fakeClient, cluster, objects)
182+
Expect(err).NotTo(HaveOccurred())
183+
184+
var role rbacv1.Role
185+
Expect(fakeClient.Get(ctx, client.ObjectKey{
186+
Namespace: "default",
187+
Name: "test-cluster-barman-cloud",
188+
}, &role)).To(Succeed())
189+
190+
// The secrets rule should reference the new secret
191+
secretsRule := role.Rules[2]
192+
Expect(secretsRule.ResourceNames).To(ContainElement("aws-creds"))
193+
Expect(secretsRule.ResourceNames).NotTo(ContainElement("old-secret"))
194+
})
195+
196+
It("should not call beforeCreate callbacks on update", func() {
197+
called := false
198+
err := rbac.EnsureRole(ctx, fakeClient, cluster, objects, func(_ *rbacv1.Role) error {
199+
called = true
200+
return nil
201+
})
202+
Expect(err).NotTo(HaveOccurred())
203+
Expect(called).To(BeFalse())
204+
})
205+
})
206+
})
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright © contributors to CloudNativePG, established as
3+
CloudNativePG a Series of LF Projects, LLC.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
SPDX-License-Identifier: Apache-2.0
18+
*/
19+
20+
package rbac_test
21+
22+
import (
23+
"testing"
24+
25+
. "github.com/onsi/ginkgo/v2"
26+
. "github.com/onsi/gomega"
27+
)
28+
29+
func TestRBAC(t *testing.T) {
30+
RegisterFailHandler(Fail)
31+
RunSpecs(t, "RBAC Suite")
32+
}

0 commit comments

Comments
 (0)