Skip to content

Commit

Permalink
Merge pull request #1175 from snyk/feat/log-endpoint
Browse files Browse the repository at this point in the history
Show message when using a custom API endpoint and validate its value
  • Loading branch information
JackuB authored Jun 18, 2020
2 parents f7c0abf + 2d1abfb commit 78acdcd
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/lib/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as snykConfig from 'snyk-config';
import { InvalidEndpointConfigError } from './errors/invalid-endpoint-config-error';
import { config as userConfig } from './user-config';
import * as url from 'url';

Expand All @@ -21,9 +22,18 @@ const config = (snykConfig.loadConfig(
__dirname + '/../..',
) as unknown) as Config;

// allow user config override of the api end point
// allow user config override of the API endpoint
const endpoint = userConfig.get('endpoint');
if (endpoint) {
if (endpoint && endpoint !== config.API) {
const parsedEndpoint = url.parse(endpoint);
// Endpoint option must be a valid URL including protocol
if (!parsedEndpoint || !parsedEndpoint.protocol || !parsedEndpoint.host) {
throw new InvalidEndpointConfigError();
}
console.info(
'Using a custom API endpoint from `snyk config` (tip: it should contain path to `/api`):',
endpoint,
);
config.API = endpoint;
}

Expand Down
10 changes: 10 additions & 0 deletions src/lib/errors/invalid-endpoint-config-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { CustomError } from './custom-error';

export class InvalidEndpointConfigError extends CustomError {
private static ERROR_MESSAGE =
"Invalid 'endpoint' config option. Endpoint must be a full and valid URL including protocol and for Snyk.io it should contain path to '/api'";

constructor() {
super(InvalidEndpointConfigError.ERROR_MESSAGE);
}
}
119 changes: 119 additions & 0 deletions test/endpoint-config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { test, tearDown } from 'tap';
import * as Proxyquire from 'proxyquire';
const proxyquire = Proxyquire.noPreserveCache();
import { InvalidEndpointConfigError } from '../src/lib/errors/invalid-endpoint-config-error';

const DEFAULT_API = 'https://snyk.io/api/v1';
const originalSnykApiEndpoint = process.env.SNYK_API;
delete process.env.SNYK_API;

tearDown(() => {
process.env.SNYK_API = originalSnykApiEndpoint;
});

test('uses default endpoint when none is provided by user', (t) => {
const config = proxyquire('../src/lib/config', {
'./user-config': {
config: {
get: () => {
// No user options provided
return;
},
},
},
});
t.equal(config.API, DEFAULT_API);
t.end();
});

test('uses default endpoint when user endpoint is the same', (t) => {
const config = proxyquire('../src/lib/config', {
'./user-config': {
config: {
get: (key) => {
if (key === 'endpoint') {
return DEFAULT_API;
}
return;
},
},
},
});
t.equal(config.API, DEFAULT_API);
t.end();
});

test('uses a valid custom endpoint when provided', (t) => {
const providedEndpoint = 'https://myendpoint.local/api';
const config = proxyquire('../src/lib/config', {
'./user-config': {
config: {
get: (key) => {
if (key === 'endpoint') {
return providedEndpoint;
}
return;
},
},
},
});
t.equal(config.API, providedEndpoint);
t.end();
});

test('uses a valid custom endpoint when provided by SNYK_API environment', (t) => {
const providedEndpoint = 'https://myendpoint.local/api';
process.env.SNYK_API = providedEndpoint;
const config = proxyquire('../src/lib/config', {
'./user-config': {
config: {
get: () => {
// No user options provided
return;
},
},
},
});
t.equal(config.API, providedEndpoint);
delete process.env.SNYK_API;
t.end();
});

test('uses a valid custom localhost endpoint when provided', (t) => {
const providedEndpoint = 'http://localhost:8000';
const config = proxyquire('../src/lib/config', {
'./user-config': {
config: {
get: (key) => {
if (key === 'endpoint') {
return providedEndpoint;
}
return;
},
},
},
});
t.equal(config.API, providedEndpoint);
t.end();
});

test('throws an error when endpoint option is not a valid URL', (t) => {
const providedEndpoint = 'myendpoint.local/api';
t.throws(
() =>
proxyquire('../src/lib/config', {
'./user-config': {
config: {
get: (key) => {
if (key === 'endpoint') {
return providedEndpoint;
}
return;
},
},
},
}),
InvalidEndpointConfigError,
);
t.end();
});

0 comments on commit 78acdcd

Please sign in to comment.