-
Notifications
You must be signed in to change notification settings - Fork 14.5k
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
Simplify fab has access lookup #19294
Simplify fab has access lookup #19294
Conversation
e7e11e7
to
8f2dbef
Compare
45decd0
to
da380c4
Compare
def perms(self): | ||
perms = set() | ||
for role in self.roles: | ||
perms.update((perm.action.name, perm.resource.name) for perm in role.permissions) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you may need to do this as a single query for efficiency reasons?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added lazy=joined
to several model fields, which causes prefetching. It now runs 11 queries instead of 49.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@andrewgodwin that said, this PR does add some overall time to the tests. pytest tests/views --with-db-init
runs in 1:53 on main and 2:00 on my branch. How significant is that?
da380c4
to
a682403
Compare
ecb8ad1
to
bf6f20c
Compare
airflow/www/auth.py
Outdated
@@ -35,7 +35,7 @@ def decorated(*args, **kwargs): | |||
__tracebackhide__ = True # Hide from pytest traceback. | |||
|
|||
appbuilder = current_app.appbuilder | |||
if not g.user.is_anonymous and not appbuilder.sm.current_user_has_permissions(): | |||
if not (g.user.is_anonymous or g.user.perms): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel the previous version is a bit easier to understand (i.e. “not anonymous but does not have any permissions”)
if not (g.user.is_anonymous or g.user.perms): | |
if not g.user.is_anonymous and not g.user.perms: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See this one @jhtimmins
bf6f20c
to
50f4180
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM -- a few small comments to address or say "naaah" to
@@ -339,7 +333,7 @@ def filter_roles_by_perm_with_action(self, action_name: str, role_ids: List[int] | |||
) | |||
).all() | |||
|
|||
def get_role_permissions_from_db(self, role_id: int) -> List[Permission]: | |||
def get_permissions_from_roles(self, role_id: int) -> List[Permission]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def get_permissions_from_roles(self, role_id: int) -> List[Permission]: | |
def get_permissions_from_role(self, role_id: int) -> List[Permission]: |
Should be singular given it takes a single role_id. (Possibly also called _from_role_id
, but don't mind on that one)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its actually not used anymore so should be removed entirely
@property | ||
def perms(self): | ||
if not self._perms: | ||
self._perms = set() | ||
for role in self.roles: | ||
self._perms.update({(perm.action.name, perm.resource.name) for perm in role.permissions}) | ||
return self._perms |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from airflow.compat.functools import cached_property
and then:
@property | |
def perms(self): | |
if not self._perms: | |
self._perms = set() | |
for role in self.roles: | |
self._perms.update({(perm.action.name, perm.resource.name) for perm in role.permissions}) | |
return self._perms | |
@cached_property | |
def perms(self): | |
_perms = set() | |
for role in self.roles: | |
_perms.update({(perm.action.name, perm.resource.name) for perm in role.permissions}) | |
return _perms |
And then del self.perms
in the @roles.setter
to clear the cache.
(This is all functionally the same, I just find the delcarative @cached_property
clearer to read.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll make this change in a follow up PR
airflow/www/auth.py
Outdated
@@ -35,7 +35,7 @@ def decorated(*args, **kwargs): | |||
__tracebackhide__ = True # Hide from pytest traceback. | |||
|
|||
appbuilder = current_app.appbuilder | |||
if not g.user.is_anonymous and not appbuilder.sm.current_user_has_permissions(): | |||
if not (g.user.is_anonymous or g.user.perms): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See this one @jhtimmins
airflow/api_connexion/parameters.py
Outdated
@@ -100,7 +100,7 @@ def apply_sorting(query, order_by, to_replace=None, allowed_attrs=None): | |||
if to_replace: | |||
lstriped_orderby = to_replace.get(lstriped_orderby, lstriped_orderby) | |||
if order_by[0] == "-": | |||
order_by = f"{lstriped_orderby} desc" | |||
order_by = desc(text(lstriped_orderby)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated change in this file?
5975e47
to
7b0487b
Compare
d3fac5c
to
ff89d19
Compare
@ashb The most recent changes address a bug that only arises with MSSQL. Bc my changes to the permission models include prefetching of data, the My change addresses the two API functions that query on the permission models, and replaces the calls to I've duplicated some code, which I could remove by modifying the original |
This reverts commit 3e98280.
Simplifies the FAB security manager code by doing the following.
User.perms
property method and checking if a permission pair is included in the user's list of perms.AnonymousUserMixin
to createAnonymousUser
, which has bothperms
androles
property methods. Removes the need for custom auth logic for anonymous users..get_readable_dags()
.current_user_has_permissions()
._has_resource_access()
.user_has_perms()
.user_has_roles()
SecurityManager.is_item_public()
Permissions.resource_name_for_dag()
This is the next piece of the permissions refactor.