amol- / depot

Toolkit for storing files and attachments in web applications
MIT License
161 stars 41 forks source link

Authentication failed #63

Closed mat-l closed 4 years ago

mat-l commented 4 years ago

Dear all, I am having trouble with the authentication with my MongoDB GridFS while using depot.

As mentioned in the main page (https://pypi.org/project/filedepot/) of depot I am running the following commands:

from depot.manager import DepotManager

# Configure a *default* depot to store files on MongoDB GridFS
DepotManager.configure('default', {
    'depot.backend': 'depot.io.gridfs.GridFSStorage',
    'depot.mongouri': 'mongodb://admin:admin@100.x.x.x:27017/test'
})

Out[2]: <depot.io.gridfs.GridFSStorage at 0x24281ead908>

depot = DepotManager.get()
# Save the file and get the fileid
fileid = depot.create(open('C:/Users/xxx/Documents/mongodb/mineral.jpg'))

which results in the output:

Traceback (most recent call last):
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\gridfs\__init__.py", line 130, in put
    grid_file.write(data)
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\gridfs\grid_file.py", line 386, in write
    to_write = read(self.chunk_size)
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 247: character maps to <undefined>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3319, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-4-fe7a01f1c97a>", line 2, in <module>
    fileid = depot.create(open('C:/Users/xxx/Documents/Robot_agents/mongodb/mineral.jpg'))
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\depot\io\gridfs.py", line 81, in create
    last_modified=utils.timestamp())
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\gridfs\__init__.py", line 132, in put
    grid_file.close()
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\gridfs\grid_file.py", line 321, in close
    self.__flush()
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\gridfs\grid_file.py", line 297, in __flush
    self.__flush_buffer()
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\gridfs\grid_file.py", line 289, in __flush_buffer
    self.__flush_data(self._buffer.getvalue())
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\gridfs\grid_file.py", line 267, in __flush_data
    self.__ensure_indexes()
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\gridfs\grid_file.py", line 210, in __ensure_indexes
    self.__create_index(self._coll.files, _F_INDEX, False)
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\gridfs\grid_file.py", line 197, in __create_index
    doc = collection.find_one(projection={"_id": 1}, session=self._session)
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\collection.py", line 1273, in find_one
    for result in cursor.limit(-1):
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\cursor.py", line 1156, in next
    if len(self.__data) or self._refresh():
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\cursor.py", line 1073, in _refresh
    self.__send_message(q)
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\cursor.py", line 955, in __send_message
    address=self.__address)
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\mongo_client.py", line 1347, in _run_operation_with_response
    exhaust=exhaust)
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\mongo_client.py", line 1459, in _retryable_read
    exhaust=exhaust) as (sock_info,
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\contextlib.py", line 112, in __enter__
    return next(self.gen)
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\mongo_client.py", line 1283, in _slaveok_for_server
    with self._get_socket(server, session, exhaust=exhaust) as sock_info:
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\contextlib.py", line 112, in __enter__
    return next(self.gen)
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\mongo_client.py", line 1223, in _get_socket
    self.__all_credentials, checkout=exhaust) as sock_info:
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\contextlib.py", line 112, in __enter__
    return next(self.gen)
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\pool.py", line 1128, in get_socket
    sock_info.check_auth(all_credentials)
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\pool.py", line 712, in check_auth
    auth.authenticate(credentials, self)
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\auth.py", line 564, in authenticate
    auth_func(credentials, sock_info)
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\auth.py", line 539, in _authenticate_default
    return _authenticate_scram(credentials, sock_info, 'SCRAM-SHA-1')
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\auth.py", line 263, in _authenticate_scram
    res = sock_info.command(source, cmd)
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\pool.py", line 613, in command
    user_fields=user_fields)
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\network.py", line 167, in command
    parse_write_concern_error=parse_write_concern_error)
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\helpers.py", line 159, in _check_command_response
    raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: Authentication failed.

I am currently running MongoDB 4.2.8 Community within a Docker container on Ubuntu 18.04.

Can anybody give me a hint how to fix the authentication problem with depot? The mongodb-compass program and Python "pymongo" lib are working with the credentials: mongo_uri="mongodb://admin:admin@100.x.x.x:27017" client = MongoClient(mongo_uri)

Best regards matl

mat-l commented 4 years ago

I noticed that in the command mentioned on top, I have used a non exisiting database called "test". While using the existing database "admin" the command:

# Save the file and get the fileid
fileid = depot.create(open('C:/Users/xxx/Documents/mongodb/mineral.jpg'))

leads to this error message:

Traceback (most recent call last):
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3319, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-5-fe7a01f1c97a>", line 2, in <module>
    fileid = depot.create(open('C:/Users/xxx/Documents/Robot_agents/mongodb/mineral.jpg'))
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\depot\io\gridfs.py", line 81, in create
    last_modified=utils.timestamp())
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\gridfs\__init__.py", line 130, in put
    grid_file.write(data)
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\gridfs\grid_file.py", line 386, in write
    to_write = read(self.chunk_size)
  File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 247: character maps to <undefined>
amol- commented 4 years ago

You are opening the file in text mode on Python3, which leads to trying to decode it as text. Try to replace open('C:/Users/xxx/Documents/mongodb/mineral.jpg') with open('C:/Users/xxx/Documents/mongodb/mineral.jpg', "rb")