Skip to content

Commit 96c1e18

Browse files
committed
Get managed identity from the azure utils package
1 parent 945dd5f commit 96c1e18

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

src/commands/createFunctionApp/FunctionAppCreateStep.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import { type NameValuePair, type Site, type SiteConfig, type WebSiteManagementClient } from '@azure/arm-appservice';
7-
import { AuthorizationManagementClient } from '@azure/arm-authorization-profile-2020-09-01-hybrid';
7+
import { type Identity } from '@azure/arm-resources';
88
import { BlobServiceClient } from '@azure/storage-blob';
99
import { ParsedSite, WebsiteOS, type CustomLocation, type IAppServiceWizardContext } from '@microsoft/vscode-azext-azureappservice';
1010
import { LocationListStep } from '@microsoft/vscode-azext-azureutils';
@@ -57,16 +57,6 @@ export class FunctionAppCreateStep extends AzureWizardExecuteStep<IFunctionAppWi
5757
context.telemetry.properties.fileLoggingError = parseError(error).message;
5858
}
5959
}
60-
const principalId = nonNullProp(nonNullProp(context.site, 'identity'), 'principalId');
61-
// this is the same apiVersion being used by the portal
62-
const apiVersion = '2020-06-01';
63-
const amClient = new AuthorizationManagementClient(context.credentials, context.subscriptionId, { apiVersion });
64-
65-
const scope = nonNullProp(nonNullProp(context, 'storageAccount'), 'id');
66-
const guid = crypto.randomUUID();
67-
// this roleDefintionId cooresponds to the "Storage Blob Data Contributor" role
68-
const roleDefinitionId = '/providers/Microsoft.Authorization/roleDefinitions/ba92f5b4-2d11-453d-a403-e96b0029c9fe';
69-
await amClient.roleAssignments.create(scope, guid, { properties: { roleDefinitionId, principalId } });
7060
showSiteCreated(site, context);
7161
}
7262

@@ -76,6 +66,14 @@ export class FunctionAppCreateStep extends AzureWizardExecuteStep<IFunctionAppWi
7666

7767
private async getNewSite(context: IFunctionAppWizardContext, stack: FullFunctionAppStack): Promise<Site> {
7868
const location = await LocationListStep.getLocation(context, webProvider);
69+
let identity: Identity | undefined = undefined;
70+
if (context.managedIdentity) {
71+
const userAssignedIdentities = {};
72+
userAssignedIdentities[nonNullProp(context.managedIdentity, 'id')] =
73+
{ principalId: context.managedIdentity?.principalId, clientId: context.managedIdentity?.clientId };
74+
identity = { type: 'UserAssigned', userAssignedIdentities }
75+
}
76+
7977
const site: Site = {
8078
name: context.newSiteName,
8179
kind: getSiteKind(context),
@@ -84,7 +82,7 @@ export class FunctionAppCreateStep extends AzureWizardExecuteStep<IFunctionAppWi
8482
clientAffinityEnabled: false,
8583
siteConfig: await this.getNewSiteConfig(context, stack),
8684
reserved: context.newSiteOS === WebsiteOS.linux, // The secret property - must be set to true to make it a Linux plan. Confirmed by the team who owns this API.
87-
identity: { type: 'SystemAssigned' }
85+
identity
8886
};
8987

9088
if (context.customLocation) {

src/commands/createFunctionApp/createCreateFunctionAppComponents.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import { AppInsightsCreateStep, AppInsightsListStep, AppKind, AppServicePlanCreateStep, AppServicePlanListStep, CustomLocationListStep, LogAnalyticsCreateStep, SiteNameStep, WebsiteOS, type IAppServiceWizardContext } from "@microsoft/vscode-azext-azureappservice";
7-
import { LocationListStep, ResourceGroupCreateStep, ResourceGroupListStep, StorageAccountCreateStep, StorageAccountKind, StorageAccountListStep, StorageAccountPerformance, StorageAccountReplication, type INewStorageAccountDefaults } from "@microsoft/vscode-azext-azureutils";
7+
import { LocationListStep, ResourceGroupCreateStep, ResourceGroupListStep, RoleAssignmentExecuteStep, RoleDefinitionId, StorageAccountCreateStep, StorageAccountKind, StorageAccountListStep, StorageAccountPerformance, StorageAccountReplication, UserAssignedIdentityCreateStep, type INewStorageAccountDefaults } from "@microsoft/vscode-azext-azureutils";
88
import { type AzureWizardExecuteStep, type AzureWizardPromptStep, type ISubscriptionContext } from "@microsoft/vscode-azext-utils";
99
import { FuncVersion, latestGAVersion, tryParseFuncVersion } from "../../FuncVersion";
1010
import { funcVersionSetting } from "../../constants";
@@ -71,6 +71,8 @@ export async function createCreateFunctionAppComponents(context: ICreateFunction
7171
executeSteps.push(new ResourceGroupCreateStep());
7272
executeSteps.push(new StorageAccountCreateStep(storageAccountCreateOptions));
7373
executeSteps.push(new AppInsightsCreateStep());
74+
executeSteps.push(new UserAssignedIdentityCreateStep());
75+
executeSteps.push(new RoleAssignmentExecuteStep(() => wizardContext?.storageAccount?.id, RoleDefinitionId['Storage Blob Data Contributor']));
7476
if (!context.dockerfilePath) {
7577
executeSteps.push(new AppServicePlanCreateStep());
7678
executeSteps.push(new LogAnalyticsCreateStep());

0 commit comments

Comments
 (0)