Skip to content

Commit 2719d41

Browse files
authored
grant db owners to cron_admin (#1805)
* grant db owners to cron_admin * allow specifiying more extra owner roles * add unit test for InitAdditionalOwnerRoles * add e2e test
1 parent 6ba05fe commit 2719d41

File tree

17 files changed

+192
-23
lines changed

17 files changed

+192
-23
lines changed

charts/postgres-operator/crds/operatorconfigurations.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ spec:
130130
users:
131131
type: object
132132
properties:
133+
additional_owner_roles:
134+
type: array
135+
nullable: true
136+
items:
137+
type: string
133138
enable_password_rotation:
134139
type: boolean
135140
default: false
@@ -514,6 +519,7 @@ spec:
514519
type: string
515520
default:
516521
- admin
522+
- cron_admin
517523
role_deletion_suffix:
518524
type: string
519525
default: "_deleted"

charts/postgres-operator/values.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ configGeneral:
5959

6060
# parameters describing Postgres users
6161
configUsers:
62+
# roles to be granted to database owners
63+
# additional_owner_roles:
64+
# - cron_admin
65+
66+
# enable password rotation for app users that are not database owners
67+
enable_password_rotation: false
68+
# rotation interval for updating credentials in K8s secrets of app users
69+
password_rotation_interval: 90
70+
# retention interval to keep rotation users
71+
password_rotation_user_retention: 180
6272
# postgres username used for replication between instances
6373
replication_username: standby
6474
# postgres superuser name to be created by initdb
@@ -348,6 +358,7 @@ configTeamsApi:
348358
# List of roles that cannot be overwritten by an application, team or infrastructure role
349359
protected_role_names:
350360
- admin
361+
- cron_admin
351362
# Suffix to add if members are removed from TeamsAPI or PostgresTeam CRD
352363
role_deletion_suffix: "_deleted"
353364
# role name to grant to team members created from the Teams API

docs/reference/operator_parameters.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,15 @@ under the `users` key.
177177
Postgres username used for replication between instances. The default is
178178
`standby`.
179179

180+
* **additional_owner_roles**
181+
Specifies database roles that will become members of all database owners.
182+
Then owners can use `SET ROLE` to obtain privileges of these roles to e.g.
183+
create/update functionality from extensions as part of a migration script.
184+
Note, that roles listed here should be preconfigured in the docker image
185+
and already exist in the database cluster on startup. One such role can be
186+
`cron_admin` which is provided by the Spilo docker image to set up cron
187+
jobs inside the `postgres` database. Default is `empty`.
188+
180189
* **enable_password_rotation**
181190
For all `LOGIN` roles that are not database owners the operator can rotate
182191
credentials in the corresponding K8s secrets by replacing the username and
@@ -770,7 +779,7 @@ key.
770779

771780
* **protected_role_names**
772781
List of roles that cannot be overwritten by an application, team or
773-
infrastructure role. The default is `admin`.
782+
infrastructure role. The default list is `admin` and `cron_admin`.
774783

775784
* **postgres_superuser_teams**
776785
List of teams which members need the superuser role in each PG database

e2e/tests/test_e2e.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,37 @@ def setUpClass(cls):
158158
print('Operator log: {}'.format(k8s.get_operator_log()))
159159
raise
160160

161+
@timeout_decorator.timeout(TEST_TIMEOUT_SEC)
162+
def test_additional_owner_roles(self):
163+
'''
164+
Test adding additional member roles to existing database owner roles
165+
'''
166+
k8s = self.k8s
167+
168+
# enable PostgresTeam CRD and lower resync
169+
owner_roles = {
170+
"data": {
171+
"additional_owner_roles": "cron_admin",
172+
},
173+
}
174+
k8s.update_config(owner_roles)
175+
self.eventuallyEqual(lambda: k8s.get_operator_state(), {"0": "idle"},
176+
"Operator does not get in sync")
177+
178+
leader = k8s.get_cluster_leader_pod()
179+
owner_query = """
180+
SELECT a2.rolname
181+
FROM pg_catalog.pg_authid a
182+
JOIN pg_catalog.pg_auth_members am
183+
ON a.oid = am.member
184+
AND a.rolname = 'cron_admin'
185+
JOIN pg_catalog.pg_authid a2
186+
ON a2.oid = am.roleid
187+
WHERE a2.rolname IN ('zalando', 'bar_owner', 'bar_data_owner');
188+
"""
189+
self.eventuallyEqual(lambda: len(self.query_database(leader.metadata.name, "postgres", owner_query)), 3,
190+
"Not all additional users found in database", 10, 5)
191+
161192
@timeout_decorator.timeout(TEST_TIMEOUT_SEC)
162193
def test_additional_pod_capabilities(self):
163194
'''

manifests/configmap.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ kind: ConfigMap
33
metadata:
44
name: postgres-operator
55
data:
6+
# additional_owner_roles: "cron_admin"
67
# additional_pod_capabilities: "SYS_NICE"
78
# additional_secret_mount: "some-secret-name"
89
# additional_secret_mount_path: "/some/dir"
@@ -114,7 +115,7 @@ data:
114115
# pod_service_account_role_binding_definition: ""
115116
pod_terminate_grace_period: 5m
116117
# postgres_superuser_teams: "postgres_superusers"
117-
# protected_role_names: "admin"
118+
# protected_role_names: "admin,cron_admin"
118119
ready_wait_interval: 3s
119120
ready_wait_timeout: 30s
120121
repair_period: 5m

manifests/operatorconfiguration.crd.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ spec:
128128
users:
129129
type: object
130130
properties:
131+
additional_owner_roles:
132+
type: array
133+
nullable: true
134+
items:
135+
type: string
131136
enable_password_rotation:
132137
type: boolean
133138
default: false
@@ -512,6 +517,7 @@ spec:
512517
type: string
513518
default:
514519
- admin
520+
- cron_admin
515521
role_deletion_suffix:
516522
type: string
517523
default: "_deleted"

manifests/postgresql-operator-default-configuration.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ configuration:
2626
# protocol: TCP
2727
workers: 8
2828
users:
29+
# additional_owner_roles:
30+
# - cron_admin
2931
enable_password_rotation: false
3032
password_rotation_interval: 90
3133
password_rotation_user_retention: 180
@@ -168,6 +170,7 @@ configuration:
168170
# - postgres_superusers
169171
protected_role_names:
170172
- admin
173+
- cron_admin
171174
role_deletion_suffix: "_deleted"
172175
team_admin_role: admin
173176
team_api_role_configuration:

pkg/apis/acid.zalan.do/v1/crds.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,24 @@ var OperatorConfigCRDResourceValidation = apiextv1.CustomResourceValidation{
11481148
"users": {
11491149
Type: "object",
11501150
Properties: map[string]apiextv1.JSONSchemaProps{
1151+
"additional_owner_roles": {
1152+
Type: "array",
1153+
Nullable: true,
1154+
Items: &apiextv1.JSONSchemaPropsOrArray{
1155+
Schema: &apiextv1.JSONSchemaProps{
1156+
Type: "string",
1157+
},
1158+
},
1159+
},
1160+
"enable_password_rotation": {
1161+
Type: "boolean",
1162+
},
1163+
"password_rotation_interval": {
1164+
Type: "integer",
1165+
},
1166+
"password_rotation_user_retention": {
1167+
Type: "integer",
1168+
},
11511169
"replication_username": {
11521170
Type: "string",
11531171
},

pkg/apis/acid.zalan.do/v1/operator_configuration_type.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ type OperatorConfigurationList struct {
3737

3838
// PostgresUsersConfiguration defines the system users of Postgres.
3939
type PostgresUsersConfiguration struct {
40-
SuperUsername string `json:"super_username,omitempty"`
41-
ReplicationUsername string `json:"replication_username,omitempty"`
42-
EnablePasswordRotation bool `json:"enable_password_rotation,omitempty"`
43-
PasswordRotationInterval uint32 `json:"password_rotation_interval,omitempty"`
44-
PasswordRotationUserRetention uint32 `json:"password_rotation_user_retention,omitempty"`
40+
SuperUsername string `json:"super_username,omitempty"`
41+
ReplicationUsername string `json:"replication_username,omitempty"`
42+
AdditionalOwnerRoles []string `json:"additional_owner_roles,omitempty"`
43+
EnablePasswordRotation bool `json:"enable_password_rotation,omitempty"`
44+
PasswordRotationInterval uint32 `json:"password_rotation_interval,omitempty"`
45+
PasswordRotationUserRetention uint32 `json:"password_rotation_user_retention,omitempty"`
4546
}
4647

4748
// MajorVersionUpgradeConfiguration defines how to execute major version upgrades of Postgres.

pkg/apis/acid.zalan.do/v1/zz_generated.deepcopy.go

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)