anaghapramesh
anaghapramesh

Reputation: 87

Azure KeyVault: get_secret() - Python TypeError: string indices must be integers

I have been trying to fetch the azure key vault secret from local VM using the below python code. Unfortunately, I'm getting the type error. It would be a great help if someone could resolve this:

from azure.identity import ClientSecretCredential
from azure.keyvault.secrets import SecretClient

SECRET_NAME = 'secretName'
TENANT_ID = 'client_id'
CLIENT_ID = 'tenant_id'
CLIENT_SECRET = 'secretValue'
KEYVAULT_NAME = 'keyVaultName'
KEYVAULT_URL = 'https://vault_url'

_credential = ClientSecretCredential(
    tenant_id = TENANT_ID,
    client_id = CLIENT_ID,
    client_secret = CLIENT_SECRET
    )

_sc = SecretClient(vault_url = KEYVAULT_URL, credential = _credential)
secret = _sc.get_secret(SECRET_NAME)

print(secret.name) 

I also tried - print(_sc.get_secret(SECRET_NAME).value). But it is also displaying the same error.

Please find the displayed error below:

Traceback (most recent call last):
  File "C:\Users\user\Desktop\trialSecret.py", line 37, in <module>
    print(_sc.get_secret(KEYVAULT_NAME).value)
  File "C:\Users\user\AppData\Roaming\Python\Python39\site-packages\azure\core\tracing\decorator.py", line 83, in wrapper_use_tracer
    return func(*args, **kwargs)
  File "C:\Users\user\AppData\Roaming\Python\Python39\site-packages\azure\keyvault\secrets\_client.py", line 67, in get_secret
    bundle = self._client.get_secret(
  File "C:\Users\user\AppData\Roaming\Python\Python39\site-packages\azure\keyvault\secrets\_generated\_operations_mixin.py", line 1515, in get_secret
    return mixin_instance.get_secret(vault_base_url, secret_name, secret_version, **kwargs)
  File "C:\Users\user\AppData\Roaming\Python\Python39\site-packages\azure\keyvault\secrets\_generated\v7_2\operations\_key_vault_client_operations.py", line 290, in get_secret
    map_error(status_code=response.status_code, response=response, error_map=error_map)
  File "C:\Users\user\AppData\Roaming\Python\Python39\site-packages\azure\core\exceptions.py", line 104, in map_error
    error = error_type(response=response)
  File "C:\Users\user\AppData\Roaming\Python\Python39\site-packages\azure\keyvault\secrets\_shared\exceptions.py", line 24, in _get_exception_for_key_vault_error
    message = "({}) {}".format(body["error"]["code"], body["error"]["message"])  # type: Optional[str]
TypeError: string indices must be integers

Upvotes: 1

Views: 1546

Answers (2)

fmarz10
fmarz10

Reputation: 95

I go about this a bit differently by using:

secrets = _sc.list_properties_of_secrets()

Then I can loop for each secret in secrets. I then set a variable called key as secret.name. Using this key, I get the value from the vault:

secrets = _sc.list_properties_of_secrets()

for secret in secrets:
    key = secret.name
    val = _sc.get_secret(key).value

Upvotes: 0

VenkateshDodda
VenkateshDodda

Reputation: 5506

We have tested the same code in our local environment(local machine is running with "azure.keyvault.secrets v 4.3.0 " ) . we are able to pull the secret value as shown in below using either of the below print statement

print(secret.value)
print(_sc.get_secret(SECRET_NAME).value)

enter image description here

"C:\Users\user\Desktop\trialSecret.py", line 37, in <module>
    print(_sc.get_secret(KEYVAULT_NAME).value)   File "C:\Users\user\AppData\Roaming\Python\Python39\site-packages\azure\core\tracing\decorator.py",
line 83, in wrapper_use_tracer
    return func(*args, **kwargs)

Looking at the complete stack trace we understood that you are passing print(_sc.get_secret(KEYVAULT_NAME).value) to get the secret value it is not possible because keyvault name itself is a name and it does not contains any value whereas secret follows (key, value) pair.

Upvotes: 1

Related Questions