@cumulus/deployment
Version:
Deployment templates for cumulus
102 lines (86 loc) • 2.94 kB
JavaScript
;
/**
* Generates private/public keys and Upload them to a given bucket
*
* @param {string} bucket - the bucket to upload the keys to
* @param {string} key - the key (folder) to use for the uploaded files
* @param {Object} s3 - an instance of the AWS S3 class
* @returns {Promise} resolves `undefined` when upload is complete
*/
let uploadKeyPair = (() => {
var _ref = _asyncToGenerator(function* (bucket, key, s3) {
const pki = forge.pki;
const keyPair = generateKeyPair();
console.log('Keys Generated');
// upload the private key
const privateKey = pki.privateKeyToPem(keyPair.privateKey);
const params1 = {
Bucket: bucket,
Key: `${key}/private.pem`,
ACL: 'private',
Body: privateKey
};
// upload the public key
const publicKey = pki.publicKeyToPem(keyPair.publicKey);
const params2 = {
Bucket: bucket,
Key: `${key}/public.pub`,
ACL: 'private',
Body: publicKey
};
yield s3.putObject(params1).promise();
yield s3.putObject(params2).promise();
console.log('keys uploaded to S3');
});
return function uploadKeyPair(_x, _x2, _x3) {
return _ref.apply(this, arguments);
};
})();
/**
* Checks if the private/public key exists. If not, it
* generates and uploads them
*
* @param {string} stack - name of the stack
* @param {string} bucket - the bucket to upload the keys to
* @param {Object} s3 - an instance of AWS S3 class
* @returns {Promise} resolves `undefined` when complete
*/
let crypto = (() => {
var _ref2 = _asyncToGenerator(function* (stack, bucket, s3) {
const key = `${stack}/crypto`;
// check if files are generated
try {
yield s3.headObject({
Key: `${key}/public.pub`,
Bucket: bucket
}).promise();
yield s3.headObject({
Key: `${key}/private.pem`,
Bucket: bucket
}).promise();
} catch (e) {
yield uploadKeyPair(bucket, key, s3);
}
});
return function crypto(_x4, _x5, _x6) {
return _ref2.apply(this, arguments);
};
})();
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"); }); }; }
const forge = require('node-forge');
/**
* Generates public/private key pairs
*
* @function generateKeyPair
* @returns {Object} a forge pki object
*/
function generateKeyPair() {
const rsa = forge.pki.rsa;
console.log('Generating keys. It might take a few seconds!');
return rsa.generateKeyPair({ bits: 2048, e: 0x10001 });
}
module.exports = {
generateKeyPair,
uploadKeyPair,
crypto
};