Skip to content

Commit

Permalink
Add user-agent header (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
EmptyInfinity authored Aug 12, 2022
1 parent 499ce62 commit fe22383
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 30 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.5.1 (August 12, 2022)
* Updated @elasticio/component-commons-library to v3.0.1

## 1.5.0 (July 29, 2022)
* Added retries on errors while connecting SFTP client
* Upgrade component-commons-library version to 3.0.0
Expand Down
2 changes: 1 addition & 1 deletion component.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "SFTP",
"description": "Provides file access and transfer using SSH File Transfer Protocol",
"version": "1.5.0",
"version": "1.5.1",
"credentials": {
"fields": {
"host": {
Expand Down
3 changes: 2 additions & 1 deletion lib/actions/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { AttachmentProcessor } = require('@elastic.io/component-commons-library')
const path = require('path');
const eioUtils = require('elasticio-node').messages;
const Sftp = require('../Sftp');
const { getUserAgent } = require('../utils/utils');

/**
* This method will be called from elastic.io platform providing following data
Expand Down Expand Up @@ -39,7 +40,7 @@ exports.process = async function processAction(msg, cfg) {
this.logger.debug('Writing attachment to targetPath');

this.logger.debug('Getting attachment...');
const file = await new AttachmentProcessor().getAttachment(attachment.url, 'stream');
const file = await new AttachmentProcessor(getUserAgent()).getAttachment(attachment.url, 'stream');
this.logger.debug('Uploading attachment to targetPath');
await sftp.put(file.data, targetPath, { encoding: null });
this.logger.info('Attachment uploaded successfully');
Expand Down
5 changes: 3 additions & 2 deletions lib/attachments.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ const path = require('path');
const { AttachmentProcessor } = require('@elastic.io/component-commons-library');
const { unixTimeToIsoDate } = require('./utils/utils');
const { MAX_FILE_SIZE } = require('./constants');
const { getUserAgent } = require('./utils/utils');

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 attachmentProcessor = new AttachmentProcessor();
const attachmentProcessor = new AttachmentProcessor(getUserAgent());
const attachmentId = await attachmentProcessor.uploadAttachment(getStream);
const attachmentUrl = attachmentProcessor.getMaesterAttachmentUrlById(attachmentId);
const curUsed = process.memoryUsage();
Expand Down Expand Up @@ -40,7 +41,7 @@ async function uploadFromSftpToAttachmentBuffer(context, body, dir) {
}

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

const attachmentId = await attachmentProcessor.uploadAttachment(getStream);
const curUsed = process.memoryUsage();
Expand Down
3 changes: 2 additions & 1 deletion lib/utils/upsertUtil.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { UpsertObjectById } = require('@elastic.io/oih-standard-library/lib/actions/upsert');
const { AttachmentProcessor } = require('@elastic.io/component-commons-library');
const path = require('path');
const { getUserAgent } = require('./utils');

class SftpUpsertObject extends UpsertObjectById {
constructor(logger, client) {
Expand All @@ -20,7 +21,7 @@ class SftpUpsertObject extends UpsertObjectById {

// eslint-disable-next-line no-unused-vars,class-methods-use-this
async getObjectFromMessage(msg, cfg) {
const fileStream = await new AttachmentProcessor().getAttachment(msg.body.attachmentUrl, 'stream');
const fileStream = await new AttachmentProcessor(getUserAgent()).getAttachment(msg.body.attachmentUrl, 'stream');
return { fileStream: fileStream.data };
}

Expand Down
10 changes: 10 additions & 0 deletions lib/utils/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const moment = require('moment');
const Client = require('ssh2-sftp-client');
const packageJson = require('../../package.json');
const compJson = require('../../component.json');

function getDirectory(cfg) {
const { directory } = cfg;
Expand All @@ -26,6 +28,14 @@ class ClientReadStream extends Client {
}
}

const getUserAgent = () => {
const { name: compName } = packageJson;
const { version: compVersion } = compJson;
const compCommonsLibVersion = packageJson.dependencies['@elastic.io/component-commons-library'];
return `${compName}/${compVersion} component-commons-library/${compCommonsLibVersion}`;
};

exports.getUserAgent = getUserAgent;
exports.getDirectory = getDirectory;
exports.Client = ClientReadStream;
exports.unixTimeToIsoDate = unixTimeToIsoDate;
Expand Down
95 changes: 71 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"author": "elastic.io GmbH",
"license": "Apache-2.0",
"dependencies": {
"@elastic.io/component-commons-library": "3.0.0",
"@elastic.io/component-commons-library": "3.0.1",
"@elastic.io/oih-standard-library": "2.0.3",
"async": "3.2.3",
"elasticio-node": "0.0.9",
Expand Down

0 comments on commit fe22383

Please sign in to comment.