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

Refactor MFA entry to be used with different token functions #237

Merged
merged 2 commits into from
May 11, 2020
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions src/CLIMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from "./services/processEventLogs";
import { getLogEvents } from "./services/awsCloudwatchLogs";
import checkForUpdates from "./utils/updateNotifier";
import { getAWSCredentials } from "./services";
import { getAWSCredentialsHQ } from "./services";

import {
eventRegistryModal,
Expand Down Expand Up @@ -188,7 +188,7 @@ class Main {
}

init() {
const creds = getAWSCredentials(this.program.profile);
const creds = getAWSCredentialsHQ(this.program.profile, this.screen);

return creds
.getPromise()
Expand Down
24 changes: 15 additions & 9 deletions src/services/awsCredentials.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@ import AWS from "aws-sdk";

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

function getMfaToken(serial, callback) {
promptMfaModal(callback, this.screen);
}

function getAWSCredentials(profile) {
function getAWSCredentials(
profile,
tokenFn = () => console.error("In-tool MFA not supported for Guardian")
) {
if (profile) {
process.env.AWS_SDK_LOAD_CONFIG = 1;
return new AWS.SharedIniFileCredentials({
profile,
tokenCodeFn: getMfaToken,
tokenCodeFn: tokenFn,
callback: (err) => {
if (err) {
console.error(`SharedIniFileCreds Error: ${err}`);
process.exit(0);
}
},
});
Expand All @@ -30,18 +28,26 @@ function getAWSCredentials(profile) {
if (process.env.AWS_PROFILE) {
return new AWS.SharedIniFileCredentials({
profile: process.env.AWS_PROFILE,
tokenCodeFn: getMfaToken,
tokenCodeFn: tokenFn,
callback: (err) => {
if (err) {
console.error(`SharedIniFileCreds Error: ${err}`);
process.exit(0);
}
},
});
}
return new AWS.SharedIniFileCredentials({ profile: "default" });
}

function generateTokenFn(screen) {
return (serial, callback) => promptMfaModal(callback, screen);
}

function getAWSCredentialsHQ(profile, screen) {
getAWSCredentials(profile, generateTokenFn(screen));
}

module.exports = {
getAWSCredentials,
getAWSCredentialsHQ,
};
2 changes: 1 addition & 1 deletion src/services/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { getAWSCredentials } from "./awsCredentials";
export { getAWSCredentials, getAWSCredentialsHQ } from "./awsCredentials";
export { getStackResources } from "./stackResources";
export { getLambdaFunctions } from "./awsLambda";