canonical / canonical/mongodb-operator

[VM & K8S] MongoDBProvider duplicates information in databag and is error-prone

Open
#236 1 comment 0 reactions 0 assignees View on GitHub
bug
Dominant language
HCL
Stars
14
Forks
15
Avg merge
9h 44m
Merged PRs (30d)
17

Description

Looking into MongoDBProvider class, I can see it registers the same information for each user twice, where username, database, hosts, password and replset are registered as variables and as part of the URI in the databag.

That means any change in the charm code must take into consideration that both the configuration itself and the URI must be updated as either maybe used.

That is passed in the mongodb provider:
```
class MongoDBProvider(Object):

...

def _set_relation(self, config: MongoDBConfiguration):
"""Save all output fields into application relation."""
relation = self._get_relation_from_username(config.username)
if relation is None:
return None

data = relation.data[self.charm.app]
data["username"] = config.username
data["password"] = config.password
data["database"] = config.database
data["endpoints"] = ",".join(config.hosts)
data["replset"] = config.replset
data["uris"] = config.uri
relation.data[self.charm.app].update(data)

```

And the MongoDBConfiguration code:

```
class MongoDBConfiguration:

...

replset: str
database: Optional[str]
username: str
password: str
hosts: Set[str]
roles: Set[str]
tls_external: bool
tls_internal: bool

@property
def uri(self):
"""Return URI concatenated from fields."""
hosts = ",".join(self.hosts)
# Auth DB should be specified while user connects to application DB.
auth_source = ""
if self.database != "admin":
auth_source = "&authSource=admin"
return (
f"mongodb://{quote_plus(self.username)}:"
f"{quote_plus(self.password)}@"
f"{hosts}/{quote_plus(self.database)}?"
f"replicaSet={quote_plus(self.replset)}"
f"{auth_source}"
)
```

Contributor guide

Open the contributing guide

Research direction

Start by reading MongoDBProvider._set_relation and MongoDBConfiguration.uri to understand how relation data is assembled. Trace consumers of the username, password, database, endpoints, replset, and uris fields, then confirm the chosen representation is consistent and no longer duplicates the same information.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
databases
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.