2

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.

  • 1
    I am not sure why anyone would be asking for this but outputting passwords is not right and it may lead you to have legal issues in the future. I would stir away from doing this – Nelson Jun 14 '22 at 19:56
  • @Nelson Basically I need to connect to the database and for that i need to fetch credentials from vault (username and password). If there is a workaround which can directly fetch credentials from vault without getting them in some variables and then passing them to conn = pymysql.connect( host='prod-read-replica.eu-central-1.rds.amazonaws.com', user='', password='< password from vault>', db= 'analytics Please let me know, how to do that? Thanks' – Mubashir Ali Jun 15 '22 at 08:47
  • I am also facing error while fetching data from vault, Below is my error "TypeError: 'Response' object is not subscriptable" – satish pujara Oct 13 '22 at 10:29

1 Answers1

1

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'])
Peter
  • 3,186
  • 3
  • 26
  • 59
SaurabhVer
  • 11
  • 1