Skip to content

Commit cebc353

Browse files
authored
Merge pull request #4374 from snyk/fix/license_urls
fix: host URL for instances in different regions
2 parents 880f1e0 + 4667d6c commit cebc353

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

src/lib/config/api-url.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,15 @@ export function getHiddenApiUrl(restUrl: string): string {
180180

181181
return parsedBaseUrl.toString();
182182
}
183+
184+
export function getRootUrl(apiUrlString: string): string {
185+
// based on https://docs.snyk.io/snyk-processes/data-residency-at-snyk#what-regions-are-available the pattern is as follows
186+
// https://app.[region.]snyk.io
187+
// given an api url that starts with api means, that we can replace "api" by "app".
188+
189+
const apiUrl = new URL(apiUrlString);
190+
apiUrl.host = apiUrl.host.replace(/^api\./, '');
191+
192+
const rootUrl = apiUrl.protocol + '//' + apiUrl.host;
193+
return rootUrl;
194+
}

src/lib/config/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
getRestApiUrl,
66
getV1ApiUrl,
77
getHiddenApiUrl,
8+
getRootUrl,
89
} from './api-url';
910

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

8786
config.PUBLIC_VULN_DB_URL = 'https://security.snyk.io';

test/jest/unit/config/api-url.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
getBaseApiUrl,
33
getV1ApiUrl,
44
getRestApiUrl,
5+
getRootUrl,
56
} from '../../../../src/lib/config/api-url';
67

78
const urls = [
@@ -10,72 +11,84 @@ const urls = [
1011
expectedBase: 'https://snyk.io/api/',
1112
expectedV1: 'https://snyk.io/api/v1',
1213
expectedRest: 'https://api.snyk.io/rest',
14+
expectedRoot: 'https://snyk.io',
1315
},
1416
{
1517
userInput: 'https://snyk.io/api',
1618
expectedBase: 'https://snyk.io/api',
1719
expectedV1: 'https://snyk.io/api/v1',
1820
expectedRest: 'https://api.snyk.io/rest',
21+
expectedRoot: 'https://snyk.io',
1922
},
2023
{
2124
userInput: 'https://app.snyk.io/api',
2225
expectedBase: 'https://app.snyk.io/api',
2326
expectedV1: 'https://app.snyk.io/api/v1',
2427
expectedRest: 'https://api.snyk.io/rest',
28+
expectedRoot: 'https://app.snyk.io',
2529
},
2630
{
2731
userInput: 'https://app.snyk.io/api/v1',
2832
expectedBase: 'https://app.snyk.io/api/',
2933
expectedV1: 'https://app.snyk.io/api/v1',
3034
expectedRest: 'https://api.snyk.io/rest',
35+
expectedRoot: 'https://app.snyk.io',
3136
},
3237
{
3338
userInput: 'https://api.snyk.io/v1',
3439
expectedBase: 'https://api.snyk.io/',
3540
expectedV1: 'https://api.snyk.io/v1',
3641
expectedRest: 'https://api.snyk.io/rest',
42+
expectedRoot: 'https://snyk.io',
3743
},
3844
{
3945
userInput: 'https://api.snyk.io',
4046
expectedBase: 'https://api.snyk.io',
4147
expectedV1: 'https://api.snyk.io/v1',
4248
expectedRest: 'https://api.snyk.io/rest',
49+
expectedRoot: 'https://snyk.io',
4350
},
4451
{
4552
userInput: 'https://api.snyk.io/',
4653
expectedBase: 'https://api.snyk.io/',
4754
expectedV1: 'https://api.snyk.io/v1',
4855
expectedRest: 'https://api.snyk.io/rest',
56+
expectedRoot: 'https://snyk.io',
4957
},
5058
{
5159
userInput: 'https://api.custom.snyk.io',
5260
expectedBase: 'https://api.custom.snyk.io',
5361
expectedV1: 'https://api.custom.snyk.io/v1',
5462
expectedRest: 'https://api.custom.snyk.io/rest',
63+
expectedRoot: 'https://custom.snyk.io',
5564
},
5665
{
5766
userInput: 'http://localhost:9000/',
5867
expectedBase: 'http://localhost:9000/',
5968
expectedV1: 'http://localhost:9000/v1',
6069
expectedRest: 'http://localhost:9000/rest',
70+
expectedRoot: 'http://localhost:9000',
6171
},
6272
{
6373
userInput: 'http://localhost:9000/api/v1',
6474
expectedBase: 'http://localhost:9000/api/',
6575
expectedV1: 'http://localhost:9000/api/v1',
6676
expectedRest: 'http://localhost:9000/rest',
77+
expectedRoot: 'http://localhost:9000',
6778
},
6879
{
6980
userInput: 'http://alpha:omega@localhost:9000',
7081
expectedBase: 'http://alpha:omega@localhost:9000',
7182
expectedV1: 'http://alpha:omega@localhost:9000/v1',
7283
expectedRest: 'http://alpha:omega@localhost:9000/rest',
84+
expectedRoot: 'http://localhost:9000',
7385
},
7486
{
7587
userInput: 'https://app.dev.snyk.io/api/v1',
7688
expectedBase: 'https://app.dev.snyk.io/api/',
7789
expectedV1: 'https://app.dev.snyk.io/api/v1',
7890
expectedRest: 'https://api.dev.snyk.io/rest',
91+
expectedRoot: 'https://app.dev.snyk.io',
7992
},
8093
];
8194

@@ -152,4 +165,12 @@ describe('CLI config - API URL', () => {
152165
});
153166
});
154167
});
168+
169+
describe('getRootUrl', () => {
170+
urls.forEach((url) => {
171+
it(`returns ROOT URL ${url.userInput}`, () => {
172+
expect(getRootUrl(url.userInput)).toEqual(url.expectedRoot);
173+
});
174+
});
175+
});
155176
});

0 commit comments

Comments
 (0)