1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | let 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 |
|
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 |
|
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 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | let crypto = (() => {
|
58 | var _ref2 = _asyncToGenerator(function* (stack, bucket, s3) {
|
59 | const key = `${stack}/crypto`;
|
60 |
|
61 |
|
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 |
|
82 | function _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 |
|
84 | const forge = require('node-forge');
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | function 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 |
|
98 | module.exports = {
|
99 | generateKeyPair,
|
100 | uploadKeyPair,
|
101 | crypto
|
102 | }; |
\ | No newline at end of file |