Configure a Hashicorp Vault secrets backend on Astro Private Cloud
In this section, you’ll learn how to use Hashicorp Vault as a secrets backend for both local development and on Astro Private Cloud. To do this, you will:
- Create an AppRole in Vault which grants Astronomer minimal required permissions.
- Write a test Airflow variable or connection as a secret to your Vault server.
- Configure your Astro project to pull the secret from Vault.
- Test the backend in a local environment.
- Deploy your changes to Astro Private Cloud.
Prerequisites
- A Deployment on Astronomer.
- The Astro CLI.
- A Hashicorp Vault server.
- An Astro project initialized with
astro dev init
. - The Vault CLI.
- Your Vault Server’s URL. If you’re using a local server, this should be
http://127.0.0.1:8200/
.
If you do not already have a Vault server deployed but would like to test this feature, Astronomer recommends that you either:
- Sign up for a Vault trial on Hashicorp Cloud Platform (HCP) or
- Deploy a local Vault server. See Starting the server in Hashicorp documentation.
Step 1: Create a Policy and AppRole in Vault
To use Vault as a secrets backend, Astronomer recommends configuring a Vault AppRole with a policy that grants only the minimum necessary permissions for Astro Private Cloud. To do this:
-
Create a Vault policy with the following permissions:
-
Create a Vault AppRole and attach the policy you just created to it.
-
Retrieve the
role-id
andsecret-id
for your AppRole by running the following commands:Save these values for Step 3.
Step 2: Write an Airflow variable or connection to Vault
To test whether your Vault server is set up properly, create a test Airflow variable or connection to store as a secret.
To store an Airflow variable in Vault as a secret, run the following Vault CLI command with your own values:
To store a connection in Vault as a secret, run the following Vault CLI command with your own values:
To confirm that your secret was written to Vault successfully, run:
Step 3: Set up Vault locally
In your Astro project, add the Hashicorp Airflow provider to your project by adding the following to your requirements.txt
file:
Then, add the following environment variables to your Dockerfile
:
This tells Airflow to look for variable and connection information at the secret/variables/*
and secret/connections/*
paths in your Vault server. In the next step, you’ll test this configuration in a local Airflow environment.
If you want to deploy your project to a hosted Git repository before deploying to Astro Private Cloud, be sure to save <your-approle-id>
and <your-approle-secret>
securely. Astronomer recommends adding them to your project’s .env
file and specifying this file in .gitignore
.
When you deploy to Astro Private Cloud in Step 4, you can set these values as secrets in the UI.
"kv_engine_version": 2
, but this secret was written using v1. You can change this to accommodate how you write and read your secrets.For more information on the Airflow provider for Hashicorp Vault and how to further customize your integration, see the Apache Airflow documentation.
Step 4: Run an example Dag to test Vault locally
To test Vault, write a simple dag which calls your test secret and add this dag to your project’s dags
directory. For example, you can use the following dag to print the value of a variable to your task logs:
Once you’ve added this dag to your project:
-
Run
astro dev restart
to push your changes to your local Airflow environment. -
In the Airflow UI (
http://localhost:8080/admin/
), trigger your new dag. -
Click on
test-task
> View Logs. If you ran the example dag above, you should see the contents of your secret in the task logs:
Once you confirm that the setup was successful, you can delete this example dag.
Step 5: Deploy on Astro Private Cloud
Once you’ve confirmed that the integration with Vault works locally, you can complete a similar set up with a Deployment on Astro Private Cloud.
- In the Astro Private Cloud UI, add the same environment variables found in your
Dockerfile
to your Deployment environment variables. SpecifyAIRFLOW__SECRETS__BACKEND_KWARGS
as secret to ensure that your Vault credentials are stored securely. - In your Astro project, delete the environment variables from your
Dockerfile
. - Deploy your changes to Astro Private Cloud.
Now, any Airflow variable or connection that you write to your Vault server can be successfully accessed and pulled by any dag in your Deployment on Astro Private Cloud.