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.
Go to the Secrets engine tab, click Enable new engine + , then select Databases to enable a new database engine.
Configure Vault with the proper plugin and connection information by clicking into the database engine and selecting Connect a database.
Type in the necessary information for the connection.
Go back, select the Roles Tab and click on Create role.
Type in the necessary information for the role.
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:
Key Value
--- -----
Seal Type shamir
Initialized true
Sealed false
Total Shares 1
Threshold 1
Version 1.13.1
Build Date 2023-03-23T12:51:35Z
Storage Type inmem
Cluster Name vault-cluster-141bcf10
Cluster ID df9845ca-a0c1-d2b0-3eb7-45a845adfaee
HA Enabled false