Skip to content

Commit

Permalink
passphrase + README (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
if0s authored Jul 29, 2022
1 parent f3b9551 commit 499ce62
Show file tree
Hide file tree
Showing 12 changed files with 951 additions and 6,992 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 1.5.0 (July 29, 2022)
* Added retries on errors while connecting SFTP client
* Upgrade component-commons-library version to 3.0.0
* Upgrade oih-standard-library version to 2.0.3
* Add support `passphrase` to credentials
* README improvements

## 1.4.9 (June 03, 2022)
* Add a file filter to the `Poll Files` trigger

Expand Down
807 changes: 178 additions & 629 deletions README.md

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions component.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"title": "SFTP",
"buildType": "docker",
"description": "Provides file access and transfer using SSH File Transfer Protocol",
"version": "1.4.9",
"version": "1.5.0",
"credentials": {
"fields": {
"host": {
Expand Down Expand Up @@ -36,6 +35,13 @@
"required": false,
"placeholder": "Paste your SFTP Private Key",
"note": "For using Private Key based authentication, leave Password field empty"
},
"passphrase": {
"viewClass": "PasswordFieldView",
"label": "Passphrase",
"required": false,
"placeholder": "Paste your passphrase",
"note": "If private key is protected by a passphrase, put it here"
}
}
},
Expand Down Expand Up @@ -529,4 +535,4 @@
}
}
}
}
}
5 changes: 3 additions & 2 deletions lib/Sftp.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,22 @@ module.exports = class Sftp {

async createConnectionOptions() {
const {
host, port = 22, username, password, privateKey,
host, port = 22, username, password, privateKey, passphrase,
} = this._cfg;
await ip.resolve(host);
this.logger.debug('IP successfully resolved');
const params = {
host,
port,
username,
retries: 1,
retries: 5,
readyTimeout: 10000,
};
if (password) {
params.password = password;
} else if (privateKey) {
params.privateKey = privateKey;
if (passphrase) params.passphrase = passphrase;
}
return params;
}
Expand Down
13 changes: 7 additions & 6 deletions lib/attachments.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ const { AttachmentProcessor } = require('@elastic.io/component-commons-library')
const { unixTimeToIsoDate } = require('./utils/utils');
const { MAX_FILE_SIZE } = require('./constants');

async function addAttachment(msg, name, stream, contentLength) {
async function addAttachment(msg, name, getStream, contentLength) {
try {
if (contentLength > MAX_FILE_SIZE) {
throw new Error(`File size is ${contentLength} bytes, it violates the variable MAX_FILE_SIZE, which is currently set to ${MAX_FILE_SIZE} bytes`);
}
const result = await new AttachmentProcessor().uploadAttachment(stream);
const attachmentUrl = `${result.config.url}${result.data.objectId}?storage_type=maester`;
const attachmentProcessor = new AttachmentProcessor();
const attachmentId = await attachmentProcessor.uploadAttachment(getStream);
const attachmentUrl = attachmentProcessor.getMaesterAttachmentUrlById(attachmentId);
const curUsed = process.memoryUsage();
for (const key in curUsed) { this.logger.debug(`post done...' ${key} ${Math.round((curUsed[key] / 1024 / 1024) * 100) / 100} MB`); }
msg.attachments[name] = {
Expand Down Expand Up @@ -38,15 +39,15 @@ async function uploadFromSftpToAttachmentBuffer(context, body, dir) {
throw new Error(`File size is ${fileSize} bytes, it violates the variable MAX_FILE_SIZE, which is currently set to ${MAX_FILE_SIZE} bytes`);
}

const readableStream = await client.getReadStream(filePath);
const getStream = async () => client.getReadStream(filePath);
const attachmentProcessor = new AttachmentProcessor();

const uploadResult = await attachmentProcessor.uploadAttachment(readableStream);
const attachmentId = await attachmentProcessor.uploadAttachment(getStream);
const curUsed = process.memoryUsage();
for (const key in curUsed) { logger.debug(`post done...' ${key} ${Math.round((curUsed[key] / 1024 / 1024) * 100) / 100} MB`); }

const attachmentUrl = `${uploadResult.config.url}${uploadResult.data.objectId}?storage_type=maester`;
logger.info('File is successfully uploaded to URL');
const attachmentUrl = attachmentProcessor.getMaesterAttachmentUrlById(attachmentId);
const attachments = {
[body.name]: {
url: attachmentUrl,
Expand Down
4 changes: 2 additions & 2 deletions lib/triggers/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ async function createMessageForFile(sftp, dir, file) {
this.logger.info('Moving file into staging folder');
await moveFile.call(this, sftp, dir, fileName, newFileName);
this.logger.info('Reading file into read stream');
const readableStream = await sftp.getReadStream(Sftp.createPath(dir, `${PROCESSED_FOLDER_NAME}/${newFileName}`));
await attachments.addAttachment.call(this, msg, fileName, readableStream, file.size);
const getStream = async () => sftp.getReadStream(Sftp.createPath(dir, `${PROCESSED_FOLDER_NAME}/${newFileName}`));
await attachments.addAttachment.call(this, msg, fileName, getStream, file.size);
return msg;
}

Expand Down
Loading

0 comments on commit 499ce62

Please sign in to comment.