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

    <!-- SPDX-License-Identifier: Apache-2.0
         https://www.apache.org/licenses/LICENSE-2.0 -->
   
   <!--
   Thank you for contributing!
   
   Please provide above a brief description of the changes made in this pull 
request.
   Write a good git commit message following this guide: 
https://chris.beams.io/posts/git-commit/
   
   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.
   
   For user-facing UI changes, please attach before/after screenshots (or a 
short
   screen recording) so reviewers can assess the visual impact.
   
   Feel free to ping (in general) for the review if you do not see reaction for 
a few days
   (72 Hours is the minimum reaction time you can expect from volunteers) - we 
sometimes miss notifications.
   
   In case of an existing issue, reference it using one of the following:
   
   * closes: #ISSUE
   * related: #ISSUE
   -->
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   <!--
   If generative AI tooling has been used in the process of authoring this PR, 
please
   change below checkbox to `[X]` followed by the name of the tool, uncomment 
the "Generated-by".
   -->
   
   - [ ] Yes (please specify the tool below)
   
   <!--
   Generated-by: [Tool Name] following [the 
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
   -->
   
   ## What and Why
   
   When `VaultBackend` is configured as a secrets backend for airflow, fetching 
one secret from Vault costs four HTTP requests today. Only one of them reads 
the secret.
   
   | Request | Source |
   |---|---|
   | `POST auth/approle/login` | `_VaultClient._client` |
   | `GET auth/token/lookup-self` | post-login check in `_client` |
   | `GET auth/token/lookup-self` | staleness check in the `client` property |
   | `GET <mount>/data/<path>` | the read |
   
   Neither validation is local. `hvac.Client.is_authenticated()` calls 
`lookup_token()`, which issues `auth/token/lookup-self` over the network every 
time it is asked.
   
   The first check runs immediately after a successful login, against a token 
whose login response has already been received. The second guards a cached 
client against expiry, which is reasonable in isolation, but the secrets
   backend is rebuilt on every lookup so that client never lives long enough to 
need guarding.
   
   This replaces both with reactive validation instead. Issue the request, and 
if Vault rejects the token with a 403, authenticate again and retry once. Vault 
answers 403 for both an expired token and an unreadable path, so a single retry 
is the only way to tell them apart without paying a round trip before every 
request.
   
   ## Improvements
   
   Set up airflow with a running hashicorp vault docker image by confuguring 
these envs:
   ```shell
   
AIRFLOW__SECRETS__BACKEND=airflow.providers.hashicorp.secrets.vault.VaultBackend
   
AIRFLOW__SECRETS__BACKEND_KWARGS='{"url":"http://vault-test:8200","auth_type":"approle","role_id":"blahblah","secret_id":"blahblah","mount_point":"secret","variables_path":"variables","connections_path":null,"config_path":null}'
   
   ```
   
   Enabled audit logs on vault using: `vault audit enable file 
file_path=/vault/logs/audit.log`
   
   Placed two variables on vault using:
   ```shell
   vault kv put -mount=secret variables/demo value=hello
   vault kv put -mount=secret variables/demo2 value=world
   ```
   
   ### One variable lookup
   
   Before and after changes run on `airflow variables get demo`:
   
   Before:
   
   ```shell
   amoghrajesh.desai  …/airflow   vault-login-cost-reduction $!?    
orbstack  ♥ 12:30  docker exec vault-test cat /vault/logs/audit.log \
     | jq -r 'select(.type=="request") | .request.path' | sort | uniq -c
      1 auth/approle/login
      2 auth/token/lookup-self
      1 secret/data/variables/demo
   ```
   
   After:
   
   ```shell
   amoghrajesh.desai  …/airflow   vault-login-cost-reduction $!?    
orbstack  ♥ 12:26  docker exec vault-test cat /vault/logs/audit.log \
     | jq -r 'select(.type=="request") | .request.path' | sort | uniq -c
      1 auth/approle/login
      1 secret/data/variables/demo
   ```
   
   
   ### Two variable lookup
   
   Before and after changes, ran: `airflow variables get demo` and then 
`airflow variables get demo2` in same session
   
   Before:
   ```shell
   amoghrajesh.desai  …/airflow   vault-login-cost-reduction $!?    
orbstack  ♥ 12:31  docker exec vault-test cat /vault/logs/audit.log \
     | jq -r 'select(.type=="request") | .request.path' | sort | uniq -c
      2 auth/approle/login
      4 auth/token/lookup-self
      1 secret/data/variables/demo
      1 secret/data/variables/demo2
   ```
   
   After:
   ```shell
   amoghrajesh.desai  …/airflow   vault-login-cost-reduction $!?    
orbstack  ♥ 12:32  docker exec vault-test cat /vault/logs/audit.log \
     | jq -r 'select(.type=="request") | .request.path' | sort | uniq -c        
                                                                                
                                                             2 
auth/approle/login
      1 secret/data/variables/demo
      1 secret/data/variables/demo2
   ```
   
   ### Summary
   
   <img width="589" height="135" alt="image" 
src="https://github.com/user-attachments/assets/5cb77e03-d7ce-46c2-9454-fdac93a8015c";
 />
   
   
   **Scales linearly. Two variables fetched in one process: 8 requests before, 
4 after.**
   
   ## Behaviour / Backcompat
   
   `VaultHook.get_conn()` hands the raw client to callers who then issue their 
own requests, so they cannot benefit from the retry the wrapped methods use. It 
keeps validating and rebuilding exactly as before, which preserves transparent
   re-authentication for that public entry point. The secrets backend never 
goes through `get_conn()`, so its hot path stays probe free and gets the full 
reduction.
   
   ---
   
   * Read the **[Pull Request 
Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)**
 for more information. Note: commit author/co-author name and email in commits 
become permanently public when merged.
   * For fundamental code changes, an Airflow Improvement Proposal 
([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals))
 is needed.
   * When adding dependency, check compliance with the [ASF 3rd Party License 
Policy](https://www.apache.org/legal/resolved.html#category-x).
   * For significant user-facing changes create newsfragment: 
`{pr_number}.significant.rst`, in 
[airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments).
 You can add this file in a follow-up commit after the PR is created so you 
know the PR number.
   


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to