You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "vincbeck (via GitHub)" <gi...@apache.org> on 2023/10/18 19:52:49 UTC
[PR] Move authentication related methods to FAB auth manager [airflow]
vincbeck opened a new pull request, #35038:
URL: https://github.com/apache/airflow/pull/35038
One of the many PR which move stuff related to FAB user management from security manager to auth manager.
Code only being moved here.
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<!--
Thank you for contributing! Please make sure that your code changes
are covered with tests. And in case of new features or big changes
remember to adjust the documentation.
Feel free to ping committers for the review!
In case of an existing issue, reference it using one of the following:
closes: #ISSUE
related: #ISSUE
How to write a good git commit message:
http://chris.beams.io/posts/git-commit/
-->
<!-- Please keep an empty line above the dashes. -->
---
**^ Add meaningful description above**
Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/CONTRIBUTING.rst#pull-request-guidelines)** for more information.
In case of fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed.
In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
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](https://github.com/apache/airflow/tree/main/newsfragments).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Move authentication related methods to FAB auth manager [airflow]
Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #35038:
URL: https://github.com/apache/airflow/pull/35038#discussion_r1368303263
##########
airflow/auth/managers/fab/security_manager/override.py:
##########
@@ -103,6 +112,16 @@ class FabAirflowSecurityManagerOverride(AirflowSecurityManagerV2):
permission_model = Permission
registeruser_model = RegisterUser
+ """ Flask-JWT-Extended """
+ jwt_manager = None
+ """ Flask-OpenID OpenID """
+ oid = None
+ """ OAuth email whitelists """
+ oauth = None
+ """ OAuth email whitelists """
+ oauth_remotes: dict[str, Any]
+ oauth_user_info = None
Review Comment:
I believe the convention is to put docstrings _below_ attributes but I also see there are already other variables doing this…
https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Move authentication related methods to FAB auth manager [airflow]
Posted by "vincbeck (via GitHub)" <gi...@apache.org>.
vincbeck merged PR #35038:
URL: https://github.com/apache/airflow/pull/35038
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Move authentication related methods to FAB auth manager [airflow]
Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #35038:
URL: https://github.com/apache/airflow/pull/35038#discussion_r1368303263
##########
airflow/auth/managers/fab/security_manager/override.py:
##########
@@ -103,6 +112,16 @@ class FabAirflowSecurityManagerOverride(AirflowSecurityManagerV2):
permission_model = Permission
registeruser_model = RegisterUser
+ """ Flask-JWT-Extended """
+ jwt_manager = None
+ """ Flask-OpenID OpenID """
+ oid = None
+ """ OAuth email whitelists """
+ oauth = None
+ """ OAuth email whitelists """
+ oauth_remotes: dict[str, Any]
+ oauth_user_info = None
Review Comment:
I believe the convention is to put docstrings _below_ attributes
https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Move authentication related methods to FAB auth manager [airflow]
Posted by "vincbeck (via GitHub)" <gi...@apache.org>.
vincbeck commented on code in PR #35038:
URL: https://github.com/apache/airflow/pull/35038#discussion_r1369048804
##########
airflow/auth/managers/fab/security_manager/override.py:
##########
@@ -103,6 +112,16 @@ class FabAirflowSecurityManagerOverride(AirflowSecurityManagerV2):
permission_model = Permission
registeruser_model = RegisterUser
+ """ Flask-JWT-Extended """
+ jwt_manager = None
+ """ Flask-OpenID OpenID """
+ oid = None
+ """ OAuth email whitelists """
+ oauth = None
+ """ OAuth email whitelists """
+ oauth_remotes: dict[str, Any]
+ oauth_user_info = None
Review Comment:
Done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Move authentication related methods to FAB auth manager [airflow]
Posted by "vincbeck (via GitHub)" <gi...@apache.org>.
vincbeck commented on code in PR #35038:
URL: https://github.com/apache/airflow/pull/35038#discussion_r1368982785
##########
airflow/auth/managers/fab/security_manager/override.py:
##########
@@ -103,6 +112,16 @@ class FabAirflowSecurityManagerOverride(AirflowSecurityManagerV2):
permission_model = Permission
registeruser_model = RegisterUser
+ """ Flask-JWT-Extended """
+ jwt_manager = None
+ """ Flask-OpenID OpenID """
+ oid = None
+ """ OAuth email whitelists """
+ oauth = None
+ """ OAuth email whitelists """
+ oauth_remotes: dict[str, Any]
+ oauth_user_info = None
Review Comment:
My bad, I'll fix it
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Move authentication related methods to FAB auth manager [airflow]
Posted by "vincbeck (via GitHub)" <gi...@apache.org>.
vincbeck commented on PR #35038:
URL: https://github.com/apache/airflow/pull/35038#issuecomment-1775716918
> Rebase needed.
Done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Move authentication related methods to FAB auth manager [airflow]
Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #35038:
URL: https://github.com/apache/airflow/pull/35038#discussion_r1369679932
##########
airflow/auth/managers/fab/security_manager/override.py:
##########
@@ -1650,3 +1950,82 @@ def _ldap_calculate_user_roles(self, user_attributes: dict[str, list[bytes]]) ->
log.warning("Can't find AUTH_USER_REGISTRATION role: %s", registration_role_name)
return list(user_role_objects)
+
+ def _oauth_calculate_user_roles(self, userinfo) -> list[str]:
+ user_role_objects = set()
+
+ # apply AUTH_ROLES_MAPPING
+ if self.auth_roles_mapping:
+ user_role_keys = userinfo.get("role_keys", [])
+ user_role_objects.update(self.get_roles_from_keys(user_role_keys))
+
+ # apply AUTH_USER_REGISTRATION_ROLE
+ if self.auth_user_registration:
+ registration_role_name = self.auth_user_registration_role
+
+ # if AUTH_USER_REGISTRATION_ROLE_JMESPATH is set,
+ # use it for the registration role
+ if self.auth_user_registration_role_jmespath:
+ import jmespath
+
+ registration_role_name = jmespath.search(self.auth_user_registration_role_jmespath, userinfo)
+
+ # lookup registration role in flask db
+ fab_role = self.find_role(registration_role_name)
+ if fab_role:
+ user_role_objects.add(fab_role)
+ else:
+ log.warning("Can't find AUTH_USER_REGISTRATION role: %s", registration_role_name)
+
+ return list(user_role_objects)
+
+ def _get_user_permission_resources(
+ self, user: User | None, action_name: str, resource_names: list[str] | None = None
+ ) -> set[str]:
+ """Get resource names with a certain action name that a user has access to.
+
+ Mainly used to fetch all menu permissions on a single db call, will also
+ check public permissions and builtin roles
+ """
+ if not resource_names:
+ resource_names = []
+
+ db_role_ids = []
+ if user is None:
+ # include public role
+ roles = [self.get_public_role()]
+ else:
+ roles = user.roles
+ # First check against builtin (statically configured) roles
+ # because no database query is needed
+ result = set()
+ for role in roles:
+ if role.name in self.builtin_roles:
+ for resource_name in resource_names:
+ if self._has_access_builtin_roles(role, action_name, resource_name):
+ result.add(resource_name)
+ else:
+ db_role_ids.append(role.id)
+ # Then check against database-stored roles
+ role_resource_names = [
+ perm.resource.name for perm in self.filter_roles_by_perm_with_action(action_name, db_role_ids)
+ ]
+ result.update(role_resource_names)
+ return result
+
+ def filter_roles_by_perm_with_action(self, action_name: str, role_ids: list[int]):
+ """Find roles with permission."""
+ return (
+ self.appbuilder.get_session.query(self.permission_model)
Review Comment:
Should we change this to be compatible with SQLALchemy 2?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Move authentication related methods to FAB auth manager [airflow]
Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on PR #35038:
URL: https://github.com/apache/airflow/pull/35038#issuecomment-1774213685
Rebase needed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Move authentication related methods to FAB auth manager [airflow]
Posted by "vincbeck (via GitHub)" <gi...@apache.org>.
vincbeck commented on code in PR #35038:
URL: https://github.com/apache/airflow/pull/35038#discussion_r1370347338
##########
airflow/auth/managers/fab/security_manager/override.py:
##########
@@ -1650,3 +1950,82 @@ def _ldap_calculate_user_roles(self, user_attributes: dict[str, list[bytes]]) ->
log.warning("Can't find AUTH_USER_REGISTRATION role: %s", registration_role_name)
return list(user_role_objects)
+
+ def _oauth_calculate_user_roles(self, userinfo) -> list[str]:
+ user_role_objects = set()
+
+ # apply AUTH_ROLES_MAPPING
+ if self.auth_roles_mapping:
+ user_role_keys = userinfo.get("role_keys", [])
+ user_role_objects.update(self.get_roles_from_keys(user_role_keys))
+
+ # apply AUTH_USER_REGISTRATION_ROLE
+ if self.auth_user_registration:
+ registration_role_name = self.auth_user_registration_role
+
+ # if AUTH_USER_REGISTRATION_ROLE_JMESPATH is set,
+ # use it for the registration role
+ if self.auth_user_registration_role_jmespath:
+ import jmespath
+
+ registration_role_name = jmespath.search(self.auth_user_registration_role_jmespath, userinfo)
+
+ # lookup registration role in flask db
+ fab_role = self.find_role(registration_role_name)
+ if fab_role:
+ user_role_objects.add(fab_role)
+ else:
+ log.warning("Can't find AUTH_USER_REGISTRATION role: %s", registration_role_name)
+
+ return list(user_role_objects)
+
+ def _get_user_permission_resources(
+ self, user: User | None, action_name: str, resource_names: list[str] | None = None
+ ) -> set[str]:
+ """Get resource names with a certain action name that a user has access to.
+
+ Mainly used to fetch all menu permissions on a single db call, will also
+ check public permissions and builtin roles
+ """
+ if not resource_names:
+ resource_names = []
+
+ db_role_ids = []
+ if user is None:
+ # include public role
+ roles = [self.get_public_role()]
+ else:
+ roles = user.roles
+ # First check against builtin (statically configured) roles
+ # because no database query is needed
+ result = set()
+ for role in roles:
+ if role.name in self.builtin_roles:
+ for resource_name in resource_names:
+ if self._has_access_builtin_roles(role, action_name, resource_name):
+ result.add(resource_name)
+ else:
+ db_role_ids.append(role.id)
+ # Then check against database-stored roles
+ role_resource_names = [
+ perm.resource.name for perm in self.filter_roles_by_perm_with_action(action_name, db_role_ids)
+ ]
+ result.update(role_resource_names)
+ return result
+
+ def filter_roles_by_perm_with_action(self, action_name: str, role_ids: list[int]):
+ """Find roles with permission."""
+ return (
+ self.appbuilder.get_session.query(self.permission_model)
Review Comment:
Let's do it in another PR. Some other code might not be compatible with SQLALchemy 2, so let's create another PR for that purpose
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org