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
Alt: Add a session backend to store session data in the database #21478
Merged
jedcunningham
merged 14 commits into
apache:main
from
astronomer:alt_db-session-backend
Feb 15, 2022
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c8037b8
Vendor in Flask-Session/flask_session/sessions.py
blag 8bb3ba2
Modify the Flask-Session code
blag 7f00fbe
Add database migrations
blag 1a67c19
Add database backend for www sessions
blag 77286ed
Tweak tests and add tests for www sessions
blag 4e0e63f
Unvendor flask-session
jedcunningham 9708d7c
Actually install flask-session :)
jedcunningham 85cde3c
PR feedback
jedcunningham 00f8309
Remove model!
jedcunningham c446b50
Use singular table name
jedcunningham 018353f
Fix test
jedcunningham f3af9e3
Fix mysql tests
jedcunningham 24883f0
Undo DEPENDENCIES_EPOCH_NUMBER bump
jedcunningham 9b16f00
Set strict upper-bound for flask-session
jedcunningham File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
airflow/migrations/versions/c381b21cb7e4_add_session_table_to_db.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# | ||
# 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. | ||
|
||
"""add session table to db | ||
|
||
Revision ID: c381b21cb7e4 | ||
Revises: be2bfac3da23 | ||
Create Date: 2022-01-25 13:56:35.069429 | ||
|
||
""" | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = 'c381b21cb7e4' | ||
down_revision = 'be2bfac3da23' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
TABLE_NAME = 'session' | ||
|
||
|
||
def upgrade(): | ||
"""Apply add session table to db""" | ||
op.create_table( | ||
TABLE_NAME, | ||
sa.Column('id', sa.Integer()), | ||
sa.Column('session_id', sa.String(255)), | ||
sa.Column('data', sa.LargeBinary()), | ||
sa.Column('expiry', sa.DateTime()), | ||
sa.PrimaryKeyConstraint('id'), | ||
sa.UniqueConstraint('session_id'), | ||
) | ||
|
||
|
||
def downgrade(): | ||
"""Unapply add session table to db""" | ||
op.drop_table(TABLE_NAME) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 SesssionExemptMixin: | ||
"""Exempt certain blueprints/paths from autogenerated sessions""" | ||
|
||
def save_session(self, *args, **kwargs): | ||
"""Prevent creating session from REST API and health requests.""" | ||
if request.blueprint == '/api/v1': | ||
return None | ||
if request.path == '/health': | ||
return None | ||
return super().save_session(*args, **kwargs) | ||
|
||
|
||
class AirflowDatabaseSessionInterface(SesssionExemptMixin, SqlAlchemySessionInterface): | ||
"""Session interface that exempts some routes and stores session data in the database""" | ||
|
||
|
||
class AirflowSecureCookieSessionInterface(SesssionExemptMixin, SecureCookieSessionInterface): | ||
"""Session interface that exempts some routes and stores session data in a signed cookie""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1255,6 +1255,7 @@ sdk | |
secretRef | ||
secretRefs | ||
securable | ||
securecookie | ||
securestring | ||
securityManager | ||
seealso | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# 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. | ||
|
||
import pytest | ||
|
||
from airflow.exceptions import AirflowConfigException | ||
from airflow.www import app | ||
from tests.test_utils.config import conf_vars | ||
from tests.test_utils.decorators import dont_initialize_flask_app_submodules | ||
|
||
|
||
def test_session_cookie_created_on_login(user_client): | ||
assert any(cookie.name == 'session' for cookie in user_client.cookie_jar) | ||
|
||
|
||
def test_session_inaccessible_after_logout(user_client): | ||
session_cookie = next((cookie for cookie in user_client.cookie_jar if cookie.name == 'session'), None) | ||
assert session_cookie is not None | ||
|
||
resp = user_client.get('/logout/') | ||
assert resp.status_code == 302 | ||
|
||
# Try to access /home with the session cookie from earlier | ||
user_client.set_cookie('session', session_cookie.value) | ||
user_client.get('/home/') | ||
assert resp.status_code == 302 | ||
|
||
|
||
def test_invalid_session_backend_option(): | ||
@dont_initialize_flask_app_submodules( | ||
skip_all_except=[ | ||
"init_api_connexion", | ||
"init_appbuilder", | ||
"init_appbuilder_links", | ||
"init_appbuilder_views", | ||
"init_flash_views", | ||
"init_jinja_globals", | ||
"init_plugins", | ||
"init_airflow_session_interface", | ||
] | ||
) | ||
def poorly_configured_app_factory(): | ||
with conf_vars({("webserver", "session_backend"): "invalid_value_for_session_backend"}): | ||
return app.create_app(testing=True) | ||
|
||
expected_exc_regex = ( | ||
"^Unrecognized session backend specified in web_server_session_backend: " | ||
r"'invalid_value_for_session_backend'\. Please set this to .+\.$" | ||
) | ||
with pytest.raises(AirflowConfigException, match=expected_exc_regex): | ||
poorly_configured_app_factory() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 think we should also add an ORM model for this table so that it's not only through migration file that the table can be created.
I'm working on a change that would have new DBs created through the ORM rather than going through the migration files. #21462
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 can do it like this I think
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.
(If we should or not is the next question)
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.
This is what it ends up looking like, when considering #21462's changes: