Update Azure core packages and additional error handling (#24500)

This commit is contained in:
Cheena Malhotra
2023-09-28 15:53:49 -07:00
committed by GitHub
parent 25cdb558a7
commit 731dc6318f
8 changed files with 149 additions and 115 deletions

View File

@@ -32,6 +32,7 @@ const localize = nls.loadMessageBundle();
export type GetTenantsResponseData = {
value: TenantResponse[];
error?: string;
}
export abstract class AzureAuth implements vscode.Disposable {
@@ -45,6 +46,7 @@ export abstract class AzureAuth implements vscode.Disposable {
protected readonly clientId: string;
protected readonly resources: Resource[];
protected readonly httpClient: HttpClient;
private readonly _disposableStore: vscode.Disposable[];
constructor(
protected readonly metadata: AzureAccountProviderMetadata,
@@ -55,7 +57,7 @@ export abstract class AzureAuth implements vscode.Disposable {
protected readonly authType: AzureAuthType,
public readonly userFriendlyName: string
) {
this._disposableStore = [];
this.loginEndpointUrl = this.metadata.settings.host;
this.commonTenant = {
id: 'common',
@@ -99,6 +101,7 @@ export abstract class AzureAuth implements vscode.Disposable {
this.scopes = [...this.metadata.settings.scopes];
this.scopesString = this.scopes.join(' ');
this.httpClient = getProxyEnabledHttpClient();
this._disposableStore.push(this.uriEventEmitter);
}
public async startLogin(): Promise<AzureAccount | azdata.PromptFailedResult> {
@@ -510,8 +513,7 @@ export abstract class AzureAuth implements vscode.Disposable {
this.clientApplication.clearCache();
// unlink both cache files
await this.msalCacheProvider.unlinkMsalCache();
await this.msalCacheProvider.unlinkLocalCache();
await this.msalCacheProvider.unlinkCacheFiles();
// Delete Encryption Keys
await this.msalCacheProvider.clearCacheEncryptionKeys();
@@ -541,7 +543,9 @@ export abstract class AzureAuth implements vscode.Disposable {
await this.msalCacheProvider.clearAccountFromLocalCache(accountKey.accountId);
}
public async dispose() { }
public async dispose() {
this._disposableStore.forEach(d => d.dispose());
}
public async autoOAuthCancelled(): Promise<void> { }

View File

@@ -75,7 +75,7 @@ export class MsalCachePluginProvider {
// Handle deserialization error in cache file in case file gets corrupted.
// Clearing cache here will ensure account is marked stale so re-authentication can be triggered.
Logger.verbose(`MsalCachePlugin: Error occurred when trying to read cache file, file will be deleted: ${e.message}`);
await fsPromises.unlink(this._msalCacheConfiguration.cacheFilePath);
await this.unlinkCache(this._msalCacheConfiguration);
}
}
@@ -177,17 +177,11 @@ export class MsalCachePluginProvider {
}
/**
* Deletes Msal access token cache file
* Deletes both cache files.
*/
public async unlinkMsalCache(): Promise<void> {
await fsPromises.unlink(this._msalCacheConfiguration.cacheFilePath);
}
/**
* Deletes local access token cache file.
*/
public async unlinkLocalCache(): Promise<void> {
await fsPromises.unlink(this._localCacheConfiguration.cacheFilePath);
public async unlinkCacheFiles(): Promise<void> {
await this.unlinkCache(this._msalCacheConfiguration);
await this.unlinkCache(this._localCacheConfiguration);
}
//#region Private helper methods
@@ -225,13 +219,12 @@ export class MsalCachePluginProvider {
else {
Logger.error(`MsalCachePlugin: Failed to read from cache file: ${e}`);
Logger.verbose(`MsalCachePlugin: Error occurred when trying to read cache file ${currentConfig.name}, file will be deleted: ${e.message}`);
await fsPromises.unlink(currentConfig.cacheFilePath);
await this.unlinkCache(currentConfig);
// Ensure both configurations are not same.
if (currentConfig.name !== alternateConfig.name) {
// Delete alternate cache file as well.
alternateConfig.lockTaken = await this.waitAndLock(alternateConfig.lockFilePath, alternateConfig.lockTaken);
await fsPromises.unlink(alternateConfig.cacheFilePath);
await this.unlinkCache(alternateConfig);
lockFile.unlockSync(alternateConfig.lockFilePath);
alternateConfig.lockTaken = false;
Logger.verbose(`MsalCachePlugin: Cache file for ${alternateConfig.name} cache also deleted.`);
@@ -276,5 +269,16 @@ export class MsalCachePluginProvider {
}
return lockTaken;
}
/**
* Deletes access token cache file for specified config
*/
private async unlinkCache(config: CacheConfiguration): Promise<void> {
try {
await fsPromises.unlink(config.cacheFilePath);
} catch (e) {
Logger.info(`An error occurred when clearing ${config.name} Cache, safely ignored: ${e}`);
}
}
//#endregion
}

View File

@@ -8,7 +8,6 @@ import * as vscode from 'vscode';
import { SubscriptionClient } from '@azure/arm-subscriptions';
import { AzureAccount, azureResource } from 'azurecore';
import { IAzureResourceSubscriptionService } from '../interfaces';
import { TokenCredentials } from '@azure/ms-rest-js';
import { AzureSubscriptionError } from '../errors';
import { AzureResourceErrorMessageUtil } from '../utils';
import { Logger } from '../../utils/Logger';
@@ -16,6 +15,7 @@ import { Logger } from '../../utils/Logger';
import * as nls from 'vscode-nls';
import { TenantIgnoredError } from '../../utils/TenantIgnoredError';
import { multiple_matching_tokens_error } from '../../constants';
import { TokenCredentials } from '@azure/ms-rest-js';
const localize = nls.loadMessageBundle();
export class AzureResourceSubscriptionService implements IAzureResourceSubscriptionService {