-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Refactor serve_logs with FastAPI #52581
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Refactor serve_logs with FastAPI #52581
Conversation
8be599f
to
99651ed
Compare
Fix test_invalid_characters_handled Refactor with StaticFiles
99651ed
to
b5facdf
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I though there isn't Flask'ssend_from_directory
alternative in FastAPI.
So I implement the validation for file path in first try ( and the security check is angry ).
Fortunately I found the gist for having authorization for FastAPI's StaticFiles
. ( linked in the PR description )
await self.validate_jwt_token(request) | ||
await super().__call__(scope, receive, send) | ||
|
||
async def validate_jwt_token(self, request: Request): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I move the validate_jwt_token
to new JWTAuthStaticFiles
class.
Also make it as async
method, since the unit test will raise some event loop issue related to async_to_sync
if I use the original signer.validated_claims(auth)
instead of await signer.avalidated_claims(auth)
.
raise ImportError(f"Unable to load {log_config_class} due to error: {e}") | ||
|
||
fastapi_app = FastAPI() | ||
fastapi_app.state.signer = JWTValidator( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have to set the signer
instance in app.state
just like what we do in core-api for dag_bag
to make it singleton.
|
||
options = [bind_option, GunicornOption("workers", 2)] | ||
StandaloneGunicornApplication(wsgi_app, options).run() | ||
# Use Uvicorn worker class for ASGI applications | ||
options = [ | ||
bind_option, | ||
GunicornOption("workers", 2), | ||
GunicornOption("worker_class", "uvicorn.workers.UvicornWorker"), | ||
] | ||
StandaloneGunicornApplication(asgi_app, options).run() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, I'm also think about replace StandaloneGunicornApplication
with uvicorn.run
. Since the api_server_command
use uvicorn.run
to start the whole core-api.
Any comment for this ?
airflow/airflow-core/src/airflow/cli/commands/api_server_command.py
Lines 106 to 117 in b5facdf
uvicorn.run( | |
"airflow.api_fastapi.main:app", | |
host=args.host, | |
port=args.port, | |
workers=num_workers, | |
timeout_keep_alive=worker_timeout, | |
timeout_graceful_shutdown=worker_timeout, | |
ssl_keyfile=ssl_key, | |
ssl_certfile=ssl_cert, | |
access_log=access_logfile, | |
proxy_headers=proxy_headers, | |
) |
closes: #52526
related: https://lists.apache.org/thread/hfr8q85rgr6knpp5wblbz301ysnmzhht
Why
What
Replace Flask's
send_from_directory
fastapi_app.mount
withJWTAuthStaticFiles
( which inherent from FastAPI'sStaticFiles
and extend the existed authorization. reference from fastapi/fastapi#858 (comment) )