Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store and access password using Apache airflow

Tags:

python

airflow

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

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

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

like image 695
Anup Avatar asked Jul 24 '17 12:07

Anup


People also ask

How does airflow store passwords?

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

What is BashOperator in airflow?

The Airflow BashOperator does exactly what you are looking for. It is a very simple but powerful operator, allowing you to execute either a bash script, a command or a set of commands from your DAGs.

What is BaseHook?

BaseHook. Abstract base class for hooks, hooks are meant as an interface to. DiscoverableHook. Interface that providers can implement to be discovered by ProvidersManager.


1 Answers

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 via the UI:

Main Menu Then: Create Connection

To access this password:

from airflow.hooks.base_hook import BaseHook  # Deprecated in Airflow 2  connection = BaseHook.get_connection("username_connection") password = connection.password # This is a getter that returns the unencrypted password. 

Update since Airflow 2 launch

The library airflow.hooks.base_hook has been deprecated and you must use airflow.hooks.base instead.

like image 131
Daniel Lee Avatar answered Sep 16 '22 14:09

Daniel Lee