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

implement credential_process on ProcessCredentials provider #2559

Merged
merged 13 commits into from
Mar 26, 2019
5 changes: 5 additions & 0 deletions .changes/next-release/feature-Credentials-1a22eb00.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "feature",
"category": "Credentials",
"description": "enables use of credentials_process for sourcing credentials from an external process https://docs.aws.amazon.com/cli/latest/topic/config-vars.html#sourcing-credentials-from-external-processes"
}
59 changes: 56 additions & 3 deletions lib/credentials/shared_ini_file_credentials.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var AWS = require('../core');
var STS = require('../../clients/sts');
var proc = require('child_process');
var iniLoader = AWS.util.iniLoader;

/**
Expand Down Expand Up @@ -158,9 +159,26 @@ AWS.SharedIniFileCredentials = AWS.util.inherit(AWS.Credentials, {
return;
}

this.accessKeyId = profile['aws_access_key_id'];
this.secretAccessKey = profile['aws_secret_access_key'];
this.sessionToken = profile['aws_session_token'];
if (profile['aws_access_key_id'] || profile['aws_secret_access_key']) {
this.accessKeyId = profile['aws_access_key_id'];
this.secretAccessKey = profile['aws_secret_access_key'];
this.sessionToken = profile['aws_session_token'];
} else if (profile['credential_process']) {
this.loadViaCredentialProcess(profile, function(err, data) {
if (err) {
callback(err);
} else {
self.expired = false;
self.accessKeyId = data.AccessKeyId;
self.secretAccessKey = data.SecretAccessKey;
self.sessionToken = data.SessionToken;
if (data.Expiration) {
self.expireTime = new Date(data.Expiration);
}
callback(null);
}
});
}

if (!this.accessKeyId || !this.secretAccessKey) {
throw AWS.util.error(
Expand All @@ -175,6 +193,41 @@ AWS.SharedIniFileCredentials = AWS.util.inherit(AWS.Credentials, {
}
},

/**
* Executes the credential_process and retrieves
* credentials from the output
* @api private
* @param profile [map] credentials profile
* @throws SharedIniFileCredentialsProviderFailure
*/
loadViaCredentialProcess: function loadViaCredentialProcess(profile, callback) {
proc.exec(profile['credential_process'], function(err,stdOut, stdErr) {
srchase marked this conversation as resolved.
Show resolved Hide resolved
if (err) {
callback(err, null);
} else if (stdErr) {
callback(stdErr, null);
srchase marked this conversation as resolved.
Show resolved Hide resolved
} else {
var credData = JSON.parse(stdOut);
if (credData.Expiration) {
var currentTime = AWS.util.date.getDate();
var expireTime = new Date(credData.Expiration);
if (expireTime < currentTime) {
err = AWS.util.error(
new Error('credential_process returned expired credentials'),
{ code: 'SharedIniFileCredentialsProviderFailure' }
);
}
} else if (parseInt(credData.Version) !== 1) {
srchase marked this conversation as resolved.
Show resolved Hide resolved
err = AWS.util.error(
new Error('credential_process does not return Version == 1'),
{ code: 'SharedIniFileCredentialsProviderFailure' }
);
}
callback(err, credData);
srchase marked this conversation as resolved.
Show resolved Hide resolved
}
});
},

/**
* Loads the credentials from the shared credentials file
*
Expand Down
136 changes: 135 additions & 1 deletion test/credentials.spec.js

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