Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: CWYD | SFI Issue fix for Storage and Cosmos | rbac and keys deployment changes #4

Merged
merged 5 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions infra/app/function.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ module function '../core/host/functions.bicep' = {
runtimeName: runtimeName
runtimeVersion: runtimeVersion
dockerFullImageName: dockerFullImageName
useKeyVault: useKeyVault
appSettings: union(appSettings, {
WEBSITES_ENABLE_APP_SERVICE_STORAGE: 'false'
AZURE_AUTH_TYPE: authType
Expand Down
36 changes: 26 additions & 10 deletions infra/app/web.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,16 @@ module web '../core/host/appservice.bicep' = {
'2023-05-01'
).key1
AZURE_COSMOSDB_ACCOUNT_KEY: (useKeyVault || cosmosDBKeyName == '')
? cosmosDBKeyName
: listKeys(
resourceId(
subscription().subscriptionId,
resourceGroup().name,
'Microsoft.DocumentDB/databaseAccounts',
cosmosDBKeyName
),
'2022-08-15'
).primaryMasterKey
? cosmosDBKeyName
: listKeys(
resourceId(
subscription().subscriptionId,
resourceGroup().name,
'Microsoft.DocumentDB/databaseAccounts',
cosmosDBKeyName
),
'2022-08-15'
).primaryMasterKey
})
keyVaultName: keyVaultName
runtimeName: runtimeName
Expand Down Expand Up @@ -192,6 +192,22 @@ module webaccess '../core/security/keyvault-access.bicep' = if (useKeyVault) {
}
}

resource cosmosRoleDefinition 'Microsoft.DocumentDB/databaseAccounts/sqlRoleDefinitions@2024-05-15' existing = {
name: '${json(appSettings.AZURE_COSMOSDB_INFO).accountName}/00000000-0000-0000-0000-000000000002'
}

module cosmosUserRole '../core/database/cosmos-sql-role-assign.bicep' = {
name: 'cosmos-sql-user-role-${web.name}'
params: {
accountName: json(appSettings.AZURE_COSMOSDB_INFO).accountName
roleDefinitionId: cosmosRoleDefinition.id
principalId: web.outputs.identityPrincipalId
}
dependsOn: [
cosmosRoleDefinition
]
}

output FRONTEND_API_IDENTITY_PRINCIPAL_ID string = web.outputs.identityPrincipalId
output FRONTEND_API_NAME string = web.outputs.name
output FRONTEND_API_URI string = web.outputs.uri
19 changes: 19 additions & 0 deletions infra/core/database/cosmos-sql-role-assign.bicep
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
metadata description = 'Creates a SQL role assignment under an Azure Cosmos DB account.'
param accountName string

param roleDefinitionId string
param principalId string = ''

resource role 'Microsoft.DocumentDB/databaseAccounts/sqlRoleAssignments@2022-05-15' = {
parent: cosmos
name: guid(roleDefinitionId, principalId, cosmos.id)
properties: {
principalId: principalId
roleDefinitionId: roleDefinitionId
scope: cosmos.id
}
}

resource cosmos 'Microsoft.DocumentDB/databaseAccounts@2022-08-15' existing = {
name: accountName
}
18 changes: 16 additions & 2 deletions infra/core/host/functions.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ param appServicePlanId string
param keyVaultName string = ''
param managedIdentity bool = !empty(keyVaultName)
param storageAccountName string
param useKeyVault bool

// Runtime Properties
@allowed([
Expand Down Expand Up @@ -67,10 +68,14 @@ module functions 'appservice.bicep' = {
appSettings: union(
appSettings,
{
AzureWebJobsStorage: 'DefaultEndpointsProtocol=https;AccountName=${storage.name};AccountKey=${storage.listKeys().keys[0].value};EndpointSuffix=${environment().suffixes.storage}'
FUNCTIONS_EXTENSION_VERSION: extensionVersion
},
!useDocker ? { FUNCTIONS_WORKER_RUNTIME: runtimeName } : {}
!useDocker ? { FUNCTIONS_WORKER_RUNTIME: runtimeName } : {},
useKeyVault
? {
AzureWebJobsStorage: 'DefaultEndpointsProtocol=https;AccountName=${storage.name};AccountKey=${storage.listKeys().keys[0].value};EndpointSuffix=${environment().suffixes.storage}'
}
: { AzureWebJobsStorage__accountName: storage.name }
)
clientAffinityEnabled: clientAffinityEnabled
enableOryxBuild: enableOryxBuild
Expand All @@ -90,6 +95,15 @@ module functions 'appservice.bicep' = {
}
}

module storageBlobRoleFunction '../security/role.bicep' = {
name: 'storage-blob-role-function'
params: {
principalId: functions.outputs.identityPrincipalId
roleDefinitionId: 'ba92f5b4-2d11-453d-a403-e96b0029c9fe'
principalType: 'ServicePrincipal'
}
}

resource storage 'Microsoft.Storage/storageAccounts@2021-09-01' existing = {
name: storageAccountName
}
Expand Down
3 changes: 2 additions & 1 deletion infra/core/storage/storage-account.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ param tags object = {}
param accessTier string = 'Hot'
param allowBlobPublicAccess bool = false
param allowCrossTenantReplication bool = true
param allowSharedKeyAccess bool = true
param useKeyVault bool
param allowSharedKeyAccess bool = useKeyVault
param containers array = []
param defaultToOAuthAuthentication bool = false
param deleteRetentionPolicy object = {}
Expand Down
9 changes: 5 additions & 4 deletions infra/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,7 @@ module storage 'core/storage/storage-account.bicep' = {
params: {
name: storageAccountName
location: location
useKeyVault: useKeyVault
sku: {
name: 'Standard_GRS'
}
Expand Down Expand Up @@ -1086,7 +1087,7 @@ module storage 'core/storage/storage-account.bicep' = {

// USER ROLES
// Storage Blob Data Contributor
module storageRoleUser 'core/security/role.bicep' = if (authType == 'rbac') {
module storageRoleUser 'core/security/role.bicep' = if (authType == 'rbac' && principalId != '') {
scope: resourceGroup()
name: 'storage-role-user'
params: {
Expand All @@ -1097,7 +1098,7 @@ module storageRoleUser 'core/security/role.bicep' = if (authType == 'rbac') {
}

// Cognitive Services User
module openaiRoleUser 'core/security/role.bicep' = if (authType == 'rbac') {
module openaiRoleUser 'core/security/role.bicep' = if (authType == 'rbac' && principalId != '') {
scope: resourceGroup()
name: 'openai-role-user'
params: {
Expand All @@ -1108,7 +1109,7 @@ module openaiRoleUser 'core/security/role.bicep' = if (authType == 'rbac') {
}

// Contributor
module openaiRoleUserContributor 'core/security/role.bicep' = if (authType == 'rbac') {
module openaiRoleUserContributor 'core/security/role.bicep' = if (authType == 'rbac' && principalId != '') {
scope: resourceGroup()
name: 'openai-role-user-contributor'
params: {
Expand All @@ -1119,7 +1120,7 @@ module openaiRoleUserContributor 'core/security/role.bicep' = if (authType == 'r
}

// Search Index Data Contributor
module searchRoleUser 'core/security/role.bicep' = if (authType == 'rbac') {
module searchRoleUser 'core/security/role.bicep' = if (authType == 'rbac' && principalId != '') {
scope: resourceGroup()
name: 'search-role-user'
params: {
Expand Down
Loading
Loading