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 async #44

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
51 changes: 51 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
defaults: &defaults
shell: /bin/bash --login
docker:
- image: circleci/node:8.12.0-stretch

version: 2
jobs:
test:
<<: *defaults
steps:
- checkout
- run: npm ci
- run: npm test
- run: npm run coverage
- persist_to_workspace:
root: ~/project
paths:
- .
publish:
<<: *defaults
steps:
- attach_workspace:
at: ~/project
- run: sudo apt-get update
- run: sudo apt-get install -y python-pip
- run: sudo pip install awscli credstash
- run: echo "//registry.npmjs.org/:_authToken=$(credstash -r eu-west-1 get npm-auth-token-circleci)" > ~/.npmrc
- run:
command: |
set -e
PACKAGE_VERSION=$(jq -r '[.name, .version] | join("@")' package.json)
if test -z "$(npm info $PACKAGE_VERSION 2> /dev/null)"
then
echo "Publishing $PACKAGE_VERSION"
npm publish
else
echo "Not publishing $PACKAGE_VERSION, already exists."
fi

workflows:
version: 2
all:
jobs:
- test
- publish:
context: phdv-aws
requires:
- test
filters:
branches:
only: master
17 changes: 0 additions & 17 deletions circle.yml

This file was deleted.

19 changes: 19 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export = Credstash;

declare class Credstash {
list(options: Credstash.IOptions, callback: Credstash.Callback<string[]>): void;
list(callback: Credstash.Callback<string[]>): void;

get(value: string, options: Credstash.IOptions, callback: Credstash.Callback<string>): void;
get(value: string, callback: Credstash.Callback<string>): void;
}

declare namespace Credstash {
type Callback<T> = (err: Error | null, response: T) => void;

interface IOptions {
limit?: number,
region?: string,
table?: string
}
}
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
12 changes: 12 additions & 0 deletions lib/aws.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const AWS = require('aws-sdk');

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

module.exports = {
getAWS,
};
2 changes: 1 addition & 1 deletion lib/decrypter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function decryptedString(stash) {
const value = encoder.decode(stash.contents);
const aesCtr = new aesjs.ModeOfOperation.ctr(key, new aesjs.Counter(1));
const decryptedBytes = aesCtr.decrypt(value);
return decryptedBytes.toString();
return aesjs.utils.utf8.fromBytes(decryptedBytes);
}

module.exports = {
Expand Down
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