Skip to content

Commit 6f56aae

Browse files
authored
Made secrets only load once per container lifetime (#30)
1 parent 8800f43 commit 6f56aae

File tree

5 files changed

+4659
-27
lines changed

5 files changed

+4659
-27
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 2.0.13 (May 26, 2022)
2+
* Made secrets only load once per container lifetime (per token expiration time in case of OAUTH2)
3+
14
## 2.0.12 (April 08, 2022)
25

36
* Update Sailor version to 2.6.27

component.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"title": "REST API V2",
3-
"version": "2.0.12",
3+
"version": "2.0.13",
44
"description": "A generic connector for accessing HTTP and REST APIs .",
55
"authClientTypes": [
66
"oauth2",
@@ -141,4 +141,4 @@
141141
}
142142
}
143143
}
144-
}
144+
}

lib/utils.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ const authTypes = {
5252

5353
const CREDS_HEADER_TYPE = 'CREDS_HEADER_TYPE';
5454

55+
let secret;
56+
5557
/**
5658
* Executes the action's/trigger's logic by sending a request to the assigned URL and emitting response to the platform.
5759
* The function returns a Promise sending a request and resolving the response as platform message.
@@ -109,7 +111,9 @@ module.exports.processMethod = async function (msg, cfg) {
109111

110112
const existingAuthHeader = (headers || []).find((header) => header._type === CREDS_HEADER_TYPE);
111113

112-
const secret = await getSecret(this, cfg.secretId);
114+
if (!secret) {
115+
secret = await getSecret(this, cfg.secretId);
116+
}
113117
switch (secret.type) {
114118
case authTypes.BASIC:
115119
if (existingAuthHeader) {
@@ -169,6 +173,7 @@ module.exports.processMethod = async function (msg, cfg) {
169173
doImmediateRetry = false;
170174

171175
return await buildRequestBody()
176+
// eslint-disable-next-line no-loop-func
172177
.then(async () => {
173178
emitter.logger.trace('Got request body');
174179
let result;
@@ -177,17 +182,32 @@ module.exports.processMethod = async function (msg, cfg) {
177182
iteration -= 1;
178183
try {
179184
result = await request(requestOptions); // eslint-disable-line
185+
// 'requestOptions.simple' means that in case of 401 or 403 an exception won't be thrown.
186+
// For this case we throw an error manually.
187+
if ((result.statusCode === 403 || result.statusCode === 401) && secret.type === authTypes.OAUTH2) {
188+
const err = new Error('Request unauthorized. Failing...');
189+
err.statusCode = result.statusCode;
190+
throw err;
191+
}
180192
break;
181193
} catch (e) {
182194
this.logger.error('Got request error');
183195
if ((e.statusCode === 403 || e.statusCode === 401) && secret.type === authTypes.OAUTH2) {
184-
try {
185-
this.logger.info('Going to refresh token');
186-
const token = await refreshToken(this, cfg.secretId); // eslint-disable-line
187-
this.logger.info('Token successfully refreshed');
188-
requestOptions.headers.authorization = `Bearer ${token}`;
189-
} catch (e) {
190-
this.logger.error('Failed to refresh token');
196+
const newSecret = await getSecret(this, cfg.secretId);
197+
if (secret.credentials.access_token !== newSecret.credentials.access_token) {
198+
this.logger.info('Secret has been changed, trying to use new one...');
199+
secret = newSecret;
200+
requestOptions.headers.authorization = `Bearer ${secret.credentials.access_token}`;
201+
} else {
202+
try {
203+
this.logger.info('Going to refresh token');
204+
await refreshToken(this, cfg.secretId);
205+
secret = await getSecret(this, cfg.secretId);
206+
this.logger.info('Token successfully refreshed');
207+
requestOptions.headers.authorization = `Bearer ${secret.credentials.access_token}`;
208+
} catch (e) {
209+
this.logger.error('Failed to refresh token');
210+
}
191211
}
192212
} else {
193213
throw e;

0 commit comments

Comments
 (0)