Skip to content

Commit a794a94

Browse files
nmarukovichhors
andauthored
K8SPG-571 grant user access to public schema (#1097)
* K8SPG-571 grant user access to public schema * add test case * update database condition * fix unit tests * fix names * fix PR comments * add validation and update new schema access * fix PR comments * delete force * add import * fix tests * fix tests * fix vars * update vars * fix test * fix log level --------- Co-authored-by: Viacheslav Sarzhan <[email protected]>
1 parent a71d121 commit a794a94

19 files changed

+378
-17
lines changed

build/crd/crunchy/generated/postgres-operator.crunchydata.com_postgresclusters.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20451,6 +20451,10 @@ spec:
2045120451
type: string
2045220452
type: array
2045320453
x-kubernetes-list-type: set
20454+
grantPublicSchemaAccess:
20455+
description: Grant the user access to the public schema in each
20456+
database listed under `databases`.
20457+
type: boolean
2045420458
name:
2045520459
description: |-
2045620460
The name of this PostgreSQL user. The value may contain only lowercase

build/crd/percona/generated/pgv2.percona.com_perconapgclusters.yaml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ spec:
5858
properties:
5959
autoCreateUserSchema:
6060
description: |-
61-
Whether or not the cluster has schemas automatically created for the user
62-
defined in `spec.users` for all of the databases listed for that user.
61+
Indicates whether schemas are automatically created for the user
62+
specified in `spec.users` across all databases associated with that user.
6363
type: boolean
6464
backups:
6565
description: PostgreSQL backup configuration
@@ -18252,6 +18252,10 @@ spec:
1825218252
type: string
1825318253
type: array
1825418254
x-kubernetes-list-type: set
18255+
grantPublicSchemaAccess:
18256+
description: Grant the user access to the public schema in each
18257+
database listed under `databases`.
18258+
type: boolean
1825518259
name:
1825618260
description: |-
1825718261
The name of this PostgreSQL user. The value may contain only lowercase
@@ -18303,6 +18307,11 @@ spec:
1830318307
- instances
1830418308
- postgresVersion
1830518309
type: object
18310+
x-kubernetes-validations:
18311+
- message: PostgresVersion must be >= 15 if grantPublicSchemaAccess exists
18312+
and is true
18313+
rule: '!has(self.users) || self.postgresVersion >= 15 || self.users.all(u,
18314+
!has(u.grantPublicSchemaAccess) || !u.grantPublicSchemaAccess)'
1830618315
status:
1830718316
properties:
1830818317
host:

config/crd/bases/pgv2.percona.com_perconapgclusters.yaml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,8 @@ spec:
463463
properties:
464464
autoCreateUserSchema:
465465
description: |-
466-
Whether or not the cluster has schemas automatically created for the user
467-
defined in `spec.users` for all of the databases listed for that user.
466+
Indicates whether schemas are automatically created for the user
467+
specified in `spec.users` across all databases associated with that user.
468468
type: boolean
469469
backups:
470470
description: PostgreSQL backup configuration
@@ -18657,6 +18657,10 @@ spec:
1865718657
type: string
1865818658
type: array
1865918659
x-kubernetes-list-type: set
18660+
grantPublicSchemaAccess:
18661+
description: Grant the user access to the public schema in each
18662+
database listed under `databases`.
18663+
type: boolean
1866018664
name:
1866118665
description: |-
1866218666
The name of this PostgreSQL user. The value may contain only lowercase
@@ -18708,6 +18712,11 @@ spec:
1870818712
- instances
1870918713
- postgresVersion
1871018714
type: object
18715+
x-kubernetes-validations:
18716+
- message: PostgresVersion must be >= 15 if grantPublicSchemaAccess exists
18717+
and is true
18718+
rule: '!has(self.users) || self.postgresVersion >= 15 || self.users.all(u,
18719+
!has(u.grantPublicSchemaAccess) || !u.grantPublicSchemaAccess)'
1871118720
status:
1871218721
properties:
1871318722
host:

config/crd/bases/postgres-operator.crunchydata.com_postgresclusters.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20349,6 +20349,10 @@ spec:
2034920349
type: string
2035020350
type: array
2035120351
x-kubernetes-list-type: set
20352+
grantPublicSchemaAccess:
20353+
description: Grant the user access to the public schema in each
20354+
database listed under `databases`.
20355+
type: boolean
2035220356
name:
2035320357
description: |-
2035420358
The name of this PostgreSQL user. The value may contain only lowercase

deploy/bundle.yaml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -760,8 +760,8 @@ spec:
760760
properties:
761761
autoCreateUserSchema:
762762
description: |-
763-
Whether or not the cluster has schemas automatically created for the user
764-
defined in `spec.users` for all of the databases listed for that user.
763+
Indicates whether schemas are automatically created for the user
764+
specified in `spec.users` across all databases associated with that user.
765765
type: boolean
766766
backups:
767767
description: PostgreSQL backup configuration
@@ -18954,6 +18954,10 @@ spec:
1895418954
type: string
1895518955
type: array
1895618956
x-kubernetes-list-type: set
18957+
grantPublicSchemaAccess:
18958+
description: Grant the user access to the public schema in each
18959+
database listed under `databases`.
18960+
type: boolean
1895718961
name:
1895818962
description: |-
1895918963
The name of this PostgreSQL user. The value may contain only lowercase
@@ -19005,6 +19009,11 @@ spec:
1900519009
- instances
1900619010
- postgresVersion
1900719011
type: object
19012+
x-kubernetes-validations:
19013+
- message: PostgresVersion must be >= 15 if grantPublicSchemaAccess exists
19014+
and is true
19015+
rule: '!has(self.users) || self.postgresVersion >= 15 || self.users.all(u,
19016+
!has(u.grantPublicSchemaAccess) || !u.grantPublicSchemaAccess)'
1900819017
status:
1900919018
properties:
1901019019
host:
@@ -46837,6 +46846,10 @@ spec:
4683746846
type: string
4683846847
type: array
4683946848
x-kubernetes-list-type: set
46849+
grantPublicSchemaAccess:
46850+
description: Grant the user access to the public schema in each
46851+
database listed under `databases`.
46852+
type: boolean
4684046853
name:
4684146854
description: |-
4684246855
The name of this PostgreSQL user. The value may contain only lowercase

deploy/cr.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ spec:
7272
# password:
7373
# type: ASCII
7474
# secretName: "rhino-credentials"
75+
# grantPublicSchemaAccess: false
7576

7677
# databaseInitSQL:
7778
# key: init.sql

deploy/crd.yaml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -760,8 +760,8 @@ spec:
760760
properties:
761761
autoCreateUserSchema:
762762
description: |-
763-
Whether or not the cluster has schemas automatically created for the user
764-
defined in `spec.users` for all of the databases listed for that user.
763+
Indicates whether schemas are automatically created for the user
764+
specified in `spec.users` across all databases associated with that user.
765765
type: boolean
766766
backups:
767767
description: PostgreSQL backup configuration
@@ -18954,6 +18954,10 @@ spec:
1895418954
type: string
1895518955
type: array
1895618956
x-kubernetes-list-type: set
18957+
grantPublicSchemaAccess:
18958+
description: Grant the user access to the public schema in each
18959+
database listed under `databases`.
18960+
type: boolean
1895718961
name:
1895818962
description: |-
1895918963
The name of this PostgreSQL user. The value may contain only lowercase
@@ -19005,6 +19009,11 @@ spec:
1900519009
- instances
1900619010
- postgresVersion
1900719011
type: object
19012+
x-kubernetes-validations:
19013+
- message: PostgresVersion must be >= 15 if grantPublicSchemaAccess exists
19014+
and is true
19015+
rule: '!has(self.users) || self.postgresVersion >= 15 || self.users.all(u,
19016+
!has(u.grantPublicSchemaAccess) || !u.grantPublicSchemaAccess)'
1900819017
status:
1900919018
properties:
1901019019
host:
@@ -46837,6 +46846,10 @@ spec:
4683746846
type: string
4683846847
type: array
4683946848
x-kubernetes-list-type: set
46849+
grantPublicSchemaAccess:
46850+
description: Grant the user access to the public schema in each
46851+
database listed under `databases`.
46852+
type: boolean
4684046853
name:
4684146854
description: |-
4684246855
The name of this PostgreSQL user. The value may contain only lowercase

deploy/cw-bundle.yaml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -760,8 +760,8 @@ spec:
760760
properties:
761761
autoCreateUserSchema:
762762
description: |-
763-
Whether or not the cluster has schemas automatically created for the user
764-
defined in `spec.users` for all of the databases listed for that user.
763+
Indicates whether schemas are automatically created for the user
764+
specified in `spec.users` across all databases associated with that user.
765765
type: boolean
766766
backups:
767767
description: PostgreSQL backup configuration
@@ -18954,6 +18954,10 @@ spec:
1895418954
type: string
1895518955
type: array
1895618956
x-kubernetes-list-type: set
18957+
grantPublicSchemaAccess:
18958+
description: Grant the user access to the public schema in each
18959+
database listed under `databases`.
18960+
type: boolean
1895718961
name:
1895818962
description: |-
1895918963
The name of this PostgreSQL user. The value may contain only lowercase
@@ -19005,6 +19009,11 @@ spec:
1900519009
- instances
1900619010
- postgresVersion
1900719011
type: object
19012+
x-kubernetes-validations:
19013+
- message: PostgresVersion must be >= 15 if grantPublicSchemaAccess exists
19014+
and is true
19015+
rule: '!has(self.users) || self.postgresVersion >= 15 || self.users.all(u,
19016+
!has(u.grantPublicSchemaAccess) || !u.grantPublicSchemaAccess)'
1900819017
status:
1900919018
properties:
1901019019
host:
@@ -46837,6 +46846,10 @@ spec:
4683746846
type: string
4683846847
type: array
4683946848
x-kubernetes-list-type: set
46849+
grantPublicSchemaAccess:
46850+
description: Grant the user access to the public schema in each
46851+
database listed under `databases`.
46852+
type: boolean
4684046853
name:
4684146854
description: |-
4684246855
The name of this PostgreSQL user. The value may contain only lowercase
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: kuttl.dev/v1beta1
2+
kind: TestStep
3+
timeout: 10
4+
commands:
5+
- script: |-
6+
set -o errexit
7+
set -o xtrace
8+
9+
source ../../functions
10+
11+
kubectl -n ${NAMESPACE} patch perconapgcluster/${test_name} --type=json -p '[{"op":"add", "path":"/spec/autoCreateUserSchema","value":true},{"op":"add", "path":"/spec/users","value":[{"name":"chico","databases":["spain"],"password":{"type":"ASCII"},"secretName":"chico-credentials", "grantPublicSchemaAccess": true}]}]'
12+
sleep 10

e2e-tests/tests/users/13-assert.yaml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
apiVersion: postgres-operator.crunchydata.com/v1beta1
2+
kind: PostgresCluster
3+
metadata:
4+
name: users
5+
ownerReferences:
6+
- apiVersion: pgv2.percona.com/v2
7+
kind: PerconaPGCluster
8+
name: users
9+
controller: true
10+
blockOwnerDeletion: true
11+
finalizers:
12+
- postgres-operator.crunchydata.com/finalizer
13+
status:
14+
instances:
15+
- name: instance1
16+
readyReplicas: 3
17+
replicas: 3
18+
updatedReplicas: 3
19+
pgbackrest:
20+
repoHost:
21+
apiVersion: apps/v1
22+
kind: StatefulSet
23+
ready: true
24+
repos:
25+
- bound: true
26+
name: repo1
27+
replicaCreateBackupComplete: true
28+
stanzaCreated: true
29+
proxy:
30+
pgBouncer:
31+
readyReplicas: 3
32+
replicas: 3
33+
---
34+
apiVersion: pgv2.percona.com/v2
35+
kind: PerconaPGCluster
36+
metadata:
37+
name: users
38+
status:
39+
pgbouncer:
40+
ready: 3
41+
size: 3
42+
postgres:
43+
instances:
44+
- name: instance1
45+
ready: 3
46+
size: 3
47+
ready: 3
48+
size: 3
49+
state: ready
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
apiVersion: kuttl.dev/v1beta1
2+
kind: TestStep
3+
commands:
4+
- script: |-
5+
set -o errexit
6+
set -o xtrace
7+
8+
source ../../functions
9+
10+
password=$(get_psql_user_pass chico-credentials)
11+
user='chico'
12+
db_name='spain'
13+
schema='public'
14+
hostname=$(get_pgbouncer_host chico-credentials)
15+
16+
17+
run_psql \
18+
'SET search_path TO public;CREATE TABLE IF NOT EXISTS customApp (id int PRIMARY KEY);' \
19+
"-h $hostname -U $user -d $db_name" "$password"
20+
run_psql \
21+
"INSERT INTO $schema.customApp (id) VALUES (100500)" \
22+
"-h $hostname -U $user -d $db_name" "$password"
23+

e2e-tests/tests/users/15-assert.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apiVersion: kuttl.dev/v1beta1
2+
kind: TestAssert
3+
timeout: 30
4+
---
5+
kind: ConfigMap
6+
apiVersion: v1
7+
metadata:
8+
name: 10-read-from-primary-custom-db
9+
data:
10+
data: ' 100500'
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
apiVersion: kuttl.dev/v1beta1
2+
kind: TestStep
3+
timeout: 30
4+
commands:
5+
- script: |-
6+
set -o errexit
7+
set -o xtrace
8+
9+
source ../../functions
10+
11+
password=$(get_psql_user_pass chico-credentials)
12+
user='chico'
13+
db_name='spain'
14+
schema='public'
15+
hostname=$(get_pgbouncer_host chico-credentials)
16+
17+
data=$(run_psql "SELECT * from $schema.customApp;" "-h $hostname -U $user -d $db_name" "$password")
18+
19+
kubectl create configmap -n "${NAMESPACE}" 10-read-from-primary-custom-db --from-literal=data="${data}"

0 commit comments

Comments
 (0)