You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "seyoon-lim (via GitHub)" <gi...@apache.org> on 2024/07/12 17:10:14 UTC

[PR] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

seyoon-lim opened a new pull request, #40757:
URL: https://github.com/apache/airflow/pull/40757

   <!--
    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/
   -->
   closes: https://github.com/apache/airflow/issues/40749
   
   
   <!-- 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-docs/05_pull_requests.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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "seyoon-lim (via GitHub)" <gi...@apache.org>.
seyoon-lim commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1747780969


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -370,10 +448,12 @@ def _build_spark_submit_command(self, application: str) -> list[str]:
             connection_cmd += ["--executor-memory", self._executor_memory]
         if self._driver_memory:
             connection_cmd += ["--driver-memory", self._driver_memory]
-        if self._keytab:
-            connection_cmd += ["--keytab", self._keytab]
-        if self._principal:
-            connection_cmd += ["--principal", self._principal]
+        keytab = self._get_keytab()
+        if keytab:
+            connection_cmd += ["--keytab", keytab]
+        principal = self._get_principal()

Review Comment:
   This part was added because the `_resolve_connection` method might throw an exception(before [here](https://github.com/apache/airflow/pull/40757/files/b27cd4f12a86d2f0bb20720b8fd0c68ed6a5573a#diff-aa05e2f3d2472084951026f8344dbc7134cb1e3f94d626127ab9dee50fbde2c6R294)) if the principal and keytab of conn_data are assigned as None, as seen [here](https://github.com/apache/airflow/pull/40757/files/b27cd4f12a86d2f0bb20720b8fd0c68ed6a5573a#diff-aa05e2f3d2472084951026f8344dbc7134cb1e3f94d626127ab9dee50fbde2c6R261-R262)
   
   ~~However, now that I think about it, setting the default values of conn_data to self._keytab and self._principal might resolve this issue as per your feedback.
   This seems to be a matter of choice, so if you think it needs to be changed, please let me know and I'll make the adjustments!~~



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "nevcohen (via GitHub)" <gi...@apache.org>.
nevcohen commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1742450126


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -282,10 +313,57 @@ def _resolve_connection(self) -> dict[str, Any]:
     def get_conn(self) -> Any:
         pass
 
+    def _create_keytab_path_from_base64_keytab(self, base64_keytab: str, principal: str | None) -> str:
+        _uuid = uuid.uuid4()
+        temp_dir_path = Path(tempfile.gettempdir()).resolve()
+        temp_file_name = f"airflow_keytab-{principal or _uuid}"
+
+        keytab_path = temp_dir_path / temp_file_name
+        staging_path = temp_dir_path / f".{temp_file_name}.{_uuid}"
+
+        try:
+            keytab = base64.b64decode(base64_keytab)
+        except Exception as err:

Review Comment:
   Great!



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "nevcohen (via GitHub)" <gi...@apache.org>.
nevcohen commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1741112895


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -282,10 +313,57 @@ def _resolve_connection(self) -> dict[str, Any]:
     def get_conn(self) -> Any:
         pass
 
+    def _create_keytab_path_from_base64_keytab(self, base64_keytab: str, principal: str | None) -> str:
+        _uuid = uuid.uuid4()
+        temp_dir_path = Path(tempfile.gettempdir()).resolve()
+        temp_file_name = f"airflow_keytab-{principal or _uuid}"
+
+        keytab_path = temp_dir_path / temp_file_name
+        staging_path = temp_dir_path / f".{temp_file_name}.{_uuid}"
+
+        try:
+            keytab = base64.b64decode(base64_keytab)
+        except Exception as err:
+            self.log.error("Failed to decode base64 keytab: %s", err)
+            raise AirflowException("Failed to decode base64 keytab") from err
+
+        # If a keytab file with the same content exists, return its path for the --keytab argument.
+        if keytab_path.exists():
+            with open(keytab_path, "rb") as f:
+                existing_keytab = f.read()
+            if existing_keytab == keytab:
+                self.log.info("Keytab file already exists and is the same as the provided keytab")
+                return str(keytab_path)
+
+        # Generate a new keytab file from the base64 encoded keytab value to use as a --keytab argument.
+        try:
+            with open(staging_path, "wb") as f:
+                self.log.info("Saving keytab to %s", staging_path)
+                f.write(keytab)
+
+            self.log.info("Moving keytab from %s to %s", staging_path, keytab_path)
+            shutil.move(str(staging_path), str(keytab_path))
+            return str(keytab_path)
+        except Exception as err:

Review Comment:
   Also what with add test to this exception? What if it fails to write to the file or copy it with `shutil.move` we don't want there to be a test on 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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "seyoon-lim (via GitHub)" <gi...@apache.org>.
seyoon-lim commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1747780969


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -370,10 +448,12 @@ def _build_spark_submit_command(self, application: str) -> list[str]:
             connection_cmd += ["--executor-memory", self._executor_memory]
         if self._driver_memory:
             connection_cmd += ["--driver-memory", self._driver_memory]
-        if self._keytab:
-            connection_cmd += ["--keytab", self._keytab]
-        if self._principal:
-            connection_cmd += ["--principal", self._principal]
+        keytab = self._get_keytab()
+        if keytab:
+            connection_cmd += ["--keytab", keytab]
+        principal = self._get_principal()

Review Comment:
   This part was added because the `_resolve_connection` method might throw an exception(before [here](https://github.com/apache/airflow/pull/40757/files/b27cd4f12a86d2f0bb20720b8fd0c68ed6a5573a#diff-aa05e2f3d2472084951026f8344dbc7134cb1e3f94d626127ab9dee50fbde2c6R294)) if the principal and keytab of conn_data are assigned as None, as seen [here](https://github.com/apache/airflow/pull/40757/files/b27cd4f12a86d2f0bb20720b8fd0c68ed6a5573a#diff-aa05e2f3d2472084951026f8344dbc7134cb1e3f94d626127ab9dee50fbde2c6R261-R262)
   
   However, now that I think about it, setting the default values of conn_data to self._keytab and self._principal might resolve this issue as per your feedback.
   This seems to be a matter of choice, so if you think it needs to be changed, please let me know and I'll make the adjustments!



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "seyoon-lim (via GitHub)" <gi...@apache.org>.
seyoon-lim commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1761192669


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -370,10 +448,12 @@ def _build_spark_submit_command(self, application: str) -> list[str]:
             connection_cmd += ["--executor-memory", self._executor_memory]
         if self._driver_memory:
             connection_cmd += ["--driver-memory", self._driver_memory]
-        if self._keytab:
-            connection_cmd += ["--keytab", self._keytab]
-        if self._principal:
-            connection_cmd += ["--principal", self._principal]
+        keytab = self._get_keytab()
+        if keytab:
+            connection_cmd += ["--keytab", keytab]
+        principal = self._get_principal()

Review Comment:
   @romsharon98 I have rolled back this logic due to the following reason from https://github.com/apache/airflow/pull/40757#discussion_r1747780969
   
   The test fails([link](https://github.com/apache/airflow/actions/runs/10883694059/job/30201008840)) when an error is raised from _resolve_connection. In this case, I need to return self._principal(self._keytab) instead of `self._connection["principal"]`(`self._connection["keytab"]`).
   
   so I set the default value on `conn_data` (https://github.com/apache/airflow/pull/40757/commits/01b22d41b0ee3a7e36afa4348660988ff515c418) thanks!



##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -370,10 +448,12 @@ def _build_spark_submit_command(self, application: str) -> list[str]:
             connection_cmd += ["--executor-memory", self._executor_memory]
         if self._driver_memory:
             connection_cmd += ["--driver-memory", self._driver_memory]
-        if self._keytab:
-            connection_cmd += ["--keytab", self._keytab]
-        if self._principal:
-            connection_cmd += ["--principal", self._principal]
+        keytab = self._get_keytab()
+        if keytab:
+            connection_cmd += ["--keytab", keytab]
+        principal = self._get_principal()

Review Comment:
   @romsharon98 I have rolled back this logic due to the following reason from https://github.com/apache/airflow/pull/40757#discussion_r1747780969
   
   The test fails([link](https://github.com/apache/airflow/actions/runs/10883694059/job/30201008840)) when an error is raised from _resolve_connection. In this case, I need to return self._principal(self._keytab) instead of `self._connection["principal"]`(`self._connection["keytab"]`).
   
   so I set the default value on `conn_data` (https://github.com/apache/airflow/pull/40757/commits/01b22d41b0ee3a7e36afa4348660988ff515c418)
   thanks!



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "seyoon-lim (via GitHub)" <gi...@apache.org>.
seyoon-lim commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1677189547


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -281,10 +310,57 @@ def _resolve_connection(self) -> dict[str, Any]:
     def get_conn(self) -> Any:
         pass
 
+    def _get_keytab_from_base64(self, base64_keytab: str, principal: str | None) -> str:
+        _uuid = uuid.uuid4()
+        temp_dir_path = Path(tempfile.gettempdir()).resolve()
+        temp_file_name = f"airflow_keytab-{principal or _uuid}"
+
+        keytab_path = temp_dir_path / temp_file_name
+        staging_path = temp_dir_path / f".{temp_file_name}.{_uuid}"
+
+        try:
+            keytab = base64.b64decode(base64_keytab)
+        except Exception as err:
+            self.log.error("Failed to decode base64 keytab: %s", err)
+            raise AirflowException("Failed to decode base64 keytab") from err
+
+        # validate exists keytab file
+        if keytab_path.exists():

Review Comment:
   This logic exists to prevent writing the same file in the same execution environment each time.



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "nevcohen (via GitHub)" <gi...@apache.org>.
nevcohen commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1678103525


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -281,10 +310,57 @@ def _resolve_connection(self) -> dict[str, Any]:
     def get_conn(self) -> Any:
         pass
 
+    def _get_keytab_from_base64(self, base64_keytab: str, principal: str | None) -> str:
+        _uuid = uuid.uuid4()
+        temp_dir_path = Path(tempfile.gettempdir()).resolve()
+        temp_file_name = f"airflow_keytab-{principal or _uuid}"
+
+        keytab_path = temp_dir_path / temp_file_name
+        staging_path = temp_dir_path / f".{temp_file_name}.{_uuid}"
+
+        try:
+            keytab = base64.b64decode(base64_keytab)
+        except Exception as err:
+            self.log.error("Failed to decode base64 keytab: %s", err)
+            raise AirflowException("Failed to decode base64 keytab") from err
+
+        # validate exists keytab file
+        if keytab_path.exists():

Review Comment:
   So why do you move the file from staging to `keytab_path`?



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "seyoon-lim (via GitHub)" <gi...@apache.org>.
seyoon-lim commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1678735972


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -281,10 +310,57 @@ def _resolve_connection(self) -> dict[str, Any]:
     def get_conn(self) -> Any:
         pass
 
+    def _get_keytab_from_base64(self, base64_keytab: str, principal: str | None) -> str:

Review Comment:
   Firstly, I have changed the function name. Thank you for the suggestions.



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "seyoon-lim (via GitHub)" <gi...@apache.org>.
seyoon-lim commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1678489823


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -281,10 +310,57 @@ def _resolve_connection(self) -> dict[str, Any]:
     def get_conn(self) -> Any:
         pass
 
+    def _get_keytab_from_base64(self, base64_keytab: str, principal: str | None) -> str:
+        _uuid = uuid.uuid4()
+        temp_dir_path = Path(tempfile.gettempdir()).resolve()
+        temp_file_name = f"airflow_keytab-{principal or _uuid}"
+
+        keytab_path = temp_dir_path / temp_file_name
+        staging_path = temp_dir_path / f".{temp_file_name}.{_uuid}"
+
+        try:
+            keytab = base64.b64decode(base64_keytab)
+        except Exception as err:
+            self.log.error("Failed to decode base64 keytab: %s", err)
+            raise AirflowException("Failed to decode base64 keytab") from err
+
+        # validate exists keytab file
+        if keytab_path.exists():

Review Comment:
   After considering the scenarios where a read might occur during a write operation or a write might fail, I thought that it would be safest to write to a staging file successfully first and then move it.
   
   Additionally, the cases where writing occurs are when the keytab is not present at the path or when the contents of the keytab at the path have changed.



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "seyoon-lim (via GitHub)" <gi...@apache.org>.
seyoon-lim commented on PR #40757:
URL: https://github.com/apache/airflow/pull/40757#issuecomment-2324549763

   > Cool. Can you address the comments left by @nevcohen ? If issues are resooved then please resolve the threads so we know it was addressed
   
   I'm not exactly sure what changes are needed for the section in [this comment](https://github.com/apache/airflow/pull/40757#discussion_r1677180143), but I believe I have addressed all the other comments. Thank you for letting me know!


-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "seyoon-lim (via GitHub)" <gi...@apache.org>.
seyoon-lim commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1761192669


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -370,10 +448,12 @@ def _build_spark_submit_command(self, application: str) -> list[str]:
             connection_cmd += ["--executor-memory", self._executor_memory]
         if self._driver_memory:
             connection_cmd += ["--driver-memory", self._driver_memory]
-        if self._keytab:
-            connection_cmd += ["--keytab", self._keytab]
-        if self._principal:
-            connection_cmd += ["--principal", self._principal]
+        keytab = self._get_keytab()
+        if keytab:
+            connection_cmd += ["--keytab", keytab]
+        principal = self._get_principal()

Review Comment:
   @romsharon98 I have rolled back this logic due to the following reason from https://github.com/apache/airflow/pull/40757#discussion_r1747780969
   
   The test fails([link](https://github.com/apache/airflow/actions/runs/10883694059/job/30201008840)) when an error is raised from _resolve_connection. In this case, I need to return self._principal(self._keytab) instead of `self._connection["principal"]`(`self._connection["keytab"]`).
   
   so I set the default value on `conn_data` (https://github.com/apache/airflow/pull/40757/commits/01b22d41b0ee3a7e36afa4348660988ff515c418)



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "seyoon-lim (via GitHub)" <gi...@apache.org>.
seyoon-lim commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1742312400


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -282,10 +313,57 @@ def _resolve_connection(self) -> dict[str, Any]:
     def get_conn(self) -> Any:
         pass
 
+    def _create_keytab_path_from_base64_keytab(self, base64_keytab: str, principal: str | None) -> str:
+        _uuid = uuid.uuid4()
+        temp_dir_path = Path(tempfile.gettempdir()).resolve()
+        temp_file_name = f"airflow_keytab-{principal or _uuid}"
+
+        keytab_path = temp_dir_path / temp_file_name
+        staging_path = temp_dir_path / f".{temp_file_name}.{_uuid}"
+
+        try:
+            keytab = base64.b64decode(base64_keytab)
+        except Exception as err:

Review Comment:
   https://github.com/apache/airflow/pull/40757/commits/912cab06280d8b6fa42279764cc28d9a5bda79ca 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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "seyoon-lim (via GitHub)" <gi...@apache.org>.
seyoon-lim commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1747798708


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -370,10 +448,12 @@ def _build_spark_submit_command(self, application: str) -> list[str]:
             connection_cmd += ["--executor-memory", self._executor_memory]
         if self._driver_memory:
             connection_cmd += ["--driver-memory", self._driver_memory]
-        if self._keytab:
-            connection_cmd += ["--keytab", self._keytab]
-        if self._principal:
-            connection_cmd += ["--principal", self._principal]
+        keytab = self._get_keytab()
+        if keytab:
+            connection_cmd += ["--keytab", keytab]
+        principal = self._get_principal()

Review Comment:
   Looking at [this part](https://github.com/apache/airflow/pull/40757/files#diff-aa05e2f3d2472084951026f8344dbc7134cb1e3f94d626127ab9dee50fbde2c6R461-R464), it seems you're right about making the change. I've corrected it (https://github.com/apache/airflow/pull/40757/commits/55007bc3401eb42bff86dfdd4cb442c0e66c4f4b). Thanks!



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "nevcohen (via GitHub)" <gi...@apache.org>.
nevcohen commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1680492196


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -281,10 +310,57 @@ def _resolve_connection(self) -> dict[str, Any]:
     def get_conn(self) -> Any:
         pass
 
+    def _get_keytab_from_base64(self, base64_keytab: str, principal: str | None) -> str:
+        _uuid = uuid.uuid4()
+        temp_dir_path = Path(tempfile.gettempdir()).resolve()
+        temp_file_name = f"airflow_keytab-{principal or _uuid}"
+
+        keytab_path = temp_dir_path / temp_file_name
+        staging_path = temp_dir_path / f".{temp_file_name}.{_uuid}"
+
+        try:
+            keytab = base64.b64decode(base64_keytab)
+        except Exception as err:
+            self.log.error("Failed to decode base64 keytab: %s", err)
+            raise AirflowException("Failed to decode base64 keytab") from err
+
+        # validate exists keytab file
+        if keytab_path.exists():

Review Comment:
   Got it, so maybe just add to the comment `#write new keytab file`, why you're creating a new keytab.



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "nevcohen (via GitHub)" <gi...@apache.org>.
nevcohen commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1678105236


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -281,10 +310,57 @@ def _resolve_connection(self) -> dict[str, Any]:
     def get_conn(self) -> Any:
         pass
 
+    def _get_keytab_from_base64(self, base64_keytab: str, principal: str | None) -> str:

Review Comment:
   OK, waiting for action



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "seyoon-lim (via GitHub)" <gi...@apache.org>.
seyoon-lim commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1677189342


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -281,10 +310,57 @@ def _resolve_connection(self) -> dict[str, Any]:
     def get_conn(self) -> Any:
         pass
 
+    def _get_keytab_from_base64(self, base64_keytab: str, principal: str | None) -> str:

Review Comment:
   yeah, it actually does `get_or_create_keytab_file_path_from_base64_encoded_keytab_value` .
   but in `spark_submit`, the keytab variable refers to the keytab path, which might be confusing.
   
   actually, this function is not strictly dependent on `spark_submit.py`, so I'm considering moving it to a separate utility module. (I've taken this into account while writing the test code.)



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "seyoon-lim (via GitHub)" <gi...@apache.org>.
seyoon-lim commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1677189342


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -281,10 +310,57 @@ def _resolve_connection(self) -> dict[str, Any]:
     def get_conn(self) -> Any:
         pass
 
+    def _get_keytab_from_base64(self, base64_keytab: str, principal: str | None) -> str:

Review Comment:
   yeah, it actually does `get_or_create_keytab_path_from_base64_encoded_keytab_value` 
   
   actually, this function is not strictly dependent on `spark_submit.py`, so I'm considering moving it to a separate utility module. (I've taken this into account while writing the test code.)



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "nevcohen (via GitHub)" <gi...@apache.org>.
nevcohen commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1742450532


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -282,10 +313,57 @@ def _resolve_connection(self) -> dict[str, Any]:
     def get_conn(self) -> Any:
         pass
 
+    def _create_keytab_path_from_base64_keytab(self, base64_keytab: str, principal: str | None) -> str:
+        _uuid = uuid.uuid4()
+        temp_dir_path = Path(tempfile.gettempdir()).resolve()
+        temp_file_name = f"airflow_keytab-{principal or _uuid}"
+
+        keytab_path = temp_dir_path / temp_file_name
+        staging_path = temp_dir_path / f".{temp_file_name}.{_uuid}"
+
+        try:
+            keytab = base64.b64decode(base64_keytab)
+        except Exception as err:
+            self.log.error("Failed to decode base64 keytab: %s", err)
+            raise AirflowException("Failed to decode base64 keytab") from err
+
+        # If a keytab file with the same content exists, return its path for the --keytab argument.
+        if keytab_path.exists():
+            with open(keytab_path, "rb") as f:
+                existing_keytab = f.read()
+            if existing_keytab == keytab:
+                self.log.info("Keytab file already exists and is the same as the provided keytab")
+                return str(keytab_path)
+
+        # Generate a new keytab file from the base64 encoded keytab value to use as a --keytab argument.
+        try:
+            with open(staging_path, "wb") as f:
+                self.log.info("Saving keytab to %s", staging_path)
+                f.write(keytab)
+
+            self.log.info("Moving keytab from %s to %s", staging_path, keytab_path)
+            shutil.move(str(staging_path), str(keytab_path))
+            return str(keytab_path)
+        except Exception as err:

Review Comment:
   Great!



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "seyoon-lim (via GitHub)" <gi...@apache.org>.
seyoon-lim commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1678489823


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -281,10 +310,57 @@ def _resolve_connection(self) -> dict[str, Any]:
     def get_conn(self) -> Any:
         pass
 
+    def _get_keytab_from_base64(self, base64_keytab: str, principal: str | None) -> str:
+        _uuid = uuid.uuid4()
+        temp_dir_path = Path(tempfile.gettempdir()).resolve()
+        temp_file_name = f"airflow_keytab-{principal or _uuid}"
+
+        keytab_path = temp_dir_path / temp_file_name
+        staging_path = temp_dir_path / f".{temp_file_name}.{_uuid}"
+
+        try:
+            keytab = base64.b64decode(base64_keytab)
+        except Exception as err:
+            self.log.error("Failed to decode base64 keytab: %s", err)
+            raise AirflowException("Failed to decode base64 keytab") from err
+
+        # validate exists keytab file
+        if keytab_path.exists():

Review Comment:
   After considering the scenarios where a read might occur during a write operation or a write might fail, I thought that it would be safest to write to a staging file successfully first and then move it.
   
   Additionally, the cases where writing occurs are when the keytab is not present at the path or when the contents of the keytab have changed.



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "seyoon-lim (via GitHub)" <gi...@apache.org>.
seyoon-lim commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1678489823


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -281,10 +310,57 @@ def _resolve_connection(self) -> dict[str, Any]:
     def get_conn(self) -> Any:
         pass
 
+    def _get_keytab_from_base64(self, base64_keytab: str, principal: str | None) -> str:
+        _uuid = uuid.uuid4()
+        temp_dir_path = Path(tempfile.gettempdir()).resolve()
+        temp_file_name = f"airflow_keytab-{principal or _uuid}"
+
+        keytab_path = temp_dir_path / temp_file_name
+        staging_path = temp_dir_path / f".{temp_file_name}.{_uuid}"
+
+        try:
+            keytab = base64.b64decode(base64_keytab)
+        except Exception as err:
+            self.log.error("Failed to decode base64 keytab: %s", err)
+            raise AirflowException("Failed to decode base64 keytab") from err
+
+        # validate exists keytab file
+        if keytab_path.exists():

Review Comment:
   After considering the scenarios where a read might occur during a write operation or a write might fail or some concurrency cases, I thought that it would be safest to write to a staging file successfully first and then move it.
   
   Additionally, the cases where writing occurs are when the keytab is not present at the path or when the contents of the keytab at the path have changed.



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "seyoon-lim (via GitHub)" <gi...@apache.org>.
seyoon-lim commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1679157182


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -236,6 +257,8 @@ def _resolve_connection(self) -> dict[str, Any]:
             "deploy_mode": None,
             "spark_binary": self.spark_binary or DEFAULT_SPARK_BINARY,
             "namespace": None,
+            "principal": None,
+            "keytab": None,

Review Comment:
   I just realized what were you meaning 🤔🤣
   
   will push soon! thanks for the suggestion!



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "romsharon98 (via GitHub)" <gi...@apache.org>.
romsharon98 commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1747672315


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -282,10 +313,57 @@ def _resolve_connection(self) -> dict[str, Any]:
     def get_conn(self) -> Any:
         pass
 
+    def _create_keytab_path_from_base64_keytab(self, base64_keytab: str, principal: str | None) -> str:
+        _uuid = uuid.uuid4()
+        temp_dir_path = Path(tempfile.gettempdir()).resolve()
+        temp_file_name = f"airflow_keytab-{principal or _uuid}"
+
+        keytab_path = temp_dir_path / temp_file_name
+        staging_path = temp_dir_path / f".{temp_file_name}.{_uuid}"

Review Comment:
   Is there a reason you added here `.{_uuid}`?



##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -370,10 +448,12 @@ def _build_spark_submit_command(self, application: str) -> list[str]:
             connection_cmd += ["--executor-memory", self._executor_memory]
         if self._driver_memory:
             connection_cmd += ["--driver-memory", self._driver_memory]
-        if self._keytab:
-            connection_cmd += ["--keytab", self._keytab]
-        if self._principal:
-            connection_cmd += ["--principal", self._principal]
+        keytab = self._get_keytab()
+        if keytab:
+            connection_cmd += ["--keytab", keytab]
+        principal = self._get_principal()

Review Comment:
   I think you can just do `self._connection["principal"]` because [here](https://github.com/apache/airflow/pull/40757/files#diff-aa05e2f3d2472084951026f8344dbc7134cb1e3f94d626127ab9dee50fbde2c6R295)
   you already did this if (same for the keytab)



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "eladkal (via GitHub)" <gi...@apache.org>.
eladkal commented on PR #40757:
URL: https://github.com/apache/airflow/pull/40757#issuecomment-2323884724

   @seyoon-lim you need to rebase. I can't do that for you as you removed the allow mantainers edits
   https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork#enabling-repository-maintainer-permissions-on-existing-pull-requests


-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "seyoon-lim (via GitHub)" <gi...@apache.org>.
seyoon-lim commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1761192669


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -370,10 +448,12 @@ def _build_spark_submit_command(self, application: str) -> list[str]:
             connection_cmd += ["--executor-memory", self._executor_memory]
         if self._driver_memory:
             connection_cmd += ["--driver-memory", self._driver_memory]
-        if self._keytab:
-            connection_cmd += ["--keytab", self._keytab]
-        if self._principal:
-            connection_cmd += ["--principal", self._principal]
+        keytab = self._get_keytab()
+        if keytab:
+            connection_cmd += ["--keytab", keytab]
+        principal = self._get_principal()

Review Comment:
   @romsharon98 I have rolled back this logic due to the following reason from https://github.com/apache/airflow/pull/40757#discussion_r1747780969
   
   The test fails when an error is raised from _resolve_connection. In this case, I need to return self._principal(self._keytab) instead of self._connection["principal"](self._connection["keytab"]).



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "seyoon-lim (via GitHub)" <gi...@apache.org>.
seyoon-lim commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1676984823


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -268,6 +291,12 @@ def _resolve_connection(self) -> dict[str, Any]:
                 )
             conn_data["spark_binary"] = self.spark_binary
             conn_data["namespace"] = extra.get("namespace")
+            conn_data["principal"] = self._principal or extra.get("principal")
+            base64_keytab = extra.get("keytab")
+            if self._keytab is not None:
+                conn_data["keytab"] = self._keytab
+            elif base64_keytab is not None:
+                conn_data["keytab"] = self._get_keytab_from_base64(base64_keytab, conn_data["principal"])

Review Comment:
   Values passed as parameters to the constructor have higher priority.



##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -281,10 +310,53 @@ def _resolve_connection(self) -> dict[str, Any]:
     def get_conn(self) -> Any:
         pass
 
+    def _get_keytab_from_base64(self, base64_keytab: str, principal: str | None) -> str:
+        _uuid = uuid.uuid4()
+        temp_dir_path = Path(tempfile.gettempdir()).resolve()
+        temp_file_name = f"airflow_keytab-{principal or _uuid}"
+
+        keytab_path = temp_dir_path / temp_file_name
+        staging_path = temp_dir_path / f".{temp_file_name}.{_uuid}"
+
+        keytab = base64.b64decode(base64_keytab)
+
+        # validate exists keytab file
+        if keytab_path.exists():
+            with open(keytab_path, "rb") as f:
+                existing_keytab = f.read()
+            if existing_keytab == keytab:

Review Comment:
   I was considering comparing the checksums for this part, but I'd appreciate your opinion on it



##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -281,10 +310,53 @@ def _resolve_connection(self) -> dict[str, Any]:
     def get_conn(self) -> Any:
         pass
 
+    def _get_keytab_from_base64(self, base64_keytab: str, principal: str | None) -> str:
+        _uuid = uuid.uuid4()
+        temp_dir_path = Path(tempfile.gettempdir()).resolve()
+        temp_file_name = f"airflow_keytab-{principal or _uuid}"
+
+        keytab_path = temp_dir_path / temp_file_name
+        staging_path = temp_dir_path / f".{temp_file_name}.{_uuid}"
+
+        keytab = base64.b64decode(base64_keytab)
+
+        # validate exists keytab file
+        if keytab_path.exists():
+            with open(keytab_path, "rb") as f:
+                existing_keytab = f.read()
+            if existing_keytab == keytab:
+                self.log.info("Keytab file already exists and is the same as the provided keytab")
+                return str(keytab_path)
+
+        # write new keytab file
+        try:
+            with open(staging_path, "wb") as f:
+                self.log.info("Saving keytab to %s", staging_path)
+                f.write(keytab)
+
+            self.log.info("Moving keytab from %s to %s", staging_path, keytab_path)
+            shutil.move(str(staging_path), str(keytab_path))
+            return str(keytab_path)
+        except Exception as err:
+            self.log.error("Failed to save keytab: %s", err)
+            raise AirflowException("Failed to save keytab") from err
+        finally:
+            if staging_path.exists():
+                self.log.info("Removing staging keytab file: %s", staging_path)
+                staging_path.unlink()
+
     def _get_spark_binary_path(self) -> list[str]:
         # Assume that spark-submit is present in the path to the executing user
         return [self._connection["spark_binary"]]
 
+    def _get_principal(self) -> str | None:
+        # for the case where the principal is not set in the broken connection
+        return self._connection["principal"] or self._principal
+
+    def _get_keytab(self) -> str | None:
+        # for the case where the keytab is not set in the broken connection
+        return self._connection["keytab"] or self._keytab

Review Comment:
   if an exception occurs in the `_resolve_connection` method, the values passed as parameters will be used.



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "seyoon-lim (via GitHub)" <gi...@apache.org>.
seyoon-lim commented on PR #40757:
URL: https://github.com/apache/airflow/pull/40757#issuecomment-2225992060

   I am currently writing the test code, and I will change the draft status once the test code is completed.


-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "nevcohen (via GitHub)" <gi...@apache.org>.
nevcohen commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1680481707


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -236,6 +257,8 @@ def _resolve_connection(self) -> dict[str, Any]:
             "deploy_mode": None,
             "spark_binary": self.spark_binary or DEFAULT_SPARK_BINARY,
             "namespace": None,
+            "principal": None,
+            "keytab": None,

Review Comment:
   Don't you have several different users on the same yarn queue?



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "eladkal (via GitHub)" <gi...@apache.org>.
eladkal commented on PR #40757:
URL: https://github.com/apache/airflow/pull/40757#issuecomment-2324424146

   Cool. Can you address the comments left by @nevcohen ? If issues are resooved then please resolve the threads so we know it was addressed 


-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "seyoon-lim (via GitHub)" <gi...@apache.org>.
seyoon-lim commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1747780969


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -370,10 +448,12 @@ def _build_spark_submit_command(self, application: str) -> list[str]:
             connection_cmd += ["--executor-memory", self._executor_memory]
         if self._driver_memory:
             connection_cmd += ["--driver-memory", self._driver_memory]
-        if self._keytab:
-            connection_cmd += ["--keytab", self._keytab]
-        if self._principal:
-            connection_cmd += ["--principal", self._principal]
+        keytab = self._get_keytab()
+        if keytab:
+            connection_cmd += ["--keytab", keytab]
+        principal = self._get_principal()

Review Comment:
   This part was added because the `_resolve_connection` method might throw an exception(before [here](https://github.com/apache/airflow/pull/40757/files/b27cd4f12a86d2f0bb20720b8fd0c68ed6a5573a#diff-aa05e2f3d2472084951026f8344dbc7134cb1e3f94d626127ab9dee50fbde2c6R294)) if the principal and keytab of conn_data are assigned as None, as seen [here](https://github.com/apache/airflow/pull/40757/files/b27cd4f12a86d2f0bb20720b8fd0c68ed6a5573a#diff-aa05e2f3d2472084951026f8344dbc7134cb1e3f94d626127ab9dee50fbde2c6R261-R262)
   
   However, now that I think about it, setting the default values of conn_data to self._keytab and self._principal might resolve this issue as per your feedback.
   This seems to be a matter of choice, so if you think it needs to be changed, please let me know and I'll make the adjustments!



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "nevcohen (via GitHub)" <gi...@apache.org>.
nevcohen commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1741111324


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -282,10 +313,57 @@ def _resolve_connection(self) -> dict[str, Any]:
     def get_conn(self) -> Any:
         pass
 
+    def _create_keytab_path_from_base64_keytab(self, base64_keytab: str, principal: str | None) -> str:
+        _uuid = uuid.uuid4()
+        temp_dir_path = Path(tempfile.gettempdir()).resolve()
+        temp_file_name = f"airflow_keytab-{principal or _uuid}"
+
+        keytab_path = temp_dir_path / temp_file_name
+        staging_path = temp_dir_path / f".{temp_file_name}.{_uuid}"
+
+        try:
+            keytab = base64.b64decode(base64_keytab)
+        except Exception as err:

Review Comment:
   What with add test to this exception?



##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -282,10 +313,57 @@ def _resolve_connection(self) -> dict[str, Any]:
     def get_conn(self) -> Any:
         pass
 
+    def _create_keytab_path_from_base64_keytab(self, base64_keytab: str, principal: str | None) -> str:
+        _uuid = uuid.uuid4()
+        temp_dir_path = Path(tempfile.gettempdir()).resolve()
+        temp_file_name = f"airflow_keytab-{principal or _uuid}"
+
+        keytab_path = temp_dir_path / temp_file_name
+        staging_path = temp_dir_path / f".{temp_file_name}.{_uuid}"
+
+        try:
+            keytab = base64.b64decode(base64_keytab)
+        except Exception as err:
+            self.log.error("Failed to decode base64 keytab: %s", err)
+            raise AirflowException("Failed to decode base64 keytab") from err
+
+        # If a keytab file with the same content exists, return its path for the --keytab argument.
+        if keytab_path.exists():
+            with open(keytab_path, "rb") as f:
+                existing_keytab = f.read()
+            if existing_keytab == keytab:
+                self.log.info("Keytab file already exists and is the same as the provided keytab")
+                return str(keytab_path)
+
+        # Generate a new keytab file from the base64 encoded keytab value to use as a --keytab argument.
+        try:
+            with open(staging_path, "wb") as f:
+                self.log.info("Saving keytab to %s", staging_path)
+                f.write(keytab)
+
+            self.log.info("Moving keytab from %s to %s", staging_path, keytab_path)
+            shutil.move(str(staging_path), str(keytab_path))
+            return str(keytab_path)
+        except Exception as err:

Review Comment:
   Also what with add test to this exception? What if it fails to write to the file or copy it with `shutil.move`?



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "nevcohen (via GitHub)" <gi...@apache.org>.
nevcohen commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1741112895


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -282,10 +313,57 @@ def _resolve_connection(self) -> dict[str, Any]:
     def get_conn(self) -> Any:
         pass
 
+    def _create_keytab_path_from_base64_keytab(self, base64_keytab: str, principal: str | None) -> str:
+        _uuid = uuid.uuid4()
+        temp_dir_path = Path(tempfile.gettempdir()).resolve()
+        temp_file_name = f"airflow_keytab-{principal or _uuid}"
+
+        keytab_path = temp_dir_path / temp_file_name
+        staging_path = temp_dir_path / f".{temp_file_name}.{_uuid}"
+
+        try:
+            keytab = base64.b64decode(base64_keytab)
+        except Exception as err:
+            self.log.error("Failed to decode base64 keytab: %s", err)
+            raise AirflowException("Failed to decode base64 keytab") from err
+
+        # If a keytab file with the same content exists, return its path for the --keytab argument.
+        if keytab_path.exists():
+            with open(keytab_path, "rb") as f:
+                existing_keytab = f.read()
+            if existing_keytab == keytab:
+                self.log.info("Keytab file already exists and is the same as the provided keytab")
+                return str(keytab_path)
+
+        # Generate a new keytab file from the base64 encoded keytab value to use as a --keytab argument.
+        try:
+            with open(staging_path, "wb") as f:
+                self.log.info("Saving keytab to %s", staging_path)
+                f.write(keytab)
+
+            self.log.info("Moving keytab from %s to %s", staging_path, keytab_path)
+            shutil.move(str(staging_path), str(keytab_path))
+            return str(keytab_path)
+        except Exception as err:

Review Comment:
   Also what with add test to this exception? What if it fails to write to the file or copy it with `shutil.move` we don't want test on this?



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "seyoon-lim (via GitHub)" <gi...@apache.org>.
seyoon-lim commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1742312863


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -282,10 +313,57 @@ def _resolve_connection(self) -> dict[str, Any]:
     def get_conn(self) -> Any:
         pass
 
+    def _create_keytab_path_from_base64_keytab(self, base64_keytab: str, principal: str | None) -> str:
+        _uuid = uuid.uuid4()
+        temp_dir_path = Path(tempfile.gettempdir()).resolve()
+        temp_file_name = f"airflow_keytab-{principal or _uuid}"
+
+        keytab_path = temp_dir_path / temp_file_name
+        staging_path = temp_dir_path / f".{temp_file_name}.{_uuid}"
+
+        try:
+            keytab = base64.b64decode(base64_keytab)
+        except Exception as err:
+            self.log.error("Failed to decode base64 keytab: %s", err)
+            raise AirflowException("Failed to decode base64 keytab") from err
+
+        # If a keytab file with the same content exists, return its path for the --keytab argument.
+        if keytab_path.exists():
+            with open(keytab_path, "rb") as f:
+                existing_keytab = f.read()
+            if existing_keytab == keytab:
+                self.log.info("Keytab file already exists and is the same as the provided keytab")
+                return str(keytab_path)
+
+        # Generate a new keytab file from the base64 encoded keytab value to use as a --keytab argument.
+        try:
+            with open(staging_path, "wb") as f:
+                self.log.info("Saving keytab to %s", staging_path)
+                f.write(keytab)
+
+            self.log.info("Moving keytab from %s to %s", staging_path, keytab_path)
+            shutil.move(str(staging_path), str(keytab_path))
+            return str(keytab_path)
+        except Exception as err:

Review Comment:
   https://github.com/apache/airflow/pull/40757/commits/cd6c35898bafaa233cf9dcb20482fc06ec03782c done! thanks!



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "seyoon-lim (via GitHub)" <gi...@apache.org>.
seyoon-lim commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1761192669


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -370,10 +448,12 @@ def _build_spark_submit_command(self, application: str) -> list[str]:
             connection_cmd += ["--executor-memory", self._executor_memory]
         if self._driver_memory:
             connection_cmd += ["--driver-memory", self._driver_memory]
-        if self._keytab:
-            connection_cmd += ["--keytab", self._keytab]
-        if self._principal:
-            connection_cmd += ["--principal", self._principal]
+        keytab = self._get_keytab()
+        if keytab:
+            connection_cmd += ["--keytab", keytab]
+        principal = self._get_principal()

Review Comment:
   @romsharon98 I have rolled back this logic due to the following reason from https://github.com/apache/airflow/pull/40757#discussion_r1747780969
   
   The test fails when an error is raised from _resolve_connection. In this case, I need to return self._principal(self._keytab) instead of `self._connection["principal"]`(`self._connection["keytab"]`).



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "seyoon-lim (via GitHub)" <gi...@apache.org>.
seyoon-lim commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1677189342


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -281,10 +310,57 @@ def _resolve_connection(self) -> dict[str, Any]:
     def get_conn(self) -> Any:
         pass
 
+    def _get_keytab_from_base64(self, base64_keytab: str, principal: str | None) -> str:

Review Comment:
   yeah, it actually does `get_or_create_keytab_file_path_from_base64_encoded_keytab_value` 
   
   actually, this function is not strictly dependent on `spark_submit.py`, so I'm considering moving it to a separate utility module. (I've taken this into account while writing the test code.)



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "seyoon-lim (via GitHub)" <gi...@apache.org>.
seyoon-lim commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1678489823


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -281,10 +310,57 @@ def _resolve_connection(self) -> dict[str, Any]:
     def get_conn(self) -> Any:
         pass
 
+    def _get_keytab_from_base64(self, base64_keytab: str, principal: str | None) -> str:
+        _uuid = uuid.uuid4()
+        temp_dir_path = Path(tempfile.gettempdir()).resolve()
+        temp_file_name = f"airflow_keytab-{principal or _uuid}"
+
+        keytab_path = temp_dir_path / temp_file_name
+        staging_path = temp_dir_path / f".{temp_file_name}.{_uuid}"
+
+        try:
+            keytab = base64.b64decode(base64_keytab)
+        except Exception as err:
+            self.log.error("Failed to decode base64 keytab: %s", err)
+            raise AirflowException("Failed to decode base64 keytab") from err
+
+        # validate exists keytab file
+        if keytab_path.exists():

Review Comment:
   After considering the scenarios where a read might occur during a write operation or a write might fail, I thought that it would be safest to write to a staging file successfully first and then move 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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "seyoon-lim (via GitHub)" <gi...@apache.org>.
seyoon-lim commented on PR #40757:
URL: https://github.com/apache/airflow/pull/40757#issuecomment-2227182665

   Sorry, the changes to the www directory that I was working on locally were pushed by my mistake.
   
   sorry for the reviewers (@ryanahamilton @ashb @bbovenzi @pierrejeambrun)


-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "seyoon-lim (via GitHub)" <gi...@apache.org>.
seyoon-lim commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1761192669


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -370,10 +448,12 @@ def _build_spark_submit_command(self, application: str) -> list[str]:
             connection_cmd += ["--executor-memory", self._executor_memory]
         if self._driver_memory:
             connection_cmd += ["--driver-memory", self._driver_memory]
-        if self._keytab:
-            connection_cmd += ["--keytab", self._keytab]
-        if self._principal:
-            connection_cmd += ["--principal", self._principal]
+        keytab = self._get_keytab()
+        if keytab:
+            connection_cmd += ["--keytab", keytab]
+        principal = self._get_principal()

Review Comment:
   @romsharon98 I have rolled back this logic due to the following reason from https://github.com/apache/airflow/pull/40757#discussion_r1747780969
   
   The test fails([link](https://github.com/apache/airflow/actions/runs/10883694059/job/30201008840)) when an error is raised from _resolve_connection. In this case, I need to return self._principal(self._keytab) instead of `self._connection["principal"]`(`self._connection["keytab"]`).



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "seyoon-lim (via GitHub)" <gi...@apache.org>.
seyoon-lim commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1747798708


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -370,10 +448,12 @@ def _build_spark_submit_command(self, application: str) -> list[str]:
             connection_cmd += ["--executor-memory", self._executor_memory]
         if self._driver_memory:
             connection_cmd += ["--driver-memory", self._driver_memory]
-        if self._keytab:
-            connection_cmd += ["--keytab", self._keytab]
-        if self._principal:
-            connection_cmd += ["--principal", self._principal]
+        keytab = self._get_keytab()
+        if keytab:
+            connection_cmd += ["--keytab", keytab]
+        principal = self._get_principal()

Review Comment:
   ~~Looking at [this part](https://github.com/apache/airflow/pull/40757/files#diff-aa05e2f3d2472084951026f8344dbc7134cb1e3f94d626127ab9dee50fbde2c6R461-R464), it seems you're right about making the change. I've corrected it (https://github.com/apache/airflow/pull/40757/commits/55007bc3401eb42bff86dfdd4cb442c0e66c4f4b). Thanks!~~



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "nevcohen (via GitHub)" <gi...@apache.org>.
nevcohen commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1678105236


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -281,10 +310,57 @@ def _resolve_connection(self) -> dict[str, Any]:
     def get_conn(self) -> Any:
         pass
 
+    def _get_keytab_from_base64(self, base64_keytab: str, principal: str | None) -> str:

Review Comment:
   OK, waiting for some action



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "nevcohen (via GitHub)" <gi...@apache.org>.
nevcohen commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1677180248


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -281,10 +310,57 @@ def _resolve_connection(self) -> dict[str, Any]:
     def get_conn(self) -> Any:
         pass
 
+    def _get_keytab_from_base64(self, base64_keytab: str, principal: str | None) -> str:

Review Comment:
   What does this func actually do? From what I understand the func creates a path and file for the base64 keytab and put it there? So u mean `_create_keytab_path_from_base64_keytab`?



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "seyoon-lim (via GitHub)" <gi...@apache.org>.
seyoon-lim commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1747780969


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -370,10 +448,12 @@ def _build_spark_submit_command(self, application: str) -> list[str]:
             connection_cmd += ["--executor-memory", self._executor_memory]
         if self._driver_memory:
             connection_cmd += ["--driver-memory", self._driver_memory]
-        if self._keytab:
-            connection_cmd += ["--keytab", self._keytab]
-        if self._principal:
-            connection_cmd += ["--principal", self._principal]
+        keytab = self._get_keytab()
+        if keytab:
+            connection_cmd += ["--keytab", keytab]
+        principal = self._get_principal()

Review Comment:
   This part was added because the `_resolve_connection` method might throw an exception if the principal and keytab of conn_data are assigned as None, as seen [here](https://github.com/apache/airflow/pull/40757/files/b27cd4f12a86d2f0bb20720b8fd0c68ed6a5573a#diff-aa05e2f3d2472084951026f8344dbc7134cb1e3f94d626127ab9dee50fbde2c6R261-R262)
   
   However, now that I think about it, setting the default values of conn_data to self._keytab and self._principal might resolve this issue as per your feedback.
   This seems to be a matter of choice, so if you think it needs to be changed, please let me know and I'll make the adjustments!



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "seyoon-lim (via GitHub)" <gi...@apache.org>.
seyoon-lim commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1747774571


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -282,10 +313,57 @@ def _resolve_connection(self) -> dict[str, Any]:
     def get_conn(self) -> Any:
         pass
 
+    def _create_keytab_path_from_base64_keytab(self, base64_keytab: str, principal: str | None) -> str:
+        _uuid = uuid.uuid4()
+        temp_dir_path = Path(tempfile.gettempdir()).resolve()
+        temp_file_name = f"airflow_keytab-{principal or _uuid}"
+
+        keytab_path = temp_dir_path / temp_file_name
+        staging_path = temp_dir_path / f".{temp_file_name}.{_uuid}"

Review Comment:
   Yeah! This part was added to assign a unique staging_path to prevent concurrency issues that can occur with the same principal.



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "nevcohen (via GitHub)" <gi...@apache.org>.
nevcohen commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1741069969


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -236,6 +257,8 @@ def _resolve_connection(self) -> dict[str, Any]:
             "deploy_mode": None,
             "spark_binary": self.spark_binary or DEFAULT_SPARK_BINARY,
             "namespace": None,
+            "principal": None,
+            "keytab": None,

Review Comment:
   I meant what about there are several different principles, since you only override the user from the connection if he has not entered a principle, this is correct and it will be possible to use several different principles on the same queue.
   
   So I approve this comment



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #40757:
URL: https://github.com/apache/airflow/pull/40757#issuecomment-2323558095

   This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 5 days if no further activity occurs. Thank you for your contributions.


-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "seyoon-lim (via GitHub)" <gi...@apache.org>.
seyoon-lim commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1740233294


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -281,10 +310,57 @@ def _resolve_connection(self) -> dict[str, Any]:
     def get_conn(self) -> Any:
         pass
 
+    def _get_keytab_from_base64(self, base64_keytab: str, principal: str | None) -> str:
+        _uuid = uuid.uuid4()
+        temp_dir_path = Path(tempfile.gettempdir()).resolve()
+        temp_file_name = f"airflow_keytab-{principal or _uuid}"
+
+        keytab_path = temp_dir_path / temp_file_name
+        staging_path = temp_dir_path / f".{temp_file_name}.{_uuid}"
+
+        try:
+            keytab = base64.b64decode(base64_keytab)
+        except Exception as err:
+            self.log.error("Failed to decode base64 keytab: %s", err)
+            raise AirflowException("Failed to decode base64 keytab") from err
+
+        # validate exists keytab file
+        if keytab_path.exists():

Review Comment:
   added the detailed reason as a comment, thanks!



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "nevcohen (via GitHub)" <gi...@apache.org>.
nevcohen commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1677180609


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -281,10 +310,57 @@ def _resolve_connection(self) -> dict[str, Any]:
     def get_conn(self) -> Any:
         pass
 
+    def _get_keytab_from_base64(self, base64_keytab: str, principal: str | None) -> str:
+        _uuid = uuid.uuid4()
+        temp_dir_path = Path(tempfile.gettempdir()).resolve()
+        temp_file_name = f"airflow_keytab-{principal or _uuid}"
+
+        keytab_path = temp_dir_path / temp_file_name
+        staging_path = temp_dir_path / f".{temp_file_name}.{_uuid}"
+
+        try:
+            keytab = base64.b64decode(base64_keytab)
+        except Exception as err:
+            self.log.error("Failed to decode base64 keytab: %s", err)
+            raise AirflowException("Failed to decode base64 keytab") from err
+
+        # validate exists keytab file
+        if keytab_path.exists():

Review Comment:
   Why would it exist if only a few lines ago you created this path?



##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -236,6 +257,8 @@ def _resolve_connection(self) -> dict[str, Any]:
             "deploy_mode": None,
             "spark_binary": self.spark_binary or DEFAULT_SPARK_BINARY,
             "namespace": None,
+            "principal": None,
+            "keytab": None,

Review Comment:
   Why not separate them into two different connections? There can be many more than just one keytab and principal in each master



##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -281,10 +310,57 @@ def _resolve_connection(self) -> dict[str, Any]:
     def get_conn(self) -> Any:
         pass
 
+    def _get_keytab_from_base64(self, base64_keytab: str, principal: str | None) -> str:

Review Comment:
   What does this func actually do? From what I understand the func creates a path and file for the base64 keytab and put it there? So u mean `_create_and_get_keytab_path_from_base64_keytab`?



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "boring-cyborg[bot] (via GitHub)" <gi...@apache.org>.
boring-cyborg[bot] commented on PR #40757:
URL: https://github.com/apache/airflow/pull/40757#issuecomment-2225989591

   Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contributors' Guide (https://github.com/apache/airflow/blob/main/contributing-docs/README.rst)
   Here are some useful points:
   - Pay attention to the quality of your code (ruff, mypy and type annotations). Our [pre-commits]( https://github.com/apache/airflow/blob/main/contributing-docs/08_static_code_checks.rst#prerequisites-for-pre-commit-hooks) will help you with that.
   - In case of a new feature add useful documentation (in docstrings or in `docs/` directory). Adding a new operator? Check this short [guide](https://github.com/apache/airflow/blob/main/docs/apache-airflow/howto/custom-operator.rst) Consider adding an example DAG that shows how users should use it.
   - Consider using [Breeze environment](https://github.com/apache/airflow/blob/main/dev/breeze/doc/README.rst) for testing locally, it's a heavy docker but it ships with a working Airflow and a lot of integrations.
   - Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
   - Please follow [ASF Code of Conduct](https://www.apache.org/foundation/policies/conduct) for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
   - Be sure to read the [Airflow Coding style]( https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#coding-style-and-best-practices).
   - Always keep your Pull Requests rebased, otherwise your build might fail due to changes not related to your commits.
   Apache Airflow is a community-driven project and together we are making it better 🚀.
   In case of doubts contact the developers at:
   Mailing List: dev@airflow.apache.org
   Slack: https://s.apache.org/airflow-slack
   


-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "seyoon-lim (via GitHub)" <gi...@apache.org>.
seyoon-lim commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1679157182


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -236,6 +257,8 @@ def _resolve_connection(self) -> dict[str, Any]:
             "deploy_mode": None,
             "spark_binary": self.spark_binary or DEFAULT_SPARK_BINARY,
             "namespace": None,
+            "principal": None,
+            "keytab": None,

Review Comment:
   ~~I just realized what were you meaning 🤔🤣~~
   
   I'm sorry, I still didn't quite get it.
   
   Could you explain in more detail what you mean by how to separate the connections?
   
   The use case I'm working with involved creating a `conn_id` for each user to use.
   
   I still don't fully understand, so your detailed explanation would be appreciated.



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "boring-cyborg[bot] (via GitHub)" <gi...@apache.org>.
boring-cyborg[bot] commented on PR #40757:
URL: https://github.com/apache/airflow/pull/40757#issuecomment-2354618451

   Awesome work, congrats on your first merged pull request! You are invited to check our [Issue Tracker](https://github.com/apache/airflow/issues) for additional contributions.
   


-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "romsharon98 (via GitHub)" <gi...@apache.org>.
romsharon98 merged PR #40757:
URL: https://github.com/apache/airflow/pull/40757


-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "nevcohen (via GitHub)" <gi...@apache.org>.
nevcohen commented on code in PR #40757:
URL: https://github.com/apache/airflow/pull/40757#discussion_r1741112895


##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -282,10 +313,57 @@ def _resolve_connection(self) -> dict[str, Any]:
     def get_conn(self) -> Any:
         pass
 
+    def _create_keytab_path_from_base64_keytab(self, base64_keytab: str, principal: str | None) -> str:
+        _uuid = uuid.uuid4()
+        temp_dir_path = Path(tempfile.gettempdir()).resolve()
+        temp_file_name = f"airflow_keytab-{principal or _uuid}"
+
+        keytab_path = temp_dir_path / temp_file_name
+        staging_path = temp_dir_path / f".{temp_file_name}.{_uuid}"
+
+        try:
+            keytab = base64.b64decode(base64_keytab)
+        except Exception as err:
+            self.log.error("Failed to decode base64 keytab: %s", err)
+            raise AirflowException("Failed to decode base64 keytab") from err
+
+        # If a keytab file with the same content exists, return its path for the --keytab argument.
+        if keytab_path.exists():
+            with open(keytab_path, "rb") as f:
+                existing_keytab = f.read()
+            if existing_keytab == keytab:
+                self.log.info("Keytab file already exists and is the same as the provided keytab")
+                return str(keytab_path)
+
+        # Generate a new keytab file from the base64 encoded keytab value to use as a --keytab argument.
+        try:
+            with open(staging_path, "wb") as f:
+                self.log.info("Saving keytab to %s", staging_path)
+                f.write(keytab)
+
+            self.log.info("Moving keytab from %s to %s", staging_path, keytab_path)
+            shutil.move(str(staging_path), str(keytab_path))
+            return str(keytab_path)
+        except Exception as err:

Review Comment:
   Also what with add to this exception? What if it fails to write to the file or copy it with `shutil.move`?



##########
airflow/providers/apache/spark/hooks/spark_submit.py:
##########
@@ -282,10 +313,57 @@ def _resolve_connection(self) -> dict[str, Any]:
     def get_conn(self) -> Any:
         pass
 
+    def _create_keytab_path_from_base64_keytab(self, base64_keytab: str, principal: str | None) -> str:
+        _uuid = uuid.uuid4()
+        temp_dir_path = Path(tempfile.gettempdir()).resolve()
+        temp_file_name = f"airflow_keytab-{principal or _uuid}"
+
+        keytab_path = temp_dir_path / temp_file_name
+        staging_path = temp_dir_path / f".{temp_file_name}.{_uuid}"
+
+        try:
+            keytab = base64.b64decode(base64_keytab)
+        except Exception as err:

Review Comment:
   What with add to this exception?



-- 
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] Add kerberos related connection fields(principal, keytab) on SparkSubmitHook [airflow]

Posted by "seyoon-lim (via GitHub)" <gi...@apache.org>.
seyoon-lim commented on PR #40757:
URL: https://github.com/apache/airflow/pull/40757#issuecomment-2323931555

   @eladkal 
   I'm sorry, I missed to check the 'Allow edits from maintainers' option.
   
   I've already merged the PR with the main branch, but if necessary, please let me know and I will close and reopen the PR.


-- 
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