diff --git a/Makefile b/Makefile index d3512557b..ea2bfe48b 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,10 @@ wait-remove: remove: # remove app from current model - juju remove-application --destroy-storage $(app) + juju remove-application $(app) + juju status --storage --format=json 2>/dev/null \ + | jq -r '.storage.volumes[] | select( .status.current == "detached" ) | .storage' \ + | xargs juju remove-storage # redeployment chain redeploy: remove build wait-remove deploy-local diff --git a/lib/charms/mongodb/v0/helpers.py b/lib/charms/mongodb/v0/helpers.py index f64740a0c..1d7b81604 100644 --- a/lib/charms/mongodb/v0/helpers.py +++ b/lib/charms/mongodb/v0/helpers.py @@ -3,6 +3,7 @@ # See LICENSE file for licensing details. import logging +import os import secrets import string import subprocess @@ -20,7 +21,7 @@ # Increment this PATCH version before using `charmcraft publish-lib` or reset # to 0 if you are raising the major API version -LIBPATCH = 6 +LIBPATCH = 7 # path to store mongodb ketFile @@ -166,13 +167,13 @@ def build_unit_status(mongodb_config: MongoDBConfiguration, unit_ip: str) -> Sta replica_status = replset_status[unit_ip] if replica_status == "PRIMARY": - return ActiveStatus("Replica set primary") + return ActiveStatus("Primary") elif replica_status == "SECONDARY": - return ActiveStatus("Replica set secondary") + return ActiveStatus("") elif replica_status in ["STARTUP", "STARTUP2", "ROLLBACK", "RECOVERING"]: - return WaitingStatus("Member is syncing..") + return WaitingStatus("Member is syncing...") elif replica_status == "REMOVED": - return WaitingStatus("Member is removing..") + return WaitingStatus("Member is removing...") else: return BlockedStatus(replica_status) except ServerSelectionTimeoutError as e: @@ -188,6 +189,8 @@ def build_unit_status(mongodb_config: MongoDBConfiguration, unit_ip: str) -> Sta def copy_licenses_to_unit(): """Copies licenses packaged in the snap to the charm's licenses directory.""" + os.makedirs("src/licenses", exist_ok=True) + subprocess.check_output("cp LICENSE src/licenses/LICENSE-charm", shell=True) subprocess.check_output( "cp -r /snap/charmed-mongodb/current/licenses/* src/licenses", shell=True ) diff --git a/lib/charms/mongodb/v0/mongodb.py b/lib/charms/mongodb/v0/mongodb.py index 12bb76e3a..25a65d7c0 100644 --- a/lib/charms/mongodb/v0/mongodb.py +++ b/lib/charms/mongodb/v0/mongodb.py @@ -5,7 +5,7 @@ import logging import re from dataclasses import dataclass -from typing import Dict, List, Set +from typing import Dict, List, Optional, Set from urllib.parse import quote_plus from bson.json_util import dumps @@ -29,7 +29,7 @@ # Increment this PATCH version before using `charmcraft publish-lib` or reset # to 0 if you are raising the major API version -LIBPATCH = 6 +LIBPATCH = 7 # path to store mongodb ketFile logger = logging.getLogger(__name__) @@ -49,7 +49,7 @@ class MongoDBConfiguration: """ replset: str - database: str + database: Optional[str] username: str password: str hosts: Set[str] @@ -284,6 +284,7 @@ def create_user(self, config: MongoDBConfiguration): config.username, pwd=config.password, roles=self._get_roles(config), + mechanisms=["SCRAM-SHA-256"], ) def update_user(self, config: MongoDBConfiguration): diff --git a/lib/charms/mongodb/v0/mongodb_provider.py b/lib/charms/mongodb/v0/mongodb_provider.py index 34f452c86..3738108b7 100644 --- a/lib/charms/mongodb/v0/mongodb_provider.py +++ b/lib/charms/mongodb/v0/mongodb_provider.py @@ -76,8 +76,7 @@ def _on_relation_event(self, event): return # legacy relations have auth disabled, which new relations require - legacy_relation_users = self._get_users_from_relations(None, rel=LEGACY_REL_NAME) - if len(legacy_relation_users) > 0: + if self.model.get_relation(LEGACY_REL_NAME): self.charm.unit.status = BlockedStatus("cannot have both legacy and new relations") logger.error("Auth disabled due to existing connections to legacy relations") return @@ -117,6 +116,13 @@ def oversee_users(self, departed_relation_id: Optional[int], event): relation is still on the list of all relations. Therefore, for proper work of the function, we need to exclude departed relation from the list. """ + # This hook gets called from other contexts within the charm so it is necessary to check + # for legacy relations which have auth disabled, which new relations require + if self.model.get_relation(LEGACY_REL_NAME): + self.charm.unit.status = BlockedStatus("cannot have both legacy and new relations") + logger.error("Auth disabled due to existing connections to legacy relations") + return + with MongoDBConnection(self.charm.mongodb_config) as mongo: database_users = mongo.get_users() relation_users = self._get_users_from_relations(departed_relation_id) diff --git a/src/charm.py b/src/charm.py index 1dd3db2ee..007b7f29a 100755 --- a/src/charm.py +++ b/src/charm.py @@ -502,7 +502,7 @@ def _set_user_created(self, user: MongoDBUser) -> None: self.app_peer_data[f"{user.get_username()}-user-created"] = "True" def _get_mongodb_config_for_user( - self, user: MongoDBUser, hosts: list[str] + self, user: MongoDBUser, hosts: List[str] ) -> MongoDBConfiguration: external_ca, _ = self.tls.get_tls_files("unit") internal_ca, _ = self.tls.get_tls_files("app")