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-3314] remove secrets on broken #336

Merged
merged 3 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
36 changes: 33 additions & 3 deletions lib/charms/mongodb/v0/config_server_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

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


class ClusterProvider(Object):
Expand Down Expand Up @@ -182,8 +182,13 @@ def __init__(
self.framework.observe(
charm.on[self.relation_name].relation_changed, self._on_relation_changed
)

# TODO Future PRs handle scale down
self.framework.observe(
charm.on[self.relation_name].relation_departed,
self.charm.check_relation_broken_or_scale_down,
)
self.framework.observe(
charm.on[self.relation_name].relation_broken, self._on_relation_broken
)

def _on_database_created(self, event) -> None:
if not self.charm.unit.is_leader():
Expand Down Expand Up @@ -228,6 +233,31 @@ def _on_relation_changed(self, event) -> None:

self.charm.unit.status = ActiveStatus()

def _on_relation_broken(self, event) -> None:
MiaAltieri marked this conversation as resolved.
Show resolved Hide resolved
# Only relation_deparated events can check if scaling down
departed_relation_id = event.relation.id
MiaAltieri marked this conversation as resolved.
Show resolved Hide resolved
if not self.charm.has_departed_run(departed_relation_id):
logger.info(
"Deferring, must wait for relation departed hook to decide if relation should be removed."
)
event.defer()
return

if not self.charm.proceed_on_broken_event(event):
logger.info("Skipping relation broken event, broken event due to scale down")
return

self.charm.stop_mongos_service()
logger.info("Stopped mongos daemon")

if not self.charm.unit.is_leader():
return

logger.info("Database and user removed for mongos application")
self.charm.remove_secret(Config.Relations.APP_SCOPE, Config.Secrets.USERNAME)
self.charm.remove_secret(Config.Relations.APP_SCOPE, Config.Secrets.PASSWORD)
self.charm.remove_connection_info()

# BEGIN: helper functions

def is_mongos_running(self) -> bool:
Expand Down
18 changes: 14 additions & 4 deletions lib/charms/mongodb/v1/shards_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 3
LIBPATCH = 4
KEYFILE_KEY = "key-file"
HOSTS_KEY = "host"
OPERATOR_PASSWORD_KEY = MongoDBUser.get_password_key_name_for_user(OperatorUser.get_username())
Expand Down Expand Up @@ -146,6 +146,18 @@ def pass_hook_checks(self, event: EventBase) -> bool:
event.defer()
return False

if isinstance(event, RelationBrokenEvent):
MiaAltieri marked this conversation as resolved.
Show resolved Hide resolved
departed_relation_id = event.relation.id
MiaAltieri marked this conversation as resolved.
Show resolved Hide resolved
if not self.charm.has_departed_run(departed_relation_id):
logger.info(
"Deferring, must wait for relation departed hook to decide if relation should be removed."
)
event.defer()
return False

if not self.charm.proceed_on_broken_event(event):
return False

return True

def _on_relation_event(self, event):
Expand All @@ -159,9 +171,7 @@ def _on_relation_event(self, event):

departed_relation_id = None
if isinstance(event, RelationBrokenEvent):
departed_relation_id = self.charm.proceed_on_broken_event(event)
if not departed_relation_id:
return
departed_relation_id = event.relation.id
MiaAltieri marked this conversation as resolved.
Show resolved Hide resolved

try:
logger.info("Adding/Removing shards not present in cluster.")
Expand Down
6 changes: 3 additions & 3 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1334,16 +1334,16 @@ def proceed_on_broken_event(self, event) -> int:
"Deferring, must wait for relation departed hook to decide if relation should be removed."
)
event.defer()
return
return False
MiaAltieri marked this conversation as resolved.
Show resolved Hide resolved

# check if were scaling down and add a log message
if self.is_scaling_down(departed_relation_id):
logger.info(
"Relation broken event occurring due to scale down, do not proceed to remove users."
)
return
return False

return departed_relation_id
return True

@staticmethod
def _generate_relation_departed_key(rel_id: int) -> str:
Expand Down
Loading