Skip to content

Call init from auth managers only once#47869

Merged
vincbeck merged 2 commits into
apache:mainfrom
aws-mwaa:vincbeck/am_init
Mar 20, 2025
Merged

Call init from auth managers only once#47869
vincbeck merged 2 commits into
apache:mainfrom
aws-mwaa:vincbeck/am_init

Conversation

@vincbeck

Copy link
Copy Markdown
Contributor

The method init from auth managers is called multiples times for two reasons:

  • The method init is also called in the Flask applications. This is only needed for fab auth manager. Thus, I created a method specific init_flask_resources in FAB auth manager that I call in the Flask applications
  • fastapi spins up multiple workers (4 by default), therefore init is called 4 times. I updated the init function of simple auth manager to be called only once

^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
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.

Comment thread airflow/api_fastapi/auth/managers/simple/simple_auth_manager.py Fixed
Comment thread airflow/api_fastapi/auth/managers/simple/simple_auth_manager.py Outdated
Comment thread airflow/api_fastapi/auth/managers/simple/simple_auth_manager.py Outdated
if changed:
file.seek(0)
file.truncate()
file.write(json.dumps(passwords) + "\n")

Check failure

Code scanning / CodeQL

Clear-text storage of sensitive information

This expression stores [sensitive data (password)](1) as clear text. This expression stores [sensitive data (password)](2) as clear text.

@bugraoz93 bugraoz93 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks cool!

Comment thread airflow/api_fastapi/auth/managers/simple/simple_auth_manager.py Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR ensures that the auth manager initialization is only executed once per process, addressing the multiple invocations caused by the Flask application and FastAPI’s multi-worker environment. Key changes include:

  • Adding file locking in SimpleAuthManager to restrict the init method to a single execution.
  • Updating and adding tests to verify the correct handling of passwords and file content.
  • Modifying FAB auth manager to use a dedicated method (init_flask_resources) for initialization in Flask applications.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
tests/api_fastapi/auth/managers/simple/test_simple_auth_manager.py Added tests covering password file reading/updating and filtering of user entries.
airflow/api_fastapi/auth/managers/simple/simple_auth_manager.py Introduced file locking via fcntl in the init method and refactored password retrieval logic.
providers/fab/src/airflow/providers/fab/www/extensions/init_appbuilder.py Modified to call init_flask_resources instead of init for FAB auth manager.
providers/fab/src/airflow/providers/fab/auth_manager/fab_auth_manager.py Renamed init to init_flask_resources, aligning with the new initialization strategy.
Comments suppressed due to low confidence (1)

airflow/api_fastapi/auth/managers/simple/simple_auth_manager.py:127

  • Typo in the comment: 'User dot not exist in the file' should be corrected to 'User does not exist in the file'.
# User dot not exist in the file, adding it

Comment thread airflow/api_fastapi/auth/managers/simple/simple_auth_manager.py Outdated
vincbeck and others added 2 commits March 20, 2025 10:01
Co-authored-by: Jed Cunningham <66968678+jedcunningham@users.noreply.github.com>
@vincbeck
vincbeck merged commit afc284c into apache:main Mar 20, 2025
@vincbeck
vincbeck deleted the vincbeck/am_init branch March 20, 2025 15:26
agupta01 pushed a commit to agupta01/airflow that referenced this pull request Mar 21, 2025
shubham-pyc pushed a commit to shubham-pyc/airflow that referenced this pull request Mar 22, 2025
nailo2c pushed a commit to nailo2c/airflow that referenced this pull request Apr 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants