🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
NEWSLIVESearch All Content
đŸ’ŧ Servicesâ„šī¸ Aboutâœ‰ī¸ ContactView Pricing Plansfrom $10

Security Best Practices in Apache Airflow

đŸŸĸ Free Lesson

Security Best Practices

Security LayersAuthenticationOAuth, LDAP, SAMLRBACRole-based accessEncryptionTLS, AES-256NetworkFirewalls, VPNSecrets BackendVault, AWS SMSecrets PriorityVault {'>'} AWS SM {'>'} GCP SM {'>'} Env Vars {'>'} DBComplianceGDPR, SOC2, HIPAA controlsAudit LoggingActivity trackingDefense in depth: multiple security layers protect against different threat vectors

Architecture Diagram

Formal Definitions

Detailed Explanation

RBAC Configuration

Role-Based Access Control restricts user permissions.


Default Roles:

RolePermissions
AdminFull access to all resources
OpCreate/edit DAGs, manage connections/variables
UserRead-only access to DAGs and history
ViewerRead-only access to DAGs

Custom Roles:

python
ROLES = {
    'DataEngineer': [
        (permissions.ACTION_CAN_READ, permissions.RESOURCE_DAG),
        (permissions.ACTION_CAN_EDIT, permissions.RESOURCE_DAG),
    ],
    'DataAnalyst': [
        (permissions.ACTION_CAN_READ, permissions.RESOURCE_DAG),
    ],
}

OAuth Integration: Google, GitHub, LDAP, and custom providers supported.


Secrets Backend Setup

Store credentials in external secret management systems.


Supported Backends:

BackendProviderConfiguration
VaultHashiCorpbackend = airflow.providers.hashicorp.secrets.vault.VaultBackend
AWS SMAWSbackend = airflow.providers.amazon.aws.secrets.secrets_manager.SecretsManagerBackend
GCP SMGoogle Cloudbackend = airflow.providers.google.cloud.secrets.secret_manager.CloudSecretManagerBackend
python
# airflow.cfg
[secrets]
backend = airflow.providers.hashicorp.secrets.vault.VaultBackend
backend_kwargs = {
    "connections_path": "airflow/connections",
    "variables_path": "airflow/variables",
}

Note: Hooks resolve connections through the secrets backend automatically, so no DAG change is required when credentials move out of the metadata database.


Connection Encryption

Encrypt connections in transit and at rest.


Security Checklist:

PracticeImplementation
SSL/TLSsslmode: require in connection extras
FernetEnable AIRFLOW__CORE__FERNET_KEY for metadata encryption
HTTPSUse HTTPS for web server
NetworkDeploy in private VPC/subnet
python
# Encrypted connection example
conn = Connection(
    conn_id='production_db',
    conn_type='postgres',
    host='db.example.com',
    extra='{"sslmode": "require", "sslrootcert": "/path/to/ca.pem"}',
)

# Additional SSL parameters for strict verification
conn_params = {
    'sslmode': 'require',  # Enforce SSL
    'sslcert': '/path/to/client-cert.pem',
    'sslkey': '/path/to/client-key.pem',
    'sslrootcert': '/path/to/ca-cert.pem',
}

def verify_encryption():
    """Verify connection is encrypted."""
    hook = PostgresHook(postgres_conn_id='encrypted_db')

    result = hook.get_first("""
        SELECT
            pgssl.ssl_is_used as ssl_enabled,
            pgssl.ssl_version as ssl_version,
            pgssl.ssl_cipher as cipher
        FROM pg_stat_ssl pgssl
        JOIN pg_stat_activity pgact ON pgssl.pid = pgact.pid
        WHERE pgact.usename = current_user
    """)

    return {
        'ssl_enabled': result[0],
        'ssl_version': result[1],
        'cipher': result[2],
    }

Threat Model: What a Compromise Buys

Airflow is a high-value target precisely because it is trusted everywhere at once. Its metadata database holds connection passwords and variable values, its workers execute arbitrary Python on your infrastructure, and its task logs routinely contain rows of real business data. Rank attacker positions by leverage rather than by how hard they are to reach. Write access to the DAG folder is equivalent to code execution, because the DAG processor imports every file on a timer, so planting a module there runs it inside your cluster without anyone touching the webserver. Editing a saved connection is nearly as powerful: pointing a connection at an attacker-controlled host redirects every task that uses it, and no alarm fires because nothing about the configuration is invalid. An Admin session exposes every variable, every connection, the rendered configuration page and the full history of task logs. A Viewer session is narrower but still reads DAG source and log output, which often includes query results, file paths and stack traces with environment details. Database-level read access defeats Fernet as soon as the key is readable from a process environment, which it usually is.

That ranking sets the order of work. First, restrict who can write DAG files and who can hold the Admin role, since those two permissions are the ones that convert a small foothold into arbitrary execution or credential theft. Second, keep the metadata database and the Fernet key in separate trust boundaries, so an attacker who reaches one still needs the other. Third, treat log content as data with the same classification as the tables it was selected from. Network segmentation, TLS and rotation schedules reduce exposure, but they only matter once those three foundations are right.

Hardening the Webserver and the Role Model

Most Airflow exposures begin with a webserver that was never finished being configured. webserver_config.py is the Flask-AppBuilder configuration file, and one setting decides whether authentication exists at all: AUTH_ROLE_PUBLIC, the role granted to unauthenticated visitors. Its default assigns no permissions, while setting it to Viewer publishes every DAG definition and task log to whoever can reach the port — convenient for a local demo and a reportable disclosure in production. The companion setting expose_config must stay False, because the Admin configuration page renders every value from airflow.cfg, including sql_alchemy_conn with its embedded password and the fernet_key itself.

Beneath authentication sits the role model. Admin, Op, User and Viewer ship with every installation, PUBLIC describes anonymous traffic, and custom roles are built from permission pairs of action plus resource, such as reading a DAG or editing a connection. Grant the narrowest role that lets a person finish their job, extend the model with your own roles instead of promoting people to Admin, and use per-DAG access control so one team's pipelines stay invisible to another's. Finally, do not run on the bootstrap Admin account created during installation: issue named accounts behind your identity provider with OAuth, LDAP or SAML, so that an employee leaving the company becomes a directory change rather than an Airflow cleanup task.

Key Concepts Table

Security LayerComponentImplementationPriority
AuthenticationWebserverOAuth, LDAP, SAMLP0
AuthorizationRBACRole-based permissionsP0
EncryptionDatabaseTLS, AES-256P0
SecretsBackendVault, AWS SMP0
NetworkInfrastructureFirewalls, VPNP1
AuditLoggingActivity logsP1
CompliancePoliciesSOC2, GDPRP2

Code Examples

Security Audit Script

python
# security_audit.py
from airflow import settings
from airflow.models import Connection, Variable
from sqlalchemy import text
import json

def audit_security_posture():
    """Comprehensive security audit of Airflow deployment."""
    session = settings.Session()
    findings = []
    
    # Check for plaintext passwords in metadata DB
    plaintext_conns = session.query(Connection).filter(
        Connection.conn_type.notin_(['aws', 'google_cloud_platform'])
    ).all()
    
    for conn in plaintext_conns:
        if conn.password and not conn.password.startswith('{'):
            findings.append({
                'severity': 'HIGH',
                'category': 'Secrets',
                'finding': f'Plaintext password in connection: {conn.conn_id}',
                'recommendation': 'Move to secrets backend',
            })
    
    # Check for variables with sensitive data
    sensitive_patterns = ['password', 'secret', 'key', 'token']
    variables = session.query(Variable).all()
    
    for var in variables:
        if any(pattern in var.key.lower() for pattern in sensitive_patterns):
            findings.append({
                'severity': 'MEDIUM',
                'category': 'Secrets',
                'finding': f'Potentially sensitive variable: {var.key}',
                'recommendation': 'Move to secrets backend',
            })
    
    # Check for DAGs with hardcoded secrets
    import ast
    import os
    
    dags_folder = '/opt/airflow/dags'
    for root, dirs, files in os.walk(dags_folder):
        for file in files:
            if file.endswith('.py'):
                filepath = os.path.join(root, file)
                with open(filepath, 'r') as f:
                    content = f.read()
                
                # Simple pattern matching
                for pattern in ['password=', 'secret=', 'api_key=']:
                    if pattern in content:
                        findings.append({
                            'severity': 'HIGH',
                            'category': 'Code',
                            'finding': f'Potential hardcoded secret in {filepath}',
                            'recommendation': 'Use variables or secrets backend',
                        })
    
    return findings

def generate_security_report(findings):
    """Generate security audit report."""
    report = {
        'total_findings': len(findings),
        'high_severity': len([f for f in findings if f['severity'] == 'HIGH']),
        'medium_severity': len([f for f in findings if f['severity'] == 'MEDIUM']),
        'low_severity': len([f for f in findings if f['severity'] == 'LOW']),
        'findings': findings,
    }
    
    print(json.dumps(report, indent=2))
    return report

if __name__ == "__main__":
    findings = audit_security_posture()
    generate_security_report(findings)

Network Security Configuration

# docker-compose-security.yml
version: '3.8'

services:
  airflow-webserver:
    image: apache/airflow:2.8.0
    command: webserver
    networks:
      - airflow-internal
    environment:
      - AIRFLOW__CORE__FERNET_KEY=${FERNET_KEY}
      - AIRFLOW__WEBSERVER__EXPOSE_CONFIG=False
    deploy:
      resources:
        limits:
          cpus: '1'
          memory: 2G
    # Security context
    security_opt:
      - no-new-privileges:true
    read_only: true
    tmpfs:
      - /tmp

  airflow-scheduler:
    image: apache/airflow:2.8.0
    command: scheduler
    networks:
      - airflow-internal
    environment:
      - AIRFLOW__CORE__FERNET_KEY=${FERNET_KEY}
    security_opt:
      - no-new-privileges:true

  postgres:
    image: postgres:15
    networks:
      - airflow-internal
    environment:
      POSTGRES_PASSWORD_FILE: /run/secrets/db_password
    secrets:
      - db_password
    volumes:
      - postgres_data:/var/lib/postgresql/data
    # Disable network access except from Airflow
    # Use internal network only

  vault:
    image: hashicorp/vault:1.15
    networks:
      - airflow-internal
    cap_add:
      - IPC_LOCK
    environment:
      VAULT_ADDR: 'http://0.0.0.0:8200'
    volumes:
      - vault_data:/vault/file
    command: server -dev

networks:
  airflow-internal:
    driver: bridge
    internal: true  # No external access

secrets:
  db_password:
    file: ./secrets/db_password.txt

volumes:
  postgres_data:
  vault_data:

Performance Metrics

Security Posture Score

MetricScoreWeightStatus
Secrets in Backend100%30%PASS
TLS Enabled100%25%PASS
RBAC Configured80%20%PASS
Audit Logging90%15%PASS
Network Segmentation70%10%WARNING

Compliance Checklist

RequirementStatusEvidenceOwner
SOC2 - Access ControlsPASSRBAC configuredSecurity
SOC2 - EncryptionPASSTLS 1.3 enabledPlatform
GDPR - Data ProtectionPASSEncryption at restPlatform
GDPR - Audit TrailPASSLogging enabledSecurity
HIPAA - PHI HandlingN/ANo PHI processed-

See Also

CW

ChatWhole Learn Editorial Team

Written by experienced data scientists, software engineers, and educators. All content is reviewed by industry professionals to ensure accuracy and relevance.

Last reviewed: 2026-10-17

—
☆☆☆☆☆
0 ratings

Rate & Feedback

Need Expert Airflow Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement