Skip to content

user-agent #49

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
112 changes: 56 additions & 56 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,82 +13,82 @@ commands:
event: fail
custom: |
{
"blocks": [
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": ":red_circle: *$CIRCLE_PROJECT_REPONAME*:*$CIRCLE_TAG* build failed"
}
]
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "View Job"
},
"url": "${CIRCLE_BUILD_URL}"
}
]
}
]
"blocks": [
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": ":red_circle: *$CIRCLE_PROJECT_REPONAME*:*$CIRCLE_TAG* build failed"
}
]
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "View Job"
},
"url": "${CIRCLE_BUILD_URL}"
}
]
}
]
}
notify_on_pass:
steps:
- slack/notify:
event: pass
custom: |
{
"blocks": [
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": ":tada: *$CIRCLE_PROJECT_REPONAME*:*$CIRCLE_TAG* was successfully built and published"
}
]
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "View Job"
},
"url": "${CIRCLE_BUILD_URL}"
}
]
}
]
"blocks": [
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": ":tada: *$CIRCLE_PROJECT_REPONAME*:*$CIRCLE_TAG* was successfully built and published"
}
]
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "View Job"
},
"url": "${CIRCLE_BUILD_URL}"
}
]
}
]
}
jobs:
test:
docker: # run the steps with Docker
- image: cimg/base:stable
docker:
- image: circleci/node:16-stretch
steps:
- checkout
- node/install:
node-version: << pipeline.parameters.node-version >>
- node/install-packages:
cache-path: ./node_modules
override-ci-command: npm install
- run:
name: Audit Dependencies
command: npm audit --production --audit-level=high
- node/install-packages:
cache-path: ./node_modules
override-ci-command: npm install
- run:
name: test
name: Running Mocha Tests
command: npm test
build:
docker:
- image: cimg/base:stable
- image: circleci/node:16-stretch
user: root
steps:
- checkout
Expand Down Expand Up @@ -122,4 +122,4 @@ workflows:
branches:
ignore: /.*/
tags:
only: /^([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$/
only: /^([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$/
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 2.3.1 (August 26, 2022)

* Update Sailor version to 2.6.29
* Get rid of vulnerabilities in dependencies
* Update component-commons-library version to 3.0.2

## 2.3.0 (June 17, 2022)
* Added new `Type Of Search` - `External IDs` to `Upsert Object` action
* Implemented caching for metadata in `Upsert Object` action (metadata needs to find fields that contain attachment)
Expand Down
3 changes: 1 addition & 2 deletions component.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"description": "Customer relationship management (CRM) software & cloud computing from the leader in CRM solutions for businesses large & small.",
"docsUrl": "https://github.com/elasticio/salesforce-component-v2",
"url": "http://www.salesforce.com/",
"buildType": "docker",
"version": "2.3.0",
"version": "2.3.1-dev.1",
"authClientTypes": [
"oauth2"
],
Expand Down
4 changes: 2 additions & 2 deletions lib/actions/rawRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function processAction(msg, cfg) {
logger.debug('Trying to get access token');
try {
this.logger.debug('Fetching credentials by secretId');
const { credentials } = await getSecret(this, secretId);
const { credentials } = await getSecret(this, secretId, msg.id);
accessToken = credentials.access_token;
instanceUrl = credentials.undefined_params.instance_url;
logger.debug('Access token has been received');
Expand Down Expand Up @@ -66,7 +66,7 @@ async function processAction(msg, cfg) {
if (e.name === 'INVALID_SESSION_ID') {
try {
logger.debug('Session is expired, trying to refresh token...');
accessToken = await refreshToken(this, secretId);
accessToken = await refreshToken(this, secretId, msg.id);
this.logger.debug('Token is successfully refreshed');
} catch (err) {
logger.error('Failed to refresh token');
Expand Down
4 changes: 3 additions & 1 deletion lib/actions/upsert_v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { messages } = require('elasticio-node');
const { AttachmentProcessor } = require('@elastic.io/component-commons-library');
const { callJSForceMethod } = require('../helpers/wrapper');
const { createProperty, getLookupFieldsModelWithTypeOfSearch } = require('../helpers/utils');
const { getUserAgent } = require('../util');

const timeOut = process.env.UPSERT_TIME_OUT ? Number(process.env.UPSERT_TIME_OUT) : 120000;

Expand Down Expand Up @@ -70,9 +71,10 @@ module.exports.process = async function upsertObject(msg, cfg) {
this.logger.info(`Starting processing Upsert "${cfg.sobject}" object action`);

if (!meta) meta = await callJSForceMethod.call(this, cfg, 'describe');
const attachmentProcessor = new AttachmentProcessor(getUserAgent(), msg.id);
for (const field in meta.fields) {
if (field.type === 'base64' && msg.body[field.name]) {
const { data } = await new AttachmentProcessor().getAttachment(msg.body[field.name], 'arraybuffer');
const { data } = await attachmentProcessor.getAttachment(msg.body[field.name], 'arraybuffer');
msg.body[field.name] = data.toString('base64');
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/triggers/streamPlatformEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async function processTrigger(msg, configuration) {
throw new Error('secretId is missing in configuration, credentials cannot be fetched');
}
this.logger.debug('Fetching credentials by secretId');
const { credentials } = await getSecret(this, secretId);
const { credentials } = await getSecret(this, secretId, msg.id);
const accessToken = credentials.access_token;
const instanceUrl = credentials.undefined_params.instance_url;
this.logger.debug('Preparing SalesForce connection...');
Expand All @@ -39,7 +39,7 @@ async function processTrigger(msg, configuration) {
if (err.ext && err.ext.sfdc && err.ext.sfdc.failureReason && (err.ext.sfdc.failureReason === '401::Authentication invalid')) {
try {
this.logger.debug('Session is expired, trying to refresh token');
await refreshToken(this, secretId);
await refreshToken(this, secretId, msg.id);
this.logger.debug('Token is successfully refreshed');
} catch (error) {
this.logger.error('Failed to fetch refresh token');
Expand Down
22 changes: 20 additions & 2 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const axios = require('axios');
const client = require('elasticio-rest-node')();
const { URL } = require('url');
const path = require('path');
const packageJson = require('../package.json');
const compJson = require('../component.json');

const REQUEST_TIMEOUT = process.env.REQUEST_TIMEOUT || 10000; // 10s
const REQUEST_MAX_CONTENT_LENGTH = process.env.REQUEST_MAX_CONTENT_LENGTH || 10485760; // 10 MB
Expand All @@ -21,9 +23,17 @@ function addRetryCountInterceptorToAxios(ax) {
});
}

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

module.exports.base64Encode = (value) => Buffer.from(value).toString('base64');
module.exports.base64Decode = (value) => Buffer.from(value, 'base64').toString('utf-8');
module.exports.createSignedUrl = async () => client.resources.storage.createSignedUrl();
module.exports.getUserAgent = getUserAgent;

module.exports.uploadAttachment = async (url, payload) => {
const ax = axios.create();
Expand Down Expand Up @@ -64,7 +74,7 @@ function getSecretUri(secretId, isRefresh) {
return parsedUrl.toString();
}

module.exports.getSecret = async (emitter, secretId) => {
module.exports.getSecret = async (emitter, secretId, msgId) => {
const secretUri = getSecretUri(secretId);
emitter.logger.info('Going to fetch secret');
const ax = axios.create();
Expand All @@ -73,13 +83,17 @@ module.exports.getSecret = async (emitter, secretId) => {
timeout: REQUEST_TIMEOUT,
retry: REQUEST_MAX_RETRY,
delay: REQUEST_RETRY_DELAY,
headers: {
'User-Agent': getUserAgent(),
'x-request-id': `f:${process.env.ELASTICIO_FLOW_ID};s:${process.env.ELASTICIO_STEP_ID};m:${msgId || ''}`,
},
});
const parsedSecret = secret.data.data.attributes;
emitter.logger.info('Got secret');
return parsedSecret;
};

module.exports.refreshToken = async (emitter, secretId) => {
module.exports.refreshToken = async (emitter, secretId, msgId) => {
const secretUri = getSecretUri(secretId, true);
emitter.logger.info('going to refresh secret');
const ax = axios.create();
Expand All @@ -88,6 +102,10 @@ module.exports.refreshToken = async (emitter, secretId) => {
timeout: REQUEST_TIMEOUT,
retry: REQUEST_MAX_RETRY,
delay: REQUEST_RETRY_DELAY,
headers: {
'User-Agent': getUserAgent(),
'x-request-id': `f:${process.env.ELASTICIO_FLOW_ID};s:${process.env.ELASTICIO_STEP_ID};m:${msgId || ''}`,
},
});
const token = secret.data.data.attributes.credentials.access_token;
emitter.logger.info('Token refreshed');
Expand Down
Loading