Skip to content

Commit c2b23a1

Browse files
chore: added migration step to migrate keytar secrets to vscode SecretStorage - VSCODE-435 (#552)
1 parent 7ae0768 commit c2b23a1

File tree

8 files changed

+891
-159
lines changed

8 files changed

+891
-159
lines changed

src/connectionController.ts

Lines changed: 219 additions & 144 deletions
Large diffs are not rendered by default.

src/storage/storageController.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ export type ConnectionsFromStorage = {
3232
[connectionId: string]: StoreConnectionInfo;
3333
};
3434

35+
export const SecretStorageLocation = {
36+
Keytar: 'vscode.Keytar',
37+
SecretStorage: 'vscode.SecretStorage',
38+
} as const;
39+
40+
export type SecretStorageLocationType =
41+
| typeof SecretStorageLocation.Keytar
42+
| typeof SecretStorageLocation.SecretStorage;
43+
3544
interface StorageVariableContents {
3645
[StorageVariables.GLOBAL_USER_ID]: string;
3746
[StorageVariables.GLOBAL_ANONYMOUS_ID]: string;
@@ -48,11 +57,14 @@ export default class StorageController {
4857
[StorageLocation.WORKSPACE]: vscode.Memento;
4958
};
5059

60+
_secretStorage: vscode.SecretStorage;
61+
5162
constructor(context: vscode.ExtensionContext) {
5263
this._storage = {
5364
[StorageLocation.GLOBAL]: context.globalState,
5465
[StorageLocation.WORKSPACE]: context.workspaceState,
5566
};
67+
this._secretStorage = context.secrets;
5668
}
5769

5870
get<T extends StoredVariableName>(
@@ -123,9 +135,9 @@ export default class StorageController {
123135
);
124136
}
125137

126-
async saveConnection(
127-
storeConnectionInfo: StoreConnectionInfo
128-
): Promise<StoreConnectionInfo> {
138+
async saveConnection<T extends StoreConnectionInfo>(
139+
storeConnectionInfo: T
140+
): Promise<T> {
129141
const dontShowSaveLocationPrompt = vscode.workspace
130142
.getConfiguration('mdb.connectionSaving')
131143
.get('hideOptionToChooseWhereToSaveNewConnections');
@@ -243,4 +255,17 @@ export default class StorageController {
243255

244256
return StorageLocation.NONE;
245257
}
258+
259+
async getSecret(key: string): Promise<string | null> {
260+
return (await this._secretStorage.get(key)) ?? null;
261+
}
262+
263+
async deleteSecret(key: string): Promise<boolean> {
264+
await this._secretStorage.delete(key);
265+
return true;
266+
}
267+
268+
async setSecret(key: string, value: string): Promise<void> {
269+
await this._secretStorage.store(key, value);
270+
}
246271
}

src/telemetry/telemetryService.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ type PlaygroundLoadedTelemetryEventProperties = {
6868
file_type?: string;
6969
};
7070

71+
type KeytarSecretsMigrationFailedProperties = {
72+
totalConnections: number;
73+
connectionsWithFailedKeytarMigration: number;
74+
};
75+
7176
export type TelemetryEventProperties =
7277
| PlaygroundTelemetryEventProperties
7378
| LinkClickedTelemetryEventProperties
@@ -78,7 +83,8 @@ export type TelemetryEventProperties =
7883
| QueryExportedTelemetryEventProperties
7984
| PlaygroundCreatedTelemetryEventProperties
8085
| PlaygroundSavedTelemetryEventProperties
81-
| PlaygroundLoadedTelemetryEventProperties;
86+
| PlaygroundLoadedTelemetryEventProperties
87+
| KeytarSecretsMigrationFailedProperties;
8288

8389
export enum TelemetryEventTypes {
8490
PLAYGROUND_CODE_EXECUTED = 'Playground Code Executed',
@@ -92,6 +98,7 @@ export enum TelemetryEventTypes {
9298
QUERY_EXPORTED = 'Query Exported',
9399
AGGREGATION_EXPORTED = 'Aggregation Exported',
94100
PLAYGROUND_CREATED = 'Playground Created',
101+
KEYTAR_SECRETS_MIGRATION_FAILED = 'Keytar Secrets Migration Failed',
95102
}
96103

97104
/**

0 commit comments

Comments
 (0)