From 6a38035fe521a783f53008de8f28105948b5ab7b Mon Sep 17 00:00:00 2001 From: ecmel Date: Thu, 27 Feb 2025 15:27:17 +0300 Subject: [PATCH 1/3] implemented custom authentication for kdb connections --- src/classes/localConnection.ts | 27 +++++++++++++++++++++++++++ src/extension.ts | 7 +++++++ src/extensionVariables.ts | 3 +++ src/models/customAuth.ts | 29 +++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+) create mode 100644 src/models/customAuth.ts diff --git a/src/classes/localConnection.ts b/src/classes/localConnection.ts index 42f9ce6d..75e34621 100644 --- a/src/classes/localConnection.ts +++ b/src/classes/localConnection.ts @@ -61,6 +61,33 @@ export class LocalConnection { options.password = creds[1]; } this.connLabel = connLabel; + + if (ext.customAuthApi) { + const { kdb } = ext.customAuthApi.auth({ + label: connLabel, + kdb: { + host: options.host, + port: options.port, + user: options.user, + password: options.password, + unixSocket: options.unixSocket, + socketTimeout: options.socketTimeout, + socketNoDelay: options.socketNoDelay, + }, + }); + if (kdb) { + if (kdb.host !== undefined) options.host = kdb.host; + if (kdb.port !== undefined) options.port = kdb.port; + if (kdb.user !== undefined) options.user = kdb.user; + if (kdb.password !== undefined) options.password = kdb.password; + if (kdb.unixSocket !== undefined) options.unixSocket = kdb.unixSocket; + if (kdb.socketTimeout !== undefined) + options.socketTimeout = kdb.socketTimeout; + if (kdb.socketNoDelay !== undefined) + options.socketNoDelay = options.socketNoDelay; + } + } + this.options = options; this.connected = false; } diff --git a/src/extension.ts b/src/extension.ts index 32b9783f..79521307 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -738,6 +738,13 @@ export async function activate(context: ExtensionContext) { .update("yaml.schemas", actualSchema, ConfigurationTarget.Global); }); } + const authExtension = extensions.getExtension("KX.kdb-auth"); + if (authExtension) { + const api = await authExtension.activate(); + if ("auth" in api) { + ext.customAuthApi = api; + } + } } export async function deactivate(): Promise { diff --git a/src/extensionVariables.ts b/src/extensionVariables.ts index 83ce62fc..1579e8a6 100644 --- a/src/extensionVariables.ts +++ b/src/extensionVariables.ts @@ -39,6 +39,7 @@ import { InsightsConnection } from "./classes/insightsConnection"; import { DataSourceFiles } from "./models/dataSource"; import { ConnectionLabel, LabelColors, Labels } from "./models/labels"; import { kdbAuthMap } from "./models/connectionsModels"; +import { CustomAuth } from "./models/customAuth"; // eslint-disable-next-line @typescript-eslint/no-namespace export namespace ext { @@ -438,4 +439,6 @@ export namespace ext { colorHex: "#15A7CD", }, ]; + + export let customAuthApi: CustomAuth | null = null; } diff --git a/src/models/customAuth.ts b/src/models/customAuth.ts new file mode 100644 index 00000000..8d17bd4c --- /dev/null +++ b/src/models/customAuth.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 1998-2025 Kx Systems Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +export interface CustomAuthParams { + label: string; + kdb?: { + host?: string; + port?: number; + user?: string; + password?: string; + socketNoDelay?: boolean; + socketTimeout?: number; + unixSocket?: string; + }; +} + +export interface CustomAuth { + auth: (options: CustomAuthParams) => CustomAuthParams; +} From aefd2299bbeb05d40f36564ed7d8eb2781dac0b8 Mon Sep 17 00:00:00 2001 From: ecmel Date: Thu, 27 Feb 2025 15:34:17 +0300 Subject: [PATCH 2/3] fixed typo --- src/classes/localConnection.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/classes/localConnection.ts b/src/classes/localConnection.ts index 75e34621..28b82cb6 100644 --- a/src/classes/localConnection.ts +++ b/src/classes/localConnection.ts @@ -84,7 +84,7 @@ export class LocalConnection { if (kdb.socketTimeout !== undefined) options.socketTimeout = kdb.socketTimeout; if (kdb.socketNoDelay !== undefined) - options.socketNoDelay = options.socketNoDelay; + options.socketNoDelay = kdb.socketNoDelay; } } From 89fad38b4f43dc4d7995e53d837014a816c0281a Mon Sep 17 00:00:00 2001 From: ecmel Date: Thu, 27 Feb 2025 16:16:26 +0300 Subject: [PATCH 3/3] reduced complexity --- src/classes/localConnection.ts | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/classes/localConnection.ts b/src/classes/localConnection.ts index 28b82cb6..633153e1 100644 --- a/src/classes/localConnection.ts +++ b/src/classes/localConnection.ts @@ -75,16 +75,9 @@ export class LocalConnection { socketNoDelay: options.socketNoDelay, }, }); + if (kdb) { - if (kdb.host !== undefined) options.host = kdb.host; - if (kdb.port !== undefined) options.port = kdb.port; - if (kdb.user !== undefined) options.user = kdb.user; - if (kdb.password !== undefined) options.password = kdb.password; - if (kdb.unixSocket !== undefined) options.unixSocket = kdb.unixSocket; - if (kdb.socketTimeout !== undefined) - options.socketTimeout = kdb.socketTimeout; - if (kdb.socketNoDelay !== undefined) - options.socketNoDelay = kdb.socketNoDelay; + updateOptions(options, kdb); } } @@ -394,3 +387,21 @@ export class LocalConnection { callback(err, this); } } + +function updateOptions(options: any, kdb: any): void { + const keys = [ + "host", + "port", + "user", + "password", + "unixSocket", + "socketTimeout", + "socketNoDelay", + ]; + + keys.forEach((key) => { + if (kdb[key] !== undefined) { + options[key] = kdb[key]; + } + }); +}