【问题标题】:Cannot get certificate from Azure key vault - Failed to acquire a new access token无法从 Azure 密钥保管库获取证书 - 无法获取新的访问令牌
【发布时间】:2022-02-23 20:12:05
【问题描述】:

我在 azure 中创建了一个密钥保管库并存储了机密和证书。我正在使用托管身份访问密钥保管库。从我使用 java 的 Azure VM 我可以使用

从密钥库中获取秘密

使用下面的代码,我能够正确地获取秘密。

SecretClient secretClient = new SecretClientBuilder()
    .httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
    .vaultUrl("https://testkeyvault.vault.azure.net/")
    .credential(new ManagedIdentityCredentialBuilder().build()).buildClient();
KeyVaultSecret keyVaultSecret = secretClient.getSecret("test-secret");
System.out.println(keyVaultSecret.getName());

但是当我尝试使用以下代码获取证书时 -

CertificateClient certificateClient = new CertificateClientBuilder()
    .httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
    .vaultUrl("https://testkeyvault.vault.azure.net/")
    .credential(new ManagedIdentityCredentialBuilder().build()).buildClient();
    
KeyVaultCertificateWithPolicy certificate = certificateClient.getCertificate("test-cert");

我得到了例外 -

[main] ERROR com.azure.core.credential.SimpleTokenCache - Failed to acquire a new access token.
[parallel-2] ERROR com.azure.core.credential.SimpleTokenCache - Failed to acquire a new access token.
[parallel-4] ERROR com.azure.core.credential.SimpleTokenCache - Failed to acquire a new access token.
[parallel-6] ERROR com.azure.core.credential.SimpleTokenCache - Failed to acquire a new access token.
[parallel-6] WARN com.azure.security.keyvault.certificates.CertificateAsyncClient - Failed to Retrieve the certificate - test-cert
Max retries 3 times exceeded. Error Details: To convert to a resource string the specified array must be exactly length 1

在 keyvault 访问策略中,我已授予所有权限,包括获取此 VM 的机密和证书的权限。但是获取秘密效果很好,而不是证书。有人可以在这里帮助我吗?

【问题讨论】:

  • 您是否检查过 Keyvault 的访问策略。如果您有权在访问策略中获取证书?在我看来,你很可能没有权限。
  • @Jatin 我已经为证书添加了密钥保管库访问中的所有权限。

标签: java azure azure-keyvault


【解决方案1】:

• 我建议您清除系统内存缓存,因为可能已经存在使用这些身份验证凭据的现有会话,并且令牌可能存储在当前系统内存中,因此您可能无法访问通过 Java 应用程序存储在密钥保管库中的证书。

另外,我建议您通过 Azure 中的任何现有或并行会话注销,然后重试。确保在密钥保管库中上传的证书设置了正确的密码,以便从中检索托管标识的私钥。有关更多详细信息,请参阅以下有关使用 Java 从密钥库中检索证书的示例代码:-

有关使用托管身份验证服务主体的更多详细信息,请查找以下链接:-

https://github.com/Azure/azure-sdk-for-java/wiki/Azure-Identity-Examples#authenticating-a-service-principal-with-a-client-certificate

【讨论】:

    猜你喜欢
    • 2020-06-28
    • 2015-08-06
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-10
    相关资源
    最近更新 更多