Skip to content

Commit

Permalink
Merge pull request #1544 from halcyon-tech/fix/privateKeyReconnect
Browse files Browse the repository at this point in the history
Renamed connection's privateKey to privateKeyPath
  • Loading branch information
worksofliam authored Sep 21, 2023
2 parents b32402e + b9809e7 commit dd19225
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 27 deletions.
10 changes: 1 addition & 9 deletions src/api/IBMi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,27 @@ export async function activate(context: ExtensionContext): Promise<CodeForIBMi>
]);
});

await fixPrivateKeys();

return { instance, customUI: () => new CustomUI(), deployTools: DeployTools, evfeventParser: parseErrors, tools: Tools };
}

async function fixPrivateKeys(){
const connections = (GlobalConfiguration.get<ConnectionData[]>(`connections`) || []);
let update = false;
for(const connection of connections){
if('privateKey' in connection && connection.privateKey){
connection.privateKeyPath = connection.privateKey as string;
connection.privateKey = undefined;
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);
Expand Down
6 changes: 2 additions & 4 deletions src/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -154,8 +153,7 @@ export async function handleStartup() {
name: `Sandbox-${username}`,
username,
password,
port: 22,
privateKey: null
port: 22
};

if (env.VSCODE_IBMI_SANDBOX) {
Expand Down
2 changes: 1 addition & 1 deletion src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export interface ConnectionData {
port: number;
username: string;
password?: string;
privateKey: string | null;
privateKeyPath?: string;
keepaliveInterval?: number;
}

Expand Down
12 changes: 6 additions & 6 deletions src/webviews/login/index.ts
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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` }
Expand All @@ -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}`);
Expand Down Expand Up @@ -128,7 +128,7 @@ export class Login {
const existingConnections = GlobalConfiguration.get<ConnectionData[]>(`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({
Expand Down
14 changes: 7 additions & 7 deletions src/webviews/settings/index.ts
Original file line number Diff line number Diff line change
@@ -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`];

Expand Down Expand Up @@ -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<any>(`Login Settings: ${name}`);

Expand All @@ -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;
Expand Down

0 comments on commit dd19225

Please sign in to comment.