log4mongo / log4mongo-python

python logging handler for mongo database
http://log4mongo.org
Other
111 stars 37 forks source link

Not authorized on admin [ Mongo version : 3.4.4 ] #27

Open sreecodeslayer opened 7 years ago

sreecodeslayer commented 7 years ago

Issue :

File "/usr/local/lib/python2.7/dist-packages/log4mongo/handlers.py", line 123, in __init__
    self._connect(**kwargs)
  File "/usr/local/lib/python2.7/dist-packages/log4mongo/handlers.py", line 145, in _connect
    self.connection.is_locked
  File "/home/sreenadh/.local/lib/python2.7/site-packages/pymongo/mongo_client.py", line 1186, in is_locked
    ops = self._database_default_options('admin').current_op()
  File "/home/sreenadh/.local/lib/python2.7/site-packages/pymongo/database.py", line 620, in current_op
    return sock_info.command("admin", cmd)
  File "/home/sreenadh/.local/lib/python2.7/site-packages/pymongo/pool.py", line 239, in command
    read_concern)
  File "/home/sreenadh/.local/lib/python2.7/site-packages/pymongo/network.py", line 102, in command
    helpers._check_command_response(response_doc, None, allowable_errors)
  File "/home/sreenadh/.local/lib/python2.7/site-packages/pymongo/helpers.py", line 205, in _check_command_response
    raise OperationFailure(msg % errmsg, code, response)
OperationFailure: not authorized on admin to execute command { currentOp: 1, $all: false }

What am trying to do with log4mongo :

I am writing a scrapy spider in which I need the log to be sent to MongoDB. My other mongo connections using MongoClient auth uri works fine. (ie.: client=MongoClient('mongodb://username:password@localhost:27045') ) with auth db as admin that has roles granted accordingly.

I followed the README, but I can't get the Auth part to work, when the MongoDB is with auth disabled, the connection works.

handler = MongoHandler(
level=logging.DEBUG,
host='localhost',
port=27045,
database_name='SPIDER_LOGS',
collection='logs',
username='username_here',
password='password_here',
authentication_db='admin'
)

Versions used:

python : 2.7
mongodb : 3.4.4
pymongo : 3.4.0
char0n commented 7 years ago

@oz123 any idea ? I don't see any reason why this should not work.

sreecodeslayer commented 7 years ago

Hey, I got it to work

When I checked the Pymongo doc, it says we can pass uri in to host.

The host parameter can be a full mongodb URI, in addition to a simple hostname. It can also be a list of hostnames or URIs.

So instead of giving username, password separately, i gave it like this,

handler = MongoHandler(host='mongodb://username:password@localhost:27045')

and everything remains same, and I can confirm I have log data in the collection, so i guess it is better to mention this in the README for others who is using Auth :slightly_smiling_face: :+1:

char0n commented 7 years ago

Will do. Thanks.

I will close this issue after README is updated.

char0n commented 7 years ago

Still pending..

lamter commented 7 years ago

It happended to me and I guess I found the reason. It happens in MongoHandler._connect meth. For a database need auth, connect.is_locked should be call after auth.But, it adverses. It will be normal at no-auth situation, but not at auth need.