|
MongoDB includes basic authentication functionality. By default, authentication is off. When off, it is then required that one runs the database in a trusted (network restricted) environment.
Please also see the replica set authentication documentation. Running Without Security (Trusted Environment)Trusted environment is the default option and is recommended. It is often valid to run the database in a trusted environment with no in-database security and authentication (much like how one would use, say, memcached). Of course, in such a configuration, one must be sure only trusted machines can access database TCP ports. Firewall RulesGiven the default ports here are the basic firewall rules needed.
Mongo SecurityThe current version of Mongo supports only basic security. You authenticate a username and password in the context of a particular database. Once authenticated, a normal user has full read and write access to the database. You can also create read-only users that only have read access. The admin database is special. Several administrative commands can only run on the admin database (so can only be run by an admin user). Also, authentication on admin gives a user read and write access to all databases on the server.
To enable security, run the database (mongod process) with the --auth option (or --keyFile for replica sets and sharding). You must either have added a user to the admin db before starting the server with authentication, or add the first user from the localhost interface (you cannot add the first user from a connection that is not local with respect to mongod). Configuring Authentication and SecurityWe should first create an administrator user for the entire db server process. This user is stored under the special admin database. If there are no admin users, one may access the database from the localhost interface without authenticating. Thus, from the server running the database (and thus on localhost), run the database shell and configure an administrative user: $ ./mongo > use admin > db.addUser("theadmin", "anadminpassword") We now have a user created for database admin. Note that if we have not previously authenticated, we now must if we wish to perform further operations, as there is now an admin user. > db.auth("theadmin", "anadminpassword") Now, let's configure a "regular" user for another database. > use projectx > db.addUser("joe", "passwordForJoe") Finally, let's add a readonly user. (only supported in 1.3.2+) > use projectx > db.addUser("guest", "passwordForGuest", true) Viewing UsersUser information is stored in each database's system.users collection. For example, on a database projectx, projectx.system.users will contain user information. We can view existing users for the current database with the query: > db.system.users.find() Changing PasswordsThe shell addUser command may also be used to update a password: if the user already exists, the password simply updates. Some Mongo drivers provide a helper function equivalent to the db shell's addUser method. Deleting UsersTo delete a user: db.removeUser( username ) or db.system.users.remove( { user: username } )
Replica Set and Sharding Authentication
Replica sets and sharding can use authentication in v1.9.1+ (replica sets without sharding in v1.8). From the client's perspective, it is identical to using single-server authentication: connect to your mongos, create users, authenticate when you make connections. The only difference is that the servers use a key file to authenticate internal communication. A key file is basically a plaintext file which is hashed and used as an internal password. To set up replica sets and/or sharding with authentication:
You do not need to use the --auth option, too (although there's no harm in doing so), --keyFile implies --auth. --auth does not imply --keyFile. Sharded SecurityWhen you enable authentication on the sharded cluster you will be securing connections via the mongos instances and not on individual resources directly like with a single server or a replica set. This is partly because the security information is stored in the sharding config database and not directly on the shards for the admin users. It is also important in a sharded cluster to always have clients connect through a mongos instance and not directly to any resource like the shard servers or config server. If you do add individual users on sharded resources, the individual shard members, then those users are separate from the sharding "admin" users. These two users sources are different between the sharded cluster and the shards for admin users; care should be taken not to confuse the two or to think that they are same set of users. Sharded Database UsersUsers are stored on the primary shard so authentication can be done there. This may lead to a bit of confusion when you have sharded collections because it may not be obvious that the there is no "system.users" collection on the non-primary shards. Enabling/disabling authentication on an existing clusterTo enable authentication on an existing cluster, shut down all members and restart them with the --keyFile option. Remember that you must add users before you can access the data remotely. You can disable authentication by restarting all members without the --keyFile option. About the Key FileA key file must contain at least 6 Base64 characters and be no larger than 1KB (whitespace included). Whitespace characters are stripped (mostly for cross-platform convenience), so the following keys are identical to the database: $ echo -e "my secret key" > key1 $ echo -e "my secret key\n" > key2 $ echo -e "my secret key" > key3 $ echo -e "my\r\nsecret\r\nkey\r\n" > key4 If you run mongod with -v, the key will be printed in the log. Key file permissionsOn *NIX, group and everyone must have 0 permissions (up to 700 is allowed). If the permissions are too open on the key file, MongoDB will exit with an error to that effect. At the moment, permissions are not checked by mongod on Windows. PortsDefault TCP port numbers for MongoDB processes are as follows:
You can change the port number using the 'port' option when starting mongod.
IP Address BindingBy default, a mongod server will listen on all available IP addresses on a machine. You can restrict this to a single IP address with the 'bind_ip' configuration option for mongod. Typically, this would be set to 127.0.0.1, the loopback interface, to require that mongod only listen to requests from the same machine (localhost). To enable listening on all interfaces, remove the bind_ip option from your server configuration file.
Report a Security IssueIf you have discovered any potential security issues in MongoDB, please email security@10gen.com with any and all relevant information. FAQ
|

PLEASE POST QUESTIONS IN THE USER GROUPS FORUM. Post non-question comments and helpful hints here.
blog comments powered by Disqus