Skip to content
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

Merged
merged 4 commits into from
May 17, 2024
Merged

Conversation

dragomirp
Copy link
Contributor

@dragomirp dragomirp commented May 8, 2024

Issue

Cannot drop db objects when rerelating to pgbouncer

Solution

  • Pass relations to create_database
  • Update charm libs

Comment on lines +260 to +262
self.charm.backend.postgres.create_database(
database, user, client_relations=self.charm.client_relations
)
Copy link
Contributor Author

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.

Copy link
Member

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.

Comment on lines +433 to +434
dbs = self.charm.get_relation_databases()
database = dbs.pop(str(broken_event.relation.id), {}).get("name")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need the db name to remove the auth function.

@@ -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])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the auth_function was not removed, create_database() will change ownership and initialise_auth_function() will not recreate it.

@dragomirp dragomirp marked this pull request as ready for review May 8, 2024 11:23
Comment on lines 438 to 440
delete_db = database not in dbs.values()
if database and delete_db:
self.charm.backend.remove_auth_function(dbs=[database])
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Comment on lines 202 to 204
delete_db = database not in dbs.values()
if database and delete_db:
self.charm.backend.remove_auth_function(dbs=[database])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the auth_function is not being correctly created/removed correctly.

By running the following commands, I lost the connection through app1 credentials (data-integrator) when I removed only the relation between app2 (another data-integrator) and pgbouncer-k8s.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dbs.values() are dicts so delete_db was always true. Nice catch.

Comment on lines +260 to +262
self.charm.backend.postgres.create_database(
database, user, client_relations=self.charm.client_relations
)
Copy link
Member

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.

@dragomirp dragomirp requested a review from marceloneppel May 10, 2024 13:42
Copy link
Contributor

@taurus-forever taurus-forever left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@dragomirp dragomirp merged commit 6247dc6 into main May 17, 2024
34 checks passed
@dragomirp dragomirp deleted the dpe-1454-rerelation-drop branch May 17, 2024 08:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants