Skip to content

Commit

Permalink
chore: added migration step to migrate keytar secrets to vscode Secre…
Browse files Browse the repository at this point in the history
…tStorage - VSCODE-435 (#552)
  • Loading branch information
himanshusinghs authored Jul 6, 2023
1 parent 7ae0768 commit c2b23a1
Show file tree
Hide file tree
Showing 8 changed files with 891 additions and 159 deletions.
363 changes: 219 additions & 144 deletions src/connectionController.ts

Large diffs are not rendered by default.

31 changes: 28 additions & 3 deletions src/storage/storageController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ export type ConnectionsFromStorage = {
[connectionId: string]: StoreConnectionInfo;
};

export const SecretStorageLocation = {
Keytar: 'vscode.Keytar',
SecretStorage: 'vscode.SecretStorage',
} as const;

export type SecretStorageLocationType =
| typeof SecretStorageLocation.Keytar
| typeof SecretStorageLocation.SecretStorage;

interface StorageVariableContents {
[StorageVariables.GLOBAL_USER_ID]: string;
[StorageVariables.GLOBAL_ANONYMOUS_ID]: string;
Expand All @@ -48,11 +57,14 @@ export default class StorageController {
[StorageLocation.WORKSPACE]: vscode.Memento;
};

_secretStorage: vscode.SecretStorage;

constructor(context: vscode.ExtensionContext) {
this._storage = {
[StorageLocation.GLOBAL]: context.globalState,
[StorageLocation.WORKSPACE]: context.workspaceState,
};
this._secretStorage = context.secrets;
}

get<T extends StoredVariableName>(
Expand Down Expand Up @@ -123,9 +135,9 @@ export default class StorageController {
);
}

async saveConnection(
storeConnectionInfo: StoreConnectionInfo
): Promise<StoreConnectionInfo> {
async saveConnection<T extends StoreConnectionInfo>(
storeConnectionInfo: T
): Promise<T> {
const dontShowSaveLocationPrompt = vscode.workspace
.getConfiguration('mdb.connectionSaving')
.get('hideOptionToChooseWhereToSaveNewConnections');
Expand Down Expand Up @@ -243,4 +255,17 @@ export default class StorageController {

return StorageLocation.NONE;
}

async getSecret(key: string): Promise<string | null> {
return (await this._secretStorage.get(key)) ?? null;
}

async deleteSecret(key: string): Promise<boolean> {
await this._secretStorage.delete(key);
return true;
}

async setSecret(key: string, value: string): Promise<void> {
await this._secretStorage.store(key, value);
}
}
9 changes: 8 additions & 1 deletion src/telemetry/telemetryService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ type PlaygroundLoadedTelemetryEventProperties = {
file_type?: string;
};

type KeytarSecretsMigrationFailedProperties = {
totalConnections: number;
connectionsWithFailedKeytarMigration: number;
};

export type TelemetryEventProperties =
| PlaygroundTelemetryEventProperties
| LinkClickedTelemetryEventProperties
Expand All @@ -78,7 +83,8 @@ export type TelemetryEventProperties =
| QueryExportedTelemetryEventProperties
| PlaygroundCreatedTelemetryEventProperties
| PlaygroundSavedTelemetryEventProperties
| PlaygroundLoadedTelemetryEventProperties;
| PlaygroundLoadedTelemetryEventProperties
| KeytarSecretsMigrationFailedProperties;

export enum TelemetryEventTypes {
PLAYGROUND_CODE_EXECUTED = 'Playground Code Executed',
Expand All @@ -92,6 +98,7 @@ export enum TelemetryEventTypes {
QUERY_EXPORTED = 'Query Exported',
AGGREGATION_EXPORTED = 'Aggregation Exported',
PLAYGROUND_CREATED = 'Playground Created',
KEYTAR_SECRETS_MIGRATION_FAILED = 'Keytar Secrets Migration Failed',
}

/**
Expand Down
Loading

0 comments on commit c2b23a1

Please sign in to comment.