Python, Pymongo: Create a User for a DataBase with read only provilege

82 views
Skip to first unread message

Vincent Konaté

unread,
Mar 1, 2018, 8:14:25 PM3/1/18
to mongodb-user
I am trying to create a user for a database , that would have read-only access,

I tried the following

from pymongo import MongoClient
mongo=MongoClient()
mongo.test.command("createUser", "testing", pwd="testing", roles=["read"])

Then I authenticate

mongo.test.authenticate('testing', 'testing')

However if I try to insert a document, it actually does insert:

mongo.test.test.insert_one({'name':'vincent'})

The command mongo.test.command("usersInfo") returns

{'ok': 1.0,
 'users': [{'_id': 'test.testing',
   'db': 'test',
   'roles': [{'db': 'test', 'role': 'read'}],
   'user': 'testing'}]}

Which seems to indicate that the user has been created properly. What should I do in order to create and authenticate with a read-only user that would see his insert requests rejected?

Bernie Hackett

unread,
Mar 2, 2018, 12:27:07 PM3/2/18
to mongodb-user
Since your createUser command succeeded without you first authenticating as a user with permissions to create users, are you sure you have authentication enabled for the server?

Vincent Konaté

unread,
Mar 2, 2018, 4:58:46 PM3/2/18
to mongodb-user
OK thx - Someone gave me the solaution on stack overflow: https://stackoverflow.com/questions/49060924/python-pymongo-create-a-user-for-a-database-with-read-only-privilege

The reason why i am switching to authenticated is so that i can test the db safely: I need to be able to connect in read-only in test mode.
Reply all
Reply to author
Forward
0 new messages