Save username/password for BDC HDFS connections (#12667)

* Save username/password for BDC HDFS connections

* comment
This commit is contained in:
Charles Gagnon
2020-09-30 08:40:20 -07:00
committed by GitHub
parent 82648fab3e
commit b3578d058f
3 changed files with 18 additions and 9 deletions

View File

@@ -53,7 +53,8 @@ async function findSqlClusterConnectionBySqlConnProfile(sqlConnProfile: azdata.I
}
export async function getSqlClusterConnectionParams(
obj: azdata.IConnectionProfile | azdata.connection.Connection | ICommandObjectExplorerContext): Promise<ConnectionParam> {
obj: azdata.IConnectionProfile | azdata.connection.Connection | ICommandObjectExplorerContext,
appContext: AppContext): Promise<ConnectionParam> {
if (!obj) { return undefined; }
@@ -62,16 +63,16 @@ export async function getSqlClusterConnectionParams(
if (obj.providerName === constants.mssqlClusterProviderName) {
sqlClusterConnInfo = 'id' in obj ? connProfileToConnectionParam(obj) : connToConnectionParam(obj);
} else {
sqlClusterConnInfo = await createSqlClusterConnInfo(obj);
sqlClusterConnInfo = await createSqlClusterConnInfo(obj, appContext);
}
} else {
sqlClusterConnInfo = await createSqlClusterConnInfo(obj.explorerContext.connectionProfile);
sqlClusterConnInfo = await createSqlClusterConnInfo(obj.explorerContext.connectionProfile, appContext);
}
return sqlClusterConnInfo;
}
async function createSqlClusterConnInfo(sqlConnInfo: azdata.IConnectionProfile | azdata.connection.Connection): Promise<ConnectionParam> {
async function createSqlClusterConnInfo(sqlConnInfo: azdata.IConnectionProfile | azdata.connection.Connection, appContext: AppContext): Promise<ConnectionParam> {
if (!sqlConnInfo) { return undefined; }
let connectionId: string = 'id' in sqlConnInfo ? sqlConnInfo.id : sqlConnInfo.connectionId;
@@ -96,11 +97,19 @@ async function createSqlClusterConnInfo(sqlConnInfo: azdata.IConnectionProfile |
let authType = clusterConnInfo.options[constants.authenticationTypePropName] = sqlConnInfo.options[constants.authenticationTypePropName];
const controllerEndpoint = endpoints.find(ep => ep.name.toLowerCase() === 'controller');
if (authType && authType.toLowerCase() !== constants.integratedAuth) {
clusterConnInfo.options[constants.userPropName] = sqlConnInfo.options[constants.userPropName]; //should be the same user as sql master
clusterConnInfo.options[constants.passwordPropName] = credentials.password;
const usernameKey = `bdc.username::${connectionId}`;
const savedUsername = appContext.extensionContext.globalState.get(usernameKey);
const credentialProvider = await azdata.credentials.getProvider('mssql.bdc.password');
const savedPassword = (await credentialProvider.readCredential(connectionId)).password;
// If we don't have a previously saved username/password then use the SQL connection credentials as a best guess,
// if those don't work then we'll prompt the user for the info
clusterConnInfo.options[constants.userPropName] = savedUsername ?? sqlConnInfo.options[constants.userPropName];
clusterConnInfo.options[constants.passwordPropName] = savedPassword ?? credentials.password;
try {
clusterController = await getClusterController(controllerEndpoint.endpoint, clusterConnInfo);
// We've successfully connected so now store the username/password for future connections
appContext.extensionContext.globalState.update(usernameKey, clusterConnInfo.options[constants.userPropName]);
credentialProvider.saveCredential(connectionId, clusterConnInfo.options[constants.passwordPropName]);
clusterConnInfo.options[constants.userPropName] = await clusterController.getKnoxUsername(clusterConnInfo.options[constants.userPropName]);
} catch (err) {
console.log(`Unexpected error getting Knox username for SQL Cluster connection: ${err}`);