Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
160 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
'use strict'; | ||
|
||
const { STS, Firehose } = require('./aws-wrappers'); | ||
const LogError = require('./log-error'); | ||
|
||
const sts = new STS(); | ||
const ARN_DURATION = 1800; // 30 min | ||
const MAX_TIMEOUT = 500; | ||
|
||
class FirehoseInstance { | ||
|
||
static get serviceName() { | ||
return process.env.JANIS_SERVICE_NAME; | ||
} | ||
|
||
static get roleArn() { | ||
return process.env.LOG_ROLE_ARN; | ||
} | ||
|
||
/** | ||
* Returns a FirehoseInstance | ||
* | ||
* @returns {Object} A FireHoseInstance | ||
*/ | ||
static async getFirehoseInstance() { | ||
|
||
if(!this.validCredentials()) { | ||
|
||
|
||
const firehoseParams = { | ||
region: process.env.AWS_DEFAULT_REGION, | ||
httpOptions: { timeout: MAX_TIMEOUT } | ||
}; | ||
|
||
if(this.roleArn) { | ||
firehoseParams.credentials = await this.getCredentials(); | ||
this.credentialsExpiration = new Date(firehoseParams.credentials.expiration); | ||
} | ||
|
||
this.firehose = new Firehose(firehoseParams); | ||
} | ||
|
||
return this.firehose; | ||
} | ||
|
||
static validCredentials() { | ||
return this.firehose | ||
&& this.credentialsExpiration | ||
&& this.credentialsExpiration >= new Date(); | ||
} | ||
|
||
static async getCredentials() { | ||
|
||
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, | ||
sessionToken: Credentials.SessionToken, | ||
expiration: Expiration | ||
}; | ||
} | ||
} | ||
|
||
module.exports = FirehoseInstance; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use strict'; | ||
|
||
const Settings = require('@janiscommerce/settings'); | ||
|
||
module.exports = () => { | ||
|
||
const logRoleArn = Settings.get('logRoleArn'); | ||
|
||
return [ | ||
['envVars', { | ||
LOG_ROLE_ARN: logRoleArn | ||
}], | ||
['iamStatement', { | ||
action: 'Sts:AssumeRole', | ||
resource: logRoleArn | ||
}] | ||
]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters