Skip to content

Commit 2a4be1c

Browse files
authoredFeb 14, 2025··
fix creating secrets for rotation users (zalando#2863)
* fix creating secrets for rotation users * rework annotation comparison on update to decide on when to call syncSecrets
1 parent c8063eb commit 2a4be1c

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed
 

‎e2e/tests/test_e2e.py

+23-2
Original file line numberDiff line numberDiff line change
@@ -1752,9 +1752,13 @@ def test_password_rotation(self):
17521752
Test password rotation and removal of users due to retention policy
17531753
'''
17541754
k8s = self.k8s
1755+
cluster_label = 'application=spilo,cluster-name=acid-minimal-cluster'
17551756
leader = k8s.get_cluster_leader_pod()
17561757
today = date.today()
17571758

1759+
# remember number of secrets to make sure it stays the same
1760+
secret_count = k8s.count_secrets_with_label(cluster_label)
1761+
17581762
# enable password rotation for owner of foo database
17591763
pg_patch_rotation_single_users = {
17601764
"spec": {
@@ -1810,6 +1814,7 @@ def test_password_rotation(self):
18101814
enable_password_rotation = {
18111815
"data": {
18121816
"enable_password_rotation": "true",
1817+
"inherited_annotations": "environment",
18131818
"password_rotation_interval": "30",
18141819
"password_rotation_user_retention": "30", # should be set to 60
18151820
},
@@ -1856,13 +1861,29 @@ def test_password_rotation(self):
18561861
self.eventuallyEqual(lambda: len(self.query_database_with_user(leader.metadata.name, "postgres", "SELECT 1", "foo_user")), 1,
18571862
"Could not connect to the database with rotation user {}".format(rotation_user), 10, 5)
18581863

1864+
# add annotation which triggers syncSecrets call
1865+
pg_annotation_patch = {
1866+
"metadata": {
1867+
"annotations": {
1868+
"environment": "test",
1869+
}
1870+
}
1871+
}
1872+
k8s.api.custom_objects_api.patch_namespaced_custom_object(
1873+
"acid.zalan.do", "v1", "default", "postgresqls", "acid-minimal-cluster", pg_annotation_patch)
1874+
self.eventuallyEqual(lambda: k8s.get_operator_state(), {"0": "idle"}, "Operator does not get in sync")
1875+
time.sleep(10)
1876+
self.eventuallyEqual(lambda: k8s.count_secrets_with_label(cluster_label), secret_count, "Unexpected number of secrets")
1877+
18591878
# check if rotation has been ignored for user from test_cross_namespace_secrets test
18601879
db_user_secret = k8s.get_secret(username="test.db_user", namespace="test")
18611880
secret_username = str(base64.b64decode(db_user_secret.data["username"]), 'utf-8')
1862-
18631881
self.assertEqual("test.db_user", secret_username,
18641882
"Unexpected username in secret of test.db_user: expected {}, got {}".format("test.db_user", secret_username))
18651883

1884+
# check if annotation for secret has been updated
1885+
self.assertTrue("environment" in db_user_secret.metadata.annotations, "Added annotation was not propagated to secret")
1886+
18661887
# disable password rotation for all other users (foo_user)
18671888
# and pick smaller intervals to see if the third fake rotation user is dropped
18681889
enable_password_rotation = {
@@ -2100,7 +2121,7 @@ def test_statefulset_annotation_propagation(self):
21002121
patch_sset_propagate_annotations = {
21012122
"data": {
21022123
"downscaler_annotations": "deployment-time,downscaler/*",
2103-
"inherited_annotations": "owned-by",
2124+
"inherited_annotations": "environment,owned-by",
21042125
}
21052126
}
21062127
k8s.update_config(patch_sset_propagate_annotations)

‎pkg/cluster/cluster.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -1034,19 +1034,26 @@ func (c *Cluster) Update(oldSpec, newSpec *acidv1.Postgresql) error {
10341034
// only when streams were not specified in oldSpec but in newSpec
10351035
needStreamUser := len(oldSpec.Spec.Streams) == 0 && len(newSpec.Spec.Streams) > 0
10361036

1037-
annotationsChanged, _ := c.compareAnnotations(oldSpec.Annotations, newSpec.Annotations, nil)
1038-
10391037
initUsers := !sameUsers || !sameRotatedUsers || needPoolerUser || needStreamUser
1040-
if initUsers {
1038+
1039+
// if inherited annotations differ secrets have to be synced on update
1040+
newAnnotations := c.annotationsSet(nil)
1041+
oldAnnotations := make(map[string]string)
1042+
for _, secret := range c.Secrets {
1043+
oldAnnotations = secret.ObjectMeta.Annotations
1044+
break
1045+
}
1046+
annotationsChanged, _ := c.compareAnnotations(oldAnnotations, newAnnotations, nil)
1047+
1048+
if initUsers || annotationsChanged {
10411049
c.logger.Debug("initialize users")
10421050
if err := c.initUsers(); err != nil {
10431051
c.logger.Errorf("could not init users - skipping sync of secrets and databases: %v", err)
10441052
userInitFailed = true
10451053
updateFailed = true
10461054
return
10471055
}
1048-
}
1049-
if initUsers || annotationsChanged {
1056+
10501057
c.logger.Debug("syncing secrets")
10511058
//TODO: mind the secrets of the deleted/new users
10521059
if err := c.syncSecrets(); err != nil {

0 commit comments

Comments
 (0)
Please sign in to comment.