From d5c7e6f2cff63337948122a11c3707316c937b9f Mon Sep 17 00:00:00 2001 From: Michael Kret <88898367+michael-radency@users.noreply.github.com> Date: Tue, 23 May 2023 12:01:24 +0300 Subject: [PATCH] fix(SSH Node): Private key field as password and credential test (#6298) --- .../credentials/SshPrivateKey.credentials.ts | 1 + packages/nodes-base/nodes/Ssh/Ssh.node.ts | 13 +++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/nodes-base/credentials/SshPrivateKey.credentials.ts b/packages/nodes-base/credentials/SshPrivateKey.credentials.ts index e457bd491568e..6fdc8c2e96107 100644 --- a/packages/nodes-base/credentials/SshPrivateKey.credentials.ts +++ b/packages/nodes-base/credentials/SshPrivateKey.credentials.ts @@ -35,6 +35,7 @@ export class SshPrivateKey implements ICredentialType { type: 'string', typeOptions: { rows: 4, + password: true, }, default: '', }, diff --git a/packages/nodes-base/nodes/Ssh/Ssh.node.ts b/packages/nodes-base/nodes/Ssh/Ssh.node.ts index 3a852eb43754a..7f8ac5aedf9c8 100644 --- a/packages/nodes-base/nodes/Ssh/Ssh.node.ts +++ b/packages/nodes-base/nodes/Ssh/Ssh.node.ts @@ -47,6 +47,14 @@ async function resolveHomeDir( return path; } +function sanitizePrivateKey(privateKey: string) { + const [openSshKey, bodySshKey, endSshKey] = privateKey + .split('-----') + .filter((item) => item !== ''); + + return `-----${openSshKey}-----\n${bodySshKey.replace(/ /g, '\n')}\n-----${endSshKey}-----`; +} + export class Ssh implements INodeType { description: INodeTypeDescription = { displayName: 'SSH', @@ -76,6 +84,7 @@ export class Ssh implements INodeType { { name: 'sshPrivateKey', required: true, + testedBy: 'sshConnectionTest', displayOptions: { show: { authentication: ['privateKey'], @@ -297,7 +306,7 @@ export class Ssh implements INodeType { } else { const { path } = await tmpFile({ prefix: 'n8n-ssh-' }); temporaryFiles.push(path); - await writeFile(path, credentials.privateKey as string); + await writeFile(path, sanitizePrivateKey(credentials.privateKey as string)); const options: Config = { host: credentials.host as string, @@ -358,7 +367,7 @@ export class Ssh implements INodeType { const { path } = await tmpFile({ prefix: 'n8n-ssh-' }); temporaryFiles.push(path); - await writeFile(path, credentials.privateKey as string); + await writeFile(path, sanitizePrivateKey(credentials.privateKey as string)); const options: Config = { host: credentials.host as string,