Merge pull request #173 from hybridgroup/295-optional-ssl

Adding optional ssl param in API
This commit is contained in:
Andrew Stewart 2014-04-24 10:38:25 -06:00
commit 31dfd4b6c9
4 changed files with 71 additions and 43 deletions

View File

@ -8,8 +8,7 @@
"use strict";
var fs = require('fs'),
https = require('https');
var fs = require('fs');
var express = require('express'),
namespace = require('node-namespace');
@ -23,23 +22,27 @@ namespace("Cylon", function() {
if (opts == null) { opts = {}; }
this.opts = opts;
this.host = opts.host || "127.0.0.1";
this.port = opts.port || "3000";
this.opts = opts;
this.host = opts.host || "127.0.0.1";
this.port = opts.port || "3000";
this.ssl = opts.ssl;
this.master = opts.master;
var options = {
cert: fs.readFileSync(opts.cert || __dirname + "/ssl/server.crt"),
key: fs.readFileSync(opts.key || __dirname + "/ssl/server.key")
}
this.server = express();
this.server.https = https.createServer(options, this.server);
//configure ssl if requested
if (this.ssl && typeof(this.ssl) === 'object') {
var https = require('https');
this.server.set('title', 'Cylon API Server');
var options = {
key: fs.readFileSync(this.ssl.key || __dirname + "/ssl/server.key"),
cert: fs.readFileSync(this.ssl.cert || __dirname + "/ssl/server.crt")
};
this.server.node = https.createServer(options, this.server);
} else {
Logger.warn("API using insecure connection. We recommend using an SSL certificate with Cylon.")
this.server.node = this.server;
}
// configure basic auth, if requested
if (opts.auth && opts.auth.type && opts.auth.type === 'basic') {
@ -51,6 +54,7 @@ namespace("Cylon", function() {
}
}
this.server.set('title', 'Cylon API Server');
this.server.use(express.json());
this.server.use(express.urlencoded());
this.server.use(express["static"](__dirname + "/../node_modules/robeaux/"));
@ -58,10 +62,12 @@ namespace("Cylon", function() {
ApiServer.prototype.listen = function() {
var self = this;
this.server.https.listen(this.port, this.host, null, function() {
var title = self.server.get('title');
this.server.node.listen(this.port, this.host, null, function() {
var title = self.server.get('title');
var protocol = self.ssl ? "https" : "http";
Logger.info(title + " is now online.");
Logger.info("Listening at https://" + self.host + ":" + self.port);
Logger.info("Listening at " + protocol + "://" + self.host + ":" + self.port);
});
};

View File

@ -46,10 +46,9 @@ var Cylon = (function() {
this.api_config = {
host: '127.0.0.1',
port: '3000',
cert: null,
key: null,
auth: {},
CORS: null
CORS: null,
ssl: {}
};
this.robot = bind(this.robot, this);
@ -101,7 +100,7 @@ var Cylon = (function() {
Master.prototype.api = function(opts) {
if (opts == null) { opts = {}; }
var keys = ['host', 'port', 'auth', 'cert', 'CORS', 'key'];
var keys = ['host', 'port', 'auth', 'CORS', 'ssl'];
for (var i = 0; i < keys.length; i++) {
var key = keys[i];

View File

@ -9,21 +9,20 @@ var API = source('api');
describe("API", function() {
var api, opts;
beforeEach(function() {
stub(https, 'createServer').returns({ listen: spy() });
opts = {
master: { name: 'master' }
}
api = new API(opts);
});
afterEach(function() {
https.createServer.restore();
});
describe("constructor", function() {
beforeEach(function() {
stub(https, 'createServer').returns({ listen: spy() });
opts = { master: { name: 'master' }, ssl: {} }
api = new API(opts);
});
afterEach(function() {
https.createServer.restore();
});
it("sets @opts to the passed opts object", function() {
expect(api.opts).to.be.eql(opts);
});
@ -58,6 +57,28 @@ describe("API", function() {
var title = api.server.get('title');
expect(title).to.be.eql("Cylon API Server");
});
});
describe("ssl disabled", function () {
beforeEach(function() {
stub(https, 'createServer').returns({ listen: spy() });
opts = { ssl: false }
api = new API(opts);
});
afterEach(function() {
https.createServer.restore();
});
it("doesn't create https server", function() {
expect(https.createServer).not.to.be.calledWith();
});
});
describe("#configureRoutes", function() {

View File

@ -42,10 +42,9 @@ describe("Cylon", function() {
expectedConfig = {
host: '127.0.0.1',
port: '3000',
cert: null,
key: null,
auth: {},
CORS: null
CORS: null,
ssl: {}
};
// this is the shortest, cheapest way to dup an object in JS.
@ -90,12 +89,15 @@ describe("Cylon", function() {
});
});
context("specifiying new SSL key and cert", function() {
context("specifiying SSL key and cert", function() {
it("changes the SSL key and cert", function() {
expectedConfig.cert = "/path/to/cert/file";
expectedConfig.key = "/path/to/key/file";
expectedConfig.ssl.cert = "/path/to/cert/file";
expectedConfig.ssl.key = "/path/to/key/file";
cylon.api({ cert: "/path/to/cert/file", key: "/path/to/key/file" });
cylon.api({ ssl: {
cert: "/path/to/cert/file",
key: "/path/to/key/file" }
});
expect(cylon.api_config).to.be.eql(expectedConfig);
})