UNPKG

2.94 kBJavaScriptView Raw
1'use strict';
2
3/**
4 * Generates private/public keys and Upload them to a given bucket
5 *
6 * @param {string} bucket - the bucket to upload the keys to
7 * @param {string} key - the key (folder) to use for the uploaded files
8 * @param {Object} s3 - an instance of the AWS S3 class
9 * @returns {Promise} resolves `undefined` when upload is complete
10 */
11let uploadKeyPair = (() => {
12 var _ref = _asyncToGenerator(function* (bucket, key, s3) {
13 const pki = forge.pki;
14 const keyPair = generateKeyPair();
15 console.log('Keys Generated');
16
17 // upload the private key
18 const privateKey = pki.privateKeyToPem(keyPair.privateKey);
19 const params1 = {
20 Bucket: bucket,
21 Key: `${key}/private.pem`,
22 ACL: 'private',
23 Body: privateKey
24 };
25
26 // upload the public key
27 const publicKey = pki.publicKeyToPem(keyPair.publicKey);
28 const params2 = {
29 Bucket: bucket,
30 Key: `${key}/public.pub`,
31 ACL: 'private',
32 Body: publicKey
33 };
34
35 yield s3.putObject(params1).promise();
36 yield s3.putObject(params2).promise();
37
38 console.log('keys uploaded to S3');
39 });
40
41 return function uploadKeyPair(_x, _x2, _x3) {
42 return _ref.apply(this, arguments);
43 };
44})();
45
46/**
47 * Checks if the private/public key exists. If not, it
48 * generates and uploads them
49 *
50 * @param {string} stack - name of the stack
51 * @param {string} bucket - the bucket to upload the keys to
52 * @param {Object} s3 - an instance of AWS S3 class
53 * @returns {Promise} resolves `undefined` when complete
54 */
55
56
57let crypto = (() => {
58 var _ref2 = _asyncToGenerator(function* (stack, bucket, s3) {
59 const key = `${stack}/crypto`;
60
61 // check if files are generated
62 try {
63 yield s3.headObject({
64 Key: `${key}/public.pub`,
65 Bucket: bucket
66 }).promise();
67
68 yield s3.headObject({
69 Key: `${key}/private.pem`,
70 Bucket: bucket
71 }).promise();
72 } catch (e) {
73 yield uploadKeyPair(bucket, key, s3);
74 }
75 });
76
77 return function crypto(_x4, _x5, _x6) {
78 return _ref2.apply(this, arguments);
79 };
80})();
81
82function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
83
84const forge = require('node-forge');
85
86/**
87 * Generates public/private key pairs
88 *
89 * @function generateKeyPair
90 * @returns {Object} a forge pki object
91 */
92function generateKeyPair() {
93 const rsa = forge.pki.rsa;
94 console.log('Generating keys. It might take a few seconds!');
95 return rsa.generateKeyPair({ bits: 2048, e: 0x10001 });
96}
97
98module.exports = {
99 generateKeyPair,
100 uploadKeyPair,
101 crypto
102};
\No newline at end of file