Skip to content

Commit

Permalink
fix: updated mongo provider to accept special character in passwords,…
Browse files Browse the repository at this point in the history
… pass… (#627)
  • Loading branch information
vijay-jangir authored Dec 13, 2023
1 parent 9086303 commit 29a6c2c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 23 deletions.
14 changes: 8 additions & 6 deletions docs/providers/documentation/mongodb-provider.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,24 @@ The `query` function returns either a `list` or a `tuple` of results, depending

The following authentication parameters are used to connect to the MongoDB database:

- `uri` (str, optional): The MongoDB connection URI. If provided, other authentication parameters will be ignored.
- `username` (str): The MongoDB username.
- `password` (str): The MongoDB password.
- `host` (str): The MongoDB hostname.
- `database` (str): The name of the MongoDB database.
- `host` (str): The MongoDB connection URI. It can be a full uri with database, authSource, user, pass; or just hostip.
- `username` (str, optional): The MongoDB username.
- `password` (str, optional): The MongoDB password.
- `database` (str, optional): The name of the MongoDB database.
- `authSource` (str, optional): The name of the database against which authentication needs to be done.
- `additional_options` (str, optional): Additinal options to be passed to MongoClient as kwargs.

## Connecting with the Provider

In order to connect to the MongoDB database, you can use either a connection URI or individual parameters. Here's how you can provide authentication information:

1. If using a connection URI, provide the `uri` parameter with the MongoDB connection string.
1. If using a connection URI, provide the `host` parameter with the MongoDB connection string.
2. If using individual parameters, provide the following:
- `username`: MongoDB username.
- `password`: MongoDB password.
- `host`: MongoDB hostname.
- `database`: MongoDB database name.
- `authSource`: MongoDB database name.

## Notes

Expand Down
57 changes: 40 additions & 17 deletions keep/providers/mongodb_provider/mongodb_provider.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
"""
MongodbProvider is a class that provides a way to read data from MySQL.
"""

import dataclasses
import os

Expand All @@ -11,24 +15,38 @@

@pydantic.dataclasses.dataclass
class MongodbProviderAuthConfig:
uri: str | None = dataclasses.field(
metadata={"required": False, "description": "MongoDB connection URI"}
host: str = dataclasses.field(
metadata={
"required": True,
"description": "Mongo host_uri",
"hint": "any valid mongo host_uri like host:port, user:paassword@host:port?authSource",
}
)
username: str = dataclasses.field(
metadata={"required": False, "description": "MongoDB username"}
metadata={"required": False, "description": "MongoDB username"}, default=None
)
password: str = dataclasses.field(
metadata={
"required": False,
"description": "MongoDB password",
"sensitive": True,
}
)
host: str = dataclasses.field(
metadata={"required": False, "description": "MongoDB hostname"}
},
default=None,
)
database: str = dataclasses.field(
metadata={"required": False, "description": "MongoDB database name"}
metadata={"required": False, "description": "MongoDB database name"},
default=None,
)
auth_source: str | None = dataclasses.field(
metadata={"required": False, "description": "Mongo authSource database name"},
default=None,
)
additional_options: dict | None = dataclasses.field(
metadata={
"required": False,
"description": "Mongo kwargs, these will be passed to MongoClient",
},
default_factory=dict,
)


Expand Down Expand Up @@ -74,12 +92,18 @@ def __generate_client(self):
Returns:
pymongo.MongoClient: MongoDB Client
"""
if self.authentication_config.uri:
client = MongoClient(self.authentication_config.uri)
else:
client = MongoClient(
f"mongodb://{self.authentication_config.username}:{self.authentication_config.password}@{self.authentication_config.host}/{self.authentication_config.database}"
)
# removing all None fields, as mongo will not accept None fields}
client_conf = {
k: v
for k, v in self.authentication_config.__dict__.items()
if v
and not k.startswith("__pydantic") # removing pydantic default key
and k != "additional_options" # additional_options will go seperately
and k != "database"
} # database is not a valid mongo option
client = MongoClient(
**client_conf, **self.authentication_config.additional_options
)
return client

def dispose(self):
Expand Down Expand Up @@ -107,7 +131,7 @@ def _query(
"""
client = self.__generate_client()
database = client[self.authentication_config.database]
results = list(database.command(**query))
results = list(database.cursor_command(query))

if single_row:
return results[0] if results else None
Expand All @@ -118,10 +142,9 @@ def _query(
if __name__ == "__main__":
config = ProviderConfig(
authentication={
"uri": os.environ.get("MONGODB_URI"),
"host": os.environ.get("MONGODB_HOST"),
"username": os.environ.get("MONGODB_USER"),
"password": os.environ.get("MONGODB_PASSWORD"),
"host": os.environ.get("MONGODB_HOST"),
"database": os.environ.get("MONGODB_DATABASE"),
}
)
Expand Down

1 comment on commit 29a6c2c

@vercel
Copy link

@vercel vercel bot commented on 29a6c2c Dec 13, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

keep – ./

platform.keephq.dev
keep-eight.vercel.app
keep-keephq.vercel.app
keep-git-main-keephq.vercel.app

Please sign in to comment.