Google Cloud Secret Manager 後端¶
本主題描述如何設定 Airflow 使用 Secret Manager 作為密鑰後端,以及如何管理密鑰。
開始之前¶
在開始之前,請確保您已執行以下任務
將
google
子套件作為 Airflow 安裝的額外項目包含進來pip install apache-airflow[google]
設定 Secret Manager 和您的本地環境,每個專案設定一次。
啟用密鑰後端¶
若要為 Google Cloud Secret Manager 啟用密鑰後端以檢索連線/變數,請在 [secrets]
區段的 airflow.cfg
中,將 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"}
此外,如果您使用應用程式預設憑證 (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"}
設定憑證¶
您可以透過三種方式設定憑證
預設情況下,應用程式預設憑證 (ADC) 用於取得憑證。
backend_kwargs
選項中的gcp_key_path
選項 - 允許您使用儲存在本機檔案中的服務帳戶設定授權。backend_kwargs
選項中的gcp_keyfile_dict
選項 - 允許您使用儲存在 Airflow 設定中的服務帳戶設定授權。backend_kwargs
選項中的gcp_credential_config_file
選項 - 允許您使用憑證設定檔設定驗證。憑證設定檔是一種設定檔,通常包含非敏感性中繼資料,以指示google-auth
程式庫如何檢索外部主體權杖並將其交換為服務帳戶存取權杖。
管理密鑰¶
如果您想設定連線,您需要將其儲存為 (連線 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 連線。
檢查設定¶
您可以使用 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