#!/usr/bin/env node /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ // I sign keys. That's what I do. const path = require('path'), express = require('express'), statsd = require('../lib/statsd'); const config = require('../lib/configuration.js'), httputils = require('../lib/httputils.js'), validate = require('../lib/validate.js'), logger = require('../lib/logging.js').logger, heartbeat = require('../lib/heartbeat'), shutdown = require('../lib/shutdown'), computecluster = require('compute-cluster'), urlparse = require('urlparse'); const HOSTNAME = urlparse(config.get('public_url')).host; logger.info("Certs will be issued from: " + HOSTNAME); // create an express server var app = express.createServer(); // respond to health checks (before logging) heartbeat.setup(app); // our server will log app.use(express.logger({ format: config.get('express_log_format'), stream: { write: function(x) { logger.info(typeof x === 'string' ? x.trim() : x); } } })); var statsd_config = config.get('statsd'); if (statsd_config && statsd_config.enabled) { var logger_statsd = require("connect-logger-statsd"); app.use(logger_statsd({ host: statsd_config.hostname || "localhost", port: statsd_config.port || 8125, prefix: statsd_config.prefix || "browserid.keysigner." })); } // parse POST bodies app.use(express.bodyParser()); try { // explicitly relay VAR_PATH to children process.env['VAR_PATH'] = config.get('var_path'); // allocate a compute cluster var cc = new computecluster({ module: path.join(__dirname, "..", "lib", "keysigner", "keysigner-compute.js"), max_processes: config.get('max_compute_processes') }).on('error', function(e) { logger.error("error detected in keysigning computation process! fatal: " + e.toString()); setTimeout(function() { process.exit(1); }, 0); }).on('info', function(msg) { logger.info("(compute cluster): " + msg); }).on('debug', function(msg) { logger.debug("(compute cluster): " + msg); }); } catch(e) { process.stderr.write("can't allocate compute cluster: " + e + "\n"); process.exit(1); } // and our single function app.post('/wsapi/cert_key', validate({ 'email': 'email', 'pubkey': 'pubkey', 'ephemeral': 'boolean' }), function(req, resp) { var startTime = new Date(); cc.enqueue({ pubkey: req.params.pubkey, email: req.params.email, validityPeriod: (req.params.ephemeral ? config.get('ephemeral_session_duration_ms') : config.get('certificate_validity_ms')), hostname: HOSTNAME }, function (err, r) { var reqTime = new Date - startTime; statsd.timing('certification_time', reqTime); // consider "application" errors to be the same as harder errors if (!err && r && r.error) err = r.error; else if (!r || !r.success) err = "no certificate returned from child process"; if (err) { logger.error("certification generation error: " + err); httputils.serverError(resp, "certification generation error"); } else { resp.writeHead(200, {'Content-Type': 'text/plain'}); resp.write(r.success); resp.end(); } }); }); // shutdown nicely on signals shutdown.handleTerminationSignals(app, function() { cc.exit(); }); var bindTo = config.get('bind_to'); app.listen(bindTo.port, bindTo.host, function() { logger.info("running on http://" + app.address().address + ":" + app.address().port); });