forked from airbytehq/airbyte
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CDK: airbytehq#3764 obfuscate secrets in logging (airbytehq#8211)
* Secure logger implementation minus still broken new tests * Secure logger implementation and unit tests * code review changes * filter text on uncaught exceptions * auto-formatting * Mistaken change left in code * filter text on uncaught exceptions * Simplify code, remove LoggingFilter and move obfuscation functionality to Formatter * Update airbyte-cdk/python/airbyte_cdk/entrypoint.py Co-authored-by: Eugene Kulak <widowmakerreborn@gmail.com> * Obfuscate Secrets in Logging, code review changes * Obfuscate Secrets in Logging, code review changes, unit test fixes * CHANGELOG.md Co-authored-by: Eugene Kulak <widowmakerreborn@gmail.com>
- Loading branch information
Showing
9 changed files
with
346 additions
and
4 deletions.
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
24 changes: 24 additions & 0 deletions
24
airbyte-cdk/python/airbyte_cdk/utils/airbyte_secrets_utils.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,24 @@ | ||
# | ||
# Copyright (c) 2021 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
import logging | ||
from typing import Any, List, Mapping | ||
|
||
from airbyte_cdk.sources import Source | ||
from airbyte_cdk.utils.mapping_utils import all_key_pairs_dot_notation, get_value_by_dot_notation | ||
|
||
|
||
def get_secrets(source: Source, config: Mapping[str, Any], logger: logging.Logger) -> List[Any]: | ||
""" | ||
Get a list of secrets from the source config based on the source specification | ||
""" | ||
flattened_key_values = all_key_pairs_dot_notation(source.spec(logger).connectionSpecification.get("properties", {})) | ||
secret_key_names = [ | ||
".".join(key.split(".")[:1]) for key, value in flattened_key_values.items() if value and key.endswith("airbyte_secret") | ||
] | ||
return [ | ||
str(get_value_by_dot_notation(config, key)) | ||
for key in secret_key_names | ||
if config.get(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,41 @@ | ||
# | ||
# Copyright (c) 2021 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
from functools import reduce | ||
from typing import Any, List, Mapping, Optional | ||
|
||
|
||
def all_key_pairs_dot_notation(dict_obj: Mapping) -> Mapping[str, Any]: | ||
""" | ||
Recursively iterate through a dictionary and return a dictionary of all key-value pairs in dot notation. | ||
keys are prefixed with the list of keys passed in as prefix. | ||
""" | ||
|
||
def _all_key_pairs_dot_notation(_dict_obj: Mapping, prefix: List[str] = []) -> Mapping[str, Any]: | ||
for key, value in _dict_obj.items(): | ||
if isinstance(value, dict): | ||
prefix.append(str(key)) | ||
yield from _all_key_pairs_dot_notation(value, prefix) | ||
prefix.pop() | ||
else: | ||
prefix.append(str(key)) | ||
yield ".".join(prefix), value | ||
prefix.pop() | ||
|
||
return {k: v for k, v in _all_key_pairs_dot_notation(dict_obj)} | ||
|
||
|
||
def get_value_by_dot_notation(dict_obj: Mapping, key: str, default: Optional[Any] = ...) -> Any: | ||
""" | ||
Return the value of a key in dot notation in a arbitrarily nested Mapping. | ||
dict_obj: Mapping | ||
key: str | ||
default: Any | ||
raises: KeyError if default is not provided and the key is not found | ||
ex.: | ||
dict_obj = {"nested": {"key": "value"}} | ||
get_value_by_dot_notation(dict_obj, "nested.key") == "value" -> True | ||
""" | ||
|
||
return reduce(lambda d, key_name: d[key_name] if default is ... else d.get(key_name, default), key.split("."), dict_obj) |
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
Oops, something went wrong.