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

Getting error when trying to access from Swagger #179

Closed
prasanta303 opened this issue Sep 22, 2023 · 11 comments
Closed

Getting error when trying to access from Swagger #179

prasanta303 opened this issue Sep 22, 2023 · 11 comments

Comments

@prasanta303
Copy link

When I tried to access the swagger url for Database connection (say "/api/v1/database-connections" endpoint) or any other endpoint, it's giving error as,
Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/starlette/middleware/errors.py", line 162, in call
await self.app(scope, receive, _send)
File "/usr/local/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 79, in call
raise exc
File "/usr/local/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 68, in call
await self.app(scope, receive, sender)
File "/usr/local/lib/python3.11/site-packages/fastapi/middleware/asyncexitstack.py", line 20, in call
raise e
File "/usr/local/lib/python3.11/site-packages/fastapi/middleware/asyncexitstack.py", line 17, in call
await self.app(scope, receive, send)
File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 718, in call
await route.handle(scope, receive, send)
File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 276, in handle
await self.app(scope, receive, send)
File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 66, in app
response = await func(request)
^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/fastapi/routing.py", line 241, in app
raw_response = await run_endpoint_function(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/fastapi/routing.py", line 169, in run_endpoint_function
return await run_in_threadpool(dependant.call, **values)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/starlette/concurrency.py", line 41, in run_in_threadpool
return await anyio.to_thread.run_sync(func, *args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/anyio/to_thread.py", line 33, in run_sync
return await get_async_backend().run_sync_in_worker_thread(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 2106, in run_sync_in_worker_thread
return await future
^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 833, in run
result = context.run(func, *args)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/dataherald/server/fastapi/init.py", line 167, in list_database_connections
return self._api.list_database_connections()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/dataherald/api/fastapi.py", line 147, in list_database_connections
return db_connection_repository.find_all()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/dataherald/repositories/database_connections.py", line 43, in find_all
rows = self.storage.find_all(DB_COLLECTION)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/dataherald/db/mongo.py", line 48, in find_all
return list(self._data_store[collection].find({}))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pymongo/cursor.py", line 1251, in next
if len(self.__data) or self._refresh():
^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pymongo/cursor.py", line 1168, in _refresh
self.__send_message(q)
File "/usr/local/lib/python3.11/site-packages/pymongo/cursor.py", line 1055, in __send_message
response = client._run_operation(
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pymongo/_csot.py", line 106, in csot_wrapper
return func(self, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pymongo/mongo_client.py", line 1341, in _run_operation
return self._retryable_read(
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pymongo/_csot.py", line 106, in csot_wrapper
return func(self, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pymongo/mongo_client.py", line 1459, in _retryable_read
with self._socket_from_server(read_pref, server, session) as (sock_info, read_pref):
File "/usr/local/lib/python3.11/contextlib.py", line 137, in enter
return next(self.gen)
^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pymongo/mongo_client.py", line 1293, in _socket_from_server
with self._get_socket(server, session) as sock_info:
File "/usr/local/lib/python3.11/contextlib.py", line 137, in enter
return next(self.gen)
^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pymongo/mongo_client.py", line 1228, in _get_socket
with server.get_socket(handler=err_handler) as sock_info:
File "/usr/local/lib/python3.11/contextlib.py", line 137, in enter
return next(self.gen)
^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pymongo/pool.py", line 1522, in get_socket
sock_info = self._get_socket(handler=handler)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pymongo/pool.py", line 1635, in _get_socket
sock_info = self.connect(handler=handler)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pymongo/pool.py", line 1493, in connect
sock_info.authenticate()
File "/usr/local/lib/python3.11/site-packages/pymongo/pool.py", line 987, in authenticate
auth.authenticate(creds, self, reauthenticate=reauthenticate)
File "/usr/local/lib/python3.11/site-packages/pymongo/auth.py", line 617, in authenticate
auth_func(credentials, sock_info)
File "/usr/local/lib/python3.11/site-packages/pymongo/auth.py", line 522, in _authenticate_default
return _authenticate_scram(credentials, sock_info, "SCRAM-SHA-1")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pymongo/auth.py", line 248, in _authenticate_scram
res = sock_info.command(source, cmd)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pymongo/helpers.py", line 279, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pymongo/pool.py", line 879, in command
return command(
^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pymongo/network.py", line 166, in command
helpers._check_command_response(
File "/usr/local/lib/python3.11/site-packages/pymongo/helpers.py", line 194, in _check_command_response
raise OperationFailure(errmsg, code, response, max_wire_version)
pymongo.errors.OperationFailure: Authentication failed., full error: {'ok': 0.0, 'errmsg': 'Authentication failed.', 'code': 18, 'codeName': 'AuthenticationFailed'}

@jcjc712
Copy link
Contributor

jcjc712 commented Sep 22, 2023

Hi, it looks like you are having an issue connecting with your Mongo db.
Do you have your mongo container running?
Are you sure you have these envvars in your .env file

MONGODB_URI = "mongodb://admin:admin@mongodb:27017"
MONGODB_DB_NAME = 'dataherald'

If you try even the GET endpoints you get the same error, right?

@prasanta303
Copy link
Author

prasanta303 commented Sep 23, 2023

Hello, yes, the same settings are in my .env file. Even if I am getting the same error.
Please suggest how to resolve this issue.

@jcjc712
Copy link
Contributor

jcjc712 commented Sep 25, 2023

Yes, it looks like the mongo was created before the .env file so it didn't create the user using these envvars which should be set in the .env file

MONGODB_DB_USERNAME = 'admin'
MONGODB_DB_PASSWORD = 'admin'

Here's my recommendation:

  1. Make sure that the above envvars are in your .env file
  2. Start by executing docker-compose down to remove the containers.
  3. In your project directory, delete the folder named dbdata, which contains the MongoDB data.
  4. Now, run docker-compose up --build.

These steps should result in the creation of the admin user in MongoDB. Please inform me of your results so that I can assist you further if needed.

@khaianis
Copy link

hi
use the same mongo username / mongo password in ure uri :
MONGODB_URI = "mongodb://admin:admin@mongodb:27017"
MONGODB_DB_NAME = 'dataherald'
MONGODB_DB_USERNAME = 'admin'
MONGODB_DB_PASSWORD = 'admin'
B regards

@jcjc712
Copy link
Contributor

jcjc712 commented Sep 27, 2023

Hi, did you try deleting the dbdata folder and building again the container?

@prasanta303
Copy link
Author

prasanta303 commented Sep 28, 2023

Hello, when I try running in local m/c, it is working fine. But while trying to do the same in any linux server it's giving error. I tried as you suggested, but not fixed. I did an temporary workaround by hard-coding values of mongo creds in init-mongo.sh as,

set -e

mongosh <<EOF
use <database name>

db.createUser({
  user: <user in quotes>,
  pwd: <password in quotes>,
  roles: [{
    role: 'readWrite',
    db: <database name in quotes>
  }]
})

EOF

And also hard-coding in docker-compose.yml file for mogodb environment as below,

    environment:
      MONGO_INITDB_ROOT_USERNAME: <user name in double quotes>
      MONGO_INITDB_ROOT_PASSWORD: <password in double quotes>
      MONGO_INITDB_DATABASE: <database name in double quotes>

But, I understand that somehow these values are not picking from .env in linux server.

@jcjc712
Copy link
Contributor

jcjc712 commented Sep 28, 2023

@prasanta303 Thank you for your feedback. We will promptly initiate an internal ticket to address and investigate this matter further.

@prasanta303
Copy link
Author

One more input I want to give that I am using version : "3.3" instead of version : "3.9" in the docker-compose.yml file as 3.9 is not supporting in our RedHat Linux server. Is it the reason due to which the error ?

@jcjc712
Copy link
Contributor

jcjc712 commented Oct 10, 2023

Hi, sorry for my late answer I tried using the 3.3 version for my docker-compose in my local env but it worked fine. I couldn't replicate the issue.

@prasanta303
Copy link
Author

Hello, It is working in my local environment. But when I put this in Red Hat Linux server v9. It is failing.

@jcjc712
Copy link
Contributor

jcjc712 commented Oct 18, 2023

Have you tried the next posible solutions?

SELinux and Security Policies:
RHEL and other Red Hat-based distributions often have SELinux (Security-Enhanced Linux) enabled by default. SELinux can restrict certain behaviors of Docker containers, including their ability to access host resources. You may need to adjust SELinux policies or container labels to grant the necessary permissions.

Restart Docker Service:
Sometimes, issues with Docker not recognizing changes or environment variables can be resolved by restarting the Docker service. Use the following command to restart Docker:

sudo systemctl restart docker

Logging and Troubleshooting:
Review Docker's logs and container logs for any error messages or issues that might help diagnose the problem. The docker logs command can be useful for viewing container-specific logs.

Testing with Minimal Configuration:
To isolate the issue, consider creating a minimal Docker container and testing whether environment variables are correctly recognized within it. This can help determine if the issue is related to the specific container or the host environment.

Restart Docker Compose:
After making changes to the .env file, restart your Docker Compose environment. You can do this with the following command:

docker-compose down
docker-compose up -d

This will stop and remove the containers defined in your Compose file and then start them again with the updated environment variables.

I hope it can help you

@jcjc712 jcjc712 closed this as completed Oct 18, 2023
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

No branches or pull requests

3 participants