Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: host URL for instances in different regions #4374

Merged
merged 1 commit into from
Jan 27, 2023
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
12 changes: 12 additions & 0 deletions src/lib/config/api-url.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
});
});
});
});