Authenticate After Picking the Database

前端 未结 1 1804
说谎
说谎 2020-11-29 13:08

I have 3 databases in my MongoDB server. I am using pymongo to do some scripting with Python3.

I want to use the latest versions and practices. Once I open the clien

相关标签:
1条回答
  • 2020-11-29 13:27

    You seem to be missing some concepts here so I'll basically answer as a "guide" to what you should be doing instead. So "authentication' is not really something you do "after" connection, but rather you need to be "looking in the right place" when you actually attempt to authenticate.

    We can start this by essentially following the process outlined in Enable Auth from the core documentation, but specifically altered because you want to be running this "test" under your own user account and local directory.

    Revision Steps - Straight from Documentation

    So first would would want to pick a local working directory and make a path for the database storage files underneath that. On *nix based systems you can do something like:

    mkdir -p scratch/data/db
    cd scratch
    

    Then we want to startup a separate MongoDB instance without any other options. Making sure the port does not conflict with any other running instance:

    mongod --port 37017 --dbpath data/db
    

    In a new terminal or command line window, you can then connect to the shell:

    mongo --port 37017
    

    You always want at least one account with administrative privileges to at least "create accounts" and alter them in case you get in trouble, so create one:

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

    Now exit the shell and close the existing mongod instance running in the other terminal or command prompt and then start it again using --auth:

    mongod --auth --port 37017 --dbpath data/db
    

    Specific User - Make sure you follow these

    Now you actually want to create a user that will be "used by your application". So these steps are important to ensure you get it right.

    Log into a shell using your "adminstrative user":

    mongo -u admin -p admin --port 37017 --authenticationDatabase 'admin'
    

    You can alternately do the db.auth() method as shown in the question, but as noted this must be authorised on the "admin" namespace.

    The next thing you want to do is create a user with access to "mydb" as a namespace with the readWrite role. For kicks, we are also going to let this user have the readAnyDatabase allowing them to "list" all databases namespaces, if not actually being able to do anything else with them.

    IMPORTANT: You create ALL your users in the "admin" namespace. And this will be very important in future releases:

    use admin
    db.createUser(
      {
        "user": "myuser",
        "pwd": "password",
        "roles": [
          { "role": "readWrite", "db": "mydb" },
          "readAnyDatabase"
        ]
      }
    )
    

    Just for additional output, let's look at the current created users:

    db.getUsers()
    [
            {
                    "_id" : "admin.admin",
                    "user" : "admin",
                    "db" : "admin",
                    "roles" : [
                            {
                                    "role" : "userAdminAnyDatabase",
                                    "db" : "admin"
                            }
                    ]
            },
            {
                    "_id" : "admin.myuser",
                    "user" : "myuser",
                    "db" : "admin",
                    "roles" : [
                            {
                                    "role" : "readWrite",
                                    "db" : "mydb"
                            },
                            {
                                    "role" : "readAnyDatabase",
                                    "db" : "admin"
                            }
                    ]
            }
    ]
    

    See how these have expanded in naming, and particularly the values assigned to the various "db" keys on each user. This should give you a little more insight into how MongoDB looks this up and why.

    Python Connection

    Finally we just want to connect from python. So presuming you have python and pymongo installed already, then it's just a simple listing to verify:

    import pymongo
    from pymongo import MongoClient
    client = MongoClient('mongodb://myuser:password@localhost:37017');
    
    db = client['mydb']
    col = db.test
    
    col.remove()
    
    col.insert_one({ "a": 1 })
    
    for doc in col.find():
      print(doc)
    

    Which shows the document created and listed without problem:

    {u'a': 1, u'_id': ObjectId('5a08e5e0760108251722a737')}
    

    Note that we don't actually need to make any mention of "admin" here, because this is the default where the driver "expects the accounts to be" and where you really "should" be doing it.

    But I did it the wrong way

    So let's say you originally got all confused and created the user under "mydb" instead:

    use mydb
    db.createUser({ "user": "bert", "pwd": "password", "roles": ["readWrite"] })
    

    If you go look in "admin" that user is not there. But if you look on "mydb":

    use mydb
    db.getUsers()
    [
            {
                    "_id" : "mydb.bert",
                    "user" : "bert",
                    "db" : "mydb",
                    "roles" : [
                            {
                                    "role" : "readWrite",
                                    "db" : "mydb"
                            }
                    ]
            }
    ]
    

    So you can see where the actual user data is now kept and how it has been recorded.

    The simple case here is you "must" tell MongoDB where to obtain the authentication from for this user:

    client = MongoClient('mongodb://bert:password@localhost:37017/mydb');
    

    See how we add "mydb" on to the connection string. This is how it's done.


    This is actually "in progress" to be made consistent with ALL drivers in how connections are made and where authentication happens as well as where you select the database. But there are basic rules:

    1. If no other database namespace is provided with connection details for authentication credentials, then "admin" is taken to be the default.

    2. Where there is a database namespace provided on the connection string, this will be used for authentication and this is the actual intent of the database namespace on the connection string.

    3. Though other drivers "presently" differ in the role of the database namespace on the connection string, the usage is being changed to be consistent with all drivers that "using" a database namespace is in fact an API call, rather than being assigned from the connection string.

    So where you need to authenticate depends on "where you created the user". But you should really be noting that "admin" is the place where you "should" be doing this instead of anywhere else.

    Deprecation of Authenticate after connect

    Whilst all drivers actually do have a similar method to authenticate(), which is used much like the shell example in the question, this method is now considered DEPRECATED as is mentioned throughout the content of the answer it is "intended" that you actually store your users in the "admin" namespace:

    "Changed in version 3.5: Deprecated. Authenticating multiple users conflicts with support for logical sessions in MongoDB 3.6. To authenticate as multiple users, create multiple instances of MongoClient."

    This is why the whole answer here is based on NOT using that method as you are meant to creating new connection instances, or using the "sessions" functionality available from MongoDB 3.6 instead.

    0 讨论(0)
提交回复
热议问题