Hashicorp Vault 金鑰後端

要啟用 Hashicorp Vault 來檢索 Airflow 連線/變數,請在 `airflow.cfg` 檔案的 `[secrets]` 部分中將 VaultBackend 指定為 `backend`。

這是一個示例配置

[secrets]
backend = airflow.providers.hashicorp.secrets.vault.VaultBackend
backend_kwargs = {"connections_path": "connections", "variables_path": "variables", "mount_point": "airflow", "url": "http://127.0.0.1:8200"}

預設的 KV 版本引擎是 `2`,如果您使用 KV Secrets Engine Version `1`,請在 `backend_kwargs` 中傳遞 `kv_engine_version: 1`。

您還可以透過設定環境變數來設定值並將其傳遞給 Vault 客戶端。 https://www.vaultproject.io/docs/commands/#environment-variables 中列出的所有環境變數都受支援。

因此,如果您按如下方式設定 `VAULT_ADDR` 環境變數,則無需將 `url` 鍵傳遞給 `backend_kwargs`。

export VAULT_ADDR="http://127.0.0.1:8200"

設定 Vault 掛載點

您可以按如下方式為 `airflow` 設定一個 `mount_point`。

vault secrets enable -path=airflow -version=2 kv

可選查詢

可選地,可以互斥地或以任意組合方式查詢連線、變數或配置。這將阻止向 Vault 傳送被排除型別的請求。

如果您只想查詢 Vault 中的某些型別而排除其他型別,可以透過將要排除的型別的相關 `*_path` 引數設定為 `null` 來實現。

例如,如果您想將引數 `connections_path` 設定為 `"airflow-connections"` 並且不查詢變數,您的配置檔案應如下所示:

[secrets]
backend = airflow.providers.hashicorp.secrets.vault.VaultBackend
backend_kwargs = {"connections_path": "airflow-connections", "variables_path": null, "mount_point": "airflow", "url": "http://127.0.0.1:8200"}

使用連線 URI 表示儲存和檢索連線

如果您將 `connections_path` 設定為 `connections`,將 `mount_point` 設定為 `airflow`,那麼對於連線 ID 為 `smtp_default` 的連線,您需要按如下方式儲存您的金鑰:

vault kv put airflow/connections/smtp_default conn_uri=smtps://user:host@relay.example.com:465

請注意,`Key` 是 `conn_uri`,`Value` 是 `smtps://user:host@relay.example.com:465`,並且 `mount_point` 是 `airflow`。

驗證您可以從 `vault` 獲取金鑰

❯ vault kv get airflow/connections/smtp_default
====== Metadata ======
Key              Value
---              -----
created_time     2020-03-19T19:17:51.281721Z
deletion_time    n/a
destroyed        false
version          1

====== Data ======
Key         Value
---         -----
conn_uri    smtps://user:host@relay.example.com:465

Vault 鍵的值必須是連線物件的 connection URI 表示形式 以獲取連線。

使用 Connection 類表示儲存和檢索連線

如果您將 `connections_path` 設定為 `connections`,將 `mount_point` 設定為 `airflow`,那麼對於連線 ID 為 `smtp_default` 的連線,您需要按如下方式儲存您的金鑰:

vault kv put airflow/connections/smtp_default conn_type=smtps login=user password=host host=relay.example.com port=465

請注意,`Keys` 是 `Connection` 類的引數,`Value` 是它們的引數值。

驗證您可以從 `vault` 獲取金鑰

❯ vault kv get airflow/connections/smtp_default
====== Metadata ======
Key              Value
---              -----
created_time     2020-03-19T19:17:51.281721Z
deletion_time    n/a
destroyed        false
version          1

====== Data ======
Key         Value
---         -----
conn_type   smtps
login       user
password    host
host        relay.example.com
port        465

儲存和檢索變數

如果您將 `variables_path` 設定為 `variables`,將 `mount_point` 設定為 `airflow`,那麼對於鍵為 `hello` 的變數,您需要按如下方式儲存您的金鑰:

vault kv put airflow/variables/hello value=world

驗證您可以從 `vault` 獲取金鑰

❯ vault kv get airflow/variables/hello
====== Metadata ======
Key              Value
---              -----
created_time     2020-03-28T02:10:54.301784Z
deletion_time    n/a
destroyed        false
version          1

==== Data ====
Key      Value
---      -----
value    world

請注意,金鑰 `Key` 是 `value`,金鑰 `Value` 是 `world`,並且 `mount_point` 是 `airflow`。

儲存和檢索配置

如果您將 `config_path` 設定為 `config`,將 `mount_point` 設定為 `airflow`,那麼對於配置項 `sql_alchemy_conn_secret`,其值為 `sql_alchemy_conn_value`,您需要按如下方式儲存您的金鑰:

vault kv put airflow/config/sql_alchemy_conn_value value=postgres://user:pass@host:5432/db?ssl_mode=disable

驗證您可以從 `vault` 獲取金鑰

❯ vault kv get airflow/config/sql_alchemy_conn_value
====== Metadata ======
Key              Value
---              -----
created_time     2020-03-28T02:10:54.301784Z
deletion_time    n/a
destroyed        false
version          1

==== Data ====
Key      Value
---      -----
value    postgres://user:pass@host:5432/db?ssl_mode=disable

然後您可以在配置檔案中使用上述金鑰作為 `sql_alchemy_conn_secret` 的值。

[core]
 sql_alchemy_conn_secret: "sql_alchemy_conn_value"

請注意,金鑰 `Key` 是 `value`,金鑰 `Value` 是 `postgres://user:pass@host:5432/db?ssl_mode=disable`,並且 `mount_point` 是 `airflow`。

Vault 使用自簽名證書執行

新增 “verify”: “ca 證書檔案的絕對路徑”

[secrets]
backend = airflow.providers.hashicorp.secrets.vault.VaultBackend
backend_kwargs = {"connections_path": "airflow-connections", "variables_path": null, "mount_point": "airflow", "url": "http://127.0.0.1:8200", "verify": "/etc/ssl/certs/ca-certificates"}

Vault 使用 AWS Assume Role STS 進行認證

新增引數 “assume_role_kwargs”: “AWS STS assume role 認證引數字典”

更多詳細資訊,請參閱 AWS Assume Role 認證文件:https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sts/client/assume_role.html

[secrets]
backend = airflow.providers.hashicorp.secrets.vault.VaultBackend
backend_kwargs = {"connections_path": "airflow-connections", "variables_path": null, "mount_point": "airflow", "url": "http://127.0.0.1:8200", "auth_type": "aws_iam", "assume_role_kwargs": {"arn:aws:iam::123456789000:role/hashicorp-aws-iam-role", "RoleSessionName": "Airflow"}}

使用多個掛載點

您可以使用多個掛載點來儲存您的金鑰。例如,您可能希望將 Airflow 例項配置儲存在一個只能由 Airflow 部署工具訪問的 Vault KV 引擎中,同時將變數和連線儲存在另一個可供 DAG 訪問的 KV 引擎中,以便授予它們更具體的 Vault ACL。

為此,您需要按如下方式設定您的配置:

  • 將 `mount_point` 保留為 JSON `null`

  • 如果您使用 `variables_path` 和/或 `connections_path`,請將它們設定為 `"mount_point/path/to/the/secrets"`(字串將使用分隔符 `/` 進行分割,第一個元素將是掛載點,其餘元素將是金鑰的路徑)

  • 將 `config_path` 保留為空字串 `""`

  • 如果您使用 `config_path`,每個配置項都需要加上用於配置的 `mount_point` 字首,格式為 `"mount_point/path/to/the/config"`(同樣,字串將使用分隔符 `/` 進行分割,第一個元素將是掛載點,其餘元素將是配置引數的路徑)

例如

[core]
sql_alchemy_conn_secret: "deployment_mount_point/airflow/configs/sql_alchemy_conn_value"

[secrets]
backend = airflow.providers.hashicorp.secrets.vault.VaultBackend
backend_kwargs = {"connections_path": "dags_mount_point/airflow/connections", "variables_path": "dags_mount_point/airflow/variables", "config_path": "", mount_point": null, "url": "http://127.0.0.1:8200", "verify": "/etc/ssl/certs/ca-certificates"}

此條目有幫助嗎?