33 * Licensed under the MIT License. See License.txt in the project root for license information.
44 *--------------------------------------------------------------------------------------------*/
55
6- import { KnownPrincipalType , type AuthorizationManagementClient , type RoleAssignment , type RoleAssignmentCreateParameters } from "@azure/arm-authorization" ;
7- import { uiUtils } from "@microsoft/vscode-azext-azureutils" ;
6+ import { KnownPrincipalType , type AuthorizationManagementClient , type RoleAssignmentCreateParameters } from "@azure/arm-authorization" ;
87import { AzureWizardExecuteStep , GenericParentTreeItem , GenericTreeItem , activityFailContext , activityFailIcon , activitySuccessContext , activitySuccessIcon , createUniversallyUniqueContextValue , nonNullValueAndProp , type ExecuteActivityOutput } from "@microsoft/vscode-azext-utils" ;
98import * as crypto from "crypto" ;
109import { type Progress } from "vscode" ;
1110import { createAuthorizationManagementClient } from "../../../utils/azureClients" ;
1211import { localize } from "../../../utils/localize" ;
12+ import { acrPullRoleId } from "./AcrPullVerifyStep" ;
1313import { type ManagedIdentityRegistryCredentialsContext } from "./ManagedIdentityRegistryCredentialsContext" ;
1414
15- const acrPullRoleId : string = '7f951dda-4ed3-4680-a7ca-43fe172d538d' ;
16-
1715export class AcrPullEnableStep extends AzureWizardExecuteStep < ManagedIdentityRegistryCredentialsContext > {
18- public priority : number = 460 ;
19-
20- // Add a configureBeforeExecute
16+ public priority : number = 461 ;
2117
2218 public async execute ( context : ManagedIdentityRegistryCredentialsContext , progress : Progress < { message ?: string | undefined ; increment ?: number | undefined } > ) : Promise < void > {
2319 const client : AuthorizationManagementClient = await createAuthorizationManagementClient ( context ) ;
24- const registryId : string = nonNullValueAndProp ( context . registry , 'id' ) ;
25- const managedEnvironmentIdentity : string = nonNullValueAndProp ( context . managedEnvironment ?. identity , 'principalId' ) ;
26-
27- if ( await this . hasAcrPullAssignment ( client , registryId , managedEnvironmentIdentity ) ) {
28- return ;
29- }
30-
3120 const roleCreateParams : RoleAssignmentCreateParameters = {
3221 description : 'acr pull' ,
3322 roleDefinitionId : `/providers/Microsoft.Authorization/roleDefinitions/${ acrPullRoleId } ` ,
3423 principalId : nonNullValueAndProp ( context . managedEnvironment ?. identity , 'principalId' ) ,
3524 principalType : KnownPrincipalType . ServicePrincipal ,
3625 } ;
3726
38- progress . report ( { message : localize ( 'updatingRegistryCredentials ' , 'Updating registry credentials ...' ) } ) ;
27+ progress . report ( { message : localize ( 'addingAcrPull ' , 'Adding ACR pull role ...' ) } ) ;
3928 await client . roleAssignments . create (
4029 nonNullValueAndProp ( context . registry , 'id' ) ,
4130 crypto . randomUUID ( ) ,
@@ -44,18 +33,7 @@ export class AcrPullEnableStep extends AzureWizardExecuteStep<ManagedIdentityReg
4433 }
4534
4635 public shouldExecute ( context : ManagedIdentityRegistryCredentialsContext ) : boolean {
47- return ! ! context . registry ;
48- }
49-
50- private async hasAcrPullAssignment ( client : AuthorizationManagementClient , registryId : string , managedEnvironmentIdentity : string ) : Promise < boolean > {
51- const roleAssignments : RoleAssignment [ ] = await uiUtils . listAllIterator ( client . roleAssignments . listForScope (
52- registryId ,
53- {
54- // $filter=principalId eq {id}
55- filter : `principalId eq '{${ managedEnvironmentIdentity } }'` ,
56- }
57- ) ) ;
58- return roleAssignments . some ( r => ! ! r . roleDefinitionId ?. endsWith ( acrPullRoleId ) ) ;
36+ return ! ! context . registry && ! context . hasAcrPullRole ;
5937 }
6038
6139 public createSuccessOutput ( ) : ExecuteActivityOutput {
0 commit comments