Antoine Delia
Antoine Delia

Reputation: 1845

Vault - How to list folders from a specific path on a kv v2 secret engine using HVAC?

I'm trying to retrieve all the folders from a specific path in my Vault. In order to do this, I'm using the hvac Vault API client for Python.

I can easily list the folders from a kv v1 secret engine using the following command:

import hvac

client = hvac.Client(url=vault_host)    
list_folders = client.list('my/path/')

However, this does not work with kv v2 secret engines. I tried to look up the documentation, but it looks that it is only possible to retrieve a list of secrets. For example, this won't work unless a secret is stored in the path:

list_folders = client.secrets.kv.read_secret_version(path='my/path/')

Do you have any idea how I could manage to get the list of my folders on a kv v2 secret engine?

Thanks a lot.

Upvotes: 2

Views: 11594

Answers (1)

Antoine Delia
Antoine Delia

Reputation: 1845

After looking up the documentation, it appears the correct method to use is list_secrets. So, in order to get the folders using a specific path, I used the following code:

list_response = client.secrets.kv.v2.list_secrets(
    path='my/path/'
)
list_folders = list_response['data']['keys']
print(list_folders)

This outputs a list with the name of the folders available:

['dev/', 'prod/']

Also, be careful if you're working with a dev-mode server of your Vault, as it defaults the path to secret/. More info can be found in the Vault documentation.

Upvotes: 1

Related Questions