Apache Airflow variables and connections often contain sensitive information about your external systems that should be kept secret in a secure, centralized location that complies with your organization’s security requirements. While secret values of Airflow variables and connections are encrypted in the Airflow metadata database of every Deployment, Astronomer recommends integrating with a secrets backend tool.
Integrating a secrets backend tool with Astronomer Software allows you to:
Astronomer Software integrates with the following secrets backend tools:
Secrets backend integrations are configured individually with each Astronomer Software Deployment.
If you enable a secrets backend on Astronomer Software, you can continue to define Airflow variables and connections either as environment variables or in the Airflow UI as needed. If you define variables and connections in the Airflow UI, they are stored as encrypted values in the Airflow metadata database.
Airflow checks for the value of an Airflow variable or connection in the following order:
In this section, you’ll learn how to use Hashicorp Vault as a secrets backend for both local development and on Astronomer Software. To do this, you will:
astro dev init.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:
To use Vault as a secrets backend, Astronomer recommends configuring a Vault AppRole with a policy that grants only the minimum necessary permissions for Astronomer Software. 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 and secret-id for your AppRole by running the following commands:
Save these values for Step 3.
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:
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 Astronomer Software, 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 Astronomer Software in Step 4, you can set these values as secrets in the Software UI.
By default, Airflow uses "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.
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.
Once you’ve confirmed that the integration with Vault works locally, you can complete a similar set up with a Deployment on Astronomer Software.
Dockerfile to your Deployment environment variables. Specify AIRFLOW__SECRETS__BACKEND_KWARGS as secret to ensure that your Vault credentials are stored securely.Dockerfile.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 Astronomer Software.