azure - 无法使用用户分配的标识从 Azure Key Vault 获取 secret

标签 azure azure-keyvault azure-authentication azure-managed-identity

我已将 msi 添加到 VMSS,通过检查 ElasticAPV2.xts 中的 ComputerResourceGroupInfo.json,我有:

"ManagedServiceIdentityConfig": {
          "Type": "SystemAssigned, UserAssigned",
          "**UserAssignedIdentities**": [
            "/subscriptions/1234567890qwer/resourceGroups/my-rg/providers/Microsoft.ManagedIdentity/userAssignedIdentities/my-msi"
          ],
          "ServicePrincipalForSystemAssignedIdentity": "...",
          "ScaleSetIdentity": {
            "principalId": "...",
            "tenantId": "...",
            "type": "SystemAssigned, UserAssigned",
            "**userAssignedIdentities**": {
              "/subscriptions/1234567890qwer/resourceGroups/my-rg/providers/Microsoft.ManagedIdentity/userAssignedIdentities/my-msi": {
                "principalId": "...",
                "clientId": "..."
              }

my-msi 还通过 List 和 Get 添加到 Azure Key Vault 访问策略中。

在虚拟机中,我尝试使用以下方法获取 secret

PS D:\ManagementServiceCommonSetup> $accessToken = (Invoke-WebRequest -Uri 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://vault.azure.net' -Method GET -Headers @{Metadata="true"} -UseBasicParsing | ConvertFrom-Json).access_token
PS D:\ManagementServiceCommonSetup> echo $accessToken
eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IkN0ZlFDOExlLThOc0M3b0MyelFrWnBjcmZP...MCQi-bPCJQ
PS D:\ManagementServiceCommonSetup> (Invoke-WebRequest -Uri https://my-kv.vault.azure.net/certificates/my-cert/587898f2?api-version=2016-10-01 -Method GET -Headers @{Authorization="Bearer $accessToken"}).content
Invoke-WebRequest : {"error":{"code":"Forbidden","message":"Access denied","innererror":{"code":"AccessDenied"}}}
At line:1 char:2
+ (Invoke-WebRequest -Uri https://my-kv.vault.azure.net/secrets/my-cert/ ...
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

我的问题:

  1. 这是使用访问 token 检索证书的正确方法吗? 用户分配的身份?
  2. 为什么访问被拒绝?我的理解是这里不需要访问控制(IAM),因为我已将 msi 添加到访问策略中。

更新:

对于用户分配的身份,需要指定对象 ID 或客户端 ID。

Invoke-WebRequest -Uri "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://vault.azure.net&object_id=$($ObjectId)" -Method GET -Headers @{Metadata="true"}

最佳答案

1.Is this right way to retrieve certificate using access token through user-assigned identity?

不,您使用的 Uri 是 get secret ,如果你想获得证书,应该是

Invoke-WebRequest -Uri https://my-kv.vault.azure.net/certificates/my-cert/587898f2?api-version=2016-10-01 -Method GET -Headers @{Authorization="Bearer $accessToken"}

引用:Get Certificate - Get Certificate

2.Why the access got denied? My understanding is Access control (IAM) is not need here since I have add msi to access policies.

你的理解是对的。再次在访问策略中查看,确保您已授予证书权限(也可以尝试全选,很方便)。

enter image description here

更新:

我在windows vm中测试了一下,在vmss中,逻辑应该是一样的,doc of VM and VMSS to access the keyvault在一起了。

如果我使用系统分配的身份进行测试,它工作得很好。如果我使用用户分配的托管身份对其进行测试,我也会收到 403 错误。

用户分配的托管身份是一个预览版功能,我不确定它是否支持访问keyvault,该文档仅适用于系统分配身份。所以我建议您使用系统分配的身份来访问keyvault,您可以尝试一下。

$response = Invoke-WebRequest -Uri 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fvault.azure.net' -Method GET -Headers @{Metadata="true"} -UseBasicParsing
$content = $response.Content | ConvertFrom-Json 
$KeyVaultToken = $content.access_token 
(Invoke-WebRequest -Uri https://<keyvault-name>.vault.azure.net/certificates/<{certificate-name}>/<certificate-version>?api-version=2016-10-01 -Method GET -Headers @{Authorization="Bearer $KeyVaultToken"} -UseBasicParsing).content 

系统分配的身份:

enter image description here

用户分配的托管身份:

enter image description here

关于azure - 无法使用用户分配的标识从 Azure Key Vault 获取 secret ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56534229/

相关文章:

deployment - 无法部署到 Azure

c# - 无法从 C# 使用 AAD 连接到 Azure SQL 数据库

azure - 从 AzureDevOps 替换 token 在应用程序设置中设置 JSON key

重新生成 key 操作后 Azure SSL 证书不同步

c# - Azure Web App 引发 HTTP 错误 500.0 - 内部服务器错误 由于发生内部服务器错误,无法显示页面

azure - 与环境变量相比,在 KeyVault 中存储简单 secret 有哪些优点

javascript - 使用客户端 JavaScript 从 Azure 访问 Key Vault?

asp.net - MSAL 3.x GetAuthorizationRequestUrlParameterBuilder 返回错误的数据类型

azure - 使用 Function App 时如何获取详细的 Easy Auth 日志?

asp.net-mvc - Azure 身份验证 .net MVC 超时设置