Logo

dev-resources.site

for different kinds of informations.

Hashicorp Vault Agent Tutorial: Generating .env from Vault Secrets

Published at
1/2/2025
Categories
vault
dotenv
security
devops
Author
haamid
Categories
4 categories in total
vault
open
dotenv
open
security
open
devops
open
Author
6 person written this
haamid
open
Hashicorp Vault Agent Tutorial: Generating .env from Vault Secrets

In this tutorial, we will set up Vault Agent to generate a .env file with secrets from HashiCorp Vault. We’ll use the AppRole authentication method to securely authenticate and retrieve secrets, then write them to an environment file for use in your application.

You can find the complete configuration files and setup used in this tutorial in the GitHub repository.

⚠️ Important Note: This tutorial uses Vault in development mode (-dev) for simplicity. Development mode is not secure and should only be used for testing and learning purposes. In a production environment:

  • Use a properly initialized and unsealed Vault server.
  • Secure the Vault server with TLS certificates and access control.

Prerequisites

  • Vault server running (in development mode for testing)
  • Vault Agent installed and configured
  • Basic knowledge of Vault and its authentication methods

Step 1: Start Vault Server in Development Mode

We’ll start Vault in development mode for testing purposes. This will make Vault accessible via 127.0.0.1:8200 with a root token.

Run the following command:

vault server -dev -dev-root-token-id=root -dev-tls
Enter fullscreen mode Exit fullscreen mode

Then export VAULT_ADDR and VAULT_CACERT from the output of previous command to give the ability to cli to have access to Vault server:

export VAULT_ADDR='https://127.0.0.1:8200'
export VAULT_CACERT='/tmp/vault-tls900287623/vault-ca.pem'
Enter fullscreen mode Exit fullscreen mode

And create two dummy secrets as username and password of a database that later we want to write them in a env variable:

vault kv put secret/dbinfo user=root pass=test123
Enter fullscreen mode Exit fullscreen mode

Access Vault’s UI by navigating to https://127.0.0.1:8200 and logging in with the root token.

Step 2: Set Up the AppRole Authentication Method

We will use AppRole for authentication. First, enable the AppRole authentication method:

vault auth enable approle
Enter fullscreen mode Exit fullscreen mode

Next, create a new AppRole and attach a policy that allows reading from the secret/data/dbinfo path:

  1. Create the Vault policy (agent-policy.hcl):

    path "secret/data/dbinfo" {
      capabilities = ["read"]
    }
    
  2. Write the policy:

    vault policy write agent-policy agent-policy.hcl
    
  3. Create the AppRole and attach the policy:

    vault write auth/approle/role/vault-agent-role policies="agent-policy"
    
  4. Generate the role_id and secret_id:

    vault read auth/approle/role/vault-agent-role/role-id
    vault write -f auth/approle/role/vault-agent-role/secret-id
    

The role_id and secret_id are required to authenticate via AppRole. Save these values to the files role_id and secret_id.

Step 3: Configure Vault Agent

The Vault Agent configuration file (vault-agent.hcl) will authenticate with Vault using the AppRole and generate a .env file with the secret values. Here’s an example configuration:

# Vault Agent configuration
vault {
  address = "https://127.0.0.1:8200"
  token = "<client_token>"
}

auto_auth {
  method "approle" {
    mount_path = "auth/approle"
    config = {
      role_id_file_path = "./role_id"
      secret_id_file_path = "./secret_id"
    }
  }

  sink "file" {
    path = "./vault-agent-token"
  }
}

template {
  source      = "./env-template.tmpl"
  destination = "./.env"
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • The role_id_file_path and secret_id_file_path point to the files containing the AppRole credentials.
  • The template block specifies the path to the env-template.tmpl file and the destination for the generated .env file.

Step 4: Create the Template File (env-template.tmpl)

Create a template file (env-template.tmpl) that Vault Agent will use to generate the .env file. Here’s an example template:

DB_USER={{ with secret "secret/data/dbinfo" }}{{ .Data.data.user }}{{ end }}
DB_PASS={{ with secret "secret/data/dbinfo" }}{{ .Data.data.pass }}{{ end }}
Enter fullscreen mode Exit fullscreen mode

This template will insert the user and pass from the Vault secrets into the .env file.

Step 5: Start Vault Agent

Now, we will start the Vault Agent to authenticate and generate the .env file:

vault agent -config=./vault-agent.hcl
Enter fullscreen mode Exit fullscreen mode

Vault Agent will authenticate using AppRole, retrieve the secret from secret/data/dbinfo, and generate the .env file at the specified location.

Step 6: Check the Generated .env File

After Vault Agent runs, the .env file will be populated with the secrets from Vault:

DB_USER=root
DB_PASS=test123
Enter fullscreen mode Exit fullscreen mode

This file can now be used to set environment variables for your application.

Your final files should be like this:

agent-policy.hcl
env-template.tmpl
role_id
secret_id
vault-agent.hcl
vault-agent.pid
vault-agent-token
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this tutorial, we’ve configured Vault Agent to authenticate with Vault using AppRole, retrieve secrets, and generate a .env file. This is a simple and secure way to manage sensitive configuration data in your application.

I hope this tutorial helps you get Vault Agent running smoothly. Happy coding! 🚀

dotenv Article's
30 articles in total
Favicon
Load Environment Variables using dotenv-local
Favicon
Hashicorp Vault Agent Tutorial: Generating .env from Vault Secrets
Favicon
Flutter Web | Build with .env File
Favicon
Learn .env in Express.js for Beginners (Effortless Setup)
Favicon
How to Hide Only API Keys Instead of Entire Files on GitHub and From Its Commit History
Favicon
Practical Introduction to Environment Variables Using Node.js
Favicon
From dotenv to dotenvx: Next Generation Config Management
Favicon
How to use `.env` file v:20.6.0 `dotenv` npm package do not use.
Favicon
Community Spotlight: David Cochrum
Favicon
Node.js 20.6.0 includes built-in support for .env files
Favicon
What is a .env.vault file
Favicon
Environment variables and configuration anti patterns in Node.js applications
Favicon
Dotenv: Python app environment variable vs. Linux environment variable
Favicon
Node.js includes built-in support for .env files
Favicon
How does python-dotenv simplify Configuration Management?
Favicon
Env::Dot
Favicon
How do you set up .env variables in your NextJS project ?
Favicon
Using ENV file in React & Webpack
Favicon
A simple trick for your dotenv files
Favicon
dotenv and typescript
Favicon
Environment variables & Its best practices
Favicon
Password Manage your environment and secrets with bitwarden
Favicon
5 reasons why your .env environment variables don't work
Favicon
Creating a DotEnv Loader in PHP
Favicon
NextJS - Get rid of DotENV
Favicon
Setting-up a Django project for production
Favicon
Stop using Dotenv in your front-end
Favicon
Supercharge your .env powered projects!
Favicon
Ways to load env variables for your script
Favicon
Doppler: The Triumph and Tragedy of .env Files

Featured ones: