Skip to content

Commit

Permalink
fix: check secretStorageLocation instead of storageLocation (#583)
Browse files Browse the repository at this point in the history
* fix: check secretStorageLocation instead of storageLocation

* feat: perform a second attempt to migrate connections
  • Loading branch information
alenakhineika authored Aug 23, 2023
1 parent 801c9b7 commit d1ba961
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 19 deletions.
22 changes: 15 additions & 7 deletions src/connectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export default class ConnectionController {
(ids, [connectionId, connectionInfo]) => {
if (
connectionInfo.secretStorageLocation ===
SecretStorageLocation.Keytar
SecretStorageLocation.KeytarSecondAttempt
) {
return [...ids, connectionId];
}
Expand All @@ -175,7 +175,9 @@ export default class ConnectionController {

const hasConnectionsThatDidNotMigrateEarlier =
!!globalAndWorkspaceConnections.some(
([, connectionInfo]) => !connectionInfo.storageLocation
([, connectionInfo]) =>
!connectionInfo.secretStorageLocation ||
connectionInfo.secretStorageLocation === 'vscode.Keytar'
);

if (hasConnectionsThatDidNotMigrateEarlier) {
Expand Down Expand Up @@ -203,7 +205,7 @@ export default class ConnectionController {
this._connections[connectionId] = connectionInfoWithSecrets;
const connectionSecretsInKeytar =
connectionInfoWithSecrets.secretStorageLocation ===
SecretStorageLocation.Keytar;
SecretStorageLocation.KeytarSecondAttempt;
if (
connectionSecretsInKeytar &&
!connectionIdsThatDidNotMigrateEarlier.includes(connectionId)
Expand All @@ -229,7 +231,9 @@ export default class ConnectionController {
loaded_connections: loadedConnections.length,
connections_with_secrets_in_keytar: loadedConnections.filter(
(connection) =>
connection.secretStorageLocation === SecretStorageLocation.Keytar
connection.secretStorageLocation === SecretStorageLocation.Keytar ||
connection.secretStorageLocation ===
SecretStorageLocation.KeytarSecondAttempt
).length,
connections_with_secrets_in_secret_storage: loadedConnections.filter(
(connection) =>
Expand Down Expand Up @@ -265,7 +269,10 @@ export default class ConnectionController {
);
}

if (!connectionInfo.secretStorageLocation) {
if (
!connectionInfo.secretStorageLocation ||
connectionInfo.secretStorageLocation === 'vscode.Keytar'
) {
return await this._migrateConnectionWithKeytarSecrets(
connectionInfo as StoreConnectionInfoWithConnectionOptions
);
Expand All @@ -274,7 +281,8 @@ export default class ConnectionController {
// We tried migrating this connection earlier but failed because Keytar was not
// available. So we return simply the connection without secrets.
if (
connectionInfo.secretStorageLocation === SecretStorageLocation.Keytar
connectionInfo.secretStorageLocation ===
SecretStorageLocation.KeytarSecondAttempt
) {
return connectionInfo as MigratedStoreConnectionInfoWithConnectionOptions;
}
Expand Down Expand Up @@ -322,7 +330,7 @@ export default class ConnectionController {
return await this._storageController.saveConnection<MigratedStoreConnectionInfoWithConnectionOptions>(
{
...savedConnectionInfo,
secretStorageLocation: SecretStorageLocation.Keytar,
secretStorageLocation: SecretStorageLocation.KeytarSecondAttempt,
}
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/storage/storageController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ export type ConnectionsFromStorage = {

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

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

interface StorageVariableContents {
Expand Down
44 changes: 32 additions & 12 deletions src/test/suite/connectionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1464,7 +1464,7 @@ suite('Connection Controller Test Suite', function () {
);
assert.strictEqual(
updatedConnection.secretStorageLocation,
SecretStorageLocation.Keytar
SecretStorageLocation.KeytarSecondAttempt
);
});

Expand Down Expand Up @@ -1661,6 +1661,16 @@ suite('Connection Controller Test Suite', function () {
},
'random-connection-2': {
id: 'random-connection-2',
name: 'localhost:27017',
storageLocation: 'GLOBAL',
secretStorageLocation: SecretStorageLocation.KeytarSecondAttempt,
connectionOptions: {
connectionString:
'mongodb://localhost:27017/?readPreference=primary&ssl=false',
},
},
'random-connection-3': {
id: 'random-connection-3',
name: 'localhost:27018',
storageLocation: 'GLOBAL',
connectionOptions: {
Expand All @@ -1676,7 +1686,7 @@ suite('Connection Controller Test Suite', function () {
(connectionInfo) =>
Promise.resolve({
...connectionInfo,
secretStorageLocation: SecretStorageLocation.Keytar,
secretStorageLocation: SecretStorageLocation.KeytarSecondAttempt,
} as any)
);
const trackStub = testSandbox.stub(
Expand All @@ -1691,16 +1701,16 @@ suite('Connection Controller Test Suite', function () {
// Notified to user
assert.strictEqual(showInformationMessageStub.calledOnce, true);
assert.deepStrictEqual(showInformationMessageStub.lastCall.args, [
keytarMigrationFailedMessage(1),
keytarMigrationFailedMessage(2),
]);

// Tracked
assert.strictEqual(trackStub.calledOnce, true);
assert.deepStrictEqual(trackStub.lastCall.args, [
{
saved_connections: 2,
loaded_connections: 2,
connections_with_failed_keytar_migration: 1,
saved_connections: 3,
loaded_connections: 3,
connections_with_failed_keytar_migration: 2,
},
]);
});
Expand All @@ -1719,7 +1729,7 @@ suite('Connection Controller Test Suite', function () {
id: 'random-connection-1',
name: 'localhost:27017',
storageLocation: 'GLOBAL',
secretStorageLocation: SecretStorageLocation.Keytar,
secretStorageLocation: SecretStorageLocation.KeytarSecondAttempt,
connectionOptions: {
connectionString:
'mongodb://localhost:27017/?readPreference=primary&ssl=false',
Expand All @@ -1729,7 +1739,7 @@ suite('Connection Controller Test Suite', function () {
id: 'random-connection-2',
name: 'localhost:27018',
storageLocation: 'GLOBAL',
secretStorageLocation: SecretStorageLocation.Keytar,
secretStorageLocation: SecretStorageLocation.KeytarSecondAttempt,
connectionOptions: {
connectionString:
'mongodb://localhost:27018/?readPreference=primary&ssl=false',
Expand All @@ -1743,7 +1753,7 @@ suite('Connection Controller Test Suite', function () {
(connectionInfo) =>
Promise.resolve({
...connectionInfo,
secretStorageLocation: SecretStorageLocation.Keytar,
secretStorageLocation: SecretStorageLocation.KeytarSecondAttempt,
} as any)
);
const trackStub = testSandbox.stub(
Expand Down Expand Up @@ -1802,6 +1812,16 @@ suite('Connection Controller Test Suite', function () {
'mongodb://localhost:27018/?readPreference=primary&ssl=false',
},
},
'random-connection-4': {
id: 'random-connection-4',
name: 'localhost:27018',
storageLocation: 'GLOBAL',
secretStorageLocation: SecretStorageLocation.KeytarSecondAttempt,
connectionOptions: {
connectionString:
'mongodb://localhost:27018/?readPreference=primary&ssl=false',
},
},
} as any;
});
testSandbox.replace(
Expand All @@ -1824,10 +1844,10 @@ suite('Connection Controller Test Suite', function () {
assert.strictEqual(trackStub.calledOnce, true);
assert.deepStrictEqual(trackStub.lastCall.args, [
{
connections_with_secrets_in_keytar: 1,
connections_with_secrets_in_keytar: 2,
connections_with_secrets_in_secret_storage: 2,
saved_connections: 3,
loaded_connections: 3,
saved_connections: 4,
loaded_connections: 4,
},
]);
});
Expand Down

0 comments on commit d1ba961

Please sign in to comment.