@@ -356,12 +356,14 @@ private AcsServicePrincipal BuildServicePrincipal(string name, string clientSecr
356
356
return new AcsServicePrincipal { SpId = app . AppId , ClientSecret = clientSecret , ObjectId = sp . Id } ;
357
357
}
358
358
359
- protected RoleAssignment GetRoleAssignmentWithRoleDefinitionId ( string roleDefinitionId )
359
+ protected RoleAssignment GetRoleAssignmentWithRoleDefinitionId ( string roleDefinitionId , string acrResourceId , string acsServicePrincipalObjectId )
360
360
{
361
361
RoleAssignment roleAssignment = null ;
362
362
var actionSuccess = RetryAction ( ( ) =>
363
363
{
364
- roleAssignment = AuthClient . RoleAssignments . List ( ) . Where ( x => x . Properties . RoleDefinitionId == roleDefinitionId && x . Name == Name ) . FirstOrDefault ( ) ;
364
+ roleAssignment = AuthClient . RoleAssignments . ListForScope ( acrResourceId )
365
+ . Where ( x => ( x . Properties . RoleDefinitionId == roleDefinitionId && ( x . Name == Name || x . Properties . PrincipalId == acsServicePrincipalObjectId ) ) )
366
+ . FirstOrDefault ( ) ;
365
367
} ) ;
366
368
if ( ! actionSuccess )
367
369
{
@@ -374,29 +376,33 @@ protected RoleAssignment GetRoleAssignmentWithRoleDefinitionId(string roleDefini
374
376
375
377
protected void AddAcrRoleAssignment ( string acrName , string acrParameterName , AcsServicePrincipal acsServicePrincipal )
376
378
{
377
- string acrResourceId = null ;
378
- try
379
- {
380
- //Find Acr resourceId first
381
- var acrQuery = new ODataQuery < GenericResourceFilter > ( $ "$filter=resourceType eq 'Microsoft.ContainerRegistry/registries' and name eq '{ acrName } '") ;
382
- var acrObjects = RmClient . Resources . List ( acrQuery ) ;
383
- acrResourceId = acrObjects . First ( ) . Id ;
384
- }
385
- catch ( Exception )
386
- {
387
- throw new AzPSArgumentException (
388
- string . Format ( Resources . CouldNotFindSpecifiedAcr , acrName ) ,
389
- acrParameterName ,
390
- string . Format ( Resources . CouldNotFindSpecifiedAcr , "*" ) ) ;
391
- }
379
+ string acrResourceId = getSpecifiedAcr ( acrName , acrParameterName ) ;
380
+
381
+ var roleDefinitionId = GetRoleId ( "acrpull" , acrResourceId ) ;
382
+ var spObjectId = getSPObjectId ( acsServicePrincipal ) ;
392
383
393
- var roleId = GetRoleId ( "acrpull" , acrResourceId ) ;
394
- RoleAssignment roleAssignment = GetRoleAssignmentWithRoleDefinitionId ( roleId ) ;
384
+ RoleAssignment roleAssignment = GetRoleAssignmentWithRoleDefinitionId ( roleDefinitionId , acrResourceId , spObjectId ) ;
395
385
if ( roleAssignment != null )
396
386
{
397
387
WriteWarning ( string . Format ( Resources . AcrRoleAssignmentIsAlreadyExist , acrResourceId ) ) ;
398
388
return ;
399
389
}
390
+
391
+ var success = RetryAction ( ( ) =>
392
+ AuthClient . RoleAssignments . Create ( acrResourceId , Guid . NewGuid ( ) . ToString ( ) , new RoleAssignmentCreateParameters ( )
393
+ {
394
+ Properties = new RoleAssignmentProperties ( roleDefinitionId , spObjectId )
395
+ } ) , Resources . AddRoleAssignment ) ;
396
+
397
+ if ( ! success )
398
+ {
399
+ throw new AzPSInvalidOperationException (
400
+ Resources . CouldNotAddAcrRoleAssignment ,
401
+ desensitizedMessage : Resources . CouldNotAddAcrRoleAssignment ) ;
402
+ }
403
+ }
404
+
405
+ protected string getSPObjectId ( AcsServicePrincipal acsServicePrincipal ) {
400
406
var spObjectId = acsServicePrincipal . ObjectId ;
401
407
if ( spObjectId == null )
402
408
{
@@ -414,17 +420,31 @@ protected void AddAcrRoleAssignment(string acrName, string acrParameterName, Acs
414
420
string . Format ( Resources . CouldNotFindObjectIdForServicePrincipal , "*" ) ) ;
415
421
}
416
422
}
417
- var success = RetryAction ( ( ) =>
418
- AuthClient . RoleAssignments . Create ( acrResourceId , Guid . NewGuid ( ) . ToString ( ) , new RoleAssignmentCreateParameters ( )
419
- {
420
- Properties = new RoleAssignmentProperties ( roleId , spObjectId )
421
- } ) , Resources . AddRoleAssignment ) ;
423
+ return spObjectId ;
424
+ }
422
425
423
- if ( ! success )
426
+ protected string getSpecifiedAcr ( string acrName , string acrParameterName ) {
427
+ try
424
428
{
425
- throw new AzPSInvalidOperationException (
426
- Resources . CouldNotAddAcrRoleAssignment ,
427
- desensitizedMessage : Resources . CouldNotAddAcrRoleAssignment ) ;
429
+ //Find Acr resourceId first
430
+ var acrQuery = new ODataQuery < GenericResourceFilter > ( $ "$filter=resourceType eq 'Microsoft.ContainerRegistry/registries' and name eq '{ acrName } '") ;
431
+ var acrObjects = RmClient . Resources . List ( acrQuery ) ;
432
+ while ( acrObjects . Count ( ) == 0 && acrObjects . NextPageLink != null )
433
+ {
434
+ acrObjects = RmClient . Resources . ListNext ( acrObjects . NextPageLink ) ;
435
+ }
436
+ if ( acrObjects . Count ( ) == 0 )
437
+ {
438
+ throw new AzPSArgumentException (
439
+ string . Format ( Resources . CouldNotFindSpecifiedAcr , acrName ) ,
440
+ acrParameterName ,
441
+ string . Format ( Resources . CouldNotFindSpecifiedAcr , "*" ) ) ;
442
+ }
443
+ return acrObjects . First ( ) . Id ;
444
+ }
445
+ catch ( Exception ex )
446
+ {
447
+ throw new AzPSArgumentException ( string . Format ( Resources . CouldNotFindSpecifiedAcr , acrName ) , ex , string . Format ( Resources . CouldNotFindSpecifiedAcr , "*" ) ) ;
428
448
}
429
449
}
430
450
0 commit comments