@@ -40,6 +40,8 @@ param storageSkuName string // Set in main.parameters.json
40
40
param userStorageAccountName string = ''
41
41
param userStorageContainerName string = 'user-content'
42
42
43
+ param tokenStorageContainerName string = 'tokens'
44
+
43
45
param appServiceSkuName string // Set in main.parameters.json
44
46
45
47
@allowed (['azure' , 'openai' , 'azure_custom' ])
@@ -248,6 +250,16 @@ param containerRegistryName string = deploymentTarget == 'containerapps'
248
250
? '${replace (toLower (environmentName ), '-' , '' )}acr'
249
251
: ''
250
252
253
+ // Configure CORS for allowing different web apps to use the backend
254
+ // For more information please see https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
255
+ var msftAllowedOrigins = [ 'https://portal.azure.com' , 'https://ms.portal.azure.com' ]
256
+ var loginEndpoint = environment ().authentication .loginEndpoint
257
+ var loginEndpointFixed = lastIndexOf (loginEndpoint , '/' ) == length (loginEndpoint ) - 1 ? substring (loginEndpoint , 0 , length (loginEndpoint ) - 1 ) : loginEndpoint
258
+ var allMsftAllowedOrigins = !(empty (clientAppId )) ? union (msftAllowedOrigins , [ loginEndpointFixed ]) : msftAllowedOrigins
259
+ var allowedOrigins = union (split (allowedOrigin , ';' ), allMsftAllowedOrigins )
260
+ // Filter out any empty origin strings and remove any duplicate origins
261
+ var allowedOriginsEnv = join (reduce (filter (allowedOrigins , o => length (trim (o )) > 0 ), [], (cur , next ) => union (cur , [next ])), ';' )
262
+
251
263
// Organize resources in a resource group
252
264
resource resourceGroup 'Microsoft.Resources/resourceGroups@2021-04-01' = {
253
265
name : !empty (resourceGroupName ) ? resourceGroupName : '${abbrs .resourcesResourceGroups }${environmentName }'
@@ -376,14 +388,12 @@ var appEnvVariables = {
376
388
AZURE_ENABLE_GLOBAL_DOCUMENT_ACCESS : enableGlobalDocuments
377
389
AZURE_ENABLE_UNAUTHENTICATED_ACCESS : enableUnauthenticatedAccess
378
390
AZURE_SERVER_APP_ID : serverAppId
379
- AZURE_SERVER_APP_SECRET : serverAppSecret
380
391
AZURE_CLIENT_APP_ID : clientAppId
381
- AZURE_CLIENT_APP_SECRET : clientAppSecret
382
392
AZURE_TENANT_ID : tenantId
383
393
AZURE_AUTH_TENANT_ID : tenantIdForAuth
384
394
AZURE_AUTHENTICATION_ISSUER_URI : authenticationIssuerUri
385
395
// CORS support, for frontends on other hosts
386
- ALLOWED_ORIGIN : allowedOrigin
396
+ ALLOWED_ORIGIN : allowedOriginsEnv
387
397
USE_VECTORS : useVectors
388
398
USE_GPT4V : useGPT4V
389
399
USE_USER_UPLOAD : useUserUpload
@@ -412,7 +422,7 @@ module backend 'core/host/appservice.bicep' = if (deploymentTarget == 'appservic
412
422
managedIdentity : true
413
423
virtualNetworkSubnetId : isolation .outputs .appSubnetId
414
424
publicNetworkAccess : publicNetworkAccess
415
- allowedOrigins : [ allowedOrigin ]
425
+ allowedOrigins : allowedOrigins
416
426
clientAppId : clientAppId
417
427
serverAppId : serverAppId
418
428
enableUnauthenticatedAccess : enableUnauthenticatedAccess
@@ -421,7 +431,10 @@ module backend 'core/host/appservice.bicep' = if (deploymentTarget == 'appservic
421
431
authenticationIssuerUri : authenticationIssuerUri
422
432
use32BitWorkerProcess : appServiceSkuName == 'F1'
423
433
alwaysOn : appServiceSkuName != 'F1'
424
- appSettings : appEnvVariables
434
+ appSettings : union (appEnvVariables , {
435
+ AZURE_SERVER_APP_SECRET : serverAppSecret
436
+ AZURE_CLIENT_APP_SECRET : clientAppSecret
437
+ })
425
438
}
426
439
}
427
440
@@ -472,11 +485,40 @@ module acaBackend 'core/host/container-app-upsert.bicep' = if (deploymentTarget
472
485
targetPort : 8000
473
486
containerCpuCoreCount : '1.0'
474
487
containerMemory : '2Gi'
475
- allowedOrigins : [ allowedOrigin ]
488
+ allowedOrigins : allowedOrigins
476
489
env : union (appEnvVariables , {
477
490
// For using managed identity to access Azure resources. See https://github.com/microsoft/azure-container-apps/issues/442
478
491
AZURE_CLIENT_ID : (deploymentTarget == 'containerapps' ) ? acaIdentity .outputs .clientId : ''
479
492
})
493
+ secrets : useAuthentication ? {
494
+ azureclientappsecret : clientAppSecret
495
+ azureserverappsecret : serverAppSecret
496
+ } : {}
497
+ envSecrets : useAuthentication ? [
498
+ {
499
+ name : 'AZURE_CLIENT_APP_SECRET'
500
+ secretRef : 'azureclientappsecret'
501
+ }
502
+ {
503
+ name : 'AZURE_SERVER_APP_SECRET'
504
+ secretRef : 'azureserverappsecret'
505
+ }
506
+ ] : []
507
+ }
508
+ }
509
+
510
+ module acaAuth 'core/host/container-apps-auth.bicep' = if (deploymentTarget == 'containerapps' && !empty (clientAppId )) {
511
+ name : 'aca-auth'
512
+ scope : resourceGroup
513
+ params : {
514
+ name : acaBackend .outputs .name
515
+ clientAppId : clientAppId
516
+ serverAppId : serverAppId
517
+ clientSecretSettingName : !empty (clientAppSecret ) ? 'azureclientappsecret' : ''
518
+ authenticationIssuerUri : authenticationIssuerUri
519
+ enableUnauthenticatedAccess : enableUnauthenticatedAccess
520
+ blobContainerUri : 'https://${storageAccountName }.blob.${environment ().suffixes .storage }/${tokenStorageContainerName }'
521
+ appIdentityResourceId : (deploymentTarget == 'appservice' ) ? '' : acaBackend .outputs .identityResourceId
480
522
}
481
523
}
482
524
@@ -661,6 +703,10 @@ module storage 'core/storage/storage-account.bicep' = {
661
703
name : storageContainerName
662
704
publicAccess : 'None'
663
705
}
706
+ {
707
+ name : tokenStorageContainerName
708
+ publicAccess : 'None'
709
+ }
664
710
]
665
711
}
666
712
}
0 commit comments