mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-16 10:58:30 -05:00
getCredentials returns credentials for any saved connection (#11029)
* getCredentials returns credentials for any saved connection * Update doc comment * Add tests * cleanup * cleanup
This commit is contained in:
2
src/sql/azdata.d.ts
vendored
2
src/sql/azdata.d.ts
vendored
@@ -132,7 +132,7 @@ declare module 'azdata' {
|
|||||||
export function getConnectionString(connectionId: string, includePassword: boolean): Thenable<string>;
|
export function getConnectionString(connectionId: string, includePassword: boolean): Thenable<string>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the credentials for an active connection
|
* Get the credentials for a connection
|
||||||
* @param connectionId The id of the connection
|
* @param connectionId The id of the connection
|
||||||
* @returns A dictionary containing the credentials as they would be included in the connection's options dictionary
|
* @returns A dictionary containing the credentials as they would be included in the connection's options dictionary
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -268,12 +268,12 @@ export interface IConnectionManagementService {
|
|||||||
removeConnectionProfileCredentials(profile: IConnectionProfile): IConnectionProfile;
|
removeConnectionProfileCredentials(profile: IConnectionProfile): IConnectionProfile;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the credentials for a connected connection profile, as they would appear in the options dictionary
|
* Get the credentials for a connection profile, as they would appear in the options dictionary
|
||||||
* @param profileId The id of the connection profile to get the password for
|
* @param profileId The id of the connection profile to get the password for
|
||||||
* @returns A dictionary containing the credentials as they would be included
|
* @returns A dictionary containing the credentials as they would be included
|
||||||
* in the connection profile's options dictionary, or undefined if the profile is not connected
|
* in the connection profile's options dictionary, or undefined if the profile was not found
|
||||||
*/
|
*/
|
||||||
getActiveConnectionCredentials(profileId: string): { [name: string]: string };
|
getConnectionCredentials(profileId: string): Promise<{ [name: string]: string }>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the ServerInfo for a connected connection profile
|
* Get the ServerInfo for a connected connection profile
|
||||||
|
|||||||
@@ -259,8 +259,8 @@ export class TestConnectionManagementService implements IConnectionManagementSer
|
|||||||
return undefined!;
|
return undefined!;
|
||||||
}
|
}
|
||||||
|
|
||||||
getActiveConnectionCredentials(profileId: string): { [name: string]: string } {
|
getConnectionCredentials(profileId: string): Promise<{ [name: string]: string }> {
|
||||||
return undefined!;
|
return Promise.resolve(undefined);
|
||||||
}
|
}
|
||||||
|
|
||||||
getServerInfo(profileId: string): azdata.ServerInfo {
|
getServerInfo(profileId: string): azdata.ServerInfo {
|
||||||
|
|||||||
@@ -121,7 +121,7 @@ export class MainThreadConnectionManagement extends Disposable implements MainTh
|
|||||||
}
|
}
|
||||||
|
|
||||||
public $getCredentials(connectionId: string): Thenable<{ [name: string]: string }> {
|
public $getCredentials(connectionId: string): Thenable<{ [name: string]: string }> {
|
||||||
return Promise.resolve(this._connectionManagementService.getActiveConnectionCredentials(connectionId));
|
return Promise.resolve(this._connectionManagementService.getConnectionCredentials(connectionId));
|
||||||
}
|
}
|
||||||
|
|
||||||
public $getServerInfo(connectionId: string): Thenable<azdata.ServerInfo> {
|
public $getServerInfo(connectionId: string): Thenable<azdata.ServerInfo> {
|
||||||
|
|||||||
@@ -1357,10 +1357,15 @@ export class ConnectionManagementService extends Disposable implements IConnecti
|
|||||||
return this._connectionStore.getProfileWithoutPassword(originalProfile);
|
return this._connectionStore.getProfileWithoutPassword(originalProfile);
|
||||||
}
|
}
|
||||||
|
|
||||||
public getActiveConnectionCredentials(profileId: string): { [name: string]: string } {
|
public async getConnectionCredentials(profileId: string): Promise<{ [name: string]: string }> {
|
||||||
let profile = find(this.getActiveConnections(), connectionProfile => connectionProfile.id === profileId);
|
let profile = find(this.getActiveConnections(), connectionProfile => connectionProfile.id === profileId);
|
||||||
if (!profile) {
|
if (!profile) {
|
||||||
return undefined;
|
// Couldn't find an active profile so try all profiles now - fetching the password if we found one
|
||||||
|
profile = find(this.getConnections(), connectionProfile => connectionProfile.id === profileId);
|
||||||
|
if (!profile) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
await this.addSavedPassword(profile);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find the password option for the connection provider
|
// Find the password option for the connection provider
|
||||||
|
|||||||
@@ -1199,16 +1199,57 @@ suite('SQL ConnectionManagementService tests', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('getActiveConnectionCredentials returns the credentials dictionary for a connection profile', () => {
|
test('getConnectionCredentials returns the credentials dictionary for an active connection profile', async () => {
|
||||||
let profile = assign({}, connectionProfile);
|
let profile = assign({}, connectionProfile);
|
||||||
profile.options = { password: profile.password };
|
profile.options = { password: profile.password };
|
||||||
profile.id = 'test_id';
|
profile.id = 'test_id';
|
||||||
connectionStatusManager.addConnection(profile, 'test_uri');
|
connectionStatusManager.addConnection(profile, 'test_uri');
|
||||||
(connectionManagementService as any)._connectionStatusManager = connectionStatusManager;
|
(connectionManagementService as any)._connectionStatusManager = connectionStatusManager;
|
||||||
let credentials = connectionManagementService.getActiveConnectionCredentials(profile.id);
|
let credentials = await connectionManagementService.getConnectionCredentials(profile.id);
|
||||||
assert.equal(credentials['password'], profile.options['password']);
|
assert.equal(credentials['password'], profile.options['password']);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('getConnectionCredentials returns the credentials dictionary for a recently used connection profile', async () => {
|
||||||
|
const test_password = 'test_password';
|
||||||
|
const profile = createConnectionProfile('test_id', '');
|
||||||
|
const connectionStoreMock = TypeMoq.Mock.ofType(ConnectionStore, TypeMoq.MockBehavior.Loose, new TestStorageService());
|
||||||
|
connectionStoreMock.setup(x => x.getRecentlyUsedConnections(undefined)).returns(() => {
|
||||||
|
return [profile];
|
||||||
|
});
|
||||||
|
connectionStoreMock.setup(x => x.addSavedPassword(TypeMoq.It.isAny())).returns(async () => {
|
||||||
|
profile.password = test_password;
|
||||||
|
return { profile: profile, savedCred: true };
|
||||||
|
});
|
||||||
|
const connectionManagementService = new ConnectionManagementService(connectionStoreMock.object, undefined, undefined, undefined, undefined, undefined, undefined, new TestCapabilitiesService(), undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, getBasicExtensionService());
|
||||||
|
assert.equal(profile.password, '', 'Profile should not have password initially');
|
||||||
|
assert.equal(profile.options['password'], '', 'Profile options should not have password initially');
|
||||||
|
let credentials = await connectionManagementService.getConnectionCredentials(profile.id);
|
||||||
|
assert.equal(credentials['password'], test_password);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('getConnectionCredentials returns the credentials dictionary for a saved connection profile', async () => {
|
||||||
|
const test_password = 'test_password';
|
||||||
|
const profile = createConnectionProfile('test_id', '');
|
||||||
|
const connectionStoreMock = TypeMoq.Mock.ofType(ConnectionStore, TypeMoq.MockBehavior.Loose, new TestStorageService());
|
||||||
|
const group1 = createConnectionGroup('group1');
|
||||||
|
group1.connections = [profile];
|
||||||
|
connectionStoreMock.setup(x => x.getRecentlyUsedConnections(undefined)).returns(() => {
|
||||||
|
return [];
|
||||||
|
});
|
||||||
|
connectionStoreMock.setup(x => x.getConnectionProfileGroups(TypeMoq.It.isAny(), undefined)).returns(() => {
|
||||||
|
return [group1];
|
||||||
|
});
|
||||||
|
connectionStoreMock.setup(x => x.addSavedPassword(TypeMoq.It.isAny())).returns(async () => {
|
||||||
|
profile.password = test_password;
|
||||||
|
return { profile: profile, savedCred: true };
|
||||||
|
});
|
||||||
|
const connectionManagementService = new ConnectionManagementService(connectionStoreMock.object, undefined, undefined, undefined, undefined, undefined, undefined, new TestCapabilitiesService(), undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, getBasicExtensionService());
|
||||||
|
assert.equal(profile.password, '', 'Profile should not have password initially');
|
||||||
|
assert.equal(profile.options['password'], '', 'Profile options should not have password initially');
|
||||||
|
let credentials = await connectionManagementService.getConnectionCredentials(profile.id);
|
||||||
|
assert.equal(credentials['password'], test_password);
|
||||||
|
});
|
||||||
|
|
||||||
test('getConnectionUriFromId returns a URI of an active connection with the given id', () => {
|
test('getConnectionUriFromId returns a URI of an active connection with the given id', () => {
|
||||||
let profile = assign({}, connectionProfile);
|
let profile = assign({}, connectionProfile);
|
||||||
profile.options = { password: profile.password };
|
profile.options = { password: profile.password };
|
||||||
@@ -1428,8 +1469,7 @@ test('clearRecentConnection and ConnectionsList should call connectionStore func
|
|||||||
assert(called);
|
assert(called);
|
||||||
});
|
});
|
||||||
|
|
||||||
function createConnectionProfile(id: string): ConnectionProfile {
|
function createConnectionProfile(id: string, password?: string): ConnectionProfile {
|
||||||
|
|
||||||
const capabilitiesService = new TestCapabilitiesService();
|
const capabilitiesService = new TestCapabilitiesService();
|
||||||
return new ConnectionProfile(capabilitiesService, {
|
return new ConnectionProfile(capabilitiesService, {
|
||||||
connectionName: 'newName',
|
connectionName: 'newName',
|
||||||
@@ -1438,7 +1478,7 @@ function createConnectionProfile(id: string): ConnectionProfile {
|
|||||||
serverName: 'testServerName',
|
serverName: 'testServerName',
|
||||||
databaseName: 'testDatabaseName',
|
databaseName: 'testDatabaseName',
|
||||||
authenticationType: Constants.integrated,
|
authenticationType: Constants.integrated,
|
||||||
password: 'test',
|
password: password ?? 'test',
|
||||||
userName: 'testUsername',
|
userName: 'testUsername',
|
||||||
groupId: undefined,
|
groupId: undefined,
|
||||||
providerName: Constants.mssqlProviderName,
|
providerName: Constants.mssqlProviderName,
|
||||||
|
|||||||
Reference in New Issue
Block a user