适用对象:
NoSQL
MongoDB
Cassandra
Gremlin
表
Azure 资源托管标识在 Microsoft Entra ID 中为 Azure 服务提供了一个自动托管标识。 本文介绍如何为 Azure Cosmos DB 帐户创建托管标识。
- 如果不熟悉 Azure 资源的托管标识,请参阅 什么是 Azure 资源的托管标识? 若要了解托管标识类型,请参阅 托管标识类型。
- 若要设置托管标识,您的帐户需要拥有 DocumentDB 帐户参与者 角色。
若要在现有 Azure Cosmos DB 帐户上启用系统分配的托管标识,请在 Azure 门户中导航到帐户,然后从边栏菜单中选择 “标识 ”。
在 “系统分配 ”部分下,将 “状态 ”翻转为 “打开 ”,然后选择“ 保存”。 系统会要求你确认是否创建了系统分配的托管标识。
创建并分配标识后,可以检索其对象(主体)ID。
重要
使用托管标识时,请确保使用 apiVersion
或更高的 2021-03-15
。
若要在新的或现有的 Azure Cosmos DB 帐户中启用系统分配的标识,请在资源定义中包含以下属性:
"identity": {
"type": "SystemAssigned"
}
然后,ARM 模板的 resources
节应如下所示:
"resources": [
{
"type": " Microsoft.DocumentDB/databaseAccounts",
"identity": {
"type": "SystemAssigned"
},
// ...
},
// ...
]
创建或更新 Azure Cosmos DB 帐户后,它会显示以下属性:
"identity": {
"type": "SystemAssigned",
"tenantId": "<azure-ad-tenant-id>",
"principalId": "<azure-ad-principal-id>"
}
若要在新建 Azure Cosmos DB 帐户时启用系统分配的标识,请添加 --assign-identity
选项:
resourceGroupName='myResourceGroup'
accountName='mycosmosaccount'
az cosmosdb create \
-n $accountName \
-g $resourceGroupName \
--locations regionName='China North 2' failoverPriority=0 isZoneRedundant=False \
--assign-identity
还可以使用 az cosmosdb identity assign
以下命令在现有帐户上添加系统分配的标识:
resourceGroupName='myResourceGroup'
accountName='mycosmosaccount'
az cosmosdb identity assign \
-n $accountName \
-g $resourceGroupName
创建或更新 Azure Cosmos DB 帐户后,可以使用以下命令提取分配的 az cosmosdb identity show
标识:
resourceGroupName='myResourceGroup'
accountName='mycosmosaccount'
az cosmosdb identity show \
-n $accountName \
-g $resourceGroupName
{
"type": "SystemAssigned",
"tenantId": "<azure-ad-tenant-id>",
"principalId": "<azure-ad-principal-id>"
}
若要在现有 Azure Cosmos DB 帐户上启用用户分配的托管标识,请在 Azure 门户中导航到帐户,然后从边栏菜单中选择 “标识 ”。
在“ 用户分配 ”部分下,选择“ + 添加”。
找到并选择要分配给 Azure Cosmos DB 帐户的所有标识,然后选择“添加”。
重要
使用托管标识时,请确保使用 apiVersion
或更高的 2021-03-15
。
若要在新的或现有的 Azure Cosmos DB 帐户中启用用户分配的标识,请在资源定义中包含以下属性:
"identity": {
"type": "UserAssigned",
"userAssignedIdentities": {
"<identity-resource-id>": {}
}
}
在你的 ARM 模板中,resources
的部分应如以下示例所示:
"resources": [
{
"type": " Microsoft.DocumentDB/databaseAccounts",
"identity": {
"type": "UserAssigned",
"userAssignedIdentities": {
"<identity-resource-id>": {}
}
},
// ...
},
// ...
]
创建或更新 Azure Cosmos DB 帐户后,它会显示以下属性:
"identity": {
"type": "UserAssigned",
"tenantId": "<azure-ad-tenant-id>",
"principalId": "<azure-ad-principal-id>"
}
若要在创建新的 Azure Cosmos DB 帐户时启用用户分配的标识,请添加 --assign-identity
选项并传递要分配的标识的资源 ID:
resourceGroupName='myResourceGroup'
accountName='mycosmosaccount'
az cosmosdb create \
-n $accountName \
-g $resourceGroupName \
--locations regionName='China North 2' failoverPriority=0 isZoneRedundant=False \
--assign-identity <identity-resource-id>
还可以使用 az cosmosdb identity assign
以下命令在现有帐户上添加用户分配的标识:
resourceGroupName='myResourceGroup'
accountName='mycosmosaccount'
az cosmosdb identity assign \
-n $accountName \
-g $resourceGroupName
--identities <identity-resource-id>
创建或更新 Azure Cosmos DB 帐户后,可以使用以下命令提取分配的 az cosmosdb identity show
标识:
resourceGroupName='myResourceGroup'
accountName='mycosmosaccount'
az cosmosdb identity show \
-n $accountName \
-g $resourceGroupName
{
"type": "UserAssigned",
"tenantId": "<azure-ad-tenant-id>",
"principalId": "<azure-ad-principal-id>"
}
重要
使用托管标识时,请确保使用 apiVersion
或更高的 2021-03-15
。
若要从 Azure Cosmos DB 帐户中删除系统分配的标识,请将 type
属性的 identity
设置为 None
:
"identity": {
"type": "None"
}
若要从 Azure Cosmos DB 帐户中删除所有托管的标识,请使用 az cosmosdb identity remove
命令:
resourceGroupName='myResourceGroup'
accountName='mycosmosaccount'
az cosmosdb identity remove \
-n $accountName \
-g $resourceGroupName
- 详细了解 Azure 资源的托管标识
- 详细了解 Azure Cosmos DB 上的客户管理的密钥