From fe22383dc1aa491b7f05231b31cdb8c0b9714a00 Mon Sep 17 00:00:00 2001 From: Illya Date: Fri, 12 Aug 2022 14:00:05 +0300 Subject: [PATCH] Add user-agent header (#119) --- CHANGELOG.md | 3 ++ component.json | 2 +- lib/actions/upload.js | 3 +- lib/attachments.js | 5 ++- lib/utils/upsertUtil.js | 3 +- lib/utils/utils.js | 10 +++++ package-lock.json | 95 ++++++++++++++++++++++++++++++----------- package.json | 2 +- 8 files changed, 93 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b478a8b..869ed76 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/component.json b/component.json index f6a71e8..5b0ffd5 100644 --- a/component.json +++ b/component.json @@ -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": { diff --git a/lib/actions/upload.js b/lib/actions/upload.js index 9780057..fca77c4 100644 --- a/lib/actions/upload.js +++ b/lib/actions/upload.js @@ -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 @@ -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'); diff --git a/lib/attachments.js b/lib/attachments.js index d029f77..ee83984 100644 --- a/lib/attachments.js +++ b/lib/attachments.js @@ -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(); @@ -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(); diff --git a/lib/utils/upsertUtil.js b/lib/utils/upsertUtil.js index 91e2c2c..0e7bb45 100644 --- a/lib/utils/upsertUtil.js +++ b/lib/utils/upsertUtil.js @@ -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) { @@ -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 }; } diff --git a/lib/utils/utils.js b/lib/utils/utils.js index f28e471..bdd2c95 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -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; @@ -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; diff --git a/package-lock.json b/package-lock.json index 79e0e81..28637a1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -374,12 +374,12 @@ } }, "@elastic.io/component-commons-library": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/@elastic.io/component-commons-library/-/component-commons-library-3.0.0.tgz", - "integrity": "sha512-BrQKXDuHTZfTxt7dMpGg7P+nmMwiXMm2EQqQzA6+3EjhTYYdbjyt34lkFaT2TlI1yNt2NsDzG0If4d6NEQ1tcA==", + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/@elastic.io/component-commons-library/-/component-commons-library-3.0.1.tgz", + "integrity": "sha512-+hf+b0UZ+rx2ROFNmx0CLOvy2Q2a4nO+dZ2W16E6d9dUVZfCGS/n08wwCpGfUakYum73VbupFiC03bsJKoG3MA==", "requires": { "@elastic.io/jsonata-moment": "1.1.4", - "@elastic.io/maester-client": "4.0.0", + "@elastic.io/maester-client": "4.0.2", "@elastic.io/ntlm-client": "1.0.0", "async": "3.2.3", "axios": "0.27.2", @@ -453,12 +453,12 @@ } }, "@elastic.io/maester-client": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/@elastic.io/maester-client/-/maester-client-4.0.0.tgz", - "integrity": "sha512-07AD2RHqUehPIZ0rhUWRQlHG0PwZMgqsBHplmPGfBwFfLsPDkQYzBeHPfl/trYcMOT9zsOgAt6fXrTrVjchSXw==", + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/@elastic.io/maester-client/-/maester-client-4.0.2.tgz", + "integrity": "sha512-faW94GyfSuJ3a0+1+9w+BVlm53rcFAHUm3Q6ovqbDH3gs/RURHqm1ltfOnRGA9ZFqaHa4JlN6c1DZDrAYnNQOQ==", "requires": { "@elastic.io/bunyan-logger": "1.0.8", - "@elastic.io/component-commons-library": "3.0.0-dev.7", + "@elastic.io/component-commons-library": "3.0.0", "@types/sinon": "10.0.0", "axios": "0.26.1", "form-data": "4.0.0", @@ -477,9 +477,9 @@ } }, "@elastic.io/component-commons-library": { - "version": "3.0.0-dev.7", - "resolved": "https://registry.npmjs.org/@elastic.io/component-commons-library/-/component-commons-library-3.0.0-dev.7.tgz", - "integrity": "sha512-ZphFZw5qp0hpCTL+3ZoQg+14zs+RV6l9YKGs6sE/G2aURxzq6WMPRdyDp3z9jQcBnsfdyg2zG1w7Ogw9Yf3OFg==", + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/@elastic.io/component-commons-library/-/component-commons-library-3.0.0.tgz", + "integrity": "sha512-BrQKXDuHTZfTxt7dMpGg7P+nmMwiXMm2EQqQzA6+3EjhTYYdbjyt34lkFaT2TlI1yNt2NsDzG0If4d6NEQ1tcA==", "requires": { "@elastic.io/jsonata-moment": "1.1.4", "@elastic.io/ntlm-client": "1.0.0", @@ -517,12 +517,12 @@ } }, "@elastic.io/maester-client": { - "version": "4.0.0-dev.11", - "resolved": "https://registry.npmjs.org/@elastic.io/maester-client/-/maester-client-4.0.0-dev.11.tgz", - "integrity": "sha512-eUCuiIlgQ4HcoBVPfwkJIqG8kGKFoVtMDSfpGQmkJso1lFCgYr/T2WSr0V30Bm0sFdgRDzo/D/axEEjZcU6pmw==", + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/@elastic.io/maester-client/-/maester-client-4.0.0.tgz", + "integrity": "sha512-07AD2RHqUehPIZ0rhUWRQlHG0PwZMgqsBHplmPGfBwFfLsPDkQYzBeHPfl/trYcMOT9zsOgAt6fXrTrVjchSXw==", "requires": { "@elastic.io/bunyan-logger": "1.0.8", - "@elastic.io/component-commons-library": "3.0.0-dev.6", + "@elastic.io/component-commons-library": "3.0.0-dev.7", "@types/sinon": "10.0.0", "axios": "0.26.1", "form-data": "4.0.0", @@ -533,9 +533,9 @@ }, "dependencies": { "@elastic.io/component-commons-library": { - "version": "3.0.0-dev.6", - "resolved": "https://registry.npmjs.org/@elastic.io/component-commons-library/-/component-commons-library-3.0.0-dev.6.tgz", - "integrity": "sha512-gzqOnySPYDF1uJUrHDYmhuBjoITKJcWKfOG98dZyrQFZLgrsRK3kHNWLgHq0Z8nYCifYDXBi8bbtxqJNXg9LYQ==", + "version": "3.0.0-dev.7", + "resolved": "https://registry.npmjs.org/@elastic.io/component-commons-library/-/component-commons-library-3.0.0-dev.7.tgz", + "integrity": "sha512-ZphFZw5qp0hpCTL+3ZoQg+14zs+RV6l9YKGs6sE/G2aURxzq6WMPRdyDp3z9jQcBnsfdyg2zG1w7Ogw9Yf3OFg==", "requires": { "@elastic.io/jsonata-moment": "1.1.4", "@elastic.io/ntlm-client": "1.0.0", @@ -562,11 +562,12 @@ } }, "@elastic.io/maester-client": { - "version": "4.0.0-dev.10", - "resolved": "https://registry.npmjs.org/@elastic.io/maester-client/-/maester-client-4.0.0-dev.10.tgz", - "integrity": "sha512-XRE778tB3sotc3KHycVkIp6mOEjX937MDcf2j3FAja4LZBl/ehgR7PtrZkm/JsapCAiMv1MNkgsXST5D3odD2A==", + "version": "4.0.0-dev.11", + "resolved": "https://registry.npmjs.org/@elastic.io/maester-client/-/maester-client-4.0.0-dev.11.tgz", + "integrity": "sha512-eUCuiIlgQ4HcoBVPfwkJIqG8kGKFoVtMDSfpGQmkJso1lFCgYr/T2WSr0V30Bm0sFdgRDzo/D/axEEjZcU6pmw==", "requires": { "@elastic.io/bunyan-logger": "1.0.8", + "@elastic.io/component-commons-library": "3.0.0-dev.6", "@types/sinon": "10.0.0", "axios": "0.26.1", "form-data": "4.0.0", @@ -574,6 +575,52 @@ "jsonwebtoken": "8.5.1", "sinon": "11.1.2", "stream-mime-type": "1.0.2" + }, + "dependencies": { + "@elastic.io/component-commons-library": { + "version": "3.0.0-dev.6", + "resolved": "https://registry.npmjs.org/@elastic.io/component-commons-library/-/component-commons-library-3.0.0-dev.6.tgz", + "integrity": "sha512-gzqOnySPYDF1uJUrHDYmhuBjoITKJcWKfOG98dZyrQFZLgrsRK3kHNWLgHq0Z8nYCifYDXBi8bbtxqJNXg9LYQ==", + "requires": { + "@elastic.io/jsonata-moment": "1.1.4", + "@elastic.io/ntlm-client": "1.0.0", + "async": "3.2.3", + "axios": "0.27.2", + "bunyan": "1.8.14", + "bunyan-format": "0.2.1", + "bunyan-serializers": "0.0.2", + "elasticio-node": "0.0.9", + "remove-leading-slash": "1.0.1", + "remove-trailing-slash": "0.1.1", + "request": "2.88.2" + }, + "dependencies": { + "axios": { + "version": "0.27.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.27.2.tgz", + "integrity": "sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==", + "requires": { + "follow-redirects": "^1.14.9", + "form-data": "^4.0.0" + } + } + } + }, + "@elastic.io/maester-client": { + "version": "4.0.0-dev.10", + "resolved": "https://registry.npmjs.org/@elastic.io/maester-client/-/maester-client-4.0.0-dev.10.tgz", + "integrity": "sha512-XRE778tB3sotc3KHycVkIp6mOEjX937MDcf2j3FAja4LZBl/ehgR7PtrZkm/JsapCAiMv1MNkgsXST5D3odD2A==", + "requires": { + "@elastic.io/bunyan-logger": "1.0.8", + "@types/sinon": "10.0.0", + "axios": "0.26.1", + "form-data": "4.0.0", + "get-stream": "6.0.1", + "jsonwebtoken": "8.5.1", + "sinon": "11.1.2", + "stream-mime-type": "1.0.2" + } + } } }, "bunyan": { @@ -4666,9 +4713,9 @@ } }, "token-types": { - "version": "4.2.0", - "resolved": "https://registry.npmjs.org/token-types/-/token-types-4.2.0.tgz", - "integrity": "sha512-P0rrp4wUpefLncNamWIef62J0v0kQR/GfDVji9WKY7GDCWy5YbVSrKUTam07iWPZQGy0zWNOfstYTykMmPNR7w==", + "version": "4.2.1", + "resolved": "https://registry.npmjs.org/token-types/-/token-types-4.2.1.tgz", + "integrity": "sha512-6udB24Q737UD/SDsKAHI9FCRP7Bqc9D/MQUV02ORQg5iskjtLJlZJNdN4kKtcdtwCeWIwIHDGaUsTsCCAa8sFQ==", "requires": { "@tokenizer/token": "^0.3.0", "ieee754": "^1.2.1" diff --git a/package.json b/package.json index ffd9500..4b506d0 100644 --- a/package.json +++ b/package.json @@ -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",