Store and access password using Apache airflow

前端 未结 6 1858
忘了有多久
忘了有多久 2020-12-25 11:43

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 c

6条回答
  •  孤城傲影
    2020-12-25 12:14

    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:

    Then:

提交回复
热议问题