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

使用托管标识从其他 Azure 服务访问用于能源的 Azure 数据管理器

本文介绍如何使用 托管标识从其他 Microsoft Azure 服务访问 Azure Data Manager for Energy 的数据平面或控制平面。

需要 Azure Functions 等服务才能使用适用于能源 API 的 Azure 数据管理器。 通过这种互操作性,可以使用多个 Azure 服务的最佳功能。

例如,可以在 Azure Functions 中编写脚本以引入 Azure Data Manager for Energy 中的数据。 在这种情况下,应假定 Azure Functions 是源服务,Azure Data Manager for Energy 是目标服务。

本文指导你完成配置 Azure Functions 以访问 Azure Data Manager for Energy 的五个主要步骤。

托管标识概述

Microsoft Entra ID 中的托管标识允许应用程序轻松访问其他受 Microsoft Entra 保护的资源。 标识由 Azure 平台管理,无需创建或轮换任何机密。 任何想要访问 Azure Data Manager for Energy 控制平面或数据平面的任何 Azure 服务都可以使用托管标识执行此操作。

托管标识分为两种类型:

  • 系统分配的 托管标识的生命周期与创建它们的资源相关联。
  • 用户分配的 托管标识可用于多个资源。

若要详细了解托管标识,请参阅什么是 Azure 资源的托管标识?

目前,其他服务可以使用系统分配的托管标识或用户分配的托管标识连接到 Azure Data Manager for Energy。 但是,Azure Data Manager for Energy 不支持系统分配的托管标识。

对于本文中的方案,你将在 Azure Functions 中使用用户分配的托管标识在 Azure Data Manager for Energy 中调用数据平面 API。

先决条件

在开始之前,请创建以下资源:

步骤 1:检索对象 ID

若要检索将访问 Azure Data Manager for Energy API 的用户分配标识的对象 ID:

  1. 登录 Azure 门户
  2. 转到托管标识,然后选择“ 概述”。
  3. 在“概要”,记下“对象”(主体)ID 值。

Screenshot of the object ID for a user-assigned identity.

步骤 2:检索应用程序 ID

使用对象 ID 检索用户分配标识的应用程序 ID:

  1. 在 Azure 门户中,转到 Microsoft Entra ID
  2. 在左侧菜单中,选择“企业应用程序”。
  3. “按应用程序名称或对象 ID 搜索”框中,输入对象 ID。
  4. 对于结果中显示的应用程序,请注意 应用程序 ID 值。

Screenshot of the application ID for a user-assigned identity.

步骤 3:将用户分配的托管标识添加到 Azure Functions

  1. 在Azure 门户中,转到 Azure 函数。
  2. 在“帐户设置”下选择“标识” 。
  3. 选择“用户分配”选项卡,然后选择“添加” 。
  4. 选择现有用户分配的托管标识,然后选择“ 添加”。 然后返回到 “用户分配 ”选项卡。

Screenshot of a newly added user-assigned identity to an Azure function.

步骤 4:将应用程序 ID 添加到权利组

接下来,将应用程序 ID 添加到将使用权利服务访问 Azure Data Manager for Energy API 的相应组。 以下示例将应用程序 ID 添加到两个组:

  • users@[partition ID].dataservices.energy
  • users.datalake.editors@[partition ID].dataservices.energy

添加应用程序 ID:

  1. 收集以下信息:

    • 租户 ID
    • 客户端 ID
    • 客户端机密
    • 适用于能源的 Azure 数据管理器 URI
    • 数据分区 ID
    • 访问令牌
    • 托管标识的应用程序 ID
  2. 使用“添加成员 API”将用户分配的托管标识的应用程序 ID 添加到相应的权利组。

    注意

    在以下命令中,请务必使用托管标识的应用程序 ID,而不是对象 ID。

    1. 若要将应用程序 ID 添加到 users@[partition ID].dataservices.energy 组,请在 Azure 中通过 Bash 运行以下 cURL 命令:

       curl --location --request POST 'https://<Azure Data Manager for Energy URI>/api/entitlements/v2/groups/users@ <data-partition-id>.dataservices.energy/members' \
          --header 'data-partition-id: <data-partition-id>' \
          --header 'Authorization: Bearer \
          --header 'Content-Type: application/json' \
          --data-raw '{
                          "email": "<application ID of the managed identity>",
                          "role": "MEMBER"
                      }'
      

      下面是一个示例响应:

      {
          "email": "<application ID of the managed identity>",
          "role": "MEMBER"
          }
      
    2. 若要将应用程序 ID 添加到 users.datalake.editors@[partition ID].dataservices.energy group,请在 Azure 中通过 Bash 运行以下 cURL 命令:

       curl --location --request POST 'https://<Azure Data Manager for Energy URI>/api/entitlements/v2/groups/ users.datalake.editors@ <data-partition-id>.dataservices.energy/members' \
          --header 'data-partition-id: <data-partition-id>' \
          --header 'Authorization: Bearer \
          --header 'Content-Type: application/json' \
          --data-raw '{
                          "email": "<application ID of the managed identity>",
                          "role": "MEMBER"
                      }'
      

      下面是一个示例响应:

      {
          "email": "<application ID of the managed identity>",
          "role": "MEMBER"
          }
      

步骤 5:生成令牌

现在,Azure Functions 已准备好访问适用于能源的 Azure 数据管理器 API。

Azure 函数使用用户分配的标识生成令牌。 该函数使用 Azure Data Manager for Energy 实例中存在的应用程序 ID,同时生成令牌。

下面是 Azure 函数代码的示例:

import logging
import requests
import azure.functions as func
from msrestazure.azure_active_directory import MSIAuthentication

def main(req: func.HttpRequest) -> str:
    logging.info('Python HTTP trigger function processed a request.')

    //To authenticate by using a managed identity, you need to pass the Azure Data Manager for Energy application ID as the resource.     
    //To use a user-assigned identity, you should include the   
    //client ID as an additional parameter.
    //Managed identity using user-assigned identity: MSIAuthentication(client_id, resource)

    creds = MSIAuthentication(client_id="<client_id_of_managed_identity>”, resource="<meds_app_id>")
    url = "https://<meds-uri>/api/entitlements/v2/groups"
    payload = {}
    // Passing the data partition ID of Azure Data Manager for Energy in headers along with the token received using the managed instance.
    headers = {
        'data-partition-id': '<data partition id>',
        'Authorization': 'Bearer ' + creds.token["access_token"]
    }
    response = requests.request("GET", url, headers=headers, data=payload, verify=False)
    return response.text

应从 Azure Functions 获得以下成功的响应:

Screenshot of a success message from Azure Functions.

完成上述步骤后,现在可以使用 Azure Functions 通过适当的托管标识来访问 Azure Data Manager for Energy API。

后续步骤

了解密码箱: