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

Update deps, allow aws-sdk configuration for multi-region support #3

Merged
merged 2 commits into from
May 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const async = require('async');
const getAWS = require('./lib/aws').getAWS;
const decrypter = require('./lib/decrypter');
const encoder = require('./lib/encoder');
const hmac = require('./lib/hmac');
Expand All @@ -7,24 +8,27 @@ const secrets = require('./lib/secrets');
const xtend = require('xtend');

const defaults = {
limit: 1
limit: 1,
region: process.env.AWS_DEFAULT_REGION || 'eu-west-1',
table: 'credential-store'
};

function Credstash(config) {
this.table = config ? config.table : undefined;
this.config = xtend(defaults, config);
}

Credstash.prototype.list = function(options, done) {
if (typeof options === 'function') {
done = options;
options = defaults;
options = this.config;
} else {
options = xtend(defaults, options);
options = xtend(this.config, options);
}

const AWS = getAWS(options);
return async.waterfall([
async.apply(secrets.list, this.table, options),
async.apply(keys.decrypt),
async.apply(secrets.list, AWS, options),
async.apply(keys.decrypt, AWS),
async.apply(hmac.check),
async.apply(decrypter.decryptedObject)
], function (err, secrets) {
Expand All @@ -39,14 +43,15 @@ Credstash.prototype.list = function(options, done) {
Credstash.prototype.get = function(name, options, done) {
if (typeof options === 'function') {
done = options;
options = defaults;
options = this.config;
} else {
options = xtend(defaults, options);
options = xtend(this.config, options);
}

const AWS = getAWS(options);
return async.waterfall([
async.apply(secrets.get, this.table, name, options),
async.apply(keys.decrypt),
async.apply(secrets.get, AWS, name, options),
async.apply(keys.decrypt, AWS),
async.apply(hmac.check),
async.apply(decrypter.decryptedList)
], function (err, secrets) {
Expand Down
10 changes: 10 additions & 0 deletions lib/aws.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const AWS = require('aws-sdk');

function getAWS(options) {
AWS.config.update({region: options.region});
return AWS;
}

module.exports = {
getAWS,
};
21 changes: 9 additions & 12 deletions lib/keys.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
const AWS = require('aws-sdk');
const async = require('async');
const encoder = require('./encoder');

if (typeof process.env.AWS_DEFAULT_REGION !== 'undefined') {
AWS.config.update({region: process.env.AWS_DEFAULT_REGION});
}

function decrypt(key, done) {
var params = {
CiphertextBlob: encoder.decode(key)
};
function decrypt(AWS) {
return function(key, done) {
var params = {
CiphertextBlob: encoder.decode(key)
};

return new AWS.KMS().decrypt(params, done);
return new AWS.KMS().decrypt(params, done);
}
}

function split(stashes, decryptedKeys, done) {
Expand All @@ -26,9 +23,9 @@ function split(stashes, decryptedKeys, done) {
}

module.exports = {
decrypt: (stashes, done) => {
decrypt: (AWS, stashes, done) => {
return async.waterfall([
async.apply(async.map, stashes.map(s => s.key), decrypt),
async.apply(async.map, stashes.map(s => s.key), decrypt(AWS)),
async.apply(split, stashes)
], done);
}
Expand Down
21 changes: 8 additions & 13 deletions lib/secrets.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
const AWS = require('aws-sdk');
const async = require('async');

if (typeof process.env.AWS_DEFAULT_REGION !== 'undefined') {
AWS.config.update({region: process.env.AWS_DEFAULT_REGION});
}

// Blatantly borrowed from https://www.electrictoolbox.com/pad-number-zeroes-javascript/
function pad(number, length) {
var str = '' + number;
Expand All @@ -23,9 +18,9 @@ function makeVersion(version) {
};
}

function scan(table, options, done) {
function scan(AWS, options, done) {
var params = {
TableName: table || 'credential-store',
TableName: options.table || 'credential-store',
ConsistentRead: true,
ScanFilter: {}
};
Expand All @@ -37,9 +32,9 @@ function scan(table, options, done) {
return new AWS.DynamoDB().scan(params, done);
}

function find(table, name, options, done) {
function find(AWS, name, options, done) {
var params = {
TableName: table || 'credential-store',
TableName: options.table || 'credential-store',
ConsistentRead: true,
Limit: options.limit,
ScanIndexForward: false,
Expand Down Expand Up @@ -76,15 +71,15 @@ function map(name, data, done) {
}

module.exports = {
get: (table, name, options, done) => {
get: (AWS, name, options, done) => {
return async.waterfall([
async.apply(find, table, name, options),
async.apply(find, AWS, name, options),
async.apply(map, name),
], done);
},
list: (table, options, done) => {
list: (AWS, options, done) => {
return async.waterfall([
async.apply(scan, table, options),
async.apply(scan, AWS, options),
async.apply(map, 'all secrets'),
], done);
}
Expand Down
Loading