Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does class MongoDBBackend accept a password parameter? #54

Closed
oshihirii opened this issue Nov 11, 2013 · 7 comments
Closed

Does class MongoDBBackend accept a password parameter? #54

oshihirii opened this issue Nov 11, 2013 · 7 comments

Comments

@oshihirii
Copy link

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!)

@FedericoCeratto
Copy link
Owner

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/

@oshihirii
Copy link
Author

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.

@FedericoCeratto
Copy link
Owner

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.

@oshihirii
Copy link
Author

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

@oshihirii
Copy link
Author

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

@oshihirii oshihirii reopened this Nov 12, 2013
@FedericoCeratto
Copy link
Owner

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

@oshihirii
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants