gpt4 book ai didi

python - 如何在 Airflow 中使用 HashiCorp Vault?

转载 作者:行者123 更新时间:2023-12-03 14:38:35 27 4
gpt4 key购买 nike

我开始使用 Apache Airflow,我想知道如何有效地让它使用存储在 Vault 中的 secret 和密码。不幸的是,搜索不会返回超出 yet-to-be-implemented hook in Airflow 的有意义的答案。项目本身。

我总是可以使用 Python 的 hvacPythonOperator 访问 Vault 的模块但我想知道是否有更好的方法或好的做法(例如,我错过了一个 Airflow 插件)。

最佳答案

Airflow >=1.10.10 支持 Secrets Backends 并支持从 Hashicorp Vault 获取 Airflow 变量和连接。

Airflow 文档中的更多详细信息:https://airflow.apache.org/docs/stable/howto/use-alternative-secrets-backend.html#hashicorp-vault-secrets-backend

如果您想在本地测试它,请查看 https://www.astronomer.io/guides/airflow-and-hashicorp-vault/ 上的教程

airflow.cfg 中设置以下配置,根据您的环境更新:

backend = airflow.contrib.secrets.hashicorp_vault.VaultBackend
backend_kwargs = {"connections_path": "connections", "variables_path": "variables", "mount_point": "airflow", "url": "http://127.0.0.1:8200"}

用于测试集成的示例 DAG:

from airflow import DAG
from airflow.operators.python_operator import PythonOperator
from datetime import datetime
from airflow.hooks.base_hook import BaseHook


def get_secrets(**kwargs):
conn = BaseHook.get_connection(kwargs['my_conn_id'])
print(f"Password: {conn.password}, Login: {conn.login}, URI: {conn.get_uri()}, Host: {conn.host}")

with DAG('example_secrets_dags', start_date=datetime(2020, 1, 1), schedule_interval=None) as dag:


test_task = PythonOperator(
task_id='test-task',
python_callable=get_secrets,
op_kwargs={'my_conn_id': 'smtp_default'},
)

关于python - 如何在 Airflow 中使用 HashiCorp Vault?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58593247/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com