From 2c8bd993d26bfadad62a8261eb131e40cca7293b Mon Sep 17 00:00:00 2001 From: Peter Svetlichny Date: Tue, 6 Feb 2018 16:47:53 -0800 Subject: [PATCH] test(ssl): address pr comments --- .../collector-remote-method.tap.js | 24 +++- test/integration/proxy-api-connection.tap.js | 111 +++++++++++++----- 2 files changed, 103 insertions(+), 32 deletions(-) diff --git a/test/integration/collector-remote-method.tap.js b/test/integration/collector-remote-method.tap.js index 03bfb6051a..b6ae9d3b63 100644 --- a/test/integration/collector-remote-method.tap.js +++ b/test/integration/collector-remote-method.tap.js @@ -50,7 +50,20 @@ tap.test('DataSender (callback style) talking to fake collector', function(t) { tap.test('remote method to get redirect host', function(t) { t.test('https with custom certificate', function(t) { t.plan(3) - var method = createRemoteMethod() + var method = createRemoteMethod(true) + + // create mock collector + startMockCollector(t, function() { + method.invoke([], function(error, returnValue) { + validateResponse(t, error, returnValue) + t.end() + }) + }) + }) + + t.test('https without custom certificate', function(t) { + t.plan(3) + var method = createRemoteMethod(false) // create mock collector startMockCollector(t, function() { @@ -66,12 +79,17 @@ tap.test('remote method to get redirect host', function(t) { t.equal(returnValue, 'some-collector-url', 'should get expected response') } - function createRemoteMethod() { + function createRemoteMethod(useCertificate) { var config = { host: 'ssl.lvh.me', port: 8765, ssl: true, - certificates: read(join(__dirname, '../lib/ca-certificate.crt'), 'utf8') + } + + if (useCertificate) { + config.certificates = [ + read(join(__dirname, '../lib/ca-certificate.crt'), 'utf8') + ] } var method = new RemoteMethod('preconnect', config) diff --git a/test/integration/proxy-api-connection.tap.js b/test/integration/proxy-api-connection.tap.js index 5139ac2091..eadc497169 100644 --- a/test/integration/proxy-api-connection.tap.js +++ b/test/integration/proxy-api-connection.tap.js @@ -11,7 +11,60 @@ var configurator = require('../../lib/config') var Agent = require('../../lib/agent') var CollectorAPI = require('../../lib/collector/api') -tap.test("setting proxy_port should use the proxy agent", function(t) { +tap.test('support ssl to the proxy', function(t) { + var port = 0 + var opts = { + key: read(join(__dirname, '../lib/test-key.key')), + cert: read(join(__dirname, '../lib/self-signed-test-certificate.crt')), + } + var server = proxySetup(https.createServer(opts)) + + server.listen(port, function() { + port = server.address().port + + var config = configurator.initialize({ + app_name: 'node.js Tests', + license_key: 'd67afc830dab717fd163bfcb0b8b88423e9a1a3b', + host: 'staging-collector.newrelic.com', + port: 443, + proxy: 'https://ssl.lvh.me:' + port, + ssl: true, + utilization: { + detect_aws: false, + detect_pcf: false, + detect_gcp: false, + detect_docker: false + }, + logging: { + level: 'trace' + }, + certificates: [ + read(join(__dirname, '..', 'lib', 'ca-certificate.crt'), 'utf8') + ] + }) + var agent = new Agent(config) + var api = new CollectorAPI(agent) + + api.connect(function(error, returned) { + t.notOk(error, 'connected without error') + t.ok(returned, 'got boot configuration') + t.ok(returned.agent_run_id, 'got run ID') + t.ok(agent.config.run_id, 'run ID set in configuration') + + api.shutdown(function(error, returned, json) { + t.notOk(error, 'should have shut down without issue') + t.equal(returned, null, 'collector explicitly returns null') + t.deepEqual(json, {return_value: null}, 'raw message looks right') + t.notOk(agent.config.run_id, 'run ID should have been cleared by shutdown') + + server.close() + t.end() + }) + }) + }) +}) + +tap.test('setting proxy_port should use the proxy agent', function(t) { var opts = { key: read(join(__dirname, '../lib/test-key.key')), cert: read(join(__dirname, '../lib/self-signed-test-certificate.crt')), @@ -46,16 +99,16 @@ tap.test("setting proxy_port should use the proxy agent", function(t) { var api = new CollectorAPI(agent) api.connect(function(error, returned) { - t.notOk(error, "connected without error") - t.ok(returned, "got boot configuration") - t.ok(returned.agent_run_id, "got run ID") - t.ok(agent.config.run_id, "run ID set in configuration") + t.notOk(error, 'connected without error') + t.ok(returned, 'got boot configuration') + t.ok(returned.agent_run_id, 'got run ID') + t.ok(agent.config.run_id, 'run ID set in configuration') api.shutdown(function(error, returned, json) { - t.notOk(error, "should have shut down without issue") - t.equal(returned, null, "collector explicitly returns null") - t.deepEqual(json, {return_value: null}, "raw message looks right") - t.notOk(agent.config.run_id, "run ID should have been cleared by shutdown") + t.notOk(error, 'should have shut down without issue') + t.equal(returned, null, 'collector explicitly returns null') + t.deepEqual(json, {return_value: null}, 'raw message looks right') + t.notOk(agent.config.run_id, 'run ID should have been cleared by shutdown') server.close() t.end() @@ -64,7 +117,7 @@ tap.test("setting proxy_port should use the proxy agent", function(t) { }) }) -tap.test("proxy agent with SSL tunnel to collector", function(t) { +tap.test('proxy agent with SSL tunnel to collector', function(t) { var server = proxySetup(https.createServer()) var port = 0 @@ -93,16 +146,16 @@ tap.test("proxy agent with SSL tunnel to collector", function(t) { api.connect(function(error, returned) { - t.notOk(error, "connected without error") - t.ok(returned, "got boot configuration") - t.ok(returned.agent_run_id, "got run ID") - t.ok(agent.config.run_id, "run ID set in configuration") + t.notOk(error, 'connected without error') + t.ok(returned, 'got boot configuration') + t.ok(returned.agent_run_id, 'got run ID') + t.ok(agent.config.run_id, 'run ID set in configuration') api.shutdown(function(error, returned, json) { - t.notOk(error, "should have shut down without issue") - t.equal(returned, null, "collector explicitly returns null") - t.deepEqual(json, {return_value: null}, "raw message looks right") - t.notOk(agent.config.run_id, "run ID should have been cleared by shutdown") + t.notOk(error, 'should have shut down without issue') + t.equal(returned, null, 'collector explicitly returns null') + t.deepEqual(json, {return_value: null}, 'raw message looks right') + t.notOk(agent.config.run_id, 'run ID should have been cleared by shutdown') server.close() t.end() @@ -111,7 +164,7 @@ tap.test("proxy agent with SSL tunnel to collector", function(t) { }) }) -tap.test("proxy authentication should set headers", function(t) { +tap.test('proxy authentication should set headers', function(t) { t.plan(2) var server = net.createServer() @@ -119,7 +172,7 @@ tap.test("proxy authentication should set headers", function(t) { server.on('connection', function(socket) { socket.on('data', function(chunk) { var data = chunk.toString().split('\r\n') - t.equal(data[0], 'CONNECT staging-collector.newrelic.com:80 HTTP/1.1') + t.equal(data[0], 'CONNECT staging-collector.newrelic.com:443 HTTP/1.1') t.equal(data[1], 'Proxy-Authorization: Basic YTpi') server.close() }) @@ -157,7 +210,7 @@ tap.test("proxy authentication should set headers", function(t) { }) }) -test("no proxy set should not use proxy agent", function(t) { +tap.test('no proxy set should not use proxy agent', function(t) { var config = configurator.initialize({ app_name: 'node.js Tests', license_key: 'd67afc830dab717fd163bfcb0b8b88423e9a1a3b', @@ -179,16 +232,16 @@ test("no proxy set should not use proxy agent", function(t) { api.connect(function(error, returned) { - t.notOk(error, "connected without error") - t.ok(returned, "got boot configuration") - t.ok(returned.agent_run_id, "got run ID") - t.ok(agent.config.run_id, "run ID set in configuration") + t.notOk(error, 'connected without error') + t.ok(returned, 'got boot configuration') + t.ok(returned.agent_run_id, 'got run ID') + t.ok(agent.config.run_id, 'run ID set in configuration') api.shutdown(function(error, returned, json) { - t.notOk(error, "should have shut down without issue") - t.equal(returned, null, "collector explicitly returns null") - t.deepEqual(json, {return_value: null}, "raw message looks right") - t.notOk(agent.config.run_id, "run ID should have been cleared by shutdown") + t.notOk(error, 'should have shut down without issue') + t.equal(returned, null, 'collector explicitly returns null') + t.deepEqual(json, {return_value: null}, 'raw message looks right') + t.notOk(agent.config.run_id, 'run ID should have been cleared by shutdown') t.end() })