diff --git a/.gitignore b/.gitignore index ad46b30..5123e7e 100644 --- a/.gitignore +++ b/.gitignore @@ -59,3 +59,6 @@ typings/ # next.js build output .next + +# VSCode +.vscode/ diff --git a/lib/aws-wrappers.js b/lib/aws-wrappers.js index 9f3d253..093e0cc 100644 --- a/lib/aws-wrappers.js +++ b/lib/aws-wrappers.js @@ -10,7 +10,7 @@ class FirehoseWrapper { /* istanbul ignore next */ // AWS generates the Firehose class on the fly, the putRecord method do not exists before creating the insance - async putRecord(record) { + putRecord(record) { return this._firehose.putRecord(record).promise(); } } @@ -23,7 +23,7 @@ class StsWrapper { /* istanbul ignore next */ // AWS generates the STS class on the fly, the assumeRole method do not exists before creating the insance - async assumeRole(params) { + assumeRole(params) { return this._sts.assumeRole(params).promise(); } } diff --git a/lib/log-error.js b/lib/log-error.js index 81f83bd..4fd30fe 100644 --- a/lib/log-error.js +++ b/lib/log-error.js @@ -7,7 +7,8 @@ class LogError extends Error { return { INVALID_LOG: 1, FIREHOSE_ERROR: 2, - NO_ENVIRONMENT: 3 + NO_ENVIRONMENT: 3, + ASSUME_ROLE_ERROR: 4 }; } diff --git a/lib/log.js b/lib/log.js index 124c234..0d931ac 100644 --- a/lib/log.js +++ b/lib/log.js @@ -150,12 +150,17 @@ class Log { static async _getCredentials() { - const { Credentials, Expiration } = await sts.assumeRole({ + const assumedRole = await sts.assumeRole({ RoleArn: this._roleArn, RoleSessionName: this._serviceName, DurationSeconds: ARN_DURATION }); + if(!assumedRole) + throw new LogError('Failed to assume role, invalid response.', LogError.codes.ASSUME_ROLE_ERROR); + + const { Credentials, Expiration } = assumedRole; + return { accessKeyId: Credentials.AccessKeyId, secretAccessKey: Credentials.SecretAccessKey, @@ -174,10 +179,10 @@ class Log { static async _add(log, attempts = 0) { - const firehose = await this._getFirehoseInstance(); - try { + const firehose = await this._getFirehoseInstance(); + await firehose.putRecord({ DeliveryStreamName: this.deliveryStreamName, Record: { diff --git a/tests/log-test.js b/tests/log-test.js index b07b8c0..12ae1a9 100644 --- a/tests/log-test.js +++ b/tests/log-test.js @@ -245,6 +245,30 @@ describe('Log', () => { sandbox.assert.notCalled(Firehose.prototype.putRecord); }); + it('Should not call Firehose putRecord when assume role rejects', async () => { + + sandbox.stub(STS.prototype, 'assumeRole') + .rejects(); + + sandbox.spy(Firehose.prototype, 'putRecord'); + + await Log.add('some-client', fakeLog); + + sandbox.assert.notCalled(Firehose.prototype.putRecord); + }); + + it('Should not call Firehose putRecord when assume role returns an invalid result', async () => { + + sandbox.stub(STS.prototype, 'assumeRole') + .resolves(null); + + sandbox.spy(Firehose.prototype, 'putRecord'); + + await Log.add('some-client', fakeLog); + + sandbox.assert.notCalled(Firehose.prototype.putRecord); + }); + it('Should emit an error when something goes wrong', async () => { let errorEmitted = false;