Skip to content

Commit

Permalink
fix(SSH Node): Replace ~ with /home/username (#6269)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-radency committed May 18, 2023
1 parent a1b1f24 commit 4219490
Showing 1 changed file with 51 additions and 6 deletions.
57 changes: 51 additions & 6 deletions packages/nodes-base/nodes/Ssh/Ssh.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import { BINARY_ENCODING } from 'n8n-workflow';
import { BINARY_ENCODING, NodeOperationError } from 'n8n-workflow';

import { rm, writeFile } from 'fs/promises';

Expand All @@ -15,6 +15,35 @@ import type { Config } from 'node-ssh';
import { NodeSSH } from 'node-ssh';
import type { Readable } from 'stream';

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;
}

export class Ssh implements INodeType {
description: INodeTypeDescription = {
displayName: 'SSH',
Expand Down Expand Up @@ -226,7 +255,7 @@ export class Ssh implements INodeType {
displayOptions: {
show: {
resource: ['file'],
operation: ['upload'],
operation: ['upload', 'download'],
},
},
default: {},
Expand Down Expand Up @@ -292,7 +321,12 @@ export class Ssh implements INodeType {
if (resource === 'command') {
if (operation === 'execute') {
const command = this.getNodeParameter('command', i) as string;
const cwd = this.getNodeParameter('cwd', i) as string;
const cwd = await resolveHomeDir.call(
this,
this.getNodeParameter('cwd', i) as string,
ssh,
i,
);
returnItems.push({
json: (await ssh.execCommand(command, { cwd })) as unknown as IDataObject,
pairedItem: {
Expand All @@ -305,7 +339,12 @@ export class Ssh implements INodeType {
if (resource === 'file') {
if (operation === 'download') {
const dataPropertyNameDownload = this.getNodeParameter('binaryPropertyName', i);
const parameterPath = this.getNodeParameter('path', i) as string;
const parameterPath = await resolveHomeDir.call(
this,
this.getNodeParameter('path', i) as string,
ssh,
i,
);

const { path } = await tmpFile({ prefix: 'n8n-ssh-' });
temporaryFiles.push(path);
Expand All @@ -329,14 +368,20 @@ export class Ssh implements INodeType {

items[i] = newItem;

const fileName = this.getNodeParameter('options.fileName', i, '') as string;
items[i].binary![dataPropertyNameDownload] = await this.nodeHelpers.copyBinaryFile(
path,
parameterPath,
fileName || parameterPath,
);
}

if (operation === 'upload') {
const parameterPath = this.getNodeParameter('path', i) as string;
const parameterPath = await resolveHomeDir.call(
this,
this.getNodeParameter('path', i) as string,
ssh,
i,
);
const fileName = this.getNodeParameter('options.fileName', i, '') as string;

const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i);
Expand Down

0 comments on commit 4219490

Please sign in to comment.