Secrets Management in Apache Airflow
Secure by Design ππ‘οΈβ
The Story: The Password That Took Down Productionβ
Everything worked perfectly β until someone pushed a DAG to Git.
Inside the code:
password = "SuperSecret123"
That one line caused:
- Credential leaks
- Repo rotation
- Emergency access revocation
- Production downtime
Airflow didnβt fail.
Secrets management did.
In this article, youβll learn how Airflow handles secrets, where they should live, and how to manage them securely in real-world production systems.
What Are Secrets in Airflow?β
In Airflow, secrets include anything sensitive, such as:
- Database passwords
- API keys
- Cloud credentials
- Tokens
- Certificates
They are commonly used by:
- Connections
- Variables
- Hooks
- Operators
π Rule #1:
Secrets must never live in DAG code.
How Airflow Uses Secrets Internallyβ
Airflow retrieves secrets at runtime, not parse time.
Task starts
β Airflow requests secret
β Secret backend responds
β Task uses secret
β Secret never written to logs or DB
π This design allows secure rotation without code changes.
Secret Storage Options in Airflowβ
Airflow supports multiple secret backends, in this priority order:
- Environment Variables
- Secrets Backend (Vault, AWS, GCP, Azure)
- Metadata Database (fallback)
1οΈβ£ Environment Variables (Simple but Limited)β
How It Worksβ
Secrets are injected as environment variables.
export AIRFLOW_CONN_POSTGRES_DB=postgresql://user:pass@host:5432/db
Airflow automatically detects:
AIRFLOW_CONN_*AIRFLOW_VAR_*
Example: Using an Environment Variable Connectionβ
Inputβ
AIRFLOW_CONN_MY_API=http://user:token@api.example.com
DAG Codeβ
from airflow.hooks.base import BaseHook
conn = BaseHook.get_connection("my_api")
print(conn.host)
Outputβ
api.example.com
Pros & Consβ
β
Easy to set up
β
Good for local/dev
β Hard to rotate
β Not scalable
β Visible to system users
π Use only for development or small setups.