|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { SequencerByKey } from 'vs/base/common/async'; |
| 7 | +import { IEncryptionService } from 'vs/platform/encryption/common/encryptionService'; |
| 8 | +import { createDecorator } from 'vs/platform/instantiation/common/instantiation'; |
| 9 | +import { IStorageService, InMemoryStorageService, StorageScope, StorageTarget } from 'vs/platform/storage/common/storage'; |
| 10 | +import { Event, PauseableEmitter } from 'vs/base/common/event'; |
| 11 | +import { ILogService } from 'vs/platform/log/common/log'; |
| 12 | + |
| 13 | +export const ISecretStorageService = createDecorator<ISecretStorageService>('secretStorageService'); |
| 14 | + |
| 15 | +export interface ISecretStorageProvider { |
| 16 | + get(key: string): Promise<string | undefined>; |
| 17 | + set(key: string, value: string): Promise<void>; |
| 18 | + delete(key: string): Promise<void>; |
| 19 | +} |
| 20 | + |
| 21 | +export interface ISecretStorageService extends ISecretStorageProvider { |
| 22 | + readonly _serviceBrand: undefined; |
| 23 | + onDidChangeSecret: Event<string>; |
| 24 | +} |
| 25 | + |
| 26 | +export class SecretStorageService implements ISecretStorageService { |
| 27 | + declare readonly _serviceBrand: undefined; |
| 28 | + |
| 29 | + private _storagePrefix = 'secret://'; |
| 30 | + |
| 31 | + private readonly _onDidChangeSecret = new PauseableEmitter<string>(); |
| 32 | + onDidChangeSecret: Event<string> = this._onDidChangeSecret.event; |
| 33 | + |
| 34 | + private readonly _sequencer = new SequencerByKey<string>(); |
| 35 | + private initialized = this.init(); |
| 36 | + |
| 37 | + constructor( |
| 38 | + @IStorageService private _storageService: IStorageService, |
| 39 | + @IEncryptionService private _encryptionService: IEncryptionService, |
| 40 | + @ILogService private readonly _logService: ILogService, |
| 41 | + ) { |
| 42 | + this._storageService.onDidChangeValue(e => this.onDidChangeValue(e.key)); |
| 43 | + } |
| 44 | + |
| 45 | + private onDidChangeValue(key: string): void { |
| 46 | + if (!key.startsWith(this._storagePrefix)) { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + if (this._onDidChangeSecret.isPaused) { |
| 51 | + this._logService.trace(`[SecretStorageService] Skipping change event for secret: ${key} because it is paused`); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + const secretKey = key.slice(this._storagePrefix.length); |
| 56 | + |
| 57 | + this._logService.trace(`[SecretStorageService] Notifying change in value for secret: ${secretKey}`); |
| 58 | + this._onDidChangeSecret.fire(secretKey); |
| 59 | + } |
| 60 | + |
| 61 | + get(key: string): Promise<string | undefined> { |
| 62 | + return this._sequencer.queue(key, async () => { |
| 63 | + await this.initialized; |
| 64 | + |
| 65 | + const fullKey = this.getKey(key); |
| 66 | + const encrypted = this._storageService.get(fullKey, StorageScope.APPLICATION); |
| 67 | + if (!encrypted) { |
| 68 | + return undefined; |
| 69 | + } |
| 70 | + |
| 71 | + try { |
| 72 | + return await this._encryptionService.decrypt(encrypted); |
| 73 | + } catch (e) { |
| 74 | + this._logService.error(e); |
| 75 | + this.delete(key); |
| 76 | + return undefined; |
| 77 | + } |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + set(key: string, value: string): Promise<void> { |
| 82 | + return this._sequencer.queue(key, async () => { |
| 83 | + await this.initialized; |
| 84 | + |
| 85 | + const encrypted = await this._encryptionService.encrypt(value); |
| 86 | + const fullKey = this.getKey(key); |
| 87 | + try { |
| 88 | + this._onDidChangeSecret.pause(); |
| 89 | + this._storageService.store(fullKey, encrypted, StorageScope.APPLICATION, StorageTarget.MACHINE); |
| 90 | + } finally { |
| 91 | + this._onDidChangeSecret.resume(); |
| 92 | + } |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + delete(key: string): Promise<void> { |
| 97 | + return this._sequencer.queue(key, async () => { |
| 98 | + await this.initialized; |
| 99 | + |
| 100 | + const fullKey = this.getKey(key); |
| 101 | + try { |
| 102 | + this._onDidChangeSecret.pause(); |
| 103 | + this._storageService.remove(fullKey, StorageScope.APPLICATION); |
| 104 | + } finally { |
| 105 | + this._onDidChangeSecret.resume(); |
| 106 | + } |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + private async init(): Promise<void> { |
| 111 | + if (await this._encryptionService.isEncryptionAvailable()) { |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + this._logService.trace('[SecretStorageService] Encryption is not available, falling back to in-memory storage'); |
| 116 | + |
| 117 | + this._storageService = new InMemoryStorageService(); |
| 118 | + } |
| 119 | + |
| 120 | + private getKey(key: string): string { |
| 121 | + return `${this._storagePrefix}${key}`; |
| 122 | + } |
| 123 | +} |
0 commit comments