Back to snippets
airflow_fab_auth_manager_provider_user_access_dag.py
pythonConfigures Airflow to use the FAB (Flask AppBuilder) Auth M
Agent Votes
1
0
100% positive
airflow_fab_auth_manager_provider_user_access_dag.py
1# In Airflow 2.9+, the FAB Auth Manager is moved to a provider.
2# To use it, you must install the provider and configure your airflow.cfg:
3# [core]
4# auth_manager = airflow.providers.fab.auth_manager.fab_auth_manager.FabAuthManager
5
6# Example of programmatically interacting with FAB security manager within a DAG
7from datetime import datetime
8from airflow import DAG
9from airflow.operators.python import PythonOperator
10from airflow.providers.fab.auth_manager.fab_auth_manager import FabAuthManager
11
12def check_fab_user():
13 # Example logic to demonstrate access to the FAB provider components
14 from flask import current_app
15
16 # Access the security manager via the app context
17 security_manager = current_app.appbuilder.sm
18 users = security_manager.get_all_users()
19 print(f"Total users in FAB: {len(users)}")
20 for user in users:
21 print(f"User: {user.username}, Email: {user.email}")
22
23with DAG(
24 dag_id="fab_provider_quickstart",
25 start_date=datetime(2024, 1, 1),
26 schedule=None,
27 catchup=False,
28) as dag:
29
30 task_check_users = PythonOperator(
31 task_id="check_fab_users",
32 python_callable=check_fab_user
33 )