|
| 1 | +--- |
| 2 | +title: Use managed identities for Microsoft Energy Data Services on Azure |
| 3 | +description: Learn how to use Managed Identity to access Microsoft Energy Data Services from other Azure services. |
| 4 | +author: sandeepchads |
| 5 | +ms.author: sancha |
| 6 | +ms.service: energy-data-services |
| 7 | +ms.topic: how-to |
| 8 | +ms.date: 01/04/2023 |
| 9 | +ms.custom: template-how-to |
| 10 | +#Customer intent: As a developer, I want to use managed identity to access Microsoft Energy Data Services from other Azure services such as Azure Functions. |
| 11 | +--- |
| 12 | + |
| 13 | + |
| 14 | +# Use managed identity to access Microsoft Energy Data Services from other Azure services |
| 15 | + |
| 16 | +This article provides an overview on how to access data plane or control plane of Microsoft Energy Data Services from other Microsoft Azure Services using *managed identity*. |
| 17 | + |
| 18 | +There's a need for services such as Azure Functions etc. to be able to consume Microsoft Energy Data Services APIs. This interoperability will allow you to use the best of multiple Azure services, for example, you can write a script in Azure Function to ingest data in Microsoft Energy Data Services. Here, we should assume that Azure Functions is the source service while Microsoft Energy Data Services is the target service. To understand how this scenario works, it's important to understand the concept of managed identity. |
| 19 | + |
| 20 | +## Managed Identity |
| 21 | + |
| 22 | +A managed identity from Azure Active Directory (Azure AD) allows your application to easily access other Azure AD-protected resources. The identity is managed by the Azure platform and doesn't require you to create or rotate any secrets. Any Azure service that wants to access Microsoft Energy Data Services control plane or data plane for any operation can use managed identity to do so. |
| 23 | + |
| 24 | +Managed identity is of two types. It could be a system assigned managed identity or user assigned managed identity. System-assigned managed identities have their lifecycle tied to the resource that created them. User-assigned managed identities can be used on multiple resources. To learn more about managed identities, see [What are managed identities for Azure resources?](../active-directory/managed-identities-azure-resources/overview.md) |
| 25 | + |
| 26 | +Currently, other services can connect to Microsoft Energy Data Services using system or user assigned managed identity. However, Microsoft Energy Data Services doesn't support system assigned managed identity. |
| 27 | + |
| 28 | +For this scenario, we'll use a user assigned managed identity in Azure Function to call a data plane API in Microsoft Energy Data Services. |
| 29 | + |
| 30 | +## Pre-requisites |
| 31 | + |
| 32 | +Before you begin, make sure: |
| 33 | + |
| 34 | +* You've created a [Microsoft Energy Data Services instance](quickstart-create-microsoft-energy-data-services-instance.md). |
| 35 | + |
| 36 | +* You've created a [Azure Function App](../azure-functions/functions-create-function-app-portal.md). |
| 37 | + |
| 38 | +* You've created a [Python Azure Function using portal](../azure-functions/create-first-function-vs-code-python.md) or using [command line.](../azure-functions/create-first-function-cli-python.md) |
| 39 | + |
| 40 | +* You've created [user assigned managed identity](../active-directory/managed-identities-azure-resources/how-manage-user-assigned-managed-identities.md). You can create a system assigned identity as well however, this document will explain the flow using user assigned managed identity. |
| 41 | + |
| 42 | + |
| 43 | +## Steps for Azure Functions to access Microsoft Energy Data Services using Managed Identity |
| 44 | + |
| 45 | +There are five important steps to configure Azure Functions to access Microsoft Energy Data Services. |
| 46 | + |
| 47 | + |
| 48 | +### Step 1: Retrieve the Object ID of system or user-assigned identity that wants to access the Microsoft Energy Data Services APIs. |
| 49 | + |
| 50 | +1. You can get the *Object ID* of system assigned identity associated with Azure Functions by navigating to *Identity* screen of the Azure Function. |
| 51 | + |
| 52 | +[](media/how-to-use-managed-identity/1-object-id-system-assigned-identity.png#lightbox) |
| 53 | + |
| 54 | +2. Similarly, navigate to the *Overview* tab of the user assigned identity to find its *Object ID*. |
| 55 | + |
| 56 | +[](media/how-to-use-managed-identity/2-object-id-user-assigned-identity.png#lightbox) |
| 57 | + |
| 58 | +### Step 2. Retrieve the *Application ID* of system or user-assigned identity using the Object ID. |
| 59 | + |
| 60 | +1. Navigate to *Azure Active Directory (Azure AD)* in Azure |
| 61 | +2. Navigate to *Enterprise Application* tab. |
| 62 | +3. Search for the *Object ID* of the user assigned identity or system assigned identity in the *Search by application name or Object ID* search box. |
| 63 | +4. Copy the *Application ID* from Enterprise Application section of Azure Active Directory. |
| 64 | + |
| 65 | +[](media/how-to-use-managed-identity/3-object-id-application-id-user-assigned-identity.png#lightbox) |
| 66 | + |
| 67 | +### Step 3: Add the user assigned managed identity to Azure Functions |
| 68 | + |
| 69 | +1. Sign in to the Azure portal. |
| 70 | +2. In the Azure portal, navigate to your Azure Function. |
| 71 | +3. Under Account Settings, select Identity. |
| 72 | +4. Select the User assigned tab, and then select Add. |
| 73 | +5. Select your existing user-assigned managed identity and then select Add. You'll then be returned to the User assigned tab. |
| 74 | + |
| 75 | +[](media/how-to-use-managed-identity/4-user-assigned-identity-azure-function.png#lightbox) |
| 76 | + |
| 77 | +### Step 4: Add the application ID to entitlement groups to access Microsoft Energy Data Services APIs |
| 78 | +Next, you need to add this Application ID to appropriate groups using the entitlement service to access Microsoft Energy Data Services APIs. You need to perform the following actions: |
| 79 | + |
| 80 | +1. Find the tenant-id, client-id, client-secret, Microsoft Energy Data Services url, and data partition-id and generate the [access token](how-to-manage-users.md#prerequisites). You should have the following information handy with you: |
| 81 | + |
| 82 | +* tenant-id |
| 83 | +* client-id |
| 84 | +* client-secret |
| 85 | +* microsoft energy data services uri |
| 86 | +* data-partition-id |
| 87 | +* access token |
| 88 | +* Application ID of the managed identity |
| 89 | + |
| 90 | + |
| 91 | +2. Next, use the [add-member-api](https://microsoft.github.io/meds-samples/rest-apis/index.html?page=/meds-samples/rest-apis/entitlements_openapi.yaml#/add-member-api/addMemberUsingPOST) to add the Application ID of the user managed identity to appropriate entitlement groups. For example, in this case, we'll add the Application ID to two groups: |
| 92 | + |
| 93 | +* users@[partition ID].dataservices.energy |
| 94 | +* users.datalake.editors@[partition ID].dataservices.energy |
| 95 | + |
| 96 | +> [!NOTE] |
| 97 | +> In the below commands use the Application ID of the managed identity and not the Object Id of the managed identity in the below command. |
| 98 | +
|
| 99 | +* Adding Application ID of the managed identity to users@[partition ID].dataservices.energy |
| 100 | + |
| 101 | +3. Run the following CURL command on Azure bash: |
| 102 | + |
| 103 | +```bash |
| 104 | + curl --location --request POST 'https://<microsoft energy data services uri>/api/entitlements/v2/groups/users@ <data-partition-id>.dataservices.energy/members' \ |
| 105 | + --header 'data-partition-id: <data-partition-id>' \ |
| 106 | + --header 'Authorization: Bearer \ |
| 107 | + --header 'Content-Type: application/json' \ |
| 108 | + --data-raw '{ |
| 109 | + "email": "<Application ID of the managed identity>", |
| 110 | + "role": "MEMBER" |
| 111 | + }' |
| 112 | +``` |
| 113 | +
|
| 114 | +Sample response: |
| 115 | +```JSON |
| 116 | +{ |
| 117 | + "email": "<Application ID of the managed identity>", |
| 118 | + "role": "MEMBER" |
| 119 | + } |
| 120 | +``` |
| 121 | +* Adding Application ID of the managed identity to users.datalake.editors@[partition ID].dataservices.energy |
| 122 | +
|
| 123 | +4. Run the following CURL command on Azure bash: |
| 124 | +
|
| 125 | +```bash |
| 126 | + curl --location --request POST 'https://<microsoft energy data services uri>/api/entitlements/v2/groups/ users.datalake.editors@ <data-partition-id>.dataservices.energy/members' \ |
| 127 | + --header 'data-partition-id: <data-partition-id>' \ |
| 128 | + --header 'Authorization: Bearer \ |
| 129 | + --header 'Content-Type: application/json' \ |
| 130 | + --data-raw '{ |
| 131 | + "email": "<Application ID of the managed identity>", |
| 132 | + "role": "MEMBER" |
| 133 | + }' |
| 134 | +``` |
| 135 | + |
| 136 | +Sample response: |
| 137 | +```JSON |
| 138 | +{ |
| 139 | + "email": "<Application ID of the managed identity>", |
| 140 | + "role": "MEMBER" |
| 141 | + } |
| 142 | +``` |
| 143 | + |
| 144 | +### Step 5: Generate token for accessing Microsoft Energy Data Services from Azure Function |
| 145 | + |
| 146 | +Now Azure Functions is ready to access Microsoft Energy Data Services APIs. |
| 147 | + |
| 148 | +In this case, Azure function generates a token using User Assigned identity. The Azure function uses the Application ID present in the Microsoft Energy Data Services instance, while generating the token. |
| 149 | +Sample Azure function code. |
| 150 | + |
| 151 | +```python |
| 152 | +import logging |
| 153 | +import requests |
| 154 | +import azure.functions as func |
| 155 | +from msrestazure.azure_active_directory import MSIAuthentication |
| 156 | + |
| 157 | +def main(req: func.HttpRequest) -> str: |
| 158 | + logging.info('Python HTTP trigger function processed a request.') |
| 159 | + print('Python HTTP trigger function processed a request') |
| 160 | + |
| 161 | + creds = MSIAuthentication(resource="<clientId>") |
| 162 | + url = "https://<meds-uri>/api/entitlements/v2/groups" |
| 163 | + payload = {} |
| 164 | + print('creds is', creds) |
| 165 | + headers = { |
| 166 | + 'data-partition-id': '<data partition id>', |
| 167 | + 'Authorization': 'Bearer ' + creds.token["access_token"] |
| 168 | + } |
| 169 | + response = requests.request("GET", url, headers=headers, data=payload, verify=False) |
| 170 | + logging.info("Got a response") |
| 171 | + return response.text |
| 172 | +``` |
| 173 | +You should get the following successful response from Azure Function: |
| 174 | + |
| 175 | +[](media/how-to-use-managed-identity/5-azure-function-success.png#lightbox) |
| 176 | + |
| 177 | +With the following steps completed, you're now able to use Azure Functions to access Microsoft Energy Data Services APIs with appropriate use of managed identities. |
| 178 | + |
| 179 | +## Next steps |
| 180 | +<!-- Add a context sentence for the following links --> |
| 181 | +To learn more about Lockbox in Microsoft Energy Data Services |
| 182 | +> [!div class="nextstepaction"] |
| 183 | +> [Lockbox in Microsoft Energy Data Services](how-to-use-managed-identity.md) |
0 commit comments