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

Shell script hooks #145

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 50 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"mkdirp": "^1.0.4",
"proxy-agent": "^3.1.1",
"puppeteer": "^5.2.1",
"shelljs": "^0.8.3",
"uuid": "^8.3.0"
},
"devDependencies": {
Expand All @@ -51,6 +52,7 @@
"@types/mkdirp": "^1.0.1",
"@types/node": "^14.0.27",
"@types/puppeteer": "^3.0.1",
"@types/shelljs": "^0.8.8",
"@types/uuid": "^8.0.0",
"@typescript-eslint/eslint-plugin": "^3.8.0",
"@typescript-eslint/parser": "^3.8.0",
Expand Down
72 changes: 50 additions & 22 deletions src/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ import { paths } from "./paths";
import mkdirp from "mkdirp";

const debug = _debug("aws-azure-login");
import {
getUsernameSh,
getPasswordSh,
getVerificationCodeSh,
} from "./shellScrpts";

const WIDTH = 425;
const HEIGHT = 550;
Expand Down Expand Up @@ -68,14 +73,21 @@ const states = [
debug("Not prompting user for username");
username = defaultUsername;
} else {
debug("Prompting user for username");
({ username } = await inquirer.prompt([
{
name: "username",
message: "Username:",
default: defaultUsername,
} as Question,
]));
const usernameSh = getUsernameSh();
if (usernameSh) {
debug("Username retrieved");
username = usernameSh;
console.log(`Entering username (${username})`);
} else {
debug("Prompting user for username");
({ username } = await inquirer.prompt([
{
name: "username",
message: "Username:",
default: defaultUsername,
} as Question,
]));
}
}

debug("Waiting for username input to be visible");
Expand Down Expand Up @@ -211,14 +223,21 @@ const states = [
debug("Not prompting user for password");
password = defaultPassword;
} else {
debug("Prompting user for password");
({ password } = await inquirer.prompt([
{
name: "password",
message: "Password:",
type: "password",
} as Question,
]));
const passwordSh = getPasswordSh();
if (passwordSh) {
debug("Password retrieved");
password = passwordSh;
console.log("Entering password");
} else {
debug("Prompting user for password");
({ password } = await inquirer.prompt([
{
name: "password",
message: "Password:",
type: "password",
} as Question,
]));
}
}

debug("Focusing on password input");
Expand Down Expand Up @@ -297,12 +316,21 @@ const states = [
console.log(descriptionMessage);
}

const { verificationCode } = await inquirer.prompt([
{
name: "verificationCode",
message: "Verification Code:",
} as Question,
]);
let verificationCode;

const verificationCodeSh = getVerificationCodeSh();
if (verificationCodeSh) {
debug("Verification code retrieved");
verificationCode = verificationCodeSh;
console.log(`Entering verification code (${verificationCode})`);
} else {
({ verificationCode } = await inquirer.prompt([
{
name: "verificationCode",
message: "Verification Code:",
} as Question,
]));
}

debug("Focusing on verification code input");
await page.focus(`input[name="otc"]`);
Expand Down
33 changes: 33 additions & 0 deletions src/shellScrpts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import shell from "shelljs";
import path from "path";
import { paths } from "./paths";

import _debug from "debug";
const debug = _debug("aws-azure-login");

const usernameScriptPath: string =
process.env.AWS_LOGIN_SCRIPT_USERNAME ??
path.join(paths.awsDir, ".aws-azure-login.username.sh");
const passwordScriptPath: string =
process.env.AWS_LOGIN_SCRIPT_PASSWORD ??
path.join(paths.awsDir, ".aws-azure-login.password.sh");
const staticChallengeScriptPath: string =
process.env.AWS_LOGIN_SCRIPT_MFA ??
path.join(paths.awsDir, ".aws-azure-login.static-challenge.sh");

const trim = (str: string): string => (str ? str.trim() : str);
const execSh = (path: string): string | undefined => {
if (shell.test("-e", path)) {
debug(`Executing ${path}`);
return trim(shell.exec(path, { silent: true }).stdout);
} else {
debug(`Script ${path} does not exist`);
}
};

export const getUsernameSh = (): string | undefined =>
execSh(usernameScriptPath);
export const getPasswordSh = (): string | undefined =>
execSh(passwordScriptPath);
export const getVerificationCodeSh = (): string | undefined =>
execSh(staticChallengeScriptPath);