Skip to content

Commit 355e667

Browse files
committed
Allow admin extra user role to regrant
1 parent 1c90b97 commit 355e667

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

lib/charms/postgresql_k8s/v0/postgresql.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
# Increment this PATCH version before using `charmcraft publish-lib` or reset
3737
# to 0 if you are raising the major API version
38-
LIBPATCH = 51
38+
LIBPATCH = 52
3939

4040
# Groups to distinguish HBA access
4141
ACCESS_GROUP_IDENTITY = "identity_access"
@@ -208,6 +208,7 @@ def create_database(
208208
user: str,
209209
plugins: Optional[List[str]] = None,
210210
client_relations: Optional[List[Relation]] = None,
211+
admin: bool = False,
211212
) -> None:
212213
"""Creates a new database and grant privileges to a user on it.
213214
@@ -216,6 +217,7 @@ def create_database(
216217
user: user that will have access to the database.
217218
plugins: extensions to enable in the new database.
218219
client_relations: current established client relations.
220+
admin: if the user should be admin.
219221
"""
220222
plugins = plugins if plugins else []
221223
client_relations = client_relations if client_relations else []
@@ -233,9 +235,14 @@ def create_database(
233235
)
234236
)
235237
for user_to_grant_access in [user, PERMISSIONS_GROUP_ADMIN, *self.system_users]:
238+
regrant = ""
239+
if (
240+
user_to_grant_access == user and admin
241+
) or user_to_grant_access == PERMISSIONS_GROUP_ADMIN:
242+
regrant = "WITH GRANT OPTION"
236243
cursor.execute(
237-
SQL("GRANT ALL PRIVILEGES ON DATABASE {} TO {};").format(
238-
Identifier(database), Identifier(user_to_grant_access)
244+
SQL("GRANT ALL PRIVILEGES ON DATABASE {} TO {}{};").format(
245+
Identifier(database), Identifier(user_to_grant_access), Literal(regrant)
239246
)
240247
)
241248
relations_accessing_this_database = 0

src/relations/postgresql_provider.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,11 @@ def _on_database_requested(self, event: DatabaseRequestedEvent) -> None:
106106
plugins = self.charm.get_plugins()
107107

108108
self.charm.postgresql.create_database(
109-
database, user, plugins=plugins, client_relations=self.charm.client_relations
109+
database,
110+
user,
111+
plugins=plugins,
112+
client_relations=self.charm.client_relations,
113+
admin="admin" in extra_user_roles,
110114
)
111115

112116
# Share the credentials with the application.

tests/unit/test_postgresql.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def test_create_database(harness):
9393
Identifier(database),
9494
SQL(" TO "),
9595
Identifier(user),
96+
Literal(""),
9697
SQL(";"),
9798
])
9899
),
@@ -102,6 +103,7 @@ def test_create_database(harness):
102103
Identifier(database),
103104
SQL(" TO "),
104105
Identifier(PERMISSIONS_GROUP_ADMIN),
106+
Literal("WITH GRANT OPTION"),
105107
SQL(";"),
106108
])
107109
),
@@ -111,6 +113,7 @@ def test_create_database(harness):
111113
Identifier(database),
112114
SQL(" TO "),
113115
Identifier(BACKUP_USER),
116+
Literal(""),
114117
SQL(";"),
115118
])
116119
),
@@ -120,6 +123,7 @@ def test_create_database(harness):
120123
Identifier(database),
121124
SQL(" TO "),
122125
Identifier(REPLICATION_USER),
126+
Literal(""),
123127
SQL(";"),
124128
])
125129
),
@@ -129,6 +133,7 @@ def test_create_database(harness):
129133
Identifier(database),
130134
SQL(" TO "),
131135
Identifier(REWIND_USER),
136+
Literal(""),
132137
SQL(";"),
133138
])
134139
),
@@ -138,6 +143,7 @@ def test_create_database(harness):
138143
Identifier(database),
139144
SQL(" TO "),
140145
Identifier(USER),
146+
Literal(""),
141147
SQL(";"),
142148
])
143149
),
@@ -147,6 +153,7 @@ def test_create_database(harness):
147153
Identifier(database),
148154
SQL(" TO "),
149155
Identifier(MONITORING_USER),
156+
Literal(""),
150157
SQL(";"),
151158
])
152159
),

tests/unit/test_postgresql_provider.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,7 @@ def test_on_database_requested(harness):
117117
database_relation = harness.model.get_relation(RELATION_NAME)
118118
client_relations = [database_relation]
119119
postgresql_mock.create_database.assert_called_once_with(
120-
DATABASE,
121-
user,
122-
plugins=["pgaudit"],
123-
client_relations=client_relations,
120+
DATABASE, user, plugins=["pgaudit"], client_relations=client_relations, admin=False
124121
)
125122
postgresql_mock.get_postgresql_version.assert_called_once()
126123

0 commit comments

Comments
 (0)