Skip to content

Commit

Permalink
Fixing various bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ljacobsson committed Aug 21, 2020
1 parent b8ceddc commit d44e149
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ AWS.SingleSignOnCredentials = AWS.util.inherit(AWS.Credentials, {

this.filename = options.filename;
this.profile =
options.profile || process.env.AWS_PROFILE || AWS.util.defaultProfile;
options.profile || process.env.AWS_PROFILE || AWS.util.defaultProfile;
this.get((options || {}).callback || AWS.util.fn.noop);
},

init: function (options) {
Expand All @@ -23,8 +24,7 @@ AWS.SingleSignOnCredentials = AWS.util.inherit(AWS.Credentials, {
process.env.AWS_CONFIG_FILE ||
path.join(os.homedir(), ".aws", "config");
var profiles = AWS.util.getProfilesFromSharedConfig(iniLoader, filepath);
var profile = profiles[this.profile] || {};

var profile = profiles["profile " + this.profile] || {};
if (Object.keys(profile).length === 0) {
throw AWS.util.error(
new Error("Profile " + this.profile + " not found"),
Expand All @@ -50,14 +50,18 @@ AWS.SingleSignOnCredentials = AWS.util.inherit(AWS.Credentials, {
process.env.AWS_CONFIG_FILE ||
path.join(os.homedir(), ".aws", "config");
var profiles = AWS.util.getProfilesFromSharedConfig(iniLoader, filepath);
var profile = profiles[this.profile] || {};
var profile = profiles[this.profile === "default" ? "default" : "profile " + 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) {
callback(new Error("No start url"));
return;
}
AWS.config.update({ region: profile.sso_region });
const sso = new AWS.SSO();

Expand All @@ -75,22 +79,20 @@ AWS.SingleSignOnCredentials = AWS.util.inherit(AWS.Credentials, {
const cachedFile = fs.readFileSync(cachePath);
cacheObj = JSON.parse(cachedFile.toString());
}
if (!cacheObj) {
throw new Error(`Cached credentials not found under ${cachePath}. Please make sure you log in with 'aws sso login' first`);
} else {
const request = {
accessToken: cacheObj.accessToken,
accountId: profile.sso_account_id,
roleName: profile.sso_role_name,
};
if (!request) {
throw AWS.util.error(
new Error(
`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'");
callback(err);
return;
}
self.expired = false;
AWS.util.update(self, {
Expand All @@ -100,9 +102,10 @@ AWS.SingleSignOnCredentials = AWS.util.inherit(AWS.Credentials, {
expireTime: new Date(c.roleCredentials.expiration),
});
this.coalesceRefresh(callback || AWS.util.fn.callback);
// console.log(AWS.config.credentials);
callback(null);
});
} catch (err) {
}} catch (err) {
console.log(err);
callback(err);
}
Expand Down

0 comments on commit d44e149

Please sign in to comment.