Skip to content

Commit d1ba961

Browse files
fix: check secretStorageLocation instead of storageLocation (#583)
* fix: check secretStorageLocation instead of storageLocation * feat: perform a second attempt to migrate connections
1 parent 801c9b7 commit d1ba961

File tree

3 files changed

+49
-19
lines changed

3 files changed

+49
-19
lines changed

src/connectionController.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ export default class ConnectionController {
164164
(ids, [connectionId, connectionInfo]) => {
165165
if (
166166
connectionInfo.secretStorageLocation ===
167-
SecretStorageLocation.Keytar
167+
SecretStorageLocation.KeytarSecondAttempt
168168
) {
169169
return [...ids, connectionId];
170170
}
@@ -175,7 +175,9 @@ export default class ConnectionController {
175175

176176
const hasConnectionsThatDidNotMigrateEarlier =
177177
!!globalAndWorkspaceConnections.some(
178-
([, connectionInfo]) => !connectionInfo.storageLocation
178+
([, connectionInfo]) =>
179+
!connectionInfo.secretStorageLocation ||
180+
connectionInfo.secretStorageLocation === 'vscode.Keytar'
179181
);
180182

181183
if (hasConnectionsThatDidNotMigrateEarlier) {
@@ -203,7 +205,7 @@ export default class ConnectionController {
203205
this._connections[connectionId] = connectionInfoWithSecrets;
204206
const connectionSecretsInKeytar =
205207
connectionInfoWithSecrets.secretStorageLocation ===
206-
SecretStorageLocation.Keytar;
208+
SecretStorageLocation.KeytarSecondAttempt;
207209
if (
208210
connectionSecretsInKeytar &&
209211
!connectionIdsThatDidNotMigrateEarlier.includes(connectionId)
@@ -229,7 +231,9 @@ export default class ConnectionController {
229231
loaded_connections: loadedConnections.length,
230232
connections_with_secrets_in_keytar: loadedConnections.filter(
231233
(connection) =>
232-
connection.secretStorageLocation === SecretStorageLocation.Keytar
234+
connection.secretStorageLocation === SecretStorageLocation.Keytar ||
235+
connection.secretStorageLocation ===
236+
SecretStorageLocation.KeytarSecondAttempt
233237
).length,
234238
connections_with_secrets_in_secret_storage: loadedConnections.filter(
235239
(connection) =>
@@ -265,7 +269,10 @@ export default class ConnectionController {
265269
);
266270
}
267271

268-
if (!connectionInfo.secretStorageLocation) {
272+
if (
273+
!connectionInfo.secretStorageLocation ||
274+
connectionInfo.secretStorageLocation === 'vscode.Keytar'
275+
) {
269276
return await this._migrateConnectionWithKeytarSecrets(
270277
connectionInfo as StoreConnectionInfoWithConnectionOptions
271278
);
@@ -274,7 +281,8 @@ export default class ConnectionController {
274281
// We tried migrating this connection earlier but failed because Keytar was not
275282
// available. So we return simply the connection without secrets.
276283
if (
277-
connectionInfo.secretStorageLocation === SecretStorageLocation.Keytar
284+
connectionInfo.secretStorageLocation ===
285+
SecretStorageLocation.KeytarSecondAttempt
278286
) {
279287
return connectionInfo as MigratedStoreConnectionInfoWithConnectionOptions;
280288
}
@@ -322,7 +330,7 @@ export default class ConnectionController {
322330
return await this._storageController.saveConnection<MigratedStoreConnectionInfoWithConnectionOptions>(
323331
{
324332
...savedConnectionInfo,
325-
secretStorageLocation: SecretStorageLocation.Keytar,
333+
secretStorageLocation: SecretStorageLocation.KeytarSecondAttempt,
326334
}
327335
);
328336
}

src/storage/storageController.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ export type ConnectionsFromStorage = {
3434

3535
export const SecretStorageLocation = {
3636
Keytar: 'vscode.Keytar',
37+
KeytarSecondAttempt: 'vscode.KeytarSecondAttempt',
3738
SecretStorage: 'vscode.SecretStorage',
3839
} as const;
3940

4041
export type SecretStorageLocationType =
4142
| typeof SecretStorageLocation.Keytar
43+
| typeof SecretStorageLocation.KeytarSecondAttempt
4244
| typeof SecretStorageLocation.SecretStorage;
4345

4446
interface StorageVariableContents {

src/test/suite/connectionController.test.ts

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,7 +1464,7 @@ suite('Connection Controller Test Suite', function () {
14641464
);
14651465
assert.strictEqual(
14661466
updatedConnection.secretStorageLocation,
1467-
SecretStorageLocation.Keytar
1467+
SecretStorageLocation.KeytarSecondAttempt
14681468
);
14691469
});
14701470

@@ -1661,6 +1661,16 @@ suite('Connection Controller Test Suite', function () {
16611661
},
16621662
'random-connection-2': {
16631663
id: 'random-connection-2',
1664+
name: 'localhost:27017',
1665+
storageLocation: 'GLOBAL',
1666+
secretStorageLocation: SecretStorageLocation.KeytarSecondAttempt,
1667+
connectionOptions: {
1668+
connectionString:
1669+
'mongodb://localhost:27017/?readPreference=primary&ssl=false',
1670+
},
1671+
},
1672+
'random-connection-3': {
1673+
id: 'random-connection-3',
16641674
name: 'localhost:27018',
16651675
storageLocation: 'GLOBAL',
16661676
connectionOptions: {
@@ -1676,7 +1686,7 @@ suite('Connection Controller Test Suite', function () {
16761686
(connectionInfo) =>
16771687
Promise.resolve({
16781688
...connectionInfo,
1679-
secretStorageLocation: SecretStorageLocation.Keytar,
1689+
secretStorageLocation: SecretStorageLocation.KeytarSecondAttempt,
16801690
} as any)
16811691
);
16821692
const trackStub = testSandbox.stub(
@@ -1691,16 +1701,16 @@ suite('Connection Controller Test Suite', function () {
16911701
// Notified to user
16921702
assert.strictEqual(showInformationMessageStub.calledOnce, true);
16931703
assert.deepStrictEqual(showInformationMessageStub.lastCall.args, [
1694-
keytarMigrationFailedMessage(1),
1704+
keytarMigrationFailedMessage(2),
16951705
]);
16961706

16971707
// Tracked
16981708
assert.strictEqual(trackStub.calledOnce, true);
16991709
assert.deepStrictEqual(trackStub.lastCall.args, [
17001710
{
1701-
saved_connections: 2,
1702-
loaded_connections: 2,
1703-
connections_with_failed_keytar_migration: 1,
1711+
saved_connections: 3,
1712+
loaded_connections: 3,
1713+
connections_with_failed_keytar_migration: 2,
17041714
},
17051715
]);
17061716
});
@@ -1719,7 +1729,7 @@ suite('Connection Controller Test Suite', function () {
17191729
id: 'random-connection-1',
17201730
name: 'localhost:27017',
17211731
storageLocation: 'GLOBAL',
1722-
secretStorageLocation: SecretStorageLocation.Keytar,
1732+
secretStorageLocation: SecretStorageLocation.KeytarSecondAttempt,
17231733
connectionOptions: {
17241734
connectionString:
17251735
'mongodb://localhost:27017/?readPreference=primary&ssl=false',
@@ -1729,7 +1739,7 @@ suite('Connection Controller Test Suite', function () {
17291739
id: 'random-connection-2',
17301740
name: 'localhost:27018',
17311741
storageLocation: 'GLOBAL',
1732-
secretStorageLocation: SecretStorageLocation.Keytar,
1742+
secretStorageLocation: SecretStorageLocation.KeytarSecondAttempt,
17331743
connectionOptions: {
17341744
connectionString:
17351745
'mongodb://localhost:27018/?readPreference=primary&ssl=false',
@@ -1743,7 +1753,7 @@ suite('Connection Controller Test Suite', function () {
17431753
(connectionInfo) =>
17441754
Promise.resolve({
17451755
...connectionInfo,
1746-
secretStorageLocation: SecretStorageLocation.Keytar,
1756+
secretStorageLocation: SecretStorageLocation.KeytarSecondAttempt,
17471757
} as any)
17481758
);
17491759
const trackStub = testSandbox.stub(
@@ -1802,6 +1812,16 @@ suite('Connection Controller Test Suite', function () {
18021812
'mongodb://localhost:27018/?readPreference=primary&ssl=false',
18031813
},
18041814
},
1815+
'random-connection-4': {
1816+
id: 'random-connection-4',
1817+
name: 'localhost:27018',
1818+
storageLocation: 'GLOBAL',
1819+
secretStorageLocation: SecretStorageLocation.KeytarSecondAttempt,
1820+
connectionOptions: {
1821+
connectionString:
1822+
'mongodb://localhost:27018/?readPreference=primary&ssl=false',
1823+
},
1824+
},
18051825
} as any;
18061826
});
18071827
testSandbox.replace(
@@ -1824,10 +1844,10 @@ suite('Connection Controller Test Suite', function () {
18241844
assert.strictEqual(trackStub.calledOnce, true);
18251845
assert.deepStrictEqual(trackStub.lastCall.args, [
18261846
{
1827-
connections_with_secrets_in_keytar: 1,
1847+
connections_with_secrets_in_keytar: 2,
18281848
connections_with_secrets_in_secret_storage: 2,
1829-
saved_connections: 3,
1830-
loaded_connections: 3,
1849+
saved_connections: 4,
1850+
loaded_connections: 4,
18311851
},
18321852
]);
18331853
});

0 commit comments

Comments
 (0)