Integrate Vault with Django testing

I’ve integrated Vault into a Django application, but didn’t want to mock it out during testing. I had to verify that the secret was written into the correct path.

Vault is a database, but for secrets. When testing with a database, you want to achieve test isolation. Most test frameworks allow you to add setup and teardown hooks. In these hooks we will invoke the cleanup code to reset the state of the database.

The vault client is set up in the settings file, so it can be used everywhere when importing the settings:

from django.conf import settings

I’ve also wrapped the CRUD interfaces of the HVAC library to be able to target the correct database (VAULT_MOUNTPOINT).

Finally, you can use the following cleanup script to recursively delete all keys in your setup and teardown hooks:

I used it with pytest the following way:

You’re probably wondering why to call it in the setup hook as well. This is in order to reset your Vault in case a previous test run crashed and didn’t call the teardown.

The used HVAC library can be found here: https://github.com/hvac/hvac

I hope this will help you in your Vault journey.


Integrate Vault with Django testing was originally published in Everything Full Stack on Medium, where people are continuing the conversation by highlighting and responding to this story.

Senior Backend Engineer

Backend Group

Thank you for your interest!

We will contact you as soon as possible.

Want to Know More?

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com
Thank you for your interest!

We will contact you as soon as possible.

Let's talk

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com