mirror of
https://github.com/jambonz/speech-utils.git
synced 2026-01-25 02:08:26 +00:00
@@ -5,16 +5,16 @@ const EXPIRY = 3600;
|
|||||||
|
|
||||||
async function getAwsAuthToken(
|
async function getAwsAuthToken(
|
||||||
logger, createHash, retrieveHash,
|
logger, createHash, retrieveHash,
|
||||||
awsAccessKeyId, awsSecretAccessKey, awsRegion, roleArn = null) {
|
{accessKeyId, secretAccessKey, region, roleArn}) {
|
||||||
logger = logger || noopLogger;
|
logger = logger || noopLogger;
|
||||||
try {
|
try {
|
||||||
const key = makeAwsKey(roleArn || awsAccessKeyId);
|
const key = makeAwsKey(roleArn || accessKeyId);
|
||||||
const obj = await retrieveHash(key);
|
const obj = await retrieveHash(key);
|
||||||
if (obj) return {...obj, servedFromCache: true};
|
if (obj) return {...obj, servedFromCache: true};
|
||||||
|
|
||||||
let data;
|
let data;
|
||||||
if (roleArn) {
|
if (roleArn) {
|
||||||
const stsClient = new STSClient({ region: awsRegion});
|
const stsClient = new STSClient({ region });
|
||||||
const roleToAssume = { RoleArn: roleArn, RoleSessionName: 'Jambonz_Speech', DurationSeconds: EXPIRY};
|
const roleToAssume = { RoleArn: roleArn, RoleSessionName: 'Jambonz_Speech', DurationSeconds: EXPIRY};
|
||||||
const command = new AssumeRoleCommand(roleToAssume);
|
const command = new AssumeRoleCommand(roleToAssume);
|
||||||
|
|
||||||
@@ -22,10 +22,10 @@ async function getAwsAuthToken(
|
|||||||
} else {
|
} else {
|
||||||
/* access token not found in cache, so generate it using STS */
|
/* access token not found in cache, so generate it using STS */
|
||||||
const stsClient = new STSClient({
|
const stsClient = new STSClient({
|
||||||
region: awsRegion,
|
region,
|
||||||
credentials: {
|
credentials: {
|
||||||
accessKeyId: awsAccessKeyId,
|
accessKeyId,
|
||||||
secretAccessKey: awsSecretAccessKey,
|
secretAccessKey,
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
const command = new GetSessionTokenCommand({DurationSeconds: EXPIRY});
|
const command = new GetSessionTokenCommand({DurationSeconds: EXPIRY});
|
||||||
|
|||||||
@@ -107,7 +107,12 @@ const getAwsVoices = async(_client, createHash, retrieveHash, logger, credential
|
|||||||
} else if (roleArn) {
|
} else if (roleArn) {
|
||||||
client = new PollyClient({
|
client = new PollyClient({
|
||||||
region,
|
region,
|
||||||
credentials: await getAwsAuthToken(logger, createHash, retrieveHash, null, null, region, roleArn),
|
credentials: await getAwsAuthToken(
|
||||||
|
logger, createHash, retrieveHash,
|
||||||
|
{
|
||||||
|
region,
|
||||||
|
roleArn
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
client = new PollyClient({region});
|
client = new PollyClient({region});
|
||||||
|
|||||||
@@ -281,7 +281,12 @@ const synthPolly = async(createHash, retrieveHash, logger,
|
|||||||
} else if (roleArn) {
|
} else if (roleArn) {
|
||||||
polly = new PollyClient({
|
polly = new PollyClient({
|
||||||
region,
|
region,
|
||||||
credentials: await getAwsAuthToken(logger, createHash, retrieveHash, null, null, region, roleArn),
|
credentials: await getAwsAuthToken(
|
||||||
|
logger, createHash, retrieveHash,
|
||||||
|
{
|
||||||
|
region,
|
||||||
|
roleArn
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// AWS RoleArn assigned to Instance profile
|
// AWS RoleArn assigned to Instance profile
|
||||||
|
|||||||
12
test/aws.js
12
test/aws.js
@@ -19,12 +19,20 @@ test('AWS - create and cache auth token', async(t) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
let obj = await getAwsAuthToken(process.env.AWS_ACCESS_KEY_ID, process.env.AWS_SECRET_ACCESS_KEY, process.env.AWS_REGION);
|
let obj = await getAwsAuthToken({
|
||||||
|
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
|
||||||
|
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
|
||||||
|
region: process.env.AWS_REGION
|
||||||
|
});
|
||||||
//console.log({obj}, 'received auth token from AWS');
|
//console.log({obj}, 'received auth token from AWS');
|
||||||
t.ok(obj.securityToken && !obj.servedFromCache, 'successfullY generated auth token from AWS');
|
t.ok(obj.securityToken && !obj.servedFromCache, 'successfullY generated auth token from AWS');
|
||||||
|
|
||||||
await sleep(250);
|
await sleep(250);
|
||||||
obj = await getAwsAuthToken(process.env.AWS_ACCESS_KEY_ID, process.env.AWS_SECRET_ACCESS_KEY, process.env.AWS_REGION);
|
obj = await getAwsAuthToken({
|
||||||
|
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
|
||||||
|
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
|
||||||
|
region: process.env.AWS_REGION
|
||||||
|
});
|
||||||
//console.log({obj}, 'received auth token from AWS - second request');
|
//console.log({obj}, 'received auth token from AWS - second request');
|
||||||
t.ok(obj.securityToken && obj.servedFromCache, 'successfully received access token from cache');
|
t.ok(obj.securityToken && obj.servedFromCache, 'successfully received access token from cache');
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user