1. 系统分配的托管标识和用户分配的托管标识有什么区别?
根据我的研究,系统分配的托管标识直接在 Azure 服务实例上启用。系统分配标识的生命周期直接与启用它的 Azure 服务实例相关联。如果实例被删除,Azure 会自动清理 Azure AD 中的凭据和标识。
但是,用户分配的托管标识创建为独立的 Azure 资源。创建标识后,可以将标识分配给一个或多个 Azure 服务实例。用户分配标识的生命周期与其分配到的 Azure 服务实例的生命周期分开管理。
更多详细信息,请参阅文档。
2. 如何配置 MSI、Azure Key Vault 并授予访问权限
预配用户分配的托管标识
根据我的研究,如果我们想提供用户分配的托管标识,我们可以使用Azure REST API、Azure Powershell和Azure CLI
例如
Azure CLI
az login
az identity create -g <RESOURCE GROUP> -n <USER ASSIGNED IDENTITY NAME>
Azure REST API 使用 Azure CLI 获取访问令牌
az login
az account get-access-token
湾。调用其余 api
curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroup
s/<RESOURCE GROUP>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<USER ASSIGNED IDENTITY NAME>?api-version=2015-08-31-preview' -X PUT -d '{"loc
ation": "<LOCATION>"}' -H "Content-Type: application/json" -H "Authorization: Bearer <ACCESS TOKEN>"
预配 Azure Key Vault 并授予访问权限
根据我的研究,如果我们想实现它,如果我们想提供用户分配的托管标识,我们可以使用Azure REST API、Azure Powershell、Azure CLI和 sdk(例如 .net)。更多详细信息,请参阅文档
例如
Azure 休息 API
一种。使用 Azure CLI 获取访问令牌
az login
az account get-access-token
湾。调用其余 api
PUT https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.KeyVault/vaults/{vaultName}?api-version=2018-02-14
Header :
Content-Type: application/json
Authorization: Bearer <ACCESS TOKEN>
Body
{
"location": "westus",
"properties": {
"tenantId": "<your tenant id>",
"sku": {
"family": "A",
"name": "standard"
},
"accessPolicies": [
{
"tenantId": "<your tenant id>",
"objectId": "<the object id of the MSI>",
"permissions": {
"keys": [
"encrypt",
"decrypt",
"wrapKey",
"unwrapKey",
"sign",
"verify",
"get",
"list",
"create",
"update",
"import",
"delete",
"backup",
"restore",
"recover",
"purge"
],
"secrets": [
"get",
"list",
"set",
"delete",
"backup",
"restore",
"recover",
"purge"
],
"certificates": [
"get",
"list",
"delete",
"create",
"import",
"update",
"managecontacts",
"getissuers",
"listissuers",
"setissuers",
"deleteissuers",
"manageissuers",
"recover",
"purge"
]
}
}
],
"enabledForDeployment": true,
"enabledForDiskEncryption": true,
"enabledForTemplateDeployment": true
}
}
.Net SDK
一种。使用 Azure CLI 创建服务主体
az login
az ad sp create-for-rbac -n "MyApp" --role contributor --sdk-auth
湾。代码。有关详细信息,请参阅示例
// please install package Microsoft.Azure.Management.Fluent
var credentials = SdkContext.AzureCredentialsFactory
.FromServicePrincipal(<the sp app id>,
<the sp password>,
tenantId,
AzureEnvironment.AzureGlobalCloud);
var azure = Microsoft.Azure.Management.Fluent.Azure
.Configure()
.Authenticate(credentials)
.WithSubscription ("<your subscription id>");
var vault =await azure.Vaults.Define("")
.WithRegion(Region.AsiaSouthEast)
.WithExistingResourceGroup("groupname")
.DefineAccessPolicy()
.ForObjectId("the object id of msi")
.AllowCertificateAllPermissions()
.AllowKeyAllPermissions()
.AllowSecretAllPermissions()
.Attach()
.WithDeploymentEnabled()
.WithDiskEncryptionEnabled()
.WithTemplateDeploymentEnabled()
.WithSku(Microsoft.Azure.Management.KeyVault.Fluent.Models.SkuName.Standard)
.CreateAsync()