Skip to content

Conversation

@jedcunningham
Copy link
Member

For DAGs without access_control that already have their PermissionView
records, we can skip syncing their permissions during parsing. This cuts
down on database queries and is faster (~2 seconds, mostly import time).

@boring-cyborg boring-cyborg bot added the area:webserver Webserver related Issues label Apr 20, 2021
@jedcunningham jedcunningham added the area:Scheduler including HA (high availability) scheduler label Apr 20, 2021
For DAGs without `access_control` that already have their PermissionView
records, we can skip syncing their permissions during parsing. This cuts
down on database queries and is faster (~2 seconds, mostly import time).
DAG_PERMS = {ACTION_CAN_READ, ACTION_CAN_EDIT}


def prefixed_dag_id(dag_id):
Copy link
Member

Choose a reason for hiding this comment

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

Can we rename this to permission_for_dag or something better? WDYT

Copy link
Member Author

Choose a reason for hiding this comment

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

I went back and forth on renaming it since it is being moved from www/security, but I'm also happy with a more descriptive name. Related, should we follow up later with deprecating the method in www/security?

Copy link
Member

Choose a reason for hiding this comment

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

Yup agreed on deprecated method in www/security

Comment on lines 548 to 571
from flask_appbuilder.security.sqla import models as sqla_models

from airflow.security.permissions import DAG_PERMS, prefixed_dag_id

def needs_perm_views(dag_id: str) -> bool:
view_menu_name = prefixed_dag_id(dag_id)
for permission_name in DAG_PERMS:
if not (
session.query(sqla_models.PermissionView)
.join(sqla_models.Permission)
.join(sqla_models.ViewMenu)
.filter(sqla_models.Permission.name == permission_name)
.filter(sqla_models.ViewMenu.name == view_menu_name)
.one_or_none()
):
return True
return False

if dag.access_control or needs_perm_views(dag.dag_id):
self.log.debug("Syncing DAG permissions: %s to the DB", dag.dag_id)
from airflow.www.security import ApplessAirflowSecurityManager

security_manager = ApplessAirflowSecurityManager(session=session)
security_manager.sync_perm_for_dag(dag.dag_id, dag.access_control)
Copy link
Member

Choose a reason for hiding this comment

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

Can we separate this code block to a separate internal method too -- will be easier and test just that block in isolation too

Copy link
Member

Choose a reason for hiding this comment

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

By this I mean the entire block from L548:L571

Copy link
Member Author

Choose a reason for hiding this comment

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

I refactored the tests in a separate commit. I can go both ways here.

jedcunningham and others added 3 commits April 21, 2021 15:44
Co-authored-by: Kaxil Naik <kaxilnaik@gmail.com>
@kaxil kaxil merged commit 3bfe0e0 into apache:master Apr 22, 2021
@kaxil kaxil deleted the faster_dag_perm_sync branch April 22, 2021 18:10
kaxil pushed a commit to astronomer/airflow that referenced this pull request Apr 26, 2021
For DAGs without `access_control` that already have their PermissionView
records, we can skip syncing their permissions during parsing. This cuts
down on database queries and is faster (~2 seconds, mostly import time).

(cherry picked from commit 3bfe0e0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Scheduler including HA (high availability) scheduler area:webserver Webserver related Issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants