Skip to content

Commit

Permalink
WIP: investigating issue with updating password
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-ratushnyy committed Oct 5, 2023
1 parent 26c93d2 commit 1421721
Showing 1 changed file with 40 additions and 14 deletions.
54 changes: 40 additions & 14 deletions lib/charms/mongodb/v0/mongodb_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,18 @@ def oversee_users(self, departed_relation_id: Optional[int], event):
# We need to wait for the moment when the provider library
# set the database name into the relation.
continue
logger.info("Create relation user: %s on %s", config.username, config.database)
logger.error(">>>>>> Create relation user: %s on %s", config.username, config.database)
mongo.create_user(config)
self._set_relation(config)


for username in relation_users.intersection(database_users):
config = self._get_config(username, None)
logger.info("Update relation user: %s on %s", config.username, config.database)
logger.error(">>>>>> Update relation user: %s on %s", config.username, config.database)
mongo.update_user(config)
logger.info("Updating relation data according to diff")
logger.error("Updating relation data according to diff")
self._diff(event)


if not self.charm.model.config["auto-delete"]:
return
Expand Down Expand Up @@ -224,23 +226,47 @@ def update_app_relation_data(self) -> None:

for relation in self.charm.model.relations[REL_NAME]:
username = self._get_username_from_relation_id(relation.id)
password = relation.data[self.charm.app]["password"]
password = self._get_or_set_password(relation)
config = self._get_config(username, password)
if username in database_users:
self.database_provides.set_endpoints(
relation.id,
",".join(config.hosts),
)
self.database_provides.set_uris(
relation.id,
config.uri,
)
# if username in database_users:
# self.database_provides.set_endpoints(
# relation.id,
# ",".join(config.hosts),
# )
# self.database_provides.set_uris(
# relation.id,
# config.uri,
# )
logger.error(">>>>>>>>>>>>>>>>>>>> update_app_relation_data '%s", self)

def _get_or_set_password(self, relation: Relation) -> str:
"""Retrieve password from cache or generate a new one.
Args:
relation (Relation): The relation for each the password is cached.
Returns:
str: The password.
"""
relation_data = self.database_provides.fetch_relation_data(
[relation.id], ["password"], relation.name
)
logger.error(">>>>>>>>>>>>>>>>>>>> _get_or_set_password '%s', '%s'", relation_data, self)
password = relation_data.get(relation.id, {}).get("password")
if password:
logger.error(">>>>>>>>>>>>>>>>>>>> _get_or_set_password found password '%s', '%s'", password, self)
return password
password = generate_password()
logger.error(">>>>>>>>>>>>>>>>>>>> _get_or_set_password generated password '%s', '%s'", password, self)
self.database_provides.update_relation_data(relation.id, {"password": password})
return password


def _get_config(self, username: str, password: Optional[str]) -> MongoDBConfiguration:
"""Construct the config object for future user creation."""
relation = self._get_relation_from_username(username)
if not password:
password = generate_password()
password = self._get_or_set_password(relation)

return MongoDBConfiguration(
replset=self.charm.app.name,
Expand Down

0 comments on commit 1421721

Please sign in to comment.