Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get database secrets (username and password) from hashicorp vault using python

I want to get credentials (username and password)from Hashicorp vault using hvac python library and print them out or store them in some variable. However, I am getting an error TypeError: 'Response' object is not subscriptable after the last print statement. But, my credentials are getting authenticated.

My codes is as follows

import hvac 

vault_token = 's.MYBDmBvO5I.....' # Copying my token from vault

vault_url = 'https://vault.corp.foxbase.de/ui/vault/secrets'

client = hvac.Client(url=vault_url, token=vault_token)

res=client.is_authenticated()

print('here is : ',res)

secrets_list = client.secrets.kv.v1.read_secret(
    path = 'databases/creds/data-science-access'
)

print("secrets list: ", secrets_list['username'])

output is

    here is :  True
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-52-3a463dc902b7> in <module>()
     23 #secrets_list1 = secrets_list.json()
     24 
---> 25 print("secrets list: ", secrets_list['username'])

My credentials in Vault sit in databases/creds/data-science-access . When I run this command in vault terminal "vault read databases/creds/data-science-access" it generates the credentials as follows

Key      Value               
password xxx
username xxx

When I login to my vault, it looks like this enter image description here

Can someone please rectify where I am getting it wrong? Perhaps some parameters like "vault_url","vault_token" or "path" values I have wrongly passed.

like image 407
Mubashir Ali Avatar asked Mar 05 '26 11:03

Mubashir Ali


1 Answers

You need to query result inside "data", basically below as last line:

#print("secrets list: ", secrets_list['username'])
print("secrets list: ", secrets_list['data']['username'])
like image 131
SaurabhVer Avatar answered Mar 06 '26 23:03

SaurabhVer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!