Skip to content

Commit

Permalink
Applied PR corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
Nataniel López committed Mar 4, 2020
1 parent caaa624 commit 4d4f859
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -59,3 +59,6 @@ typings/

# next.js build output
.next

# VSCode
.vscode/
4 changes: 2 additions & 2 deletions lib/aws-wrappers.js
Expand Up @@ -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();
}
}
Expand All @@ -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();
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/log-error.js
Expand Up @@ -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
};

}
Expand Down
11 changes: 8 additions & 3 deletions lib/log.js
Expand Up @@ -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,
Expand All @@ -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: {
Expand Down
24 changes: 24 additions & 0 deletions tests/log-test.js
Expand Up @@ -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;
Expand Down

0 comments on commit 4d4f859

Please sign in to comment.