So I have been experimenting with mongodb a little these days as I am interesting in writing some content on the subject, aside from the fact that it will typically be the database solution I will run into when working in a node.js environment. In this post I will be writing abut enabling authentication for a database.
This is a post on enabling authentication in mongodb so that a user name and password must be provided in order to do anything with the database. I am covering several examples of mongodb shell scripts, but will not get into that in detail. I am also writing about some more examples of the use of a model with mongoose, but will not be getting into those things in detail as well. The focus of this post is on setting up authentication for a database locally.
Out of the box authentication is disabled when working with mongodb locally, but often when I go to deploy a user name and password is required as part of the mongodb connect string. So this is something that is typically setup before hand on the node that I will be deploying the app, and in that case I just need to know the username and password to connect to mongodb.
So enabling authentication is something that I often will only need to bother with when trying to reproduce a similar environment locally to that of what I am dealing with when deploying.
So far my preferred way of enabling or disabling authentication is by editing the mongod.cfg file. As you would expect this is a configuration file for mongodb that is stored in several different locations depending on the version of mongodb, the os, or the setup. In any case I would want to know where this file is, and make sure I have the authority to edit it if I want to get this working, or disable it for that matter.
The mongo.conf file that I am using in a windows environment is located in the bin folder of the programFiles folder for mongodb.
Anyway it currently looks like this:
This is a yaml formated file, and the particular value of interest is the authorization property under security. This can have two possible string values ‘enabled’ or ‘disabled’. In order to edit this file I need the proper access permissions, and once I make a change I will want to restart the mongod service that uses this file. In windows I would want to use services administrative tool to stop, and then restart the service.
To help show how to set things up with this I made a project complete with mongodb shell scripts, and a simple User model, along with scripts that connect to and use that model.
If you want to follow along locally you can clone down this demo, and do an npm install.
The mongo_shell folder is a folder that I have started placing in projects that involve mongodb. These are shell scripts, that are not intended to be used from any kind of client system, unless I am willing to take the chance to implement some kind of admin user account. In any case as far as I am concerned with the content of this post, these are scripts that must be called from the command line, either locally, or by remote when dealing with a deployment.
In this project there are just a few scripts that I have put together that have to do with setting up a user and password for a database, these scripts must be used when authentication is disabled, and the values are hard coded into them. However as far as this post is concerned they should do the job just fine.
To use one of these scripts authentication must be disabled, and the script must be called with mongo, or from within the mongo shell with the load command.
Here is the shell script for creating a user. I start by making an instance of the Mongo Constructor, then get the database I want to create a user for, in this case it is a database called ‘mongoose_users’. Once I have the database I just need to get the user to find out if the user is there before hand as it will return null if the user is not there, and give me the user object if it is. In the event that the user is not there, the user is created with a hard coded name, and password.
In a more professional script these values will not be hard coded, but this still is a huge improvement from having to hand code this into the mongo shell each time I need to do this, no way am I ever doing that.
This mongo shell script can be used to list the users in the database.
If I want to drop the user in order to start over I can do so with this.
This is the file that I use to connect to mongodb.
This is just a simple user model that is used for this simple example project.
This is the script that I call to create an instance of the User model that is defined in user.js.
This script will list all the instances of the User model in the database, this is not to be confused with the users that are defined that have access to the database.
I have made this script for the purpose of dropping the whole database if I want to in order to quickly start over.
The conf.json file that I have at the root of the project is used to provide login credentials to scripts that use the connect.js file. What I have in the conf.json file will be passed as an options object to connect.js, and will result in a connection string that uses the credentials defined here.
Here I am using login credentials
This will result in a connection string that does not have login credentials
So now I should cover how to use this project to point out some basic things about setting up authentication in mongodb.
So the main conf.json file in the root of the demo is where I am storing options that will be used in my connect.js file to make the connection to mongodb. This can include a few values, but for the purpose of this post the two key values are of course username, and password.
So If I have a conf.json file like this:
And I have not created a user for the ‘mongoose_users’ database, then authentication will fail even if I do not have authtaction enabled yet in the mongod.cfg file.
However this can easily be fixed by setting the values for username, and password to null.
At which point it will work just fine if authentication is disabled in the mongod service.
So if I want to password protect this database I will need to set up a user for this database, and I will also want to enable authentication in mongod.cfg.
So to set up the user that I defined in my users_add.js mongo shell script, I just need to go to my mongo_shell script folder, and call that script with mongo.
This will create the user ‘dustin’ with the password ‘1234’ that I hard coded in the js file users_add.js, This could be treated differently, but yo get the idea. I set the user up with the role of ‘dbOwner’ this is a mongodb hard coded role that gives be full control over the database, even droping the whole thing if I want.
So now with the conf.json file set like this
Authntacton does not fail, becuase the user is now there.
Event though I have an user set up, I can still just use the database without a password. Thats because authentication will only fail if I give incorrect credentials, or if I drop the user like before. However if I just do not give anything I can still just use it.
This is because I have not actually enabled authentication in the mongod.cfg file that I mentioned above.
So to enable authentication I need to find the mongod.cfg file that the mongod service is using and just make sure the follwing is in there.
This will of course enable authentication when I restart the service. after restarting mongod, I will run into an authentication failure when trying to do something with the database when no credentials are given as expected.
So now I just need to use the proper credentials when connecting.
And of course I can use it agin juts fine then.