Skip to content

Use is_authorized_* APIs in views#35000

Merged
vincbeck merged 14 commits into
apache:mainfrom
aws-mwaa:vincbeck/use_is_authorized_2
Nov 2, 2023
Merged

Use is_authorized_* APIs in views#35000
vincbeck merged 14 commits into
apache:mainfrom
aws-mwaa:vincbeck/use_is_authorized_2

Conversation

@vincbeck

Copy link
Copy Markdown
Contributor

Use is_authorized_* APIs provided by the auth manager in views.


^ 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.

@vincbeck vincbeck added the AIP-56 Extensible user management label Oct 17, 2023
@boring-cyborg boring-cyborg Bot added area:API Airflow's REST/HTTP API area:webserver Webserver related Issues kind:documentation labels Oct 17, 2023
@vincbeck vincbeck force-pushed the vincbeck/use_is_authorized_2 branch from 0f3b915 to 1dff292 Compare October 17, 2023 18:59
@vincbeck vincbeck force-pushed the vincbeck/use_is_authorized_2 branch from 1dff292 to e18a5cd Compare October 17, 2023 21:43
Comment thread airflow/auth/managers/utils/fab.py
@uranusjr

uranusjr commented Oct 18, 2023

Copy link
Copy Markdown
Member

I wonder if we can refactor the giant if-elif chain into a dispatching dict. Something like…

@cache
def _get_auth_manager_map() -> dict[str, Callable[[str, BaseUser | None]]]:
    manager = get_auth_manager()
    actions = get_method_from_fab_action_map()
    return {
        RESOURCE_AUDIT_LOG: lambda action, user: manager.is_authorized_dag(
            method=actions[action], access_entity=DagAccessEntity.AUDIT_LOG, user=user,
        ),
        RESOURCE_CLUSTER_ACTIVITY: lambda action, user: manager.is_authorized_view(
            access_view=AccessView.CLUSTER_ACTIVITY, user=user,
        ),
        ...
    }

class AirflowSecirutyManagerV2(...):
    def _get_auth_manager_is_authorized_method(
        self, fab_action_name: str, fab_resource_name: str, user
    ) -> Callable[[str, BaseUser | None]] | None:
        return _get_auth_manager_map().get(fab_resource_name)

This could be easier to read (?) and also margianlly faster.

@vincbeck

Copy link
Copy Markdown
Contributor Author

I wonder if we can refactor the giant if-elif chain into a dispatching dict. Something like…

@cache
def _get_auth_manager_map() -> dict[str, Callable[[str, BaseUser | None]]]:
    manager = get_auth_manager()
    actions = get_method_from_fab_action_map()
    return {
        RESOURCE_AUDIT_LOG: lambda action, user: manager.is_authorized_dag(
            method=actions[action], access_entity=DagAccessEntity.AUDIT_LOG, user=user,
        ),
        RESOURCE_CLUSTER_ACTIVITY: lambda action, user: manager.is_authorized_view(
            access_view=AccessView.CLUSTER_ACTIVITY, user=user,
        ),
        ...
    }

class AirflowSecirutyManagerV2(...):
    def _get_auth_manager_is_authorized_method(
        self, fab_action_name: str, fab_resource_name: str, user
    ) -> Callable[[str, BaseUser | None]] | None:
        return _get_auth_manager_map().get(fab_resource_name)

This could be easier to read (?) and also margianlly faster.

Agree, nice suggestion! Let me do that

Comment thread airflow/www/security_manager.py
@vincbeck

Copy link
Copy Markdown
Contributor Author

Any takers on this one?

Spoiler alert! Once this PR merged I'll be able to create a PR to remove the deprecation warning currently raised:

/opt/airflow/airflow/auth/managers/fab/api_endpoints/user_endpoint.py:46 RemovedInAirflow3Warning: The 'requires_access' decorator is deprecated. Please use one of the decorator `requires_access_*`defined in airflow/api_connexion/security.py instead.

Could be cool to do this before the next Airflow release so users dont get deprecation warnings for nothing 👼

@vincbeck vincbeck requested review from potiuk and uranusjr October 25, 2023 13:48

@o-nikolas o-nikolas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not super familiar with the auth code, but in general the PR looks reasonable to me!

@vincbeck vincbeck added the use public runners Makes sure that Public runners are used even if commiters creates the PR (useful for testing) label Nov 2, 2023
@vincbeck vincbeck closed this Nov 2, 2023
@vincbeck vincbeck reopened this Nov 2, 2023
@vincbeck vincbeck merged commit aaa2aed into apache:main Nov 2, 2023
@vincbeck vincbeck deleted the vincbeck/use_is_authorized_2 branch November 2, 2023 16:18
# Resource Constants
RESOURCE_ACTION = "Permissions"
RESOURCE_ADMIN_MENU = "Admin"
RESOURCE_AIRFLOW = "Airflow"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What's now the equivalent of this resource and logins resource?

@vincbeck vincbeck Nov 9, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I could not find this one used anywhere, that's why I removed it. Is it used somewhere?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can't find the usage too. LGTM

romsharon98 pushed a commit to romsharon98/airflow that referenced this pull request Nov 10, 2023
@ephraimbuddy ephraimbuddy added the changelog:skip Changes that should be skipped from the changelog (CI, tests, etc..) label Nov 20, 2023
@ephraimbuddy ephraimbuddy added this to the Airflow 2.8.0 milestone Nov 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AIP-56 Extensible user management area:API Airflow's REST/HTTP API area:webserver Webserver related Issues changelog:skip Changes that should be skipped from the changelog (CI, tests, etc..) kind:documentation use public runners Makes sure that Public runners are used even if commiters creates the PR (useful for testing)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants