Skip to content
This repository was archived by the owner on Jun 2, 2022. It is now read-only.

Commit 15e110e

Browse files
committed
feat: enable configuration of homebase url origin
1 parent 3fa649a commit 15e110e

File tree

5 files changed

+20
-12
lines changed

5 files changed

+20
-12
lines changed

demo/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// load the agent from the local project and start it
22
// env vars provide the configuration with default values as a fallback
33
require('../lib')({
4-
url: process.env.SNYK_HOMEBASE_URL || 'http://localhost:8000/api/v1/beacon',
5-
snapshotUrl: process.env.SNYK_SNAPSHOT_URL || 'http://localhost:8000/api/v2/snapshot/A3B8ADA9-B726-41E9-BC6B-5169F7F89A0C/node',
4+
baseUrl: process.env.SNYK_HOMEBASE_ORIGIN || 'http://localhost:8000',
65
projectId: process.env.SNYK_PROJECT_ID || 'A3B8ADA9-B726-41E9-BC6B-5169F7F89A0C',
76
beaconIntervalMs: process.env.SNYK_BEACON_INTERVAL_MS || 10000,
87
snapshotIntervalMs: process.env.SNYK_SNAPSHOT_INTERVAL_MS || 60 * 60 * 1000,

lib/config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ module.exports = {
99
function initConfig(startingConfig) {
1010
debug('Starting with config', startingConfig);
1111
validateStartingConfig(startingConfig);
12+
const {baseUrl = 'https://homebase.snyk.io'} = startingConfig;
1213
const config = {};
1314

1415
config['enable'] = true;
1516
config['flushOnExit'] = true;
1617
config['agentId'] = uuidv4();
1718
config['beaconIntervalMs'] = 60 * 1000;
1819
config['snapshotIntervalMs'] = 60 * 60 * 1000;
19-
config['beaconUrl'] = 'https://homebase.snyk.io/api/v1/beacon';
20-
config['snapshotUrl'] = `https://homebase.snyk.io/api/v2/snapshot/${startingConfig.projectId}/node`;
20+
config['beaconUrl'] = `${baseUrl}/api/v1/beacon`;
21+
config['snapshotUrl'] = `${baseUrl}/api/v2/snapshot/${startingConfig.projectId}/node`;
2122
config['allowUnknownCA'] = false;
2223

2324
config['functionPaths'] = {

test/config.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const test = require('tap').test;
2+
const nock = require('nock');
3+
const sinon = require('sinon');
4+
const needle = require('needle');
5+
6+
const config = require('../lib/config');
7+
8+
test('Beacons and snapshots are sent to configured base url', async function(t) {
9+
config.initConfig({projectId: 'whatever', baseUrl: 'http://localhost:8000'});
10+
t.equal(config.beaconUrl, 'http://localhost:8000/api/v1/beacon', 'beacon url with prefix is correct')
11+
t.equal(config.snapshotUrl, 'http://localhost:8000/api/v2/snapshot/whatever/node', 'snapshot url with prefix is correct')
12+
});

test/e2e.test.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,7 @@ test('demo app reports a vuln method when called', async (t) => {
143143
const SNAPSHOT_INTERVAL_MS = 2500; // retrieve newer snapshot every 2.5 seconds
144144

145145
// configure agent in demo server via env vars
146-
process.env.SNYK_HOMEBASE_URL = 'http://localhost:8000/api/v1/beacon';
147-
process.env.SNYK_SNAPSHOT_URL = 'http://localhost:8000/api/v2/snapshot/A3B8ADA9-B726-41E9-BC6B-5169F7F89A0C/node';
146+
process.env.SNYK_HOMEBASE_ORIGIN = 'http://localhost:8000';
148147
process.env.SNYK_BEACON_INTERVAL_MS = BEACON_INTERVAL_MS;
149148
process.env.SNYK_SNAPSHOT_INTERVAL_MS = SNAPSHOT_INTERVAL_MS;
150149
process.env.SNYK_TRIGGER_EXTRA_VULN = true;
@@ -176,8 +175,7 @@ test('demo app reports a vuln method when called', async (t) => {
176175
// make sure all beacon calls were made
177176
t.ok(nock.isDone(), 'all beacon call were made');
178177

179-
delete process.env.SNYK_HOMEBASE_URL;
180-
delete process.env.SNYK_SNAPSHOT_URL;
178+
delete process.env.SNYK_HOMEBASE_ORIGIN;
181179
delete process.env.SNYK_BEACON_INTERVAL_MS;
182180
delete process.env.SNYK_SNAPSHOT_INTERVAL_MS;
183181
delete process.env.SNYK_TRIGGER_EXTRA_VULN;

test/failures.test.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ test('node agent does not crash the demo app', async (t) => {
3131

3232
test('node agent does not crash the demo app', async (t) => {
3333
const BEACON_INTERVAL_MS = 1000;
34-
process.env.SNYK_HOMEBASE_URL = 'http://localhost:-1/api/v1/beacon';
34+
process.env.SNYK_HOMEBASE_ORIGIN = 'http://localhost:-1';
3535
process.env.SNYK_BEACON_INTERVAL_MS = BEACON_INTERVAL_MS;
36-
process.env.SNYK_SNAPSHOT_URL = 'http://localhost:8000/api/v1/project/1231231/derp';
3736
process.env.SNYK_SNAPSHOT_INTERVAL_MS = 200;
3837

3938
// 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) => {
4241
// wait to let the agent go through a cycle
4342
await sleep(BEACON_INTERVAL_MS);
4443

45-
delete process.env.SNYK_HOMEBASE_URL;
46-
delete process.env.SNYK_SNAPSHOT_URL;
44+
delete process.env.SNYK_HOMEBASE_ORIGIN;
4745
delete process.env.SNYK_SNAPSHOT_INTERVAL_MS;
4846
delete process.env.SNYK_BEACON_INTERVAL_MS;
4947

0 commit comments

Comments
 (0)