Skip to content

Commit f282e73

Browse files
authored
Merge pull request #10 from Azure-Samples/feature/keys
Feature/keys - enabling options to enable/disable JWT/Key-based authentication
2 parents 5a4a146 + 50cfcbe commit f282e73

16 files changed

Lines changed: 1135 additions & 166 deletions

File tree

infra/bicep/apimOaiApi.bicep

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ param apimInstanceName string
22
param oaiApiName string
33
param openAiServiceUrl string
44

5+
@description('Deploy the JWT-authenticated OpenAI API endpoint.')
6+
param enableJwt bool = true
7+
8+
@description('Deploy the subscription-key-authenticated OpenAI API endpoint.')
9+
param enableKeys bool = true
10+
511

612
resource apimInstance 'Microsoft.ApiManagement/service@2021-08-01' existing = {
713
name: apimInstanceName
@@ -18,12 +24,12 @@ resource openAiBackend 'Microsoft.ApiManagement/service/backends@2021-08-01' = {
1824
}
1925
}
2026

21-
resource apimOaiApi 'Microsoft.ApiManagement/service/apis@2021-08-01' = {
27+
resource apimJwtOaiApi 'Microsoft.ApiManagement/service/apis@2021-08-01' = if (enableJwt) {
2228
parent: apimInstance
23-
name: oaiApiName
29+
name: '${oaiApiName}-jwt'
2430
properties: {
2531
displayName: 'Azure OpenAI Service API'
26-
path: 'openai'
32+
path: 'jwt/openai'
2733
serviceUrl: openAiServiceUrl
2834
protocols: [
2935
'https'
@@ -36,8 +42,8 @@ resource apimOaiApi 'Microsoft.ApiManagement/service/apis@2021-08-01' = {
3642
var passthroughMethods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS']
3743

3844
@batchSize(1)
39-
resource apimOaiApiPassthrough 'Microsoft.ApiManagement/service/apis/operations@2021-08-01' = [for method in passthroughMethods: {
40-
parent: apimOaiApi
45+
resource apimJwtOaiApiPassthrough 'Microsoft.ApiManagement/service/apis/operations@2021-08-01' = [for method in enableJwt ? passthroughMethods : []: {
46+
parent: apimJwtOaiApi
4147
name: 'passthrough-${toLower(method)}'
4248
properties: {
4349
displayName: 'Passthrough ${method}'
@@ -46,3 +52,33 @@ resource apimOaiApiPassthrough 'Microsoft.ApiManagement/service/apis/operations@
4652
}
4753
}]
4854

55+
// Key-based API – subscription-key authenticated passthrough
56+
resource apimKeyOaiApi 'Microsoft.ApiManagement/service/apis@2021-08-01' = if (enableKeys) {
57+
parent: apimInstance
58+
name: '${oaiApiName}-keys'
59+
properties: {
60+
displayName: 'Azure OpenAI Key-Based API'
61+
path: 'keys/openai'
62+
serviceUrl: openAiServiceUrl
63+
protocols: [
64+
'https'
65+
]
66+
subscriptionRequired: true
67+
subscriptionKeyParameterNames: {
68+
header: 'api-key'
69+
query: 'api-key'
70+
}
71+
}
72+
}
73+
74+
@batchSize(1)
75+
resource apimKeyOaiApiPassthrough 'Microsoft.ApiManagement/service/apis/operations@2021-08-01' = [for method in enableKeys ? passthroughMethods : []: {
76+
parent: apimKeyOaiApi
77+
name: 'key-passthrough-${toLower(method)}'
78+
properties: {
79+
displayName: 'Key Passthrough ${method}'
80+
method: method
81+
urlTemplate: '/*'
82+
}
83+
}]
84+

infra/bicep/apimOaiApi.json

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
{
2+
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
3+
"contentVersion": "1.0.0.0",
4+
"metadata": {
5+
"_generator": {
6+
"name": "bicep",
7+
"version": "0.38.33.27573",
8+
"templateHash": "16240781146342215937"
9+
}
10+
},
11+
"parameters": {
12+
"apimInstanceName": {
13+
"type": "string"
14+
},
15+
"oaiApiName": {
16+
"type": "string"
17+
},
18+
"openAiServiceUrl": {
19+
"type": "string"
20+
},
21+
"enableJwt": {
22+
"type": "bool",
23+
"defaultValue": true,
24+
"metadata": {
25+
"description": "Deploy the JWT-authenticated OpenAI API endpoint."
26+
}
27+
},
28+
"enableKeys": {
29+
"type": "bool",
30+
"defaultValue": true,
31+
"metadata": {
32+
"description": "Deploy the subscription-key-authenticated OpenAI API endpoint."
33+
}
34+
}
35+
},
36+
"variables": {
37+
"passthroughMethods": [
38+
"GET",
39+
"POST",
40+
"PUT",
41+
"PATCH",
42+
"DELETE",
43+
"HEAD",
44+
"OPTIONS"
45+
]
46+
},
47+
"resources": [
48+
{
49+
"type": "Microsoft.ApiManagement/service/backends",
50+
"apiVersion": "2021-08-01",
51+
"name": "[format('{0}/{1}', parameters('apimInstanceName'), 'openAiBackend')]",
52+
"properties": {
53+
"url": "[parameters('openAiServiceUrl')]",
54+
"protocol": "http",
55+
"title": "OpenAI Backend",
56+
"description": "Backend for Azure OpenAI APIs"
57+
}
58+
},
59+
{
60+
"condition": "[parameters('enableJwt')]",
61+
"type": "Microsoft.ApiManagement/service/apis",
62+
"apiVersion": "2021-08-01",
63+
"name": "[format('{0}/{1}', parameters('apimInstanceName'), format('{0}-jwt', parameters('oaiApiName')))]",
64+
"properties": {
65+
"displayName": "Azure OpenAI Service API",
66+
"path": "jwt/openai",
67+
"serviceUrl": "[parameters('openAiServiceUrl')]",
68+
"protocols": [
69+
"https"
70+
],
71+
"subscriptionRequired": false
72+
}
73+
},
74+
{
75+
"copy": {
76+
"name": "apimJwtOaiApiPassthrough",
77+
"count": "[length(if(parameters('enableJwt'), variables('passthroughMethods'), createArray()))]",
78+
"mode": "serial",
79+
"batchSize": 1
80+
},
81+
"type": "Microsoft.ApiManagement/service/apis/operations",
82+
"apiVersion": "2021-08-01",
83+
"name": "[format('{0}/{1}/{2}', parameters('apimInstanceName'), format('{0}-jwt', parameters('oaiApiName')), format('passthrough-{0}', toLower(if(parameters('enableJwt'), variables('passthroughMethods'), createArray())[copyIndex()])))]",
84+
"properties": {
85+
"displayName": "[format('Passthrough {0}', if(parameters('enableJwt'), variables('passthroughMethods'), createArray())[copyIndex()])]",
86+
"method": "[if(parameters('enableJwt'), variables('passthroughMethods'), createArray())[copyIndex()]]",
87+
"urlTemplate": "/*"
88+
},
89+
"dependsOn": [
90+
"[resourceId('Microsoft.ApiManagement/service/apis', parameters('apimInstanceName'), format('{0}-jwt', parameters('oaiApiName')))]"
91+
]
92+
},
93+
{
94+
"condition": "[parameters('enableKeys')]",
95+
"type": "Microsoft.ApiManagement/service/apis",
96+
"apiVersion": "2021-08-01",
97+
"name": "[format('{0}/{1}', parameters('apimInstanceName'), format('{0}-keys', parameters('oaiApiName')))]",
98+
"properties": {
99+
"displayName": "Azure OpenAI Key-Based API",
100+
"path": "keys/openai",
101+
"serviceUrl": "[parameters('openAiServiceUrl')]",
102+
"protocols": [
103+
"https"
104+
],
105+
"subscriptionRequired": true
106+
}
107+
},
108+
{
109+
"copy": {
110+
"name": "apimKeyOaiApiPassthrough",
111+
"count": "[length(if(parameters('enableKeys'), variables('passthroughMethods'), createArray()))]",
112+
"mode": "serial",
113+
"batchSize": 1
114+
},
115+
"type": "Microsoft.ApiManagement/service/apis/operations",
116+
"apiVersion": "2021-08-01",
117+
"name": "[format('{0}/{1}/{2}', parameters('apimInstanceName'), format('{0}-keys', parameters('oaiApiName')), format('key-passthrough-{0}', toLower(if(parameters('enableKeys'), variables('passthroughMethods'), createArray())[copyIndex()])))]",
118+
"properties": {
119+
"displayName": "[format('Key Passthrough {0}', if(parameters('enableKeys'), variables('passthroughMethods'), createArray())[copyIndex()])]",
120+
"method": "[if(parameters('enableKeys'), variables('passthroughMethods'), createArray())[copyIndex()]]",
121+
"urlTemplate": "/*"
122+
},
123+
"dependsOn": [
124+
"[resourceId('Microsoft.ApiManagement/service/apis', parameters('apimInstanceName'), format('{0}-keys', parameters('oaiApiName')))]"
125+
]
126+
}
127+
]
128+
}

infra/bicep/main.bicep

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ param tags object = {
6060
Environment: 'Dev'
6161
}
6262

63+
@description('Deploy the JWT-authenticated OpenAI API endpoint.')
64+
param enableJwt bool = true
65+
66+
@description('Deploy the subscription-key-authenticated OpenAI API endpoint.')
67+
param enableKeys bool = true
68+
6369
var abbrs = loadJsonContent('./abbrs.json')
6470
var roles = loadJsonContent('./roles.json')
6571
var resourceToken = toLower(uniqueString(subscription().id, workloadName, location))
@@ -340,6 +346,8 @@ module apimOaiApi './apimOaiApi.bicep' = {
340346
apimInstanceName: apimInstanceName
341347
oaiApiName: oaiApiName
342348
openAiServiceUrl: openAiServiceUrl
349+
enableJwt: enableJwt
350+
enableKeys: enableKeys
343351
}
344352
}
345353

0 commit comments

Comments
 (0)