Skip to content

Commit

Permalink
Enable AWS profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
ljacobsson committed Jul 23, 2020
1 parent 2e129b7 commit 6ae6830
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 70 deletions.
73 changes: 11 additions & 62 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ const patternBuilder = require("./pattern-builder");
const AWS = require("aws-sdk");
const program = require("commander");
const ssoAuth = require("@mhlabs/aws-sso-client-auth");
const storage = require("node-persist");
const inputUtil = require("./input-util");
const os = require("os");
const iniFileLoader = require("@aws-sdk/shared-ini-file-loader");

const EVB_CACHE_DIR = `${os.homedir()}/.evb-cli`;

Expand All @@ -13,7 +14,7 @@ program
.command("pattern")
.alias("p")
.option("-f, --format <json|yaml>", "Select output format", "json")
.option("-p, --profile <profile>", "AWS profile to use")
.option("-p, --profile [profile]", "AWS profile to use")
.option(
"--sso",
"Authenticate with AWS SSO. Set environment variable EVB_CLI_SSO=1 for default behaviour"
Expand All @@ -29,7 +30,7 @@ program
.command("input")
.alias("i")
.option("-f, --format <json|yaml>", "Select output format", "json")
.option("-p, --profile <profile>", "AWS profile to use")
.option("-p, --profile [profile]", "AWS profile to use")
.option(
"--sso",
"Authenticate with AWS SSO. Set environment variable EVB_CLI_SSO=1 for default behaviour"
Expand All @@ -44,7 +45,7 @@ program
program
.command("browse")
.alias("b")
.option("-p, --profile <profile>", "AWS profile to use")
.option("-p, --profile [profile]", "AWS profile to use")
.description("Browses sources and detail types and shows their consumers")
.action(async (cmd) => {
await authenticate(cmd.profile);
Expand All @@ -53,70 +54,18 @@ program
await patternBuilder.browseEvents(cmd.format, schemaApi, evbApi);
});

// program
// .command("publish")
// // .alias("p")
// .description("Browses the schema registry and lets you publish messages built from schemas")
// .option("-p, --profile <profile>", "AWS profile to use", "default")
// .action(async cmd => {
// await authenticate();
// const schemaApi = new AWS.Schemas();
// const evbApi = new AWS.EventBridge();
// await patternBuilder.publishEvents(cmd.format, schemaApi, evbApi);
// });

program
.on("command:*", () => {
const command = program.args[0];

console.error(`Unknown command '${command}'`);
process.exit(1);
})
.command("configure-sso")
.option("-a, --account-id <accountId>", "Account ID")
.option("-u, --start-url <startUrl>", "AWS SSO start URL")
.option("--region <region>", "AWS region")
.option("--role <role>", "Role to get credentials for")
.description("Configure authentication with AWS Single Sign-On")
.action(async (cmd) => {
await storage.init({
dir: EVB_CACHE_DIR,
expiredInterval: 0,
});

await storage.setItem("evb-cli-sso", {
accountId: cmd.accountId,
startUrl: cmd.startUrl,
region: cmd.region,
role: cmd.role,
});
});

program.parse(process.argv);

if (process.argv.length < 3) {
program.help();
}
async function authenticate(profile) {

if (profile) {
var credentials = new AWS.SharedIniFileCredentials({ profile: profile });
AWS.config.update({ credentials: credentials });
if (profile === true) {
const configFile = await iniFileLoader.loadSharedConfigFiles();
profile = await inputUtil.selectFrom(Object.keys(configFile.configFile), "Select profile", true);
}

await storage.init({
dir: EVB_CACHE_DIR,
const config = await ssoAuth.requestAuth("evb-cli", profile);
AWS.config.update({
config
});
const ssoConfig = await storage.getItem("evb-cli-sso");
if (ssoConfig) {
await ssoAuth.configure({
clientName: "evb-cli",
startUrl: ssoConfig.startUrl,
accountId: ssoConfig.accountId,
region: ssoConfig.region,
});
AWS.config.update({
credentials: await ssoAuth.authenticate(ssoConfig.role),
});
}
}
4 changes: 2 additions & 2 deletions input-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ async function getRegistry(schemas) {
return registry;
}

async function selectFrom(list, message) {
async function selectFrom(list, message, skipBack) {
const answer = await prompt({
name: "id",
type: "list",
message: message || "Please select",
choices: [BACK, ...list]
choices: [!skipBack ? BACK : null, ...list].filter(p => p)
});
return answer.id;
}
Expand Down
17 changes: 13 additions & 4 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mhlabs/evb-cli",
"version": "1.0.13",
"version": "1.0.14",
"description": "A package for building EventBridge/CloudWatch Events patterns",
"main": "index.js",
"scripts": {
Expand All @@ -13,7 +13,7 @@
"author": "mhdev",
"license": "ISC",
"dependencies": {
"@mhlabs/aws-sso-client-auth": "^1.0.9",
"@mhlabs/aws-sso-client-auth": "^1.1.3",
"aws-sdk": "^2.639.0",
"commander": "^4.1.1",
"inquirer": "^7.0.4",
Expand Down

0 comments on commit 6ae6830

Please sign in to comment.