Skip to content

Commit

Permalink
Merge pull request #160 from KxSystems/KXI-31894
Browse files Browse the repository at this point in the history
KXI-31894 User workflow changes for the 'create connection' option
  • Loading branch information
Philip-Carneiro-KX authored Oct 24, 2023
2 parents e9a8bd2 + 5af8fa4 commit 9dd0ff1
Show file tree
Hide file tree
Showing 7 changed files with 377 additions and 109 deletions.
33 changes: 31 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
"onCommand:kdb.removeConnection",
"onCommand:kdb.connect",
"onCommand:kdb.disconnect",
"onCommand:kdb.addAuthentication",
"onCommand:kdb.enableTLS",
"onCommand:kdb.dataSource.addDataSource",
"onCommand:kdb.dataSource.saveDataSource",
"onCommand:kdb.dataSource.runDataSource",
Expand Down Expand Up @@ -205,6 +207,15 @@
"command": "kdb.connect",
"title": "Connect server"
},
{
"command": "kdb.addAuthentication",
"title": "Add Authentication",
"position": "end"
},
{
"command": "kdb.enableTLS",
"title": "Enable TLS"
},
{
"command": "kdb.insightsConnect",
"title": "Connect to Insights"
Expand Down Expand Up @@ -416,6 +427,14 @@
{
"command": "kdb.disconnect",
"when": "false"
},
{
"command": "kdb.addAuthentication",
"when": "false"
},
{
"command": "kdb.enableTLS",
"when": "false"
}
],
"webview/context": [
Expand Down Expand Up @@ -465,7 +484,17 @@
{
"command": "kdb.connect",
"when": "view == kdb-servers && viewItem not in kdb.connected && viewItem in kdb.rootNodes",
"group": "connection"
"group": "connection@1"
},
{
"command": "kdb.addAuthentication",
"when": "view == kdb-servers && viewItem not in kdb.insightsNodes && viewItem in kdb.kdbNodesWithoutAuth",
"group": "connection@3"
},
{
"command": "kdb.enableTLS",
"when": "view == kdb-servers && viewItem not in kdb.insightsNodes && viewItem in kdb.kdbNodesWithoutTls",
"group": "connection@4"
},
{
"command": "kdb.insightsConnect",
Expand All @@ -485,7 +514,7 @@
{
"command": "kdb.removeConnection",
"when": "view == kdb-servers && viewItem in kdb.rootNodes",
"group": "connection"
"group": "connection@2"
},
{
"command": "kdb.startLocalProcess",
Expand Down
229 changes: 123 additions & 106 deletions src/commands/serverCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,81 @@ export async function addInsightsConnection() {
});
}

// Not possible to test secrets
/* istanbul ignore next */
export function addAuthConnection(serverKey: string): void {
const connectionUsername: InputBoxOptions = {
prompt: connectionUsernameInput.prompt,
placeHolder: connectionUsernameInput.placeholder,
validateInput: (value: string | undefined) => validateServerUsername(value),
};
const connectionPassword: InputBoxOptions = {
prompt: connectionPasswordInput.prompt,
placeHolder: connectionPasswordInput.placeholder,
password: true,
};
window.showInputBox(connectionUsername).then(async (username) => {
if (username?.trim()?.length) {
window.showInputBox(connectionPassword).then(async (password) => {
if (password?.trim()?.length) {
const servers: Server | undefined = getServers();
// store secrets
if (
(username != undefined || username != "") &&
(password != undefined || password != "") &&
servers &&
servers[serverKey]
) {
servers[serverKey].auth = true;
ext.secretSettings.storeAuthData(
serverKey,
`${username}:${password}`
);
await updateServers(servers);
const newServers = getServers();
if (newServers != undefined) {
ext.serverProvider.refresh(newServers);
}
}
}
});
}
});
}

export async function enableTLS(serverKey: string): Promise<void> {
const servers: Server | undefined = getServers();

// validate if TLS is possible
if (ext.openSslVersion === null) {
window
.showErrorMessage(
"OpenSSL not found, please ensure this is installed",
"More Info",
"Cancel"
)
.then(async (result) => {
if (result === "More Info") {
await openUrl("https://code.kx.com/q/kb/ssl/");
}
});
return;
}
if (servers && servers[serverKey]) {
servers[serverKey].tls = true;
await updateServers(servers);
const newServers = getServers();
if (newServers != undefined) {
ext.serverProvider.refresh(newServers);
}
return;
}
window.showErrorMessage(
"Server not found, please ensure this is a correct server",
"Cancel"
);
}

export function addKdbConnection(): void {
const connectionAlias: InputBoxOptions = {
prompt: connectionAliasInput.prompt,
Expand All @@ -163,16 +238,6 @@ export function addKdbConnection(): void {
placeHolder: connectionPortInput.placeholder,
validateInput: (value: string | undefined) => validateServerPort(value),
};
const connectionUsername: InputBoxOptions = {
prompt: connectionUsernameInput.prompt,
placeHolder: connectionUsernameInput.placeholder,
validateInput: (value: string | undefined) => validateServerUsername(value),
};
const connectionPassword: InputBoxOptions = {
prompt: connectionPasswordInput.prompt,
placeHolder: connectionPasswordInput.placeholder,
password: true,
};

const connectionTls: InputBoxOptions = {
prompt: connnectionTls.prompt,
Expand All @@ -185,102 +250,54 @@ export function addKdbConnection(): void {
if (hostname) {
window.showInputBox(connectionPort).then(async (port) => {
if (port) {
window.showInputBox(connectionUsername).then(async (username) => {
window.showInputBox(connectionPassword).then(async (password) => {
window.showInputBox(connectionTls).then(async (tls) => {
let tlsEnabled;
if (tls !== undefined && tls === "true") {
tlsEnabled = true;
} else {
tlsEnabled = false;
}

// validate if TLS is possible
if (tlsEnabled && ext.openSslVersion === null) {
window
.showErrorMessage(
"OpenSSL not found, please ensure this is installed",
"More Info",
"Cancel"
)
.then(async (result) => {
if (result === "More Info") {
await openUrl("https://code.kx.com/q/kb/ssl/");
}
});
return;
}

// store secrets
let authUsed = false;
if (
(username != undefined || username != "") &&
(password != undefined || password != "")
) {
authUsed = true;
ext.secretSettings.storeAuthData(
alias !== undefined
? getHash(`${hostname}${port}${alias}`)
: getHash(`${hostname}${port}`),
`${username}:${password}`
);
}

let servers: Server | undefined = getServers();

if (
servers != undefined &&
servers[getHash(`${hostname}:${port}`)]
) {
await window.showErrorMessage(
`Server ${hostname}:${port} already exists.`
);
} else {
const key =
alias != undefined
? getHash(`${hostname}${port}${alias}`)
: getHash(`${hostname}${port}`);
if (servers === undefined) {
servers = {
key: {
auth: authUsed,
serverName: hostname,
serverPort: port,
serverAlias: alias,
managed: alias === "local" ? true : false,
tls: tlsEnabled,
},
};
if (servers[0].managed) {
await addLocalConnectionContexts(
getServerName(servers[0])
);
}
} else {
servers[key] = {
auth: authUsed,
serverName: hostname,
serverPort: port,
serverAlias: alias,
managed: alias === "local" ? true : false,
tls: tlsEnabled,
};
if (servers[key].managed) {
await addLocalConnectionContexts(
getServerName(servers[key])
);
}
}

await updateServers(servers);
const newServers = getServers();
if (newServers != undefined) {
ext.serverProvider.refresh(newServers);
}
}
});
});
});
let servers: Server | undefined = getServers();

if (
servers != undefined &&
servers[getHash(`${hostname}:${port}`)]
) {
await window.showErrorMessage(
`Server ${hostname}:${port} already exists.`
);
} else {
const key =
alias != undefined
? getHash(`${hostname}${port}${alias}`)
: getHash(`${hostname}${port}`);
if (servers === undefined) {
servers = {
key: {
auth: false,
serverName: hostname,
serverPort: port,
serverAlias: alias,
managed: alias === "local" ? true : false,
tls: false,
},
};
if (servers[0].managed) {
await addLocalConnectionContexts(getServerName(servers[0]));
}
} else {
servers[key] = {
auth: false,
serverName: hostname,
serverPort: port,
serverAlias: alias,
managed: alias === "local" ? true : false,
tls: false,
};
if (servers[key].managed) {
await addLocalConnectionContexts(getServerName(servers[key]));
}
}

await updateServers(servers);
const newServers = getServers();
if (newServers != undefined) {
ext.serverProvider.refresh(newServers);
}
}
}
});
}
Expand Down
14 changes: 14 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ import {
stopLocalProcessByServerName,
} from "./commands/installTools";
import {
addAuthConnection,
addNewConnection,
connect,
connectInsights,
disconnect,
enableTLS,
removeConnection,
removeInsightsConnection,
runQuery,
Expand Down Expand Up @@ -154,6 +156,18 @@ export async function activate(context: ExtensionContext) {
commands.registerCommand("kdb.connect", async (viewItem: KdbNode) => {
await connect(viewItem);
}),

commands.registerCommand(
"kdb.addAuthentication",
async (viewItem: KdbNode) => {
addAuthConnection(viewItem.children[0]);
}
),

commands.registerCommand("kdb.enableTLS", async (viewItem: KdbNode) => {
await enableTLS(viewItem.children[0]);
}),

commands.registerCommand(
"kdb.insightsConnect",
async (viewItem: InsightsNode) => {
Expand Down
2 changes: 2 additions & 0 deletions src/extensionVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export namespace ext {
export const kdbQueryHistoryList: QueryHistory[] = [];
export const kdbrootNodes: string[] = [];
export const kdbinsightsNodes: string[] = [];
export const kdbNodesWithoutAuth: string[] = [];
export const kdbNodesWithoutTls: string[] = [];
export const maxRetryCount = 5;

export let secretSettings: AuthSettings;
Expand Down
Loading

0 comments on commit 9dd0ff1

Please sign in to comment.