Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean encryption keys with cache clear command #23875

Merged
merged 1 commit into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,9 @@ export abstract class AzureAuth implements vscode.Disposable {
// unlink both cache files
await this.msalCacheProvider.unlinkMsalCache();
await this.msalCacheProvider.unlinkLocalCache();

// Delete Encryption Keys
await this.msalCacheProvider.clearCacheEncryptionKeys();
}

public async deleteAllCacheAdal(): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { Configuration, PublicClientApplication } from '@azure/msal-node';
import * as Constants from '../constants';
import { Logger } from '../utils/Logger';
import { ILoggerCallback, LogLevel as MsalLogLevel } from "@azure/msal-common";
import { displayReloadAds } from '../utils';
import { reloadPromptCacheClear } from '../localizedConstants';

let localize = nls.loadMessageBundle();

Expand Down Expand Up @@ -108,8 +110,7 @@ export class AzureAccountProviderService implements vscode.Disposable {
return Promise.all(promises)
.then(
() => {
let message = localize('clearTokenCacheSuccess', "Token cache successfully cleared");
void vscode.window.showInformationMessage(`${loc.extensionName}: ${message}`);
void displayReloadAds(reloadPromptCacheClear);
},
err => {
let message = localize('clearTokenCacheFailure', "Failed to clear token cache");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,21 @@ export class FileEncryptionHelper {
if (resetOnError) {
// Reset IV/Keys if crypto cannot encrypt/decrypt data.
// This could be a possible case of corruption of expected iv/key combination
await this.deleteEncryptionKey(this._ivCredId);
await this.deleteEncryptionKey(this._keyCredId);
this._ivBuffer = undefined;
this._keyBuffer = undefined;
await this.clearEncryptionKeys();
await this.init();
}
// Throw error so cache file can be reset to empty.
throw new Error(`Decryption failed with error: ${ex}`);
}
}

public async clearEncryptionKeys(): Promise<void> {
await this.deleteEncryptionKey(this._ivCredId);
await this.deleteEncryptionKey(this._keyCredId);
this._ivBuffer = undefined;
this._keyBuffer = undefined;
}

protected async readEncryptionKey(credentialId: string): Promise<string | undefined> {
return (await this._credentialService.readCredential(credentialId))?.password;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ export class MsalCachePluginProvider {
return this._fileEncryptionHelper.getEncryptionKeys();
}

public async clearCacheEncryptionKeys(): Promise<void> {
await this._fileEncryptionHelper.clearEncryptionKeys();
}

public getCachePlugin(): ICachePlugin {
const beforeCacheAccess = async (cacheContext: TokenCacheContext): Promise<void> => {
try {
Expand Down
16 changes: 1 addition & 15 deletions extensions/azurecore/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,25 +293,11 @@ async function onDidChangeConfiguration(e: vscode.ConfigurationChangeEvent): Pro
if (vscode.workspace.getConfiguration(Constants.AzureSection).get('authenticationLibrary') === 'ADAL') {
void vscode.window.showInformationMessage(loc.deprecatedOption);
}
await displayReloadAds();
await utils.displayReloadAds(loc.reloadPrompt);
}
}

function updatePiiLoggingLevel(): void {
const piiLogging: boolean = vscode.workspace.getConfiguration(Constants.AzureSection).get('piiLogging', false);
Logger.piiLogging = piiLogging;
}

// Display notification with button to reload
// return true if button clicked
// return false if button not clicked
async function displayReloadAds(): Promise<boolean> {
const result = await vscode.window.showInformationMessage(loc.reloadPrompt, loc.reloadChoice);
if (result === loc.reloadChoice) {
await vscode.commands.executeCommand('workbench.action.reloadWindow');
return true;
} else {
return false;
}

}
1 change: 1 addition & 0 deletions extensions/azurecore/src/localizedConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const subscription = localize('azurecore.subscription', "Subscription");
export const typeIcon = localize('azurecore.typeIcon', "Type Icon");

export const reloadPrompt = localize('azurecore.reloadPrompt', "Authentication Library has changed, please reload Azure Data Studio.");
export const reloadPromptCacheClear = localize('azurecore.reloadPromptCacheClear', "Token cache has been cleared successfully, please reload Azure Data Studio.");
export const reloadChoice = localize('azurecore.reloadChoice', "Reload Azure Data Studio");

export const deprecatedOption = localize('azurecore.deprecated', "Warning: ADAL has been deprecated, and is scheduled to be removed in the next release. Please use MSAL instead.");
Expand Down
14 changes: 14 additions & 0 deletions extensions/azurecore/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,17 @@ export function getProxyEnabledHttpClient(): HttpClient {

return new HttpClient(proxy, agentOptions);
}

/* Display notification with button to reload
* return true if button clicked
* return false if button not clicked
*/
export async function displayReloadAds(message: string): Promise<boolean> {
const result = await vscode.window.showInformationMessage(message, loc.reloadChoice);
if (result === loc.reloadChoice) {
await vscode.commands.executeCommand('workbench.action.reloadWindow');
return true;
} else {
return false;
}
}
Loading