Skip to content

MongoDB user authorization - how-to #1168

@mohierf

Description

@mohierf

Expected Behavior

Configuring MONGO_* settings with user authorization should not raise an error if the database does not exist.

First step, configure mongodb

I set-up a recent mongodb instance locally and I create an administrator user:

use admin
db.createUser(
  {
    user: "alignak",
    pwd: "alignak",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

I configure mongo in authorization mode and restart the server:

   sudo vi /etc/mongod.conf
      security:
         authorization: enabled

   # Restart mongo
   sudo systemctl restart mongod.service

As of now, I always need to authenticate for any operation on the databases

$ mongo
   > show dbs
   > # Raises an Unauthorized error!
   > db.auth("alignak", "alignak" )
   1
   > show dbs
   admin   0.000GB
   config  0.000GB
   local   0.000GB

Second step, configure my REST Eve backend:

My configuration file MONGO part

  "MONGO_URI": "mongodb://alignak:alignak@localhost:27017/admin",
  "MONGO_HOST": "localhost",
  "MONGO_PORT": 27017,
  "MONGO_DBNAME": "alignak",
  "MONGO_USERNAME": "alignak",
  "MONGO_PASSWORD": "alignak",

Note: it is a JSON file that is transformed to a python dict...
Also note: that I checked that the same configuration without any authorization is fully functional.

Actual Behavior

Tell us what happens instead.

MongoDB connection string: mongodb://alignak:alignak@localhost:27017/admin
Traceback (most recent call last):
  File "/usr/local/bin/alignak-backend", line 9, in <module>
    load_entry_point('alignak-backend==1.4.11', 'console_scripts', 'alignak-backend')()
  File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 542, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2569, in load_entry_point
    return ep.load()
  File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2229, in load
    return self.resolve()
  File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2235, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "/usr/local/lib/python2.7/dist-packages/alignak_backend/main.py", line 8, in <module>
    from alignak_backend.app import app, manifest
  File "/usr/local/lib/python2.7/dist-packages/alignak_backend/app.py", line 2158, in <module>
    static_folder=base_path
  File "/usr/local/lib/python2.7/dist-packages/eve/flaskapp.py", line 176, in __init__
    self.register_resource(resource, settings)
  File "/usr/local/lib/python2.7/dist-packages/eve/flaskapp.py", line 922, in register_resource
    create_index(self, resource, name, list_of_keys, index_options)
  File "/usr/local/lib/python2.7/dist-packages/eve/io/mongo/mongo.py", line 969, in create_index
    db = app.data.pymongo(resource, px).db
  File "/usr/local/lib/python2.7/dist-packages/eve/io/mongo/mongo.py", line 893, in pymongo
    self.driver[px] = PyMongo(self.app, px)
  File "/usr/local/lib/python2.7/dist-packages/flask_pymongo/__init__.py", line 97, in __init__
    self.init_app(app, config_prefix)
  File "/usr/local/lib/python2.7/dist-packages/flask_pymongo/__init__.py", line 283, in init_app
    mechanism=auth_mechanism)
  File "/usr/local/lib/python2.7/dist-packages/pymongo/database.py", line 1274, in authenticate
    connect=True)
  File "/usr/local/lib/python2.7/dist-packages/pymongo/mongo_client.py", line 614, in _cache_credentials
    sock_info.authenticate(credentials)
  File "/usr/local/lib/python2.7/dist-packages/pymongo/pool.py", line 688, in authenticate
    auth.authenticate(credentials, self)
  File "/usr/local/lib/python2.7/dist-packages/pymongo/auth.py", line 542, in authenticate
    auth_func(credentials, sock_info)
  File "/usr/local/lib/python2.7/dist-packages/pymongo/auth.py", line 245, in _authenticate_scram
    res = sock_info.command(source, cmd)
  File "/usr/local/lib/python2.7/dist-packages/pymongo/pool.py", line 579, in command
    unacknowledged=unacknowledged)
  File "/usr/local/lib/python2.7/dist-packages/pymongo/network.py", line 150, in command
    parse_write_concern_error=parse_write_concern_error)
  File "/usr/local/lib/python2.7/dist-packages/pymongo/helpers.py", line 155, in _check_command_response
    raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: Authentication failed.

It looks like Eve do not consider the authorization parameters I provided to create the database!

I tried almost every parameters combination, with or whitout MONGO_URI. The only solution I found to start correctly is to remove mongo authorization and then restore once my database is created .with a user that is authorized to use this db ...

Perharps I did something not correctly ... but what I expect is that Eve manages the DB creation by itself 😉

Environment

  • Python version: 2.7, 3.5
  • Eve version: 0.7.9

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions