Skip to content

Commit 0442107

Browse files
committed
Add option to supply mfa token in command line
1 parent 83cd31b commit 0442107

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

src/CLIMain.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,11 @@ class Main {
188188
}
189189

190190
init() {
191-
const creds = getAWSCredentials(this.program.profile, this.screen);
191+
const creds = getAWSCredentials(
192+
this.program.profile,
193+
this.program,
194+
this.screen
195+
);
192196

193197
return creds
194198
.getPromise()

src/guardian/index.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ const failTitleLog = chalk.redBright.underline.bold;
2020

2121
class GuardianCI {
2222
constructor(program) {
23-
AWS.config.credentials = getAWSCredentials(program.profile);
23+
AWS.config.credentials = getAWSCredentials(
24+
program.profile,
25+
program,
26+
null,
27+
true
28+
);
2429
if (program.region) {
2530
AWS.config.region = program.region;
2631
}

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ program
3030
.option("--sls", "use the serverless framework to execute commands")
3131
.option("--sam", "use the SAM framework to execute commands")
3232
.option("-c, --ci", "ci mode for sls-dev-guardian checks")
33+
.option("--mfa <mfa>", "mfa token for profiles with mfa authentication")
3334
.parse(process.argv);
3435

3536
program.location = program.location || process.cwd();

src/services/awsCredentials.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,25 @@ import AWS from "aws-sdk";
22

33
import { promptMfaModal } from "../modals";
44

5-
function getAWSCredentials(profile, screen) {
5+
function getAWSCredentials(profile, program, screen, isGuardian = false) {
6+
let codeFn;
7+
if (program.mfa) {
8+
const token = program.mfa;
9+
codeFn = (serial, callback) => callback(null, token);
10+
} else if (!isGuardian) {
11+
codeFn = (serial, callback) => promptMfaModal(callback, screen);
12+
} else {
13+
codeFn = () =>
14+
console.error(
15+
"In-tool mfa authentication isn't supported for guardian. Please provide your mfa token via the --mfa option"
16+
);
17+
}
18+
619
if (profile) {
720
process.env.AWS_SDK_LOAD_CONFIG = 1;
821
return new AWS.SharedIniFileCredentials({
922
profile,
10-
tokenCodeFn: (serial, callback) => promptMfaModal(callback, screen),
23+
tokenCodeFn: codeFn,
1124
callback: (err) => {
1225
if (err) {
1326
console.error(`SharedIniFileCreds Error: ${err}`);
@@ -25,7 +38,7 @@ function getAWSCredentials(profile, screen) {
2538
if (process.env.AWS_PROFILE) {
2639
return new AWS.SharedIniFileCredentials({
2740
profile: process.env.AWS_PROFILE,
28-
tokenCodeFn: (serial, callback) => promptMfaModal(callback, screen),
41+
tokenCodeFn: codeFn,
2942
callback: (err) => {
3043
if (err) {
3144
console.error(`SharedIniFileCreds Error: ${err}`);

0 commit comments

Comments
 (0)