Store and access password using Apache airflow

Anup picture Anup · Jul 24, 2017 · Viewed 24.5k times · Source

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.

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.

Answer

Daniel Lee picture Daniel Lee · Jul 25, 2017

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.

To access this password:

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.

EDIT:

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

Main Menu Then: Create Connection