This is an automated email from the ASF dual-hosted git repository. ephraimanierobi pushed a commit to branch v2-7-test in repository https://gitbox.apache.org/repos/asf/airflow.git
commit 0e513d83f924d0798400c9b01303022077718494 Author: Ephraim Anierobi <[email protected]> AuthorDate: Thu Aug 3 14:30:18 2023 +0100 Fix issue with using the various state enum value in logs (#33065) * Fix issue with using the various state enum value in logs The secrets masker is unable to work on the various state enums: DagRunState, TaskInstanceState, JobState, and State enums in logs. This PR fixes this by converting the enums to strings during the secrets mask search * fixup! Fix issue with using the various state enum value in logs * apply to all enum * test with custom enum (cherry picked from commit b0f61be2f9791b75da3bca0bc30fdbb88e1e0a8a) --- airflow/utils/log/secrets_masker.py | 3 +++ tests/utils/log/test_secrets_masker.py | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/airflow/utils/log/secrets_masker.py b/airflow/utils/log/secrets_masker.py index 2c5e4c47e0..c70e469a3c 100644 --- a/airflow/utils/log/secrets_masker.py +++ b/airflow/utils/log/secrets_masker.py @@ -20,6 +20,7 @@ from __future__ import annotations import collections.abc import logging import sys +from enum import Enum from functools import cached_property from typing import ( TYPE_CHECKING, @@ -242,6 +243,8 @@ class SecretsMasker(logging.Filter): for dict_key, subval in item.items() } return to_return + elif isinstance(item, Enum): + return self._redact(item=item.value, name=name, depth=depth, max_depth=max_depth) elif _is_v1_env_var(item): tmp: dict = item.to_dict() if should_hide_value_for_key(tmp.get("name", "")) and "value" in tmp: diff --git a/tests/utils/log/test_secrets_masker.py b/tests/utils/log/test_secrets_masker.py index e47cd5cee5..823662baa6 100644 --- a/tests/utils/log/test_secrets_masker.py +++ b/tests/utils/log/test_secrets_masker.py @@ -24,6 +24,7 @@ import logging.config import os import sys import textwrap +from enum import Enum from unittest.mock import patch import pytest @@ -36,6 +37,7 @@ from airflow.utils.log.secrets_masker import ( redact, should_hide_value_for_key, ) +from airflow.utils.state import DagRunState, JobState, State, TaskInstanceState from tests.test_utils.config import conf_vars settings.MASK_SECRETS_IN_LOGS = True @@ -43,6 +45,10 @@ settings.MASK_SECRETS_IN_LOGS = True p = "password" +class MyEnum(str, Enum): + testname = "testvalue" + + @pytest.fixture def logger(caplog): logging.config.dictConfig( @@ -299,6 +305,23 @@ class TestSecretsMasker: got = redact(val, max_depth=max_depth) assert got == expected + @pytest.mark.parametrize( + "state, expected", + [ + (DagRunState.SUCCESS, "success"), + (TaskInstanceState.FAILED, "failed"), + (JobState.RUNNING, "running"), + ([DagRunState.SUCCESS, DagRunState.RUNNING], ["success", "running"]), + ([TaskInstanceState.FAILED, TaskInstanceState.SUCCESS], ["failed", "success"]), + (State.failed_states, frozenset([TaskInstanceState.FAILED, TaskInstanceState.UPSTREAM_FAILED])), + (MyEnum.testname, "testvalue"), + ], + ) + def test_redact_state_enum(self, logger, caplog, state, expected): + logger.info("State: %s", state) + assert caplog.text == f"INFO State: {expected}\n" + assert "TypeError" not in caplog.text + class TestShouldHideValueForKey: @pytest.mark.parametrize(
