Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KXI-33084: Insights remote connections; KXI-33419: User switching; KXI-33420: Failed login #229

Merged
merged 9 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,6 @@
"moment-timezone": "^0.5.43",
"node-fetch": "^2.6.6",
"node-q": "^2.6.1",
"open": "^6.4.0",
"picomatch": "^2.3.1",
"semver": "^7.5.3",
"vscode-extension-telemetry": "^0.4.5",
Expand Down
10 changes: 1 addition & 9 deletions src/commands/serverCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,16 +341,8 @@ export async function removeConnection(viewItem: KdbNode): Promise<void> {
}

export async function connectInsights(viewItem: InsightsNode): Promise<void> {
if (env.remoteName === "ssh-remote") {
window.showErrorMessage(
"Connecting to a kdb Insights Enterprise server with a remote connection is not supported.",
""
);
return;
}

commands.executeCommand("kdb-results.focus");

ext.context.secrets.delete(viewItem.details.alias);
await getCurrentToken(viewItem.details.server, viewItem.details.alias);

ext.outputChannel.appendLine(
Expand Down
62 changes: 52 additions & 10 deletions src/services/kdbInsights/codeFlowLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ import axios from "axios";
import * as crypto from "crypto";
import * as fs from "fs-extra";
import * as http from "http";
import open from "open";
ecmel marked this conversation as resolved.
Show resolved Hide resolved
import { join } from "path";
import * as querystring from "querystring";
import * as url from "url";
import { ext } from "../../extensionVariables";
import { Uri, env } from "vscode";
import { Socket } from "net";

interface IDeferred<T> {
resolve: (result: T | Promise<T>) => void;
Expand All @@ -41,8 +42,32 @@ const commonRequestParams = {
redirect_uri: ext.insightsAuthUrls.callbackURL,
};

let flow: { server: http.Server; codePromise: Promise<string> } | undefined;

export async function signIn(insightsUrl: string) {
const { server, codePromise } = createServer();
if (flow) {
flow.server.close();
}
flow = createServer();

const { server, codePromise } = flow;
const sockets: Socket[] = [];

server.on("connection", (socket) => {
sockets.push(socket);
socket.on("close", () => {
const index = sockets.indexOf(socket);
if (index > -1) {
sockets.splice(index, 1);
}
});
});

server.on("close", () => {
for (const socket of sockets) {
socket.destroy();
}
});

try {
await startServer(server);
Expand All @@ -60,15 +85,21 @@ export async function signIn(insightsUrl: string) {

authorizationUrl.search = queryString(authParams);

await open(authorizationUrl.toString());
const opened = await env.openExternal(
Uri.parse(authorizationUrl.toString())
);

const code = await codePromise;
if (opened) {
const code = await codePromise;
return await getToken(insightsUrl, code);
}

return await getToken(insightsUrl, code);
} catch (error) {
throw error;
throw new Error("Error opening url");
} finally {
setTimeout(() => server.close(), closeTimeout);
setImmediate(() => {
server.close();
flow = undefined;
});
}
}

Expand Down Expand Up @@ -150,7 +181,11 @@ async function tokenRequest(
): Promise<IToken | undefined> {
const queryParams = queryString(params);
const headers = {
headers: { "Content-Type": "application/x-www-form-urlencoded" },
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
timeout: closeTimeout,
signal: AbortSignal.timeout(closeTimeout),
};

const requestUrl = new url.URL(ext.insightsAuthUrls.tokenURL, insightsUrl);
Expand Down Expand Up @@ -278,7 +313,14 @@ function startServer(server: http.Server): Promise<number> {
server.listen(9010, ext.localhost);

portPromise.then(cancelPortTimer, cancelPortTimer);
return portPromise;

return new Promise((resolve) => {
env
.asExternalUri(
Uri.parse(`${ext.networkProtocols.http}${ext.localhost}:9010`)
)
.then(() => resolve(portPromise));
});
}

function sendFile(
Expand Down
16 changes: 15 additions & 1 deletion test/suite/services.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import axios from "axios";
import assert from "node:assert";
import sinon from "sinon";
import { TreeItemCollapsibleState } from "vscode";
import { TreeItemCollapsibleState, env } from "vscode";
import { ext } from "../../src/extensionVariables";
import { Insights } from "../../src/models/insights";
import { QueryHistory } from "../../src/models/queryHistory";
Expand All @@ -37,6 +37,7 @@ import {
QueryHistoryProvider,
QueryHistoryTreeItem,
} from "../../src/services/queryHistoryProvider";
import * as http from "http";

// eslint-disable-next-line @typescript-eslint/no-var-requires
const codeFlow = require("../../src/services/kdbInsights/codeFlowLogin");
Expand Down Expand Up @@ -577,6 +578,19 @@ describe("Code flow login service tests", () => {
"Should return undefined when server alias is empty."
);
});

it("Should not sign in if link is not opened", async () => {
sinon.stub(env, "openExternal").value(async () => false);
await assert.rejects(() => signIn("http://127.0.0.1"));
});

it("Should not sign in in case of error", async () => {
sinon.stub(env, "openExternal").value(async () => true);
setTimeout(async () => {
await axios.get("http://127.0.0.1:9010/redirect?error=1");
}, 500);
await assert.rejects(() => signIn("http://127.0.0.1"));
});
});

describe("queryHistoryProvider", () => {
Expand Down