Skip to content

Commit 12c8e93

Browse files
New bicep and rolebicep
1 parent ffcfa86 commit 12c8e93

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

infra/main.bicep

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ param existingLogAnalyticsWorkspaceId string = ''
2020

2121
param azureopenaiVersion string = '2025-01-01-preview'
2222

23+
// ADD AFTER LINE 22 - Get the current deployer's information
24+
var deployerInfo = deployer()
25+
var deployingUserPrincipalId = deployerInfo.objectId
26+
var enableUserRoleAssignment = !empty(deployingUserPrincipalId)
27+
2328
// Restricting deployment to only supported Azure OpenAI regions validated with GPT-4o model
2429
@metadata({
2530
azd : {
@@ -812,6 +817,37 @@ module cogServiceRoleAssignmentsExisting './modules/role.bicep' = if(useExisting
812817
scope: resourceGroup( split(existingFoundryProjectResourceId, '/')[2], split(existingFoundryProjectResourceId, '/')[4])
813818
}
814819

820+
// ADD AFTER LINE 700 - User Role Assignment for Azure OpenAI
821+
// User Role Assignment for Azure OpenAI - New Resources
822+
module userOpenAiRoleAssignment './modules/role.bicep' = if (enableUserRoleAssignment && aiFoundryAIservicesEnabled && !useExistingResourceId) {
823+
name: take('user-openai-${uniqueString(deployingUserPrincipalId, aiFoundryAiServicesResourceName)}', 64)
824+
params: {
825+
name: 'user-openai-${uniqueString(deployingUserPrincipalId, aiFoundryAiServicesResourceName)}'
826+
principalId: deployingUserPrincipalId
827+
aiServiceName: aiFoundryAiServices.outputs.name
828+
principalType: 'User'
829+
}
830+
scope: resourceGroup(subscription().subscriptionId, resourceGroup().name)
831+
dependsOn: [
832+
aiFoundryAiServices
833+
]
834+
}
835+
836+
// User Role Assignment for Azure OpenAI - Existing Resources
837+
module userOpenAiRoleAssignmentExisting './modules/role.bicep' = if (enableUserRoleAssignment && aiFoundryAIservicesEnabled && useExistingResourceId) {
838+
name: take('user-openai-existing-${uniqueString(deployingUserPrincipalId, aiFoundryAiServicesResourceName)}', 64)
839+
params: {
840+
name: 'user-openai-existing-${uniqueString(deployingUserPrincipalId, aiFoundryAiServicesResourceName)}'
841+
principalId: deployingUserPrincipalId
842+
aiServiceName: aiFoundryAiServices.outputs.name
843+
principalType: 'User'
844+
}
845+
scope: resourceGroup(split(existingFoundryProjectResourceId, '/')[2], split(existingFoundryProjectResourceId, '/')[4])
846+
dependsOn: [
847+
aiFoundryAiServices
848+
]
849+
}
850+
815851
// ========== Cosmos DB ========== //
816852
// WAF best practices for Cosmos DB: https://learn.microsoft.com/en-us/azure/well-architected/service-guides/cosmos-db
817853
module privateDnsZonesCosmosDb 'br/public:avm/res/network/private-dns-zone:0.7.0' = if (virtualNetworkEnabled) {
@@ -886,9 +922,11 @@ module cosmosDb 'br/public:avm/res/document-db/database-account:0.12.0' = if (co
886922
capabilitiesToAdd: [
887923
'EnableServerless'
888924
]
889-
sqlRoleAssignmentsPrincipalIds: [
890-
containerApp.outputs.?systemAssignedMIPrincipalId
891-
]
925+
// REPLACE LINE 773
926+
sqlRoleAssignmentsPrincipalIds: concat(
927+
[containerApp.outputs.?systemAssignedMIPrincipalId],
928+
enableUserRoleAssignment ? [deployingUserPrincipalId] : []
929+
)
892930
sqlRoleDefinitions: [
893931
{
894932
// Replace this with built-in role definition Cosmos DB Built-in Data Contributor: https://docs.azure.cn/en-us/cosmos-db/nosql/security/reference-data-plane-roles#cosmos-db-built-in-data-contributor
@@ -1739,3 +1777,7 @@ output AZURE_AI_MODEL_DEPLOYMENT_NAME string = aiFoundryAiServicesModelDeploymen
17391777
output AZURE_AI_AGENT_MODEL_DEPLOYMENT_NAME string = aiFoundryAiServicesModelDeployment.name
17401778
output AZURE_AI_AGENT_ENDPOINT string = aiFoundryAiServices.outputs.aiProjectInfo.apiEndpoint
17411779
output APP_ENV string = 'Prod'
1780+
1781+
// ADD AFTER LINE 941
1782+
output deployerInfo object = deployerInfo
1783+
output userRoleAssignmentStatus string = enableUserRoleAssignment ? 'User ${deployingUserPrincipalId} (${deployerInfo.?userPrincipalName ?? 'N/A'}) has been granted access to Cosmos DB and Azure OpenAI' : 'No user role assignment configured.'

infra/modules/role.bicep

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ param principalId string
77
@description('The name of the existing Azure Cognitive Services account.')
88
param aiServiceName string
99

10+
// ADD THIS PARAMETER
11+
@allowed(['Device', 'ForeignGroup', 'Group', 'ServicePrincipal', 'User'])
12+
param principalType string = 'ServicePrincipal'
13+
1014
resource cognitiveServiceExisting 'Microsoft.CognitiveServices/accounts@2025-04-01-preview' existing = {
1115
name: aiServiceName
1216
}
@@ -29,7 +33,7 @@ resource aiUserAccessFoundry 'Microsoft.Authorization/roleAssignments@2022-04-01
2933
properties: {
3034
roleDefinitionId: aiUser.id
3135
principalId: principalId
32-
principalType: 'ServicePrincipal'
36+
principalType: principalType // ADD THIS
3337
}
3438
}
3539

@@ -39,7 +43,7 @@ resource aiDeveloperAccessFoundry 'Microsoft.Authorization/roleAssignments@2022-
3943
properties: {
4044
roleDefinitionId: aiDeveloper.id
4145
principalId: principalId
42-
principalType: 'ServicePrincipal'
46+
principalType: principalType // ADD THIS
4347
}
4448
}
4549

@@ -49,6 +53,6 @@ resource cognitiveServiceOpenAIUserAccessFoundry 'Microsoft.Authorization/roleAs
4953
properties: {
5054
roleDefinitionId: cognitiveServiceOpenAIUser.id
5155
principalId: principalId
52-
principalType: 'ServicePrincipal'
56+
principalType: principalType // ADD THIS
5357
}
5458
}

0 commit comments

Comments
 (0)