Skip to content

Commit 3460aca

Browse files
authored
Merge pull request #580 from Portkey-AI/feat/azure_entraid_integration
feat: azure entra and managed identity integration
2 parents fa25367 + ad35209 commit 3460aca

File tree

4 files changed

+118
-2
lines changed

4 files changed

+118
-2
lines changed

src/handlers/handlerUtils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,13 @@ export function constructConfigFromRequestHeaders(
10131013
resourceName: requestHeaders[`x-${POWERED_BY}-azure-resource-name`],
10141014
deploymentId: requestHeaders[`x-${POWERED_BY}-azure-deployment-id`],
10151015
apiVersion: requestHeaders[`x-${POWERED_BY}-azure-api-version`],
1016+
azureAuthMode: requestHeaders[`x-${POWERED_BY}-azure-auth-mode`],
1017+
azureManagedClientId:
1018+
requestHeaders[`x-${POWERED_BY}-azure-managed-client-id`],
1019+
azureEntraClientId: requestHeaders[`x-${POWERED_BY}-azure-entra-client-id`],
1020+
azureEntraClientSecret:
1021+
requestHeaders[`x-${POWERED_BY}-azure-entra-client-secret`],
1022+
azureEntraTenantId: requestHeaders[`x-${POWERED_BY}-azure-entra-tenant-id`],
10161023
azureModelName: requestHeaders[`x-${POWERED_BY}-azure-model-name`],
10171024
};
10181025

src/providers/azure-openai/api.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,46 @@
11
import { ProviderAPIConfig } from '../types';
2+
import {
3+
getAccessTokenFromEntraId,
4+
getAzureManagedIdentityToken,
5+
} from './utils';
26

37
const AzureOpenAIAPIConfig: ProviderAPIConfig = {
48
getBaseURL: ({ providerOptions }) => {
59
const { resourceName, deploymentId } = providerOptions;
610
return `https://${resourceName}.openai.azure.com/openai/deployments/${deploymentId}`;
711
},
8-
headers: ({ providerOptions, fn }) => {
12+
headers: async ({ providerOptions, fn }) => {
13+
const { apiKey, azureAuthMode } = providerOptions;
14+
15+
if (azureAuthMode === 'entra') {
16+
const { azureEntraTenantId, azureEntraClientId, azureEntraClientSecret } =
17+
providerOptions;
18+
if (azureEntraTenantId && azureEntraClientId && azureEntraClientSecret) {
19+
const scope = 'https://cognitiveservices.azure.com/.default';
20+
const accessToken = await getAccessTokenFromEntraId(
21+
azureEntraTenantId,
22+
azureEntraClientId,
23+
azureEntraClientSecret,
24+
scope
25+
);
26+
return {
27+
Authorization: `Bearer ${accessToken}`,
28+
};
29+
}
30+
}
31+
if (azureAuthMode === 'managed') {
32+
const { azureManagedClientId } = providerOptions;
33+
const resource = 'https://cognitiveservices.azure.com/';
34+
const accessToken = await getAzureManagedIdentityToken(
35+
resource,
36+
azureManagedClientId
37+
);
38+
return {
39+
Authorization: `Bearer ${accessToken}`,
40+
};
41+
}
942
const headersObj: Record<string, string> = {
10-
'api-key': `${providerOptions.apiKey}`,
43+
'api-key': `${apiKey}`,
1144
};
1245
if (fn === 'createTranscription' || fn === 'createTranslation')
1346
headersObj['Content-Type'] = 'multipart/form-data';
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
export async function getAccessTokenFromEntraId(
2+
tenantId: string,
3+
clientId: string,
4+
clientSecret: string,
5+
scope = 'https://cognitiveservices.azure.com/.default'
6+
) {
7+
try {
8+
const url = `https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token`;
9+
const params = new URLSearchParams({
10+
client_id: clientId,
11+
client_secret: clientSecret,
12+
scope: scope,
13+
grant_type: 'client_credentials',
14+
});
15+
16+
const response = await fetch(url, {
17+
method: 'POST',
18+
headers: {
19+
'Content-Type': 'application/x-www-form-urlencoded',
20+
},
21+
body: params,
22+
});
23+
24+
if (!response.ok) {
25+
const errorMessage = await response.text();
26+
console.log({ message: `Error from Entra ${errorMessage}` });
27+
return undefined;
28+
}
29+
const data: { access_token: string } = await response.json();
30+
return data.access_token;
31+
} catch (error) {
32+
console.log(error);
33+
}
34+
}
35+
36+
export async function getAzureManagedIdentityToken(
37+
resource: string,
38+
clientId?: string
39+
) {
40+
try {
41+
const response = await fetch(
42+
`http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=${encodeURIComponent(resource)}${clientId ? `&client_id=${encodeURIComponent(clientId)}` : ''}`,
43+
{
44+
method: 'GET',
45+
headers: {
46+
Metadata: 'true',
47+
},
48+
}
49+
);
50+
if (!response.ok) {
51+
const errorMessage = await response.text();
52+
console.log({ message: `Error from Managed ${errorMessage}` });
53+
return undefined;
54+
}
55+
const data: { access_token: string } = await response.json();
56+
return data.access_token;
57+
} catch (error) {
58+
console.log({ error });
59+
}
60+
}

src/types/requestBody.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ export interface Options {
6060
apiVersion?: string;
6161
adAuth?: string;
6262
azureModelName?: string;
63+
azureAuthMode?: string; // can be entra or managed
64+
azureManagedClientId?: string;
65+
azureEntraClientId?: string;
66+
azureEntraClientSecret?: string;
67+
azureEntraTenantId?: string;
6368
/** Workers AI specific */
6469
workersAiAccountId?: string;
6570
/** The parameter to set custom base url */
@@ -143,6 +148,12 @@ export interface Targets {
143148
deploymentId?: string;
144149
apiVersion?: string;
145150
adAuth?: string;
151+
azureAuthMode?: string;
152+
azureManagedClientId?: string;
153+
azureEntraClientId?: string;
154+
azureEntraClientSecret?: string;
155+
azureEntraTenantId?: string;
156+
azureModelName?: string;
146157
/** provider option index picked based on weight in loadbalance mode */
147158
index?: number;
148159
cache?: CacheSettings | string;
@@ -356,6 +367,11 @@ export interface ShortConfig {
356367
azureModelName?: string;
357368
workersAiAccountId?: string;
358369
apiVersion?: string;
370+
azureAuthMode?: string;
371+
azureManagedClientId?: string;
372+
azureEntraClientId?: string;
373+
azureEntraClientSecret?: string;
374+
azureEntraTenantId?: string;
359375
customHost?: string;
360376
// Google Vertex AI specific
361377
vertexRegion?: string;

0 commit comments

Comments
 (0)