FedericoCeratto/bottle-cork

Does class MongoDBBackend accept a password parameter?

Closed this issue · 7 comments

In OpenShift, the standard way to login to MongoDB is with environment variables:

dbname = 'mydbname'
connection = pymongo.MongoClient(os.environ['OPENSHIFT_MONGODB_DB_URL'])
db = connection[dbname]

The environment variable OPENSHIFT_MONGODB_DB_URL consists of:

OPENSHIFT_MONGODB_DB_URL=mongodb://admin:password-here@127.x.xx.xxx:27017/

You can also access these environment variables which are self explanatory:

OPENSHIFT_MONGODB_DB_USERNAME
OPENSHIFT_MONGODB_DB_PASSWORD
OPENSHIFT_MONGODB_DB_HOST
OPENSHIFT_MONGODB_DB_PORT

But in any case, in OpenShift, you have to provide a password to access MongoDB.

I am editing mongodb_backend.py so that it can connect to the database at line 138:

class MongoDBBackend(Backend):
    def __init__(self, db_name='cork', hostname='localhost', port=27017, initialize=False):
    """Initialize MongoDB Backend"""
    connection = MongoClient(host=hostname, port=port)
    db = connection[db_name]

How can I add a password parameter to this class?

(I am a bit of a newb with this, and trying to document my process as I go on the wiki so simple information appreciated if possible - thanks!)

See: http://api.mongodb.org/python/current/api/pymongo/mongo_client.html
"The host parameter can be a full mongodb URI, in addition to a simple hostname."
And: http://docs.mongodb.org/manual/reference/connection-string/

Could I therefore use:

class MongoDBBackend(Backend):
    def __init__(self, db_name='cork', hostname='os.environ[\'OPENSHIFT_MONGODB_DB_URL\']', initialize=False):
    """Initialize MongoDB Backend"""
    connection = MongoClient(hostname)
    db = connection[db_name]

Two things I am confused about:

  • Is the syntax correct for hostname value?
  • Would each user therefore be accessing a connection where they were logged in with the MongoDB admin username and password (as that is all in the OPENSHIFT_MONGODB_DB_URL environment variable - see first post)? And if so is that a problem?

Sorry for asking so many questions, but I seem to be coming up against a few issues when implementing. Nevertheless, I hope they may be helpful for others implementing on OpenShift.

No need to modify mongodb_backend.py - in your own code you can do:

dbname = 'mydbname'
hostname = os.environ['OPENSHIFT_MONGODB_DB_URL']
port = os.environ['OPENSHIFT_MONGODB_DB_PORT']
port = int(port)
connection = pymongo.MongoClient(hostname=hostname, port=port)
db = connection[dbname]

"connection" would be used by any user within the same application. It should have limited access rights, e.g. update the collection that belong to your webapp and nothing more.

Thank you for the clarification, that makes sense to me.

I ran my application from shell and got error:

  File "mybottleapp.py", line 47, in populate_mongodb_backend
    connection = pymongo.MongoClient(hostname=hostname)
NameError: global name 'hostname' is not defined

Looking at the class definition of MongoDBBackend in mongodb_backend.py, 'hostname' is defined as a parameter.

This is the code in my application:

def populate_mongodb_backend():
    mb = MongoDBBackend(db_name='cork-example', initialize=True,
            hostname = os.environ['OPENSHIFT_MONGODB_DB_URL'],
            connection = pymongo.MongoClient(hostname=hostname)
            )

Note the value of OPENSHIFT_MONGODB_DB_URL is:

mongodb://admin:password-here@127.x.xx.xxx:27017/

Update:

I realised host is the MongoClient parameter name, not hostname, so I tried:

def populate_mongodb_backend():
    mb = MongoDBBackend(db_name='cork-example', initialize=True,
            hostname = os.environ['OPENSHIFT_MONGODB_DB_URL'],
            connection = pymongo.MongoClient(host=hostname)
            )

And get the same error:

    connection = pymongo.MongoClient(host=hostname)
NameError: global name 'hostname' is not defined

That's because the "hostname" variable is not defined. Use:
hostname = os.environ['OPENSHIFT_MONGODB_DB_URL']

working code:

def populate_mongodb_backend():
    hostname = os.environ['OPENSHIFT_MONGODB_DB_URL']
    connection = pymongo.MongoClient(host=hostname)
    mb = MongoDBBackend(db_name='cork-example', initialize=True,hostname = hostname)
    .... # the rest is the same