Video Transcript (English)
Authorization determines the permissions that a user has on a database.
It’s important to enable authorization so that you can control which data users have access to, and what actions they are allowed to perform on that data.
This helps ensure the security and integrity of your data.
In this video, you’ll learn how to set up authorization for self-managed MongoDB deployments by using Role-Based Access Control.
Specifically, you’ll learn how to assign a built-in role to a database user and remove a built-in role from a user.
Built-in roles are predefined sets of privilege actions on database resources. Built-in roles provide various levels of access that are commonly needed in a database system.
Examples of built-in roles include: Database user roles like read and readWrite, Database administration roles like dbAdmin and dbOwner, All-database roles like readAnyDatabase and userAdminAnyDatabase, and many more.
First, let’s assign a built-in role to a database user.
You can assign roles to users during user creation, or by updating existing users to grant new roles.
Let’s work through an example of assigning a built-in role during user creation by using the `db.createUser` method. While you can also grant additional roles to an existing user by using the `db.grantRolesToUser` method, we won’t be covering it in this video.
We already fulfilled the prerequisites for creating users. We have a MongoDB instance running with access control enabled and a user administrator.
Let’s say that we want to create a user named `analystUser` with a strong password and the built-in role of read on the sample_analytics database.
First, we connect to the instance, authenticating as the user administrator.
We enter the globalUserAdmin password when prompted.
It’s best practice to create all users in the `admin` database, so we switch to the admin database.
This will make `admin` the authentication database for the new user. But, the user can still have privileges in other databases.
Now we use the `db.createUser` method to create the analystUser.
We’re using the `passwordPrompt` user management method.
Lastly, we assign the `read` role on the `sample_analytics` database.
Note that if we wanted to assign this user roles on multiple databases, we could add additional roles to the `roles` array.
For example, if we wanted to add a second role for the analystUser, such as `readWrite`, on the `sample_training` database, we could do so by adding it to the `roles` array like this.
We'll proceed with assigning only one role to analystUser, the `read` role on the `sample_analytics` database.
We run the operation and enter a password for analystUser when prompted.
Now let’s try authenticating to the database by using the analystUser.
First, we quit the current shell session.
We reconnect to the `mongod` to authenticate as analystUser.
We specify `admin` as the authSource in our connection string because we created analystUser on the `admin` database.
We’re connecting directly to the `sample_analytics` database, where analystUser has `read` privileges.
We enter analystUser’s password when prompted.
Now that we’re connected to the `sample_analytics` database, let’s check analystUser’s access permissions.
We run `show collections` to see a list of all collections in the database.
There are three collections in the database: accounts, customers, and transactions.
analystUser should be able to read from any of these collections.
We query for a document in the `accounts` collection.
A document is returned, which confirms that analystUser has read access on the sample_analytics database. analystUser’s permissions are working correctly!
Sometimes you may need to remove a role from a user.
Say that we have a `financeUser` with two roles, and we need to remove one of them.
First we quit the current shell session.
Then, we connect to the MongoDB instance again using `mongosh`, authenticating as the user administrator.
We need to use the `admin` database, because that’s where we’ve created the financeUser.
First, we want to confirm financeUser’s current roles.
We use the `db.getUser` method to retrieve user information about financeUser, including current roles.
`db.getUser()` has one required parameter, the name of the user that you want to return information about.
The output shows that `financeUser` has a `readWrite` role on the `sample_analytics` database, and a `read` role on the `sample_training` database.
We need to remove financeUser’s `read` role on `sample_training`.
We do this by using the `db.revokeRolesFromUser` method, entering the username and role as arguments.
Let’s review the updated information about financeUser’s roles by running `db.getUser` again.
Now the output shows that financeUser has only the `readWrite` role on the `sample_analytics` database.
We’ve successfully removed the `read` role on the `sample_training` database.
In this video, you learned how to set up authorization with Role-Based Access Control for self-managed MongoDB deployments.
First, you learned that there are two prerequisites for creating users. You need to enable access control on your MongoDB deployment, and create a user administrator.
You learned to assign a built-in role during user creation by using the`db.createUser` method.
You also learned to remove a role from a user by using the `db.revokeRolesFromUser` method.