mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Restructure to monorepo (#6907)
* wip: restructure to monorepo Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * refactor create-release-pr to a package Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * build fixes Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * github workflow fixes Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix typo Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * add webpack-env types to core Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix github workflows Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * refactor/fix integration tests Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * lint fix Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * yarn run dev Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * eslint settings for vscode Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * move templates to right package Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * open-lens build fixes Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * integration test fix Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix nx task dependencies Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * use bash shell for unit tests in test workflow Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix test:unit for windows Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix win-ca webpack error in open-lens Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix win-ca webpack error in open-lens Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix build:app on windows Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * remove ELECTRON_BUILDER_EXTRA_ARGS Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * sync src/ from master Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * remove Makefile from core Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
1 parent
0066ca4ea2
commit
2657df2293
3321 files changed
+28699
-11447
No files matched your search
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import getClusterByIdInjectable from "../../common/cluster-store/get-by-id.injectable";
|
||||
import { getClusterIdFromHost } from "../../common/utils";
|
||||
import { apiKubePrefix } from "../../common/vars";
|
||||
import type { GetClusterForRequest } from "./lens-proxy";
|
||||
|
||||
const getClusterForRequestInjectable = getInjectable({
|
||||
id: "get-cluster-for-request",
|
||||
instantiate: (di): GetClusterForRequest => {
|
||||
const getClusterById = di.inject(getClusterByIdInjectable);
|
||||
|
||||
return (req) => {
|
||||
if (!req.headers.host) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// lens-server is connecting to 127.0.0.1:<port>/<uid>
|
||||
if (req.url && req.headers.host.startsWith("127.0.0.1")) {
|
||||
const clusterId = req.url.split("/")[1];
|
||||
const cluster = getClusterById(clusterId);
|
||||
|
||||
if (cluster) {
|
||||
// we need to swap path prefix so that request is proxied to kube api
|
||||
req.url = req.url.replace(`/${clusterId}`, apiKubePrefix);
|
||||
}
|
||||
|
||||
return cluster;
|
||||
}
|
||||
|
||||
const clusterId = getClusterIdFromHost(req.headers.host);
|
||||
|
||||
if (!clusterId) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return getClusterById(clusterId);
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export default getClusterForRequestInjectable;
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import type { SelfSignedCert } from "selfsigned";
|
||||
import { lensProxyCertificateChannel } from "../../common/certificate/lens-proxy-certificate-channel";
|
||||
import { getRequestChannelListenerInjectable } from "../utils/channel/channel-listeners/listener-tokens";
|
||||
import lensProxyCertificateInjectable from "../../common/certificate/lens-proxy-certificate.injectable";
|
||||
|
||||
const lensProxyCertificateRequestHandlerInjectable = getRequestChannelListenerInjectable({
|
||||
channel: lensProxyCertificateChannel,
|
||||
handler: (di) => {
|
||||
const lensProxyCertificate = di.inject(lensProxyCertificateInjectable).get() as SelfSignedCert;
|
||||
|
||||
return () => ({
|
||||
cert: lensProxyCertificate.cert,
|
||||
public: lensProxyCertificate.public,
|
||||
private: "",
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
export default lensProxyCertificateRequestHandlerInjectable;
|
||||
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
|
||||
const lensProxyPortInjectable = getInjectable({
|
||||
id: "lens-proxy-port",
|
||||
|
||||
instantiate: () => {
|
||||
let _portNumber: number;
|
||||
|
||||
return {
|
||||
get: () => {
|
||||
if (!_portNumber) {
|
||||
throw new Error(
|
||||
"Tried to access port number of LensProxy while it has not been set yet.",
|
||||
);
|
||||
}
|
||||
|
||||
return _portNumber;
|
||||
},
|
||||
|
||||
set: (portNumber: number) => {
|
||||
if (_portNumber) {
|
||||
throw new Error(
|
||||
"Tried to set port number for LensProxy when it has already been set.",
|
||||
);
|
||||
}
|
||||
|
||||
_portNumber = portNumber;
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export default lensProxyPortInjectable;
|
||||
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import { LensProxy } from "./lens-proxy";
|
||||
import { kubeApiUpgradeRequest } from "./proxy-functions";
|
||||
import routerInjectable from "../router/router.injectable";
|
||||
import httpProxy from "http-proxy";
|
||||
import shellApiRequestInjectable from "./proxy-functions/shell-api-request.injectable";
|
||||
import lensProxyPortInjectable from "./lens-proxy-port.injectable";
|
||||
import contentSecurityPolicyInjectable from "../../common/vars/content-security-policy.injectable";
|
||||
import emitAppEventInjectable from "../../common/app-event-bus/emit-event.injectable";
|
||||
import loggerInjectable from "../../common/logger.injectable";
|
||||
import lensProxyCertificateInjectable from "../../common/certificate/lens-proxy-certificate.injectable";
|
||||
import getClusterForRequestInjectable from "./get-cluster-for-request.injectable";
|
||||
|
||||
const lensProxyInjectable = getInjectable({
|
||||
id: "lens-proxy",
|
||||
|
||||
instantiate: (di) => new LensProxy({
|
||||
router: di.inject(routerInjectable),
|
||||
proxy: httpProxy.createProxy(),
|
||||
kubeApiUpgradeRequest,
|
||||
shellApiRequest: di.inject(shellApiRequestInjectable),
|
||||
getClusterForRequest: di.inject(getClusterForRequestInjectable),
|
||||
lensProxyPort: di.inject(lensProxyPortInjectable),
|
||||
contentSecurityPolicy: di.inject(contentSecurityPolicyInjectable),
|
||||
emitAppEvent: di.inject(emitAppEventInjectable),
|
||||
logger: di.inject(loggerInjectable),
|
||||
certificate: di.inject(lensProxyCertificateInjectable).get(),
|
||||
}),
|
||||
});
|
||||
|
||||
export default lensProxyInjectable;
|
||||
@@ -0,0 +1,252 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import net from "net";
|
||||
import https from "https";
|
||||
import type http from "http";
|
||||
import type httpProxy from "http-proxy";
|
||||
import { apiPrefix, apiKubePrefix } from "../../common/vars";
|
||||
import type { Router } from "../router/router";
|
||||
import type { ClusterContextHandler } from "../context-handler/context-handler";
|
||||
import type { Cluster } from "../../common/cluster/cluster";
|
||||
import type { ProxyApiRequestArgs } from "./proxy-functions";
|
||||
import { getBoolean } from "../utils/parse-query";
|
||||
import assert from "assert";
|
||||
import type { SetRequired } from "type-fest";
|
||||
import type { EmitAppEvent } from "../../common/app-event-bus/emit-event.injectable";
|
||||
import type { Logger } from "../../common/logger";
|
||||
import type { SelfSignedCert } from "selfsigned";
|
||||
|
||||
export type GetClusterForRequest = (req: http.IncomingMessage) => Cluster | undefined;
|
||||
export type ServerIncomingMessage = SetRequired<http.IncomingMessage, "url" | "method">;
|
||||
export type LensProxyApiRequest = (args: ProxyApiRequestArgs) => void | Promise<void>;
|
||||
|
||||
interface Dependencies {
|
||||
getClusterForRequest: GetClusterForRequest;
|
||||
shellApiRequest: LensProxyApiRequest;
|
||||
kubeApiUpgradeRequest: LensProxyApiRequest;
|
||||
emitAppEvent: EmitAppEvent;
|
||||
readonly router: Router;
|
||||
readonly proxy: httpProxy;
|
||||
readonly lensProxyPort: { set: (portNumber: number) => void };
|
||||
readonly contentSecurityPolicy: string;
|
||||
readonly logger: Logger;
|
||||
readonly certificate: SelfSignedCert;
|
||||
}
|
||||
|
||||
const watchParam = "watch";
|
||||
const followParam = "follow";
|
||||
|
||||
export function isLongRunningRequest(reqUrl: string) {
|
||||
const url = new URL(reqUrl, "http://localhost");
|
||||
|
||||
return getBoolean(url.searchParams, watchParam) || getBoolean(url.searchParams, followParam);
|
||||
}
|
||||
|
||||
/**
|
||||
* This is the list of ports that chrome considers unsafe to allow HTTP
|
||||
* conntections to. Because they are the standard ports for processes that are
|
||||
* too forgiving in the connection types they accept.
|
||||
*
|
||||
* If we get one of these ports, the easiest thing to do is to just try again.
|
||||
*
|
||||
* Source: https://chromium.googlesource.com/chromium/src.git/+/refs/heads/main/net/base/port_util.cc
|
||||
*/
|
||||
const disallowedPorts = new Set([
|
||||
1, 7, 9, 11, 13, 15, 17, 19, 20, 21, 22, 23, 25, 37, 42, 43, 53, 69, 77, 79,
|
||||
87, 95, 101, 102, 103, 104, 109, 110, 111, 113, 115, 117, 119, 123, 135, 137,
|
||||
139, 143, 161, 179, 389, 427, 465, 512, 513, 514, 515, 526, 530, 531, 532,
|
||||
540, 548, 554, 556, 563, 587, 601, 636, 989, 990, 993, 995, 1719, 1720, 1723,
|
||||
2049, 3659, 4045, 5060, 5061, 6000, 6566, 6665, 6666, 6667, 6668, 6669, 6697,
|
||||
10080,
|
||||
]);
|
||||
|
||||
export class LensProxy {
|
||||
protected proxyServer: https.Server;
|
||||
protected closed = false;
|
||||
protected retryCounters = new Map<string, number>();
|
||||
|
||||
constructor(private readonly dependencies: Dependencies) {
|
||||
this.configureProxy(dependencies.proxy);
|
||||
|
||||
this.proxyServer = https.createServer(
|
||||
{
|
||||
key: dependencies.certificate.private,
|
||||
cert: dependencies.certificate.cert,
|
||||
},
|
||||
(req, res) => {
|
||||
this.handleRequest(req as ServerIncomingMessage, res);
|
||||
},
|
||||
);
|
||||
|
||||
this.proxyServer
|
||||
.on("upgrade", (req: ServerIncomingMessage, socket: net.Socket, head: Buffer) => {
|
||||
const cluster = this.dependencies.getClusterForRequest(req);
|
||||
|
||||
if (!cluster) {
|
||||
this.dependencies.logger.error(`[LENS-PROXY]: Could not find cluster for upgrade request from url=${req.url}`);
|
||||
socket.destroy();
|
||||
} else {
|
||||
const isInternal = req.url.startsWith(`${apiPrefix}?`);
|
||||
const reqHandler = isInternal ? this.dependencies.shellApiRequest : this.dependencies.kubeApiUpgradeRequest;
|
||||
|
||||
(async () => reqHandler({ req, socket, head, cluster }))()
|
||||
.catch(error => this.dependencies.logger.error("[LENS-PROXY]: failed to handle proxy upgrade", error));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts to listen on an OS provided port. Will reject if the server throws
|
||||
* an error.
|
||||
*
|
||||
* Resolves with the port number that was picked
|
||||
*/
|
||||
private attemptToListen(): Promise<number> {
|
||||
return new Promise<number>((resolve, reject) => {
|
||||
this.proxyServer.listen(0, "127.0.0.1");
|
||||
|
||||
this.proxyServer
|
||||
.once("listening", () => {
|
||||
this.proxyServer.removeAllListeners("error"); // don't reject the promise
|
||||
|
||||
const { address, port } = this.proxyServer.address() as net.AddressInfo;
|
||||
|
||||
this.dependencies.lensProxyPort.set(port);
|
||||
|
||||
this.dependencies.logger.info(`[LENS-PROXY]: Proxy server has started at ${address}:${port}`);
|
||||
|
||||
this.proxyServer.on("error", (error) => {
|
||||
this.dependencies.logger.info(`[LENS-PROXY]: Subsequent error: ${error}`);
|
||||
});
|
||||
|
||||
this.dependencies.emitAppEvent({ name: "lens-proxy", action: "listen", params: { port }});
|
||||
resolve(port);
|
||||
})
|
||||
.once("error", (error) => {
|
||||
this.dependencies.logger.info(`[LENS-PROXY]: Proxy server failed to start: ${error}`);
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the lens proxy.
|
||||
* @resolves After the server is listening on a good port
|
||||
* @rejects if there is an error before that happens
|
||||
*/
|
||||
async listen(): Promise<void> {
|
||||
const seenPorts = new Set<number>();
|
||||
|
||||
while(true) {
|
||||
this.proxyServer?.close();
|
||||
const port = await this.attemptToListen();
|
||||
|
||||
if (!disallowedPorts.has(port)) {
|
||||
// We didn't get a port that would result in an ERR_UNSAFE_PORT error, use it
|
||||
return;
|
||||
}
|
||||
|
||||
this.dependencies.logger.warn(`[LENS-PROXY]: Proxy server has with port known to be considered unsafe to connect to by chrome, restarting...`);
|
||||
|
||||
if (seenPorts.has(port)) {
|
||||
/**
|
||||
* Assume that if we have seen the port before, then the OS has looped
|
||||
* through all the ports possible and we will not be able to get a safe
|
||||
* port.
|
||||
*/
|
||||
throw new Error("Failed to start LensProxy due to seeing too many unsafe ports. Please restart Lens.");
|
||||
} else {
|
||||
seenPorts.add(port);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
close() {
|
||||
this.dependencies.logger.info("[LENS-PROXY]: Closing server");
|
||||
|
||||
this.proxyServer.close();
|
||||
this.closed = true;
|
||||
}
|
||||
|
||||
protected configureProxy(proxy: httpProxy): httpProxy {
|
||||
proxy.on("proxyRes", (proxyRes, req, res) => {
|
||||
const retryCounterId = this.getRequestId(req);
|
||||
|
||||
if (this.retryCounters.has(retryCounterId)) {
|
||||
this.retryCounters.delete(retryCounterId);
|
||||
}
|
||||
|
||||
proxyRes.on("aborted", () => { // happens when proxy target aborts connection
|
||||
res.end();
|
||||
});
|
||||
});
|
||||
|
||||
proxy.on("error", (error, req, res, target) => {
|
||||
if (this.closed || res instanceof net.Socket) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.dependencies.logger.error(`[LENS-PROXY]: http proxy errored for cluster: ${error}`, { url: req.url });
|
||||
|
||||
if (target) {
|
||||
this.dependencies.logger.debug(`Failed proxy to target: ${JSON.stringify(target, null, 2)}`);
|
||||
|
||||
if (req.method === "GET" && (!res.statusCode || res.statusCode >= 500)) {
|
||||
const reqId = this.getRequestId(req);
|
||||
const retryCount = this.retryCounters.get(reqId) || 0;
|
||||
const timeoutMs = retryCount * 250;
|
||||
|
||||
if (retryCount < 20) {
|
||||
this.dependencies.logger.debug(`Retrying proxy request to url: ${reqId}`);
|
||||
setTimeout(() => {
|
||||
this.retryCounters.set(reqId, retryCount + 1);
|
||||
this.handleRequest(req as ServerIncomingMessage, res)
|
||||
.catch(error => this.dependencies.logger.error(`[LENS-PROXY]: failed to handle request on proxy error: ${error}`));
|
||||
}, timeoutMs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
res.writeHead(500).end(`Oops, something went wrong.\n${error}`);
|
||||
} catch (e) {
|
||||
this.dependencies.logger.error(`[LENS-PROXY]: Failed to write headers: `, e);
|
||||
}
|
||||
});
|
||||
|
||||
return proxy;
|
||||
}
|
||||
|
||||
protected async getProxyTarget(req: http.IncomingMessage, contextHandler: ClusterContextHandler): Promise<httpProxy.ServerOptions | void> {
|
||||
if (req.url?.startsWith(apiKubePrefix)) {
|
||||
delete req.headers.authorization;
|
||||
req.url = req.url.replace(apiKubePrefix, "");
|
||||
|
||||
return contextHandler.getApiTarget(isLongRunningRequest(req.url));
|
||||
}
|
||||
}
|
||||
|
||||
protected getRequestId(req: http.IncomingMessage): string {
|
||||
assert(req.headers.host);
|
||||
|
||||
return req.headers.host + req.url;
|
||||
}
|
||||
|
||||
protected async handleRequest(req: ServerIncomingMessage, res: http.ServerResponse) {
|
||||
const cluster = this.dependencies.getClusterForRequest(req);
|
||||
|
||||
if (cluster) {
|
||||
const proxyTarget = await this.getProxyTarget(req, cluster.contextHandler);
|
||||
|
||||
if (proxyTarget) {
|
||||
return this.dependencies.proxy.web(req, res, proxyTarget);
|
||||
}
|
||||
}
|
||||
|
||||
res.setHeader("Content-Security-Policy", this.dependencies.contentSecurityPolicy);
|
||||
await this.dependencies.router.route(cluster, req, res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
export * from "./kube-api-upgrade-request";
|
||||
export * from "./types";
|
||||
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import { chunk } from "lodash";
|
||||
import type { ConnectionOptions } from "tls";
|
||||
import { connect } from "tls";
|
||||
import url from "url";
|
||||
import { apiKubePrefix } from "../../../common/vars";
|
||||
import type { ProxyApiRequestArgs } from "./types";
|
||||
|
||||
const skipRawHeaders = new Set(["Host", "Authorization"]);
|
||||
|
||||
export async function kubeApiUpgradeRequest({ req, socket, head, cluster }: ProxyApiRequestArgs) {
|
||||
const proxyUrl = await cluster.contextHandler.resolveAuthProxyUrl() + req.url.replace(apiKubePrefix, "");
|
||||
const proxyCa = cluster.contextHandler.resolveAuthProxyCa();
|
||||
const apiUrl = url.parse(cluster.apiUrl);
|
||||
const pUrl = url.parse(proxyUrl);
|
||||
const connectOpts: ConnectionOptions = {
|
||||
port: pUrl.port ? parseInt(pUrl.port) : undefined,
|
||||
host: pUrl.hostname ?? undefined,
|
||||
ca: proxyCa,
|
||||
};
|
||||
|
||||
const proxySocket = connect(connectOpts, () => {
|
||||
proxySocket.write(`${req.method} ${pUrl.path} HTTP/1.1\r\n`);
|
||||
proxySocket.write(`Host: ${apiUrl.host}\r\n`);
|
||||
|
||||
for (const [key, value] of chunk(req.rawHeaders, 2)) {
|
||||
if (skipRawHeaders.has(key)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
proxySocket.write(`${key}: ${value}\r\n`);
|
||||
}
|
||||
|
||||
proxySocket.write("\r\n");
|
||||
proxySocket.write(head);
|
||||
});
|
||||
|
||||
proxySocket.setKeepAlive(true);
|
||||
socket.setKeepAlive(true);
|
||||
proxySocket.setTimeout(0);
|
||||
socket.setTimeout(0);
|
||||
|
||||
proxySocket.on("data", function (chunk) {
|
||||
socket.write(chunk);
|
||||
});
|
||||
proxySocket.on("end", function () {
|
||||
socket.end();
|
||||
});
|
||||
proxySocket.on("error", function () {
|
||||
socket.write(`HTTP/${req.httpVersion} 500 Connection error\r\n\r\n`);
|
||||
socket.end();
|
||||
});
|
||||
socket.on("data", function (chunk) {
|
||||
proxySocket.write(chunk);
|
||||
});
|
||||
socket.on("end", function () {
|
||||
proxySocket.end();
|
||||
});
|
||||
socket.on("error", function () {
|
||||
proxySocket.end();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import shellRequestAuthenticatorInjectable from "./shell-request-authenticator/shell-request-authenticator.injectable";
|
||||
import openShellSessionInjectable from "../../shell-session/create-shell-session.injectable";
|
||||
import type { LensProxyApiRequest } from "../lens-proxy";
|
||||
import URLParse from "url-parse";
|
||||
import { Server as WebSocketServer } from "ws";
|
||||
import loggerInjectable from "../../../common/logger.injectable";
|
||||
import getClusterForRequestInjectable from "../get-cluster-for-request.injectable";
|
||||
|
||||
const shellApiRequestInjectable = getInjectable({
|
||||
id: "shell-api-request",
|
||||
|
||||
instantiate: (di): LensProxyApiRequest => {
|
||||
const openShellSession = di.inject(openShellSessionInjectable);
|
||||
const authenticateRequest = di.inject(shellRequestAuthenticatorInjectable).authenticate;
|
||||
const getClusterForRequest = di.inject(getClusterForRequestInjectable);
|
||||
const logger = di.inject(loggerInjectable);
|
||||
|
||||
return ({ req, socket, head }) => {
|
||||
const cluster = getClusterForRequest(req);
|
||||
const { query: { node: nodeName, shellToken, id: tabId }} = new URLParse(req.url, true);
|
||||
|
||||
if (!tabId || !cluster || !authenticateRequest(cluster.id, tabId, shellToken)) {
|
||||
socket.write("Invalid shell request");
|
||||
socket.end();
|
||||
} else {
|
||||
const ws = new WebSocketServer({ noServer: true });
|
||||
|
||||
ws.handleUpgrade(req, socket, head, (websocket) => {
|
||||
openShellSession({ websocket, cluster, tabId, nodeName })
|
||||
.catch(error => logger.error(`[SHELL-SESSION]: failed to open a ${nodeName ? "node" : "local"} shell`, error));
|
||||
});
|
||||
}
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export default shellApiRequestInjectable;
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import { ShellRequestAuthenticator } from "./shell-request-authenticator";
|
||||
|
||||
const shellRequestAuthenticatorInjectable = getInjectable({
|
||||
id: "shell-request-authenticator",
|
||||
|
||||
instantiate: () => {
|
||||
const authenticator = new ShellRequestAuthenticator();
|
||||
|
||||
authenticator.init();
|
||||
|
||||
return authenticator;
|
||||
},
|
||||
});
|
||||
|
||||
export default shellRequestAuthenticatorInjectable;
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getOrInsertMap } from "../../../../common/utils";
|
||||
import type { ClusterId } from "../../../../common/cluster-types";
|
||||
import { ipcMainHandle } from "../../../../common/ipc";
|
||||
import crypto from "crypto";
|
||||
import { promisify } from "util";
|
||||
|
||||
const randomBytes = promisify(crypto.randomBytes);
|
||||
|
||||
export class ShellRequestAuthenticator {
|
||||
private tokens = new Map<ClusterId, Map<string, Uint8Array>>();
|
||||
|
||||
init() {
|
||||
ipcMainHandle("cluster:shell-api", async (event, clusterId, tabId) => {
|
||||
const authToken = Uint8Array.from(await randomBytes(128));
|
||||
const forCluster = getOrInsertMap(this.tokens, clusterId);
|
||||
|
||||
forCluster.set(tabId, authToken);
|
||||
|
||||
return authToken;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Authenticates a single use token for creating a new shell
|
||||
* @param clusterId The `ClusterId` for the shell
|
||||
* @param tabId The ID for the shell
|
||||
* @param token The value that is being presented as a one time authentication token
|
||||
* @returns `true` if `token` was valid, false otherwise
|
||||
*/
|
||||
authenticate = (clusterId: ClusterId, tabId: string, token: string | undefined): boolean => {
|
||||
const clusterTokens = this.tokens.get(clusterId);
|
||||
|
||||
if (!clusterTokens || !tabId || !token) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const authToken = clusterTokens.get(tabId);
|
||||
const buf = Uint8Array.from(Buffer.from(token, "base64"));
|
||||
|
||||
if (authToken instanceof Uint8Array && authToken.length === buf.length && crypto.timingSafeEqual(authToken, buf)) {
|
||||
// remove the token because it is a single use token
|
||||
clusterTokens.delete(tabId);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import type http from "http";
|
||||
import type net from "net";
|
||||
import type { SetRequired } from "type-fest";
|
||||
import type { Cluster } from "../../../common/cluster/cluster";
|
||||
|
||||
export interface ProxyApiRequestArgs {
|
||||
req: SetRequired<http.IncomingMessage, "url" | "method">;
|
||||
socket: net.Socket;
|
||||
head: Buffer;
|
||||
cluster: Cluster;
|
||||
}
|
||||
Reference in new issue
Block a user