Skip to content

Commit

Permalink
fix aws arnrole
Browse files Browse the repository at this point in the history
  • Loading branch information
xquanluu committed Jun 14, 2024
1 parent 2ec56f5 commit c4feac9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
16 changes: 8 additions & 8 deletions lib/get-aws-sts-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Check failure on line 11 in lib/get-aws-sts-token.js

View workflow job for this annotation

GitHub Actions / build

'roleArn' is not defined
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});
Expand Down
8 changes: 7 additions & 1 deletion lib/get-tts-voices.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Check failure on line 112 in lib/get-tts-voices.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 12 spaces but found 10
secretAccessKey: null,

Check failure on line 113 in lib/get-tts-voices.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 12 spaces but found 10
region,

Check failure on line 114 in lib/get-tts-voices.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 12 spaces but found 10
RoleArn: roleArn

Check failure on line 115 in lib/get-tts-voices.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 12 spaces but found 10
}),

Check failure on line 116 in lib/get-tts-voices.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 10 spaces but found 8
});
} else {
client = new PollyClient({region});
Expand Down
9 changes: 8 additions & 1 deletion lib/synth-audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Check failure on line 285 in lib/synth-audio.js

View workflow job for this annotation

GitHub Actions / build

Trailing spaces not allowed
{
accessKeyId: null,
secretAccessKey: null,
region,
RoleArn: roleArn
}),

Check failure on line 291 in lib/synth-audio.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 10 spaces but found 8
});
} else {
// AWS RoleArn assigned to Instance profile
Expand Down
12 changes: 10 additions & 2 deletions test/aws.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down

0 comments on commit c4feac9

Please sign in to comment.