Skip to content

Commit 7bd6325

Browse files
authored
#6233 fix sailor ignores error from maester; update test (#191)
* #6233 fix sailor ignores error from maester; update test * #6233 bump object-storage-client version * #6233 npm audit fix * #6233 add better-npm-audit * #6233 code style * #6233 cleanup; improve log/error message * #6233 run audit from packege.json -> scripts * #6233 update test * #6233 code style * #6233 use latest version for object-storage-client * #6233 fix incorrect params passed to objectStorage methods * #6233 update tests * #6233 update version * #6233 update changelog
1 parent a29a0a9 commit 7bd6325

File tree

7 files changed

+376
-369
lines changed

7 files changed

+376
-369
lines changed

.circleci/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs: # a collection of steps
2222
- ./node_modules
2323
- run:
2424
name: "Running audit"
25-
command: npm audit --production --audit-level=high
25+
command: npm run audit
2626
- run: # run tests
2727
name: test
2828
command: npm test

.nsprc

Whitespace-only changes.

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.6.28 (June 21, 2022)
2+
3+
* Fix: "sailor-nodejs ignores errors from maester during lightweight message upload" [#6233](https://github.com/elasticio/elasticio/issues/6233))
4+
15
## 2.6.27 (March 10, 2022)
26

37
* Added npm audit to CI and fixed all dependencies

lib/sailor.js

+39-41
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,10 @@ class Sailor {
280280
logger.info('Going to fetch message body.', { objectId });
281281

282282
try {
283-
object = await this.objectStorage.getAsJSON(objectId, this.settings.OBJECT_STORAGE_TOKEN);
283+
object = await this.objectStorage.getAsJSON(
284+
objectId,
285+
{ jwtPayloadOrToken: this.settings.OBJECT_STORAGE_TOKEN }
286+
);
284287
} catch (e) {
285288
log.error(e);
286289
throw new Error(`Failed to get message body with id=${objectId}`);
@@ -292,17 +295,12 @@ class Sailor {
292295
return object;
293296
}
294297

295-
async uploadMessageBody(bodyBuf) {
296-
let id;
297-
298-
try {
299-
const stream = () => Readable.from(bodyBuf);
300-
id = await this.objectStorage.addAsStream(stream, this.settings.OBJECT_STORAGE_TOKEN);
301-
} catch (e) {
302-
log.error(e);
303-
}
304-
305-
return id;
298+
uploadMessageBody(bodyBuf) {
299+
const stream = () => Readable.from(bodyBuf);
300+
return this.objectStorage.addAsStream(
301+
stream,
302+
{ jwtPayloadOrToken: this.settings.OBJECT_STORAGE_TOKEN }
303+
);
306304
}
307305

308306
async runExec(module, payload, message, outgoingMessageHeaders, stepData, timeStart, logger) {
@@ -386,38 +384,38 @@ class Sailor {
386384
OBJECT_STORAGE_SIZE_THRESHOLD: settings.OBJECT_STORAGE_SIZE_THRESHOLD
387385
}
388386
);
389-
const [bodyId, ...passthroughIds] = await Promise.all([
390-
that.uploadMessageBody(bodyBuf),
391-
...passthroughBufs.map(async ({ stepId, body, id }) => {
392-
const bodyId = id || await that.uploadMessageBody(body);
393-
return { stepId, bodyId };
394-
})
395-
]);
396-
397-
if (bodyId) {
398-
logger.info('Message body uploaded', { id: bodyId });
399-
const { headers } = data;
400-
data.body = {};
401-
data.headers = {
402-
...(headers || {}),
403-
[OBJECT_ID_HEADER]: bodyId
404-
};
405-
} else {
406-
logger.info('Message body not uploaded');
387+
388+
let bodyId;
389+
let passthroughIds;
390+
try {
391+
[bodyId, ...passthroughIds] = await Promise.all([
392+
that.uploadMessageBody(bodyBuf),
393+
...passthroughBufs.map(async ({ stepId, body, id }) => {
394+
const bodyId = id || await that.uploadMessageBody(body);
395+
return { stepId, bodyId };
396+
})
397+
]);
398+
} catch (e) {
399+
logger.error(e, 'Error during message/passthrough body upload');
400+
return onError(new Error('Lightweight message/passthrough body upload error'));
407401
}
408402

403+
logger.info('Message body uploaded', { id: bodyId });
404+
const { headers } = data;
405+
data.body = {};
406+
data.headers = {
407+
...(headers || {}),
408+
[OBJECT_ID_HEADER]: bodyId
409+
};
410+
409411
for (const { stepId, bodyId } of passthroughIds) {
410-
if (bodyId) {
411-
logger.info('Passthrough Message body uploaded', { stepId, id: bodyId });
412-
const { [stepId]: { headers } } = passthrough;
413-
data.passthrough[stepId].body = {};
414-
data.passthrough[stepId].headers = {
415-
...(headers || {}),
416-
[OBJECT_ID_HEADER]: bodyId
417-
};
418-
} else {
419-
logger.info('Passtrough Message body not uploaded', { stepId, id: bodyId });
420-
}
412+
logger.info('Passthrough Message body uploaded', { stepId, id: bodyId });
413+
const { [stepId]: { headers } } = passthrough;
414+
data.passthrough[stepId].body = {};
415+
data.passthrough[stepId].headers = {
416+
...(headers || {}),
417+
[OBJECT_ID_HEADER]: bodyId
418+
};
421419
}
422420

423421
} else {

mocha_spec/unit/sailor.spec.js

+9-42
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,7 @@ describe('Sailor', () => {
10631063
beforeEach(async () => {
10641064
const getObjectStub = sandbox.stub(sailor.objectStorage, 'getAsJSON');
10651065
bodyRequestStub = getObjectStub
1066-
.withArgs(bodyObjectId, settings.OBJECT_STORAGE_TOKEN)
1066+
.withArgs(bodyObjectId, { jwtPayloadOrToken: settings.OBJECT_STORAGE_TOKEN })
10671067
.resolves(body);
10681068
passthroughRequestStub = bodyRequestStub
10691069
.withArgs(passthroughObjectId)
@@ -1144,7 +1144,7 @@ describe('Sailor', () => {
11441144
beforeEach(async () => {
11451145
const getObjectStub = sandbox.stub(sailor.objectStorage, 'getAsJSON');
11461146
bodyRequestStub = getObjectStub
1147-
.withArgs(bodyObjectId, settings.OBJECT_STORAGE_TOKEN)
1147+
.withArgs(bodyObjectId, { jwtPayloadOrToken: settings.OBJECT_STORAGE_TOKEN })
11481148
.resolves(body);
11491149
passthroughRequestStub = bodyRequestStub.withArgs(passthroughObjectId).rejects(new Error());
11501150

@@ -1195,7 +1195,7 @@ describe('Sailor', () => {
11951195

11961196
passthroughRequestStub = sandbox
11971197
.stub(sailor.objectStorage, 'getAsJSON')
1198-
.withArgs(passthroughObjectId, settings.OBJECT_STORAGE_TOKEN)
1198+
.withArgs(passthroughObjectId, { jwtPayloadOrToken: settings.OBJECT_STORAGE_TOKEN })
11991199
.resolves(passThroughBody);
12001200

12011201
await sailor.connect();
@@ -1310,7 +1310,7 @@ describe('Sailor', () => {
13101310
});
13111311

13121312
sandbox.stub(sailor.objectStorage, 'getAsJSON')
1313-
.withArgs(passthroughObjectId, settings.OBJECT_STORAGE_TOKEN)
1313+
.withArgs(passthroughObjectId, { jwtPayloadOrToken: settings.OBJECT_STORAGE_TOKEN })
13141314
.resolves({ passthrough: 'body' });
13151315

13161316
await sailor.connect();
@@ -1391,46 +1391,13 @@ describe('Sailor', () => {
13911391
expect(sailor.apiClient.tasks.retrieveStep).to.have.been.calledOnce;
13921392
expect(fakeAMQPConnection.connect).to.have.been.calledOnce;
13931393
sinon.assert.calledTwice(addObjectStub);
1394-
sinon.assert.notCalled(fakeAMQPConnection.sendError);
1395-
sinon.assert.calledOnce(fakeAMQPConnection.sendData);
1396-
sinon.assert.calledWith(
1397-
fakeAMQPConnection.sendData,
1398-
{
1399-
body: { items: [1,2,3,4,5,6] },
1400-
headers: {},
1401-
passthrough: {
1402-
...payload.passthrough,
1403-
step_2: {
1404-
body: {},
1405-
headers: {
1406-
[Sailor.OBJECT_ID_HEADER]: passthroughObjectId //reuse already uploaded
1407-
}
1408-
},
1409-
step_1: {
1410-
headers: {},
1411-
body: { items: [1,2,3,4,5,6] }
1412-
}
1413-
}
1414-
},
1394+
expect(fakeAMQPConnection.sendError).to.have.been.calledOnce.and.calledWith(
14151395
sinon.match({
1416-
compId: '5559edd38968ec0736000456',
1417-
containerId: 'dc1c8c3f-f9cb-49e1-a6b8-716af9e15948',
1418-
end: sinon.match.number,
1419-
execId: 'some-exec-id',
1420-
function: 'data_trigger',
1421-
messageId: sinon.match.string,
1422-
parentMessageId: message.properties.headers.messageId,
1423-
start: sinon.match.number,
1424-
stepId: 'step_1',
1425-
taskId: '5559edd38968ec0736000003',
1426-
threadId: message.properties.headers.threadId,
1427-
userId: '5559edd38968ec0736000002',
1428-
workspaceId: '5559edd38968ec073600683'
1429-
})
1396+
message: 'Lightweight message/passthrough body upload error',
1397+
stack: sinon.match.string
1398+
}),
14301399
);
1431-
14321400
expect(fakeAMQPConnection.ack).to.have.been.calledOnce.and.calledWith(message);
1433-
const [{ headers, passthrough }] = fakeAMQPConnection.sendData.getCall(0).args;
14341401
});
14351402
});
14361403
});
@@ -1450,7 +1417,7 @@ describe('Sailor', () => {
14501417
});
14511418

14521419
sandbox.stub(sailor.objectStorage, 'getAsJSON')
1453-
.withArgs(passthroughObjectId, settings.OBJECT_STORAGE_TOKEN)
1420+
.withArgs(passthroughObjectId, { jwtPayloadOrToken: settings.OBJECT_STORAGE_TOKEN })
14541421
.resolves({ passthrough: 'body' });
14551422

14561423
await sailor.connect();

0 commit comments

Comments
 (0)