[Python] Python hvac implementation to retrieve secrets

Discussão em 'Python' iniciado por Stack, Julho 3, 2025.

  1. Stack

    Stack Membro Participativo

    I am working at a python sample code for the first time, this is about how to retrieve secrets from Hashicorp Vault secrets manager api; hvac.client.secrets.kv.v2, my code is as follows:

    # 1. Setup client by the url of the custom vault instance.
    # if null defualts points to a localhost instance (host='localhost', port=port)
    client = Client(url = vault_url)
    # Note: The instance is being managed by Hasicorp, and python hvac implementation by default builds the URL appending and prepending the following '/v1/auth/' +CUSTOM_AUTH_PATH+ '/login' to the given url, so
    # for the login I used custom login with application credentials

    # 2. Preparing json params

    params = {
    'client_id': vault_user_id,
    'client_secret': vault_token_id,
    'grant_type' : vault_login_type,
    'audience': vault_audience
    }

    # 3. Login to retrieve the token
    result = client.login(url=vault_path,use_token=False,json=params)


    As next step I am trying to retrieve the secrets

    client.secrets.kv.v2.read_secret_version(path=vault_secret_name, version="1",mount_point='secret'))


    This is a very simple step but It is raising an error code I can't find on the web:

    InvalidPath: {"code":5,"message":"Not Found","details":[]}, on get


    It seems like the URL I am giving is not the right one, because the secret path I am requesting is not available, then after reviewing it, it seems correct:

    https://api.cloud.hashicorp.com/secrets/.../v1/secret/data/vault_secret_name


    I also reviwed the Client object properties, and correct them as follows

    client.token = result ['access_token']

    client.url = vault_secret_engine_url


    Then I tried curl - it and it works, without raising any error. I think it is an easy implementation, that must be working fine,but it doesn't. I hope my explanation is understandable, letting you know the required details to find a solution.

    Thanks for your time in advance.

    Continue reading...

Compartilhe esta Página