@@ -12,13 +12,7 @@ import { CONNECTION_STATUS } from './views/webview-app/extension-app-message-con
12
12
import { createLogger } from './logging' ;
13
13
import formatError from './utils/formatError' ;
14
14
import type LegacyConnectionModel from './views/webview-app/legacy/connection-model/legacy-connection-model' ;
15
- import type { SecretStorageLocationType } from './storage/storageController' ;
16
- import {
17
- StorageLocation ,
18
- SecretStorageLocation ,
19
- } from './storage/storageController' ;
20
15
import type { StorageController } from './storage' ;
21
- import { StorageVariables } from './storage' ;
22
16
import type { StatusView } from './views' ;
23
17
import type TelemetryService from './telemetry/telemetryService' ;
24
18
import LINKS from './utils/links' ;
@@ -27,11 +21,11 @@ import type {
27
21
ConnectionOptions as ConnectionOptionsFromLegacyDS ,
28
22
} from 'mongodb-data-service-legacy' ;
29
23
import {
30
- getConnectionTitle ,
31
24
extractSecrets ,
32
- mergeSecrets ,
33
25
convertConnectionModelToInfo ,
34
26
} from 'mongodb-data-service-legacy' ;
27
+ import type { LoadedConnection } from './storage/connectionStorage' ;
28
+ import { ConnectionStorage } from './storage/connectionStorage' ;
35
29
36
30
export function launderConnectionOptionTypeFromLegacyToCurrent (
37
31
opts : ConnectionOptionsFromLegacyDS
@@ -58,15 +52,6 @@ export enum ConnectionTypes {
58
52
CONNECTION_ID = 'CONNECTION_ID' ,
59
53
}
60
54
61
- export interface StoreConnectionInfo {
62
- id : string ; // Connection model id or a new uuid.
63
- name : string ; // Possibly user given name, not unique.
64
- storageLocation : StorageLocation ;
65
- secretStorageLocation ?: SecretStorageLocationType ;
66
- connectionOptions ?: ConnectionOptionsFromLegacyDS ;
67
- connectionModel ?: LegacyConnectionModel ;
68
- }
69
-
70
55
export enum NewConnectionType {
71
56
NEW_CONNECTION = 'NEW_CONNECTION' ,
72
57
SAVED_CONNECTION = 'SAVED_CONNECTION' ,
@@ -82,15 +67,6 @@ interface ConnectionQuickPicks {
82
67
data : { type : NewConnectionType ; connectionId ?: string } ;
83
68
}
84
69
85
- type StoreConnectionInfoWithConnectionOptions = StoreConnectionInfo &
86
- Required < Pick < StoreConnectionInfo , 'connectionOptions' > > ;
87
-
88
- type StoreConnectionInfoWithSecretStorageLocation = StoreConnectionInfo &
89
- Required < Pick < StoreConnectionInfo , 'secretStorageLocation' > > ;
90
-
91
- type LoadedConnection = StoreConnectionInfoWithConnectionOptions &
92
- StoreConnectionInfoWithSecretStorageLocation ;
93
-
94
70
export default class ConnectionController {
95
71
// This is a map of connection ids to their configurations.
96
72
// These connections can be saved on the session (runtime),
@@ -99,7 +75,7 @@ export default class ConnectionController {
99
75
[ connectionId : string ] : LoadedConnection ;
100
76
} = Object . create ( null ) ;
101
77
_activeDataService : DataService | null = null ;
102
- _storageController : StorageController ;
78
+ _connectionStorage : ConnectionStorage ;
103
79
_telemetryService : TelemetryService ;
104
80
105
81
private readonly _serviceName = 'mdb.vscode.savedConnections' ;
@@ -130,37 +106,19 @@ export default class ConnectionController {
130
106
telemetryService : TelemetryService ;
131
107
} ) {
132
108
this . _statusView = statusView ;
133
- this . _storageController = storageController ;
134
109
this . _telemetryService = telemetryService ;
110
+ this . _connectionStorage = new ConnectionStorage ( {
111
+ storageController,
112
+ } ) ;
135
113
}
136
114
137
115
async loadSavedConnections ( ) : Promise < void > {
138
- const globalAndWorkspaceConnections = Object . entries ( {
139
- ...this . _storageController . get (
140
- StorageVariables . GLOBAL_SAVED_CONNECTIONS ,
141
- StorageLocation . GLOBAL
142
- ) ,
143
- ...this . _storageController . get (
144
- StorageVariables . WORKSPACE_SAVED_CONNECTIONS ,
145
- StorageLocation . WORKSPACE
146
- ) ,
147
- } ) ;
148
-
149
- await Promise . all (
150
- globalAndWorkspaceConnections . map (
151
- async ( [ connectionId , connectionInfo ] ) => {
152
- const connectionInfoWithSecrets =
153
- await this . _getConnectionInfoWithSecrets ( connectionInfo ) ;
154
- if ( ! connectionInfoWithSecrets ) {
155
- return ;
156
- }
116
+ const loadedConnections = await this . _connectionStorage . loadConnections ( ) ;
157
117
158
- this . _connections [ connectionId ] = connectionInfoWithSecrets ;
159
- }
160
- )
161
- ) ;
118
+ for ( const connection of loadedConnections ) {
119
+ this . _connections [ connection . id ] = connection ;
120
+ }
162
121
163
- const loadedConnections = Object . values ( this . _connections ) ;
164
122
if ( loadedConnections . length ) {
165
123
this . eventEmitter . emit ( DataServiceEventTypes . CONNECTIONS_DID_CHANGE ) ;
166
124
}
@@ -179,61 +137,6 @@ export default class ConnectionController {
179
137
}); */
180
138
}
181
139
182
- // TODO: Move this into the connectionStorage.
183
- async _getConnectionInfoWithSecrets (
184
- connectionInfo : StoreConnectionInfo
185
- ) : Promise < LoadedConnection | undefined > {
186
- try {
187
- // We tried migrating this connection earlier but failed because Keytar was not
188
- // available. So we return simply the connection without secrets.
189
- if (
190
- connectionInfo . connectionModel ||
191
- ! connectionInfo . secretStorageLocation ||
192
- connectionInfo . secretStorageLocation === 'vscode.Keytar' ||
193
- connectionInfo . secretStorageLocation ===
194
- SecretStorageLocation . KeytarSecondAttempt
195
- ) {
196
- // We had migrations in VSCode for ~5 months. We drop the connections
197
- // that did not migrate.
198
- return undefined ;
199
- }
200
-
201
- const unparsedSecrets =
202
- ( await this . _storageController . getSecret ( connectionInfo . id ) ) ?? '' ;
203
-
204
- return this . _mergedConnectionInfoWithSecrets (
205
- connectionInfo as LoadedConnection ,
206
- unparsedSecrets
207
- ) ;
208
- } catch ( error ) {
209
- log . error ( 'Error while retrieving connection info' , error ) ;
210
- return undefined ;
211
- }
212
- }
213
-
214
- _mergedConnectionInfoWithSecrets (
215
- connectionInfo : LoadedConnection ,
216
- unparsedSecrets : string
217
- ) : LoadedConnection {
218
- if ( ! unparsedSecrets ) {
219
- return connectionInfo ;
220
- }
221
-
222
- const secrets = JSON . parse ( unparsedSecrets ) ;
223
- const connectionInfoWithSecrets = mergeSecrets (
224
- {
225
- id : connectionInfo . id ,
226
- connectionOptions : connectionInfo . connectionOptions ,
227
- } ,
228
- secrets
229
- ) ;
230
-
231
- return {
232
- ...connectionInfo ,
233
- connectionOptions : connectionInfoWithSecrets . connectionOptions ,
234
- } ;
235
- }
236
-
237
140
async connectWithURI ( ) : Promise < boolean > {
238
141
let connectionString : string | undefined ;
239
142
@@ -293,7 +196,7 @@ export default class ConnectionController {
293
196
) ;
294
197
295
198
try {
296
- const connectResult = await this . saveNewConnectionFromFormAndConnect (
199
+ const connectResult = await this . saveNewConnectionAndConnect (
297
200
{
298
201
id : uuidv4 ( ) ,
299
202
connectionOptions : {
@@ -333,52 +236,24 @@ export default class ConnectionController {
333
236
} ) ;
334
237
}
335
238
336
- private async _saveConnectionWithSecrets (
337
- newStoreConnectionInfoWithSecrets : LoadedConnection
338
- ) : Promise < LoadedConnection > {
339
- // We don't want to store secrets to disc.
340
- const { connectionInfo : safeConnectionInfo , secrets } = extractSecrets (
341
- newStoreConnectionInfoWithSecrets as ConnectionInfoFromLegacyDS
342
- ) ;
343
- const savedConnectionInfo = await this . _storageController . saveConnection ( {
344
- ...newStoreConnectionInfoWithSecrets ,
345
- connectionOptions : safeConnectionInfo . connectionOptions , // The connection info without secrets.
346
- } ) ;
347
- await this . _storageController . setSecret (
348
- savedConnectionInfo . id ,
349
- JSON . stringify ( secrets )
350
- ) ;
351
-
352
- return savedConnectionInfo ;
353
- }
354
-
355
- async saveNewConnectionFromFormAndConnect (
239
+ async saveNewConnectionAndConnect (
356
240
originalConnectionInfo : ConnectionInfoFromLegacyDS ,
357
241
connectionType : ConnectionTypes
358
242
) : Promise < ConnectionAttemptResult > {
359
- const name = getConnectionTitle ( originalConnectionInfo ) ;
360
- const newConnectionInfo = {
361
- id : originalConnectionInfo . id ,
362
- name,
363
- // To begin we just store it on the session, the storage controller
364
- // handles changing this based on user preference.
365
- storageLocation : StorageLocation . NONE ,
366
- secretStorageLocation : SecretStorageLocation . SecretStorage ,
367
- connectionOptions : originalConnectionInfo . connectionOptions ,
368
- } ;
243
+ const savedConnectionWithoutSecrets =
244
+ await this . _connectionStorage . saveNewConnection ( originalConnectionInfo ) ;
369
245
370
- const savedConnectionInfo = await this . _saveConnectionWithSecrets (
371
- newConnectionInfo
372
- ) ;
373
-
374
- this . _connections [ savedConnectionInfo . id ] = {
375
- ...savedConnectionInfo ,
246
+ this . _connections [ savedConnectionWithoutSecrets . id ] = {
247
+ ...savedConnectionWithoutSecrets ,
376
248
connectionOptions : originalConnectionInfo . connectionOptions , // The connection options with secrets.
377
249
} ;
378
250
379
- log . info ( 'Connect called to connect to instance' , savedConnectionInfo . name ) ;
251
+ log . info (
252
+ 'Connect called to connect to instance' ,
253
+ savedConnectionWithoutSecrets . name
254
+ ) ;
380
255
381
- return this . _connect ( savedConnectionInfo . id , connectionType ) ;
256
+ return this . _connect ( savedConnectionWithoutSecrets . id , connectionType ) ;
382
257
}
383
258
384
259
async _connectWithDataService (
@@ -571,15 +446,10 @@ export default class ConnectionController {
571
446
return true ;
572
447
}
573
448
574
- private async _removeSecretsFromKeychain ( connectionId : string ) {
575
- await this . _storageController . deleteSecret ( connectionId ) ;
576
- }
577
-
578
449
async removeSavedConnection ( connectionId : string ) : Promise < void > {
579
450
delete this . _connections [ connectionId ] ;
580
451
581
- await this . _removeSecretsFromKeychain ( connectionId ) ;
582
- this . _storageController . removeConnection ( connectionId ) ;
452
+ await this . _connectionStorage . removeConnection ( connectionId ) ;
583
453
584
454
this . eventEmitter . emit ( DataServiceEventTypes . CONNECTIONS_DID_CHANGE ) ;
585
455
}
@@ -680,7 +550,7 @@ export default class ConnectionController {
680
550
} ,
681
551
} ) ;
682
552
} catch ( e ) {
683
- throw new Error ( `An error occured parsing the connection name: ${ e } ` ) ;
553
+ throw new Error ( `An error occurred parsing the connection name: ${ e } ` ) ;
684
554
}
685
555
686
556
if ( ! inputtedConnectionName ) {
@@ -691,7 +561,7 @@ export default class ConnectionController {
691
561
this . eventEmitter . emit ( DataServiceEventTypes . CONNECTIONS_DID_CHANGE ) ;
692
562
this . eventEmitter . emit ( DataServiceEventTypes . ACTIVE_CONNECTION_CHANGED ) ;
693
563
694
- await this . _storageController . saveConnection (
564
+ await this . _connectionStorage . saveConnection (
695
565
this . _connections [ connectionId ]
696
566
) ;
697
567
@@ -725,7 +595,7 @@ export default class ConnectionController {
725
595
return this . _activeDataService !== null ;
726
596
}
727
597
728
- getSavedConnections ( ) : StoreConnectionInfo [ ] {
598
+ getSavedConnections ( ) : LoadedConnection [ ] {
729
599
return Object . values ( this . _connections ) ;
730
600
}
731
601
@@ -917,13 +787,10 @@ export default class ConnectionController {
917
787
} ,
918
788
} ,
919
789
...Object . values ( this . _connections )
920
- . sort (
921
- (
922
- connectionA : StoreConnectionInfo ,
923
- connectionB : StoreConnectionInfo
924
- ) => ( connectionA . name || '' ) . localeCompare ( connectionB . name || '' )
790
+ . sort ( ( connectionA : LoadedConnection , connectionB : LoadedConnection ) =>
791
+ ( connectionA . name || '' ) . localeCompare ( connectionB . name || '' )
925
792
)
926
- . map ( ( item : StoreConnectionInfo ) => ( {
793
+ . map ( ( item : LoadedConnection ) => ( {
927
794
label : item . name ,
928
795
data : {
929
796
type : NewConnectionType . SAVED_CONNECTION ,
0 commit comments