Joffreybvn opened a new pull request, #35591:
URL: https://github.com/apache/airflow/pull/35591

   <!--
    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/
   -->
   
   Hello,
   
   This PR makes possible to parameterize HttpHook's `auth_type` from the 
Connection UI. In a Connection extra settings, the reserved `"auth_kwargs"` 
field can be used to provide a dict of extra parameters to the `auth_type` 
class.
   
   **Use-case:**
   
   The `auth_type` is typically a subclass of 
[`request.AuthBase`](https://github.com/psf/requests/blob/839a8edec37c81a18ac8332cfbd44f44e1ae6206/src/requests/auth.py#L69).
 Many custom Auth classes exist for many different protocols. Sometimes, 
passing only two hard-coded `conn.username` and `conn.password` is not enough: 
The Auth class expects more than two arguments.
   
   Examples:
   - [OAuth2 from 
requests-oauthlib](https://github.com/requests/requests-oauthlib/blob/master/requests_oauthlib/oauth2_auth.py),
 which can take a token as third argument
   - [HTTPKerberosAuth from 
requests-kerberos](https://github.com/requests/requests-kerberos/blob/master/requests_kerberos/kerberos_.py),
 which can take much more than two arguments
   
   Right now, to deal with those cases, they are three possibilities:
   <ul dir="auto">
   <li>Using <code>functools.partial</code>, like <a 
href="https://github.com/apache/airflow/pull/29206#discussion_r1136503816"; 
data-hovercard-type="pull_request" 
data-hovercard-url="/apache/airflow/pull/29206/hovercard">mentioned in this 
PR</a>, in the dag file / in the operator declaration. <br><em>Opinion</em>: 
The dag developer should not care about handling the connection. He just want a 
working connection_id to call an endpoint (especially if its a beginner / 
low-experienced dev). Furthermore, some parameters are sensitive and cannot be 
written in a dag.</li>
   <li>Writing a custom Hook, which dispatch the parameters from the Connection 
correctly (eventually using partial). <br><em>Opinion</em>: This is not okay. 
Other hooks are doing better. Take the ODBCHook, which allows to parameterize 
every aspect of the connection without subclassing anything:
   <ul dir="auto">
   <li>Defining which driver has to be used</li>
   <li>Defining connection schema for SQLAlchemy</li>
   <li>Parameterize the driver via extra driver-specific parameters</li>
   <li>Parameterize the behavior of pyodbc (<code 
class="notranslate">"connect_kwargs"</code>).</li>
   </ul>
   Everything can be controlled in the Connection UI ! I'd expect the HttpHook 
to behave similarly, and let me configure everything, which includes the 
underlying authentication.
   </li>
   <li>Misusing the "username" and "password" fields of a Connection, to stack 
and pass multiple parameters in it + implementing a thin layer on top of a Auth 
class to re-dispatch the parameters. <br><em>Opinion</em>: This is definitively 
a bad workaround. I'm mentioning it because this PR won't entirely solve the 
issue, and this may (continue to) happen.</li>
   </ul>
   
   Coming to this PR, I propose to add a reserved field "auth_kwargs", which is 
passed to the underlying Auth class. No breaking change. This solve _most_ of 
the issues: a partial is not needed, a subclass is not needed, and there are 
less cases where conn.username and conn.password will be misused.
   
   **Or:**
   
   This PR can copy further what the ODBCHook does, to:
   - Implement a way to customize the Auth class from the Connection UI / 
conn.extra_json.
   - Remove [the two hard-coded conn.username and conn.password from class 
instantiation](https://github.com/apache/airflow/blob/4f5e482c1f9eebc3824d29e446828f4a3066a184/airflow/providers/http/hooks/http.py#L111)
 to replaces them by keywords arguments, eventually customizable in 
conn.extra_json too.
   
   But, I'm not sure if this can be done without a breaking change. Depending 
on your feedback and comments, I will give it a try. **What do you think ?**
   
   <!-- Please keep an empty line above the dashes. -->
   ---
   **^ Add meaningful description above**
   Read the **[Pull Request 
Guidelines](https://github.com/apache/airflow/blob/main/CONTRIBUTING.rst#pull-request-guidelines)**
 for more information.
   In case of fundamental code changes, an Airflow Improvement Proposal 
([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals))
 is needed.
   In case of a new dependency, check compliance with the [ASF 3rd Party 
License Policy](https://www.apache.org/legal/resolved.html#category-x).
   In case of backwards incompatible changes please leave a note in a 
newsfragment file, named `{pr_number}.significant.rst` or 
`{issue_number}.significant.rst`, in 
[newsfragments](https://github.com/apache/airflow/tree/main/newsfragments).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@airflow.apache.org

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

Reply via email to