1313using System . Collections . Generic ;
1414using Azure . Provisioning ;
1515using Azure . Provisioning . CloudMachine ;
16+ using Azure . Core ;
17+ using System . Runtime . CompilerServices ;
1618
1719namespace Azure . CloudMachine ;
1820
@@ -221,7 +223,8 @@ public void AddFeature(CloudMachineFeature feature)
221223 public void AddEndpoints < T > ( )
222224 {
223225 Type endpointsType = typeof ( T ) ;
224- if ( ! endpointsType . IsInterface ) throw new InvalidOperationException ( "Endpoints type must be an interface." ) ;
226+ if ( ! endpointsType . IsInterface )
227+ throw new InvalidOperationException ( "Endpoints type must be an interface." ) ;
225228 Endpoints . Add ( endpointsType ) ;
226229 }
227230
@@ -242,33 +245,35 @@ public ProvisioningPlan Build(ProvisioningBuildOptions? context = null)
242245 //Add(PrincipalTypeParameter);
243246 //Add(PrincipalNameParameter);
244247
248+ var storageBlobDataContributor = StorageBuiltInRole . StorageBlobDataContributor ;
249+ var storageTableDataContributor = StorageBuiltInRole . StorageTableDataContributor ;
250+ var azureServiceBusDataSender = ServiceBusBuiltInRole . AzureServiceBusDataSender ;
251+ var azureServiceBusDataOwner = ServiceBusBuiltInRole . AzureServiceBusDataOwner ;
252+
245253 _infrastructure . Add ( Identity ) ;
246254 _infrastructure . Add ( _storage ) ;
247- _infrastructure . Add ( _storage . CreateRoleAssignment ( StorageBuiltInRole . StorageBlobDataContributor , RoleManagementPrincipalType . User , PrincipalIdParameter ) ) ;
248- _infrastructure . Add ( _storage . CreateRoleAssignment ( StorageBuiltInRole . StorageTableDataContributor , RoleManagementPrincipalType . User , PrincipalIdParameter ) ) ;
255+ _infrastructure . Add ( _storage . CreateRoleAssignment ( storageBlobDataContributor , RoleManagementPrincipalType . User , PrincipalIdParameter ) ) ;
256+ _infrastructure . Add ( CreateRoleAssignment ( _storage , _storage . Id , storageBlobDataContributor , Identity ) ) ;
257+ _infrastructure . Add ( _storage . CreateRoleAssignment ( storageTableDataContributor , RoleManagementPrincipalType . User , PrincipalIdParameter ) ) ;
258+ _infrastructure . Add ( CreateRoleAssignment ( _storage , _storage . Id , storageTableDataContributor , Identity ) ) ;
249259 _infrastructure . Add ( _container ) ;
250260 _infrastructure . Add ( _blobs ) ;
251261 _infrastructure . Add ( _serviceBusNamespace ) ;
252- _infrastructure . Add ( _serviceBusNamespace . CreateRoleAssignment ( ServiceBusBuiltInRole . AzureServiceBusDataOwner , RoleManagementPrincipalType . User , PrincipalIdParameter ) ) ;
262+ _infrastructure . Add ( _serviceBusNamespace . CreateRoleAssignment ( azureServiceBusDataOwner , RoleManagementPrincipalType . User , PrincipalIdParameter ) ) ;
263+ _infrastructure . Add ( CreateRoleAssignment ( _serviceBusNamespace , _serviceBusNamespace . Id , azureServiceBusDataOwner , Identity ) ) ;
253264 _infrastructure . Add ( _serviceBusNamespaceAuthorizationRule ) ;
254265 _infrastructure . Add ( _serviceBusTopic_private ) ;
255266 _infrastructure . Add ( _serviceBusTopic_default ) ;
256267 _infrastructure . Add ( _serviceBusSubscription_private ) ;
257268 _infrastructure . Add ( _serviceBusSubscription_default ) ;
258269
259- // This is necessary until SystemTopic adds an AssignRole method.
260- var role = ServiceBusBuiltInRole . AzureServiceBusDataSender ;
261- RoleAssignment roleAssignment = new RoleAssignment ( "cm_servicebus_role" ) ;
262- roleAssignment . Name = BicepFunction . CreateGuid ( _serviceBusNamespace . Id , Identity . Id , BicepFunction . GetSubscriptionResourceId ( "Microsoft.Authorization/roleDefinitions" , role . ToString ( ) ) ) ;
263- roleAssignment . Scope = new IdentifierExpression ( _serviceBusNamespace . BicepIdentifier ) ;
264- roleAssignment . PrincipalType = RoleManagementPrincipalType . ServicePrincipal ;
265- roleAssignment . RoleDefinitionId = BicepFunction . GetSubscriptionResourceId ( "Microsoft.Authorization/roleDefinitions" , role . ToString ( ) ) ;
266- roleAssignment . PrincipalId = Identity . PrincipalId ;
270+ RoleAssignment roleAssignment = CreateRoleAssignment ( _serviceBusNamespace , _serviceBusNamespace . Id , azureServiceBusDataSender , Identity ) ;
267271 _infrastructure . Add ( roleAssignment ) ;
272+
273+ CreateRoleAssignment ( _serviceBusNamespace , _serviceBusNamespace . Id , azureServiceBusDataSender , Identity ) ;
268274 // the role assignment must exist before the system topic event subscription is created.
269275 _eventGridSubscription_blobs . DependsOn . Add ( roleAssignment ) ;
270276 _infrastructure . Add ( _eventGridSubscription_blobs ) ;
271-
272277 _infrastructure . Add ( _eventGridTopic_blobs ) ;
273278
274279 // Placeholders for now.
@@ -283,4 +288,21 @@ public ProvisioningPlan Build(ProvisioningBuildOptions? context = null)
283288
284289 return _infrastructure . Build ( context ) ;
285290 }
291+
292+ // Temporary until the bug is fixed in the CDK generator which uses the PrincipalId instead of the Id in BicepFunction.CreateGuid.
293+ internal RoleAssignment CreateRoleAssignment ( ProvisionableResource resource , BicepValue < ResourceIdentifier > Id , object role , UserAssignedIdentity identity )
294+ {
295+ if ( role is null ) throw new ArgumentException ( "Role must not be null." , nameof ( role ) ) ;
296+ var method = role . GetType ( ) . GetMethod ( "GetBuiltInRoleName" , System . Reflection . BindingFlags . Static | System . Reflection . BindingFlags . Public ) ;
297+ string roleName = ( string ) method ! . Invoke ( null , [ role ] ) ! ;
298+
299+ return new ( $ "{ resource . BicepIdentifier } _{ identity . BicepIdentifier } _{ roleName } ")
300+ {
301+ Name = BicepFunction . CreateGuid ( Id , identity . Id , BicepFunction . GetSubscriptionResourceId ( "Microsoft.Authorization/roleDefinitions" , role ! . ToString ( ) ! ) ) ,
302+ Scope = new IdentifierExpression ( resource . BicepIdentifier ) ,
303+ PrincipalType = RoleManagementPrincipalType . ServicePrincipal ,
304+ RoleDefinitionId = BicepFunction . GetSubscriptionResourceId ( "Microsoft.Authorization/roleDefinitions" , role . ToString ( ) ! ) ,
305+ PrincipalId = identity . PrincipalId
306+ } ;
307+ }
286308}
0 commit comments