-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DPE-1454] Update database ownership #287
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -257,8 +257,9 @@ def _on_relation_joined(self, join_event: RelationJoinedEvent): | |
logger.info(init_msg) | ||
|
||
self.charm.backend.postgres.create_user(user, password, admin=self.admin) | ||
self.charm.backend.postgres.create_database(database, user) | ||
|
||
self.charm.backend.postgres.create_database( | ||
database, user, client_relations=self.charm.client_relations | ||
) | ||
created_msg = f"database and user for {self.relation_name} relation created" | ||
self.charm.unit.status = initial_status | ||
self.charm.update_status() | ||
|
@@ -270,6 +271,7 @@ def _on_relation_joined(self, join_event: RelationJoinedEvent): | |
return | ||
|
||
# set up auth function | ||
self.charm.backend.remove_auth_function(dbs=[database]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the auth_function was not removed, |
||
self.charm.backend.initialise_auth_function([database]) | ||
|
||
def _on_relation_changed(self, change_event: RelationChangedEvent): | ||
|
@@ -428,11 +430,14 @@ def _on_relation_broken(self, broken_event: RelationBrokenEvent): | |
self._check_for_blocking_relations(broken_event.relation.id) | ||
return | ||
|
||
dbs = self.charm.generate_relation_databases() | ||
dbs.pop(str(broken_event.relation.id), None) | ||
dbs = self.charm.get_relation_databases() | ||
database = dbs.pop(str(broken_event.relation.id), {}).get("name") | ||
Comment on lines
+433
to
+434
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need the db name to remove the auth function. |
||
self.charm.set_relation_databases(dbs) | ||
if self.charm.unit.is_leader(): | ||
self.charm.backend.postgres.delete_user(user) | ||
delete_db = database not in dbs.values() | ||
if database and delete_db: | ||
self.charm.backend.remove_auth_function(dbs=[database]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only delete the auth query if no other relations use the same db. |
||
|
||
self._check_for_blocking_relations(broken_event.relation.id) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,8 +140,11 @@ def _on_database_requested(self, event: DatabaseRequestedEvent) -> None: | |
user, password, extra_user_roles=extra_user_roles | ||
) | ||
logger.debug("creating database") | ||
self.charm.backend.postgres.create_database(database, user) | ||
self.charm.backend.postgres.create_database( | ||
database, user, client_relations=self.charm.client_relations | ||
) | ||
# set up auth function | ||
self.charm.backend.remove_auth_function(dbs=[database]) | ||
self.charm.backend.initialise_auth_function(dbs=[database]) | ||
except ( | ||
PostgreSQLCreateDatabaseError, | ||
|
@@ -192,10 +195,13 @@ def _on_relation_broken(self, event: RelationBrokenEvent) -> None: | |
self.charm.peers.unit_databag.pop(self._depart_flag(event.relation), None) | ||
return | ||
|
||
dbs = self.charm.generate_relation_databases() | ||
dbs.pop(str(event.relation.id), None) | ||
dbs = self.charm.get_relation_databases() | ||
database = dbs.pop(str(event.relation.id), {}).get("name") | ||
self.charm.set_relation_databases(dbs) | ||
|
||
delete_db = database not in dbs.values() | ||
if database and delete_db: | ||
self.charm.backend.remove_auth_function(dbs=[database]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the By running the following commands, I lost the connection through juju deploy postgresql-k8s --channel 14/edge --trust
juju deploy ./pgbouncer-k8s_ubuntu-22.04-amd64.charm --resource pgbouncer-image=ghcr.io/canonical/charmed-postgresql@sha256:31cf150b4523481202c1ff9b7b5d7f0b36729edad89d61242d8f1eb56b2912c0 --trust
juju deploy data-integrator app1
juju deploy data-integrator app2
juju relate pgbouncer-k8s postgresql-k8s
juju config app1 database-name=test
juju config app2 database-name=test
juju relate app1 pgbouncer-k8s
juju relate app2 pgbouncer-k8s
juju run postgresql-k8s/leader get-password
psql "host={postgresql-unit-ip} user=operator dbname=test password={password}”
# Run SELECT specific_schema FROM information_schema.routines WHERE routine_name='get_auth'; which outputs only pgbouncer_auth_relation_id_7.
juju run app1/leader get-credentials
psql "host={ip-from-get-credentials} user={user-from-get-credentials} dbname={database-name-from-get-credentials} password={password-from-get-credentials}”
# The connection works.
juju remove-relation app2 pgbouncer-k8s
psql "host={postgresql-unit-ip} user=operator dbname=test password={password}”
# Run SELECT specific_schema FROM information_schema.routines WHERE routine_name='get_auth'; which outputs nothing.
juju run app1/leader get-credentials # To check that the same credentials from before are shown.
psql "host={ip-from-get-credentials} user={user-from-get-credentials} dbname={database-name-from-get-credentials} password={password-from-get-credentials}”
# The connection doesn’t work. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
# Delete the user. | ||
try: | ||
user = f"relation_id_{event.relation.id}" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copied from the PG charm. This can potentially cause issues if both PG and PGB are related on the same database, as both will think they own it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it may happen only when we change the PgBouncer extra user roles from
SUPERUSER
to fewer privileges.