如何在气流中使用HashiCorp保险库?

问题描述 投票:5回答:1

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

我总是可以使用Python的hvac模块从PythonOperator通用访问保险柜,但是我想知道是否有任何更好方式或良好做法(例如,我错过了一个Airflow插件) 。

python python-3.x airflow hashicorp-vault
1个回答
0
投票

Airflow> = 1.10.10支持Secrets后端,并支持从Hashicorp Vault获取气流变量和连接。

[Airflow Docs中的更多详细信息: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'},
    )
© www.soinside.com 2019 - 2024. All rights reserved.