对 Azure VM 的 Keyvault 访问进行故障排除

Troubleshooting Keyvault access from Azure VM

我需要将 .Net Core 2.2 应用程序部署到 Azure VM,并且我必须从 Azure Keyvault 读取一些凭据。

我无法完全控制环境。

我遵循了这个教程 Use Azure Key Vault with a Windows virtual machine in .NET 尽我所能,但实际上 VM 已经由系统管理员创建。

$response = 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"}

我收到这个错误

Invoke-WebRequest : {"error":"invalid_request","error_description":"Identity not found"}
At line:1 char:13
+ $response = Invoke-WebRequest -Uri 'http://169.254.169.254/metadata/i ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

如果我使用 https(本教程使用 http,但为了安全起见,我也尝试了 https 变体)

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

有一个明显的延迟然后我得到这个:

Invoke-WebRequest : Unable to connect to the remote server
At line:1 char:13
+ $response = Invoke-WebRequest -Uri 'https://169.254.169.254/metadata/ ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

您是否发现我的设置有任何错误或有一些故障排除建议?

我可以重现这个错误,这个错误可能表示找不到身份。我建议再次检查您是否在 Azure 门户中正确分配了系统分配的标识。

验证使用 Az CLI 命令创建它时 sysytemAssignedIdentity 是否与虚拟机面板中的对象 ID 相同。如果您有很多租户,请确保在正确的租户中创建系统分配的身份。

为 Azure VM 创建身份后,错误消失了。看来你不需要使用https,我得到了和你一样的错误Unable to connect to the remote server