dstandish commented on a change in pull request #21478: URL: https://github.com/apache/airflow/pull/21478#discussion_r803121386
########## File path: airflow/www/session.py ########## @@ -0,0 +1,40 @@ +# 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. + +from flask import request +from flask.sessions import SecureCookieSessionInterface +from flask_session.sessions import SqlAlchemySessionInterface + + +class SesssionExemptBlueprintMixin: Review comment: name makes it seem like it's a blueprint or specific to blueprints but it seems a little more general e.g. exempting the `health` endpoint ########## File path: airflow/utils/db.py ########## @@ -55,6 +55,9 @@ # We need to add this model manually to get reset working well from airflow.models.serialized_dag import SerializedDagModel # noqa: F401 + +# Alias this to WebSession so it doesn't conflict with sqla.orm.session.Session +from airflow.models.session import Session as WebSession # noqa: F401 Review comment: maybe we should name the model itself WebSession so as to not conflict? ########## File path: airflow/www/extensions/init_session.py ########## @@ -15,33 +15,44 @@ # specific language governing permissions and limitations # under the License. -from flask import request, session as flask_session -from flask.sessions import SecureCookieSessionInterface +from flask import session as builtin_flask_session - -class AirflowSessionInterface(SecureCookieSessionInterface): - """ - Airflow cookie session interface. - Modifications of sessions should be done here because - the change here is global. - """ - - def save_session(self, *args, **kwargs): - """Prevent creating session from REST API requests.""" - if request.blueprint == '/api/v1': - return None - return super().save_session(*args, **kwargs) - - -def init_permanent_session(app): - """Make session permanent to allows us to store data""" - - def make_session_permanent(): - flask_session.permanent = True - - app.before_request(make_session_permanent) +from airflow.configuration import conf +from airflow.exceptions import AirflowConfigException +from airflow.www.session import AirflowDatabaseSessionInterface, AirflowSecureCookieSessionInterface def init_airflow_session_interface(app): """Set airflow session interface""" - app.session_interface = AirflowSessionInterface() + config = app.config.copy() + selected_backend = conf.get('webserver', 'SESSION_BACKEND') + # A bit of a misnomer - normally cookies expire whenever the browser is closed + # or when they hit their expiry datetime, whichever comes first. "Permanent" + # cookies only expire when they hit their expiry datetime, and can outlive + # the browser being closed. + permanent_cookie = config.get('SESSION_PERMANENT', True) + + if selected_backend == 'securecookie': + app.session_interface = AirflowSecureCookieSessionInterface() + if permanent_cookie: + + def make_session_permanent(): + builtin_flask_session.permanent = True + + app.before_request(make_session_permanent) + elif selected_backend == 'database': + app.session_interface = AirflowDatabaseSessionInterface( + app=app, + db=None, + # These options come from Flask-Session, but are hard-coded Review comment: meaning of this comment could be made a little clearer -- 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]
