From 28b04e700dce9ccb144666d8ec8595d6539242ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerardo=20P=C3=A9rez=20P=C3=A9rez?= Date: Fri, 22 Dec 2017 01:45:59 -0600 Subject: [PATCH] feat: add support to create a development certificate --- src/commands/dev-cert.js | 43 ++++++++++++++++++++++++++++++++ src/commands/serve.js | 53 ++++++++++++++++++++-------------------- src/deamon.js | 23 ++++------------- templates/openssl.cnf | 23 +++++++++++++++++ 4 files changed, 97 insertions(+), 45 deletions(-) create mode 100644 src/commands/dev-cert.js create mode 100644 templates/openssl.cnf diff --git a/src/commands/dev-cert.js b/src/commands/dev-cert.js new file mode 100644 index 0000000..3e5da50 --- /dev/null +++ b/src/commands/dev-cert.js @@ -0,0 +1,43 @@ +import 'colors'; +import Command from 'cmd-line/lib/Command'; +import render from '../utils/render'; +import shell from '../utils/shell'; + +export default (new Command( + __filename, + 'Generates a local certificate to use https in your local machine')) + // .Args('common_name') + .Options([ + ['--c', '--c MX', '2 word codes to describe the country (Default: MX)', 'MX'], + ['--st', '--st ', '2 word codes to describe the estate (Default: GN)'], + ['--l', '--l ', '3 word code to describe the locality (Default: IRA)'], + ['--o', '--o', 'Organization (Default: koaton)'], + ['--ou', '--ou', 'Organization Unit (Default: DVL)'], + ['--cn', '--cn', 'Common Name (Default: [your www localhost])'], + ['--email', '--email ', "Database name for the connection default is ''. Use this with -g"] + ]) + .Action(async function (options) { + let c = options.c || 'MX'; + let st = options.st || 'GN'; + let l = options.l || 'IRA'; + let o = options.o || 'Koaton'; + let ou = options.ou || 'DVL'; + let root_website = options.cn || `www.${configuration.server.host}`; + let email = options.email; + let alt_names_dns = configuration.server.subdomains.map(sub => { + return `DNS:${sub}.${configuration.server.host}`; + }).join(',') + render(TemplatePath('openssl.cnf'), ProyPath('openssl.cnf'), { + root_website, + alt_names_dns, + email, + c, + st, + l, + o, + ou + }); + await shell('Generating key', ['openssl', 'genrsa', '-out', `${configuration.server.host}.key`, 2048]); + await shell('Generating certificate', ['openssl', 'req', '-new', '-sha256', '-x509', '-key', `${configuration.server.host}.key`, '-config', 'openssl.cnf', '-out', `${configuration.server.host}.crt`]); + return 0; + }); diff --git a/src/commands/serve.js b/src/commands/serve.js index b6f127e..ebc8305 100644 --- a/src/commands/serve.js +++ b/src/commands/serve.js @@ -135,33 +135,32 @@ export default (new Command(__filename, 'Runs your awsome Koaton applicaction es ReloadTemplates(); await WactchAndCompressImages(); } - await Events('pre', 'build'); - await Events('pre', 'ember_build'); - let buildingAppsEmber = Object.entries(embercfg).map(([app, cfg]) => (new EmberBuilder(app, 'development', cfg))); - screen.line1(true); - const building = spinner(); - building.start(100, `Building ${buildingAppsEmber.map(e => e.name).join(', ').green}`, undefined, process.stdout.columns); - let EmberPids = await Promise.all(buildingAppsEmber.map(e => e.serve(nginxbuilt, options.noEmberLive))).then((reports) => { - building.end(' Ember apps:'); - for (const report of reports) { - report.log = true; - console.log(` ${report.result}`); - } - screen.line1(); - buildingAppsEmber.map(e => e.createWatcher((file) => { - livereload.reload(file); - })); - return reports.map((r) => r.pid); - }); - await Events('post', 'ember_build'); - return Events('pre', 'serve').then(() => { - return new Promise(function (resolve, reject) { - let server = deamon(resolve, reject, EmberPids, nginxbuilt); - /* istanbul ignore next */ - process.once('SIGINT', function () { - server.kill(); - resolve(0); - }); + return new Promise(async function (resolve, reject) { + await Events('pre', 'serve'); + let server = deamon(resolve, reject, nginxbuilt); + await Events('pre', 'ember_build'); + let buildingAppsEmber = Object.entries(embercfg).map(([app, cfg]) => (new EmberBuilder(app, 'development', cfg))); + screen.line1(true); + const building = spinner(); + building.start(100, `Building ${buildingAppsEmber.map(e => e.name).join(', ').green}`, undefined, process.stdout.columns); + let EmberPids = await Promise.all(buildingAppsEmber.map(e => e.serve(nginxbuilt, options.noEmberLive))).then((reports) => { + building.end(' Ember apps:'); + for (const report of reports) { + report.log = true; + console.log(` ${report.result}`); + } + screen.line1(); + buildingAppsEmber.map(e => e.createWatcher((file) => { + livereload.reload(file); + })); + return reports.map((r) => r.pid); + }); + await Events('post', 'ember_build'); + await Events('post', 'serve'); + /* istanbul ignore next */ + process.once('SIGINT', function () { + server.kill(); + resolve(0); }); }); }); diff --git a/src/deamon.js b/src/deamon.js index 09fde0a..67a7efc 100644 --- a/src/deamon.js +++ b/src/deamon.js @@ -4,21 +4,8 @@ import { livereload, liveReloadHost } from './utils/livereload'; import * as spawn from 'cross-spawn'; import * as psTree from 'ps-tree'; -function LoadServer (resolve, reject, EmberPids, nginx) { +function LoadServer (resolve, reject, nginx) { let PIDPromises = []; - if (process.env.istesting) { - for (const pid of EmberPids) { - PIDPromises.push(new Promise((resolve) => { - psTree(pid, (err, children) => { - if (err) { - reject(err); - } - let childIPIDs = children.map((p) => parseInt(p.PID, 10)); - resolve(childIPIDs); - }); - })); - } - } let KoatonServer = spawn('node', ['app.js'], { shell: false, env: {liveReloadHost: liveReloadHost(), NODE_ENV: process.env.NODE_ENV} }); KoatonServer.stdout.on('data', (buffer) => { process.stdout.write(buffer); @@ -48,14 +35,13 @@ function LoadServer (resolve, reject, EmberPids, nginx) { KoatonServer.stderr.on('data', (data) => { console.log(`stderr: ${data}`); }); - KoatonServer.on('close', (code) => { console.log(`child process exited with code ${code}`); }); return KoatonServer; } -export default function StartKoatonServer (resolve, reject, EmberPids, nginx = false) { +async function StartKoatonServer (resolve, reject, nginx = false) { let watcher = new Watch('./**', { awaitWriteFinish: { stabilityThreshold: 500, @@ -80,11 +66,12 @@ export default function StartKoatonServer (resolve, reject, EmberPids, nginx = f '*.conf' ] }); - let server = LoadServer(resolve, reject, EmberPids, nginx); + let server = LoadServer(resolve, reject, nginx); watcher.on('all', (event, path) => { console.log(123, event, path); server.kill(); - server = LoadServer(resolve, reject, EmberPids, nginx); + server = LoadServer(resolve, reject, nginx); }); return server; } +export default StartKoatonServer; diff --git a/templates/openssl.cnf b/templates/openssl.cnf new file mode 100644 index 0000000..b1b32f3 --- /dev/null +++ b/templates/openssl.cnf @@ -0,0 +1,23 @@ +[req] +serial = 1 +default_bits = 2048 +distinguished_name = req_distinguished_name +x509_extensions = v3_ca +prompt = no + +[req_distinguished_name] +C = {{c}} +ST = {{st}} +L = {{l}} +O = {{o}} +OU = {{ou}} +CN = {{root_website}} +emailAddress = {{email}} + +[v3_ca] +subjectKeyIdentifier = hash +authorityKeyIdentifier = keyid:always,issuer:always +basicConstraints = critical,CA:TRUE +keyUsage = digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment,keyAgreement,keyCertSign +subjectAltName = {{alt_names_dns}},email:{{email}} +issuerAltName = issuer:copy