0

Python Version: 3.6

Flask Version: 1.1.1

Pymongo Version: 3.8.0

Flask-PyMongo version: 2.3.0

I'm receiving an unauthenticated message when I attempt any operation in Flask and it appears flask_pymongo is not authenticating.

Error message: pymongo.errors.OperationFailure: Authentication failed.

I'm storing the URI in a .env file that is used by both Flask and when I try and connect via the Pymongo MongoClient. I've tried authenticating against both the admin and application db's and I've created the db user in both:

> use items_db
switched to db items_db
> db.getUsers()
[
    {
        "_id" : "items_db.items_db",
        "user" : "items_user",
        "db" : "items_db",
        "roles" : [
            {
                "role" : "readWrite",
                "db" : "items_db"
            }
        ]
    }
]


My .env file URI is:

MONGO_URI=mongodb://items_user:PASS@127.0.0.1:27017/items_db

I've also changed the auth source in the URI to this:

MONGO_URI=mongodb://items_user:PASS@127.0.0.1:27017/items_db?authSource=admin

It still works okay using MongoClient but not in flask_pymongo.

I tried authenicating just using the Pymongo Client with exactly the same URI without any problems. I also have created the db user in both the admin and application db's.

In my app/__init__.py file:

from app.extensions import mongo

def create_app(config_class=Config):

    app = Flask(__name__)
    # set app configs
    app.config.from_object(config_class)

    # initial flask extensions
    mongo.init_app(app)


    return app

In my app/main/views.py

# app/main/views.py
from app import mongo

def create_item(public_id, request):

    app.logger.info(mongo.db)
    uri = app.config['MONGO_URI']
    client = MongoClient(uri)
    app.logger.info(client)

Log Files:

[2019-07-14 14:22:43,425] INFO in views: Database(MongoClient(host=['127.0.0.1:27017'], document_class=dict, tz_aware=False, connect=False), 'items_db')
[2019-07-14 14:22:43,429] INFO in views: MongoClient(host=['127.0.0.1:27017'], document_class=dict, tz_aware=False, connect=True)

As you can see the connect keyword shows True vs False. I'm using the application factory pattern. Would this cause this issue? I don't receive any out of context errors or similar.

I've also tried some of the other suggested solutions but none of them describe the exact problem I'm having.

Any help much appreciated.

clg123
  • 175
  • 4
  • 11
  • For anybody else in the same position I've found out what the problem was. For some reason flask_pymongo really didn't like my password. It contained some characters that weren't acceptable even though mongo itself had zero problems with them. – clg123 Jul 14 '19 at 15:48
  • Hi @clg123 -- I'm the author of Flask-PyMongo. Could you log at bug at https://github.com/dcrosta/flask-pymongo/issues, with as much detail as you can about what you think has gone wrong? I hope it's obvious, but please don't paste your actual password there; but if you can identify certain characters that are causing problems, that would be really helpful! – dcrosta Jul 14 '19 at 17:18
  • @dcrosta - Will do. – clg123 Jul 14 '19 at 18:52

0 Answers0