使用Apache Airflow存储和访问密码 [英] Store and access password using Apache airflow

查看:684
本文介绍了使用Apache Airflow存储和访问密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用气流作为调度程序。我想在DAG中调用一个简单的bash运算符。 bash脚本需要使用密码作为参数来进行进一步处理。

We are using airflow as a scheduler. I want to invoke a simple bash operator in a DAG. The bash script needs password as an argument to do further processing.

我如何安全地将密码存储在气流中(配置/变量/连接)并在dag定义文件中访问它。

How can I store a password securely in airflow (config/variables/connection) and access it in dag definition file.

我是气流和Python的新手,所以会感谢代码段。

I am new to airflow and Python so a code snippet will be appreciated.

推荐答案

您可以将密码存储在挂钩中-只要您设置了Fernet密钥,该密码就会被加密。

You can store the password in a Hook - this will be encrypted so long as you have setup your fernet key.

以下是创建密码的方法连接。

Here is how you can create a connection.

from airflow.models import Connection
def create_conn(username, password, host=None):
    new_conn = Connection(conn_id=f'{username}_connection',
                                  login=username,
                                  host=host if host else None)
    new_conn.set_password(password)

然后,此密码在您设置的数据库中被加密。

Then, this password is encryted in the db you setup.

要访问此密码:

from airflow.hooks.base_hook import BaseHook

 connection = BaseHook.get_connection("username_connection")
 password = connection.password # This is a getter that returns the unencrypted password.

编辑:

更容易通过UI创建连接的方法:

There is an easier way to create a connection via the UI:


然后:

Then:

这篇关于使用Apache Airflow存储和访问密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆