Skip to content

Commit

Permalink
Merge pull request #4 from Cox-Automotive/update-check
Browse files Browse the repository at this point in the history
Key list is now grouped by ALKS account.
  • Loading branch information
brianantonelli authored Aug 13, 2016
2 parents 5418c8d + dfaca0c commit a6cd948
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 21 deletions.
5 changes: 4 additions & 1 deletion bin/alks-developer
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
process.title = 'ALKS';

var program = require('commander'),
_ = require('underscore'),
utils = require('../lib/utils'),
config = require('../package.json');

Expand All @@ -14,4 +15,6 @@ program
.command('info', 'shows current developer configuration')
.command('login', 'stores password')
.command('logout', 'removes password')
.parse(process.argv);
.parse(process.argv);

utils.subcommandSuggestion(program, 'developer');
2 changes: 1 addition & 1 deletion bin/alks-developer-info
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var program = require('commander'),
clc = require('cli-color'),
_ = require('underscore'),
async = require('async'),
Table = require('cli-table'),
Table = require('cli-table2'),
config = require('../package.json'),
Account = require('../lib/account'),
utils = require('../lib/utils');
Expand Down
4 changes: 3 additions & 1 deletion bin/alks-keys
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ program
.version(config.version)
.command('create', 'creates a new session')
.command('list', 'lists previous keys')
.parse(process.argv);
.parse(process.argv);

utils.subcommandSuggestion(program, 'keys');
6 changes: 4 additions & 2 deletions bin/alks-keys-create
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@ async.waterfall([
alks.createKey(account, password, duration, callback);
},
// now deal with the session data
function(sessionData, password, callback){
function(sessionData, account, password, callback){
// calculate expiration
var expires = moment().add(duration, 'hours');
console.error(clc.white('Your AWS session data has been generated and expires ' + expires.calendar()));

// store session data in DB
keys.addKey(sessionData.accessKey, sessionData.secretKey, sessionData.sessionToken, expires.toDate(), password);
keys.addKey(sessionData.accessKey, sessionData.secretKey,
sessionData.sessionToken, account.alksAccount,
expires.toDate(), password);

callback(null, sessionData);
}
Expand Down
38 changes: 30 additions & 8 deletions bin/alks-keys-list
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ process.title = 'ALKS';
var program = require('commander'),
clc = require('cli-color'),
_ = require('underscore'),
Table = require('cli-table'),
Table = require('cli-table2'),
moment = require('moment'),
prompt = require('prompt'),
async = require('async'),
Expand Down Expand Up @@ -51,21 +51,36 @@ async.waterfall([
clc.white.bold('Expires'),
clc.white.bold('Created')
],
colWidths: [5, 25, 45, 25, 25]
colWidths: [5, 25, 25, 25, 25]
});

_.each(foundKeys, function(keydata, i){
table.push([ i+1, utils.obfuscate(keydata.accessKey), utils.obfuscate(keydata.secretKey), moment(keydata.expires).calendar(), moment(keydata.meta.created).fromNow() ])

var groupedKeys = _.groupBy(foundKeys, 'alksAccount'),
i = 0;

_.each(groupedKeys, function(keys, alksAccount){
table.push([ { colSpan: 5, content: clc.yellow.bold('ALKS Account: ' + alksAccount) } ]);

_.each(keys, function(keydata){
table.push([ (i++)+1,
utils.obfuscate(keydata.accessKey),
utils.obfuscate(keydata.secretKey),
moment(keydata.expires).calendar(),
moment(keydata.meta.created).fromNow()
]);
});
});



console.error(clc.white.underline.bold('\nAvailable Keys'));
console.error(clc.white(table.toString()));

if(foundKeys.length){
prompt.message = prompt.delimiter = '';
prompt.start();

var promptKey = clc.yellow('Use (1-' + foundKeys.length + ')?');
var promptKey = clc.yellow('Use (1-' + i + ')?');
prompt.get([promptKey], function(err, result){
var index;
try{
Expand All @@ -76,16 +91,23 @@ async.waterfall([
return utils.errorAndExit('Invalid value received.');
}

callback(null, foundKeys, index);
i = 0;
_.each(groupedKeys, function(keys, alksAccount){
_.each(keys, function(keydata){
if(i++ === index){
callback(null, keydata);
}
});
});
});
}
}
], function(err, foundKeys, selectedIndex){
], function(err, selectedKey){
if(err){
return utils.errorAndExit(err);
}

console.log(keys.getKeyOutput(program.output, foundKeys[selectedIndex], program.namedProfile, program.force));
console.log(keys.getKeyOutput(program.output, selectedKey, program.namedProfile, program.force));

utils.checkForUpdate();
});
2 changes: 1 addition & 1 deletion lib/alks-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ exports.createKey = function(account, password, duration, callback){
accessKey: results.body.accessKey,
secretKey: results.body.secretKey,
sessionToken: results.body.sessionToken
}, password);
}, account, password);
});
};

Expand Down
12 changes: 8 additions & 4 deletions lib/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ function encrypt(text, key){
}

function decrypt(text, key){
if(_.isEmpty(text)) return '';

var decipher = crypto.createDecipher(ALGORITHM, key.toString('binary')),
decryptd = decipher.update(text, ENCODING, CHARSET);

Expand Down Expand Up @@ -71,12 +73,13 @@ function updateCreds(key, profile, force){
utils.addNewLineToEOF(credFile);
}

exports.addKey = function(accessKey, secretKey, sessionToken, expires, password, callback){
exports.addKey = function(accessKey, secretKey, sessionToken, alksAccount, expires, password, callback){
getKeysCollection(function(err, keys){
keys.insert({
accessKey: encrypt(accessKey, password),
secretKey: encrypt(secretKey, password),
sessionToken: encrypt(sessionToken, password),
alksAccount: encrypt(alksAccount, password),
expires: expires.getTime()
});

Expand All @@ -91,7 +94,7 @@ exports.getKeys = function(password, callback){
var now = new Date().getTime();

// first delete any expired keys
keys.removeWhere({ expires : { '$lte': now } })
keys.removeWhere({ expires : { '$lte': now } });
db.save();

// now get valid keys, decrypt their values and return
Expand All @@ -102,9 +105,10 @@ exports.getKeys = function(password, callback){
.data();

_.each(data, function(keydata, i){
keydata.accessKey = decrypt(keydata.accessKey, password);
keydata.secretKey = decrypt(keydata.secretKey, password);
keydata.accessKey = decrypt(keydata.accessKey, password);
keydata.secretKey = decrypt(keydata.secretKey, password);
keydata.sessionToken = decrypt(keydata.sessionToken, password);
keydata.alksAccount = decrypt(keydata.alksAccount, password);
});

callback(null, data);
Expand Down
25 changes: 24 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

var _ = require('underscore'),
_s = require('underscore.string'),
fuzzy = require('fuzzy'),
clc = require('cli-color'),
Table = require('cli-table'),
Table = require('cli-table2'),
path = require('path'),
os = require('os'),
fs = require('fs'),
Expand Down Expand Up @@ -57,6 +58,8 @@ exports.trim = function(str){
};

exports.obfuscate = function(str){
if(_.isEmpty(str)) return '';

var s1 = Math.floor(str.length * 0.3),
obfuscated = [str.substring(0, s1)];

Expand Down Expand Up @@ -103,4 +106,24 @@ exports.checkForUpdate = function(){
}
}
});
};

exports.subcommandSuggestion = function(program, subcommand){
var commands = _.map(program.commands, '_name'),
requestedCommand = _.head(program.args);

if(program.args.length && !_.includes(commands, requestedCommand)){
var prefix = ['alks', subcommand, ''].join(' '),
msg = [prefix, requestedCommand, ' is not a valid ALKS command.'],
suggests = fuzzy.filter(requestedCommand, commands),
suggest = suggests.map(function(sug){ return sug.string; });

if(suggest.length){
msg.push(clc.white(' Did you mean '));
msg.push(clc.white.underline(prefix + suggest[0]));
msg.push(clc.white('?'));
}

errorAndExit(msg.join(''));
}
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "alks",
"version": "1.0.2",
"version": "1.1.0",
"description": "CLI for working with ALKS",
"main": "bin/alks",
"scripts": {
Expand Down Expand Up @@ -38,7 +38,7 @@
"dependencies": {
"async": "^2.0.0-rc.6",
"cli-color": "^1.1.0",
"cli-table": "^0.3.1",
"cli-table2": "^0.2.0",
"commander": "^2.9.0",
"crypto": "0.0.3",
"fuzzy": "^0.1.1",
Expand Down

0 comments on commit a6cd948

Please sign in to comment.