你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

创建、分配和使用用户分配的托管标识

在本指南中,您将学习:

  • 创建用户分配的托管标识(UAMI),用于与 Azure 运营商服务管理器(AOSM)配合使用。
  • 分配 UAMI 权限以访问所需的资源。
  • 执行网络功能 (NF) 或站点网络服务 (SNS) 操作时,请使用 UAMI。

警告

如果预期 SNS 操作可能运行四个小时或更长时间,则需要 UAMI。 如果在 SNS 长时间运行操作期间未使用 UAMI,SNS 可能会在组件操作完成之前错误地报告失败状态。

先决条件

  • 必须通过 创建自定义角色。 本文假定你已将自定义角色命名为“自定义角色 - AOSM 服务作员对发布服务器的访问权限”。

  • 必须与您的网络服务设计师合作,以了解您的托管标识所需的权限,以及 SNS 使用的网络函数定义版本(NFDV)。

  • 您需要在您选择的发布者的网络功能定义版本资源上具有“所有者”或“用户访问管理员”角色。 还必须拥有一个资源组,并且为其分配了“所有者”或“用户访问管理员”角色。

创建 UAMI

首先,创建 UAMI。 有关详细信息,请参阅 为 SNS 创建用户分配的托管标识

创建自定义角色并分配给 UAMI

接下来,创建自定义角色。 首先考虑基于范围的最佳方法,然后创建角色并将其分配给新的 UAMI。

UAMI 自定义角色的范围注意事项

必须为自定义角色分配足够的权限才能访问用户资源。 自定义角色可以限定为单个子资源(如 NFDV),以实现最精细的控制。 或者,自定义角色的范围可以限定为父资源,例如发布者资源组,该资源组授予对所有子资源同等的访问权限。 若要单独或通过父级执行适当的操作,必须将以下所有资源分配给自定义角色:

  • 所有网络函数定义组(NFDG)和版本。
  • 所有网络函数定义(NFD)和版本。
  • 所有网络服务设计组(NSD)和版本。
  • 所有配置组模式(CGS)及其版本。
  • 所有自定义位置。

允许对所选作用域拥有适当的权限

UAMI 需要以下各项权限来执行所需的 SNS 操作:

  • 在 NFD 上;
    • Microsoft.HybridNetwork/publishers/networkFunctionDefinitionGroups/networkFunctionDefinitionVersions/use/action
    • Microsoft.HybridNetwork/Publishers/NetworkFunctionDefinitionGroups/NetworkFunctionDefinitionVersions/read
  • 在 NSD 上;
    • Microsoft.HybridNetwork/publishers/networkServiceDesignGroups/networkServiceDesignVersions/use/action
    • Microsoft.HybridNetwork/publishers/networkServiceDesignGroups/networkServiceDesignVersions/read
  • 在 CGS 上;
    • Microsoft.HybridNetwork/Publishers/ConfigurationGroupSchemas/read
  • 在自定义位置;
    • Microsoft.ExtendedLocation/customLocations/deploy/action
    • Microsoft.ExtendedLocation/customLocations/read
  • 此外,UAMI 需要对自身具有访问权限;
    • Microsoft.ManagedIdentity/userAssignedIdentities/assign/action

如果使用父资源范围方法,则将所需的权限应用于父资源。

注释

请勿提供对这些发布者资源的任何写入或删除访问权限。

通过门户分配自定义角色

  1. 访问 Azure 门户并打开您选择的资源范围;例如,发布者资源组或网络功能定义版本。

  2. 在此项的侧菜单中,选择“访问控制”(IAM)。

  3. 选择“添加角色分配”

    显示发布者资源组访问控制页的屏幕截图。

  4. “作业函数角色 ”下,在列表中找到自定义角色,然后继续执行 “下一步”。

    显示“添加角色分配”屏幕的屏幕截图。

  5. 选择 “托管标识”,然后选择 “+ 选择成员” ,然后找到并选择新的托管标识。 选择 选择

    显示添加角色分配并选择托管标识的屏幕截图。

  6. 选择“查看并分配”

重复角色分配

根据所选范围方法,对所有剩余资源重复执行角色分配过程。

通过门户分配托管标识操作员角色

  1. 转到 Azure 门户,搜索“托管标识”

  2. 从“托管标识”列表中选择你的标识

  3. 在边栏菜单中,选择“访问控制(IAM)”

  4. 选择 添加角色分配,然后选择 托管标识操作员 角色。 屏幕截图显示“托管标识操作员”角色的“添加角色分配”操作。

  5. 选择 托管标识操作员 角色。

    屏幕截图显示“托管标识操作员”角色。

  6. 选择“托管标识”

  7. 选择 “+ 选择成员”,导航到用户分配的托管身份,然后继续进行任务分配。

    屏幕截图显示“添加角色分配”屏幕,其中已选择“托管标识”。

完成本文中概述的所有任务可确保站点网络服务 (SNS) 具有在指定 Azure 环境中有效运行所需的权限。

通过 bicep 创建权限并将其分配给 UAMI

通过 bicep 脚本也支持创建和分配权限所需的操作。 在工作流管道中如果需要自动化这些操作,此方法可能会更有效。 以下示例演示了建立具有最少分配角色的 UAMI 所需的 bicep 操作。 根据需要,根据范围方法扩展角色分配。

// ----------- MIO Role Definition -----------
// This role is used to assign the Managed Identity Operator role to the User Assigned Managed Identity (UAMI).
@description('This is the built-in MIO role. See https://learn.microsoft.com/en-us/azure/role-based-access-control/built-in-roles#managed-identity-operator')
resource MIORoleDefinition 'Microsoft.Authorization/roleDefinitions@2018-01-01-preview' existing = {
  scope: managedIdentity
  name: 'f1a07417-d97a-45cb-824c-7a7467783830'
}

// This role is used to assign the Contributor role to the User Assigned Managed Identity (UAMI) at the resource group level.
resource ContributorRoleDefinition 'Microsoft.Authorization/roleDefinitions@2018-01-01-preview' existing = {
  scope: subscription()
  name: 'b24988ac-6180-42a0-ab88-20f7382dd24c'
}

// Assign the Managed Identity Operator role to the User Assigned Managed Identity (UAMI) at the scope of the managed identity.
resource roleAssignment 'Microsoft.Authorization/roleAssignments@2020-04-01-preview' = {
  name: guid(resourceGroup().id, principalId, MIORoleDefinition.id)
  scope: managedIdentity
  properties: {
    roleDefinitionId: MIORoleDefinition.id
    principalId: managedIdentity.properties.principalId
    principalType: 'ServicePrincipal'
  }
}
// Get reference to the target resource group
resource targetRg 'Microsoft.Resources/resourceGroups@2022-09-01' existing = {
  name: 'publisherResourceGroupName' // Replace with the actual resource group name
  scope: subscription('subscriptionId')
}

// Assign the Contributor role to the User Assigned Managed Identity (UAMI) at the scope of the publisher resource group.
resource roleAssignmentContributor 'Microsoft.Authorization/roleAssignments@2020-04-01-preview' = {
  name: guid(resourceGroup().id, principalId, ContributorRoleDefinition.id)
  scope: targetRg
  properties: {
    roleDefinitionId: ContributorRoleDefinition.id
    principalId: managedIdentity.properties.principalId
    principalType: 'ServicePrincipal'
  }
}

将 UAMI 与 NF 和 SNS 操作配合使用

NF 模板注意事项

必须更新 NF 模板才能包含 identityObj 参数。 以下 JSON 示例演示如何将此参数与通用 NF 设置配合使用:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "nameValue": {
      "type": "string",
      "defaultValue": "[concat('anf-', substring(uniqueString(deployment().name), 0, 6))]"
    },
    "locationValue": {
      "type": "string",
      "defaultValue": "eastus2euap"
    },
    "nfviTypeValue": {
      "type": "string",
      "defaultValue": "AzureArcKubernetes"
    },
    "nfviIdValue": {
      "type": "string"
    },
    "config": {
      "type": "object",
      "defaultValue": {}
    },
    "nfdvId": {
      "type": "string"
    },
    "identityObj": {
      "type": "object",
      "defaultValue": {
        "type": "UserAssigned",
        "userAssignedIdentities": {
          "/subscriptions/<subscriptionId>/resourceGroups/<rgName>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<uaminame>": {}
        }
      }
    }
  },
  "variables": {
    "deploymentValuesValue": "[string(createObject('role1releasenamespace', parameters('config').role1releasenamespace, 'role1releasename',parameters('config').role1releasename, 'role2releasenamespace', parameters('config').role2releasenamespace, 'role2releasename',parameters('config').role2releasename,'role3releasenamespace', parameters('config').role3releasenamespace, 'role3releasename',parameters('config').role3releasename))]",
    "nfName": "[concat(parameters('nameValue'), '-CNF')]"
  },
  "resources": [
    {
      "type": "Microsoft.HybridNetwork/networkFunctions",
      "apiVersion": "2024-04-15",
      "name": "[variables('nfName')]",
      "___location": "[parameters('locationValue')]",
      "identity": "[parameters('identityObj')]",
      "properties": {
        "networkFunctionDefinitionVersionResourceReference": {
          "id": "[parameters('nfdvId')]",
          "idType": "Open"
        },
        "nfviType": "[parameters('nfviTypeValue')]",
        "nfviId": "[parameters('nfviIdValue')]",
        "allowSoftwareUpdate": true,
        "configurationType": "Secret",
        "secretDeploymentValues": "[string(variables('deploymentValuesValue'))]"
      }
    }
  ]
}

SNS 模板注意事项

必须更新 SNS 模板才能包含标识资源参数。 以下 bicep 示例演示如何将此参数与泛型 SNS 设置配合使用:

resource azCoreSnsUAMI 'Microsoft.HybridNetwork/sitenetworkservices@2023-09-01' = {
  name: snsNameUAMI
  ___location: ___location
  sku: {
    name: 'Standard'
  }
  identity:  {
    type: 'UserAssigned'
    userAssignedIdentities: {
     '${managedIdentity.id}': {} 
    }
  }
  properties: {
    siteReference: {
      id: azCoreSite.id
    }
    networkServiceDesignVersionResourceReference: {
        id: nsdv.id
        idType: 'Open'
    }
    desiredStateConfigurationGroupValueReferences: {
      Test_Configuration: {
        id: azCoreCgv.id
      }
      Secret_Configuration:{
        id:azCoreCgvSecret.id
      }
    }
  }
}