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

Fix mongo url with user/password #291

Merged
merged 3 commits into from
Jun 17, 2024
Merged

Fix mongo url with user/password #291

merged 3 commits into from
Jun 17, 2024

Conversation

gdespres
Copy link
Contributor

Hi !

I'm currently migrating my instance of cve-search from version 4.x to version 5.x and CveExplorer does not allow connecting to a mongodb with a user and password.

I use the environment variables DATASOURCE_HOST, DATASOURCE_USER and DATASOURCE_PASSWORD but they are ignored for the connection to mongodb (only used for mysql)

The code would have to be adapted to take into account DATASOURCE_USER and DATASOURCE_PASSWORD also for the mongodb connection url. Or maybe use DATASOURCE_CONNECTION_DETAILS if defined as an environment variable.

THANKS

CveXplore/main.py Outdated Show resolved Hide resolved
@P-T-I
Copy link
Member

P-T-I commented Jun 17, 2024

@gdespres Thanks!

@P-T-I P-T-I merged commit fec39f7 into cve-search:master Jun 17, 2024
3 checks passed
@oh2fih
Copy link
Collaborator

oh2fih commented Jun 28, 2024

@P-T-I Testing CveXplore 0.3.33... Could it be that this change breaks it for anyone not using the environment variables?

  File "/usr/lib/python3/dist-packages/CveXplore/main.py", line 173, in __init__
    self.datasource = DatabaseConnection(
  File "/usr/lib/python3/dist-packages/CveXplore/database/connection/database_connection.py", line 20, in __init__
    self._database_connection = self._database_connnections[self.database_type](
  File "/usr/lib/python3/dist-packages/CveXplore/database/connection/mongodb/mongo_db.py", line 42, in __init__
    collections = self.dbclient.list_collection_names()
  File "/usr/lib/python3/dist-packages/pymongo/database.py", line 863, in list_collection_names
    for result in self.list_collections(session=session, **kwargs)]
  File "/usr/lib/python3/dist-packages/pymongo/database.py", line 825, in list_collections
    return self.__client._retryable_read(
  File "/usr/lib/python3/dist-packages/pymongo/mongo_client.py", line 1464, in _retryable_read
    with self._slaveok_for_server(read_pref, server, session,
  File "/usr/lib/python3.10/contextlib.py", line 135, in __enter__
    return next(self.gen)
  File "/usr/lib/python3/dist-packages/pymongo/mongo_client.py", line 1309, in _slaveok_for_server
    with self._get_socket(server, session, exhaust=exhaust) as sock_info:
  File "/usr/lib/python3.10/contextlib.py", line 135, in __enter__
    return next(self.gen)
  File "/usr/lib/python3/dist-packages/pymongo/mongo_client.py", line 1246, in _get_socket
    with server.get_socket(
  File "/usr/lib/python3.10/contextlib.py", line 135, in __enter__
    return next(self.gen)
  File "/usr/lib/python3/dist-packages/pymongo/pool.py", line 1225, in get_socket
    sock_info = self._get_socket(all_credentials)
  File "/usr/lib/python3/dist-packages/pymongo/pool.py", line 1279, in _get_socket
    sock_info.check_auth(all_credentials)
  File "/usr/lib/python3/dist-packages/pymongo/pool.py", line 793, in check_auth
    self.authenticate(credentials)
  File "/usr/lib/python3/dist-packages/pymongo/pool.py", line 810, in authenticate
    auth.authenticate(credentials, self)
  File "/usr/lib/python3/dist-packages/pymongo/auth.py", line 673, in authenticate
    auth_func(credentials, sock_info)
  File "/usr/lib/python3/dist-packages/pymongo/auth.py", line 591, in _authenticate_default
    return _authenticate_scram(credentials, sock_info, 'SCRAM-SHA-1')
  File "/usr/lib/python3/dist-packages/pymongo/auth.py", line 295, in _authenticate_scram
    res = sock_info.command(source, cmd)
  File "/usr/lib/python3/dist-packages/pymongo/pool.py", line 683, in command
    return command(self, dbname, spec, slave_ok,
  File "/usr/lib/python3/dist-packages/pymongo/network.py", line 159, in command
    helpers._check_command_response(
  File "/usr/lib/python3/dist-packages/pymongo/helpers.py", line 167, in _check_command_response
    raise OperationFailure(msg % errmsg, code, response,
pymongo.errors.OperationFailure: Authentication failed., full error: {'ok': 0.0, 'errmsg': 'Authentication failed.', 'code': 18, 'codeName': 'AuthenticationFailed'}

@oh2fih
Copy link
Collaborator

oh2fih commented Jun 29, 2024

@P-T-I: It was exactly that! Those variables are never None, as they have defaults in common/config.py:

    DATASOURCE_USER = os.getenv("DATASOURCE_USER", "cvexplore")
    DATASOURCE_PASSWORD = os.getenv("DATASOURCE_PASSWORD", "cvexplore")

That gives by default: {'host': 'mongodb://cvexplore:[email protected]:27017'}.

However, setting environment variables explicitly to DATASOURCE_USER=None & DATASOURCE_PASSWORD=None treats the None as a string instead of Python datatype None: {'host': 'mongodb://None:[email protected]:27017'}.

Therefore, these should probably default to None.

oh2fih added a commit to oh2fih/CveXplore that referenced this pull request Jun 29, 2024
@P-T-I
Copy link
Member

P-T-I commented Jun 29, 2024

@oh2fih Probably need another release for this! Will do this asap!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants