mirror of
https://github.com/jambonz/speech-utils.git
synced 2026-01-25 02:08:26 +00:00
fix aws arnrole
This commit is contained in:
@@ -5,27 +5,27 @@ const EXPIRY = 3600;
|
||||
|
||||
async function getAwsAuthToken(
|
||||
logger, createHash, retrieveHash,
|
||||
awsAccessKeyId, awsSecretAccessKey, awsRegion, roleArn = null) {
|
||||
{accessKeyId, secretAccessKey, region, RoleArn}) {
|
||||
logger = logger || noopLogger;
|
||||
try {
|
||||
const key = makeAwsKey(roleArn || awsAccessKeyId);
|
||||
const key = makeAwsKey(roleArn || accessKeyId);
|
||||
const obj = await retrieveHash(key);
|
||||
if (obj) return {...obj, servedFromCache: true};
|
||||
|
||||
let data;
|
||||
if (roleArn) {
|
||||
const stsClient = new STSClient({ region: awsRegion});
|
||||
const roleToAssume = { RoleArn: roleArn, RoleSessionName: 'Jambonz_Speech', DurationSeconds: EXPIRY};
|
||||
if (RoleArn) {
|
||||
const stsClient = new STSClient({ region });
|
||||
const roleToAssume = { RoleArn, RoleSessionName: 'Jambonz_Speech', DurationSeconds: EXPIRY};
|
||||
const command = new AssumeRoleCommand(roleToAssume);
|
||||
|
||||
data = await stsClient.send(command);
|
||||
} else {
|
||||
/* access token not found in cache, so generate it using STS */
|
||||
const stsClient = new STSClient({
|
||||
region: awsRegion,
|
||||
region,
|
||||
credentials: {
|
||||
accessKeyId: awsAccessKeyId,
|
||||
secretAccessKey: awsSecretAccessKey,
|
||||
accessKeyId,
|
||||
secretAccessKey,
|
||||
}
|
||||
});
|
||||
const command = new GetSessionTokenCommand({DurationSeconds: EXPIRY});
|
||||
|
||||
@@ -107,7 +107,13 @@ const getAwsVoices = async(_client, createHash, retrieveHash, logger, credential
|
||||
} else if (roleArn) {
|
||||
client = new PollyClient({
|
||||
region,
|
||||
credentials: await getAwsAuthToken(logger, createHash, retrieveHash, null, null, region, roleArn),
|
||||
credentials: await getAwsAuthToken(
|
||||
logger, createHash, retrieveHash, {
|
||||
accessKeyId: null,
|
||||
secretAccessKey: null,
|
||||
region,
|
||||
RoleArn: roleArn
|
||||
}),
|
||||
});
|
||||
} else {
|
||||
client = new PollyClient({region});
|
||||
|
||||
@@ -281,7 +281,14 @@ const synthPolly = async(createHash, retrieveHash, logger,
|
||||
} else if (roleArn) {
|
||||
polly = new PollyClient({
|
||||
region,
|
||||
credentials: await getAwsAuthToken(logger, createHash, retrieveHash, null, null, region, roleArn),
|
||||
credentials: await getAwsAuthToken(
|
||||
logger, createHash, retrieveHash,
|
||||
{
|
||||
accessKeyId: null,
|
||||
secretAccessKey: null,
|
||||
region,
|
||||
RoleArn: roleArn
|
||||
}),
|
||||
});
|
||||
} else {
|
||||
// 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;
|
||||
}
|
||||
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');
|
||||
t.ok(obj.securityToken && !obj.servedFromCache, 'successfullY generated auth token from AWS');
|
||||
|
||||
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');
|
||||
t.ok(obj.securityToken && obj.servedFromCache, 'successfully received access token from cache');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user