对 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 已经由系统管理员创建。
我在 VM 的同一资源组中创建了 KeyVault,并添加了 2 个机密。
我检查了 VM 是否已在 Active Directory 中注册并且它具有系统分配的标识。
我向我的 Keyvault 添加了一个访问策略,允许读取和列出我的 VM 的机密。
我尝试了教程中的示例应用程序。
如果我 运行 它来自 Visual Studio 在我的电脑上(使用 Azure Cli 通过 AZ LOGIN 进行身份验证)我可以获得秘密
如果我在我的 Azure VM 中部署该应用程序,当我尝试获取令牌时出现 "Identity not found" 异常
我还尝试在 VM 上使用 powershell 获取令牌:
$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
。
我需要将 .Net Core 2.2 应用程序部署到 Azure VM,并且我必须从 Azure Keyvault 读取一些凭据。
我无法完全控制环境。
我遵循了这个教程 Use Azure Key Vault with a Windows virtual machine in .NET 尽我所能,但实际上 VM 已经由系统管理员创建。
我在 VM 的同一资源组中创建了 KeyVault,并添加了 2 个机密。
我检查了 VM 是否已在 Active Directory 中注册并且它具有系统分配的标识。
我向我的 Keyvault 添加了一个访问策略,允许读取和列出我的 VM 的机密。
我尝试了教程中的示例应用程序。
如果我 运行 它来自 Visual Studio 在我的电脑上(使用 Azure Cli 通过 AZ LOGIN 进行身份验证)我可以获得秘密
如果我在我的 Azure VM 中部署该应用程序,当我尝试获取令牌时出现 "Identity not found" 异常
我还尝试在 VM 上使用 powershell 获取令牌:
$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
。