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/07/25 18:29:08 UTC
[GitHub] [airflow] vincbeck opened a new pull request, #32838: Add `get_user` method in auth manager
vincbeck opened a new pull request, #32838:
URL: https://github.com/apache/airflow/pull/32838
Add `get_user` method in auth manager to load user information. I also added an interface `BaseUser` to define the schema of `User` returned by the different auth managers.
I also moved `airflow.www.fab_security.sqla.models` to `airflow.auth.managers.fab.models`.
<!--
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/
-->
---
**^ 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
[GitHub] [airflow] vincbeck commented on a diff in pull request #32838: Add `get_user` method in auth manager
Posted by "vincbeck (via GitHub)" <gi...@apache.org>.
vincbeck commented on code in PR #32838:
URL: https://github.com/apache/airflow/pull/32838#discussion_r1275446783
##########
airflow/auth/managers/base_auth_manager.py:
##########
@@ -42,6 +43,16 @@ def get_user_name(self) -> str:
"""Return the username associated to the user in session."""
...
+ @abstractmethod
+ def get_user(self) -> BaseUser:
+ """Return the user associated to the user in session."""
+ ...
+
+ @abstractmethod
+ def get_user_id(self) -> str:
+ """Return the user ID associated to the user in session."""
+ ...
Review Comment:
Yeah but we cannot assert it will be the case for all auth managers. Since forecasting is kind of hard, I rather have it as a string
--
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
[GitHub] [airflow] vincbeck commented on a diff in pull request #32838: Add `get_user` method in auth manager
Posted by "vincbeck (via GitHub)" <gi...@apache.org>.
vincbeck commented on code in PR #32838:
URL: https://github.com/apache/airflow/pull/32838#discussion_r1276332891
##########
airflow/auth/managers/model/base_user.py:
##########
@@ -0,0 +1,46 @@
+#
+# 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.
+from __future__ import annotations
+
+from abc import abstractmethod
+
+
+class BaseUser:
+ """
+ User model interface.
+
+ This is the base interface for all user models.
Review Comment:
I guess not :D Let me remove 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
[GitHub] [airflow] vincbeck commented on a diff in pull request #32838: Add `get_user` method in auth manager
Posted by "vincbeck (via GitHub)" <gi...@apache.org>.
vincbeck commented on code in PR #32838:
URL: https://github.com/apache/airflow/pull/32838#discussion_r1275043041
##########
airflow/api_connexion/endpoints/dag_run_endpoint.py:
##########
@@ -357,7 +357,7 @@ def post_dag_run(*, dag_id: str, session: Session = NEW_SESSION) -> APIResponse:
)
dag_run_note = post_body.get("note")
if dag_run_note:
- current_user_id = getattr(current_user, "id", None)
+ current_user_id = get_auth_manager().get_user().get_id()
Review Comment:
Sounds like a good point. Let me do that
--
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
[GitHub] [airflow] uranusjr merged pull request #32838: Add `get_user` method in auth manager
Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr merged PR #32838:
URL: https://github.com/apache/airflow/pull/32838
--
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
[GitHub] [airflow] uranusjr commented on a diff in pull request #32838: Add `get_user` method in auth manager
Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #32838:
URL: https://github.com/apache/airflow/pull/32838#discussion_r1274419030
##########
airflow/api_connexion/endpoints/dag_run_endpoint.py:
##########
@@ -357,7 +357,7 @@ def post_dag_run(*, dag_id: str, session: Session = NEW_SESSION) -> APIResponse:
)
dag_run_note = post_body.get("note")
if dag_run_note:
- current_user_id = getattr(current_user, "id", None)
+ current_user_id = get_auth_manager().get_user().get_id()
Review Comment:
I wonder if the interface should be `get_auth_manager().get_user_id()` instead. In many situations the ID can be retrieved very cheaply without building a concrete user object.
--
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
[GitHub] [airflow] vincbeck commented on a diff in pull request #32838: Add `get_user` method in auth manager
Posted by "vincbeck (via GitHub)" <gi...@apache.org>.
vincbeck commented on code in PR #32838:
URL: https://github.com/apache/airflow/pull/32838#discussion_r1275449387
##########
airflow/auth/managers/base_auth_manager.py:
##########
@@ -42,6 +43,16 @@ def get_user_name(self) -> str:
"""Return the username associated to the user in session."""
...
+ @abstractmethod
+ def get_user(self) -> BaseUser:
+ """Return the user associated to the user in session."""
+ ...
+
+ @abstractmethod
+ def get_user_id(self) -> str:
+ """Return the user ID associated to the user in session."""
+ ...
Review Comment:
Sounds good
--
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
[GitHub] [airflow] uranusjr commented on a diff in pull request #32838: Add `get_user` method in auth manager
Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #32838:
URL: https://github.com/apache/airflow/pull/32838#discussion_r1275441841
##########
airflow/auth/managers/base_auth_manager.py:
##########
@@ -42,6 +43,16 @@ def get_user_name(self) -> str:
"""Return the username associated to the user in session."""
...
+ @abstractmethod
+ def get_user(self) -> BaseUser:
+ """Return the user associated to the user in session."""
+ ...
+
+ @abstractmethod
+ def get_user_id(self) -> str:
+ """Return the user ID associated to the user in session."""
+ ...
Review Comment:
I think the ID is an int? At least that’s the case for Flask’s user model.
##########
airflow/auth/managers/base_auth_manager.py:
##########
@@ -42,6 +43,16 @@ def get_user_name(self) -> str:
"""Return the username associated to the user in session."""
...
+ @abstractmethod
+ def get_user(self) -> BaseUser:
+ """Return the user associated to the user in session."""
+ ...
+
+ @abstractmethod
+ def get_user_id(self) -> str:
+ """Return the user ID associated to the user in session."""
+ ...
Review Comment:
I think the ID is an int? At least that’s the case for Flask’s user model.
--
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
[GitHub] [airflow] ferruzzi commented on a diff in pull request #32838: Add `get_user` method in auth manager
Posted by "ferruzzi (via GitHub)" <gi...@apache.org>.
ferruzzi commented on code in PR #32838:
URL: https://github.com/apache/airflow/pull/32838#discussion_r1275547197
##########
airflow/auth/managers/model/base_user.py:
##########
@@ -0,0 +1,46 @@
+#
+# 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.
+from __future__ import annotations
+
+from abc import abstractmethod
+
+
+class BaseUser:
+ """
+ User model interface.
+
+ This is the base interface for all user models.
Review Comment:
Smallest of nitpicks, is that second line really necessary?
--
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
[GitHub] [airflow] uranusjr commented on a diff in pull request #32838: Add `get_user` method in auth manager
Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #32838:
URL: https://github.com/apache/airflow/pull/32838#discussion_r1275448579
##########
airflow/auth/managers/base_auth_manager.py:
##########
@@ -42,6 +43,16 @@ def get_user_name(self) -> str:
"""Return the username associated to the user in session."""
...
+ @abstractmethod
+ def get_user(self) -> BaseUser:
+ """Return the user associated to the user in session."""
+ ...
+
+ @abstractmethod
+ def get_user_id(self) -> str:
+ """Return the user ID associated to the user in session."""
+ ...
Review Comment:
Then do we want to do a `str()` cast in the subclass so it actually returns a str? Right now I think the returned value would be an int and not match the annotation.
--
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
[GitHub] [airflow] vincbeck commented on a diff in pull request #32838: Add `get_user` method in auth manager
Posted by "vincbeck (via GitHub)" <gi...@apache.org>.
vincbeck commented on code in PR #32838:
URL: https://github.com/apache/airflow/pull/32838#discussion_r1275051610
##########
airflow/auth/managers/fab/fab_auth_manager.py:
##########
@@ -39,13 +40,19 @@ def get_user_name(self) -> str:
For backward compatibility reasons, the username in FAB auth manager is the concatenation of the
first name and the last name.
"""
- first_name = current_user.first_name or ""
- last_name = current_user.last_name or ""
+ user = self.get_user()
+ first_name = user.first_name or ""
+ last_name = user.last_name or ""
return f"{first_name} {last_name}".strip()
+ def get_user(self) -> User:
+ """Return the user associated to the user in session."""
+ return current_user
+
def is_logged_in(self) -> bool:
"""Return whether the user is logged in."""
- return current_user and not current_user.is_anonymous
+ user = self.get_user()
+ return user and not user.is_anonymous
Review Comment:
Nop, let me fix that
--
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
[GitHub] [airflow] uranusjr commented on a diff in pull request #32838: Add `get_user` method in auth manager
Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #32838:
URL: https://github.com/apache/airflow/pull/32838#discussion_r1274419762
##########
airflow/auth/managers/fab/fab_auth_manager.py:
##########
@@ -39,13 +40,19 @@ def get_user_name(self) -> str:
For backward compatibility reasons, the username in FAB auth manager is the concatenation of the
first name and the last name.
"""
- first_name = current_user.first_name or ""
- last_name = current_user.last_name or ""
+ user = self.get_user()
+ first_name = user.first_name or ""
+ last_name = user.last_name or ""
return f"{first_name} {last_name}".strip()
+ def get_user(self) -> User:
+ """Return the user associated to the user in session."""
+ return current_user
+
def is_logged_in(self) -> bool:
"""Return whether the user is logged in."""
- return current_user and not current_user.is_anonymous
+ user = self.get_user()
+ return user and not user.is_anonymous
Review Comment:
```suggestion
return not user.is_anonymous
```
Can the user ever be falsy…?
##########
airflow/auth/managers/fab/fab_auth_manager.py:
##########
@@ -39,13 +40,19 @@ def get_user_name(self) -> str:
For backward compatibility reasons, the username in FAB auth manager is the concatenation of the
first name and the last name.
"""
- first_name = current_user.first_name or ""
- last_name = current_user.last_name or ""
+ user = self.get_user()
+ first_name = user.first_name or ""
+ last_name = user.last_name or ""
return f"{first_name} {last_name}".strip()
+ def get_user(self) -> User:
+ """Return the user associated to the user in session."""
+ return current_user
+
def is_logged_in(self) -> bool:
"""Return whether the user is logged in."""
- return current_user and not current_user.is_anonymous
+ user = self.get_user()
+ return user and not user.is_anonymous
Review Comment:
```suggestion
return not user.is_anonymous
```
Can the user ever be falsy…?
--
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