Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
Adds debugging support, adds custom user-agent
Browse files Browse the repository at this point in the history
  • Loading branch information
brianantonelli committed Mar 27, 2017
1 parent 34a45a2 commit 28db4dd
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 30 deletions.
142 changes: 113 additions & 29 deletions lib/alks-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ var exports = module.exports = {};
var ALKS_DURATIONS = [ 2, 6, 12, 18 ], // reducing due to EB not honoring long sessions: , 24, 36 ],
ACCOUNT_SELECTION_DELIMITER = ' :: ',
AWS_SIGNIN_URL = 'https://signin.aws.amazon.com/federation',
AWS_CONSOLE_URL = 'https://console.aws.amazon.com/';
AWS_CONSOLE_URL = 'https://console.aws.amazon.com/',
SANITIZE_FIELDS = [ 'password' ],
DEFAULT_UA = 'alks-node';

exports.getDurations = function(){
return ALKS_DURATIONS;
Expand All @@ -30,18 +32,44 @@ var getMessageFromBadResponse = function(results){
return 'Bad response received, please check API URL.';
};

exports.createKey = function(account, password, duration, callback){
var log = function(section, msg, options){
if(options.debug){
console.error([ '[', section, ']: ', msg ].join(''));
}
}

var sanitizeData = function(data){
var cleansed = {};
_.each(data, function(val, field){
cleansed[field] = _.contains(SANITIZE_FIELDS, field) ? '********' : val;
});

return cleansed;
}

exports.createKey = function(account, password, duration, opts, callback){
var payload = _.extend({
password: password,
sessionTime: duration,
account: account.alksAccount,
role: account.alksRole
}, account);
}, account),
options = _.extend({
debug: false,
ua: DEFAULT_UA
}, opts),
endpoint = account.server + '/getKeys/';

log('api:createKey', 'creating key at endpoint: ' + endpoint, options);
log('api:createKey', 'with data: ' + JSON.stringify(sanitizeData(payload), null, 4), options);

request({
url: account.server + '/getKeys/',
url: endpoint,
method: 'POST',
json: payload
json: payload,
headers: {
'User-Agent': options.ua
}
}, function(err, results){
if(err){
return callback(err);
Expand All @@ -62,18 +90,29 @@ exports.createKey = function(account, password, duration, callback){
});
};

exports.createIamKey = function(account, password, callback){
exports.createIamKey = function(account, password, opts, callback){
var payload = _.extend({
password: password,
sessionTime: 1,
account: account.alksAccount,
role: account.alksRole
}, account);
}, account),
options = _.extend({
debug: false,
ua: DEFAULT_UA
}, opts),
endpoint = account.server + '/getIAMKeys/';

log('api:createIamKey', 'creating IAM key at endpoint: ' + endpoint, options);
log('api:createIamKey', 'with data: ' + JSON.stringify(sanitizeData(payload), null, 4), options);

request({
url: account.server + '/getIAMKeys/',
url: endpoint,
method: 'POST',
json: payload
json: payload,
headers: {
'User-Agent': options.ua
}
}, function(err, results){
if(err){
return callback(err);
Expand All @@ -94,20 +133,31 @@ exports.createIamKey = function(account, password, callback){
});
};

exports.createIamRole = function(account, password, roleName, roleType, includeDefaultPolicies, callback){
exports.createIamRole = function(account, password, roleName, roleType, includeDefaultPolicies, opts, callback){
var payload = _.extend({
password: password,
account: account.alksAccount,
role: account.alksRole,
roleName: roleName,
roleType: roleType,
includeDefaultPolicy: includeDefaultPolicies ? '1' : '0'
}, account);
}, account),
options = _.extend({
debug: false,
ua: DEFAULT_UA
}, opts),
endpoint = account.server + '/createRole/';

log('api:createIamRole', 'creating IAM role at endpoint: ' + endpoint, options);
log('api:createIamRole', 'with data: ' + JSON.stringify(sanitizeData(payload), null, 4), options);

request({
url: account.server + '/createRole/',
url: endpoint,
method: 'POST',
json: payload
json: payload,
headers: {
'User-Agent': options.ua
}
}, function(err, results){
if(err){
return callback(err);
Expand All @@ -125,15 +175,25 @@ exports.createIamRole = function(account, password, roleName, roleType, includeD
});
};

exports.getAccounts = function(server, userid, password, options, callback){
var opts = _.extend({
filters: {}
}, options);
exports.getAccounts = function(server, userid, password, opts, callback){
var payload = { userid: userid, password: password },
options = _.extend({
filters: {},
debug: false,
ua: DEFAULT_UA
}, opts),
endpoint = server + '/getAccounts/';

log('api:getAccounts', 'getting accounts at endpoint: ' + endpoint, options);
log('api:getAccounts', 'with data: ' + JSON.stringify(sanitizeData(payload), null, 4), options);

request({
url: server + '/getAccounts/',
url: endpoint,
method: 'POST',
json: { userid: userid, password: password }
json: payload,
headers: {
'User-Agent': options.ua
}
}, function(err, results){
if(err){
return callback(err);
Expand All @@ -148,8 +208,8 @@ exports.getAccounts = function(server, userid, password, options, callback){
if(results.body.accountListRole){
var accountRolesFitlered = {};
_.each(results.body.accountListRole, function(role, acct){
if((opts.filters.iamOnly && !role[0].iamKeyActive)
|| (!opts.filters.iamOnly && role[0].iamKeyActive)){
if((options.filters.iamOnly && !role[0].iamKeyActive)
|| (!options.filters.iamOnly && role[0].iamKeyActive)){
return;
}
accountRolesFitlered[acct] = role;
Expand All @@ -173,11 +233,24 @@ exports.getAccounts = function(server, userid, password, options, callback){
});
};

exports.getIamRoleTypes = function(server, userid, password, callback){
exports.getIamRoleTypes = function(server, userid, password, opts, callback){
var payload = { userid: userid, password: password },
options = _.extend({
debug: false,
ua: DEFAULT_UA
}, opts),
endpoint = server + '/getAWSRoleTypes/';

log('api:getIamRoleTypes', 'getting role types at endpoint: ' + endpoint, options);
log('api:getIamRoleTypes', 'with data: ' + JSON.stringify(sanitizeData(payload), null, 4), options);

request({
url: server + '/getAWSRoleTypes/',
url: endpoint,
method: 'POST',
json: { userid: userid, password: password }
json: payload,
headers: {
'User-Agent': options.ua
}
}, function(err, results){
if(err){
return callback(err);
Expand All @@ -190,18 +263,29 @@ exports.getIamRoleTypes = function(server, userid, password, callback){
});
};

exports.generateConsoleUrl = function(key, callback){
exports.generateConsoleUrl = function(key, opts, callback){
var payload = {
sessionId: key.accessKey,
sessionKey: key.secretKey,
sessionToken: key.sessionToken
};
},
options = _.extend({
debug: false,
ua: DEFAULT_UA
}, opts);

var urlParms = '?Action=getSigninToken&SessionType=json&Session=' + encodeURIComponent(JSON.stringify(payload));
var urlParms = '?Action=getSigninToken&SessionType=json&Session=' + encodeURIComponent(JSON.stringify(payload)),
endpoint = AWS_SIGNIN_URL + urlParms;

log('api:generateConsoleUrl', 'generating console url at endpoint: ' + endpoint, options);
log('api:generateConsoleUrl', 'with data: ' + JSON.stringify(sanitizeData(payload), null, 4), options);
log('ua', options.ua, options)
request({
url: AWS_SIGNIN_URL + urlParms,
method: 'GET'
url: endpoint,
method: 'GET',
headers: {
'User-Agent': options.ua
}
}, function(err, results){
if(err){
return callback(err);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "alks-node",
"version": "0.2.1",
"version": "0.3.1",
"description": "Node client for ALKS",
"main": "lib/alks-api.js",
"scripts": {
Expand Down

0 comments on commit 28db4dd

Please sign in to comment.