Skip to content

Commit

Permalink
Merge pull request #283 from zettajs/tls-support
Browse files Browse the repository at this point in the history
Support tls options on external spdy server.
  • Loading branch information
AdamMagaluk committed Jan 15, 2016
2 parents 0680e30 + 5601f34 commit c634e90
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 6 deletions.
26 changes: 21 additions & 5 deletions lib/http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ var rels = require('zetta-rels');

var querytopic = require('./query_topic');

var ZettaHttpServer = module.exports = function(zettaInstance) {
var ZettaHttpServer = module.exports = function(zettaInstance, options) {
var self = this;
options = (typeof options === 'object') ? options : {};
this.idCounter = 0;
this.zetta = zettaInstance;
this.peerRegistry = zettaInstance.peerRegistry;
Expand All @@ -33,11 +34,26 @@ var ZettaHttpServer = module.exports = function(zettaInstance) {
this._deviceQueries = [];

this._collectors = {};
this.server = spdy.createServer({
windowSize: 1024 * 1024,
plain: true,
ssl: false

// external http(s) server
var httpOptions = {
windowSize: 1024 * 1024
};
var tlsCheckOptions = ['cert', 'key', 'pfx', 'ca'];
var usingSSL = false;
Object.keys(options).forEach(function(k) {
httpOptions[k] = options[k];
if (tlsCheckOptions.indexOf(k) > -1) {
usingSSL = true;
}
});

// If any tls options were specified, use ssl and not plain
httpOptions.plain = (usingSSL) ? false : true;
httpOptions.ssl = (usingSSL) ? true : false;
this.server = spdy.createServer(httpOptions);

// internal server for z2z, allways ssl: false, plain: true
this.spdyServer = spdy.createServer({
windowSize: 1024 * 1024,
plain: true,
Expand Down
19 changes: 19 additions & 0 deletions test/fixture/keys/cert.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-----BEGIN CERTIFICATE-----
MIIDBjCCAe4CCQCycr+s9u2mtTANBgkqhkiG9w0BAQUFADBFMQswCQYDVQQGEwJB
VTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0
cyBQdHkgTHRkMB4XDTE2MDExNDE5MDkxMVoXDTE2MDIxMzE5MDkxMVowRTELMAkG
A1UEBhMCQVUxEzARBgNVBAgTClNvbWUtU3RhdGUxITAfBgNVBAoTGEludGVybmV0
IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB
AMYMwgbOmp6fiouC/yIA9zWvNOYIoBmzQlPl7P7YzVJCOXFO4eSe/Yf4S6wLaUXr
NiWk5OJ1XfJawBwlZPnnYGCGUBnyWRLYt+tYlH6verLtsJl9lHQ7EwzMAKgpw4rl
hCwbolbyY8wgR7rkFLGNqa6AziPqvDa9m7aO/y1xDygPURW6tDicydVy1KgNczEe
nRziIs3igTzZsLNeVq6cUbeXCz8yMZTassgNLZT3rizRu9U7lrhQBPvzAy8528h5
wcMJmli4ZZaQho1hYY04mAbrqYNY1K6dQa+sJ1R7ifvzZDhn259XOKClkcb+Sq7B
mFslQ6iWpvdWskz1I+8e9gsCAwEAATANBgkqhkiG9w0BAQUFAAOCAQEAUqmxHYHd
BPGVHtEDPhoeQsKJAw4QyYmB5kEUKNuVvttdlukt0tIQzB/fK/oYJasbGsoD/Bgd
XB7B0w5dL+Yq1bKAPx1LXYdep+EdtDEDIonxdhzGwDzK1RbuHljXUrpfXse0cPdb
J+5WqOuWBq8LDsaIylb1jhmeAdtZikaTf0K/TCbgsLwwllC9OSGFFB4U8ZtZRonm
uNqPc4DZ0ZayTUYpd97fhhUO08uhpe9tDECZX5RlIwwU6TnyvQZkErZia2g3jazg
9aN40zGaQsY+onsnBeEAXqY2xKPBgK2ga8ZhIGma8fKK/6QcnKPNH4Cagp9eo8rL
9dnd65DZR6tvQQ==
-----END CERTIFICATE-----
27 changes: 27 additions & 0 deletions test/fixture/keys/key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEpgIBAAKCAQEAxgzCBs6anp+Ki4L/IgD3Na805gigGbNCU+Xs/tjNUkI5cU7h
5J79h/hLrAtpRes2JaTk4nV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OEp0sGmHI8odCBujMehm+cYWpe4NLzz0J0VHCAzWdUFMVxvGE7tbh6D9MTV13
0kVsyPtIDIWjhSctA2y3SvE7AoGBAI7Txc9QYyd47NA0EdJlnDVaEGaflR2k4OOJ
gDwYqiBvv+dpBgmob+rHpt6OAEvAoQCsM6WpDC9t6B6ByWYYMYhlgGjPxF5Vq+QE
3BYOaL5d1kpOcj5uv/yDaNcKeMHn2iJCGA66ddRXdywmlV3wUdpGMlwo46x4in+G
sLJ2p1NpAoGBANtdaK4+W+f1UT/8HfyjoR9fYZymGyOBWqduJoQPJtPfy0WrW0vs
T71Q2NOvEyKaVMu2gsfZJi3Tr1psqH4UQV04D8QQXL0sRB7+IIEt7GjX1Nv8me/K
ui6LTF84WyCafoZbDDPRsc6UaTBMuafBtW1CJVm79zAAPj71ONf2y0x3
-----END RSA PRIVATE KEY-----
27 changes: 27 additions & 0 deletions test/test_zetta.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var assert = require('assert');
var util = require('util');
var fs = require('fs');
var https = require('https');
var zetta = require('../zetta');
var MemRegistry = require('./fixture/mem_registry');
var MemPeerRegistry = require('./fixture/mem_peer_registry');
Expand Down Expand Up @@ -59,6 +61,31 @@ describe('Zetta', function() {
});
});

it('support tls options for https server', function(done) {
var options = {
key: fs.readFileSync(__dirname + '/fixture/keys/key.pem'),
cert: fs.readFileSync(__dirname + '/fixture/keys/cert.pem')
};

var z = zetta({ registry: reg, peerRegistry: peerRegistry, tls: options })
.silent()
.listen(0, function(err) {
if (err) return done(err);

var port = z.httpServer.server.address().port;
var req = https.get({
host: 'localhost',
port: port,
path: '/',
rejectUnauthorized: false
}, function(res) {
assert.equal(res.statusCode, 200);
done();
});
req.on('error', done);
});
});

it('has the logger() function to pass in custom logging.', function(done) {
var z = zetta({ registry: reg, peerRegistry: peerRegistry });
z.logger(function(log) {
Expand Down
8 changes: 7 additions & 1 deletion zetta.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ var Zetta = module.exports = function(opts) {
this.log.init();
this._silent = false;

this.httpServer = new HttpServer(this);
var httpOptions = {};
if (typeof opts.tls === 'object') {
Object.keys(opts.tls).forEach(function(k) {
httpOptions[k] = opts.tls[k];
});
}
this.httpServer = new HttpServer(this, httpOptions);

var runtimeOptions = {
pubsub: this.pubsub,
Expand Down

0 comments on commit c634e90

Please sign in to comment.