diff --git a/demo/index.js b/demo/index.js index 40f359c..ed2d1b3 100644 --- a/demo/index.js +++ b/demo/index.js @@ -1,8 +1,7 @@ // load the agent from the local project and start it // env vars provide the configuration with default values as a fallback require('../lib')({ - url: process.env.SNYK_HOMEBASE_URL || 'http://localhost:8000/api/v1/beacon', - snapshotUrl: process.env.SNYK_SNAPSHOT_URL || 'http://localhost:8000/api/v2/snapshot/A3B8ADA9-B726-41E9-BC6B-5169F7F89A0C/node', + baseUrl: process.env.SNYK_HOMEBASE_ORIGIN || 'http://localhost:8000', projectId: process.env.SNYK_PROJECT_ID || 'A3B8ADA9-B726-41E9-BC6B-5169F7F89A0C', beaconIntervalMs: process.env.SNYK_BEACON_INTERVAL_MS || 10000, snapshotIntervalMs: process.env.SNYK_SNAPSHOT_INTERVAL_MS || 60 * 60 * 1000, diff --git a/lib/config.js b/lib/config.js index f18abaf..495aa42 100644 --- a/lib/config.js +++ b/lib/config.js @@ -9,6 +9,7 @@ module.exports = { function initConfig(startingConfig) { debug('Starting with config', startingConfig); validateStartingConfig(startingConfig); + const {baseUrl = 'https://homebase.snyk.io'} = startingConfig; const config = {}; config['enable'] = true; @@ -16,8 +17,8 @@ function initConfig(startingConfig) { config['agentId'] = uuidv4(); config['beaconIntervalMs'] = 60 * 1000; config['snapshotIntervalMs'] = 60 * 60 * 1000; - config['beaconUrl'] = 'https://homebase.snyk.io/api/v1/beacon'; - config['snapshotUrl'] = `https://homebase.snyk.io/api/v2/snapshot/${startingConfig.projectId}/node`; + config['beaconUrl'] = `${baseUrl}/api/v1/beacon`; + config['snapshotUrl'] = `${baseUrl}/api/v2/snapshot/${startingConfig.projectId}/node`; config['allowUnknownCA'] = false; config['functionPaths'] = { diff --git a/test/config.test.js b/test/config.test.js new file mode 100644 index 0000000..022ebd8 --- /dev/null +++ b/test/config.test.js @@ -0,0 +1,12 @@ +const test = require('tap').test; +const nock = require('nock'); +const sinon = require('sinon'); +const needle = require('needle'); + +const config = require('../lib/config'); + +test('Beacons and snapshots are sent to configured base url', async function(t) { + config.initConfig({projectId: 'whatever', baseUrl: 'http://localhost:8000'}); + t.equal(config.beaconUrl, 'http://localhost:8000/api/v1/beacon', 'beacon url with prefix is correct') + t.equal(config.snapshotUrl, 'http://localhost:8000/api/v2/snapshot/whatever/node', 'snapshot url with prefix is correct') +}); diff --git a/test/e2e.test.js b/test/e2e.test.js index 1930acf..62b1ccb 100644 --- a/test/e2e.test.js +++ b/test/e2e.test.js @@ -143,8 +143,7 @@ test('demo app reports a vuln method when called', async (t) => { const SNAPSHOT_INTERVAL_MS = 2500; // retrieve newer snapshot every 2.5 seconds // configure agent in demo server via env vars - process.env.SNYK_HOMEBASE_URL = 'http://localhost:8000/api/v1/beacon'; - process.env.SNYK_SNAPSHOT_URL = 'http://localhost:8000/api/v2/snapshot/A3B8ADA9-B726-41E9-BC6B-5169F7F89A0C/node'; + process.env.SNYK_HOMEBASE_ORIGIN = 'http://localhost:8000'; process.env.SNYK_BEACON_INTERVAL_MS = BEACON_INTERVAL_MS; process.env.SNYK_SNAPSHOT_INTERVAL_MS = SNAPSHOT_INTERVAL_MS; process.env.SNYK_TRIGGER_EXTRA_VULN = true; @@ -176,8 +175,7 @@ test('demo app reports a vuln method when called', async (t) => { // make sure all beacon calls were made t.ok(nock.isDone(), 'all beacon call were made'); - delete process.env.SNYK_HOMEBASE_URL; - delete process.env.SNYK_SNAPSHOT_URL; + delete process.env.SNYK_HOMEBASE_ORIGIN; delete process.env.SNYK_BEACON_INTERVAL_MS; delete process.env.SNYK_SNAPSHOT_INTERVAL_MS; delete process.env.SNYK_TRIGGER_EXTRA_VULN; diff --git a/test/failures.test.js b/test/failures.test.js index f407872..48e6f27 100644 --- a/test/failures.test.js +++ b/test/failures.test.js @@ -31,9 +31,8 @@ test('node agent does not crash the demo app', async (t) => { test('node agent does not crash the demo app', async (t) => { const BEACON_INTERVAL_MS = 1000; - process.env.SNYK_HOMEBASE_URL = 'http://localhost:-1/api/v1/beacon'; + process.env.SNYK_HOMEBASE_ORIGIN = 'http://localhost:-1'; process.env.SNYK_BEACON_INTERVAL_MS = BEACON_INTERVAL_MS; - process.env.SNYK_SNAPSHOT_URL = 'http://localhost:8000/api/v1/project/1231231/derp'; process.env.SNYK_SNAPSHOT_INTERVAL_MS = 200; // bring up the demo server, will fail on any outgoing request @@ -42,8 +41,7 @@ test('node agent does not crash the demo app', async (t) => { // wait to let the agent go through a cycle await sleep(BEACON_INTERVAL_MS); - delete process.env.SNYK_HOMEBASE_URL; - delete process.env.SNYK_SNAPSHOT_URL; + delete process.env.SNYK_HOMEBASE_ORIGIN; delete process.env.SNYK_SNAPSHOT_INTERVAL_MS; delete process.env.SNYK_BEACON_INTERVAL_MS;