Skip to content

Commit c0b0b9a

Browse files
authored
[WIP] Add 'admin' option to create role (zalando#425)
* Add 'admin' option to create role * Fix run_locally_script
1 parent 2667040 commit c0b0b9a

File tree

7 files changed

+22
-6
lines changed

7 files changed

+22
-6
lines changed

docs/reference/operator_parameters.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,9 @@ key.
373373
role name to grant to team members created from the Teams API. The default is
374374
`admin`, that role is created by Spilo as a `NOLOGIN` role.
375375

376+
* **enable_admin_role_for_users**
377+
if `true`, the `team_admin_role` will have the rights to grant roles coming from PG manifests. Such roles will be created as in "CREATE ROLE 'role_from_manifest' ... ADMIN 'team_admin_role'". The default is `true`.
378+
376379
* **pam_role_name**
377380
when set, the operator will add all team member roles to this group and add a
378381
`pg_hba` line to authenticate members of that role via `pam`. The default is

manifests/configmap.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ data:
1919
# postgres_superuser_teams: "postgres_superusers"
2020
# enable_team_superuser: "false"
2121
# team_admin_role: "admin"
22+
# enable_admin_role_for_users: "true"
2223
# teams_api_url: http://fake-teams-api.default.svc.cluster.local
2324
# team_api_role_configuration: "log_statement:all"
2425
# infrastructure_roles_secret_name: postgresql-infrastructure-roles

pkg/cluster/cluster.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -709,11 +709,16 @@ func (c *Cluster) initRobotUsers() error {
709709
if err != nil {
710710
return fmt.Errorf("invalid flags for user %q: %v", username, err)
711711
}
712+
adminRole := ""
713+
if c.OpConfig.EnableAdminRoleForUsers {
714+
adminRole = c.OpConfig.TeamAdminRole
715+
}
712716
newRole := spec.PgUser{
713-
Origin: spec.RoleOriginManifest,
714-
Name: username,
715-
Password: util.RandomPassword(constants.PasswordLength),
716-
Flags: flags,
717+
Origin: spec.RoleOriginManifest,
718+
Name: username,
719+
Password: util.RandomPassword(constants.PasswordLength),
720+
Flags: flags,
721+
AdminRole: adminRole,
717722
}
718723
if currentRole, present := c.pgUsers[username]; present {
719724
c.pgUsers[username] = c.resolveNameConflict(&currentRole, &newRole)

pkg/spec/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type PgUser struct {
4949
Flags []string `yaml:"user_flags"`
5050
MemberOf []string `yaml:"inrole"`
5151
Parameters map[string]string `yaml:"db_parameters"`
52+
AdminRole string `yaml:"admin_role"`
5253
}
5354

5455
// PgUserMap maps user names to the definitions.

pkg/util/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ type Config struct {
9090
EnableTeamsAPI bool `name:"enable_teams_api" default:"true"`
9191
EnableTeamSuperuser bool `name:"enable_team_superuser" default:"false"`
9292
TeamAdminRole string `name:"team_admin_role" default:"admin"`
93+
EnableAdminRoleForUsers bool `name:"enable_admin_role_for_users" default:"true"`
9394
EnableMasterLoadBalancer bool `name:"enable_master_load_balancer" default:"true"`
9495
EnableReplicaLoadBalancer bool `name:"enable_replica_load_balancer" default:"false"`
9596
// deprecated and kept for backward compatibility

pkg/util/users/users.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import (
55
"fmt"
66
"strings"
77

8+
"reflect"
9+
810
"github.com/zalando-incubator/postgres-operator/pkg/spec"
911
"github.com/zalando-incubator/postgres-operator/pkg/util"
10-
"reflect"
1112
)
1213

1314
const (
@@ -19,6 +20,7 @@ const (
1920
doBlockStmt = `SET LOCAL synchronous_commit = 'local'; DO $$ BEGIN %s; END;$$;`
2021
passwordTemplate = "ENCRYPTED PASSWORD '%s'"
2122
inRoleTemplate = `IN ROLE %s`
23+
adminTemplate = `ADMIN %s`
2224
)
2325

2426
// DefaultUserSyncStrategy implements a user sync strategy that merges already existing database users
@@ -113,6 +115,9 @@ func (strategy DefaultUserSyncStrategy) createPgUser(user spec.PgUser, db *sql.D
113115
if len(user.MemberOf) > 0 {
114116
userFlags = append(userFlags, fmt.Sprintf(inRoleTemplate, quoteMemberList(user)))
115117
}
118+
if user.AdminRole != "" {
119+
userFlags = append(userFlags, fmt.Sprintf(adminTemplate, user.AdminRole))
120+
}
116121

117122
if user.Password == "" {
118123
userPassword = "PASSWORD NULL"

run_operator_locally.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ function deploy_self_built_image() {
121121
# update the tag in the postgres operator conf
122122
# since the image with this tag already exists on the machine,
123123
# docker should not attempt to fetch it from the registry due to imagePullPolicy
124-
sed --expression "s/\(image\:.*\:\).*$/\1$TAG/" manifests/postgres-operator.yaml > "$PATH_TO_LOCAL_OPERATOR_MANIFEST"
124+
sed --expression "s/\(image\:.*\:\).*$/\1$TAG/; s/smoke-tested-//" manifests/postgres-operator.yaml > "$PATH_TO_LOCAL_OPERATOR_MANIFEST"
125125

126126
retry "kubectl create -f \"$PATH_TO_LOCAL_OPERATOR_MANIFEST\"" "attempt to create $PATH_TO_LOCAL_OPERATOR_MANIFEST resource"
127127
}

0 commit comments

Comments
 (0)