From fd277720328d56a78304280ed3d2831578391dd5 Mon Sep 17 00:00:00 2001 From: Seb Julliand Date: Mon, 18 Sep 2023 14:11:26 +0200 Subject: [PATCH 1/2] Renamed connection's privateKey to privateKeyPath - Use the correct field used by node-ssh Config object - Fixes https://github.com/halcyon-tech/vscode-ibmi/issues/1542 --- src/api/IBMi.ts | 10 +--------- src/extension.ts | 19 +++++++++++++++++++ src/sandbox.ts | 6 ++---- src/typings.ts | 2 +- src/webviews/login/index.ts | 12 ++++++------ src/webviews/settings/index.ts | 14 +++++++------- 6 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/api/IBMi.ts b/src/api/IBMi.ts index de94d8503..84e69e56b 100644 --- a/src/api/IBMi.ts +++ b/src/api/IBMi.ts @@ -3,7 +3,6 @@ import * as node_ssh from "node-ssh"; import * as vscode from "vscode"; import { ConnectionConfiguration } from "./Configuration"; -import { readFileSync } from "fs"; import path from 'path'; import { instance } from "../instantiate"; import { CommandData, CommandResult, ConnectionData, IBMiMember, RemoteCommand, StandardIO } from "../typings"; @@ -117,14 +116,7 @@ export default class IBMi { try { connectionObject.keepaliveInterval = 35000; - configVars.replaceAll(connectionObject); - - // Make sure we're not passing any blank strings, as node_ssh will try to validate it - if (connectionObject.privateKey) { - connectionObject.privateKey = readFileSync(connectionObject.privateKey, {encoding: `utf-8`}); - } else { - connectionObject.privateKey = null; - } + configVars.replaceAll(connectionObject); return await vscode.window.withProgress({ location: vscode.ProgressLocation.Notification, diff --git a/src/extension.ts b/src/extension.ts index 900a88e73..609040792 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -113,9 +113,28 @@ export async function activate(context: ExtensionContext): Promise ]); }); + await fixPrivateKeys(); + return { instance, customUI: () => new CustomUI(), deployTools: DeployTools, evfeventParser: parseErrors, tools: Tools }; } +async function fixPrivateKeys(){ + const connections = (GlobalConfiguration.get(`connections`) || []); + let update = false; + for(const connection of connections){ + if('privateKey' in connection && connection.privateKey){ + connection.privateKeyPath = connection.privateKey as string; + connection.privateKey = undefined; + console.log(`Fixed private key for ${connection.name}`); + update = true; + } + } + + if(update){ + await GlobalConfiguration.set(`connections`, connections); + } +} + // this method is called when your extension is deactivated export async function deactivate() { await commands.executeCommand(`code-for-ibmi.disconnect`, true); diff --git a/src/sandbox.ts b/src/sandbox.ts index 5568b7004..246d89e7b 100644 --- a/src/sandbox.ts +++ b/src/sandbox.ts @@ -53,8 +53,7 @@ export async function registerUriHandler(context: ExtensionContext) { name: `${user}-${host}`, username: String(user), password: String(pass), - port, - privateKey: null + port }; const connectionResult = await commands.executeCommand(`code-for-ibmi.connectDirect`, connectionData); @@ -154,8 +153,7 @@ export async function handleStartup() { name: `Sandbox-${username}`, username, password, - port: 22, - privateKey: null + port: 22 }; if (env.VSCODE_IBMI_SANDBOX) { diff --git a/src/typings.ts b/src/typings.ts index fbe3194d8..589e6eef9 100644 --- a/src/typings.ts +++ b/src/typings.ts @@ -73,7 +73,7 @@ export interface ConnectionData { port: number; username: string; password?: string; - privateKey: string | null; + privateKeyPath?: string; keepaliveInterval?: number; } diff --git a/src/webviews/login/index.ts b/src/webviews/login/index.ts index de5024420..b6253de70 100644 --- a/src/webviews/login/index.ts +++ b/src/webviews/login/index.ts @@ -1,9 +1,9 @@ import vscode from "vscode"; -import IBMi from "../../api/IBMi"; -import { CustomUI } from "../../api/CustomUI"; import { GlobalConfiguration } from "../../api/Configuration"; -import { ConnectionData } from '../../typings'; +import { CustomUI } from "../../api/CustomUI"; +import IBMi from "../../api/IBMi"; import { disconnect, instance } from "../../instantiate"; +import { ConnectionData } from '../../typings'; export class Login { @@ -27,7 +27,7 @@ export class Login { .addParagraph(`Only provide either the password or a private key - not both.`) .addPassword(`password`, `Password`) .addCheckbox(`savePassword`, `Save Password`) - .addFile(`privateKey`, `Private Key`, `OpenSSH, RFC4716, or PPK formats are supported.`) + .addFile(`privateKeyPath`, `Private Key`, `OpenSSH, RFC4716, or PPK formats are supported.`) .addButtons( { id: `connect`, label: `Connect`, requiresValidation: true }, { id: `saveExit`, label: `Save & Exit` } @@ -54,7 +54,7 @@ export class Login { host: data.host, port: data.port, username: data.username, - privateKey: data.privateKey + privateKeyPath: data.privateKeyPath }); if (data.savePassword) context.secrets.store(`${data.name}_password`, `${data.password}`); @@ -128,7 +128,7 @@ export class Login { const existingConnections = GlobalConfiguration.get(`connections`) || []; let connectionConfig = existingConnections.find(item => item.name === name); if (connectionConfig) { - if (!connectionConfig.privateKey) { + if (!connectionConfig.privateKeyPath) { connectionConfig.password = await context.secrets.get(`${connectionConfig.name}_password`); if (!connectionConfig.password) { connectionConfig.password = await vscode.window.showInputBox({ diff --git a/src/webviews/settings/index.ts b/src/webviews/settings/index.ts index 042ae9b12..74eb0c10f 100644 --- a/src/webviews/settings/index.ts +++ b/src/webviews/settings/index.ts @@ -1,9 +1,9 @@ import vscode from "vscode"; +import { ConnectionConfiguration, GlobalConfiguration } from "../../api/Configuration"; import { ComplexTab, CustomUI, Section } from "../../api/CustomUI"; -import { GlobalConfiguration, ConnectionConfiguration } from "../../api/Configuration"; -import { ConnectionData, Server } from '../../typings'; -import { instance } from "../../instantiate"; import * as certificates from "../../api/debug/certificates"; +import { instance } from "../../instantiate"; +import { ConnectionData, Server } from '../../typings'; const ENCODINGS = [`37`, `256`, `273`, `277`, `278`, `280`, `284`, `285`, `297`, `500`, `871`, `870`, `905`, `880`, `420`, `875`, `424`, `1026`, `290`, `win37`, `win256`, `win273`, `win277`, `win278`, `win280`, `win284`, `win285`, `win297`, `win500`, `win871`, `win870`, `win905`, `win880`, `win420`, `win875`, `win424`, `win1026`]; @@ -228,7 +228,7 @@ export class SettingsUI { .addInput(`username`, `Username`, undefined, { default: connection.username, minlength: 1 }) .addParagraph(`Only provide either the password or a private key - not both.`) .addPassword(`password`, `Password`, `Only provide a password if you want to update an existing one or set a new one.`) - .addFile(`privateKey`, `Private Key${connection.privateKey ? ` (current: ${connection.privateKey})` : ``}`, `Only provide a private key if you want to update from the existing one or set one. OpenSSH, RFC4716, or PPK formats are supported.`) + .addFile(`privateKeyPath`, `Private Key${connection.privateKeyPath ? ` (current: ${connection.privateKeyPath})` : ``}`, `Only provide a private key if you want to update from the existing one or set one. OpenSSH, RFC4716, or PPK formats are supported.`) .addButtons({ id: `submitButton`, label: `Save`, requiresValidation: true }) .loadPage(`Login Settings: ${name}`); @@ -237,12 +237,12 @@ export class SettingsUI { const data = page.data; data.port = Number(data.port); - if (data.privateKey === ``) data.privateKey = connection.privateKey; + if (data.privateKeyPath === ``) data.privateKeyPath = connection.privateKeyPath; - if (data.password && !data.privateKey) { + if (data.password && !data.privateKeyPath) { context.secrets.delete(`${name}_password`); context.secrets.store(`${name}_password`, `${data.password}`); - data.privateKey = ``; + data.privateKeyPath = ``; }; delete data.password; From b9809e7418ef5b00a246773bbd3971c8be0a4093 Mon Sep 17 00:00:00 2001 From: Seb Julliand Date: Mon, 18 Sep 2023 14:12:19 +0200 Subject: [PATCH 2/2] Removed console log --- src/extension.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/extension.ts b/src/extension.ts index 609040792..8430c14b4 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -125,7 +125,6 @@ async function fixPrivateKeys(){ if('privateKey' in connection && connection.privateKey){ connection.privateKeyPath = connection.privateKey as string; connection.privateKey = undefined; - console.log(`Fixed private key for ${connection.name}`); update = true; } }