|
| 1 | +param storageAccountName string |
| 2 | +param appInsightsName string |
| 3 | +param managedIdentityPrincipalId string // Principal ID for the Managed Identity |
| 4 | +param userIdentityPrincipalId string = '' // Principal ID for the User Identity |
| 5 | +param allowUserIdentityPrincipal bool = false // Flag to enable user identity role assignments |
| 6 | +param enableBlob bool = true |
| 7 | +param enableQueue bool = false |
| 8 | +param enableTable bool = false |
| 9 | + |
| 10 | +// Define Role Definition IDs internally |
| 11 | +var storageRoleDefinitionId = 'b7e6dc6d-f1e8-4753-8033-0f276bb0955b' //Storage Blob Data Owner role |
| 12 | +var queueRoleDefinitionId = '974c5e8b-45b9-4653-ba55-5f855dd0fb88' // Storage Queue Data Contributor role |
| 13 | +var tableRoleDefinitionId = '0a9a7e1f-b9d0-4cc4-a60d-0319b160aaa3' // Storage Table Data Contributor role |
| 14 | +var monitoringRoleDefinitionId = '3913510d-42f4-4e42-8a64-420c390055eb' // Monitoring Metrics Publisher role ID |
| 15 | + |
| 16 | +resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' existing = { |
| 17 | + name: storageAccountName |
| 18 | +} |
| 19 | + |
| 20 | +resource applicationInsights 'Microsoft.Insights/components@2020-02-02' existing = { |
| 21 | + name: appInsightsName |
| 22 | +} |
| 23 | + |
| 24 | +// Role assignment for Storage Account (Blob) - Managed Identity |
| 25 | +resource storageRoleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = if (enableBlob) { |
| 26 | + name: guid(storageAccount.id, managedIdentityPrincipalId, storageRoleDefinitionId) // Use managed identity ID |
| 27 | + scope: storageAccount |
| 28 | + properties: { |
| 29 | + roleDefinitionId: resourceId('Microsoft.Authorization/roleDefinitions', storageRoleDefinitionId) |
| 30 | + principalId: managedIdentityPrincipalId // Use managed identity ID |
| 31 | + principalType: 'ServicePrincipal' // Managed Identity is a Service Principal |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// Role assignment for Storage Account (Blob) - User Identity |
| 36 | +resource storageRoleAssignment_User 'Microsoft.Authorization/roleAssignments@2022-04-01' = if (enableBlob && allowUserIdentityPrincipal && !empty(userIdentityPrincipalId)) { |
| 37 | + name: guid(storageAccount.id, userIdentityPrincipalId, storageRoleDefinitionId) |
| 38 | + scope: storageAccount |
| 39 | + properties: { |
| 40 | + roleDefinitionId: resourceId('Microsoft.Authorization/roleDefinitions', storageRoleDefinitionId) |
| 41 | + principalId: userIdentityPrincipalId // Use user identity ID |
| 42 | + principalType: 'User' // User Identity is a User Principal |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// Role assignment for Storage Account (Queue) - Managed Identity |
| 47 | +resource queueRoleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = if (enableQueue) { |
| 48 | + name: guid(storageAccount.id, managedIdentityPrincipalId, queueRoleDefinitionId) // Use managed identity ID |
| 49 | + scope: storageAccount |
| 50 | + properties: { |
| 51 | + roleDefinitionId: resourceId('Microsoft.Authorization/roleDefinitions', queueRoleDefinitionId) |
| 52 | + principalId: managedIdentityPrincipalId // Use managed identity ID |
| 53 | + principalType: 'ServicePrincipal' // Managed Identity is a Service Principal |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// Role assignment for Storage Account (Queue) - User Identity |
| 58 | +resource queueRoleAssignment_User 'Microsoft.Authorization/roleAssignments@2022-04-01' = if (enableQueue && allowUserIdentityPrincipal && !empty(userIdentityPrincipalId)) { |
| 59 | + name: guid(storageAccount.id, userIdentityPrincipalId, queueRoleDefinitionId) |
| 60 | + scope: storageAccount |
| 61 | + properties: { |
| 62 | + roleDefinitionId: resourceId('Microsoft.Authorization/roleDefinitions', queueRoleDefinitionId) |
| 63 | + principalId: userIdentityPrincipalId // Use user identity ID |
| 64 | + principalType: 'User' // User Identity is a User Principal |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// Role assignment for Storage Account (Table) - Managed Identity |
| 69 | +resource tableRoleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = if (enableTable) { |
| 70 | + name: guid(storageAccount.id, managedIdentityPrincipalId, tableRoleDefinitionId) // Use managed identity ID |
| 71 | + scope: storageAccount |
| 72 | + properties: { |
| 73 | + roleDefinitionId: resourceId('Microsoft.Authorization/roleDefinitions', tableRoleDefinitionId) |
| 74 | + principalId: managedIdentityPrincipalId // Use managed identity ID |
| 75 | + principalType: 'ServicePrincipal' // Managed Identity is a Service Principal |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// Role assignment for Storage Account (Table) - User Identity |
| 80 | +resource tableRoleAssignment_User 'Microsoft.Authorization/roleAssignments@2022-04-01' = if (enableTable && allowUserIdentityPrincipal && !empty(userIdentityPrincipalId)) { |
| 81 | + name: guid(storageAccount.id, userIdentityPrincipalId, tableRoleDefinitionId) |
| 82 | + scope: storageAccount |
| 83 | + properties: { |
| 84 | + roleDefinitionId: resourceId('Microsoft.Authorization/roleDefinitions', tableRoleDefinitionId) |
| 85 | + principalId: userIdentityPrincipalId // Use user identity ID |
| 86 | + principalType: 'User' // User Identity is a User Principal |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// Role assignment for Application Insights - Managed Identity |
| 91 | +resource appInsightsRoleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = { |
| 92 | + name: guid(applicationInsights.id, managedIdentityPrincipalId, monitoringRoleDefinitionId) // Use managed identity ID |
| 93 | + scope: applicationInsights |
| 94 | + properties: { |
| 95 | + roleDefinitionId: resourceId('Microsoft.Authorization/roleDefinitions', monitoringRoleDefinitionId) |
| 96 | + principalId: managedIdentityPrincipalId // Use managed identity ID |
| 97 | + principalType: 'ServicePrincipal' // Managed Identity is a Service Principal |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +// Role assignment for Application Insights - User Identity |
| 102 | +resource appInsightsRoleAssignment_User 'Microsoft.Authorization/roleAssignments@2022-04-01' = if (allowUserIdentityPrincipal && !empty(userIdentityPrincipalId)) { |
| 103 | + name: guid(applicationInsights.id, userIdentityPrincipalId, monitoringRoleDefinitionId) |
| 104 | + scope: applicationInsights |
| 105 | + properties: { |
| 106 | + roleDefinitionId: resourceId('Microsoft.Authorization/roleDefinitions', monitoringRoleDefinitionId) |
| 107 | + principalId: userIdentityPrincipalId // Use user identity ID |
| 108 | + principalType: 'User' // User Identity is a User Principal |
| 109 | + } |
| 110 | +} |
0 commit comments