Skip to content

Commit

Permalink
openlineage: do not try to redact Proxy objects from deprecated config (
Browse files Browse the repository at this point in the history
#33393)

Signed-off-by: Maciej Obuchowski <obuchowski.maciej@gmail.com>
  • Loading branch information
mobuchowski authored Aug 15, 2023
1 parent 9927bda commit 8e738cd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
60 changes: 36 additions & 24 deletions airflow/providers/openlineage/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
AirflowMappedTaskRunFacet,
AirflowRunFacet,
)
from airflow.utils.context import AirflowContextDeprecationWarning
from airflow.utils.log.secrets_masker import Redactable, Redacted, SecretsMasker, should_hide_value_for_key

if TYPE_CHECKING:
Expand Down Expand Up @@ -345,37 +346,48 @@ def _redact(self, item: Redactable, name: str | None, depth: int, max_depth: int
if depth > max_depth:
return item
try:
if name and should_hide_value_for_key(name):
return self._redact_all(item, depth, max_depth)
if attrs.has(type(item)):
# TODO: fixme when mypy gets compatible with new attrs
for dict_key, subval in attrs.asdict(item, recurse=False).items(): # type: ignore[arg-type]
if _is_name_redactable(dict_key, item):
setattr(
item,
dict_key,
self._redact(subval, name=dict_key, depth=(depth + 1), max_depth=max_depth),
)
return item
elif is_json_serializable(item) and hasattr(item, "__dict__"):
for dict_key, subval in item.__dict__.items():
if _is_name_redactable(dict_key, item):
setattr(
item,
dict_key,
self._redact(subval, name=dict_key, depth=(depth + 1), max_depth=max_depth),
)
return item
else:
return super()._redact(item, name, depth, max_depth)
# It's impossible to check the type of variable in a dict without accessing it, and
# this already causes warning - so suppress it
with suppress(AirflowContextDeprecationWarning):
if type(item).__name__ == "Proxy":
# Those are deprecated values in _DEPRECATION_REPLACEMENTS
# in airflow.utils.context.Context
return "<<non-redactable: Proxy>>"
if name and should_hide_value_for_key(name):
return self._redact_all(item, depth, max_depth)
if attrs.has(type(item)):
# TODO: fixme when mypy gets compatible with new attrs
for dict_key, subval in attrs.asdict(
item, recurse=False # type: ignore[arg-type]
).items():
if _is_name_redactable(dict_key, item):
setattr(
item,
dict_key,
self._redact(subval, name=dict_key, depth=(depth + 1), max_depth=max_depth),
)
return item
elif is_json_serializable(item) and hasattr(item, "__dict__"):
for dict_key, subval in item.__dict__.items():
if type(subval).__name__ == "Proxy":
return "<<non-redactable: Proxy>>"
if _is_name_redactable(dict_key, item):
setattr(
item,
dict_key,
self._redact(subval, name=dict_key, depth=(depth + 1), max_depth=max_depth),
)
return item
else:
return super()._redact(item, name, depth, max_depth)
except Exception as e:
log.warning(
"Unable to redact %s. Error was: %s: %s",
repr(item),
type(e).__name__,
str(e),
)
return item
return item


def is_json_serializable(item):
Expand Down
6 changes: 6 additions & 0 deletions tests/providers/openlineage/plugins/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ class NotMixin:
def __init__(self):
self.password = "passwd"

class Proxy:
pass

def default(self, o):
if isinstance(o, NotMixin):
return o.__dict__
Expand All @@ -180,6 +183,9 @@ def default(self, o):
monkeypatch.setattr(JSONEncoder, "default", default)
assert redactor.redact(NotMixin()).password == "***"

assert redactor.redact(Proxy()) == "<<non-redactable: Proxy>>"
assert redactor.redact({"a": "a", "b": Proxy()}) == {"a": "a", "b": "<<non-redactable: Proxy>>"}

class Mixined(RedactMixin):
_skip_redact = ["password"]

Expand Down

0 comments on commit 8e738cd

Please sign in to comment.