-
Notifications
You must be signed in to change notification settings - Fork 13.5k
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
feat(keys-rotation): manage cookie secret key #15296
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f62ae09
feat: add keys-management as new dependency
ofekisr bceaf24
feat: defining new optional dependency
ofekisr 7390499
feat: init keys_management
ofekisr b500f19
feat: add signing cookie key definition
ofekisr 4dc87f0
fix lints
ofekisr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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,47 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's keep init empty. |
||
# 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 __future__ import annotations | ||
|
||
from typing import Dict, TYPE_CHECKING | ||
|
||
from keys_management import EnvironemtType, KeysManagementImpl | ||
|
||
from superset.exceptions import SupersetException | ||
|
||
from .cookie_signing_key import define_cookie_signing_key | ||
|
||
if TYPE_CHECKING: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this? |
||
from superset.app import SupersetApp | ||
|
||
|
||
def configure(app: SupersetApp) -> None: | ||
keys_management_configurations = app.config.get("SECRET_KEYS_MANAGEMENT", {}) | ||
keys_management = KeysManagementImpl( | ||
environemt_type=determine_environment_type(keys_management_configurations) | ||
) | ||
app.keys_management = keys_management | ||
define_cookie_signing_key(app) | ||
|
||
|
||
def determine_environment_type( | ||
keys_management_configurations: Dict[str, str] | ||
) -> EnvironemtType: | ||
environment_type = keys_management_configurations.get("ENVIRONMENT_TYPE", "single") | ||
try: | ||
return EnvironemtType[environment_type.upper()] | ||
except KeyError as ex: | ||
raise SupersetException("environment_type config value is invalid", ex) |
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,18 @@ | ||
# 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. | ||
|
||
COOKIE_SIGNING_KEY = "COOKIE_SIGNING_KEY" |
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,69 @@ | ||
# 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 __future__ import annotations | ||
|
||
from typing import Optional, TYPE_CHECKING | ||
|
||
from flask.sessions import SecureCookieSessionInterface | ||
from itsdangerous import URLSafeTimedSerializer | ||
from keys_management import OnChangeKeyDefinition, SecretKeyFlow, SecretKeyUseCase | ||
|
||
from .consts import COOKIE_SIGNING_KEY | ||
|
||
if TYPE_CHECKING: | ||
from superset.app import SupersetApp | ||
|
||
|
||
def define_cookie_signing_key(app: SupersetApp) -> None: | ||
# pylint: disable=W0613 | ||
app.keys_management.define_key( # type: ignore | ||
name=COOKIE_SIGNING_KEY, | ||
keys_store=lambda: app.config.get(COOKIE_SIGNING_KEY), | ||
use_case=SecretKeyUseCase.ONE_WAY_TRIP, | ||
stateless=True, | ||
keep_in_cache=False, | ||
) | ||
|
||
def on_key_change( | ||
old_key: str, new_key: str, on_change_key_definition: OnChangeKeyDefinition | ||
) -> None: | ||
app.config[COOKIE_SIGNING_KEY] = new_key | ||
|
||
app.keys_management.register_on_change( # type: ignore | ||
COOKIE_SIGNING_KEY, on_key_change | ||
) | ||
app.session_interface = SupersetSecureCookieSession() | ||
|
||
|
||
class SupersetSecureCookieSession(SecureCookieSessionInterface): | ||
def get_signing_serializer( | ||
self, app: SupersetApp | ||
) -> Optional[URLSafeTimedSerializer]: | ||
current_signing_key = app.keys_management.get_key( # type: ignore | ||
COOKIE_SIGNING_KEY, SecretKeyFlow.DEFAULT | ||
) | ||
if not current_signing_key: | ||
return None | ||
signer_kwargs = dict( | ||
key_derivation=self.key_derivation, digest_method=self.digest_method | ||
) | ||
return URLSafeTimedSerializer( | ||
current_signing_key, | ||
salt=self.salt, | ||
serializer=self.serializer, | ||
signer_kwargs=signer_kwargs, | ||
) |
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.
Not sure I agree with this pattern of hanging fields off of the App. A more widely accepted/established pattern is to use a custom
extension
that can be added to the Flask app via theextensions
. We already have a mechanism for this (extensions.py)