-
Notifications
You must be signed in to change notification settings - Fork 16.4k
Updated app to support configuring the caching hash method for FIPS #28846
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
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8d68708
Updated serialized_dag to pass `usedforsecurity=False`
7d98b5b
- Added caching_hash_method to config
e5e2d2b
- Added hashlib_wrapper and switched serialized dag to use it
593a976
- Switched check for usedforsecurity, which only worked in certain cases
2815baf
Revert unrelated config formatting
shyft-mike 294bbf8
Include newsfragment for PR
shyft-mike b611fe8
Update hashlib_wrapper params and docs
00cc3d1
Update config documentation and run pre-commit
fc0b3e4
Move and update hashlib_wrapper
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,38 @@ | ||
| # | ||
| # 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 | ||
|
|
||
| import hashlib | ||
|
|
||
| from airflow import PY39 | ||
|
|
||
|
|
||
| def md5(data: bytes, *, usedforsecurity: bool | None = None): | ||
| """ | ||
| Safely allows calling the hashlib.md5 function with the "usedforsecurity" param. | ||
|
|
||
| :param data: The data to hash. | ||
| :param usedforsecurity: The value to pass to the md5 function's "usedforsecurity" param. | ||
| Defaults to None. | ||
| :return: The hashed value. | ||
| :rtype: _Hash | ||
| """ | ||
| if PY39 and usedforsecurity is not None: | ||
| return hashlib.md5(data, usedforsecurity=usedforsecurity) # type: ignore | ||
| else: | ||
| return hashlib.md5(data) |
This file contains hidden or 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 hidden or 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,52 @@ | ||
| # 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 | ||
|
|
||
| import hashlib | ||
| from tempfile import gettempdir | ||
|
|
||
| from flask_caching import Cache | ||
|
|
||
| from airflow.configuration import conf | ||
| from airflow.exceptions import AirflowConfigException | ||
|
|
||
| HASH_METHOD_MAPPING = { | ||
| "md5": hashlib.md5, | ||
| "sha1": hashlib.sha1, | ||
| "sha224": hashlib.sha224, | ||
| "sha256": hashlib.sha256, | ||
| "sha384": hashlib.sha384, | ||
| "sha512": hashlib.sha512, | ||
| } | ||
|
|
||
|
|
||
| def init_cache(app): | ||
| webserver_caching_hash_method = conf.get( | ||
| section="webserver", key="CACHING_HASH_METHOD", fallback="md5" | ||
| ).casefold() | ||
| cache_config = {"CACHE_TYPE": "flask_caching.backends.filesystem", "CACHE_DIR": gettempdir()} | ||
|
|
||
| mapped_hash_method = HASH_METHOD_MAPPING.get(webserver_caching_hash_method) | ||
|
|
||
| if mapped_hash_method is None: | ||
| raise AirflowConfigException( | ||
| f"Unsupported webserver caching hash method: `{webserver_caching_hash_method}`." | ||
| ) | ||
|
|
||
| cache_config["CACHE_OPTIONS"] = {"hash_method": mapped_hash_method} | ||
|
|
||
| Cache(app=app, config=cache_config) | ||
This file contains hidden or 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 @@ | ||
| Various updates for FIPS-compliance when running Airflow in Python 3.9+. This includes a new webserver option, ``caching_hash_method``, for changing the default flask caching method. |
This file contains hidden or 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
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.
This doesn’t seem to be necessary; we could use
hashlib.new()or evengetattr.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.
Hmm unless I'm misunderstanding what you mean, this is needed because the flask_caching config takes a reference to the hashlib function itself. I was also recommended against
hashlib.new()since it is much less performant.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.
getattris the right answer here then 👍Uh oh!
There was an error while loading. Please reload this page.
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.
hashlib.new()should not be preferred when you know what argument you’re using.hashlib.md5(...)is more performant thanhashlib.new("md5", ...), but putting the functions behind a dict andgetattr(hashlib, "md5")(...)both negate most of the performance gain.To put it in another way:
But these three are more or less the same.
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'm not planning on changing anything related to this. The mapping gives us a reference to the actual function needed, which hashlib.new() doesn't do, and if I used getattr, I could give a config value of "name", or "new", or "file" and it would just pass that into flask cache. I'd rather just have a whitelist and give a descriptive error message rather than depending on it blowing up flask cache.