【问题标题】:Token Refresh to Azure KeyVault Access令牌刷新到 Azure KeyVault 访问
【发布时间】:2020-10-08 03:46:09
【问题描述】:

当我尝试从本地计算机访问 Key Vault 时遇到以下错误

DefaultAzureCredential()

我正在运行以下两行,但我不断收到错误。

client = SecretClient(vault_url=<URL>, credential=DefaultAzureCredential())
secret = client.get_secret(<SECRET NAME>).value
azure.core.exceptions.ClientAuthenticationError: DefaultAzureCredential failed to retrieve a token from the included credentials.
Attempted credentials:
EnvironmentCredential: EnvironmentCredential authentication unavailable. Environment variables are not fully configured.
ManagedIdentityCredential: ManagedIdentityCredential authentication unavailable, no managed identity endpoint found.
SharedTokenCacheCredential: SharedTokenCacheCredential authentication unavailable. No accounts were found in the cache.
VisualStudioCodeCredential: Azure Active Directory error '(invalid_grant) AADSTS700082: The refresh token has expired due to inactivity. The token was issued on 2020-06-07T03:58:48.4708462Z and was inactive for 90.00:00:00.

我尝试通过以下方式登录 Azure az login 但这不会刷新凭据。

编辑: 我尝试了documentation 中的一些其他配置。 Azure CLI 凭据的工作似乎很奇怪,因为它看起来像是由默认凭据处理的场景之一。

【问题讨论】:

  • 您是否尝试过使用AzureCliCredential() 而不是DefaultAzureCredential()
  • 谢谢,@PamelaPeng - 那工作。知道为什么 CLI 凭证在默认凭证不起作用时起作用吗?根据documentation,CLI 似乎是 Default 考虑的情况之一。

标签: azure azure-keyvault


【解决方案1】:

如果您使用 az login 访问 Key Vault,理论上 AzureCliCredential()DefaultAzureCredential() 都可以正常工作。但错误显示在您的问题中的 VisualStudioCodeCredential() 处。

VisualStudioCodeCredential:如果用户已登录 Visual Studio Code Azure 帐户扩展,DefaultAzureCredential 将作为该用户进行身份验证。

DefaultAzureCredential 按此顺序尝试通过以下机制进行身份验证,成功时停止:

因此,您需要使用AzureCliCredential() 或设置exclude_visual_studio_code_credential=true 以确保跳过VisualStudioCode。

有关 Azure Identity 的更多详细信息,请参阅here

【讨论】:

    【解决方案2】:

    如果您希望它与 DefaultAzureCredential() 一起使用,您需要以下内容:-

    • 服务主体
    • 在 KeyVault 访问策略中授予对服务主体的访问权限。

    完成以上操作后,需要设置以下环境变量:-

    • AZURE_CLIENT_ID(这是上述服务主体 (sp) 的 clientID)
    • AZURE_CLIENT_SECRET(这是上述 sp 的客户端密钥)
    • AZURE_SUBSCRIPTION_ID(这是 Azure 中的订阅 ID。)
    • AZURE_TENANT_ID(这是 Active Directory ID)

    要查看 Windows 上的所有环境变量,请打开命令提示符键入命令“set”并按 Enter。

    要在 Windows 上设置环境变量,例如你想设置 AZURE_CLIENT_ID 变量值为 jbfdshfbsdfbdsbdgbdjgbdfjbdfj ,打开命令提示符键入命令“setx AZURE_CLIENT_ID jbfdshfbsdfbdsbdgbdjgbdfjbdfj”并按进入

    在您重新启动命令提示符之前,它不会显示更新的内容。

    然后在你使用 DefaultAzureCredentials() 的地方重启 Visual Studio/vs 代码

    【讨论】:

      【解决方案3】:

      这已经晚了,但也许它可以帮助其他人避免花费数小时试图解决这个问题。我主要使用 Intellij 在 Java 中工作,无论我做什么,我都会收到以下错误。我通过 web、cli、intellij 插件登录,清除了身份缓存,你说的。

      com.azure.core.exception.ClientAuthenticationException: DefaultAzureCredential authentication failed. ---> VisualStudioCodeCredential authentication failed. Error Details: AADSTS50173: The provided grant has expired due to it being revoked, a fresh auth token is needed. The user might have changed or reset their password. The grant was issued on '2021-10-15T12:45:38.0655988Z' and the TokensValidFrom date (before which tokens are not valid) for this user is '2021-11-29T19:36:56.0000000Z'.
      

      虽然我没有“解决”问题,但我的解决方法只是打开 VS Code 并使用 Azure 帐户插件登录。然后我在 Intellij 中没有问题。请注意,VSCode 中也没有加载任何项目或代码。

      【讨论】:

        【解决方案4】:

        尝试从 Azure Linux 跳转框连接到 Key Vault 时,我收到了类似的错误。自从我上次从这个盒子进入保险库以来,已经过去了 90 多天。事实证明,我需要执行一个新的 az login 请求来刷新我的令牌,尽管错误本身并没有明确指出这一点。

        【讨论】:

          猜你喜欢
          • 2019-08-31
          • 2018-01-15
          • 2019-06-29
          • 2019-08-25
          • 2018-12-20
          • 2020-07-12
          • 2019-01-13
          • 2019-03-28
          • 1970-01-01
          相关资源
          最近更新 更多