Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(SSH Node): Credentials test #6279

Merged
merged 1 commit into from
May 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions packages/nodes-base/nodes/Ssh/Ssh.node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type {
ICredentialTestFunctions,
ICredentialsDecrypted,
IDataObject,
IExecuteFunctions,
INodeCredentialTestResult,
INodeExecutionData,
INodeType,
INodeTypeDescription,
Expand Down Expand Up @@ -63,6 +66,7 @@ export class Ssh implements INodeType {
{
name: 'sshPassword',
required: true,
testedBy: 'sshConnectionTest',
displayOptions: {
show: {
authentication: ['password'],
Expand Down Expand Up @@ -272,6 +276,60 @@ export class Ssh implements INodeType {
],
};

methods = {
credentialTest: {
async sshConnectionTest(
this: ICredentialTestFunctions,
credential: ICredentialsDecrypted,
): Promise<INodeCredentialTestResult> {
const credentials = credential.data as IDataObject;
const ssh = new NodeSSH();
const temporaryFiles: string[] = [];

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 { path } = await tmpFile({ prefix: 'n8n-ssh-' });
temporaryFiles.push(path);
await writeFile(path, credentials.privateKey as string);

const options: Config = {
host: credentials.host as string,
username: credentials.username as string,
port: credentials.port as number,
privateKey: path,
};

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();
for (const tempFile of temporaryFiles) await rm(tempFile);
}
return {
status: 'OK',
message: 'Connection successful!',
};
},
},
};

async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();

Expand Down