From 6ae683028bda0d17584f0aa654dcdd486f21f779 Mon Sep 17 00:00:00 2001 From: Lars Jacobsson Date: Thu, 23 Jul 2020 20:27:38 +0200 Subject: [PATCH] Enable AWS profiles --- index.js | 73 +++++++---------------------------------------- input-util.js | 4 +-- package-lock.json | 17 ++++++++--- package.json | 4 +-- 4 files changed, 28 insertions(+), 70 deletions(-) diff --git a/index.js b/index.js index a61ccd4..e8ba186 100755 --- a/index.js +++ b/index.js @@ -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`; @@ -13,7 +14,7 @@ program .command("pattern") .alias("p") .option("-f, --format ", "Select output format", "json") - .option("-p, --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" @@ -29,7 +30,7 @@ program .command("input") .alias("i") .option("-f, --format ", "Select output format", "json") - .option("-p, --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" @@ -44,7 +45,7 @@ program program .command("browse") .alias("b") - .option("-p, --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); @@ -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 ", "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 ", "Account ID") - .option("-u, --start-url ", "AWS SSO start URL") - .option("--region ", "AWS region") - .option("--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), - }); - } } diff --git a/input-util.js b/input-util.js index d0c596e..fa5cca1 100644 --- a/input-util.js +++ b/input-util.js @@ -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; } diff --git a/package-lock.json b/package-lock.json index 7859b6e..9ef051d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,9 +1,17 @@ { "name": "@mhlabs/evb-cli", - "version": "1.0.12", + "version": "1.0.13", "lockfileVersion": 1, "requires": true, "dependencies": { + "@aws-sdk/shared-ini-file-loader": { + "version": "0.1.0-preview.1", + "resolved": "https://registry.npmjs.org/@aws-sdk/shared-ini-file-loader/-/shared-ini-file-loader-0.1.0-preview.1.tgz", + "integrity": "sha512-iPglaZZt2uhRpBO0oXww+oK9mtJQvh91o1NuuIOGMBceSou9qzMtGC5NzJzGnooodfwzARx1rgv9myJpcK38Sw==", + "requires": { + "tslib": "^1.8.0" + } + }, "@babel/code-frame": { "version": "7.8.3", "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.8.3.tgz", @@ -457,10 +465,11 @@ } }, "@mhlabs/aws-sso-client-auth": { - "version": "1.0.9", - "resolved": "https://registry.npmjs.org/@mhlabs/aws-sso-client-auth/-/aws-sso-client-auth-1.0.9.tgz", - "integrity": "sha512-NL2OI5ewhTQJCXtA9lmoobu9bEGTJihZHmYBU4sP7J6lAkN1XT+3nXpwLekeusIX1FJRz8FhN7wijNpVkPRI3w==", + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/@mhlabs/aws-sso-client-auth/-/aws-sso-client-auth-1.1.3.tgz", + "integrity": "sha512-JgW2kBB8REGrfASbMR+f4RCBMatAhbRE9jqoCisc+U91Q9ut+VRVJJIrL1MgI81X3sdJ4CabtUHBNukDY7dWyQ==", "requires": { + "@aws-sdk/shared-ini-file-loader": "0.1.0-preview.1", "aws-sdk": "^2.639.0", "node-persist": "^3.0.5", "opn": "^6.0.0" diff --git a/package.json b/package.json index a0c29df..c4c59e8 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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",