diff --git a/pkg/cluster/cluster.go b/pkg/cluster/cluster.go index 435d512c8..16f3c18ec 100644 --- a/pkg/cluster/cluster.go +++ b/pkg/cluster/cluster.go @@ -620,7 +620,7 @@ func (c *Cluster) initRobotUsers() error { return fmt.Errorf("invalid username: %q", username) } - if c.avoidProtectedOrSystemRole(username, "manifest robot role") { + if c.shouldAvoidProtectedOrSystemRole(username, "manifest robot role") { continue } flags, err := normalizeUserFlags(userFlags) @@ -656,7 +656,7 @@ func (c *Cluster) initHumanUsers() error { flags := []string{constants.RoleFlagLogin} memberOf := []string{c.OpConfig.PamRoleName} - if c.avoidProtectedOrSystemRole(username, "API role") { + if c.shouldAvoidProtectedOrSystemRole(username, "API role") { continue } if c.OpConfig.EnableTeamSuperuser { @@ -688,7 +688,7 @@ func (c *Cluster) initInfrastructureRoles() error { if !isValidUsername(username) { return fmt.Errorf("invalid username: '%v'", username) } - if c.avoidProtectedOrSystemRole(username, "infrastructure role") { + if c.shouldAvoidProtectedOrSystemRole(username, "infrastructure role") { continue } flags, err := normalizeUserFlags(data.Flags) @@ -701,7 +701,7 @@ func (c *Cluster) initInfrastructureRoles() error { return nil } -func (c *Cluster) avoidProtectedOrSystemRole(username, purpose string) bool { +func (c *Cluster) shouldAvoidProtectedOrSystemRole(username, purpose string) bool { if c.isProtectedUsername(username) { c.logger.Warnf("cannot initialize a new %s with the name of the protected user %q", purpose, username) return true diff --git a/pkg/cluster/cluster_test.go b/pkg/cluster/cluster_test.go index 71a45a582..6628cd4db 100644 --- a/pkg/cluster/cluster_test.go +++ b/pkg/cluster/cluster_test.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/Sirupsen/logrus" "github.com/zalando-incubator/postgres-operator/pkg/spec" + "github.com/zalando-incubator/postgres-operator/pkg/util/config" "github.com/zalando-incubator/postgres-operator/pkg/util/k8sutil" "github.com/zalando-incubator/postgres-operator/pkg/util/teams" "reflect" @@ -11,7 +12,10 @@ import ( ) var logger = logrus.New().WithField("test", "cluster") -var cl = New(Config{}, k8sutil.KubernetesClient{}, spec.Postgresql{}, logger) +var cl = New(Config{OpConfig: config.Config{ProtectedRoles: []string{"admin"}, + Auth: config.Auth{SuperUsername: "postgres", + ReplicationUsername: "standby"}}}, + k8sutil.KubernetesClient{}, spec.Postgresql{}, logger) func TestInitRobotUsers(t *testing.T) { testName := "TestInitRobotUsers" @@ -47,6 +51,12 @@ func TestInitRobotUsers(t *testing.T) { err: fmt.Errorf(`invalid flags for user "foobar": ` + `conflicting user flags: "NOINHERIT" and "INHERIT"`), }, + { + manifestUsers: map[string]spec.UserFlags{"admin": {"superuser"}, "postgres": {"createdb"}}, + infraRoles: map[string]spec.PgUser{}, + result: map[string]spec.PgUser{}, + err: nil, + }, } for _, tt := range tests { cl.Spec.Users = tt.manifestUsers @@ -109,6 +119,11 @@ func TestInitHumanUsers(t *testing.T) { result: map[string]spec.PgUser{"foo": {Name: "foo", MemberOf: []string{cl.OpConfig.PamRoleName}, Flags: []string{"LOGIN", "SUPERUSER"}}, "bar": {Name: "bar", Flags: []string{"NOLOGIN"}}}, }, + { + existingRoles: map[string]spec.PgUser{}, + teamRoles: []string{"admin", "standby"}, + result: map[string]spec.PgUser{}, + }, } for _, tt := range tests {