On this page
Enforce Keyfile Access Control in a Replica Set
On this page
Overview
Enforcing access control on an existing replica set requires configuring:
- Security between members of the replica set using Internal Authentication, and
- Security between connecting clients and the replica set using User Access Controls.
For this tutorial, each member of the replica set uses the same internal authentication mechanism and settings.
Enforcing internal authentication also enforces user access control. To connect to the replica set, clients like the mongo
shell need to use a user account. See Users.
Cloud Manager and Ops Manager
If Cloud Manager or Ops Manager is managing your deployment, see the Cloud Manager manual or the Ops Manager manual for enforcing access control.
Considerations
IP Binding
Changed in version 3.6.
Starting with MongoDB 3.6, MongoDB binaries, mongod
and mongos
, bind to localhost
by default. From MongoDB versions 2.6 to 3.4, only the binaries from the official MongoDB RPM (Red Hat, CentOS, Fedora Linux, and derivatives) and DEB (Debian, Ubuntu, and derivatives) packages would bind to localhost
by default. To learn more about this change, see Localhost Binding Compatibility Changes.
Operating System
This tutorial uses the mongod
programs. Windows users should use the mongod.exe
program instead.
Keyfile Security
Keyfiles are bare-minimum forms of security and are best suited for testing or development environments. For production environments we recommend using x.509 certificates.
Users
This tutorial covers creating the minimum number of administrative users on the admin
database only. For the user authentication, the tutorial uses the default SCRAM authentication mechanism. Challenge-response security mechanisms are best suited for testing or development environments. For production environments, we recommend using x.509 certificates or LDAP Proxy Authentication (available for MongoDB Enterprise only) or Kerberos Authentication (available for MongoDB Enterprise only).
For details on creating users for specific authentication mechanism, refer to the specific authentication mechanism pages.
See ➤ Configure Role-Based Access Control for best practices for user creation and management.
Downtime
The following procedure for enforcing access control requires downtime. For a procedure that does not require downtime, see Enforce Keyfile Access Control in a Replica Set without Downtime instead.
Enforce Keyfile Access Control on Existing Replica Set
Create a keyfile.
With keyfile authentication, each mongod
instances in the replica set uses the contents of the keyfile as the shared password for authenticating other members in the deployment. Only mongod
instances with the correct keyfile can join the replica set.
The content of the keyfile must be between 6 and 1024 characters long and must be the same for all members of the replica set.
Note
On UNIX systems, the keyfile must not have group or world permissions. On Windows systems, keyfile permissions are not checked.
You can generate a keyfile using any method you choose. For example, the following operation uses openssl
to generate a complex pseudo-random 1024 character string to use for a keyfile. It then uses chmod
to change file permissions to provide read permissions for the file owner only:
openssl rand -base64 756 > <path-to-keyfile>
chmod 400 <path-to-keyfile>
See Keyfiles for additional details and requirements for using keyfiles.
Copy the keyfile to each replica set member.
Copy the keyfile to each server hosting the replica set members. Ensure that the user running the mongod
instances is the owner of the file and can access the keyfile.
Avoid storing the keyfile on storage mediums that can be easily disconnected from the hardware hosting the mongod
instances, such as a USB drive or a network attached storage device.
Shut down all members of the replica set.
Shut down each mongod
in the replica set, starting with the secondaries. Continue until all members of the replica set are offline, including any arbiters. The primary must be the last member shut down to avoid potential rollbacks.
To shut down a mongod
, connect each mongod
using a mongo
shell and issue the db.shutdownServer()
on the admin
database:
use admin
db.shutdownServer()
At the end of this step, all members of the replica set should be offline.
Restart each member of the replica set with access control enforced.
Restart each mongod
in the replica set with either the security.keyFile
configuration file setting or the --keyFile
command-line option. Running mongod
with the --keyFile
command-line option or the security.keyFile
configuration file setting enforces both Internal Authentication and Role-Based Access Control.
Configuration File
If using a configuration file, set
security.keyFile
to the keyfile’s path, andreplication.replSetName
to the replica set name.
Include additional options as required for your configuration. For instance, if you wish remote clients to connect to your deployment or your deployment members are run on different hosts, specify the net.bindIp
setting. For more information, see Localhost Binding Compatibility Changes.
security:
keyFile: <path-to-keyfile>
replication:
replSetName: <replicaSetName>
net:
bindIp: localhost,<ip address>
Start the mongod
using the configuration file:
mongod --config <path-to-config-file>
For more information on the configuration file, see configuration options.
Command Line
If using the command line options, start the mongod
with the following options:
--keyFile
set to the keyfile’s path, and--replSet
set to the replica set name.
Include additional options as required for your configuration. For instance, if you wish remote clients to connect to your deployment or your deployment members are run on different hosts, specify the --bind_ip
. For more information, see Localhost Binding Compatibility Changes.
mongod --keyFile <path-to-keyfile> --replSet <replicaSetName> --bind_ip localhost,<ip address of the mongod host>
For more information on command-line options, see the mongod
reference page.
Connect to the primary using the localhost interface.
Connect a mongo
shell to one of the mongod
instances over the localhost interface. You must run the mongo
shell on the same physical machine as the mongod
instance.
Use rs.status()
to identify the primary replica set member. If you are connected to the primary, continue to the next step. If not, connect a mongo
shell to the primary over the localhost interface.
Important
You must connect to the primary before proceeding.
Create the user administrator.
Important
After you create the first user, the localhost exception is no longer available.
The first user must have privileges to create other users, such as a user with the userAdminAnyDatabase
. This ensures that you can create additional users after the Localhost Exception closes.
If at least one user does not have privileges to create users, once the localhost exception closes you may be unable to create or modify users with new privileges, and therefore unable to access necessary operations.