Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix missing tokenCodeFn #181

Merged
merged 2 commits into from
Mar 20, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 39 additions & 13 deletions bin/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,34 @@ const minimist = require('minimist'),
default: { 'source': process.cwd() }
});
},
tokenCodeFn = function (code) {
'use strict';
return function (mfaSerial, callback) {

if (code) {
return callback(null, String(code));
}
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});

readline.question(`Please enter the code for MFA device ${mfaSerial}:`, (value) => {
readline.close();
return callback(null, String(value));
});

};
},
main = function () {
'use strict';
const args = readArgs(),
commands = readCommands(),
command = args._ && args._.length && args._[0],
logger = (!args.quiet) && new ConsoleLogger(),
RoleArn = process.env.AWS_ROLE_ARN || args['sts-role-arn'];
RoleArn = process.env.AWS_ROLE_ARN || args['sts-role-arn'],
SerialNumber = process.env.AWS_MFA_SERIAL || args['mfa-serial'],
TemporaryCredentialsParams = { params: {RoleArn}};
if (args.version && !command) {
console.log(require(path.join(__dirname, '..', 'package.json')).version);
return;
Expand Down Expand Up @@ -51,23 +72,28 @@ const minimist = require('minimist'),
AWS.config.httpOptions = AWS.config.httpOptions || {};
AWS.config.httpOptions.timeout = args['aws-client-timeout'];
}
if (RoleArn) {
const params = { RoleArn };
console.log(`Assuming Role ${RoleArn}`);
if (args['mfa-serial'] && args['mfa-token']) {
Object.assign(params, {
SerialNumber: args['mfa-serial'],
TokenCode: args['mfa-token'],
DurationSeconds: args['mfa-duration'] || 3600
});
}
AWS.config.credentials = new AWS.ChainableTemporaryCredentials({ params }, AWS.config.credentials);
}

if (args.proxy) {
AWS.config.httpOptions = AWS.config.httpOptions || {};
AWS.config.httpOptions.agent = new HttpsProxyAgent(args.proxy);
}

if (SerialNumber) {
Object.assign(TemporaryCredentialsParams.params, {
SerialNumber,
DurationSeconds: process.env.AWS_MFA_DURATION || args['mfa-duration'] || 3600
});
Object.assign(TemporaryCredentialsParams, {tokenCodeFn: tokenCodeFn(args['mfa-token'])});
}

if (RoleArn) {
console.log(`Assuming Role ${RoleArn}`);
Object.assign(TemporaryCredentialsParams.params, {RoleArn});
}

if (SerialNumber || RoleArn) {
AWS.config.credentials = new AWS.ChainableTemporaryCredentials(TemporaryCredentialsParams);
}
commands[command](args, logger).then(result => {
if (result && !args.quiet) {
console.log(JSON.stringify(result, null, 2));
Expand Down