Skip to content

Commit

Permalink
fix: host URL for instances in different regions
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterSchafer committed Jan 27, 2023
1 parent 022c92b commit 4667d6c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/lib/config/api-url.ts
Expand Up @@ -180,3 +180,15 @@ export function getHiddenApiUrl(restUrl: string): string {

return parsedBaseUrl.toString();
}

export function getRootUrl(apiUrlString: string): string {
// based on https://docs.snyk.io/snyk-processes/data-residency-at-snyk#what-regions-are-available the pattern is as follows
// https://app.[region.]snyk.io
// given an api url that starts with api means, that we can replace "api" by "app".

const apiUrl = new URL(apiUrlString);
apiUrl.host = apiUrl.host.replace(/^api\./, '');

const rootUrl = apiUrl.protocol + '//' + apiUrl.host;
return rootUrl;
}
5 changes: 2 additions & 3 deletions src/lib/config/index.ts
Expand Up @@ -5,6 +5,7 @@ import {
getRestApiUrl,
getV1ApiUrl,
getHiddenApiUrl,
getRootUrl,
} from './api-url';

const DEFAULT_TIMEOUT = 5 * 60; // in seconds
Expand Down Expand Up @@ -79,9 +80,7 @@ if (!config.timeout) {
// this is a bit of an assumption that our web site origin is the same
// as our API origin, but for now it's okay - RS 2015-10-16
if (!config.ROOT) {
const apiUrl = new URL(config.API);
apiUrl.host = apiUrl.host.replace(/^ap[pi]\./, '');
config.ROOT = apiUrl.protocol + '//' + apiUrl.host;
config.ROOT = getRootUrl(config.API);
}

config.PUBLIC_VULN_DB_URL = 'https://security.snyk.io';
Expand Down
21 changes: 21 additions & 0 deletions test/jest/unit/config/api-url.spec.ts
Expand Up @@ -2,6 +2,7 @@ import {
getBaseApiUrl,
getV1ApiUrl,
getRestApiUrl,
getRootUrl,
} from '../../../../src/lib/config/api-url';

const urls = [
Expand All @@ -10,72 +11,84 @@ const urls = [
expectedBase: 'https://snyk.io/api/',
expectedV1: 'https://snyk.io/api/v1',
expectedRest: 'https://api.snyk.io/rest',
expectedRoot: 'https://snyk.io',
},
{
userInput: 'https://snyk.io/api',
expectedBase: 'https://snyk.io/api',
expectedV1: 'https://snyk.io/api/v1',
expectedRest: 'https://api.snyk.io/rest',
expectedRoot: 'https://snyk.io',
},
{
userInput: 'https://app.snyk.io/api',
expectedBase: 'https://app.snyk.io/api',
expectedV1: 'https://app.snyk.io/api/v1',
expectedRest: 'https://api.snyk.io/rest',
expectedRoot: 'https://app.snyk.io',
},
{
userInput: 'https://app.snyk.io/api/v1',
expectedBase: 'https://app.snyk.io/api/',
expectedV1: 'https://app.snyk.io/api/v1',
expectedRest: 'https://api.snyk.io/rest',
expectedRoot: 'https://app.snyk.io',
},
{
userInput: 'https://api.snyk.io/v1',
expectedBase: 'https://api.snyk.io/',
expectedV1: 'https://api.snyk.io/v1',
expectedRest: 'https://api.snyk.io/rest',
expectedRoot: 'https://snyk.io',
},
{
userInput: 'https://api.snyk.io',
expectedBase: 'https://api.snyk.io',
expectedV1: 'https://api.snyk.io/v1',
expectedRest: 'https://api.snyk.io/rest',
expectedRoot: 'https://snyk.io',
},
{
userInput: 'https://api.snyk.io/',
expectedBase: 'https://api.snyk.io/',
expectedV1: 'https://api.snyk.io/v1',
expectedRest: 'https://api.snyk.io/rest',
expectedRoot: 'https://snyk.io',
},
{
userInput: 'https://api.custom.snyk.io',
expectedBase: 'https://api.custom.snyk.io',
expectedV1: 'https://api.custom.snyk.io/v1',
expectedRest: 'https://api.custom.snyk.io/rest',
expectedRoot: 'https://custom.snyk.io',
},
{
userInput: 'http://localhost:9000/',
expectedBase: 'http://localhost:9000/',
expectedV1: 'http://localhost:9000/v1',
expectedRest: 'http://localhost:9000/rest',
expectedRoot: 'http://localhost:9000',
},
{
userInput: 'http://localhost:9000/api/v1',
expectedBase: 'http://localhost:9000/api/',
expectedV1: 'http://localhost:9000/api/v1',
expectedRest: 'http://localhost:9000/rest',
expectedRoot: 'http://localhost:9000',
},
{
userInput: 'http://alpha:omega@localhost:9000',
expectedBase: 'http://alpha:omega@localhost:9000',
expectedV1: 'http://alpha:omega@localhost:9000/v1',
expectedRest: 'http://alpha:omega@localhost:9000/rest',
expectedRoot: 'http://localhost:9000',
},
{
userInput: 'https://app.dev.snyk.io/api/v1',
expectedBase: 'https://app.dev.snyk.io/api/',
expectedV1: 'https://app.dev.snyk.io/api/v1',
expectedRest: 'https://api.dev.snyk.io/rest',
expectedRoot: 'https://app.dev.snyk.io',
},
];

Expand Down Expand Up @@ -152,4 +165,12 @@ describe('CLI config - API URL', () => {
});
});
});

describe('getRootUrl', () => {
urls.forEach((url) => {
it(`returns ROOT URL ${url.userInput}`, () => {
expect(getRootUrl(url.userInput)).toEqual(url.expectedRoot);
});
});
});
});

0 comments on commit 4667d6c

Please sign in to comment.