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

More logs around SecretStorage #165542

Merged
merged 1 commit into from
Nov 4, 2022
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
25 changes: 20 additions & 5 deletions src/vs/platform/encryption/node/encryptionMainService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import { ICommonEncryptionService } from 'vs/platform/encryption/common/encryptionService';
import { ILogService } from 'vs/platform/log/common/log';

export interface Encryption {
encrypt(salt: string, value: string): Promise<string>;
Expand All @@ -12,28 +13,42 @@ export interface Encryption {
export class EncryptionMainService implements ICommonEncryptionService {
declare readonly _serviceBrand: undefined;
constructor(
private machineId: string) {

}
private machineId: string,
@ILogService private readonly logService: ILogService
) { }

private encryption(): Promise<Encryption> {
return new Promise((resolve, reject) => require(['vscode-encrypt'], resolve, reject));
}

async encrypt(value: string): Promise<string> {
let encryption: Encryption;
try {
encryption = await this.encryption();
} catch (e) {
return value;
}

try {
const encryption = await this.encryption();
return encryption.encrypt(this.machineId, value);
} catch (e) {
this.logService.error(e);
return value;
}
}

async decrypt(value: string): Promise<string> {
let encryption: Encryption;
try {
encryption = await this.encryption();
} catch (e) {
return value;
}

try {
const encryption = await this.encryption();
return encryption.decrypt(this.machineId, value);
} catch (e) {
this.logService.error(e);
return value;
}
}
Expand Down
13 changes: 9 additions & 4 deletions src/vs/workbench/api/browser/mainThreadSecretState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,21 @@ export class MainThreadSecretState extends Disposable implements MainThreadSecre
}

async $getPassword(extensionId: string, key: string): Promise<string | undefined> {
this.logService.trace(`Getting password for ${extensionId} extension:`, key);
this.logService.trace(`MainThreadSecretState#getPassword: Getting password for ${extensionId} extension: `, key);
const fullKey = await this.getFullKey(extensionId);
const password = await this.credentialsService.getPassword(fullKey, key);
if (!password) {
this.logService.trace('No password found for:', key);
this.logService.trace('MainThreadSecretState#getPassword: No password found for: ', key);
return undefined;
}

let decrypted: string | null;
try {
this.logService.trace('MainThreadSecretState#getPassword: Decrypting password for: ', key);
decrypted = await this.encryptionService.decrypt(password);
} catch (e) {
this.logService.error(e);
this.logService.trace('MainThreadSecretState#getPassword: Trying migration for: ', key);

// If we are on a platform that newly started encrypting secrets before storing them,
// then passwords previously stored were stored un-encrypted (NOTE: but still being stored in a secure keyring).
Expand All @@ -65,7 +67,7 @@ export class MainThreadSecretState extends Disposable implements MainThreadSecre
try {
const value = JSON.parse(decrypted);
if (value.extensionId === extensionId) {
this.logService.trace('Password found for:', key);
this.logService.trace('MainThreadSecretState#getPassword: Password found for: ', key);
return value.content;
}
} catch (parseError) {
Expand All @@ -82,17 +84,20 @@ export class MainThreadSecretState extends Disposable implements MainThreadSecre
}
}

this.logService.trace('No password found for:', key);
this.logService.trace('MainThreadSecretState#getPassword: No password found for: ', key);
return undefined;
}

async $setPassword(extensionId: string, key: string, value: string): Promise<void> {
this.logService.trace(`MainThreadSecretState#setPassword: Setting password for ${extensionId} extension: `, key);
const fullKey = await this.getFullKey(extensionId);
const toEncrypt = JSON.stringify({
extensionId,
content: value
});
this.logService.trace('MainThreadSecretState#setPassword: Encrypting password for: ', key);
const encrypted = await this.encryptionService.encrypt(toEncrypt);
this.logService.trace('MainThreadSecretState#setPassword: Storing password for: ', key);
return await this.credentialsService.setPassword(fullKey, key, encrypted);
}

Expand Down