title: Azure - Accessing Key Vault using User Managed Identity in Java Spring Boot : Error Details: ManagedIdentityCredential authentication unavailable?
body: <p>I have a Java Spring boot application that just reads the secret from Azure Key Vault, below are steps used</p>
  <ul>
  <li>Created an Application Registration</li>
  </ul>
  <p><a href="https://i.stack.imgur.com/CxEOf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CxEOf.png" alt="enter image description here" /></a></p>
  <ul>
  <li>Copied the App Registration details</li>
  </ul>
  <p><a href="https://i.stack.imgur.com/tipeb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tipeb.png" alt="enter image description here" /></a></p>
  <ul>
  <li>Generated Secret</li>
  </ul>
  <p><a href="https://i.stack.imgur.com/PUD1k.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PUD1k.png" alt="enter image description here" /></a></p>
  <ul>
  <li>Granted access on Azure Key Vault</li>
  </ul>
  <p><a href="https://i.stack.imgur.com/KOYQl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KOYQl.png" alt="enter image description here" /></a></p>
  <p>and below is my Java Spring Boot application</p>
  <p><strong>pom.xml</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
      xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;2.3.6.RELEASE&lt;/version&gt;
          &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
      &lt;/parent&gt;
      &lt;groupId&gt;com.contoso&lt;/groupId&gt;
      &lt;artifactId&gt;keyvault&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;name&gt;keyvault&lt;/name&gt;
      &lt;description&gt;Demo project for Spring Boot&lt;/description&gt;
  
      &lt;properties&gt;
          &lt;java.version&gt;11&lt;/java.version&gt;
          &lt;azure.version&gt;2.3.5&lt;/azure.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.microsoft.azure&lt;/groupId&gt;
              &lt;artifactId&gt;azure-keyvault-secrets-spring-boot-starter&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
              &lt;exclusions&gt;
                  &lt;exclusion&gt;
                      &lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
                      &lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
                  &lt;/exclusion&gt;
              &lt;/exclusions&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;dependencyManagement&gt;
          &lt;dependencies&gt;
              &lt;dependency&gt;
                  &lt;groupId&gt;com.microsoft.azure&lt;/groupId&gt;
                  &lt;artifactId&gt;azure-spring-boot-bom&lt;/artifactId&gt;
                  &lt;version&gt;${azure.version}&lt;/version&gt;
                  &lt;type&gt;pom&lt;/type&gt;
                  &lt;scope&gt;import&lt;/scope&gt;
              &lt;/dependency&gt;
          &lt;/dependencies&gt;
      &lt;/dependencyManagement&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  &lt;/project&gt;
  </code></pre>
  <p><strong>application.properties</strong></p>
  <pre><code>azure.keyvault.client-id=7a111923-xxxxxxxx-xxxxxx-31be31d233dd
  azure.keyvault.client-key=gt_~k02yF_xxxxxx_vn3r1.GW
  azure.keyvault.enabled=true
  azure.keyvault.tenant-id=9cef136a-xxxx-xxxx-b7d3-d9d8a5a84182
  azure.keyvault.uri=https://contosokvxxx.vault.azure.net/
  </code></pre>
  <p><strong>KeyvaultApplication.java</strong></p>
  <pre><code>package com.example.demo;
  
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.web.bind.annotation.GetMapping;
  import org.springframework.web.bind.annotation.RestController;
  import org.springframework.beans.factory.annotation.Value;
  
  @SpringBootApplication
  @RestController
  public class KeyvaultApplication {
  
     public static void main(String[] args) {
       SpringApplication.run(KeyvaultApplication.class, args);
     }
  
    @GetMapping(&quot;get&quot;)
    public String get() {
      return connectionString;
    }
  
    @Value(&quot;${connectionString}&quot;)
    private String connectionString;
  
    public void run(String... varl) throws Exception {
      System.out.println(String.format(&quot;\nConnection String stored in Azure Key Vault:\n%s\n&quot;,connectionString));
    }  
  
  }
  </code></pre>
  <p>It works and I could read the Secret from Azure Key Vault</p>
  <p><a href="https://i.stack.imgur.com/hlMcj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hlMcj.png" alt="enter image description here" /></a></p>
  <p>so decided to remove the secrets from the application.properties and commented as below</p>
  <pre><code>#azure.keyvault.client-id=7a111923-1xxxxxxxx-31be31d233dd
  #azure.keyvault.client-key=gt_~k02yF_xxxxxxxx-Hr6vn3r1.GW
  azure.keyvault.enabled=true
  #azure.keyvault.tenant-id=9cef136axxxxxxx-3-d9d8a5a84182
  azure.keyvault.uri=https://contosokvxxx.vault.azure.net/
  </code></pre>
  <p>and added an Environment variable as shown below using setx</p>
  <pre><code>setx AzureServicesAuthConnectionString &quot;RunAs=App;AppId=bb01c08b-xxxxxxxx-106;TenantId=9cef1-xxxxxxx-d9d8a5a84182;AppKey=xxxxxxxx-4Dpg-E3zrj~&quot;
  </code></pre>
  <p>The project compiles without any isssues</p>
  <pre><code>&gt;mvn clean compile package
  </code></pre>
  <p>but failed running</p>
  <pre><code>&gt;mvn spring-boot:run
  </code></pre>
  <p><strong>Error:</strong></p>
  <pre><code>11:40:27.334 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
  java.lang.IllegalStateException: Failed to configure KeyVault property source
          at com.microsoft.azure.keyvault.spring.KeyVaultEnvironmentPostProcessorHelper.addKeyVaultPropertySource(KeyVaultEnvironmentPostProcessorHelper.java:110)
          at com.microsoft.azure.keyvault.spring.KeyVaultEnvironmentPostProcessor.postProcessEnvironment(KeyVaultEnvironmentPostProcessor.java:47)
  
  Caused by: java.lang.RuntimeException: Max retries 3 times exceeded. Error Details: ManagedIdentityCredential authentication unavailable. Connection to IMDS endpoint cannot be established, Network is unreachable: no further information.
          at com.azure.core.http.policy.RetryPolicy.lambda$attemptAsync$1(RetryPolicy.java:116)
          at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onError(FluxOnErrorResume.java:88)
  </code></pre>