2024-10-22 06:41:41 -07:00
import { writeFile } from 'fs/promises' ;
2023-01-31 05:03:31 -08:00
import type { Readable } from 'stream' ;
2023-01-27 03:22:44 -08:00
import type {
2023-05-19 06:03:23 -07:00
ICredentialTestFunctions ,
ICredentialsDecrypted ,
2023-01-13 09:11:56 -08:00
IDataObject ,
2023-03-06 08:33:32 -08:00
IExecuteFunctions ,
2023-05-19 06:03:23 -07:00
INodeCredentialTestResult ,
2021-05-28 21:45:59 -07:00
INodeExecutionData ,
INodeType ,
INodeTypeDescription ,
} from 'n8n-workflow' ;
2024-08-29 06:55:53 -07:00
import { BINARY_ENCODING , NodeConnectionType , NodeOperationError } from 'n8n-workflow' ;
2021-05-28 21:45:59 -07:00
2023-01-31 05:03:31 -08:00
import { file as tmpFile } from 'tmp-promise' ;
2021-05-28 21:45:59 -07:00
2023-01-31 05:03:31 -08:00
import type { Config } from 'node-ssh' ;
2023-01-13 09:11:56 -08:00
import { NodeSSH } from 'node-ssh' ;
2023-01-31 05:03:31 -08:00
import { formatPrivateKey } from '@utils/utilities' ;
2021-05-28 21:45:59 -07:00
2023-05-18 04:56:51 -07:00
async function resolveHomeDir (
this : IExecuteFunctions ,
path : string ,
ssh : NodeSSH ,
itemIndex : number ,
) {
if ( path . startsWith ( '~/' ) ) {
let homeDir = ( await ssh . execCommand ( 'echo $HOME' ) ) . stdout ;
if ( homeDir . charAt ( homeDir . length - 1 ) !== '/' ) {
homeDir += '/' ;
}
return path . replace ( '~/' , homeDir ) ;
}
if ( path . startsWith ( '~' ) ) {
throw new NodeOperationError (
this . getNode ( ) ,
'Invalid path. Replace "~" with home directory or "~/"' ,
{
itemIndex ,
} ,
) ;
}
return path ;
}
2021-05-28 21:45:59 -07:00
export class Ssh implements INodeType {
description : INodeTypeDescription = {
displayName : 'SSH' ,
2021-05-30 12:00:15 -07:00
name : 'ssh' ,
2021-05-28 21:45:59 -07:00
icon : 'fa:terminal' ,
2024-06-06 04:34:30 -07:00
iconColor : 'black' ,
2021-05-28 21:45:59 -07:00
group : [ 'input' ] ,
version : 1 ,
subtitle : '={{$parameter["operation"] + ": " + $parameter["resource"]}}' ,
description : 'Execute commands via SSH' ,
defaults : {
name : 'SSH' ,
color : '#000000' ,
} ,
2024-08-29 06:55:53 -07:00
inputs : [ NodeConnectionType . Main ] ,
outputs : [ NodeConnectionType . Main ] ,
2021-05-28 21:45:59 -07:00
credentials : [
{
name : 'sshPassword' ,
required : true ,
2023-05-19 06:03:23 -07:00
testedBy : 'sshConnectionTest' ,
2021-05-28 21:45:59 -07:00
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
authentication : [ 'password' ] ,
2021-05-28 21:45:59 -07:00
} ,
} ,
} ,
{
name : 'sshPrivateKey' ,
required : true ,
2023-05-23 02:01:24 -07:00
testedBy : 'sshConnectionTest' ,
2021-05-28 21:45:59 -07:00
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
authentication : [ 'privateKey' ] ,
2021-05-28 21:45:59 -07:00
} ,
} ,
} ,
] ,
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' ,
2022-05-20 14:47:24 -07:00
noDataExpression : true ,
2021-05-28 21:45:59 -07:00
options : [
{
name : 'Command' ,
value : 'command' ,
} ,
{
name : 'File' ,
value : 'file' ,
} ,
] ,
default : 'command' ,
} ,
{
displayName : 'Operation' ,
name : 'operation' ,
type : 'options' ,
2022-05-20 14:47:24 -07:00
noDataExpression : true ,
2021-05-28 21:45:59 -07:00
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
resource : [ 'command' ] ,
2021-05-28 21:45:59 -07:00
} ,
} ,
options : [
{
name : 'Execute' ,
value : 'execute' ,
description : 'Execute a command' ,
2022-07-10 13:50:51 -07:00
action : 'Execute a command' ,
2021-05-28 21:45:59 -07:00
} ,
] ,
default : 'execute' ,
} ,
{
displayName : 'Command' ,
name : 'command' ,
type : 'string' ,
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
resource : [ 'command' ] ,
operation : [ 'execute' ] ,
2021-05-28 21:45:59 -07:00
} ,
} ,
default : '' ,
2022-05-06 14:01:25 -07:00
description : 'The command to be executed on a remote device' ,
2021-05-28 21:45:59 -07:00
} ,
{
displayName : 'Working Directory' ,
name : 'cwd' ,
type : 'string' ,
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
resource : [ 'command' ] ,
operation : [ 'execute' ] ,
2021-05-28 21:45:59 -07:00
} ,
} ,
default : '/' ,
required : true ,
} ,
{
displayName : 'Operation' ,
name : 'operation' ,
type : 'options' ,
2022-05-20 14:47:24 -07:00
noDataExpression : true ,
2021-05-28 21:45:59 -07:00
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
resource : [ 'file' ] ,
2021-05-28 21:45:59 -07:00
} ,
} ,
options : [
{
name : 'Download' ,
value : 'download' ,
description : 'Download a file' ,
2022-07-10 13:50:51 -07:00
action : 'Download a file' ,
2021-05-28 21:45:59 -07:00
} ,
{
name : 'Upload' ,
value : 'upload' ,
description : 'Upload a file' ,
2022-07-10 13:50:51 -07:00
action : 'Upload a file' ,
2021-05-28 21:45:59 -07:00
} ,
] ,
default : 'upload' ,
} ,
{
2024-01-03 03:08:16 -08:00
displayName : 'Input Binary Field' ,
2021-05-28 21:45:59 -07:00
name : 'binaryPropertyName' ,
type : 'string' ,
default : 'data' ,
required : true ,
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
operation : [ 'upload' ] ,
resource : [ 'file' ] ,
2021-05-28 21:45:59 -07:00
} ,
} ,
placeholder : '' ,
2024-01-03 03:08:16 -08:00
hint : 'The name of the input binary field containing the file to be uploaded' ,
2021-05-28 21:45:59 -07:00
} ,
{
displayName : 'Target Directory' ,
name : 'path' ,
type : 'string' ,
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
resource : [ 'file' ] ,
operation : [ 'upload' ] ,
2021-05-28 21:45:59 -07:00
} ,
} ,
default : '' ,
required : true ,
placeholder : '/home/user' ,
2022-08-17 08:50:24 -07:00
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.' ,
2021-05-28 21:45:59 -07:00
} ,
{
displayName : 'Path' ,
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
resource : [ 'file' ] ,
operation : [ 'download' ] ,
2021-05-28 21:45:59 -07:00
} ,
} ,
name : 'path' ,
type : 'string' ,
default : '' ,
placeholder : '/home/user/invoice.txt' ,
2022-08-17 08:50:24 -07:00
description :
'The file path of the file to download. Has to contain the full path including file name.' ,
2021-05-28 21:45:59 -07:00
required : true ,
} ,
{
2024-01-03 03:08:16 -08:00
displayName : 'File Property' ,
2021-05-28 21:45:59 -07:00
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
resource : [ 'file' ] ,
operation : [ 'download' ] ,
2021-05-28 21:45:59 -07:00
} ,
} ,
name : 'binaryPropertyName' ,
type : 'string' ,
default : 'data' ,
2022-05-06 14:01:25 -07:00
description : 'Object property name which holds binary data' ,
2021-05-28 21:45:59 -07:00
required : true ,
} ,
{
displayName : 'Options' ,
name : 'options' ,
type : 'collection' ,
2024-07-29 05:27:23 -07:00
placeholder : 'Add option' ,
2021-05-28 21:45:59 -07:00
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
resource : [ 'file' ] ,
2023-05-18 04:56:51 -07:00
operation : [ 'upload' , 'download' ] ,
2021-05-28 21:45:59 -07:00
} ,
} ,
default : { } ,
options : [
{
displayName : 'File Name' ,
name : 'fileName' ,
type : 'string' ,
default : '' ,
2022-05-06 14:01:25 -07:00
description : 'Overrides the binary data file name' ,
2021-05-28 21:45:59 -07:00
} ,
] ,
} ,
] ,
} ;
2023-05-19 06:03:23 -07:00
methods = {
credentialTest : {
async sshConnectionTest (
this : ICredentialTestFunctions ,
credential : ICredentialsDecrypted ,
) : Promise < INodeCredentialTestResult > {
const credentials = credential . data as IDataObject ;
const ssh = new NodeSSH ( ) ;
try {
if ( ! credentials . privateKey ) {
await ssh . connect ( {
host : credentials.host as string ,
username : credentials.username as string ,
port : credentials.port as number ,
password : credentials.password as string ,
} ) ;
} else {
const options : Config = {
host : credentials.host as string ,
username : credentials.username as string ,
port : credentials.port as number ,
2023-08-09 04:30:53 -07:00
privateKey : formatPrivateKey ( credentials . privateKey as string ) ,
2023-05-19 06:03:23 -07:00
} ;
if ( credentials . passphrase ) {
options . passphrase = credentials . passphrase as string ;
}
await ssh . connect ( options ) ;
}
} catch ( error ) {
const message = ` SSH connection failed: ${ error . message } ` ;
return {
status : 'Error' ,
message ,
} ;
} finally {
ssh . dispose ( ) ;
}
return {
status : 'OK' ,
message : 'Connection successful!' ,
} ;
} ,
} ,
} ;
2021-05-28 21:45:59 -07:00
async execute ( this : IExecuteFunctions ) : Promise < INodeExecutionData [ ] [ ] > {
const items = this . getInputData ( ) ;
2022-06-03 08:25:07 -07:00
const returnItems : INodeExecutionData [ ] = [ ] ;
2021-05-28 21:45:59 -07:00
2022-12-02 03:53:59 -08:00
const resource = this . getNodeParameter ( 'resource' , 0 ) ;
const operation = this . getNodeParameter ( 'operation' , 0 ) ;
2021-05-28 21:45:59 -07:00
const authentication = this . getNodeParameter ( 'authentication' , 0 ) as string ;
2023-01-13 09:11:56 -08:00
const ssh = new NodeSSH ( ) ;
2021-05-28 21:45:59 -07:00
try {
if ( authentication === 'password' ) {
2022-04-14 23:00:47 -07:00
const credentials = await this . getCredentials ( 'sshPassword' ) ;
2021-05-28 21:45:59 -07:00
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' ) {
2022-04-14 23:00:47 -07:00
const credentials = await this . getCredentials ( 'sshPrivateKey' ) ;
2023-01-31 05:03:31 -08:00
const options : Config = {
2021-05-28 21:45:59 -07:00
host : credentials.host as string ,
username : credentials.username as string ,
port : credentials.port as number ,
2023-08-09 04:30:53 -07:00
privateKey : formatPrivateKey ( credentials . privateKey as string ) ,
2023-01-31 05:03:31 -08:00
} ;
2021-05-28 21:45:59 -07:00
2021-07-04 09:57:53 -07:00
if ( credentials . passphrase ) {
2021-05-28 21:45:59 -07:00
options . passphrase = credentials . passphrase as string ;
}
await ssh . connect ( options ) ;
}
for ( let i = 0 ; i < items . length ; i ++ ) {
2021-05-29 19:57:38 -07:00
try {
if ( resource === 'command' ) {
if ( operation === 'execute' ) {
const command = this . getNodeParameter ( 'command' , i ) as string ;
2023-05-18 04:56:51 -07:00
const cwd = await resolveHomeDir . call (
this ,
this . getNodeParameter ( 'cwd' , i ) as string ,
ssh ,
i ,
) ;
2022-06-03 08:25:07 -07:00
returnItems . push ( {
2023-01-13 09:11:56 -08:00
json : ( await ssh . execCommand ( command , { cwd } ) ) as unknown as IDataObject ,
2022-06-03 08:25:07 -07:00
pairedItem : {
item : i ,
} ,
} ) ;
2021-05-29 19:57:38 -07:00
}
2021-05-28 21:45:59 -07:00
}
2021-05-29 19:57:38 -07:00
if ( resource === 'file' ) {
if ( operation === 'download' ) {
2023-01-06 06:09:32 -08:00
const dataPropertyNameDownload = this . getNodeParameter ( 'binaryPropertyName' , i ) ;
2023-05-18 04:56:51 -07:00
const parameterPath = await resolveHomeDir . call (
this ,
this . getNodeParameter ( 'path' , i ) as string ,
ssh ,
i ,
) ;
2021-05-28 21:45:59 -07:00
2024-10-22 06:41:41 -07:00
const binaryFile = await tmpFile ( { prefix : 'n8n-ssh-' } ) ;
try {
await ssh . getFile ( binaryFile . path , parameterPath ) ;
const newItem : INodeExecutionData = {
json : items [ i ] . json ,
binary : { } ,
pairedItem : {
item : i ,
} ,
} ;
if ( items [ i ] . binary !== undefined && newItem . binary ) {
// 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 fileName = this . getNodeParameter ( 'options.fileName' , i , '' ) as string ;
items [ i ] . binary ! [ dataPropertyNameDownload ] = await this . nodeHelpers . copyBinaryFile (
binaryFile . path ,
fileName || parameterPath ,
) ;
} finally {
await binaryFile . cleanup ( ) ;
2021-05-29 19:57:38 -07:00
}
}
2021-05-28 21:45:59 -07:00
2021-05-29 19:57:38 -07:00
if ( operation === 'upload' ) {
2023-05-18 04:56:51 -07:00
const parameterPath = await resolveHomeDir . call (
this ,
this . getNodeParameter ( 'path' , i ) as string ,
ssh ,
i ,
) ;
2021-05-29 19:57:38 -07:00
const fileName = this . getNodeParameter ( 'options.fileName' , i , '' ) as string ;
2021-05-28 21:45:59 -07:00
2023-03-06 08:33:32 -08:00
const binaryPropertyName = this . getNodeParameter ( 'binaryPropertyName' , i ) ;
const binaryData = this . helpers . assertBinaryData ( i , binaryPropertyName ) ;
2021-05-28 21:45:59 -07:00
2023-01-31 05:03:31 -08:00
let uploadData : Buffer | Readable ;
if ( binaryData . id ) {
2023-09-25 07:59:45 -07:00
uploadData = await this . helpers . getBinaryStream ( binaryData . id ) ;
2023-01-31 05:03:31 -08:00
} else {
uploadData = Buffer . from ( binaryData . data , BINARY_ENCODING ) ;
}
2021-08-20 09:08:40 -07:00
2024-10-22 06:41:41 -07:00
const binaryFile = await tmpFile ( { prefix : 'n8n-ssh-' } ) ;
try {
await writeFile ( binaryFile . path , uploadData ) ;
await ssh . putFile (
binaryFile . path ,
` ${ parameterPath } ${
parameterPath . charAt ( parameterPath . length - 1 ) === '/' ? '' : '/'
} $ { fileName || binaryData . fileName } ` ,
) ;
returnItems . push ( {
json : {
success : true ,
} ,
pairedItem : {
item : i ,
} ,
} ) ;
} finally {
await binaryFile . cleanup ( ) ;
}
2021-05-29 19:57:38 -07:00
}
}
} catch ( error ) {
2024-08-30 00:59:30 -07:00
if ( this . continueOnFail ( ) ) {
2021-05-29 19:57:38 -07:00
if ( resource === 'file' && operation === 'download' ) {
items [ i ] = {
json : {
error : error.message ,
2021-05-29 20:04:35 -07:00
} ,
2021-05-29 19:57:38 -07:00
} ;
} else {
2022-06-03 08:25:07 -07:00
returnItems . push ( {
json : {
error : error.message ,
} ,
pairedItem : {
item : i ,
} ,
} ) ;
2021-05-29 19:57:38 -07:00
}
continue ;
2021-05-28 21:45:59 -07:00
}
2021-05-29 19:57:38 -07:00
throw error ;
2021-05-28 21:45:59 -07:00
}
}
2024-10-22 06:41:41 -07:00
} finally {
2021-05-28 21:45:59 -07:00
ssh . dispose ( ) ;
}
if ( resource === 'file' && operation === 'download' ) {
// For file downloads the files get attached to the existing items
2023-09-05 03:59:02 -07:00
return [ items ] ;
2021-05-28 21:45:59 -07:00
} else {
2023-09-05 03:59:02 -07:00
return [ returnItems ] ;
2021-05-28 21:45:59 -07:00
}
}
}