forked from DavidTanner/nodecredstash
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·320 lines (287 loc) · 9.09 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
"use strict";
const debug = require("debug")("credstash");
const DynamoDB = require("./lib/dynamoDb");
const KMS = require("./lib/kms");
const encrypter = require("./lib/encrypter");
const decrypter = require("./lib/decrypter");
const defaults = require("./lib/defaults");
const utils = require("./lib/utils");
module.exports = function (mainConfig) {
const config = Object.assign({}, mainConfig);
const table = config.table || defaults.DEFAULT_TABLE;
const ddbOpts = Object.assign({}, config.awsOpts, config.dynamoOpts);
const ddb = new DynamoDB(table, ddbOpts);
const kmsKey = config.kmsKey || defaults.DEFAULT_KMS_KEY;
const kmsOpts = Object.assign({}, config.awsOpts, config.kmsOpts);
const kms = new KMS(kmsKey, kmsOpts);
class Credstash {
constructor() {
const credstash = this;
Object.getOwnPropertyNames(Credstash.prototype).forEach((key) => {
const method = credstash[key];
credstash[key] = function () {
const args = Array.from(arguments);
const lastArg = args.slice(-1)[0];
let cb;
if (typeof lastArg === "function") {
cb = args.pop();
}
return method
.apply(credstash, args)
.then((res) => {
if (cb) {
return cb(undefined, res);
}
return res;
})
.catch((err) => {
if (cb) {
return cb(err);
}
throw err;
});
};
});
this.paddedInt = utils.paddedInt;
this.getConfiguration = () => {
const ddbOptsCopy = Object.assign({}, ddbOpts);
const kmsOptsCopy = Object.assign({}, kmsOpts);
const configCopy = Object.assign({}, config);
const configuration = {
config: configCopy,
dynamoConfig: {
table,
opts: ddbOptsCopy,
},
kmsConfig: {
kmsKey,
opts: kmsOptsCopy,
},
};
return configuration;
};
}
/**
* Retrieve the highest version of `name` in the table
*
*/
getHighestVersion(opts) {
const options = Object.assign({}, opts);
const { name } = options;
if (!name) {
return Promise.reject(new Error("name is a required parameter"));
}
return ddb
.getLatestVersion(name)
.then((res) => res.Items[0])
.then((res) => {
const { version = 0 } = res || {};
return version;
});
}
incrementVersion(opts) {
return this.getHighestVersion(opts)
.then((version) => {
if (Number.parseInt(version, 10) == version) {
return Number.parseInt(version, 10);
}
throw new Error(
`Can not autoincrement version. The current version: ${version} is not an int`
);
})
.then((version) => utils.paddedInt(defaults.PAD_LEN, version + 1));
}
putSecret(opts) {
const options = Object.assign({}, opts);
const {
name,
secret,
context,
digest = defaults.DEFAULT_DIGEST,
} = options;
if (!name) {
return Promise.reject(new Error("name is a required parameter"));
}
if (!secret) {
return Promise.reject(new Error("secret is a required parameter"));
}
const version = utils.sanitizeVersion(options.version, 1); // optional
return kms
.getEncryptionKey(context)
.catch((err) => {
if (err.code == "NotFoundException") {
throw err;
}
throw new Error(
`Could not generate key using KMS key ${kmsKey}, error:${JSON.stringify(
err,
null,
2
)}`
);
})
.then((kmsData) => encrypter.encrypt(digest, secret, kmsData))
.then((data) => Object.assign({ name, version }, data))
.then((data) => ddb.createSecret(data))
.catch((err) => {
if (err.code == "ConditionalCheckFailedException") {
throw new Error(
`${name} version ${version} is already in the credential store.`
);
} else {
throw err;
}
});
}
decryptStash(stash, context) {
const key = utils.b64decode(stash.key);
return kms.decrypt(key, context).catch((err) => {
let msg = `Decryption error: ${JSON.stringify(err, null, 2)}`;
if (err.code == "InvalidCiphertextException") {
if (context) {
msg =
"Could not decrypt hmac key with KMS. The encryption " +
"context provided may not match the one used when the " +
"credential was stored.";
} else {
msg =
"Could not decrypt hmac key with KMS. The credential may " +
"require that an encryption context be provided to decrypt " +
"it.";
}
}
throw new Error(msg);
});
}
getAllVersions(opts) {
const options = Object.assign({}, opts);
const {
name,
context, // optional
limit, // optional
} = options;
if (!name) {
return Promise.reject(new Error("name is a required parameter"));
}
return ddb
.getAllVersions(name, { limit })
.then((results) => {
const dataKeyPromises = results.Items.map((stash) =>
this.decryptStash(stash, context).then((decryptedDataKey) =>
Object.assign(stash, { decryptedDataKey })
)
);
return Promise.all(dataKeyPromises);
})
.then((stashes) =>
stashes.map((stash) => ({
version: stash.version,
secret: decrypter.decrypt(stash, stash.decryptedDataKey),
}))
);
}
getSecret(opts) {
const options = Object.assign({}, opts);
const { name, context } = options;
if (!name) {
return Promise.reject(new Error("name is a required parameter"));
}
const version = utils.sanitizeVersion(options.version); // optional
const func =
version == undefined
? ddb.getLatestVersion(name).then((res) => res.Items[0])
: ddb.getByVersion(name, version).then((res) => res.Item);
return func
.then((stash) => {
if (!stash || !stash.key) {
throw new Error(`Item {'name': '${name}'} could not be found.`);
}
return Promise.all([stash, this.decryptStash(stash, context)]);
})
.then((res) => decrypter.decrypt(res[0], res[1]));
}
deleteSecrets(opts) {
const options = Object.assign({}, opts);
const { name } = options;
if (!name) {
return Promise.reject(new Error("name is a required parameter"));
}
return ddb
.getAllVersions(name)
.then((res) => res.Items)
.then((secrets) =>
utils.mapPromise(secrets, (secret) =>
this.deleteSecret({
name: secret.name,
version: secret.version,
})
)
);
}
deleteSecret(opts) {
const options = Object.assign({}, opts);
const { name } = options;
if (!name) {
return Promise.reject(new Error("name is a required parameter"));
}
const version = utils.sanitizeVersion(options.version);
if (!version) {
return Promise.reject(new Error("version is a required parameter"));
}
debug(`Deleting ${name} -- version ${version}`);
return ddb.deleteSecret(name, version);
}
listSecrets() {
return ddb
.getAllSecretsAndVersions()
.then((res) => res.Items.sort(utils.sortSecrets));
}
getAllSecrets(opts) {
const options = Object.assign({}, opts);
const { version, context, startsWith } = options;
const unOrdered = {};
return this.listSecrets()
.then((secrets) => {
const position = {};
const filtered = [];
secrets
.filter((secret) => secret.version == (version || secret.version))
.filter(
(secret) => !startsWith || secret.name.startsWith(startsWith)
)
.forEach((next) => {
position[next.name] = position[next.name]
? position[next.name]
: filtered.push(next);
});
return filtered;
})
.then((secrets) =>
utils.mapPromise(secrets, (secret) =>
this.getSecret({
name: secret.name,
version: secret.version,
context,
})
.then((plainText) => {
unOrdered[secret.name] = plainText;
})
.catch(() => undefined)
)
)
.then(() => {
const ordered = {};
Object.keys(unOrdered)
.sort()
.forEach((key) => {
ordered[key] = unOrdered[key];
});
return ordered;
});
}
createDdbTable() {
return ddb.createTable();
}
}
return new Credstash();
};