通过检查 ElasticAPV2.xts 中的 ComputerResourceGroupInfo.json,我将 msi 添加到 VMSS,我有:
"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 访问策略。
在虚拟机中,尝试使用
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
我的问题:
- 这是通过用户分配的身份使用访问令牌检索证书的正确方法吗?
- 为什么访问被拒绝?我的理解是这里不需要访问控制 (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"}