如何将服务总线实体上的正确角色分配给使用Bicep的Azure功能管理身份?



我有一个Azure Functions项目,其中一个函数使用服务总线绑定(用于侦听订阅并发送到主题)。

Azure功能部署在托管身份下运行。由于我们希望使用Azure Bicep自动部署所有内容,因此我希望在Azure Bicep文件中自动为该托管身份在服务总线名称空间(或实体)上分配正确的角色。

但我似乎不知道该怎么做。有人能指出正确的二头肌片段,以便在服务总线实体上为特定的受管理身份创建角色分配Azure Service Bus Data ReceiverAzure Service Bus Data Sender吗?

(甚至更好的是:我知道我对二头肌很陌生,我怎么能自己发现呢)

问好

使用Bicep创建RBAC的文档可以在这里找到。
Azure内置角色可以在这里找到

对于ServiceBus和托管身份,可以创建如下的模块

// servicebus-role-assignment.bicep
param serviceBusName string
param principalId string
@allowed([
'4f6d3b9b-027b-4f4c-9142-0e5a2a2247e0' // Azure Service Bus Data Receiver
'69a216fc-b8fb-44d8-bc22-1f3c2cd27a39' // Azure Service Bus Data Sender
])
param roleId string

// Get a reference to servicebus namespace
resource servicebus 'Microsoft.ServiceBus/namespaces@2022-01-01-preview' existing = {
name: serviceBusName
}
// Grant permissions to the principalID to specific role to servicebus
resource roleAssignment 'Microsoft.Authorization/roleAssignments@2020-04-01-preview' = {
name: guid(servicebus.id, roleId, principalId)
scope: servicebus
properties: {
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', roleId)
principalId: principalId
principalType: 'ServicePrincipal'
}
}

如果您使用的是用户分配的身份,您可以在身份创建后调用此模块:

param location string = resourceGroup().location
param identityName string
param serviceBusName string
// Create the identity
resource identity 'Microsoft.ManagedIdentity/userAssignedIdentities@2022-01-31-preview' = {
name: identityName
location:location
}
// Do the role assignment
module serviceBusRoleAssignment 'servicebus-role-assignment.bicep' = {
name: 'servicebus-role-assignment'
params: {
serviceBusName: serviceBusName
roleId: '4f6d3b9b-027b-4f4c-9142-0e5a2a2247e0' // Azure Service Bus Data Receiver    
principalId: identity.properties.principalId
}
}

如果你使用的是系统分配的身份,你需要首先创建函数app:

param location string = resourceGroup().location
param functionAppName string
param serviceBusName string
...
// Create the function app
resource functionApp 'Microsoft.Web/sites@2022-03-01' = {
name: functionAppName
identity: {
type: 'SystemAssigned'
}
...
}
// Do the role assignment
module serviceBusRoleAssignment 'servicebus-role-assignment.bicep' = {
name: 'servicebus-role-assignment'
params: {
serviceBusName: serviceBusName
roleId: '4f6d3b9b-027b-4f4c-9142-0e5a2a2247e0' // Azure Service Bus Data Receiver    
principalId: functionApp.identity.principalId
}
}

最新更新