How can one use HashiCorp Vault in Airflow?

2024/10/10 6:20:28

I am starting to use Apache Airflow and I am wondering how to effectively make it use secrets and passwords stored in Vault. Unfortunately, search does not return meaningful answers beyond a yet-to-be-implemented hook in Airflow project itself.

I can always use Python's hvac module to generically access Vault from PythonOperator but I was wondering if there is any better way or a good practice (e.g. maybe an Airflow plugin I missed).

Answer

Airflow >=1.10.10 supports Secrets Backends and supports getting Airflow Variables and Connections from Hashicorp Vault.

More Details in Airflow Docs: https://airflow.apache.org/docs/stable/howto/use-alternative-secrets-backend.html#hashicorp-vault-secrets-backend

If you want to test it locally check the tutorial at https://www.astronomer.io/guides/airflow-and-hashicorp-vault/

Set the following config in airflow.cfg, update based on your environment:

backend = airflow.contrib.secrets.hashicorp_vault.VaultBackend
backend_kwargs = {"connections_path": "connections", "variables_path": "variables", "mount_point": "airflow", "url": "http://127.0.0.1:8200"}

Example DAG to test the integration:

from airflow import DAG
from airflow.operators.python_operator import PythonOperator
from datetime import datetime
from airflow.hooks.base_hook import BaseHookdef get_secrets(**kwargs):conn = BaseHook.get_connection(kwargs['my_conn_id'])print(f"Password: {conn.password}, Login: {conn.login}, URI: {conn.get_uri()}, Host: {conn.host}")with DAG('example_secrets_dags', start_date=datetime(2020, 1, 1), schedule_interval=None) as dag:test_task = PythonOperator(task_id='test-task',python_callable=get_secrets,op_kwargs={'my_conn_id': 'smtp_default'},)
https://en.xdnf.cn/q/69928.html

Related Q&A

List all words in a dictionary that start with user input

How would a go about making a program where the user enters a string, and the program generates a list of words beginning with that string?Ex: User: "abd" Program:abdicate, abdomen, abduct..…

Python version of C#s conditional operator (?)

I saw this question but it uses the ?? operator as a null check, I want to use it as a bool true/false test.I have this code in Python:if self.trait == self.spouse.trait:trait = self.trait else:trait…

Python String Replace Error

I have a python script that keeps returning the following error:TypeError: replace() takes at least 2 arguments (1 given)I cannot for the life of me figure out what is causing this.Here is part of my c…

How to run two modules at the same time in IDLE

I am working on a super simple socket program and I have code for the client and code for the server. How do I run both these .py files at the same time to see if they work ?

Passing 2 dimensional C array to python numpy

I need some help regarding passing C array to python(numpy). I have 2d array of doubles NumRows x NumInputs, it seems that PyArray_SimpleNewFromData does not convert it right way - it is hard to see be…

Best way to implement numpy.sin(x) / x where x might contain 0

What I am doing now is:import numpy as npeps = np.finfo(float).epsdef sindiv(x):x = np.abs(x)return np.maximum(eps, np.sin(x)) / np.maximum(eps, x)But there is quite a lot of additional array operation…

Scrapy process.crawl() to export data to json

This might be a subquestion of Passing arguments to process.crawl in Scrapy python but the author marked the answer (that doesnt answer the subquestion im asking myself) as a satisfying one.Heres my pr…

Embedding Python in C: Error in linking - undefined reference to PyString_AsString

I am trying to embed a python program inside a C program. My OS is Ubuntu 14.04I try to embed python 2.7 and python 3.4 interpreter in the same C code base (as separate applications). The compilation a…

How can I add properties to a class using a decorator that takes a list of names as argument?

I would like to add many dummy-properties to a class via a decorator, like this:def addAttrs(attr_names):def deco(cls):for attr_name in attr_names:def getAttr(self):return getattr(self, "_" +…

How to reshape only last dimensions in numpy?

Suppose I have A of shape (...,96) and want to reshape it into (...,32,3) keeping both lengths and number of preceding dimensions, if ever, intact.How to do this?If I writenp.reshape(A, (-1, 32, 2))it…