-
Notifications
You must be signed in to change notification settings - Fork 17.3k
Parallelize per-dag auth checks in KeycloakAuthManager #69107
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
Open
Andrushika
wants to merge
2
commits into
apache:main
Choose a base branch
from
Andrushika:keycloak-parallelize-dag-auth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+23
−5
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| import time | ||
| import warnings | ||
| from base64 import urlsafe_b64decode | ||
| from concurrent.futures import ThreadPoolExecutor | ||
| from typing import TYPE_CHECKING, Any | ||
| from urllib.parse import urljoin | ||
|
|
||
|
|
@@ -34,6 +35,7 @@ | |
|
|
||
| from airflow.api_fastapi.app import AUTH_MANAGER_FASTAPI_APP_PREFIX | ||
| from airflow.api_fastapi.auth.managers.base_auth_manager import BaseAuthManager | ||
| from airflow.api_fastapi.auth.managers.models.resource_details import DagDetails | ||
| from airflow.exceptions import AirflowProviderDeprecationWarning | ||
|
|
||
| try: | ||
|
|
@@ -68,7 +70,6 @@ | |
| ConfigurationDetails, | ||
| ConnectionDetails, | ||
| DagAccessEntity, | ||
| DagDetails, | ||
| PoolDetails, | ||
| TeamDetails, | ||
| VariableDetails, | ||
|
|
@@ -457,10 +458,27 @@ def filter_authorized_dag_ids( | |
| cache_key = (user.get_id(), method, team_name, frozenset(dag_ids)) | ||
|
|
||
| def query_keycloak() -> set[str]: | ||
| kwargs: dict = dict(dag_ids=dag_ids, user=user, method=method) | ||
| if team_name is not None: | ||
| kwargs["team_name"] = team_name | ||
| return super(KeycloakAuthManager, self).filter_authorized_dag_ids(**kwargs) | ||
| if not dag_ids: | ||
| return set() | ||
|
|
||
| max_workers = min( | ||
| len(dag_ids), conf.getint(CONF_SECTION_NAME, CONF_REQUESTS_POOL_SIZE_KEY, fallback=10) | ||
| ) | ||
|
|
||
| def check(dag_id: str) -> tuple[str, bool]: | ||
| details_kwargs: dict[str, Any] = {"id": dag_id} | ||
| if team_name is not None: | ||
| details_kwargs["team_name"] = team_name | ||
| return dag_id, self.is_authorized_dag( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you looked at whether |
||
| method=method, | ||
| user=user, | ||
| details=DagDetails(**details_kwargs), | ||
| ) | ||
|
|
||
| with ThreadPoolExecutor(max_workers=max_workers) as executor: | ||
| results = executor.map(check, dag_ids) | ||
|
|
||
| return {dag_id for dag_id, authorized in results if authorized} | ||
|
|
||
| return single_flight(cache_key, query_keycloak) | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Using the number of DAGs as the upper bound for the number of threads makes sense. But could you clarify the rationale behind using CONF_REQUESTS_POOL_SIZE_KEY? Correct me if I am wrong but I believe you are using it because it is the limit for the size of the HTTP connection pool for Keycloak requests. It would not hurt to add a comment here to explain why you are using these 2 variables to determine
max_workers.