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

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

Open
phvalguima opened this issue Jul 20, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@phvalguima
Copy link
Contributor

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}"
        )
@phvalguima phvalguima added the bug Something isn't working label Jul 20, 2023
@github-actions
Copy link

@phvalguima phvalguima changed the title MongoDBProvider duplicates information in databag and is error-prone [VM & K8S] MongoDBProvider duplicates information in databag and is error-prone Jul 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant