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

chore: only load keytar during the migration process VSCODE-450 #572

Merged
merged 1 commit into from
Aug 7, 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
9 changes: 8 additions & 1 deletion src/connectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import ConnectionString from 'mongodb-connection-string-url';
import { EventEmitter } from 'events';
import type { MongoClientOptions } from 'mongodb';
import { v4 as uuidv4 } from 'uuid';

import { createKeytar } from './utils/keytar';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we maybe update this model, so it remembers that keytar require failed once, and we don't try to resolve it again for each next connection that goes through the migration? With our webpack configuration, the failed require won't be cached:

vscode/webpack.config.js

Lines 75 to 76 in 711587b

strictModuleErrorHandling: true,
strictModuleExceptionHandling: true,

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another way to handle it is to move the createKeytar call to the loadSavedConnections function, where can see if there are connections without secretStorageLocation and then try to require it once for the whole migration attempt.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which of these solutions you think would be more valuable? From what I've understood, keytar is a deprecated feature, so all the code related to it will be removed at some point. IMO, I would just do whatever is cheaper and doesn't impact the user negatively.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would require it once in loadSavedConnections, but you can decide what works better implementation-wise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving it to loadSavedConnections needs some changes in the tests, as some of them fail.

I'll keep the current version and merge.

Thanks a lot!

Copy link
Contributor

@alenakhineika alenakhineika Aug 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What will happen when vscode team removes keytar from vscode and there are migrations that require a migration? Are we ok with resolving the keytar module for each connection? Will this make the launch of the extension slower than it could be?

Copy link
Contributor

@alenakhineika alenakhineika Aug 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving createKeytar to loadSavedConnections should note brake tests. Have you tried something like this?

async loadSavedConnections(): Promise<void> {
    const globalAndWorkspaceConnections = Object.entries({
      ...this._storageController.get(
        StorageVariables.GLOBAL_SAVED_CONNECTIONS,
        StorageLocation.GLOBAL
      ),
      ...this._storageController.get(
        StorageVariables.WORKSPACE_SAVED_CONNECTIONS,
        StorageLocation.WORKSPACE
      ),
    });

    const hasConnectionsThatDidNotMigrateEarlier =
      !!globalAndWorkspaceConnections.some(([, connectionInfo]) => !connectionInfo.storageLocation);

    if (hasConnectionsThatDidNotMigrateEarlier) {
      try {
        ext.keytarModule =
          ext.keytarModule === undefined ? createKeytar() : ext.keytarModule;
      } catch (err) {
        // Couldn't load keytar, proceed without storing & loading connections.
      }
    }

    ...
}

import { CONNECTION_STATUS } from './views/webview-app/extension-app-message-constants';
import { createLogger } from './logging';
import { ext } from './extensionConstants';
Expand Down Expand Up @@ -296,6 +296,13 @@ export default class ConnectionController {
async _migrateConnectionWithKeytarSecrets(
savedConnectionInfo: StoreConnectionInfoWithConnectionOptions
): Promise<MigratedStoreConnectionInfoWithConnectionOptions | undefined> {
try {
ext.keytarModule =
ext.keytarModule === undefined ? createKeytar() : ext.keytarModule;
} catch (err) {
// Couldn't load keytar, proceed without storing & loading connections.
}

// If the Keytar module is not available, we simply mark the connections
// storage as Keytar and return
if (!ext.keytarModule) {
Expand Down
10 changes: 0 additions & 10 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import * as vscode from 'vscode';

import { ext } from './extensionConstants';
import { createKeytar } from './utils/keytar';
import { createLogger } from './logging';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { version } = require('../package.json');
Expand All @@ -30,14 +29,6 @@ export async function activate(
context: vscode.ExtensionContext
): Promise<void> {
ext.context = context;
let hasKeytar = false;

try {
ext.keytarModule = createKeytar();
hasKeytar = true;
} catch (err) {
// Couldn't load keytar, proceed without storing & loading connections.
}

const defaultConnectionSavingLocation = vscode.workspace
.getConfiguration('mdb.connectionSaving')
Expand All @@ -53,7 +44,6 @@ export async function activate(
workspaceStoragePath: context.storageUri?.path,
globalStoragePath: context.globalStorageUri.path,
defaultConnectionSavingLocation,
hasKeytar,
buildInfo: {
nodeVersion: process.version,
runtimePlatform: process.platform,
Expand Down