forked from roylines/node-credstash
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
70 lines (60 loc) · 1.63 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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');
const keys = require('./lib/keys');
const secrets = require('./lib/secrets');
const xtend = require('xtend');
const defaults = {
limit: 1,
region: process.env.AWS_DEFAULT_REGION || 'eu-west-1',
table: 'credential-store'
};
function Credstash(config) {
this.config = xtend(defaults, config);
}
Credstash.prototype.list = function(options, done) {
if (typeof options === 'function') {
done = options;
options = this.config;
} else {
options = xtend(this.config, options);
}
const AWS = getAWS(options);
return async.waterfall([
async.apply(secrets.list, AWS, options),
async.apply(keys.decrypt, AWS),
async.apply(hmac.check),
async.apply(decrypter.decryptedObject)
], function (err, secrets) {
if (err) {
return done(err);
}
done(null, secrets);
});
};
Credstash.prototype.get = function(name, options, done) {
if (typeof options === 'function') {
done = options;
options = this.config;
} else {
options = xtend(this.config, options);
}
const AWS = getAWS(options);
return async.waterfall([
async.apply(secrets.get, AWS, name, options),
async.apply(keys.decrypt, AWS),
async.apply(hmac.check),
async.apply(decrypter.decryptedList)
], function (err, secrets) {
if (err) {
return done(err);
}
if (options.limit === 1) {
return done(null, secrets && secrets[0]);
}
done(null, secrets);
});
};
module.exports = Credstash;