@@ -949,6 +949,8 @@ func (c *Cluster) rotatePasswordInSecret(
949949 err error
950950 nextRotationDate time.Time
951951 nextRotationDateStr string
952+ expectedUsername string
953+ rotationModeChanged bool
952954 updateSecretMsg string
953955 )
954956
@@ -969,17 +971,32 @@ func (c *Cluster) rotatePasswordInSecret(
969971 nextRotationDate = currentRotationDate
970972 }
971973
974+ // set username and check if it differs from current value in secret
975+ currentUsername := string (secret .Data ["username" ])
976+ if ! slices .Contains (c .Spec .UsersWithInPlaceSecretRotation , secretUsername ) {
977+ expectedUsername = fmt .Sprintf ("%s%s" , secretUsername , currentTime .Format (constants .RotationUserDateFormat ))
978+ } else {
979+ expectedUsername = secretUsername
980+ }
981+
982+ // when changing to in-place rotation update secret immediatly
983+ // if currentUsername is longer we know it has a date suffix
984+ // the other way around we can wait until the next rotation date
985+ if len (currentUsername ) > len (expectedUsername ) {
986+ rotationModeChanged = true
987+ c .logger .Infof ("updating secret %s after switching to in-place rotation mode for username: %s" , secretName , string (secret .Data ["username" ]))
988+ }
989+
972990 // update password and next rotation date if configured interval has passed
973- if currentTime .After (nextRotationDate ) {
991+ if currentTime .After (nextRotationDate ) || rotationModeChanged {
974992 // create rotation user if role is not listed for in-place password update
975993 if ! slices .Contains (c .Spec .UsersWithInPlaceSecretRotation , secretUsername ) {
976- rotationUsername := fmt .Sprintf ("%s%s" , secretUsername , currentTime .Format (constants .RotationUserDateFormat ))
977- secret .Data ["username" ] = []byte (rotationUsername )
978- c .logger .Infof ("updating username in secret %s and creating rotation user %s in the database" , secretName , rotationUsername )
994+ secret .Data ["username" ] = []byte (expectedUsername )
995+ c .logger .Infof ("updating username in secret %s and creating rotation user %s in the database" , secretName , expectedUsername )
979996 // whenever there is a rotation, check if old rotation users can be deleted
980997 * retentionUsers = append (* retentionUsers , secretUsername )
981998 } else {
982- // when passwords of system users are rotated in place, pods have to be replaced
999+ // when passwords of system users are rotated in- place, pods have to be replaced
9831000 if roleOrigin == spec .RoleOriginSystem {
9841001 pods , err := c .listPods ()
9851002 if err != nil {
@@ -993,7 +1010,7 @@ func (c *Cluster) rotatePasswordInSecret(
9931010 }
9941011 }
9951012
996- // when password of connection pooler is rotated in place, pooler pods have to be replaced
1013+ // when password of connection pooler is rotated in- place, pooler pods have to be replaced
9971014 if roleOrigin == spec .RoleOriginConnectionPooler {
9981015 listOptions := metav1.ListOptions {
9991016 LabelSelector : c .poolerLabelsSet (true ).String (),
@@ -1010,10 +1027,12 @@ func (c *Cluster) rotatePasswordInSecret(
10101027 }
10111028 }
10121029
1013- // when password of stream user is rotated in place, it should trigger rolling update in FES deployment
1030+ // when password of stream user is rotated in- place, it should trigger rolling update in FES deployment
10141031 if roleOrigin == spec .RoleOriginStream {
10151032 c .logger .Warnf ("password in secret of stream user %s changed" , constants .EventStreamSourceSlotPrefix + constants .UserRoleNameSuffix )
10161033 }
1034+
1035+ secret .Data ["username" ] = []byte (secretUsername )
10171036 }
10181037 secret .Data ["password" ] = []byte (util .RandomPassword (constants .PasswordLength ))
10191038 secret .Data ["nextRotation" ] = []byte (nextRotationDateStr )
0 commit comments