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.
2οΈβ£ HashiCorp Vault (Enterprise-Grade)β
Why Vault?β
Vault provides:
- Dynamic secrets
- Automatic rotation
- Fine-grained access control
- Audit logs
π Best for large organizations.
Vault Integration Overviewβ
[secrets]
backend = airflow.providers.hashicorp.secrets.vault.VaultBackend
backend_kwargs = {
"connections_path": "airflow/connections",
"variables_path": "airflow/variables",
"url": "http://vault:8200",
"token": "VAULT_TOKEN"
}
Example Vault Secretβ
Input (Stored in Vault)β
{
"conn_type": "postgres",
"host": "db.prod",
"login": "airflow",
"password": "vault-generated-pass",
"schema": "analytics"
}
Output in Airflowβ
- Used dynamically
- Never stored in metadata DB
π Vault secrets can rotate without restarting Airflow.
3οΈβ£ AWS Secrets Manager (Cloud-Native Favorite)β
Why AWS Secrets Manager?β
- Automatic rotation
- IAM-based access
- Native AWS integration
- High availability
π Best for AWS-based Airflow.
Configuration Exampleβ
[secrets]
backend = airflow.providers.amazon.aws.secrets.secrets_manager.SecretsManagerBackend
backend_kwargs = {
"connections_prefix": "airflow/connections",
"variables_prefix": "airflow/variables"
}
Example Secret in AWSβ
Input (Stored in AWS)β
{
"username": "etl_user",
"password": "rotated-password",
"host": "rds.amazonaws.com",
"port": 5432
}
Outputβ
- Automatically resolved by Airflow
- IAM controls access
- Rotation supported
Secrets Priority Order (Important)β
Airflow resolves secrets in this order:
- Environment Variables
- Secrets Backend
- Metadata DB
π This allows secure overrides without code changes.
Secrets vs Variables vs Connectionsβ
| Item | Purpose | Store Secrets? |
|---|---|---|
| Variables | Config values | β Avoid |
| Connections | Credentials | β Yes |
| Secrets Backend | Secure storage | β Best |
π Never store secrets in Variables unless encrypted and unavoidable.
Input & Output Example (Secure Pipeline)β
Inputβ
- Database password stored in Vault
- DAG uses PostgresHook
from airflow.providers.postgres.hooks.postgres import PostgresHook
hook = PostgresHook(postgres_conn_id="analytics_db")
hook.get_conn()
Outputβ
- Secure connection
- No credentials in code
- No secrets in logs
Common Secrets Management Mistakesβ
β Hardcoding secrets in DAGs
β Storing passwords in Variables
β Committing .env files
β Logging credentials accidentally
β No rotation strategy
Secrets Management Best Practicesβ
β
Use a secrets backend in production
β
Apply least-privilege access
β
Rotate secrets regularly
β
Mask sensitive logs
β
Separate secrets per environment
β
Audit access to secrets
Security Checklist (Production)β
- Secrets backend enabled
- No secrets in DAG code
- IAM roles / policies enforced
- Rotation enabled
- Logs sanitized
Summary π§ β
- Secrets are critical production assets
- Airflow supports multiple secure backends
- Environment variables are entry-level only
- Vault and AWS Secrets Manager are production-ready
- Proper secrets management prevents outages and breaches
Key Takeawaysβ
- β Never hardcode secrets
- β Use secrets backends for scale
- β Rotate credentials without redeployments
- β Protect logs and configs
- β Treat secrets as infrastructure, not code
Whatβs Next? πβ
Monitoring DAGs β SLAs, Alerts, Email, Webserver, Metrics