Skip to content

Commit ef32449

Browse files
authored
fetch pooler and fes_user system user only when corresponding features are used (zalando#2009)
* fetch pooler and fes_user system user only when corresponding features are used * cover error case in unit test * use string formatting instead of +
1 parent e11edcd commit ef32449

File tree

7 files changed

+59
-26
lines changed

7 files changed

+59
-26
lines changed

pkg/cluster/cluster.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,7 @@ func (c *Cluster) initSystemUsers() {
11271127
// replication users for event streams are another exception
11281128
// the operator will create one replication user for all streams
11291129
if len(c.Spec.Streams) > 0 {
1130-
username := constants.EventStreamSourceSlotPrefix + constants.UserRoleNameSuffix
1130+
username := fmt.Sprintf("%s%s", constants.EventStreamSourceSlotPrefix, constants.UserRoleNameSuffix)
11311131
streamUser := spec.PgUser{
11321132
Origin: spec.RoleOriginStream,
11331133
Name: username,
@@ -1136,8 +1136,8 @@ func (c *Cluster) initSystemUsers() {
11361136
Password: util.RandomPassword(constants.PasswordLength),
11371137
}
11381138

1139-
if _, exists := c.systemUsers[username]; !exists {
1140-
c.systemUsers[username] = streamUser
1139+
if _, exists := c.systemUsers[constants.EventStreamUserKeyName]; !exists {
1140+
c.systemUsers[constants.EventStreamUserKeyName] = streamUser
11411141
}
11421142
}
11431143
}
@@ -1155,9 +1155,9 @@ func (c *Cluster) initPreparedDatabaseRoles() error {
11551155
constants.WriterRoleNameSuffix: constants.ReaderRoleNameSuffix,
11561156
}
11571157
defaultUsers := map[string]string{
1158-
constants.OwnerRoleNameSuffix + constants.UserRoleNameSuffix: constants.OwnerRoleNameSuffix,
1159-
constants.ReaderRoleNameSuffix + constants.UserRoleNameSuffix: constants.ReaderRoleNameSuffix,
1160-
constants.WriterRoleNameSuffix + constants.UserRoleNameSuffix: constants.WriterRoleNameSuffix,
1158+
fmt.Sprintf("%s%s", constants.OwnerRoleNameSuffix, constants.UserRoleNameSuffix): constants.OwnerRoleNameSuffix,
1159+
fmt.Sprintf("%s%s", constants.ReaderRoleNameSuffix, constants.UserRoleNameSuffix): constants.ReaderRoleNameSuffix,
1160+
fmt.Sprintf("%s%s", constants.WriterRoleNameSuffix, constants.UserRoleNameSuffix): constants.WriterRoleNameSuffix,
11611161
}
11621162

11631163
for preparedDbName, preparedDB := range c.Spec.PreparedDatabases {
@@ -1218,7 +1218,7 @@ func (c *Cluster) initDefaultRoles(defaultRoles map[string]string, admin, prefix
12181218
c.logger.Warn("secretNamespace ignored because enable_cross_namespace_secret set to false. Creating secrets in cluster namespace.")
12191219
}
12201220
}
1221-
roleName := prefix + defaultRole
1221+
roleName := fmt.Sprintf("%s%s", prefix, defaultRole)
12221222

12231223
flags := []string{constants.RoleFlagNoLogin}
12241224
if defaultRole[len(defaultRole)-5:] == constants.UserRoleNameSuffix {
@@ -1236,7 +1236,7 @@ func (c *Cluster) initDefaultRoles(defaultRoles map[string]string, admin, prefix
12361236
adminRole = admin
12371237
isOwner = true
12381238
} else {
1239-
adminRole = prefix + constants.OwnerRoleNameSuffix
1239+
adminRole = fmt.Sprintf("%s%s", prefix, constants.OwnerRoleNameSuffix)
12401240
}
12411241

12421242
newRole := spec.PgUser{

pkg/cluster/cluster_test.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,11 +759,14 @@ func TestServiceAnnotations(t *testing.T) {
759759
func TestInitSystemUsers(t *testing.T) {
760760
testName := "Test system users initialization"
761761

762-
// default cluster without connection pooler
762+
// default cluster without connection pooler and event streams
763763
cl.initSystemUsers()
764764
if _, exist := cl.systemUsers[constants.ConnectionPoolerUserKeyName]; exist {
765765
t.Errorf("%s, connection pooler user is present", testName)
766766
}
767+
if _, exist := cl.systemUsers[constants.EventStreamUserKeyName]; exist {
768+
t.Errorf("%s, stream user is present", testName)
769+
}
767770

768771
// cluster with connection pooler
769772
cl.Spec.EnableConnectionPooler = boolToPointer(true)
@@ -805,6 +808,31 @@ func TestInitSystemUsers(t *testing.T) {
805808
if _, exist := cl.systemUsers["pooler"]; !exist {
806809
t.Errorf("%s, System users are not allowed to be a connection pool user", testName)
807810
}
811+
812+
// using stream user in manifest but no streams defined should be treated like normal robot user
813+
streamUser := fmt.Sprintf("%s%s", constants.EventStreamSourceSlotPrefix, constants.UserRoleNameSuffix)
814+
cl.Spec.Users = map[string]acidv1.UserFlags{streamUser: []string{}}
815+
cl.initSystemUsers()
816+
if _, exist := cl.systemUsers[constants.EventStreamUserKeyName]; exist {
817+
t.Errorf("%s, stream user is present", testName)
818+
}
819+
820+
// cluster with streams
821+
cl.Spec.Streams = []acidv1.Stream{
822+
{
823+
ApplicationId: "test-app",
824+
Database: "test_db",
825+
Tables: map[string]acidv1.StreamTable{
826+
"data.test_table": acidv1.StreamTable{
827+
EventType: "test_event",
828+
},
829+
},
830+
},
831+
}
832+
cl.initSystemUsers()
833+
if _, exist := cl.systemUsers[constants.EventStreamUserKeyName]; !exist {
834+
t.Errorf("%s, stream user is not present", testName)
835+
}
808836
}
809837

810838
func TestPreparedDatabases(t *testing.T) {

pkg/cluster/connection_pooler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type ConnectionPoolerObjects struct {
4646
func (c *Cluster) connectionPoolerName(role PostgresRole) string {
4747
name := c.Name + "-pooler"
4848
if role == Replica {
49-
name = name + "-repl"
49+
name = fmt.Sprintf("%s-%s", name, "repl")
5050
}
5151
return name
5252
}

pkg/cluster/k8sres.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (c *Cluster) statefulSetName() string {
8080
func (c *Cluster) endpointName(role PostgresRole) string {
8181
name := c.Name
8282
if role == Replica {
83-
name = name + "-repl"
83+
name = fmt.Sprintf("%s-%s", name, "repl")
8484
}
8585

8686
return name
@@ -89,7 +89,7 @@ func (c *Cluster) endpointName(role PostgresRole) string {
8989
func (c *Cluster) serviceName(role PostgresRole) string {
9090
name := c.Name
9191
if role == Replica {
92-
name = name + "-repl"
92+
name = fmt.Sprintf("%s-%s", name, "repl")
9393
}
9494

9595
return name
@@ -2238,7 +2238,7 @@ func (c *Cluster) generateLogicalBackupPodEnvVars() []v1.EnvVar {
22382238

22392239
// getLogicalBackupJobName returns the name; the job itself may not exists
22402240
func (c *Cluster) getLogicalBackupJobName() (jobName string) {
2241-
return trimCronjobName(c.OpConfig.LogicalBackupJobPrefix + c.clusterName().Name)
2241+
return trimCronjobName(fmt.Sprintf("%s%s", c.OpConfig.LogicalBackupJobPrefix, c.clusterName().Name))
22422242
}
22432243

22442244
// Return an array of ownerReferences to make an arbitraty object dependent on

pkg/cluster/streams_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ var (
4040
namespace string = "default"
4141
appId string = "test-app"
4242
dbName string = "foo"
43-
fesUser string = constants.EventStreamSourceSlotPrefix + constants.UserRoleNameSuffix
43+
fesUser string = fmt.Sprintf("%s%s", constants.EventStreamSourceSlotPrefix, constants.UserRoleNameSuffix)
4444
fesName string = fmt.Sprintf("%s-%s", clusterName, appId)
4545
slotName string = fmt.Sprintf("%s_%s_%s", constants.EventStreamSourceSlotPrefix, dbName, strings.Replace(appId, "-", "_", -1))
4646

@@ -55,7 +55,7 @@ var (
5555
},
5656
Spec: acidv1.PostgresSpec{
5757
Databases: map[string]string{
58-
dbName: dbName + constants.UserRoleNameSuffix,
58+
dbName: fmt.Sprintf("%s%s", dbName, constants.UserRoleNameSuffix),
5959
},
6060
Streams: []acidv1.Stream{
6161
{

pkg/cluster/sync.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -715,12 +715,16 @@ func (c *Cluster) updateSecret(
715715
} else if secretUsername == c.systemUsers[constants.ReplicationUserKeyName].Name {
716716
userKey = constants.ReplicationUserKeyName
717717
userMap = c.systemUsers
718-
} else if secretUsername == constants.ConnectionPoolerUserName {
719-
userKey = constants.ConnectionPoolerUserName
720-
userMap = c.systemUsers
721-
} else if secretUsername == constants.EventStreamSourceSlotPrefix+constants.UserRoleNameSuffix {
722-
userKey = constants.EventStreamSourceSlotPrefix + constants.UserRoleNameSuffix
723-
userMap = c.systemUsers
718+
} else if _, exists := c.systemUsers[constants.ConnectionPoolerUserKeyName]; exists {
719+
if secretUsername == c.systemUsers[constants.ConnectionPoolerUserKeyName].Name {
720+
userKey = constants.ConnectionPoolerUserName
721+
userMap = c.systemUsers
722+
}
723+
} else if _, exists := c.systemUsers[constants.EventStreamUserKeyName]; exists {
724+
if secretUsername == c.systemUsers[constants.EventStreamUserKeyName].Name {
725+
userKey = fmt.Sprintf("%s%s", constants.EventStreamSourceSlotPrefix, constants.UserRoleNameSuffix)
726+
userMap = c.systemUsers
727+
}
724728
} else {
725729
userKey = secretUsername
726730
userMap = c.pgUsers
@@ -816,7 +820,7 @@ func (c *Cluster) rotatePasswordInSecret(
816820
// create rotation user if role is not listed for in-place password update
817821
if !util.SliceContains(c.Spec.UsersWithInPlaceSecretRotation, secretUsername) {
818822
rotationUser := secretPgUser
819-
newRotationUsername := secretUsername + currentTime.Format("060102")
823+
newRotationUsername := fmt.Sprintf("%s%s", secretUsername, currentTime.Format("060102"))
820824
rotationUser.Name = newRotationUsername
821825
rotationUser.MemberOf = []string{secretUsername}
822826
(*rotationUsers)[newRotationUsername] = rotationUser
@@ -976,7 +980,7 @@ func (c *Cluster) syncDatabases() error {
976980
for preparedDatabaseName := range c.Spec.PreparedDatabases {
977981
_, exists := currentDatabases[preparedDatabaseName]
978982
if !exists {
979-
createDatabases[preparedDatabaseName] = preparedDatabaseName + constants.OwnerRoleNameSuffix
983+
createDatabases[preparedDatabaseName] = fmt.Sprintf("%s%s", preparedDatabaseName, constants.OwnerRoleNameSuffix)
980984
preparedDatabases = append(preparedDatabases, preparedDatabaseName)
981985
}
982986
}
@@ -1077,9 +1081,9 @@ func (c *Cluster) syncPreparedSchemas(databaseName string, preparedSchemas map[s
10771081
if createPreparedSchemas, equal := util.SubstractStringSlices(schemas, currentSchemas); !equal {
10781082
for _, schemaName := range createPreparedSchemas {
10791083
owner := constants.OwnerRoleNameSuffix
1080-
dbOwner := databaseName + owner
1084+
dbOwner := fmt.Sprintf("%s%s", databaseName, owner)
10811085
if preparedSchemas[schemaName].DefaultRoles == nil || *preparedSchemas[schemaName].DefaultRoles {
1082-
owner = databaseName + "_" + schemaName + owner
1086+
owner = fmt.Sprintf("%s_%s%s", databaseName, schemaName, owner)
10831087
} else {
10841088
owner = dbOwner
10851089
}

pkg/util/constants/roles.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ package constants
44
const (
55
PasswordLength = 64
66
SuperuserKeyName = "superuser"
7-
ConnectionPoolerUserKeyName = "pooler"
87
ReplicationUserKeyName = "replication"
8+
ConnectionPoolerUserKeyName = "pooler"
9+
EventStreamUserKeyName = "streamer"
910
RoleFlagSuperuser = "SUPERUSER"
1011
RoleFlagInherit = "INHERIT"
1112
RoleFlagLogin = "LOGIN"

0 commit comments

Comments
 (0)