You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by ur...@apache.org on 2023/03/10 07:22:33 UTC

[airflow] branch main updated: Delay Kubernetes import in secret masker (#29993)

This is an automated email from the ASF dual-hosted git repository.

uranusjr pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 082bcab9dc Delay Kubernetes import in secret masker (#29993)
082bcab9dc is described below

commit 082bcab9dc56c6ff2bf41437462d3d6f5e37c9eb
Author: Tzu-ping Chung <ur...@gmail.com>
AuthorDate: Fri Mar 10 15:22:24 2023 +0800

    Delay Kubernetes import in secret masker (#29993)
---
 airflow/utils/log/secrets_masker.py | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/airflow/utils/log/secrets_masker.py b/airflow/utils/log/secrets_masker.py
index cbbcf8ac17..e6e8352408 100644
--- a/airflow/utils/log/secrets_masker.py
+++ b/airflow/utils/log/secrets_masker.py
@@ -22,6 +22,7 @@ import logging
 import re
 import sys
 from typing import (
+    TYPE_CHECKING,
     Any,
     Callable,
     Dict,
@@ -36,15 +37,12 @@ from typing import (
 
 from airflow import settings
 from airflow.compat.functools import cache, cached_property
+from airflow.typing_compat import TypeGuard
 
-try:
-    # kubernetes provider may not be installed
+if TYPE_CHECKING:
     from kubernetes.client import V1EnvVar
-except ImportError:
-    V1EnvVar = type("V1EnvVar", (), {})  # keep mypy happy about the V1EnvVar check
 
-
-Redactable = TypeVar("Redactable", str, Dict[Any, Any], Tuple[Any, ...], List[Any])
+Redactable = TypeVar("Redactable", str, "V1EnvVar", Dict[Any, Any], Tuple[Any, ...], List[Any])
 Redacted = Union[Redactable, str]
 
 log = logging.getLogger(__name__)
@@ -128,6 +126,19 @@ def _secrets_masker() -> SecretsMasker:
     )
 
 
+@cache
+def _get_v1_env_var_type() -> type:
+    try:
+        from kubernetes.client import V1EnvVar
+    except ImportError:
+        return type("V1EnvVar", (), {})
+    return V1EnvVar
+
+
+def _is_v1_env_var(v: Any) -> TypeGuard[V1EnvVar]:
+    return isinstance(v, _get_v1_env_var_type())
+
+
 class SecretsMasker(logging.Filter):
     """Redact secrets from logs"""
 
@@ -223,7 +234,7 @@ class SecretsMasker(logging.Filter):
                     for dict_key, subval in item.items()
                 }
                 return to_return
-            elif isinstance(item, V1EnvVar):
+            elif _is_v1_env_var(item):
                 tmp: dict = item.to_dict()
                 if should_hide_value_for_key(tmp.get("name", "")) and "value" in tmp:
                     tmp["value"] = "***"