From 5dff0446baf1ca8656b6ff625182fa14a4ee8614 Mon Sep 17 00:00:00 2001 From: Lars Jacobsson Date: Sun, 16 Aug 2020 23:03:50 +0200 Subject: [PATCH] workaround for credentials chain issues --- index.js | 25 +++++++++++++++++++++---- package.json | 2 +- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 6fc79bc..2c637a0 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,6 @@ const os = require("os"); const fs = require("fs"); const path = require("path"); const sha1 = require("sha1"); -const storage = require("node-persist"); var iniLoader = AWS.util.iniLoader; @@ -16,7 +15,24 @@ AWS.SingleSignOnCredentials = AWS.util.inherit(AWS.Credentials, { this.filename = options.filename; this.profile = options.profile || process.env.AWS_PROFILE || AWS.util.defaultProfile; - this.get(options.callback || AWS.util.fn.noop); + }, + + init: function(options) { + const filepath = + process.env.AWS_CONFIG_FILE || path.join(os.homedir(), ".aws", "config"); + var profiles = AWS.util.getProfilesFromSharedConfig(iniLoader, filepath); + var profile = profiles[this.profile] || {}; + + if (Object.keys(profile).length === 0) { + throw AWS.util.error( + new Error("Profile " + this.profile + " not found"), + { code: "ProcessCredentialsProviderFailure" } + ); + } + if (profile.sso_start_url) { + AWS.config.update({ credentials: new AWS.SingleSignOnCredentials() }); + this.get((options || {}).callback || AWS.util.fn.noop); + } }, /** @@ -60,13 +76,14 @@ AWS.SingleSignOnCredentials = AWS.util.inherit(AWS.Credentials, { roleName: profile.sso_role_name, }; if (!request) { - console.log(`Cached credentials not found under ${cachePath}. Please make sure you log in with 'aws sso login' first`); + console.log( + `Cached credentials not found under ${cachePath}. Please make sure you log in with 'aws sso login' first` + ); } sso.getRoleCredentials(request, (err, c) => { if (!c) { console.log(err.message); console.log("Please log in using 'aws sso login'"); - } self.expired = false; AWS.util.update(self, { diff --git a/package.json b/package.json index 3ae374b..2050e56 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@mhlabs/aws-sdk-sso", - "version": "0.0.4", + "version": "0.0.5", "description": "Credentials provider for AWS Single Sign-On", "main": "index.js", "scripts": {