Skip to content

Airflow not using custom API authentication/auth_backends #51362

Closed

Description

@andre-s-pedrosa-alb

Apache Airflow version

3.0.1

If "Other Airflow 2 version" selected, which one?

No response

What happened?

Recently we upgraded to ariflow 3.0.1 in our dev server but we are having problems authenticating one of our API component in Airflow through keycloak.

In the logs I simply get an error:
[2025-06-03T11:02:34.484+0000] {base_auth_manager.py:105} ERROR - JWT token is not valid: The specified alg value is not allowed

What you think should happen instead?

No response

How to reproduce

I have set a custom auth_backend
AIRFLOW__FAB__AUTH_BACKENDS=user_auth,airflow.providers.fab.auth_manager.api.auth.backend.session

I also have a file user_auth.py in /opt/airflow, which is in PYTHONPATH

user_auth.py
def init_app(_):
    # Initializes authentication backend
    pass

def auth_current_user() -> User | None:
    # Authenticate and set current user if Authorization header exists

    ab_security_manager = get_auth_manager().security_manager
    user = None
    if ab_security_manager.auth_type == AUTH_OAUTH:
        if 'Authorization' not in request.headers or not request.headers['Authorization']:
            return None

        token = str(request.headers['Authorization']).replace("Bearer ", "")

        # with signature validation. audience value was giving some problems
        #me = jwt.decode(token, public_key, algorithms=['HS256', 'RS256'], audience=CLIENT_ID)

        # witouth signature validation
        me = jwt.decode(token, options={"verify_signature": False})

        groups = me["resource_access"]["airflow"]["roles"] # unsafe
        if len(groups) < 1:
            groups = ["airflow_public"]
        else:
            groups = [group_name for group_name in groups if "airflow" in group_name]
        userinfo = {
            "username": me.get("preferred_username"),
            "email": me.get("email"),
            "first_name": me.get("given_name"),
            "last_name": me.get("family_name"),
            "role_keys": groups,
        }

        user = ab_security_manager.auth_user_oauth(userinfo)
    else:
        auth = request.authorization
        if auth is None or not auth.username or not auth.password:
            return None
        if ab_security_manager.auth_type == AUTH_LDAP:
            user = ab_security_manager.auth_user_ldap(auth.username, auth.password)
        if ab_security_manager.auth_type == AUTH_DB:
            user = ab_security_manager.auth_user_db(auth.username, auth.password)

    log.info("user: {0}".format(user))
    if user is not None:
        login_user(user, remember=False)

    return user

def requires_authentication(function: Callable):
    # Decorator for functions that require authentication
    @wraps(function)
    def decorated(*args, **kwargs):
        if auth_current_user() is not None:
            return function(*args, **kwargs)
        else:
            return Response("Unauthorized", 401, {"WWW-Authenticate": "Basic"})

    return cast(Callable, decorated)

I know that the file is being interpreted, because if I change the AIRFLOW__FAB__AUTH_BACKENDS variable to user_authhhh_invalid for example I get an error that the file was not found. OR if I add a syntax error in the user_auth.py file I also get an error.

However if I add a semantic error inside one of the functions (print of an undefined variable) I get no error, in other words the functions are not being executed when a API request is made.

Operating System

Oracle linux 8

Versions of Apache Airflow Providers

$ pip freeze | grep providers
apache-airflow-providers-apache-spark==5.2.1
apache-airflow-providers-celery==3.11.0
apache-airflow-providers-common-compat==1.6.1
apache-airflow-providers-common-io==1.5.4
apache-airflow-providers-common-sql==1.27.0
apache-airflow-providers-fab==2.0.2
apache-airflow-providers-hashicorp==4.1.1
apache-airflow-providers-postgres==6.1.3
apache-airflow-providers-redis==4.1.0
apache-airflow-providers-smtp==2.0.3
apache-airflow-providers-standard==1.1.0

Deployment

Other Docker-based deployment

Deployment details

podman with a custom image, using as base the image docker.io/apache/airflow:slim-3.0.1

Anything else?

No response

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Activity

  1. added
    kind:bug
    needs-triage
    on Jun 3, 2025
  2. boring-cyborg commented on Jun 3, 2025

    @boring-cyborg

    Thanks for opening your first issue here! Be sure to follow the issue template! If you are willing to raise PR to address this issue please do so, no need to wait for approval.

  3. added
    area:API
    on Jun 3, 2025
  4. eladkal commented on Jun 6, 2025

    @eladkal
    Contributor

    cc @vincbeck can you take a look?

  5. vincbeck commented on Jun 6, 2025

    @vincbeck
    Contributor

    Which API are you hitting to test it?

  6. andre-s-pedrosa-alb commented on Jun 6, 2025

    @andre-s-pedrosa-alb
    Author

    /api/v2/dags/:id/dagRuns?...

  7. vincbeck commented on Jun 6, 2025

    @vincbeck
    Contributor

    Airflow 3 no longer use auth backends to authenticate the Airflow public API. You need to use a JWT token in order to use the Rest API. Please read the doc.

    Auth backends are now exclusively used to call FAB auth managers API: list users, list roles, list permissions, etc.

  8. alpeshv commented on Jun 6, 2025

    @alpeshv

    @vincbeck thank you for picking this up. I am having the same issue so can provide some more insight.

    We have been using custom auth implementation with keycloak for Airflow api v1 to make airflow api calls from other apps, mainly to trigger a specific dag.

    It was working fine until we upgraded Airflow to 3.0.1 and now trying to the use same setup for Airflow api v2. My keycloak tokens use RS256, where as airflow, by default, recognises tokens with alg HS512, when jwt_secret is set as per this and thats what causing this issue.

    I have tried changing this alg to RS256 and removed jwt_secret but that was causing api-server pod to crash with error The value api_auth/jwt_secret must be set in api-server pod.

    My problem is that I am also using FabAirflowSecurityManagerOverride in my api-server config to enable keycloak oauth for Airflow UI which works fine but I am not able to make the same thing work for api auth.

      - name: AIRFLOW__API__AUTH_BACKENDS
        value: 'airflow.providers.fab.auth_manager.api.auth.backend.custom_jwt_auth_backend'
      - name: AIRFLOW__FAB__AUTH_BACKENDS
        value: 'airflow.providers.fab.auth_manager.api.auth.backend.custom_jwt_auth_backend'
      - name: AIRFLOW__CORE__AUTH_MANAGER
        value: 'airflow.providers.fab.auth_manager.fab_auth_manager.FabAuthManager'
    

    Its really confusing to me that how UI auth works with keycloak but api auth doesn't. I guess fab manager somehow exchanges keycloak token for a simpler jwt token which airflow can understand. Also not sure how these backends and auth manager work together for api authentication. I have gone through all the documentation available but could not find anything helpful.

  9. zach-overflow commented on Jun 6, 2025

    @zach-overflow
    Contributor

    not sure how these backends and auth manager work together for api authentication. I have gone through all the documentation available but could not find anything helpful.

    I'd second this point. I'd be happy to help improve the docs but I'm also running into challenges with the FAB auth manager setup, and I'm similarly not clear on what the expected behavior is here. It seems there was also a recent closed issue about some similar confusion on the FAB auth manager docs regarding the auth_backends option, but I'm not sure if the details in that issue thread were clarified, or are relevant, in the later versions of the FAB provider (versions greater than 2.0.2).

  10. vincbeck commented on Jun 6, 2025

    @vincbeck
    Contributor

    Thanks for the feedbacks, I'll try to provide more details about the different APIs and their authentication. As a final step, I agree we should update the docs, it seems it is confusing and not easy to grasp.

    Airflow API VS FAB auth manager API VS FAB auth manager token API

    Airflow API is the Airflow public API used to interact with Airflow. For example, you can list DAGs, delete connections, update variables, trigger a DAG, etc ... Its endpoint is /api/v2

    FAB auth manager API is used to manage users, roles and permissions. Its endpoint is /auth/fab/v1/.

    FAB auth manager token API is used to generate a JWT token using FAB auth manager. Its endpoint is /auth/token.

    Airflow API authentication

    In order to authenticate with Airflow API, you need to use JWT token. More information in documentation.

    In your case, since you are using FAB auth manager, you need to generate a JWT token using FAB auth manager. This is explained in the documentation here. Once you generate a JWT token, you can use it to authenticate with Airflow API.

    FAB auth manager authentication

    In order to authenticate with FAB auth manager API, you need to use auth backends. More information in documentation.

    I hope that helps and please let me know if you're still stuck.

  11. alpeshv commented on Jun 7, 2025

    @alpeshv

    @vincbeck Thank you for the summary and sorry to be pain but I still can't get my head around it.

    Using Fab Auth Manager API makes sense when you want to generate the JWT token using username and password. As I mentioned earlier, I want to pass a token generated by keycloak which uses RS256 alg. I have tried configuring Airflow to use RS256 but its still looking for jwt_secret which is needed for a symmetric algorithm like HS512.

    Also from the other closed issue, the reporter has used

    export AIRFLOW__CORE__AUTH_MANAGER="my.custom.FabAuthManager"
    export AIRFLOW__FAB__AUTH_BACKENDS="my.custom.api_backend"
    

    Do I need to provide custom implementation for AIRFLOW__CORE__AUTH_MANAGER when I am passing a custom implementation for AIRFLOW__FAB__AUTH_BACKENDS?

  12. vincbeck commented on Jun 9, 2025

    @vincbeck
    Contributor

    @vincbeck Thank you for the summary and sorry to be pain but I still can't get my head around it.

    Using Fab Auth Manager API makes sense when you want to generate the JWT token using username and password. As I mentioned earlier, I want to pass a token generated by keycloak which uses RS256 alg. I have tried configuring Airflow to use RS256 but its still looking for jwt_secret which is needed for a symmetric algorithm like HS512.

    Also from the other closed issue, the reporter has used

    export AIRFLOW__CORE__AUTH_MANAGER="my.custom.FabAuthManager"
    export AIRFLOW__FAB__AUTH_BACKENDS="my.custom.api_backend"
    

    Do I need to provide custom implementation for AIRFLOW__CORE__AUTH_MANAGER when I am passing a custom implementation for AIRFLOW__FAB__AUTH_BACKENDS?

    This is not supported today. If you want to use a token generated by Keycloak to access Airflow API you need to implement your own auth manager or wait for the future (but soon) Keycloak Auth manager. Keycloak auth manager implementation is currently in progress

  13. edsu commented on Jun 16, 2025

    @edsu
    Contributor

    @alpeshv if you do go the route of implementing your own auth manager for Keycloak it would be super helpful to see it as I seem to have the same problem.

  14. 16 remaining items

  15. vincbeck commented on Dec 2, 2025

    @vincbeck
    Contributor

    This is what @melicheradam is currently doing

  16. vincbeck commented on Dec 2, 2025

    @vincbeck
    Contributor

    Loudly thinking. MAYBE we can create and release a separate distribution with Flask and other necessary dependencies that could provide a bridge for people who used the all AuthBackends? Say apache-airflow-auth-backend-v2-backcompat ?

    I can see this can be a blocker, but I think it should be possible to extend the original JWT-only mechanism with a simple, pluggable API that could be provided by this additional package @vincbeck @pierrejeambrun -> WDYT?

    The think it do not really like with this solution is yet something else to maintain, release etc

  17. potiuk commented on Dec 2, 2025

    @potiuk
    Member

    Well. If there is a workable solution like in https://gist.github.com/melicheradam/9fb5977ea21f194eefc87d4176f3fb84 - we can simply make it into a possible workaround that those users who would like to use, could.

  18. vincbeck commented on Dec 2, 2025

    @vincbeck
    Contributor

    My understanding is this is an example of implementation but each user has their own implementation they want to use. For example, in the description of this issue there is a file user_auth.py that contains a logic @andre-s-pedrosa-alb
    would like to use to authenticate in order to use the API. But this logic might not (and very most likely) fit everyone's needs

  19. pierrejeambrun commented on Dec 2, 2025

    @pierrejeambrun
    Member

    Another proposal would be to create a new method in the auth manager interface like create_api_token (or any other better name). This method would basically contain the logic here. This logic, by default, expect username and password. Users would then be able to easily override this method and implement their own logic. What do you think?

    If we can workaround this by extending the Auth Manager interface and implementing a custom auth manager with that, I think it's a reasonable approach to replace 2.x auth backend. I would favor this over a new distribution because it's mostly straight forward and people seem familiar with custom auth manager already. A piece of doc explaining how to convert your custom auth backend to custom auth manager for a happy path upgrade to airflow 3 would be cool too!

  20. melicheradam commented on Dec 2, 2025

    @melicheradam

    I think the example provided in the issue description user_auth.py is what most users were using for Airflow 2 (me included), I have found it somewhere on the internet long ago.

    edit: some examples

  21. vincbeck commented on Dec 3, 2025

    @vincbeck
    Contributor

    I see. If no objection, I'll go with my proposal, it should be fairly easy for users to override it and put their custom logic. I'll also update the doc to explain as best as I can how to override it and maybe put the snippet code in user_auth.py in the doc as well. What do you think?

  22. pierrejeambrun commented on Dec 3, 2025

    @pierrejeambrun
    Member

    I see. If no objection, I'll go with my proposal, it should be fairly easy for users to override it and put their custom logic. I'll also update the doc to explain as best as I can how to override it and maybe put the snippet code in user_auth.py in the doc as well. What do you think?

    Sounds good to me!

  23. potiuk commented on Dec 6, 2025

    @potiuk
    Member

    I see. If no objection, I'll go with my proposal, it should be fairly easy for users to override it and put their custom logic. I'll also update the doc to explain as best as I can how to override it and maybe put the snippet code in user_auth.py in the doc as well. What do you think?

    Perfect

  24. vincbeck commented on Dec 9, 2025

    @vincbeck
    Contributor

    #59245. @melicheradam @cklat @andre-s-pedrosa-alb @alpeshv @zach-overflow Could you please take a look at the PR and confirm this is what you are looking for? Or at least solve your issue. Please review the doc as well and suggest improvement if you see some.

  25. melicheradam commented on Dec 9, 2025

    @melicheradam

    Hey @vincbeck, to me it seems like this is adressing the issue from the other end. From my testing the API authorization flow was using FabAuthManager.get_user_from_token, som im not sure where this is integrated in all of that.

    How is this intended to be used? Get token from IAM -> send this token as Bearer header to airflow/api/auth endpoint -> receive new "airflow token" -> use this "airflow token" for subsequent requests?

    In Airflow 2, it was not about creating tokens, but about processing tokens. The thing is, when having a multi-service architecture, you usually dont want each service/app issuing its own tokens. You have a IAM/Authz provider that generates the tokens, and then each service can just validate the token from this provider.

    Either way seems like this can be used, if I understood it correctly.

  26. vincbeck commented on Dec 9, 2025

    @vincbeck
    Contributor

    Have you read the doc in the PR? If yes I think this is not clear then :) I'll try to provide more details.

    In Airflow 3, the Airflow API uses JWT token to authenticate. We do NOT want to change that. This will stay as is. I understand that in Airflow 2, you could customize that, this is no longer possible in Airflow 3. In order to generate a JWT token to call Airflow API, you can use Fab auth manager token API. Each auth manager define its own way to generate a JWT token.

    To answer your question, you should:

    • Override create_token method as described in the doc
    • As a user, you should call Fab auth manager token API to generate a token and use this token to call Airflow API
  27. melicheradam commented on Dec 9, 2025

    @melicheradam

    Yes I have read the doc. I was not completely sure, it this

    Get token from IAM -> send this token as Bearer header to airflow/api/auth endpoint -> receive new "airflow token" -> use this "airflow token" for subsequent requests?

    is the intended/correct flow.

    Where have I suggested not using JWT token to authenticate? I am just saying, in Airflow 2, you could use ANY token from ANY provider when accessing the API. Now you have to use Airflow - because you need to use the Fab auth manager token API. And I am talking about the logic in FabAuthManager.get_user_from_token, since that is the one processing tokens. This was previously easily customizable, not generating JWT tokens in airflow.

    Anyway thanks for the hard work, i am sure the solution you provided will be usable :)

  28. vincbeck commented on Dec 9, 2025

    @vincbeck
    Contributor

    Theoretically you are right, you can override get_user_from_token so that you can process any token, and not only the "Airflow" token. However, this works only if you use token (in general) to authenticate users. That's why I rather document officially the Fab auth manager token API way, which can work with any type of information (not only tokens).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:APIarea:corekind:bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions