Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AIP-84 | Add Auth for Dags #47062

Conversation

jason810496
Copy link
Contributor

related: #42360


^ 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.rst or {issue_number}.significant.rst, in newsfragments.

@boring-cyborg boring-cyborg bot added area:API Airflow's REST/HTTP API area:UI Related to UI/UX. For Frontend Developers. labels Feb 25, 2025
@jason810496
Copy link
Contributor Author

jason810496 commented Feb 25, 2025

Hi @pierrejeambrun,

Here is a draft for the first entity that introduces authentication and permissions. I want to ensure consistency across other entities and would appreciate your advice on the following changes:

  • tests/api_fastapi/conftest.py

    • Added a bearer token with an admin role to the original test_client fixture.
    • Renamed the original test_client to unauthenticated_test_client (since adding the requires_access_* dependencies to routers means we should now respect the authorization header).
  • tests/api_fastapi/core_api/routes/public/test_dags.py

    • Is adding _should_response_401 test cases for each router sufficient?
    • Or should we cover more scenarios, such as Vertical Privilege Escalation?

Looking forward to your thoughts!
cc @rawwar

Copy link
Member

@pierrejeambrun pierrejeambrun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great start 🎉

Is adding _should_response_401 test cases for each router sufficient?

I think so yes.

@jason810496 jason810496 changed the title [WIP] AIP-84 | Add Auth for Configuration [WIP] AIP-84 | Add Auth for Dags Feb 27, 2025
@jason810496 jason810496 force-pushed the feature/AIP-84/add-auth-for-configuration branch from 36346db to 625cda9 Compare February 27, 2025 13:48
@jason810496 jason810496 changed the title [WIP] AIP-84 | Add Auth for Dags AIP-84 | Add Auth for Dags Feb 27, 2025
@jason810496 jason810496 force-pushed the feature/AIP-84/add-auth-for-configuration branch from 625cda9 to 5525f2a Compare February 28, 2025 03:48
@jason810496
Copy link
Contributor Author

jason810496 commented Feb 28, 2025

After discussing with @rawwar offline, we determined that the previous test failure with 403 status code was caused by JWT and test cases using time_machine.

The test cases with time_machine are decorated with @time_machine.travel(timezone.utcnow(), tick=False). However, when the method is executed, the test_client fixture is recreated, causing the token to be perceived as being from the future.

After further discussion, by setting iat and nbf to a much earlier time (when Airflow was created) and exp to 24 hours in the future resolves the issue.

  • iat: Issued at
  • nbf: Not before
  • exp: Expiration time

cc @pierrejeambrun

@jason810496 jason810496 marked this pull request as ready for review February 28, 2025 04:05
@jason810496
Copy link
Contributor Author

The rest of CI failures are caused by some side effect of auth_manage with FastAPI app, but I still can't figure out what is the root cause:

e.g.

  • tests/api_fastapi/core_api/routes/public/test_assets.py::TestPostAssetMaterialize
  • tests/api_fastapi/core_api/routes/public/test_task_instances.py::TestGetTaskInstance
  • tests/api_fastapi/core_api/routes/public/test_task_instances.py::TestGetTaskInstanceTry
  • tests/api_fastapi/core_api/routes/public/test_task_instances.py::TestGetTaskInstanceTries

If I remove the conf_vars, those test case works well, I have also tried with replace conf_vars with try, finally to setup, teardown [core/auth_manager] config without with block and it doesn't help.

@pytest.fixture
def test_client():
    with conf_vars(
        {
            (
                "core",
                "auth_manager",
            ): "airflow.auth.managers.simple.simple_auth_manager.SimpleAuthManager",
        }
    ):
        yield TestClient(create_app())

Only session.commit explicitly can resolve this case ( bring out by @rawwar in #47136 )
cc @pierrejeambrun

@jason810496 jason810496 force-pushed the feature/AIP-84/add-auth-for-configuration branch from 10f76c6 to 6a10d9e Compare February 28, 2025 11:36
Copy link
Member

@pierrejeambrun pierrejeambrun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is the first one that we need to merge to unblock other permissions PR.

Do you need help fixing the CI ?

edit: Let me take a look.

@jason810496
Copy link
Contributor Author

jason810496 commented Feb 28, 2025

Looks like this is the first one that we need to merge to unblock other permissions PR.

Indeed, this is the first PR for permissions.

Do you need help fixing the CI ?

Yes, I need more thought for the CI errors, thanks!

As I mentioned in #47062 (comment) explicitly commit the session can resolve, but I think that is kind of workaround way to solve it. I haven't come out other idea to solving it.

Copy link
Member

@pierrejeambrun pierrejeambrun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a look at the failing TestPostAssetMaterialize for test_should_respond_200.

Indeed we are missing a commit there in the create_dags fixture.dag_maker will just not commit the change I believe if the session is provided from the caller.

I don't get why it was working before, maybe changing the client fixture to a context manager updated the pytest fixture resolution order, in such a way that we were lucky before, a fixture with a commit appeared later than the create_dags one and was hidding the issue ? I don't know.

Basically it's the same for configure_git_connection_for_dag_bundle fixture. Theoritically nobody is committing the new connection. If there are no other fixtures called or init code, we will end up without the connection in the db. (and locking error on sqlite).

Also test_should_respond_200_with_versions (reason for another failed test). In the loop make_dag_with_multiple_versions the last iteration will be missing the commit, dag.sync_to_db() is acting like it, you can just move it to the end of the loop I think to achieve that.

I guess other cases are similar to that.

Basically fixture creating db objects should commit. (or not commit if the helper code they call do the commit for them)

@pierrejeambrun
Copy link
Member

I found this piece of code in the dag_maker:

            if AIRFLOW_V_3_0_PLUS:
                from airflow.models.dagbundle import DagBundleModel

                if (
                    self.session.query(DagBundleModel).filter(DagBundleModel.name == self.bundle_name).count()
                    == 0
                ):
                    self.session.add(DagBundleModel(name=self.bundle_name))
                    self.session.commit()

            return self

@pierrejeambrun
Copy link
Member

We'll merge #47136 first, you can rebase then it will fix your CI issues.

@jason810496
Copy link
Contributor Author

maybe changing the client fixture to a context manager updated the pytest fixture resolution order, in such a way that we were lucky before, a fixture with a commit appeared later than the create_dags one and was hidding the issue ?

I think that is the only way to illustrate this wired error, thanks for the explanation !

Basically fixture creating db objects should commit. (or not commit if the helper code they call do the commit for them)

Thanks to point out, so that we should commit explicitly when using session fixture.

Maybe, the test I wrote with before utilize provide_session decorator, it will commit at the end of context, so I won't add commit explicitly in test.

@jason810496 jason810496 force-pushed the feature/AIP-84/add-auth-for-configuration branch from c7410a6 to 8c8ded1 Compare March 1, 2025 03:41
@jason810496 jason810496 force-pushed the feature/AIP-84/add-auth-for-configuration branch from 8c8ded1 to 4e1b9d9 Compare March 3, 2025 12:47
Copy link
Member

@pierrejeambrun pierrejeambrun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, this needs a rebase I think.

@jason810496 jason810496 force-pushed the feature/AIP-84/add-auth-for-configuration branch from 4e1b9d9 to cc9676b Compare March 3, 2025 14:33
@jason810496
Copy link
Contributor Author

Nice, this needs a rebase I think.

Just rebased, thanks!

There's still an open discussion regarding SimpleAuthManager that needs confirmation: #47062 (comment).
Mentioning it again here to ensure it’s not overlooked.

@jedcunningham jedcunningham added the AIP-84 Modern Rest API label Mar 4, 2025
@jason810496 jason810496 force-pushed the feature/AIP-84/add-auth-for-configuration branch 2 times, most recently from 5d53a39 to e8d643b Compare March 4, 2025 11:55
Copy link
Member

@pierrejeambrun pierrejeambrun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs rebase + conflict soliving.

Nice, just a few suggestions

@Lee-W Lee-W self-requested a review March 5, 2025 06:46
@jason810496 jason810496 force-pushed the feature/AIP-84/add-auth-for-configuration branch 2 times, most recently from f2c5973 to eb6ac7c Compare March 5, 2025 10:54
Copy link
Member

@pierrejeambrun pierrejeambrun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM just a few nits (non blocking)

@jason810496 jason810496 force-pushed the feature/AIP-84/add-auth-for-configuration branch from eb6ac7c to 807dd89 Compare March 5, 2025 12:37
@jason810496
Copy link
Contributor Author

LGTM just a few nits (non blocking)

Thanks @pierrejeambrun , just resolved and rebased.

@pierrejeambrun pierrejeambrun merged commit 6107fce into apache:main Mar 5, 2025
44 checks passed
ashb added a commit that referenced this pull request Mar 5, 2025
shahar1 pushed a commit to shahar1/airflow that referenced this pull request Mar 5, 2025
* AIP-84 | Add Auth for Dag

* Refactor conftest for api_fastapi and test_dags

* fixup! AIP-84 | Add Auth for Dag

* Add unauthorized 403 test cases

* Remove PATCH in requires_access

* Fix unauthorized_test_client, requires_access_dag

* Add EditableDagsFilterDep, ReadableDagsFilterDep

* Add permitted_dag_filter for dags API

* Fix test_security

* Add OrmFilterClause

Fix mypy error

* fixup! Add OrmFilterClause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AIP-84 Modern Rest API area:API Airflow's REST/HTTP API area:UI Related to UI/UX. For Frontend Developers.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants