-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
118 lines (98 loc) · 2.75 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const async = require('async');
const Credstash = require('./lib/credstash');
const spawnSync = require('child_process').spawnSync;
function pathToEnv(p) {
return p.replace(/[^\w]/g, '_')
.toUpperCase();
}
module.exports.getSync = function(params) {
const args = [__dirname + '/bin/envcredstash', '--json'];
if (Array.isArray(params.prefixes)) {
params.prefixes.forEach(p => {
args.push('--prefix');
args.push(p);
});
} else if (Array.isArray(params.prefix)) {
params.prefix.forEach(p => {
args.push('--prefix');
args.push(p);
});
} else if (typeof params.prefix === 'string') {
args.push('--prefix');
args.push(params.prefix);
}
if (params.table) {
args.push('--table');
args.push(params.table);
}
if (params.region) {
args.push('--region');
args.push(params.region || process.env.AWS_REGION);
}
const proc = spawnSync(process.execPath, args, {
stdio: 'pipe',
encoding: 'utf8'
});
if (proc.error) { throw proc.error; }
if (proc.status !== 0) {
throw new Error(proc.stderr.toString());
}
const stdout = proc.stdout.toString();
return JSON.parse(stdout);
};
function getAsync(params, callback) {
const credstash = Credstash(params);
var prefixes;
if (Array.isArray(params.prefixes)) {
prefixes = params.prefixes;
} else if (Array.isArray(params.prefix)) {
prefixes = params.prefix;
} else if (typeof params.prefix === 'string') {
prefixes = [params.prefix];
} else {
prefixes = [''];
}
credstash.getByPrefixes({ prefixes }, (err, secrets) => {
if (err) {
return callback(err);
}
var envs = [];
prefixes.forEach(prefix => {
secrets
.filter(s => s.name.indexOf(prefix) === 0)
.forEach(s => {
const env = pathToEnv(s.name.replace(prefix, ''));
//Override the variables from other prefixes
envs = envs.filter(e => e.env !== env)
.concat({ name: s.name, version: s.version, env });
});
});
async.each(envs, (env, done) => {
credstash.getSecret({ name: env.name, version: env.version }, (err, value) => {
if (err) { return done(err); }
env.value = value;
done();
});
}, (err) => {
if (err) {
return callback(err);
}
const env = envs.reduce((result, env) => {
result[env.env] = env.value;
return result;
}, {});
callback(null, env);
});
});
};
module.exports.get = function(params, callback) {
if (typeof callback === 'function') {
return getAsync(params, callback);
}
return new Promise((resolve, reject) => {
getAsync(params, (err, result) => {
if (err) { return reject(err); }
resolve(result);
});
});
};