Skip to content

Commit 267728b

Browse files
authored
Merge pull request #49273 from MicrosoftDocs/NEW-intro-azure-openai-managed-identity-auth-javascript
New intro azure openai managed identity auth javascript
2 parents 06c7f9d + 99055f5 commit 267728b

12 files changed

+303
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### YamlMime:ModuleUnit
2+
uid: introduction-azure-openai-managed-identity-authentication-javascript.introduction
3+
title: Introduction
4+
metadata:
5+
title: Introduction
6+
description: Introduction to using managed identity authentication for JavaScript applications with Azure OpenAI Service.
7+
ms.date: 02/10/2025
8+
author: sarah-yo
9+
ms.author: sayoung
10+
ms.topic: unit
11+
durationInMinutes: 1
12+
content: |
13+
[!include[](includes/1-introduction.md)]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### YamlMime:ModuleUnit
2+
uid: introduction-azure-openai-managed-identity-authentication-javascript.azure-openai-rbac-roles
3+
title: Azure OpenAI RBAC roles
4+
metadata:
5+
title: Azure OpenAI RBAC roles
6+
description: Learn about the Azure OpenAI RBAC roles that are available for managing access to resources.
7+
ms.date: 02/10/2025
8+
author: sarah-yo
9+
ms.author: sayoung
10+
ms.topic: unit
11+
durationInMinutes: 7
12+
content: |
13+
[!include[](includes/2-azure-openai-rbac-roles.md)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### YamlMime:ModuleUnit
2+
uid: introduction-azure-openai-managed-identity-authentication-javascript.keyless-authentication-methods
3+
title: Keyless authentication methods
4+
metadata:
5+
title: Keyless authentication methods
6+
description: Learn about keyless authentication methods that can be used to authenticate with Azure services.
7+
ms.date: 02/10/2025
8+
author: sarah-yo
9+
ms.author: sayoung
10+
ms.topic: unit
11+
durationInMinutes: 12
12+
content: |
13+
[!include[](includes/3-keyless-authentication-methods.md)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### YamlMime:ModuleUnit
2+
uid: introduction-azure-openai-managed-identity-authentication-javascript.benefits-of-using-managed-identity-for-azure-openai
3+
title: Benefits of using a managed identity for Azure OpenAI
4+
metadata:
5+
title: Benefits of using a managed identity for Azure OpenAI
6+
description: Learn about the benefits of using a managed identity for Azure OpenAI.
7+
ms.date: 02/10/2025
8+
author: sarah-yo
9+
ms.author: sayoung
10+
ms.topic: unit
11+
durationInMinutes: 4
12+
content: |
13+
[!include[](includes/4-benefits-managed-identity-azure-openai.md)]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
### YamlMime:ModuleUnit
2+
uid: introduction-azure-openai-managed-identity-authentication-javascript.knowledge-check
3+
title: Knowledge check
4+
metadata:
5+
title: Knowledge check
6+
description: Display your knowledge.
7+
ms.date: 02/10/2025
8+
author: sarah-yo
9+
ms.author: sayoung
10+
ms.topic: unit
11+
durationInMinutes: 4
12+
content: Choose the best response for each question.
13+
quiz:
14+
questions:
15+
- content: "What is the primary benefit of using managed identities over API keys in Azure OpenAI?"
16+
choices:
17+
- content: "Enhanced security"
18+
isCorrect: true
19+
explanation: "Managed identities eliminate the need to store credentials within application code, reducing the risk of credential leaks. They leverage Entra ID for authentication, providing a more secure and automated method for accessing Azure services."
20+
- content: "Increased performance"
21+
isCorrect: false
22+
explanation: "While managed identities improve security, they don't inherently increase application performance."
23+
- content: "Cost reduction"
24+
isCorrect: false
25+
explanation: "Managed identities don't directly impact the cost of Azure services; their primary benefit is enhanced security."
26+
- content: "Which role in Azure OpenAI allows you to create custom fine-tuned models and manage model deployments?"
27+
choices:
28+
- content: "Cognitive Services OpenAI Contributor"
29+
isCorrect: true
30+
explanation: "This role includes permissions to create custom fine-tuned models, upload datasets, and manage model deployments."
31+
- content: "Cognitive Services OpenAI User"
32+
isCorrect: false
33+
explanation: "The OpenAI User role has limited permissions and can't create or manage models. It only allows viewing resources and making inference API calls."
34+
- content: "Cognitive Services Usages Reader"
35+
isCorrect: false
36+
explanation: "The Usages Reader role is focused on monitoring usage and doesn't have permissions for model management. It only allows viewing quota usage."
37+
- content: "In the provided JavaScript code example, what is the purpose of the openai.ChatCompletion.create method?"
38+
choices:
39+
- content: "Generate a response from the OpenAI model"
40+
isCorrect: true
41+
explanation: "This method sends a request to the OpenAI service with the specified parameters, such as the model, prompt, and other settings, and returns the generated response."
42+
- content: "Authenticate the user"
43+
isCorrect: false
44+
explanation: "Authentication is handled separately, typically using credentials or tokens, and isn't the purpose of this method."
45+
- content: "Store the API response"
46+
isCorrect: false
47+
explanation: "This method generates a response from the OpenAI model but doesn't handle storing the response; storing data would require additional code."
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### YamlMime:ModuleUnit
2+
uid: introduction-azure-openai-managed-identity-authentication-javascript.summary
3+
title: Summary
4+
metadata:
5+
title: Summary
6+
description: This unit summarizes what you learned in the module.
7+
ms.date: 02/10/2025
8+
author: sarah-yo
9+
ms.author: sayoung
10+
ms.topic: unit
11+
durationInMinutes: 1
12+
content: |
13+
[!include[](includes/6-summary.md)]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Good security practices require that you control access to applications that you develop. Developing an application in Azure OpenAI Service is no different. In this module, you learn about configuring authentication to Azure OpenAI by using JavaScript, the available roles in Azure OpenAI, and the benefits of using managed identities over API keys.
2+
3+
By the end of this module, you are able to:
4+
5+
- Describe the role-based access control (RBAC) roles for use in Azure OpenAI and configure role assignments.
6+
- Describe the differences between using API keys and managed identities within Azure OpenAI.
7+
- Configure keyless authentication to Azure OpenAI for local development for JavaScript.
8+
- Configure keyless authentication to Azure OpenAI for Azure-hosted environments for JavaScript.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
These RBAC roles are available for you to assign to identities for Azure OpenAI Service:
2+
3+
- The *Cognitive Services OpenAI User* role allows viewing resources, endpoints, and model deployments; using playground experiences; and making inference API calls. However, it doesn't permit creating resources, viewing/copying/regenerating keys, or managing model deployments.
4+
- The *Cognitive Services OpenAI Contributor* role includes all user permissions plus the ability to create custom fine-tuned models, upload datasets, and manage model deployments. It doesn't allow creating new resources or managing keys.
5+
- The *Cognitive Services Contributor* role permits creating new resources, viewing and managing keys, creating and managing model deployments, and using playground experiences. It doesn't allow access to quotas or making inference API calls.
6+
- The *Cognitive Services Usages Reader* role allows viewing quota usage across a subscription. This role provides minimal access and is typically combined with other roles.
7+
8+
Always choose a role that provides the lowest amount of privilege required for the identity to do the tasks that it needs to perform. For more detail on Azure OpenAI RBAC roles.
9+
10+
## Configure role assignments in the Azure portal
11+
12+
To enable keyless authentication, follow these steps to configure the necessary role assignments:
13+
14+
1. In the Azure portal, go to your specific Azure OpenAI resource.
15+
2. On the service menu, select **Access Control (IAM)**.
16+
3. Select **Add role assignment**. On the pane that opens, select the **Role** tab.
17+
4. Select the role that you want to assign.
18+
5. On the **Members** tab, select a user, group, service principal, or managed identity to assign to the role.
19+
6. On the **Review + assign** tab, confirm your selections. Select **Review + assign** to finalize the role assignment.
20+
21+
Within a few minutes, the selected user or identity is granted the assigned role at the chosen scope. The user or identity can then access Azure OpenAI services without needing an API key.
22+
23+
## Configure role assignments in the Azure CLI
24+
25+
To configure role assignments by using the Azure CLI, perform these steps:
26+
27+
1. Find the role for your usage of Azure OpenAI. Depending on how you intend to set that role, you need either the name or the ID:
28+
29+
- For the Azure CLI or Azure PowerShell, you can use a role name.
30+
- For Bicep, you need the role ID.
31+
32+
Use the following table to select a role name or role ID:
33+
34+
| Use case | Role name | Role ID |
35+
|---|---|---|
36+
| Assistants | Cognitive Services OpenAI Contributor | a001fd3d-188f-4b5d-821b-7da978bf7442 |
37+
| Chat completions | Cognitive Services OpenAI User | 5e0bd9bd-7b93-4f28-af87-19fc36ad61bd |
38+
39+
1. Select an identity type to use:
40+
41+
- A *personal identity* is tied to your Azure sign-in.
42+
- A *managed identity* is managed by Azure and created for use on Azure. For this choice, create a user-assigned managed identity. When you create the managed identity, you need the client ID (also called the app ID).
43+
44+
1. Find your personal identity and use the ID as the `<identity-id>` value in this step.
45+
46+
For local development, to get your own identity ID, use the following command. You need to sign in with `az login` before using this command.
47+
48+
```azurecli
49+
az ad signed-in-user show \
50+
--query id -o tsv
51+
```
52+
53+
1. Assign the RBAC role to the identity for the resource group. To grant your identity permissions to your resource through RBAC, assign a role by using the Azure CLI command `az role assignment create`. Replace `<identity-id>`, `<subscription-id>`, and `<resource-group-name>` with your actual values.
54+
55+
```azurecli
56+
az role assignment create \
57+
--role "Cognitive Services OpenAI User" \
58+
--assignee "\<identity-id>" \
59+
--scope "/subscriptions/\<subscription-id>/resourceGroups/\<resource-group-name>"
60+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
In keyless authentication to Azure OpenAI for JavaScript, you use Azure's managed identities or service principals to authenticate instead of hardcoding API keys or other credentials. This is done through the `DefaultAzureCredential` or `ManagedIdentityCredential` class, which provides a secure and streamlined way to obtain tokens needed for authenticating Azure services. Here's how it works in practice:
2+
3+
1. Set Up Azure Environment: Ensure your Azure environment is configured correctly with managed identities.
4+
2. Initialize Credentials: Use the `DefaultAzureCredential` class or `ManagedIdentityCredential` class from the Azure Identity SDK for JavaScript to handle the authentication process seamlessly.
5+
6+
Here's an example code snippet:
7+
8+
```javascript
9+
import { AzureOpenAI } from 'openai';
10+
import { getBearerTokenProvider, DefaultAzureCredential } from '@azure/identity';
11+
12+
// Make sure to set AZURE_OPENAI_ENDPOINT with the endpoint of your Azure resource.
13+
14+
const credential = new DefaultAzureCredential();
15+
const scope = 'https://cognitiveservices.azure.com/.default';
16+
const azureADTokenProvider = getBearerTokenProvider(credential, scope);
17+
18+
// Create client instance
19+
// 3) Create an Azure OpenAI client
20+
const openai = new AzureOpenAI({ azureADTokenProvider });
21+
22+
// 4) Make API call and print response
23+
const result = await openai.chat.completions.create({
24+
model: 'gpt-4-1106-preview',
25+
messages: [{ role: 'user', content: 'Say hello!' }],
26+
});
27+
28+
console.log(result.choices[0]!.message?.content);
29+
```
30+
31+
3. Create Client Instance: Instantiate your `AzureOpenAI` with the endpoint and the credentials obtained from the `DefaultAzureCredential`.
32+
4. Make API Calls: Use the client to interact with Azure OpenAI services securely, without explicitly handling sensitive credentials.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
When employing a Managed Identity, you need to specify the client ID of the user-managed identity when creating an instance of the `DefaultAzureCredential` or `ManagedIdentityCredential` class in your JavaScript application. The client ID value is established as an environment variable `$AZURE_CLIENT_ID` when the Managed Identity is set up.
2+
3+
At first glance, it might seem that this is the only vital piece of information required for the connection, aside from the endpoint URL.
4+
5+
```javascript
6+
import { DefaultAzureCredential, getBearerTokenProvider } from "@azure/identity";
7+
import { AzureOpenAI } from "openai";
8+
9+
// set environment variable AZURE_CLIENT_ID
10+
const credential = new DefaultAzureCredential();
11+
const scope = "https://cognitiveservices.azure.com/.default";
12+
const azureADTokenProvider = getBearerTokenProvider(credential, scope);
13+
14+
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
15+
const deployment = "<your Azure OpenAI deployment name>";
16+
const apiVersion = "2024-05-01-preview";
17+
const options = { azureADTokenProvider, deployment, apiVersion, endpoint }
18+
19+
const client = new AzureOpenAI(options);
20+
21+
```
22+
23+
Let's evaluate the impact of a leaked client ID versus a leaked API key.
24+
25+
An API key functions similarly to a regular password. If it's compromised, anyone with the key can access the resource. For Azure OpenAI, this means unrestricted use of AI models like GPT-4. If the network is publicly accessible, the security impact could be even greater.
26+
27+
Conversely, if the client ID is leaked, the risks are minimal. This is because the client ID alone can't establish a connection to Azure OpenAI. To utilize a Managed Identity, the service must be operating on Azure and even if Azure OpenAI is public, you can't connect from a local environment or across a network using an application.
28+
29+
Additionally, the following role assignment is configured for the Managed Identity:
30+
31+
```sh
32+
az role assignment create --assignee $USER_MANAGED_ID_PRINCIPAL_ID \
33+
--scope $OPEN_AI_RESOURCE_ID \
34+
--role "Cognitive Services OpenAI User"
35+
```
36+
37+
This configures the actions can be performed using this user ID (more on this later in the module). Here, the Cognitive Services OpenAI User role is assigned for Azure OpenAI services, limiting permissions to operations within Azure OpenAI.
38+
39+
In summary, compared to the ramifications of a leaked API key, exploiting a leaked client ID involves several steps, making it harder for malicious actors to exploit.
40+
For these reasons, Managed Identities offer a safer method to manage operations compared to API keys. It's recommended in the strongest possible terms that you use Managed Identity over API keys when authenticating to Azure OpenAI, or any other Azure service that supports Managed Identity.

0 commit comments

Comments
 (0)