Role-based access control (RBAC) in Apache Airflow
📖 Scenario: You are managing an Apache Airflow environment where different users need different access levels. To keep things secure and organized, you want to set up role-based access control (RBAC). This means defining roles with specific permissions and assigning users to these roles.
🎯 Goal: Build a simple RBAC setup in Airflow by creating roles with permissions and assigning users to these roles using Python code. You will create roles, add permissions, and assign users to roles.
📋 What You'll Learn
Create roles with specific permissions
Assign users to roles
Use Airflow's RBAC API to manage roles and users
Print the roles and their assigned users
💡 Why This Matters
🌍 Real World
RBAC is used in Airflow to control who can see or change workflows, making sure only authorized people can do sensitive actions.
💼 Career
Understanding RBAC helps you manage access securely in real Airflow deployments, a key skill for DevOps and data engineering roles.
Progress0 / 4 steps
1
Create roles with permissions
Create two roles called data_engineer and data_analyst using Airflow's Role class. Add the permission can_read to data_analyst and permissions can_read and can_edit to data_engineer.
Apache Airflow
Hint
Use the Role class to create roles and assign a set of permissions to the permissions attribute.
2
Create users and assign roles
Create two users called alice and bob using Airflow's User class. Assign the data_engineer role to alice and the data_analyst role to bob.
Apache Airflow
Hint
Use the User class to create users and assign roles by setting the roles attribute to a set containing the role objects.
3
List roles and their users
Create a dictionary called role_users that maps each role name (data_engineer and data_analyst) to a list of usernames assigned to that role. Use a for loop to check each user and add their username to the correct role list.
Apache Airflow
Hint
Loop over the list of users, then loop over each user's roles. Add the username to the list in role_users for the matching role name.
4
Print roles and assigned users
Print the role_users dictionary to display the roles and their assigned users.
Apache Airflow
Hint
Use print(role_users) to show the dictionary with roles and their users.
Practice
(1/5)
1. What is the main purpose of Role-Based Access Control (RBAC) in Apache Airflow?
easy
A. To assign users specific roles that define their permissions
B. To schedule DAG runs automatically
C. To monitor system resource usage
D. To create new DAGs from templates
Solution
Step 1: Understand RBAC concept
RBAC is about controlling who can do what by assigning roles with permissions.
Step 2: Identify RBAC purpose in Airflow
In Airflow, RBAC assigns roles to users to control access to features and data.
Final Answer:
To assign users specific roles that define their permissions -> Option A
Quick Check:
RBAC = Assign roles with permissions [OK]
Hint: RBAC means roles control user permissions [OK]
Common Mistakes:
Confusing RBAC with scheduling tasks
Thinking RBAC monitors system resources
Believing RBAC creates DAGs
2. Which Airflow CLI command correctly creates a user with the role 'Admin'?
easy
A. airflow users create --username alice --role Admin --email alice@example.com
B. airflow create user --name alice --role Admin
C. airflow user add --username alice --role Admin
D. airflow users add --user alice --role Admin
Solution
Step 1: Recall correct CLI syntax for user creation
The correct command uses 'airflow users create' with flags for username, role, and email.
Step 2: Match options to syntax
airflow users create --username alice --role Admin --email alice@example.com matches the correct syntax exactly; others have wrong commands or flags.
Final Answer:
airflow users create --username alice --role Admin --email alice@example.com -> Option A