gpt4 book ai didi

Azure - 在 Java Spring Boot 中使用用户管理身份访问 Key Vault : Error Details: ManagedIdentityCredential authentication unavailable?

转载 作者:行者123 更新时间:2023-12-02 23:04:47 31 4
gpt4 key购买 nike

我有一个 Java Spring boot 应用程序,它仅从 Azure Key Vault 读取 secret ,以下是使用的步骤

  • 创建了应用程序注册

enter image description here

  • 复制了应用注册详细信息

enter image description here

  • 生成的 secret

enter image description here

  • 已授予 Azure Key Vault 访问权限

enter image description here

下面是我的 Java Spring Boot 应用程序

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.contoso</groupId>
<artifactId>keyvault</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>keyvault</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>11</java.version>
<azure.version>2.3.5</azure.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-keyvault-secrets-spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-spring-boot-bom</artifactId>
<version>${azure.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

application.properties

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/

KeyvaultApplication.java

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("get")
public String get() {
return connectionString;
}

@Value("${connectionString}")
private String connectionString;

public void run(String... varl) throws Exception {
System.out.println(String.format("\nConnection String stored in Azure Key Vault:\n%s\n",connectionString));
}

}

它有效,我可以从 Azure Key Vault 读取 secret

enter image description here

因此决定从 application.properties 中删除 secret 并评论如下

#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/

并使用 setx 添加了一个环境变量,如下所示

setx AzureServicesAuthConnectionString "RunAs=App;AppId=bb01c08b-xxxxxxxx-106;TenantId=9cef1-xxxxxxx-d9d8a5a84182;AppKey=xxxxxxxx-4Dpg-E3zrj~"

项目编译没有任何问题

>mvn clean compile package

但运行失败

>mvn spring-boot:run

错误:

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)

最佳答案

Error Details: ManagedIdentityCredential authentication unavailable. Connection to IMDS endpoint cannot be established, Network is unreachable: no further information.

该错误意味着您无法连接到 Azure 实例元数据服务端点,它是一个可通过众所周知的不可路由 IP 地址 (169.254.169.254)、MSI 访问的 REST 端点使用它来获取 token ,它只能从 Azure 服务内访问,例如Web应用程序、VM等。简单地说,您不能在本地使用MSI(托管身份)。

要使用 MSI 从 azure keyvault 获取 secret ,请按照 this 操作要将应用程序部署到 Azure Web 应用,请启用系统分配的标识或用户分配的标识,然后从 application.properties 中删除 azure.keyvault.client-key,将azure.keyvault.client-id更改为MSI的客户端id,将其添加到keyvault的访问策略中,详细信息如下this .

azure.keyvault.client-id=56rqs994-0o66-43o3-9roo-8e3534d0cb23
azure.keyvault.enabled=true
azure.keyvault.tenant-id=72s988os-86s1-41ns-91ab-2q7pq011qo47
azure.keyvault.uri=https://contosokv.vault.azure.net/

引用 - Tutorial: Reading a secret from Azure Key Vault in a Spring Boot application

关于Azure - 在 Java Spring Boot 中使用用户管理身份访问 Key Vault : Error Details: ManagedIdentityCredential authentication unavailable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64887983/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com