n8n/packages/nodes-base/nodes/Ssh/Ssh.node.ts

434 lines
9.5 KiB
TypeScript
Raw Normal View History

import {
IExecuteFunctions,
} from 'n8n-core';
import {
IBinaryData,
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
readFile,
rm,
writeFile,
2021-05-29 12:16:47 -07:00
} from 'fs/promises';
import { file } from 'tmp-promise';
const nodeSSH = require('node-ssh');
export class Ssh implements INodeType {
description: INodeTypeDescription = {
displayName: 'SSH',
2021-05-30 12:00:15 -07:00
name: 'ssh',
icon: 'fa:terminal',
group: ['input'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Execute commands via SSH',
defaults: {
name: 'SSH',
color: '#000000',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'sshPassword',
required: true,
displayOptions: {
show: {
authentication: [
'password',
],
},
},
},
{
name: 'sshPrivateKey',
required: true,
displayOptions: {
show: {
authentication: [
'privateKey',
],
},
},
},
],
properties: [
{
displayName: 'Authentication',
name: 'authentication',
type: 'options',
options: [
{
name: 'Password',
value: 'password',
},
{
name: 'Private Key',
value: 'privateKey',
},
],
default: 'password',
},
{
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
{
name: 'Command',
value: 'command',
},
{
name: 'File',
value: 'file',
},
],
default: 'command',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'command',
],
},
},
options: [
{
name: 'Execute',
value: 'execute',
description: 'Execute a command',
},
],
default: 'execute',
description: 'Operation to perform.',
},
{
displayName: 'Command',
name: 'command',
type: 'string',
displayOptions: {
show: {
resource: [
'command',
],
operation: [
'execute',
],
},
},
default: '',
description: 'The command to be executed on a remote device.',
},
{
displayName: 'Working Directory',
name: 'cwd',
type: 'string',
displayOptions: {
show: {
resource: [
'command',
],
operation: [
'execute',
],
},
},
default: '/',
required: true,
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'file',
],
},
},
options: [
{
name: 'Download',
value: 'download',
description: 'Download a file',
},
{
name: 'Upload',
value: 'upload',
description: 'Upload a file',
},
],
default: 'upload',
description: 'Operation to perform.',
},
{
displayName: 'Binary Property',
name: 'binaryPropertyName',
type: 'string',
default: 'data',
required: true,
displayOptions: {
show: {
operation: [
'upload',
],
resource: [
'file',
],
},
},
placeholder: '',
:zap: Remove unnessasry <br/> (#2340) * introduce analytics * add user survey backend * add user survey backend * set answers on survey submit Co-authored-by: Mutasem Aldmour <4711238+mutdmour@users.noreply.github.com> * change name to personalization * lint Co-authored-by: Mutasem Aldmour <4711238+mutdmour@users.noreply.github.com> * N8n 2495 add personalization modal (#2280) * update modals * add onboarding modal * implement questions * introduce analytics * simplify impl * implement survey handling * add personalized cateogry * update modal behavior * add thank you view * handle empty cases * rename modal * standarize modal names * update image, add tags to headings * remove unused file * remove unused interfaces * clean up footer spacing * introduce analytics * refactor to fix bug * update endpoint * set min height * update stories * update naming from questions to survey * remove spacing after core categories * fix bug in logic * sort nodes * rename types * merge with be * rename userSurvey * clean up rest api * use constants for keys * use survey keys * clean up types * move personalization to its own file Co-authored-by: ahsan-virani <ahsan.virani@gmail.com> * update parameter inputs to be multiline * update spacing * Survey new options (#2300) * split up options * fix quotes * remove unused import * refactor node credentials * add user created workflow event (#2301) * update multi params * simplify env vars * fix versionCli on FE * update personalization env * clean up node detail settings * fix event User opened Credentials panel * fix font sizes across modals * clean up input spacing * fix select modal spacing * increase spacing * fix input copy * fix webhook, tab spacing, retry button * fix button sizes * fix button size * add mini xlarge sizes * fix webhook spacing * fix nodes panel event * fix workflow id in workflow execute event * improve telemetry error logging * fix config and stop process events * add flush call on n8n stop * ready for release * fix input error highlighting * revert change * update toggle spacing * fix delete positioning * keep tooltip while focused * set strict size * increase left spacing * fix sort icons * remove unnessasry <br/> * remove unnessary break * remove unnessary margin * clean unused functionality * remove unnessary css * remove duplicate tracking * only show tooltip when hovering over label * remove extra space * add br * remove extra space * clean up commas * clean up commas * remove extra space * remove extra space * rewrite desc * add commas * add space * remove extra space * add space * add dot * update credentials section * use includes Co-authored-by: ahsan-virani <ahsan.virani@gmail.com> Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
2021-10-27 13:00:13 -07:00
description: 'Name of the binary property which contains the data for the file to be uploaded.',
},
{
displayName: 'Target Directory',
name: 'path',
type: 'string',
displayOptions: {
show: {
resource: [
'file',
],
operation: [
'upload',
],
},
},
default: '',
required: true,
placeholder: '/home/user',
description: `The directory to upload the file to. The name of the file does not need to be specified, it's taken from the binary data file name. To override this behavior, set the parameter "File Name" under options.`,
},
{
displayName: 'Path',
displayOptions: {
show: {
resource: [
'file',
],
operation: [
'download',
],
},
},
name: 'path',
type: 'string',
default: '',
placeholder: '/home/user/invoice.txt',
description: 'The file path of the file to download. Has to contain the full path including file name.',
required: true,
},
{
displayName: 'Binary Property',
displayOptions: {
show: {
resource: [
'file',
],
operation: [
'download',
],
},
},
name: 'binaryPropertyName',
type: 'string',
default: 'data',
description: 'Object property name which holds binary data.',
required: true,
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Option',
displayOptions: {
show: {
resource: [
'file',
],
operation: [
'upload',
],
},
},
default: {},
options: [
{
displayName: 'File Name',
name: 'fileName',
type: 'string',
default: '',
description: `Overrides the binary data file name.`,
},
],
},
],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
const authentication = this.getNodeParameter('authentication', 0) as string;
2021-05-28 21:53:17 -07:00
const temporaryFiles: string[] = [];
const ssh = new nodeSSH.NodeSSH();
try {
if (authentication === 'password') {
const credentials = await this.getCredentials('sshPassword') as IDataObject;
await ssh.connect({
host: credentials.host as string,
username: credentials.username as string,
port: credentials.port as number,
password: credentials.password as string,
});
} else if (authentication === 'privateKey') {
const credentials = await this.getCredentials('sshPrivateKey') as IDataObject;
const { path, } = await file({ prefix: 'n8n-ssh-' });
2021-05-28 21:53:17 -07:00
temporaryFiles.push(path);
await writeFile(path, credentials.privateKey as string);
const options = {
host: credentials.host as string,
username: credentials.username as string,
port: credentials.port as number,
privateKey: path,
} as any; // tslint:disable-line: no-any
if (credentials.passphrase) {
options.passphrase = credentials.passphrase as string;
}
await ssh.connect(options);
}
for (let i = 0; i < items.length; i++) {
try {
if (resource === 'command') {
if (operation === 'execute') {
const command = this.getNodeParameter('command', i) as string;
const cwd = this.getNodeParameter('cwd', i) as string;
returnData.push(await ssh.execCommand(command, { cwd, }));
}
}
if (resource === 'file') {
if (operation === 'download') {
const dataPropertyNameDownload = this.getNodeParameter('binaryPropertyName', i) as string;
const parameterPath = this.getNodeParameter('path', i) as string;
const { path } = await file({ prefix: 'n8n-ssh-' });
temporaryFiles.push(path);
await ssh.getFile(path, parameterPath);
const newItem: INodeExecutionData = {
json: items[i].json,
binary: {},
};
if (items[i].binary !== undefined) {
// Create a shallow copy of the binary data so that the old
// data references which do not get changed still stay behind
// but the incoming data does not get changed.
Object.assign(newItem.binary, items[i].binary);
}
items[i] = newItem;
const data = await readFile(path as string);
items[i].binary![dataPropertyNameDownload] = await this.helpers.prepareBinaryData(data, parameterPath);
}
if (operation === 'upload') {
const parameterPath = this.getNodeParameter('path', i) as string;
const fileName = this.getNodeParameter('options.fileName', i, '') as string;
const item = items[i];
if (item.binary === undefined) {
throw new Error('No binary data exists on item!');
}
const propertyNameUpload = this.getNodeParameter('binaryPropertyName', i) as string;
const binaryData = item.binary[propertyNameUpload] as IBinaryData;
if (item.binary[propertyNameUpload] === undefined) {
throw new Error(`No binary data property "${propertyNameUpload}" does not exists on item!`);
}
const dataBuffer = await this.helpers.getBinaryDataBuffer(i, propertyNameUpload);
const { path } = await file({ prefix: 'n8n-ssh-' });
temporaryFiles.push(path);
await writeFile(path, dataBuffer);
await ssh.putFile(path, `${parameterPath}${(parameterPath.charAt(parameterPath.length - 1) === '/') ? '' : '/'}${fileName || binaryData.fileName}`);
returnData.push({ success: true });
}
}
} catch (error) {
if (this.continueOnFail()) {
if (resource === 'file' && operation === 'download') {
items[i] = {
json: {
error: error.message,
2021-05-29 20:04:35 -07:00
},
};
} else {
returnData.push({ error: error.message });
}
continue;
}
throw error;
}
}
} catch (error) {
ssh.dispose();
2021-05-28 21:53:17 -07:00
for (const tempFile of temporaryFiles) await rm(tempFile);
throw error;
}
2021-05-28 21:53:17 -07:00
for (const tempFile of temporaryFiles) await rm(tempFile);
ssh.dispose();
if (resource === 'file' && operation === 'download') {
// For file downloads the files get attached to the existing items
return this.prepareOutputData(items);
} else {
return [this.helpers.returnJsonArray(returnData)];
}
}
}