Skip to content

Commit

Permalink
feat: add support for single tenant setups [ROAD-829] (#206)
Browse files Browse the repository at this point in the history
- add support for single tenant setups
- update changelog
- add tests for snykCodeUrl method
  • Loading branch information
pavel-snyk authored Apr 14, 2022
1 parent 3d87439 commit 1fd44a2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Snyk Security - Code and Open Source Dependencies Changelog

## [1.2.14]

### Added

- Snyk Code: add support for Single Tenant setups

## [1.2.13]

### Fixed
Expand Down
18 changes: 16 additions & 2 deletions src/snyk/common/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,12 @@ export class Configuration implements IConfiguration {
return this.processEnv.SNYK_VSCE_DEVELOPMENT_SNYKCODE_BASE_URL ?? 'https://deeproxy.dev.snyk.io';
} else if (this.customEndpoint) {
const url = new URL(this.customEndpoint);
url.host = `deeproxy.${url.host}`;

if (Configuration.isSingleTenant(url)) {
url.host = url.host.replace('app', 'deeproxy');
} else {
url.host = `deeproxy.${url.host}`;
}
url.pathname = url.pathname.replace('api', '');

return this.removeTrailingSlash(url.toString());
Expand Down Expand Up @@ -151,7 +156,12 @@ export class Configuration implements IConfiguration {

get snykCodeUrl(): string {
const authUrl = new URL(this.authHost);
authUrl.host = `app.${authUrl.host}`;

if (Configuration.isSingleTenant(authUrl)) {
authUrl.pathname = authUrl.pathname.replace('api', '');
} else {
authUrl.host = `app.${authUrl.host}`;
}

return `${authUrl.toString()}manage/snyk-code?from=vscode`;
}
Expand Down Expand Up @@ -348,6 +358,10 @@ export class Configuration implements IConfiguration {

private getConfigName = (setting: string) => setting.replace(`${CONFIGURATION_IDENTIFIER}.`, '');

private static isSingleTenant(url: URL): boolean {
return url.host.startsWith('app') && url.host.endsWith('snyk.io');
}

private removeTrailingSlash(str: string) {
return str.replace(/\/$/, '');
}
Expand Down
21 changes: 21 additions & 0 deletions src/test/unit/common/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,27 @@ suite('Configuration', () => {
strictEqual(configuration.snykCodeBaseURL, 'http://deeproxy.custom.endpoint.com');
});

test('Snyk Code: base url respects single tenant endpoint configuration', () => {
const workspace = stubWorkspaceConfiguration(ADVANCED_CUSTOM_ENDPOINT, 'https://app.custom.snyk.io/api');
const configuration = new Configuration({}, workspace);

strictEqual(configuration.snykCodeBaseURL, 'https://deeproxy.custom.snyk.io');
});

test('Snyk Code: code url respects custom endpoint configuration', () => {
const workspace = stubWorkspaceConfiguration(ADVANCED_CUSTOM_ENDPOINT, 'https://custom.endpoint.com/api');
const configuration = new Configuration({}, workspace);

strictEqual(configuration.snykCodeUrl, 'https://app.custom.endpoint.com/manage/snyk-code?from=vscode');
});

test('Snyk Code: code url respects single tenant endpoint configuration', () => {
const workspace = stubWorkspaceConfiguration(ADVANCED_CUSTOM_ENDPOINT, 'https://app.custom.snyk.io/api');
const configuration = new Configuration({}, workspace);

strictEqual(configuration.snykCodeUrl, 'https://app.custom.snyk.io/manage/snyk-code?from=vscode');
});

test('Snyk Code: Custom base url is returned when in development and custom url specified', () => {
const customUrl = 'https://custom.url';
const configuration = new Configuration(
Expand Down

0 comments on commit 1fd44a2

Please sign in to comment.