Add user impersonation (run_as_user) support for task execution #51149
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
closes: #50423
Why?
Airflow 2 had support for user impersonation: https://airflow.apache.org/docs/apache-airflow/stable/security/workload.html. Quoting from docs:
The intention here is to de-elevate the user running the task to reduce priviliges from the process / worker launching the process which runs as root. We can configure the task to impersonate as an user with lesser priviliges and control the behaviour of the tasks running for a more secure task run.
Quoting one of the use case from one of the airflow users too:
https://apache-airflow.slack.com/archives/CCQB40SQJ/p1746728794387939
Implementation
Airflow 2 essentially did:
sudo -u user "your_bash_command_here".For airflow 3, we should do something simular, basically run the task runner running the workload as the provided
run_as_user.Introduced a helper method to attempt impersonation,
• Uses
pwd.getpwnam()to resolve the UID and GID of the user.• Applies os.setgid() followed by os.setuid() to apply privileges.
• Handles KeyError, PermissionError.
The order of setgid() → setuid() is intentional. Once a user is dropped to non-root via setuid, regaining privileges is impossible.
In the task runner startup, we check if the run_as_user is set, if not check if the config for default_impersonation is set: https://airflow.apache.org/docs/apache-airflow/stable/security/workload.html#default-impersonation.
If neither is set, continues with the current user. (root usually, for CE atleast)
Testing
Intention is to run airflow as "root" and switch to a lesser privileged user: "airflowuser". We will try and use a user that cannot list some files like
/root/airflow/airflow.cfgintentionally.Setup for testing
sudo useradd -m -s /bin/bash airflowuserDAG:
Running this without run_as_user, can access that file:
Test 1: Check if a task can run with
run_as_userprovided at task level.DAG Used:
extract is running with "airflowuser" and airflowuser is present.
Errors out, logs:

Test 2: Do not provide
run_as_userbut override with the conf instead: "airflowuser" itselfSet env in worker:
DAG Used:
Same error as before:
Logs:
Test 3: Provide
run_as_userand in conf, to check which one is picked upIn worker, create new user:
randomuserand set env to "airflowuser"DAG used:
Random user picked up:

TODO:
^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in a newsfragment file, named
{pr_number}.significant.rstor{issue_number}.significant.rst, in airflow-core/newsfragments.