added function to get an AWS security token using STS

This commit is contained in:
Dave Horton
2023-11-27 09:26:41 -05:00
parent 7df4e2f4c7
commit 1f52cd4f08
7 changed files with 3165 additions and 2382 deletions
+44
View File
@@ -0,0 +1,44 @@
const { STSClient, GetSessionTokenCommand } = require('@aws-sdk/client-sts');
const {makeAwsKey, noopLogger} = require('./utils');
const debug = require('debug')('jambonz:speech-utils');
const EXPIRY = 3600;
async function getAwsAuthToken(
logger,
createHash, retrieveHash,
awsAccessKeyId, awsSecretAccessKey, awsRegion) {
logger = logger || noopLogger;
try {
const key = makeAwsKey(awsAccessKeyId);
const obj = await retrieveHash(key);
if (obj) return {...obj, servedFromCache: true};
/* access token not found in cache, so generate it using STS */
const stsClient = new STSClient({ region: awsRegion });
const params = {
accessKeyId: awsAccessKeyId,
secretAccessKey: awsSecretAccessKey,
DurationSeconds: EXPIRY
};
const command = new GetSessionTokenCommand(params);
const data = await stsClient.send(command);
const credentials = {
accessKeyId: data.Credentials.AccessKeyId,
secretAccessKey: data.Credentials.SecretAccessKey,
sessionToken: data.Credentials.SessionToken
};
/* expire 10 minutes before the hour, so we don't lose the use of it during a call */
createHash(key, credentials, EXPIRY - 600)
.catch((err) => logger.error(err, `Error saving hash for key ${key}`));
return {...credentials, servedFromCache: false};
} catch (err) {
debug(err, 'getAwsAuthToken: Error retrieving AWS auth token');
logger.error(err, 'getAwsAuthToken: Error retrieving AWS auth token');
throw err;
}
}
module.exports = getAwsAuthToken;
+7
View File
@@ -43,6 +43,12 @@ function makeIbmKey(apiKey) {
return `ibm:${hash.digest('hex')}`;
}
function makeAwsKey(awsAccessKeyId) {
const hash = crypto.createHash('sha1');
hash.update(awsAccessKeyId);
return `aws:${hash.digest('hex')}`;
}
function makeNuanceKey(clientId, secret, scope) {
const hash = crypto.createHash('sha1');
hash.update(`${clientId}:${secret}:${scope}`);
@@ -110,6 +116,7 @@ module.exports = {
makeSynthKey,
makeNuanceKey,
makeIbmKey,
makeAwsKey,
getNuanceAccessToken,
createNuanceClient,
createKryptonClient,