o-nikolas commented on code in PR #35488:
URL: https://github.com/apache/airflow/pull/35488#discussion_r1396269524


##########
airflow/providers/amazon/aws/auth_manager/aws_auth_manager.py:
##########
@@ -0,0 +1,145 @@
+# 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
+
+import warnings
+from functools import cached_property
+from typing import TYPE_CHECKING
+
+from flask import session, url_for
+
+from airflow.exceptions import AirflowException, 
AirflowOptionalProviderFeatureException
+from 
airflow.providers.amazon.aws.auth_manager.security_manager.aws_security_manager_override
 import (
+    AwsSecurityManagerOverride,
+)
+
+try:
+    from airflow.auth.managers.base_auth_manager import BaseAuthManager, 
ResourceMethod
+except ImportError:
+    raise AirflowOptionalProviderFeatureException(
+        "Failed to import BaseUser. This feature is only available in Airflow 
versions >= 2.8.0"
+    )
+
+if TYPE_CHECKING:
+    from airflow.auth.managers.models.base_user import BaseUser
+    from airflow.auth.managers.models.resource_details import (
+        AccessView,
+        ConfigurationDetails,
+        ConnectionDetails,
+        DagAccessEntity,
+        DagDetails,
+        DatasetDetails,
+        PoolDetails,
+        VariableDetails,
+    )
+    from airflow.providers.amazon.aws.auth_manager.user import 
AwsAuthManagerUser
+    from airflow.www.extensions.init_appbuilder import AirflowAppBuilder
+
+
+class AwsAuthManager(BaseAuthManager):
+    """
+    AWS auth manager.
+
+    Leverages AWS services such as Amazon Identity Center and Amazon Verified 
Permissions to perform
+    authentication and authorization in Airflow.
+
+    :param appbuilder: the flask app builder
+    """
+
+    def __init__(self, appbuilder: AirflowAppBuilder) -> None:
+        super().__init__(appbuilder)
+        warnings.warn(

Review Comment:
   Maybe throw a `NotImplementedError`?



##########
docs/docker-stack/changelog.rst:
##########
@@ -50,6 +50,9 @@ Airflow 2.7
 
 * 2.7.3
 
+  * Add ``libxmlsec1`` and ``libxmlsec1-dev`` libraries to dev PROD image and 
``libxmlsec1`` library to runtime PROD
+    image as it is required by ``python3-saml`` library.
+

Review Comment:
   Added to 2.7.3?? Shouldn't it be 2.8.0?



##########
airflow/providers/amazon/aws/auth_manager/aws_auth_manager.py:
##########
@@ -0,0 +1,145 @@
+# 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
+
+import warnings
+from functools import cached_property
+from typing import TYPE_CHECKING
+
+from flask import session, url_for
+
+from airflow.exceptions import AirflowException, 
AirflowOptionalProviderFeatureException
+from 
airflow.providers.amazon.aws.auth_manager.security_manager.aws_security_manager_override
 import (
+    AwsSecurityManagerOverride,
+)
+
+try:
+    from airflow.auth.managers.base_auth_manager import BaseAuthManager, 
ResourceMethod
+except ImportError:
+    raise AirflowOptionalProviderFeatureException(
+        "Failed to import BaseUser. This feature is only available in Airflow 
versions >= 2.8.0"
+    )
+
+if TYPE_CHECKING:
+    from airflow.auth.managers.models.base_user import BaseUser
+    from airflow.auth.managers.models.resource_details import (
+        AccessView,
+        ConfigurationDetails,
+        ConnectionDetails,
+        DagAccessEntity,
+        DagDetails,
+        DatasetDetails,
+        PoolDetails,
+        VariableDetails,
+    )
+    from airflow.providers.amazon.aws.auth_manager.user import 
AwsAuthManagerUser
+    from airflow.www.extensions.init_appbuilder import AirflowAppBuilder
+
+
+class AwsAuthManager(BaseAuthManager):
+    """
+    AWS auth manager.
+
+    Leverages AWS services such as Amazon Identity Center and Amazon Verified 
Permissions to perform
+    authentication and authorization in Airflow.
+
+    :param appbuilder: the flask app builder
+    """
+
+    def __init__(self, appbuilder: AirflowAppBuilder) -> None:
+        super().__init__(appbuilder)
+        warnings.warn(
+            "The AWS auth manager is currently being built. It is not 
finalized. "
+            "It is not intended to be used yet."
+        )
+
+    def get_user_name(self) -> str:
+        user = self.get_user()
+        if not user:
+            self.log.error("Calling 'get_user_name()' but the user is not 
signed in.")
+            raise AirflowException("The user must be signed in.")
+        return user.get_user_name()

Review Comment:
   Could this be on the base class like `get_user_id` is? Looks pretty generic



##########
airflow/providers/amazon/aws/auth_manager/aws_auth_manager.py:
##########
@@ -0,0 +1,145 @@
+# 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
+
+import warnings
+from functools import cached_property
+from typing import TYPE_CHECKING
+
+from flask import session, url_for
+
+from airflow.exceptions import AirflowException, 
AirflowOptionalProviderFeatureException
+from 
airflow.providers.amazon.aws.auth_manager.security_manager.aws_security_manager_override
 import (
+    AwsSecurityManagerOverride,
+)
+
+try:
+    from airflow.auth.managers.base_auth_manager import BaseAuthManager, 
ResourceMethod
+except ImportError:
+    raise AirflowOptionalProviderFeatureException(
+        "Failed to import BaseUser. This feature is only available in Airflow 
versions >= 2.8.0"
+    )
+
+if TYPE_CHECKING:
+    from airflow.auth.managers.models.base_user import BaseUser
+    from airflow.auth.managers.models.resource_details import (
+        AccessView,
+        ConfigurationDetails,
+        ConnectionDetails,
+        DagAccessEntity,
+        DagDetails,
+        DatasetDetails,
+        PoolDetails,
+        VariableDetails,
+    )
+    from airflow.providers.amazon.aws.auth_manager.user import 
AwsAuthManagerUser
+    from airflow.www.extensions.init_appbuilder import AirflowAppBuilder
+
+
+class AwsAuthManager(BaseAuthManager):
+    """
+    AWS auth manager.
+
+    Leverages AWS services such as Amazon Identity Center and Amazon Verified 
Permissions to perform
+    authentication and authorization in Airflow.
+
+    :param appbuilder: the flask app builder
+    """
+
+    def __init__(self, appbuilder: AirflowAppBuilder) -> None:
+        super().__init__(appbuilder)
+        warnings.warn(
+            "The AWS auth manager is currently being built. It is not 
finalized. "
+            "It is not intended to be used yet."
+        )
+
+    def get_user_name(self) -> str:
+        user = self.get_user()
+        if not user:
+            self.log.error("Calling 'get_user_name()' but the user is not 
signed in.")
+            raise AirflowException("The user must be signed in.")
+        return user.get_user_name()
+
+    def get_user(self) -> AwsAuthManagerUser | None:
+        return session["aws_user"] if self.is_logged_in() else None

Review Comment:
   Just for my own learning, where is `session` coming from?



##########
tests/providers/amazon/aws/auth_manager/views/test_auth.py:
##########
@@ -0,0 +1,146 @@
+# 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 unittest.mock import Mock, patch
+
+import pytest
+from flask import session, url_for
+
+from airflow.exceptions import AirflowException
+from airflow.www import app as application
+from tests.test_utils.config import conf_vars
+
+SAML_METADATA_URL = "/saml/metadata"
+SAML_METADATA_PARSED = {
+    "idp": {
+        "entityId": 
"https://portal.sso.us-east-1.amazonaws.com/saml/assertion/<assertion>",
+        "singleSignOnService": {
+            "url": 
"https://portal.sso.us-east-1.amazonaws.com/saml/assertion/<assertion>",
+            "binding": "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect",
+        },
+        "singleLogoutService": {
+            "url": 
"https://portal.sso.us-east-1.amazonaws.com/saml/logout/<assertion>",
+            "binding": "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect",
+        },
+        "x509cert": "<cert>",
+    },
+    "security": {"authnRequestsSigned": False},
+    "sp": {"NameIDFormat": 
"urn:oasis:names:tc:SAML:2.0:nameid-format:transient"},
+}
+
+
+@pytest.fixture()
+def aws_app():
+    def factory():
+        with conf_vars(
+            {
+                (
+                    "core",
+                    "auth_manager",
+                ): 
"airflow.providers.amazon.aws.auth_manager.aws_auth_manager.AwsAuthManager",
+                ("aws_auth_manager", "saml_metadata_url"): SAML_METADATA_URL,
+            }
+        ):
+            with patch(
+                
"airflow.providers.amazon.aws.auth_manager.views.auth.OneLogin_Saml2_IdPMetadataParser"
+            ) as mock_parser:
+                mock_parser.parse_remote.return_value = SAML_METADATA_PARSED
+                return application.create_app(testing=True)
+
+    return factory()
+
+
+@pytest.mark.db_test
+class TestAwsAuthManagerAuthenticationViews:

Review Comment:
   Nice tests!



##########
airflow/providers/amazon/provider.yaml:
##########
@@ -895,6 +896,20 @@ config:
         type: boolean
         example: "True"
         default: "True"
+  aws_auth_manager:
+    description: |
+      This section only applies if you are using the AwsAuthManager. In other 
words, if you set
+      ``[core] auth_manager = 
airflow.providers.amazon.aws.auth_manager.aws_auth_manager.AwsAuthManager`` in
+      Airflow's configuration.
+    options:
+      saml_metadata_url:
+        description: |
+          SAML metadata XML file provided by AWS Identity Center.
+          This URL can be found in the AWS Identity Center console. Required.
+        version_added: 10.0.0
+        type: string
+        example: ~

Review Comment:
   I think an example of what the url looks like would be helpful



##########
airflow/www/security_manager.py:
##########
@@ -355,3 +347,20 @@ def _is_authorized_category_menu(self, category: str) -> 
Callable:
         return lambda action, resource_pk, user: any(
             
self._get_auth_manager_is_authorized_method(fab_resource_name=item) for item in 
items
         )
+
+    """
+    The following methods are specific to FAB auth manager. They still need to 
be "present" in the main
+    security manager class, but they do nothing.
+    """
+
+    def get_action(self, name: str) -> Action:
+        raise NotImplementedError()
+
+    def get_resource(self, name: str) -> Resource:
+        raise NotImplementedError()
+
+    def add_permissions_view(self, base_action_names, resource_name):
+        pass

Review Comment:
   I'm curious, why `pass` here but `NotImplementedError` above?



##########
airflow/providers/amazon/aws/auth_manager/views/auth.py:
##########
@@ -0,0 +1,132 @@
+# 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
+
+import logging
+from functools import cached_property
+
+from flask import make_response, redirect, request, session, url_for
+from flask_appbuilder import expose
+from onelogin.saml2.auth import OneLogin_Saml2_Auth
+from onelogin.saml2.idp_metadata_parser import OneLogin_Saml2_IdPMetadataParser
+
+from airflow.configuration import conf
+from airflow.exceptions import AirflowException
+from airflow.providers.amazon.aws.auth_manager.constants import 
CONF_SAML_METADATA_URL_KEY, CONF_SECTION_NAME
+from airflow.providers.amazon.aws.auth_manager.user import AwsAuthManagerUser
+from airflow.www.app import csrf
+from airflow.www.views import AirflowBaseView
+
+
+class AwsAuthManagerAuthenticationViews(AirflowBaseView):
+    """
+    Views specific to AWS auth manager authentication mechanism.
+
+    Some code below is inspired from
+    
https://github.com/SAML-Toolkits/python3-saml/blob/6988bdab7a203abfe8dc264992f7e350c67aef3d/demo-flask/index.py
+    """
+
+    @cached_property
+    def idp_data(self) -> dict:
+        saml_metadata_url = conf.get_mandatory_value(CONF_SECTION_NAME, 
CONF_SAML_METADATA_URL_KEY)
+        return OneLogin_Saml2_IdPMetadataParser.parse_remote(saml_metadata_url)
+
+    @expose("/login")
+    def login(self):
+        """Start logging process."""

Review Comment:
   ```suggestion
           """Start login process."""
   ```



##########
airflow/providers/amazon/aws/auth_manager/views/auth.py:
##########
@@ -0,0 +1,132 @@
+# 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
+
+import logging
+from functools import cached_property
+
+from flask import make_response, redirect, request, session, url_for
+from flask_appbuilder import expose
+from onelogin.saml2.auth import OneLogin_Saml2_Auth
+from onelogin.saml2.idp_metadata_parser import OneLogin_Saml2_IdPMetadataParser
+
+from airflow.configuration import conf
+from airflow.exceptions import AirflowException
+from airflow.providers.amazon.aws.auth_manager.constants import 
CONF_SAML_METADATA_URL_KEY, CONF_SECTION_NAME
+from airflow.providers.amazon.aws.auth_manager.user import AwsAuthManagerUser
+from airflow.www.app import csrf
+from airflow.www.views import AirflowBaseView
+
+
+class AwsAuthManagerAuthenticationViews(AirflowBaseView):
+    """
+    Views specific to AWS auth manager authentication mechanism.
+
+    Some code below is inspired from
+    
https://github.com/SAML-Toolkits/python3-saml/blob/6988bdab7a203abfe8dc264992f7e350c67aef3d/demo-flask/index.py
+    """
+
+    @cached_property
+    def idp_data(self) -> dict:
+        saml_metadata_url = conf.get_mandatory_value(CONF_SECTION_NAME, 
CONF_SAML_METADATA_URL_KEY)
+        return OneLogin_Saml2_IdPMetadataParser.parse_remote(saml_metadata_url)
+
+    @expose("/login")
+    def login(self):
+        """Start logging process."""
+        saml_auth = self._init_saml_auth()
+        return redirect(saml_auth.login())
+
+    @expose("/logout")
+    def logout(self):
+        """Start logout process."""
+        session.clear()
+        saml_auth = self._init_saml_auth()
+
+        return redirect(saml_auth.logout())
+
+    @csrf.exempt
+    @expose("/login_callback", methods=("GET", "POST"))
+    def login_callback(self):
+        """Callback where the user is redirected to after successful login."""
+        saml_auth = self._init_saml_auth()
+        saml_auth.process_response()
+        errors = saml_auth.get_errors()
+        is_authenticated = saml_auth.is_authenticated()
+        if not is_authenticated:
+            error_reason = saml_auth.get_last_error_reason()
+            logging.error("Failed to authenticate")
+            logging.error("Errors: %s", errors)
+            logging.error("Error reason: %s", error_reason)
+            raise AirflowException(f"Failed to authenticate: {error_reason}")
+
+        attributes = saml_auth.get_attributes()
+        user = AwsAuthManagerUser(
+            user_id=attributes["id"][0],
+            groups=attributes["groups"],
+            username=saml_auth.get_nameid(),
+            email=attributes["email"][0],
+        )
+        session["aws_user"] = user
+
+        return redirect(url_for("Airflow.index"))
+
+    @expose("/login_metadata")
+    def login_metadata(self):
+        saml_auth = self._init_saml_auth()
+        settings = saml_auth.get_settings()
+        metadata = settings.get_sp_metadata()
+        errors = settings.validate_metadata(metadata)
+
+        if len(errors) == 0:
+            resp = make_response(metadata, 200)
+            resp.headers["Content-Type"] = "text/xml"
+        else:
+            resp = make_response(", ".join(errors), 500)
+        return resp
+
+    @staticmethod
+    def _prepare_flask_request() -> dict:
+        return {
+            "https": "on" if request.scheme == "https" else "off",
+            "http_host": request.host,
+            "script_name": request.path,
+            "get_data": request.args.copy(),
+            "post_data": request.form.copy(),
+        }
+
+    def _init_saml_auth(self) -> OneLogin_Saml2_Auth:
+        request_data = self._prepare_flask_request()
+        base_url = conf.get(section="webserver", key="base_url")
+        settings = {
+            # We want to keep this flag on in case of errors.
+            # It provides an error reasons, if turned off, it does not
+            "debug": True,
+            "sp": {
+                "entityId": f"{base_url}/login_metadata",
+                "assertionConsumerService": {
+                    "url": f"{base_url}/login_callback",
+                    "binding": 
"urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST",
+                },
+                "singleLogoutService": {
+                    "url": f"{base_url}/logout_callback",

Review Comment:
   I don't see login_callback defined above, where do we get that from?



##########
airflow/providers/amazon/provider.yaml:
##########
@@ -895,6 +896,20 @@ config:
         type: boolean
         example: "True"
         default: "True"
+  aws_auth_manager:
+    description: |
+      This section only applies if you are using the AwsAuthManager. In other 
words, if you set
+      ``[core] auth_manager = 
airflow.providers.amazon.aws.auth_manager.aws_auth_manager.AwsAuthManager`` in
+      Airflow's configuration.
+    options:
+      saml_metadata_url:
+        description: |
+          SAML metadata XML file provided by AWS Identity Center.
+          This URL can be found in the AWS Identity Center console. Required.
+        version_added: 10.0.0

Review Comment:
   Why v10? We are on 8.11 now



-- 
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-unsubscr...@airflow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to