Azure-sdk-for-java: Accessing Azure Key Vault - DefaultAzureCredentialBuilder() throws error

Created on 6 Aug 2020  ·  3Comments  ·  Source: Azure/azure-sdk-for-java

Hi,
I'm following the tutorial here: https://docs.microsoft.com/en-us/azure/key-vault/secrets/quick-create-java
trying to retrieve a secret from a Key Vault in my Java application.

When trying to initialise a SecretClient variable, I'm getting an error in the credential bit. I pasted the following code straight from the tutorial into my code (have created a string variable kvUri pointing to my key vault):
SecretClient secretClient = new SecretClientBuilder().vaultUrl(kvUri).credential(new DefaultAzureCredentialBuilder().build()) .buildClient();

I'm getting a red error line under 'new DefaultAzureCredentialBuilder().build()'. When I hover over it I see this message:
"Required type: TokenCredential
Provided: DefaultAzureCredential"

I've installed and imported the following jars:
azure-security-keyvault-secrets-4.1.1.jar
azure-identity-1.1.0-beta.7.jar

Import statements:
import com.azure.security.keyvault.secrets.SecretClient; import com.azure.security.keyvault.secrets.SecretClientBuilder; import com.azure.identity.DefaultAzureCredentialBuilder;

  • OS: Windows 10
  • IDE: IntelliJ

Any idea what I can do to fix this?

Thanks!

Client Docs KeyVault customer-reported question

All 3 comments

Thanks for filing this issue, @ronny-sphera. @vcolin7, @g2vinay can you please follow up?

Hi @ronny-sphera, I cannot seem to reproduce the issue you mention. Here's the code that I used:

import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.security.keyvault.secrets.SecretClient;
import com.azure.security.keyvault.secrets.SecretClientBuilder;
import com.azure.security.keyvault.secrets.models.KeyVaultSecret;
import com.azure.security.keyvault.secrets.models.SecretProperties;

public class ListSecrets {
    public static void main(String[] args) {
        String keyVaultUrl = "https://<your-keyvault-name>.vault.azure.net/";

        SecretClient secretClient = new SecretClientBuilder()
            .vaultUrl(keyVaultUrl)
            .credential(new DefaultAzureCredentialBuilder().build())
            .buildClient();

        System.out.println("Listing secrets");

        for(SecretProperties secretProperties : secretClient.listPropertiesOfSecrets()) {
            if (secretProperties.isEnabled()) {
                KeyVaultSecret secret = secretClient.getSecret(secretProperties.getName());

                System.out.printf("Secret name: %s, secret value: %s\n", secret.getName(), secret.getValue());
            }
        }
    }
}

I'm also using the following dependencies:

<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-security-keyvault-secrets</artifactId>
    <version>4.1.1</version>
</dependency>
<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-identity</artifactId>
    <version>1.1.0-beta.7</version>
</dependency>

Maybe you could try to rebuild your project or run mvn clean install in the directory where your POM file is located.

Adding the dependencies to the pom and reloading the maven project seems to have solved it - thanks!

Was this page helpful?
0 / 5 - 0 ratings