Store and access password using Apache airflow

前端 未结 6 1901
忘了有多久
忘了有多久 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:06

    Use the GUI in the admin/connections tab.

    The answer that truly works, with persisting the connection in Airflow programatically, works as in the snippet below.

    In the below example myservice represents some external credential cache.

    When using the approach below, you can store your connections that you manage externally inside of airflow. Without having to poll the service from within every dag/task. Instead you can rely on airflow's connection mechanism and you don't have to lose out on the Operators that Airflow exposes either (should your organisation allow this).

    The trick is using airflow.utils.db.merge_conn to handle the setting of your created connection object.

        from airflow.utils.db import provide_session, merge_conn
    
    
    
    
        creds = {"user": myservice.get_user(), "pwd": myservice.get_pwd() 
    
        c = Connection(conn_id=f'your_airflow_connection_id_here',
                       login=creds["user"],
                       host=None)
        c.set_password(creds["pwd"])
        merge_conn(c)
    

    merge_conn is build-in and used by airflow itself to initialise empty connections. However it will not auto-update. for that you will have to use your own helper function.

    from airflow.utils.db import provide_session
    
    @provide_session
    def store_conn(conn, session=None):
        from airflow.models import Connection
        if session.query(Connection).filter(Connection.conn_id == conn.conn_id).first():
            logging.info("Connection object already exists, attempting to remove it...")
            session.delete(session.query(Connection).filter(Connection.conn_id == conn.conn_id).first())
    
        session.add(conn)
        session.commit()
    

提交回复
热议问题