Skip to content

Commit 58b0abe

Browse files
authored
Try using oauth2 token for storage client (#4506)
* Try using oauth2 token for storage client * Update azure utils package
1 parent aea67ee commit 58b0abe

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1412,7 +1412,7 @@
14121412
"@azure/storage-blob": "^12.5.0",
14131413
"@microsoft/vscode-azext-azureappservice": "^3.6.2",
14141414
"@microsoft/vscode-azext-azureappsettings": "^0.2.8",
1415-
"@microsoft/vscode-azext-azureutils": "^3.3.1",
1415+
"@microsoft/vscode-azext-azureutils": "^3.3.2",
14161416
"@microsoft/vscode-azext-utils": "^3.1.1",
14171417
"@microsoft/vscode-azureresources-api": "^2.0.4",
14181418
"cross-fetch": "^4.0.0",

src/commands/createFunctionApp/FunctionAppCreateStep.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55

66
import { type FunctionsDeploymentStorageAuthentication, type NameValuePair, type Site, type SiteConfig, type WebSiteManagementClient } from '@azure/arm-appservice';
77
import { type Identity } from '@azure/arm-resources';
8+
import { type StorageAccount } from '@azure/arm-storage';
89
import { BlobServiceClient } from '@azure/storage-blob';
910
import { ParsedSite, WebsiteOS, type CustomLocation, type IAppServiceWizardContext } from '@microsoft/vscode-azext-azureappservice';
1011
import { LocationListStep } from '@microsoft/vscode-azext-azureutils';
11-
import { AzureWizardExecuteStepWithActivityOutput, maskUserInfo, parseError, randomUtils } from '@microsoft/vscode-azext-utils';
12+
import { AzureWizardExecuteStepWithActivityOutput, maskUserInfo, nonNullProp, parseError, randomUtils } from '@microsoft/vscode-azext-utils';
1213
import { type AppResource } from '@microsoft/vscode-azext-utils/hostapi';
1314
import { type Progress } from 'vscode';
1415
import { FuncVersion, getMajorVersion } from '../../FuncVersion';
@@ -18,7 +19,6 @@ import { localize } from '../../localize';
1819
import { createWebSiteClient } from '../../utils/azureClients';
1920
import { getRandomHexString } from '../../utils/fs';
2021
import { createAzureWebJobsStorageManagedIdentitySettings } from '../../utils/managedIdentityUtils';
21-
import { nonNullProp } from '../../utils/nonNull';
2222
import { getStorageConnectionString } from '../appSettings/connectionSettings/getLocalConnectionSetting';
2323
import { enableFileLogging } from '../logstream/enableFileLogging';
2424
import { type FullFunctionAppStack, type IFlexFunctionAppWizardContext, type IFunctionAppWizardContext } from './IFunctionAppWizardContext';
@@ -241,8 +241,9 @@ export class FunctionAppCreateStep extends AzureWizardExecuteStepWithActivityOut
241241
const result = await client.webApps.beginCreateOrUpdateAndWait(rgName, siteName, site);
242242

243243
if (context.newFlexSku) {
244-
const storageConnectionString: string = (await getStorageConnectionString(context)).connectionString;
245-
await tryCreateStorageContainer(result, storageConnectionString);
244+
if (context.storageAccount) {
245+
await tryCreateStorageContainer(context, result, context.storageAccount);
246+
}
246247
}
247248

248249
return result;
@@ -284,16 +285,28 @@ function getSiteKind(context: IAppServiceWizardContext): string {
284285
}
285286

286287
// storage container is needed for flex deployment, but it is not created automatically
287-
async function tryCreateStorageContainer(site: Site, storageConnectionString: string): Promise<void> {
288+
async function tryCreateStorageContainer(context: IFlexFunctionAppWizardContext, site: Site, storageAccount: StorageAccount): Promise<void> {
289+
let client: BlobServiceClient;
290+
try {
291+
const token = await context.createCredentialsForScopes(['https://storage.azure.com/.default'])
292+
const primaryEndpoint = nonNullProp(storageAccount, 'primaryEndpoints');
293+
client = new BlobServiceClient(nonNullProp(primaryEndpoint, 'blob'), token);
294+
await client.getProperties(); // Trigger a request to validate the token
295+
} catch (error) {
296+
const storageConnectionString: string = (await getStorageConnectionString(context)).connectionString;
297+
client = BlobServiceClient.fromConnectionString(storageConnectionString);
298+
await client.getProperties(); // Trigger a request to validate the key
299+
}
300+
288301
try {
289-
const blobClient = BlobServiceClient.fromConnectionString(storageConnectionString);
290302
const containerUrl: string | undefined = site.functionAppConfig?.deployment?.storage?.value;
291303
if (containerUrl) {
292304
const containerName = containerUrl.split('/').pop();
293305
if (containerName) {
294-
const client = blobClient.getContainerClient(containerName);
295-
if (!await client.exists()) {
296-
await blobClient.createContainer(containerName);
306+
const containerClient = client.getContainerClient(containerName);
307+
if (!await containerClient.exists()) {
308+
await client.createContainer(containerName);
309+
return
297310
} else {
298311
ext.outputChannel.appendLog(localize('deploymentStorageExists', 'Deployment storage container "{0}" already exists.', containerName));
299312
return;

0 commit comments

Comments
 (0)