|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { KnownPrincipalType, type AuthorizationManagementClient, type RoleAssignment, type RoleAssignmentCreateParameters } from "@azure/arm-authorization"; |
| 7 | +import { uiUtils } from "@microsoft/vscode-azext-azureutils"; |
| 8 | +import { AzureWizardExecuteStep, GenericParentTreeItem, GenericTreeItem, activityFailContext, activityFailIcon, activitySuccessContext, activitySuccessIcon, createUniversallyUniqueContextValue, nonNullValueAndProp, type ExecuteActivityOutput } from "@microsoft/vscode-azext-utils"; |
| 9 | +import * as crypto from "crypto"; |
| 10 | +import { type Progress } from "vscode"; |
| 11 | +import { createAuthorizationManagementClient } from "../../../utils/azureClients"; |
| 12 | +import { localize } from "../../../utils/localize"; |
| 13 | +import { type ManagedIdentityRegistryCredentialsContext } from "./ManagedIdentityRegistryCredentialsContext"; |
| 14 | + |
| 15 | +const acrPullRoleId: string = '7f951dda-4ed3-4680-a7ca-43fe172d538d'; |
| 16 | + |
| 17 | +export class AcrPullEnableStep extends AzureWizardExecuteStep<ManagedIdentityRegistryCredentialsContext> { |
| 18 | + public priority: number = 460; |
| 19 | + |
| 20 | + // Add a configureBeforeExecute |
| 21 | + |
| 22 | + public async execute(context: ManagedIdentityRegistryCredentialsContext, progress: Progress<{ message?: string | undefined; increment?: number | undefined }>): Promise<void> { |
| 23 | + 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 | + |
| 31 | + const roleCreateParams: RoleAssignmentCreateParameters = { |
| 32 | + description: 'acr pull', |
| 33 | + roleDefinitionId: `/providers/Microsoft.Authorization/roleDefinitions/${acrPullRoleId}`, |
| 34 | + principalId: nonNullValueAndProp(context.managedEnvironment?.identity, 'principalId'), |
| 35 | + principalType: KnownPrincipalType.ServicePrincipal, |
| 36 | + }; |
| 37 | + |
| 38 | + progress.report({ message: localize('updatingRegistryCredentials', 'Updating registry credentials...') }); |
| 39 | + await client.roleAssignments.create( |
| 40 | + nonNullValueAndProp(context.registry, 'id'), |
| 41 | + crypto.randomUUID(), |
| 42 | + roleCreateParams, |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + 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)); |
| 59 | + } |
| 60 | + |
| 61 | + public createSuccessOutput(): ExecuteActivityOutput { |
| 62 | + return { |
| 63 | + item: new GenericTreeItem(undefined, { |
| 64 | + contextValue: createUniversallyUniqueContextValue(['containerRegistryAcrPullEnableStepSuccessItem', activitySuccessContext]), |
| 65 | + label: localize('enableAcrPull', 'Grant "{0}" access to container environment identity', 'acrPull'), |
| 66 | + iconPath: activitySuccessIcon |
| 67 | + }), |
| 68 | + message: localize('enableAcrPullSuccess', 'Successfully granted "{0}" access to container environment identity.', 'acrPull'), |
| 69 | + }; |
| 70 | + } |
| 71 | + |
| 72 | + public createFailOutput(): ExecuteActivityOutput { |
| 73 | + return { |
| 74 | + item: new GenericParentTreeItem(undefined, { |
| 75 | + contextValue: createUniversallyUniqueContextValue(['containerRegistryAcrPullEnableStepFailItem', activityFailContext]), |
| 76 | + label: localize('enableAcrPull', 'Grant "{0}" access to container environment identity"', 'acrPull'), |
| 77 | + iconPath: activityFailIcon |
| 78 | + }), |
| 79 | + message: localize('enableAcrPullFail', 'Failed to grant "{0}" access to container environment identity.', 'acrPull'), |
| 80 | + }; |
| 81 | + } |
| 82 | +} |
0 commit comments