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-4221] Recreate auth_query on backend rerelation #284

Merged
merged 2 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/charms/loki_k8s/v0/loki_push_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,9 @@ def _alert_rules_error(self, event):

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 28
LIBPATCH = 29

PYDEPS = ["cosl"]

logger = logging.getLogger(__name__)

Expand Down
17 changes: 8 additions & 9 deletions lib/charms/prometheus_k8s/v0/prometheus_scrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def _on_scrape_targets_changed(self, event):

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 44
LIBPATCH = 46

PYDEPS = ["cosl"]

Expand Down Expand Up @@ -521,8 +521,8 @@ def expand_wildcard_targets_into_individual_jobs(
# for such a target. Therefore labeling with Juju topology, excluding the
# unit name.
non_wildcard_static_config["labels"] = {
**non_wildcard_static_config.get("labels", {}),
**topology.label_matcher_dict,
**non_wildcard_static_config.get("labels", {}),
}

non_wildcard_static_configs.append(non_wildcard_static_config)
Expand All @@ -547,9 +547,9 @@ def expand_wildcard_targets_into_individual_jobs(
if topology:
# Add topology labels
modified_static_config["labels"] = {
**modified_static_config.get("labels", {}),
**topology.label_matcher_dict,
**{"juju_unit": unit_name},
**modified_static_config.get("labels", {}),
}

# Instance relabeling for topology should be last in order.
Expand Down Expand Up @@ -1537,12 +1537,11 @@ def set_scrape_job_spec(self, _=None):
relation.data[self._charm.app]["scrape_metadata"] = json.dumps(self._scrape_metadata)
relation.data[self._charm.app]["scrape_jobs"] = json.dumps(self._scrape_jobs)

if alert_rules_as_dict:
# Update relation data with the string representation of the rule file.
# Juju topology is already included in the "scrape_metadata" field above.
# The consumer side of the relation uses this information to name the rules file
# that is written to the filesystem.
relation.data[self._charm.app]["alert_rules"] = json.dumps(alert_rules_as_dict)
# Update relation data with the string representation of the rule file.
# Juju topology is already included in the "scrape_metadata" field above.
# The consumer side of the relation uses this information to name the rules file
# that is written to the filesystem.
relation.data[self._charm.app]["alert_rules"] = json.dumps(alert_rules_as_dict)

def _set_unit_ip(self, _=None):
"""Set unit host address.
Expand Down
43 changes: 41 additions & 2 deletions src/relations/backend_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,45 @@ def ready(self) -> bool:

return True

def collect_databases(self) -> List[str]:
"""Collects the names of all client dbs to inject or remove the auth_query."""
databases = [self.database.database, PG]
for relation in self.charm.model.relations.get("db", []):
database = self.charm.legacy_db_relation.get_databags(relation)[0].get("database")
if database and relation.units:
try:
con = self.postgres._connect_to_database(database)
con.close()
databases.append(database)
except psycopg2.OperationalError:
logger.debug("database %s not yet created", database)

for relation in self.charm.model.relations.get("db-admin", []):
database = self.charm.legacy_db_admin_relation.get_databags(relation)[0].get(
"database"
)
if database and relation.units:
try:
con = self.postgres._connect_to_database(database)
con.close()
databases.append(database)
except psycopg2.OperationalError:
logger.debug("database %s not yet created", database)

for _, data in self.charm.client_relation.database_provides.fetch_relation_data(
fields=["database"]
).items():
database = data.get("database")
if database:
try:
con = self.postgres._connect_to_database(database)
con.close()
databases.append(database)
except psycopg2.OperationalError:
logger.debug("database %s not yet created", database)

return databases

def _on_database_created(self, event: DatabaseCreatedEvent) -> None:
"""Handle backend-database-database-created event.

Expand Down Expand Up @@ -258,7 +297,7 @@ def _on_database_created(self, event: DatabaseCreatedEvent) -> None:
# create authentication user on postgres database, so we can authenticate other users
# later on
self.postgres.create_user(self.auth_user, hashed_password, admin=True)
self.initialise_auth_function([self.database.database, PG])
self.initialise_auth_function(self.collect_databases())

# Add the monitoring user.
if not (monitoring_password := self.charm.get_secret(APP_SCOPE, MONITORING_PASSWORD_KEY)):
Expand Down Expand Up @@ -325,7 +364,7 @@ def _on_relation_departed(self, event: RelationDepartedEvent):
# TODO de-authorise all databases
logger.info("removing auth user")
# Remove auth function before broken-hook, while we can still connect to postgres.
self.remove_auth_function([PGB, PG])
self.remove_auth_function(self.collect_databases())
except psycopg2.Error:
remove_auth_fail_msg = (
"failed to remove auth user when disconnecting from postgres application."
Expand Down
Loading