Skip to content
Open
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
11 changes: 10 additions & 1 deletion lib/charms/mongodb/v0/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class NotReadyError(PyMongoError):

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

ADMIN_AUTH_SOURCE = "authSource=admin"
SYSTEM_DBS = ("admin", "local", "config")
Expand Down Expand Up @@ -270,6 +270,11 @@ def get_users(self) -> Set[str]:
]
)

def get_all_users(self) -> Set[str]:
"""Get all users, including the three charmed managed users."""
users_info = self.client.admin.command("usersInfo")
return {user_obj["user"] for user_obj in users_info["users"]}
Comment thread
Gu1nness marked this conversation as resolved.

def get_databases(self) -> Set[str]:
"""Return list of all non-default databases."""
databases = self.client.list_database_names()
Expand All @@ -280,3 +285,7 @@ def drop_database(self, database: str):
if database in SYSTEM_DBS:
return
self.client.drop_database(database)

def drop_local_database(self):
"""DANGEROUS: Drops the local database."""
self.client.drop_database("local")
Comment on lines +289 to +291
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is there something we can calculate / store in app databag to verify whether it is safe to drop the local db? then we could add a guardrail by raising a custom exception

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure.
TBH dropping the local database is not really something that should be done, but it does work.
The big issue behind dropping the local DB is that it stores also the oplog.
I can try to drop less information than that in this call in order to be safer but I'm unsure it will work.
Should I give it a try?

48 changes: 47 additions & 1 deletion lib/charms/mongodb/v1/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

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

# path to store mongodb ketFile
KEY_FILE = "keyFile"
Expand All @@ -41,6 +41,9 @@
LOG_DIR = "/var/log/mongodb"
CONF_DIR = "/etc/mongod"
MONGODB_LOG_FILENAME = "mongodb.log"

LOCALHOST = "127.0.0.1"

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -171,6 +174,34 @@ def get_mongos_args(
return " ".join(cmd)


def get_degraded_mongod_args(snap_install: bool = False):
"""Builds a degraded MongoDB startup command line.

This degraded command line starts MongoDB with minimal configuration,
only binds to localhost, without replica set configuration and has no
auth validation in order to be able to update the users and local database.
"""
full_data_dir = f"{MONGODB_COMMON_DIR}{DATA_DIR}" if snap_install else DATA_DIR
logging_options = _get_logging_options(snap_install)
cmd = [
# Only bind to local IP
f"--bind_ip {LOCALHOST}",
# db must be located within the snap common directory since the
# snap is strictly confined
f"--dbpath={full_data_dir}",
# for simplicity we run the mongod daemon on shards, configsvrs,
# and replicas on the same port
f"--port={Config.MONGODB_PORT}",
"--setParameter processUmask=037", # required for log files perminission (g+r)
"--logRotate reopen",
"--logappend",
logging_options,
"--setParameter enableLocalhostAuthBypass=0",
"\n",
]
return " ".join(cmd)


def get_mongod_args(
config: MongoConfiguration,
auth: bool = True,
Expand Down Expand Up @@ -266,6 +297,21 @@ def generate_keyfile() -> str:
return "".join([secrets.choice(choices) for _ in range(1024)])


def generate_lock_hash() -> str:
"""Lock hash used to check if we are reusing storage in a different context or not.

Comment thread
Gu1nness marked this conversation as resolved.
This string is written in the storage of all members of the replica set in
a file, and also as a secret.
Upon starting a new unit, it will check in the storage and in the secret to
compare and decide if we're reusing storage and if yes, in which case we
are.

Returns:
An 8 character random hexadecimal string.
"""
return secrets.token_hex(8)


def copy_licenses_to_unit():
"""Copies licenses packaged in the snap to the charm's licenses directory."""
os.makedirs("src/licenses", exist_ok=True)
Expand Down
44 changes: 42 additions & 2 deletions lib/charms/mongodb/v1/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# See LICENSE file for licensing details.

import logging
from typing import Dict, Set
from typing import Dict, Set, Tuple

from bson.json_util import dumps
from charms.mongodb.v0.mongo import MongoConfiguration, MongoConnection, NotReadyError
Expand All @@ -27,7 +27,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

# path to store mongodb ketFile
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -95,6 +95,35 @@ def init_replset(self) -> None:
# finished.
logger.error("Cannot initialize replica set. error=%r", e)
raise e
else:
logger.error("Error in init %s", e)

@retry(
stop=stop_after_attempt(3),
wait=wait_fixed(5),
reraise=True,
before=before_log(logger, logging.DEBUG),
)
def reconfigure_replset(self, hosts: set[str], version: int, force: bool = False) -> None:
"""Create replica set config the first time.

Raises:
ConfigurationError, ConfigurationError, OperationFailure
"""
config = {
"_id": self.config.replset,
"members": [{"_id": i, "host": h} for i, h in enumerate(hosts)],
"version": version,
}
try:
self.client.admin.command("replSetReconfig", config, force=force)
except OperationFailure as e:
# Unauthorized error can be raised only if initial user were
# created the step after this.
# AlreadyInitialized error can be raised only if this step
# finished.
logger.error("Cannot reconfigure replica set. error=%r", e)
raise e

def get_replset_status(self) -> Dict:
"""Get a replica set status as a dict.
Expand Down Expand Up @@ -128,6 +157,17 @@ def get_replset_members(self) -> Set[str]:
]
return set(curr_members)

def get_replset_members_and_version(self) -> Tuple[Set[str], int]:
"""Get replica set members through config."""
rs_config = self.client.admin.command("replSetGetConfig")
curr_members = [
self._hostname_from_hostport(member["host"])
for member in rs_config["config"]["members"]
]
version = rs_config["config"]["version"]

return set(curr_members), version

def add_replset_member(self, hostname: str) -> None:
"""Add a new member to replica set config inside MongoDB.

Expand Down
Loading