Google Cloud Secret Manager 後端

本主題描述瞭如何配置 Airflow 使用 Secret Manager 作為金鑰後端以及如何管理金鑰。

開始之前

開始之前,請確保您已完成以下任務

  1. 在 Airflow 安裝中包含 google 子包作為 extras

    pip install apache-airflow[google]
    
  2. 每個專案配置 Secret Manager 和您的本地環境一次。

啟用金鑰後端

要啟用 Google Cloud Secrets Manager 的金鑰後端以檢索連線/變數,請在 airflow.cfg[secrets] 部分將 CloudSecretManagerBackend 指定為 backend

如果您想使用它,這裡是一個配置示例

[secrets]
backend = airflow.providers.google.cloud.secrets.secret_manager.CloudSecretManagerBackend

您也可以使用環境變數進行設定。

export AIRFLOW__SECRETS__BACKEND=airflow.providers.google.cloud.secrets.secret_manager.CloudSecretManagerBackend

您可以使用 airflow config get-value 命令驗證配置選項是否設定正確。

$ airflow config get-value secrets backend
airflow.providers.google.cloud.secrets.secret_manager.CloudSecretManagerBackend

後端引數

下一步是使用 backend_kwargs 選項配置後端引數。您可以傳遞以下引數

  • connections_prefix: 指定用於讀取連線的金鑰字首。預設值: "airflow-connections"

  • variables_prefix: 指定用於讀取變數的金鑰字首。預設值: "airflow-variables"

  • gcp_key_path: Google Cloud 服務賬號金鑰檔案 (JSON) 的路徑。

  • gcp_keyfile_dict: 金鑰檔案引數的字典。

  • gcp_credential_config_file: GCP 憑據配置檔案路徑或內容。

  • gcp_scopes: 包含 OAuth2 作用域的逗號分隔字串。

  • sep: 用於連線 connections_prefix 和 conn_id 的分隔符。預設值: "-"

  • project_id: 用於讀取金鑰的專案 ID。如果未傳遞,將使用憑據中的專案 ID。

  • impersonation_chain: 可選的服務賬號,用於使用短期憑據進行模擬,或獲取列表中最後一個賬號的訪問令牌所需的賬號鏈,該賬號將在請求中被模擬。

所有選項應作為 JSON 字典傳遞。

例如,如果您想將引數 connections_prefix 設定為 "example-connections-prefix",將引數 variables_prefix 設定為 "example-variables-prefix",您的配置檔案應如下所示

[secrets]
backend = airflow.providers.google.cloud.secrets.secret_manager.CloudSecretManagerBackend
backend_kwargs = {"connections_prefix": "example-connections-prefix", "variables_prefix": "example-variables-prefix"}

此外,如果您正在使用 Application Default Credentials (ADC) 從 example-project 讀取金鑰,但希望模擬其他服務賬號,您的配置應類似於此

[secrets]
backend = airflow.providers.google.cloud.secrets.secret_manager.CloudSecretManagerBackend
backend_kwargs = {"project_id": "example-project", "impersonation_chain": "impersonated_account@example_project.iam.gserviceaccount.com"}

設定憑據

您可以透過三種方式配置憑據

  • 預設情況下,使用 Application Default Credentials (ADC) 獲取憑據。

  • backend_kwargs 選項中的 gcp_key_path 選項 - 允許您使用儲存在本地檔案中的服務賬號配置授權。

  • backend_kwargs 選項中的 gcp_keyfile_dict 選項 - 允許您使用儲存在 Airflow 配置中的服務賬號配置授權。

  • backend_kwargs 選項中的 gcp_credential_config_file 選項 - 允許您使用憑據配置檔案配置認證。憑據配置檔案是一種配置檔案,通常包含非敏感的元資料,用於指導 google-auth 庫如何檢索外部主體令牌並將其交換為服務賬號訪問令牌。

注意

有關 Application Default Credentials (ADC) 的更多資訊,請參閱

管理金鑰

如果您想配置連線,需要將其儲存為連線 URI 表示形式。變數應儲存為純文字。

為了管理金鑰,您可以使用 gcloud 工具或其他受支援的工具。有關更多資訊,請參閱:Google Cloud 文件中的管理金鑰

金鑰的名稱必須符合以下格式

  • 對於連線: [connections_prefix][sep][connection_name]

  • 對於變數: [variables_prefix][sep][variable_name]

  • 對於 Airflow 配置: [config_prefix][sep][config_name]

其中

  • connections_prefix - 在後端配置的 connections_prefix 引數中定義的固定值。預設值: airflow-connections

  • variables_prefix - 在後端配置的 variables_prefix 引數中定義的固定值。預設值: airflow-variables

  • config_prefix - 在後端配置的 config_prefix 引數中定義的固定值。預設值: airflow-config

  • sep - 在後端配置的 sep 引數中定義的固定值。預設值: -

Cloud Secrets Manager 的金鑰名稱應遵循模式 ^[a-zA-Z0-9-_]*$

如果您使用預設的後端配置,並希望建立一個 conn_id 等於 first-connection 的連線,您應該建立一個名為 airflow-connections-first-connection 的金鑰。您可以使用 gcloud 工具完成此操作,如下例所示。

$ echo "mysql://example.org" | gcloud beta secrets create \
    airflow-connections-first-connection \
    --data-file=- \
    --replication-policy=automatic
Created version [1] of the secret [airflow-connections-first-connection].

如果您使用預設的後端配置,並希望建立一個名為 first-variable 的變數,您應該建立一個名為 airflow-variables-first-variable 的金鑰。您可以使用 gcloud 命令完成此操作,如下例所示。

$ echo "secret_content" | gcloud beta secrets create \
    airflow-variables-first-variable \
    --data-file=-\
    --replication-policy=automatic
Created version [1] of the secret [airflow-variables-first-variable].

注意

如果只需要隱藏連線中的敏感資訊,可以選擇僅將該資訊儲存在 Cloud Secret Manager 中,而不是儲存整個連線。更多詳細資訊請參閱Google Cloud Connection

檢查配置

您可以使用 airflow connections get 命令檢查連線是否從後端金鑰正確讀取

$ airflow connections get first-connection
Id: null
Connection Id: first-connection
Connection Type: mysql
Host: example.org
Schema: ''
Login: null
Password: null
Port: null
Is Encrypted: null
Is Extra Encrypted: null
Extra: {}
URI: mysql://example.org

要檢查變數是否從後端金鑰正確讀取,您可以使用 airflow variables get

$ airflow variables get first-variable
secret_content

清理

為避免您的 Google Cloud 賬號因本指南中使用的資源產生費用,請執行 gcloud beta secrets delete 命令刪除金鑰

gcloud beta secrets delete airflow-connections-first-connection
gcloud beta secrets delete airflow-variables-first-variable

本條目有幫助嗎?