遮蔽敏感資料¶
預設情況下,當 Connection 密碼、敏感 Variables 和 Connection 的額外 (JSON) 欄位中的金鑰出現在任務日誌、Variables 檢視以及 UI 中的渲染欄位檢視中時,Airflow 會對其進行遮蔽。
它透過查詢在您的輸出中出現的特定 值 來實現這一點。這意味著如果您有一個密碼為 a 的連線,那麼日誌中每個字母 a 的例項都將被替換為 ***。
要停用遮蔽,您可以將 hide_sensitive_var_conn_fields 設定為 false。
自動遮蔽由 Connection 或 Variable 訪問觸發。這意味著如果您透過 XCom 或任何其他旁路通道傳遞敏感值,當其在下游任務中列印時將不會被遮蔽。
敏感欄位名稱¶
啟用遮蔽後,Airflow 將始終遮蔽任務訪問的每個 Connection 的密碼欄位。
如果 Variable 的值、渲染模板字典、XCom 字典或 Connection 的額外 JSON blob 的欄位名稱在已知敏感欄位列表(即 'access_token', 'api_key', 'apikey', 'authorization', 'passphrase', 'passwd', 'password', 'private_key', 'secret' 或 'token')中,它也會對其進行遮蔽。此列表也可以擴充套件
[core]
sensitive_var_conn_names = comma,separated,sensitive,names
新增您自己的遮蔽規則¶
如果您想遮蔽一個尚未透過上述方法遮蔽的額外金鑰,您可以在 DAG 檔案或 operator 的 execute 函式中使用 mask_secret 函式來實現。例如
@task
def my_func():
from airflow.sdk.execution_time.secrets_masker import mask_secret
mask_secret("custom_value")
...
或
class MyOperator(BaseOperator):
def execute(self, context):
from airflow.sdk.execution_time.secrets_masker import mask_secret
mask_secret("custom_value")
...
遮蔽必須在產生任何日誌/輸出之前設定才能生效。
使用環境變數時不進行遮蔽¶
當您使用某些 operator 時,例如 airflow.providers.cncf.kubernetes.operators.pod.KubernetesPodOperator,您可能會試圖透過環境變數傳遞金鑰。這是一個非常糟糕的做法,因為環境變數對任何有許可權檢視程序環境的人都是可見的 - 透過環境變數傳遞的此類金鑰將不會被 Airflow 遮蔽。
如果您需要向 KubernetesPodOperator 傳遞金鑰,您應該使用原生的 Kubernetes secrets 或使用 Airflow Connection 或 Variables 動態檢索金鑰。