Skip to content

Commit

Permalink
Add support for TLS socket connections (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
OiNutter authored Aug 10, 2021
1 parent d4c075c commit fe9dc20
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ nodeq.connect({host: "localhost", port: 5000}, function(err, con) {
});
```

### Create TLS Connection

```javascript
var nodeq = require("node-q");
nodeq.connect({host: "localhost", port: 6000, useTLS: true}, function(err, con) {
if (err) throw err;
console.log("connected");
// interact with con like demonstrated below
});
```

### Create Connection with user and password auth

```javascript
Expand Down Expand Up @@ -270,6 +281,7 @@ For every primitive type in q, this module exports a method to wrap the JavaScri
* `unixSocket`: String (e. g. "/path/to/socket") (optional)
* `user`: String (optional)
* `password`: String (optional)
* `useTLS`: Boolean (optional)
* `socketNoDelay` : Boolean (optional, see http://nodejs.org/api/net.html#net_socket_setnodelay_nodelay)
* `socketTimeout`: Number (optional, see http://nodejs.org/api/net.html#net_socket_settimeout_timeout_callback)
* `nanos2date`: Boolean (optional, default: true)
Expand Down Expand Up @@ -368,7 +380,7 @@ If you have a kdb+tick setup please also run `make mochait`.

### Integration Test

Assumes a running q process on port 5000 with kdb+tick available in QHOME (`QHOME=~/q ~/q/m32/q -p 5000`)
Assumes a running q process on port 5000 with kdb+tick available in QHOME (`QHOME=~/q ~/q/m32/q -p 5000`). For the tls tests you will also need a running q process on port 6000 set up to require tls. Instructions for this can be found [here](https://code.kx.com/q/kb/ssl/). If you are using a self signed certificate you will also need to set the `NODE_TLS_REJECT_UNAUTHORIZED` environment variable to `0`.

make mochait

Expand Down
14 changes: 13 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var libc = require("./lib/c.js");
var net = require("net");
var tls = require("tls");
var events = require("events");
var util = require("util");
var assert = require("./lib/assert.js");
Expand Down Expand Up @@ -203,6 +204,7 @@ function connect(params, cb) {
assert.optionalBool(params.emptyChar2null, "params.emptyChar2null");
assert.optionalBool(params.long2number, "params.long2number");
assert.optionalString(params.unixSocket, "params.unixSocket");
assert.optionalBool(params.useTLS, "params.useTLS");
if (params.user !== undefined) {
assert.string(params.password, "password");
auth = params.user + ":" + params.password;
Expand Down Expand Up @@ -230,6 +232,10 @@ function connect(params, cb) {
if (error === false) {
socket.once("close", closecb);
var con = new Connection(socket, params.nanos2date, params.flipTables, params.emptyChar2null, params.long2number);
con.once("error", function(err) {
socket.removeListener("close", closecb);
cb(err)
})
con.auth(auth, function() {
socket.removeListener("close", closecb);
if (close === false) {
Expand All @@ -238,7 +244,13 @@ function connect(params, cb) {
});
}
});
socket = net.connect.apply(null, socketArgs);

if (params.useTLS) {
socket = tls.connect.apply(null, socketArgs)
} else {
socket = net.connect.apply(null, socketArgs)
}

if (params.socketTimeout !== undefined) {
socket.setTimeout(params.socketTimeout);
}
Expand Down
26 changes: 26 additions & 0 deletions itest/tls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var nodeq = require("../index.js"),
assert = require("assert")

describe("tls", function() {
"use strict"
it("should fail if endpoint expects tls and we don't set useTLS to true", function(done) {
nodeq.connect({host: "localhost", port: 6000}, function(err) {
assert.ok(err)
done()
});
});

it("should connect successfully if useTLS is true", function(done) {
nodeq.connect({host: "localhost", port: 6000, useTLS: true}, function(err) {
if (err) { throw err }
done()
});
})

it("should fail if useTLS is true and endpoint doesn't expect it", function(done) {
nodeq.connect({host: "localhost", port: 5000, useTLS: true}, function(err) {
assert.ok(err)
done()
});
})
});

0 comments on commit fe9dc20

Please sign in to comment.