-
Notifications
You must be signed in to change notification settings - Fork 583
Expand file tree
/
Copy pathconnectionStore.ts
More file actions
815 lines (723 loc) · 30.6 KB
/
connectionStore.ts
File metadata and controls
815 lines (723 loc) · 30.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from "vscode";
import * as Constants from "../constants/constants";
import * as LocalizedConstants from "../constants/locConstants";
import * as ConnInfo from "./connectionInfo";
import * as Utils from "./utils";
import ValidationException from "../utils/validationException";
import { ConnectionCredentials } from "./connectionCredentials";
import {
IConnectionProfile,
IConnectionCredentialsQuickPickItem,
CredentialsQuickPickItemType,
AuthenticationTypes,
IConnectionProfileWithSource,
IConnectionGroup,
} from "./interfaces";
import { ICredentialStore, Credential } from "../credentialstore/icredentialstore";
import { ConnectionConfig } from "../connectionconfig/connectionconfig";
import VscodeWrapper from "../controllers/vscodeWrapper";
import { IConnectionInfo } from "vscode-mssql";
import { Logger } from "./logger";
import { Deferred } from "../protocol";
import { ConnectionMatcher, MatchScore } from "./utils";
import { sendActionEvent } from "../telemetry/telemetry";
import { TelemetryActions, TelemetryViews } from "../sharedInterfaces/telemetry";
/**
* Manages the connections list including saved profiles and the most recently used connections
*
* @export
* @class ConnectionStore
*/
export class ConnectionStore {
/**
* Map to store password for connections where savePassword is false for the session.
* For connections with savePassword = true, the password will be saved to the credential
* store.
*/
private _sessionPasswords: Map<string, string> = new Map();
constructor(
private _context: vscode.ExtensionContext,
private _credentialStore: ICredentialStore,
private _logger?: Logger,
private _connectionConfig?: ConnectionConfig,
private _vscodeWrapper?: VscodeWrapper,
) {
if (!this.vscodeWrapper) {
this.vscodeWrapper = new VscodeWrapper();
}
if (!this._logger) {
this._logger = Logger.create(this.vscodeWrapper.outputChannel, "ConnectionStore");
}
if (!this._connectionConfig) {
this._connectionConfig = new ConnectionConfig(this.vscodeWrapper);
}
}
public get initialized(): Deferred<void> {
return this._connectionConfig.initialized;
}
public static get CRED_PREFIX(): string {
return "Microsoft.SqlTools";
}
public static get CRED_SEPARATOR(): string {
return "|";
}
public static get CRED_SERVER_PREFIX(): string {
return "server:";
}
public static get CRED_DB_PREFIX(): string {
return "db:";
}
public static get CRED_USER_PREFIX(): string {
return "user:";
}
public static get CRED_ITEMTYPE_PREFIX(): string {
return "itemtype:";
}
public static get CRED_CONNECTION_STRING_PREFIX(): string {
return "isConnectionString:";
}
public static get CRED_PROFILE_PREFIX(): string {
return "profile_id:";
}
public static get CRED_PROFILE_USER(): string {
return CredentialsQuickPickItemType[CredentialsQuickPickItemType.Profile];
}
public static get CRED_MRU_USER(): string {
return CredentialsQuickPickItemType[CredentialsQuickPickItemType.Mru];
}
public static get shouldSavePasswordUntilRestart(): boolean {
return vscode.workspace
.getConfiguration()
.get<boolean>(Constants.configSavePasswordsUntilRestart);
}
public static formatCredentialIdForCred(
creds: IConnectionInfo,
itemType?: CredentialsQuickPickItemType,
): string {
if (Utils.isEmpty(creds)) {
throw new ValidationException("Missing Connection which is required");
}
let itemTypeString: string = ConnectionStore.CRED_PROFILE_USER;
if (itemType) {
itemTypeString = CredentialsQuickPickItemType[itemType];
}
// Use profile ID as key if available for saved profiles
const profile = creds as IConnectionProfile;
if (profile.id) {
return ConnectionStore.formatCredentialIdFromProfileId(profile.id, itemTypeString);
}
return ConnectionStore.formatCredentialId(
creds.server,
creds.database,
creds.user,
itemTypeString,
);
}
/**
* Creates a credential ID using profile ID instead of connection details
* @param profileId The connection profile ID
* @param itemType The item type string
* @returns formatted credential ID
*/
public static formatCredentialIdFromProfileId(
profileId: string,
itemType: string = ConnectionStore.CRED_PROFILE_USER,
): string {
let cred: string[] = [ConnectionStore.CRED_PREFIX];
ConnectionStore.pushIfNonEmpty(profileId, ConnectionStore.CRED_PROFILE_PREFIX, cred);
ConnectionStore.pushIfNonEmpty(itemType, ConnectionStore.CRED_ITEMTYPE_PREFIX, cred);
return cred.join(ConnectionStore.CRED_SEPARATOR);
}
/**
* Creates a formatted credential usable for uniquely identifying a SQL Connection.
* This string can be decoded but is not optimized for this.
* @param server name of the server - required
* @param database name of the database - optional
* @param user name of the user - optional
* @param itemType type of the item (MRU or Profile) - optional
* @returns formatted string with server, DB and username
*/
public static formatCredentialId(
server: string,
database?: string,
user?: string,
itemType?: string,
isConnectionString?: boolean,
): string {
if (Utils.isEmpty(server) && !isConnectionString) {
throw new ValidationException("Missing Server Name, which is required");
}
let cred: string[] = [ConnectionStore.CRED_PREFIX];
if (!itemType) {
itemType = ConnectionStore.CRED_PROFILE_USER;
}
ConnectionStore.pushIfNonEmpty(itemType, ConnectionStore.CRED_ITEMTYPE_PREFIX, cred);
ConnectionStore.pushIfNonEmpty(server, ConnectionStore.CRED_SERVER_PREFIX, cred);
ConnectionStore.pushIfNonEmpty(database, ConnectionStore.CRED_DB_PREFIX, cred);
ConnectionStore.pushIfNonEmpty(user, ConnectionStore.CRED_USER_PREFIX, cred);
if (isConnectionString) {
ConnectionStore.pushIfNonEmpty(
"true",
ConnectionStore.CRED_CONNECTION_STRING_PREFIX,
cred,
);
}
return cred.join(ConnectionStore.CRED_SEPARATOR);
}
public get connectionConfig(): ConnectionConfig {
return this._connectionConfig;
}
private static pushIfNonEmpty(value: string, prefix: string, arr: string[]): void {
if (Utils.isNotEmpty(value)) {
arr.push(prefix.concat(value));
}
}
private get vscodeWrapper(): VscodeWrapper {
return this._vscodeWrapper;
}
private set vscodeWrapper(value: VscodeWrapper) {
this._vscodeWrapper = value;
}
/**
* Load connections from MRU and profile list and return them as a formatted picklist.
* Note: connections will not include password value
*
* @returns
*/
public async getPickListItems(): Promise<IConnectionCredentialsQuickPickItem[]> {
let pickListItems: IConnectionCredentialsQuickPickItem[] =
await this.getConnectionQuickpickItems(false);
pickListItems.push(<IConnectionCredentialsQuickPickItem>{
label: `$(add) ${LocalizedConstants.CreateProfileFromConnectionsListLabel}`,
connectionCreds: undefined,
quickPickItemType: CredentialsQuickPickItemType.NewConnection,
});
return pickListItems;
}
/**
* Gets all connection profiles stored in the user settings
* Note: connections will not include password value
*
* @returns
*/
public async getProfilePickListItems(): Promise<IConnectionCredentialsQuickPickItem[]> {
return await this.loadProfiles();
}
public async addSavedPassword(
credentialsItem: IConnectionCredentialsQuickPickItem,
): Promise<IConnectionCredentialsQuickPickItem> {
let self = this;
if (
typeof credentialsItem.connectionCreds["savePassword"] === "undefined" ||
credentialsItem.connectionCreds["savePassword"] === false
) {
// Don't try to lookup a saved password if savePassword is set to false for the credential
return credentialsItem;
// Note that 'emptyPasswordInput' property is only present for connection profiles
} else if (
self.shouldLookupSavedPassword(<IConnectionProfile>credentialsItem.connectionCreds)
) {
let credentialId = ConnectionStore.formatCredentialIdForCred(
credentialsItem.connectionCreds,
credentialsItem.quickPickItemType,
);
const savedCred = await self._credentialStore.readCredential(credentialId);
if (savedCred) {
credentialsItem.connectionCreds.password = savedCred.password;
return credentialsItem;
} else {
throw new Error("No saved password found");
}
} else {
// Already have a password, no need to look up
return credentialsItem;
}
}
/**
* Lookup password in credential store.
* @param connectionCredentials Connection credentials of profile for password lookup
* @param isConnectionString Whether this is a connection string lookup
*/
public async lookupPassword(
connectionCredentials: IConnectionInfo,
isConnectionString: boolean = false,
): Promise<string> {
const profile = connectionCredentials as IConnectionProfile;
let savedCredential: Credential;
// Generate credential ID using profile's id (new format)
const credentialId = ConnectionStore.formatCredentialIdForCred(
profile,
CredentialsQuickPickItemType.Profile,
);
// First, try to get password from session storage for non-saved passwords
if (!profile.savePassword && ConnectionStore.shouldSavePasswordUntilRestart) {
const sessionPassword = this._sessionPasswords.get(credentialId);
if (sessionPassword) {
return sessionPassword;
}
}
// We try to read from the credential store with the new format id
savedCredential = await this._credentialStore.readCredential(credentialId);
if (savedCredential && savedCredential.password) {
sendActionEvent(TelemetryViews.Connection, TelemetryActions.LookupPassword, {
isNewFormat: true.toString(),
passwordFound: true.toString(),
});
return savedCredential.password;
}
// If no password was found, fallback to legacy format and see if credential exists
const legacyCredentialId = ConnectionStore.formatCredentialId(
connectionCredentials.server,
connectionCredentials.database,
connectionCredentials.user,
ConnectionStore.CRED_PROFILE_USER,
isConnectionString,
);
// We try to read from the credential store with the legacy format id
const legacyCredential = await this._credentialStore.readCredential(legacyCredentialId);
if (legacyCredential && legacyCredential.password) {
sendActionEvent(TelemetryViews.Connection, TelemetryActions.LookupPassword, {
isNewFormat: false.toString(),
passwordFound: true.toString(),
});
return legacyCredential.password;
}
// Send telemetry event if password was not found for connections with savePassword enabled
if (profile?.savePassword) {
sendActionEvent(TelemetryViews.Connection, TelemetryActions.LookupPassword, {
passwordFound: false.toString(),
});
}
return undefined;
}
/**
* Store password in session storage for connections that don't save passwords
* @param connectionCredentials Connection credentials
* @param password Password to store
*/
public storeSessionPassword(connectionCredentials: IConnectionInfo, password: string): void {
if (ConnectionStore.shouldSavePasswordUntilRestart) {
const sessionKey = ConnectionStore.formatCredentialIdForCred(connectionCredentials);
this._sessionPasswords.set(sessionKey, password);
}
}
/**
* public for testing purposes. Validates whether a password should be looked up from the credential store or not
*
* @param connectionCreds
* @returns
* @memberof ConnectionStore
*/
public shouldLookupSavedPassword(connectionCreds: IConnectionProfile): boolean {
if (ConnectionCredentials.isPasswordBasedCredential(connectionCreds)) {
// Only lookup if password isn't saved in the profile, and if it was not explicitly defined
// as a blank password
return Utils.isEmpty(connectionCreds.password) && !connectionCreds.emptyPasswordInput;
}
return false;
}
/**
* Saves a connection profile to the user settings.
* Password values are stored to a separate credential store if the "savePassword" option is true
*
* @param profile the profile to save
* @param whether the plaintext password should be written to the settings file
* @returns a Promise that returns the original profile, for help in chaining calls
*/
public async saveProfile(
profile: IConnectionProfile,
forceWritePlaintextPassword?: boolean,
): Promise<IConnectionProfile> {
this._connectionConfig.populateMissingConnectionMetadata(profile);
// Add the profile to the saved list, taking care to clear out the password field if necessary
let savedProfile: IConnectionProfile;
if (profile.authenticationType === Utils.authTypeToString(AuthenticationTypes.AzureMFA)) {
savedProfile = Object.assign({}, profile, {
azureAccountToken: "",
});
} else {
if (forceWritePlaintextPassword) {
savedProfile = Object.assign({}, profile);
} else {
savedProfile = Object.assign({}, profile, { password: "" });
}
}
await this._connectionConfig.addConnection(savedProfile);
if (await this.saveProfilePasswordIfNeeded(profile)) {
ConnInfo.fixupConnectionCredentials(profile);
}
return profile;
}
/**
* Gets the list of recently used connections. These will not include the password - a separate call to
* {addSavedPassword} is needed to fill that before connecting
*
* @returns the array of connections, empty if none are found
*/
public getRecentlyUsedConnections(limit?: number): IConnectionInfo[] {
let configValues = this._context.globalState.get<IConnectionInfo[]>(
Constants.configRecentConnections,
);
if (!configValues) {
configValues = [];
}
if (limit && limit > 0) {
return configValues.slice(0, limit);
}
return configValues;
}
/**
* Adds a connection to the recently used list.
* Password values are stored to a separate credential store if the "savePassword" option is true
*
* @param conn the connection to add
* @returns a Promise that returns when the connection was saved
*/
public addRecentlyUsed(conn: IConnectionInfo): Promise<void> {
const self = this;
return new Promise<void>((resolve, reject) => {
// Get all profiles
let configValues = self.getRecentlyUsedConnections();
let maxConnections = self.getMaxRecentConnectionsCount();
// Remove the connection from the list if it already exists
configValues = configValues.filter(
(value) =>
!self.isSameRecentConnectionEntry(
value as IConnectionProfile,
conn as IConnectionProfile,
),
);
// Add the connection to the front of the list, taking care to clear out the password field
let savedConn: IConnectionInfo = Object.assign({}, conn, {
password: "",
});
configValues.unshift(savedConn);
// Remove last element if needed
if (configValues.length > maxConnections) {
configValues = configValues.slice(0, maxConnections);
}
self._context.globalState.update(Constants.configRecentConnections, configValues).then(
async () => {
// Only save if we successfully added the profile and if savePassword
if ((<IConnectionProfile>conn).savePassword) {
await self.doSaveCredential(conn, CredentialsQuickPickItemType.Mru);
}
// And resolve / reject at the end of the process
resolve(undefined);
},
(err) => {
reject(err);
},
);
});
}
/**
* Clear all recently used connections from the MRU list.
* @returns a boolean value indicating whether the credentials were deleted successfully.
*/
public async clearRecentlyUsed(): Promise<boolean> {
// Get all recent connection profiles and delete the associated credentials.
const mruList = this.getRecentlyUsedConnections();
let deleteCredentialSuccess = true;
for (const connection of mruList) {
const credentialId = ConnectionStore.formatCredentialId(
connection.server,
connection.database,
connection.user,
ConnectionStore.CRED_MRU_USER,
);
try {
await this._credentialStore.deleteCredential(credentialId);
} catch (err) {
deleteCredentialSuccess = false;
this._logger.log(LocalizedConstants.deleteCredentialError, credentialId, err);
}
}
// Update the MRU list to be empty
await this._context.globalState.update(Constants.configRecentConnections, []);
return deleteCredentialSuccess;
}
/**
* Remove a connection profile from the recently used list.
* @param conn connection profile to remove
* @param keepCredentialStore Whether keep the credential store after a profile removal. Defaults to false.
*/
public removeRecentlyUsed(
conn: IConnectionProfile,
keepCredentialStore: boolean = false,
): Promise<void> {
const self = this;
return new Promise<void>(async (resolve, reject) => {
// Get all profiles
let configValues = self.getRecentlyUsedConnections();
// Remove the connection from the list if it already exists
configValues = configValues.filter(
(value) => !self.isSameRecentConnectionEntry(value as IConnectionProfile, conn),
);
// Remove any saved password
if (conn.savePassword && !keepCredentialStore) {
let credentialId = ConnectionStore.formatCredentialId(
conn.server,
conn.database,
conn.user,
ConnectionStore.CRED_MRU_USER,
);
await self._credentialStore.deleteCredential(credentialId);
}
// Update the MRU list
self._context.globalState.update(Constants.configRecentConnections, configValues).then(
() => {
// And resolve / reject at the end of the process
resolve(undefined);
},
(err) => {
reject(err);
},
);
});
}
public async saveProfilePasswordIfNeeded(profile: IConnectionProfile): Promise<boolean> {
return await this.doSaveCredential(profile, CredentialsQuickPickItemType.Profile);
}
public async saveProfileWithConnectionString(profile: IConnectionProfile): Promise<boolean> {
if (!profile.connectionString) {
return Promise.resolve(true);
}
return await this.doSaveCredential(profile, CredentialsQuickPickItemType.Profile, true);
}
private async doSaveCredential(
conn: IConnectionInfo,
type: CredentialsQuickPickItemType,
isConnectionString: boolean = false,
): Promise<boolean> {
let password = isConnectionString ? conn.connectionString : conn.password;
if (Utils.isEmpty(password)) {
return true;
}
const profile = conn as IConnectionProfile;
let legacyCredentialId = ConnectionStore.formatCredentialId(
profile.server,
profile.database,
profile.user,
ConnectionStore.CRED_PROFILE_USER,
);
if (profile.id) {
// Delete legacy credential
try {
await this._credentialStore.deleteCredential(legacyCredentialId);
} catch {
// Ignore errors for legacy cleanup
}
}
let credentialId = ConnectionStore.formatCredentialIdForCred(profile, type);
if (profile.savePassword) {
return await this._credentialStore.saveCredential(credentialId, password);
} else {
if (ConnectionStore.shouldSavePasswordUntilRestart) {
await this.storeSessionPassword(profile, password);
}
return true;
}
}
/**
* Removes a profile from the user settings and deletes any related password information
* from the credential store
*
* @param profile the profile to be removed
* @param keepCredentialStore Whether to keep the credential store after a profile removal. Defaults to false.
* @returns true if successful
*/
public async removeProfile(
profile: IConnectionProfile,
keepCredentialStore: boolean = false,
): Promise<boolean> {
let profileFound = await this._connectionConfig.removeConnection(profile);
if (profileFound) {
// Remove the profile from the recently used list if necessary
await this.removeRecentlyUsed(profile, keepCredentialStore);
// Now remove password from credential store. Currently do not care about status unless an error occurred
if (profile.savePassword === true && !keepCredentialStore) {
await this.deleteCredential(profile);
}
return profileFound;
}
}
private createQuickPickItem(
item: IConnectionInfo,
itemType: CredentialsQuickPickItemType,
): IConnectionCredentialsQuickPickItem {
return <IConnectionCredentialsQuickPickItem>{
label: ConnInfo.getSimpleConnectionDisplayName(item),
description: ConnInfo.getPicklistDescription(item),
detail: ConnInfo.getPicklistDetails(item),
connectionCreds: item,
quickPickItemType: itemType,
};
}
/**
* Deletes the password for a connection from the credential store
* @param profile Connection profile
*/
public async deleteCredential(profile: IConnectionProfile): Promise<void> {
let credentialId: string;
// Use profile ID if available
credentialId = ConnectionStore.formatCredentialIdForCred(profile);
await this._credentialStore.deleteCredential(credentialId);
// Also try to delete legacy format if using profile ID
if (profile.id) {
const legacyCredentialId = ConnectionStore.formatCredentialId(
profile.server,
profile.database,
profile.user,
ConnectionStore.CRED_PROFILE_USER,
);
try {
await this._credentialStore.deleteCredential(legacyCredentialId);
} catch {
// Ignore errors for legacy cleanup
}
}
}
/**
* Removes password from a saved profile and credential store
*/
public async removeProfilePassword(connection: IConnectionInfo): Promise<void> {
// if the password is saved in the credential store, remove it
let profile = connection as IConnectionProfile;
profile.password = "";
await this.saveProfile(profile);
}
public async readAllConnectionGroups(): Promise<IConnectionGroup[]> {
const groups = await this._connectionConfig.getGroups();
return groups;
}
public async getGroupForConnectionId(
connectionId: string,
): Promise<IConnectionGroup | undefined> {
const connProfile = await this._connectionConfig.getConnectionById(connectionId);
if (connProfile) {
return this._connectionConfig.getGroupById(connProfile.groupId);
}
return undefined;
}
public async readAllConnections(
includeRecentConnections: boolean = false,
recentConnectionsLimit?: number,
): Promise<IConnectionProfileWithSource[]> {
let connResults: IConnectionProfileWithSource[] = [];
const connections = await this._connectionConfig.getConnections();
const configConnections = connections.map((c) => {
const conn = c as IConnectionProfileWithSource;
conn.profileSource = CredentialsQuickPickItemType.Profile;
return conn;
});
connResults = connResults.concat(configConnections);
// Include recent connections, if specified
if (includeRecentConnections) {
const recentConnections = this.getRecentlyUsedConnections(recentConnectionsLimit).map(
(c) => {
const conn = c as IConnectionProfileWithSource;
conn.profileSource = CredentialsQuickPickItemType.Mru;
return conn;
},
);
connResults = connResults.concat(recentConnections);
}
// Deduplicate connections within the same source while allowing the same connection
// to appear in both the saved and MRU lists.
const uniqueConnections: IConnectionProfileWithSource[] = [];
let dupeCount = 0;
for (const conn of connResults) {
const isDuplicate = uniqueConnections.some(
(existingConn) =>
existingConn.profileSource === conn.profileSource &&
(conn.profileSource === CredentialsQuickPickItemType.Mru
? this.isSameRecentConnectionEntry(existingConn, conn)
: Utils.isSameProfile(existingConn, conn)),
);
if (!isDuplicate) {
uniqueConnections.push(conn);
} else {
dupeCount++;
this._logger.verbose(
`Duplicate connection ID found: ${conn.id}. Ignoring duplicate connection.`,
);
}
}
connResults = uniqueConnections;
let logMessage = `readAllConnections(): ${connResults.length} connections found`;
if (includeRecentConnections) {
logMessage += ` (${configConnections.length} from config, ${connResults.length - configConnections.length} from recent)`;
if (typeof recentConnectionsLimit === "number" && recentConnectionsLimit > 0) {
logMessage += `; recent limit ${recentConnectionsLimit}`;
}
} else {
logMessage += "; excluded recent";
}
if (dupeCount > 0) {
logMessage += `; ${dupeCount} duplicate connections ignored`;
}
this._logger.logDebug(logMessage);
return connResults;
}
/**
* Finds the closest-matching profile from the saved connections for the given partial connection profile
* @param connProfile partial connection profile to match against
* @returns closest-matching connection profile from the saved connections. If none is found, returns {score: MatchScore.NotMatch}
*/
public async findMatchingProfile(
connProfile: IConnectionProfile,
): Promise<{ profile: IConnectionProfile; score: MatchScore }> {
const savedConnections = await this.readAllConnections();
return ConnectionMatcher.findMatchingProfile(connProfile, savedConnections);
}
public async getConnectionQuickpickItems(
includeRecentConnections: boolean = false,
recentConnectionsLimit?: number,
): Promise<IConnectionCredentialsQuickPickItem[]> {
let output: IConnectionCredentialsQuickPickItem[] = [];
const connections = await this.readAllConnections(
includeRecentConnections,
recentConnectionsLimit,
);
output = connections.map((c) => {
return this.createQuickPickItem(c, c.profileSource);
});
return output;
}
private async loadProfiles(): Promise<IConnectionCredentialsQuickPickItem[]> {
let connections: IConnectionProfile[] = await this._connectionConfig.getConnections();
let quickPickItems = connections.map((c) =>
this.createQuickPickItem(c, CredentialsQuickPickItemType.Profile),
);
return quickPickItems;
}
public getMaxRecentConnectionsCount(): number {
let config = this._vscodeWrapper.getConfiguration(Constants.extensionConfigSectionName);
let maxConnections: number = config[Constants.configMaxRecentConnections];
if (typeof maxConnections !== "number" || maxConnections <= 0) {
maxConnections = 5;
}
return maxConnections;
}
private isSameRecentConnectionEntry(
currentProfile: IConnectionProfile,
expectedProfile: IConnectionProfile,
): boolean {
const currentRecentProfile = {
...currentProfile,
id: undefined,
profileName: undefined,
} as IConnectionProfile;
const expectedRecentProfile = {
...expectedProfile,
id: undefined,
profileName: undefined,
} as IConnectionProfile;
return Utils.isSameProfile(currentRecentProfile, expectedRecentProfile);
}
}