Skip to content
This repository was archived by the owner on Jun 2, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions demo/index.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
5 changes: 3 additions & 2 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ module.exports = {
function initConfig(startingConfig) {
debug('Starting with config', startingConfig);
validateStartingConfig(startingConfig);
const {baseUrl = 'https://homebase.snyk.io'} = startingConfig;
const config = {};

config['enable'] = true;
config['flushOnExit'] = true;
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'] = {
Expand Down
12 changes: 12 additions & 0 deletions test/config.test.js
Original file line number Diff line number Diff line change
@@ -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')
});
6 changes: 2 additions & 4 deletions test/e2e.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 2 additions & 4 deletions test/failures.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

Expand Down