Database root credential rotation
Vault's database secrets engine provides a centralized workflow for managing credentials for various database systems. By leveraging this, every service instance gets a unique set of database credentials instead of sharing one. Having those credentials tied directly to each service instance and live only for the life of the service, any abnormal access pattern can be mapped to a specific service instance and its credential can be revoked immediately.
This reduces the manual tasks performed by the database administrator and makes the database access more efficient and secure.
In the dynamic secrets tutorial, you configured Vault to generate dynamic credentials for a PostgreSQL database. In this tutorial, you will learn how to rotate credentials used by Vault, known as the root user.
If you are not familar with how to configure Vault for dynamic credentials, follow the database secrets engine tutorial before you begin.
Scenario
Because Vault is managing the database credentials on behalf of the database administrator, it must also be given a set of highly privileged credentials which can grant and revoke access to the database system.
HashiCups needs to ensure the credentials used by Vault are secure, and do not violate the governance, risk and compliance (GRC) policies surrounding that data stored in the database.
Oliver with the operations team will use Vault's /database/rotate-root/:name
API endpoint to rotate the root credentials stored for the database connection.
Best Practice
Use this feature to rotate the root credentials immediately after the initial configuration of each database.
Prerequisites
This lab was tested on macOS using an x86_64 based and Apple silicon-based processors. You may also run this tutorial by clicking the Start interactive lab button.
To perform the tasks described in this tutorial, you need to have:
- Docker to run a Vault and PostgreSQL container.
- Vault binary installed.
- Git installed.
- PostgreSQL CLI (
psql
) client installed.
Policy requirements
Note
For the purpose of this tutorial, you can use your root
token to
work with Vault. However, it is recommended that root tokens are only used for
just enough initial setup or in emergencies. As a best practice, use tokens
with appropriate set of policies based on your role in the organization.
To perform all tasks demonstrated in this tutorial, your policy must include the following permissions:
If you are not familiar with policies, complete the policies tutorial.
Set up the lab
Clone the
learn-vault-dynamic-credentials
repository.Change into the
learn-vault-dynamic-credentials
directory.Deploy the Vault and PostgreSQL containers.
Example output:
Copy the export command from the Terraform output and export the environment variables.
Example:
Verify the PostgreSQL and Vault containers have started.
Vault and PostgreSQL are running. Vault connects to PostgreSQL over the Docker bridge network.
Apply the PostgreSQL configuration used in the dynamic secrets tutorial.
Example output:
Apply the Vault configuration used in the dynamic secrets tutorial.
Example output:
Vault and PostgreSQL are running and configured. You are ready to proceed with the tutorial.
Rotate the root credentials
Vault allows you to rotate the initial root database credentials used when configuring the database secrets engine.
Tip
Once you rotate the root credential, only the Vault knows the new root password. This is the same for all root database credentials given to Vault. A good practice is to create a separate superuser dedicated to Vault which is not used for other purposes.
Rotate the root credentials for the PostgreSQL database connection.
Verify that the root credential was rotated by attempting to log into PostgreSQL and enter the password
rootpassword
.Entering the initial password (
rootpassword
) fails since you rotated the password with therotate-root
command.Example output:
You can invoke the
database/rotate-root/:name
endpoint periodically to secure the root credential.
Verify the configuration
Request credentials from the readonly
role to verify that the database secrets engine dynamically
generates credentials as expected.
Read credentials from the
readonly
database role.The command outputs the results in JSON and stores the output in the
CREDS
variable.Verify you can connect to the database using the Vault generated credentials.
Enter the password generated by Vault when prompted.
Example output:
Verify that you can run commands with the PostgreSQL role provided.
Exit the PostgreSQL prompt.
You successfully rotated the root user credentials used by Vault and used the Vault generated credentials to successfully connected to your PostgreSQL server.
Clean up
Delete the Terraform state files for the PostgreSQL configuration.
Note
This step is only required because the root user password was rotated during the tutorial. In a production environment, you would use a dedicated superuser for Vault, and a dedicated user for Terraform.
Destroy the remaining Terraform resources.
Unset the environment variables.
Summary
In this tutorial, you learned how to manage the privileged root user credentials
used by Vault to generate dynamic credentials.
After rotating the root credentials, you requested credentials from Vault by
reading the readonly
to verify that the database secrets engine dynamically
generates credentials as expected.