Google Cloud Secret Manager 后端¶
本主题描述了如何配置 Airflow 使用 Secret Manager 作为密钥后端以及如何管理密钥。
开始之前¶
开始之前,请确保您已完成以下任务
在 Airflow 安装中包含
google
子包作为 extraspip install apache-airflow[google]
每个项目配置 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
库如何检索外部主体令牌并将其交换为服务账号访问令牌。
管理密钥¶
如果您想配置连接,需要将其保存为连接 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