MongoDB admin password
This tutorial will teach you how to provision MongoDB credentials using Vault.
Setup MongoDB to be ready for Vault
Connect to the MongoDB instance
You can use the MongoDB shell to connect to your instance:
You can see the following warning when you enter their shell. Now you know you haven't provisioned a secret admin password.
Switch to the admin database
Once you are connected to the MongoDB shell, switch to the admin database:
Create the administrative user
Create a new administrative user for Vault by running the following command:
This will create a new administrative user with the username vaultuser
and password your_password_here
. You can replace your_password_here
with a strong password of your choice. The command should respond with Successfully added user ...
.
Exit the MongoDB shell
Once you have created the administrative user, exit the MongoDB shell by running the following command:
Restart MongoDB with authentication enabled
To enable authentication, you need to set auth = true
in /etc/mongodb.conf
Now you can restart MongoDB. The exact steps for doing this will depend on your operating system and how you installed MongoDB. For example, on Linux systems that use systemd, you can use the following command:
Setup Vault Database Secrets Engine for MongoDB
Starting the Dev Server
Start a Vault server in development mode (dev server). The dev server is a built-in, pre-configured server that is not very secure but useful for playing with Vault locally.
Set environment variables
Then execute the following commands:
Verify the Server is Running
Verify the server is running by running the vault status
{{exec}} command. If it runs successfully, the output should look like the following:
If the output looks completely different, restart the dev server and try again.
Congratulations! You've started your first Vault server.
Setup Database Secrets Engine for MongoDB
1. Enable the database secrets engine
2. Configure Vault with the proper plugin and connection information
3. Configure a role that maps a name in Vault to a MongoDB command that executes and creates the database credential
If all of the above steps output with Success! ...
, congratulations! You have now finished the Database Secrets Engine setup for MongoDB.
Verify everything works by login using Vault Secrets
Get credentials
After the secrets engine is configured and a user has a Vault token with the proper permission, it can generate credentials:
Connect to the MongoDB instance with generated credentials
Now you can use the generated credentials in order to perform administrative tasks.
We first get a usable credential from the command we just used above.
Then we can authenticate into the MongoDB database using the credential.
Once you are authenticated, you can perform administrative tasks using the MongoDB shell, like creating a collection in the admin database:
The command should work without any issues (respond with { "ok" : 1 }
).
Verify that we need authentication
Now, let's try to perform administrative tasks without authentication. Try to create a collection again in the admin database.
Since we didn't authenticate ourselves to the database, The command should fail with the following response:
Last updated