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

feat: allow custom base url configuration for dev work [ROAD-209] #36

Merged
merged 1 commit into from
Aug 16, 2021
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
2 changes: 1 addition & 1 deletion src/snyk/base/modules/baseSnykModule.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as vscode from 'vscode';
import { configuration } from '../../common/configuration';
import { ContextService, IContextService } from '../../common/services/contextService';
import { IOpenerService, OpenerService } from '../../common/services/openerService';
import { IViewManagerService, ViewManagerService } from '../../common/services/viewManagerService';
Expand All @@ -9,6 +8,7 @@ import SnykStatusBarItem, { IStatusBarItem } from '../statusBarItem/statusBarIte
import SettingsWatcher from '../../common/watchers/settingsWatcher';
import { IWatcher } from '../../common/watchers/interfaces';
import { IBaseSnykModule, errorType } from './interfaces';
import { configuration } from '../../common/configuration/instance';

export default abstract class BaseSnykModule implements IBaseSnykModule {
statusBarItem: IStatusBarItem;
Expand Down
2 changes: 1 addition & 1 deletion src/snyk/base/modules/loginModule.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { checkSession, startSession } from '@snyk/code-client';
import { getIpFamily, IpFamily } from '@snyk/code-client/dist/http';
import { configuration } from '../../common/configuration';
import { configuration } from '../../common/configuration/instance';
import { SNYK_CONTEXT } from '../../common/constants/views';
import { Logger } from '../../common/logger/logger';
import { errorsLogs } from '../../common/messages/errorsServerLogMessages';
Expand Down
2 changes: 1 addition & 1 deletion src/snyk/base/modules/reportModule.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { constants, reportError } from '@snyk/code-client';
import * as _ from 'lodash';
import { configuration } from '../../common/configuration';
import { configuration } from '../../common/configuration/instance';
import {
COMMAND_DEBOUNCE_INTERVAL,
CONNECTION_ERROR_RETRY_INTERVAL,
Expand Down
2 changes: 1 addition & 1 deletion src/snyk/base/modules/snykLib.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as _ from 'lodash';
import { configuration } from '../../common/configuration';
import {
EXECUTION_DEBOUNCE_INTERVAL,
EXECUTION_PAUSE_INTERVAL,
Expand All @@ -13,6 +12,7 @@ import * as vscode from 'vscode';
import LoginModule from './loginModule';
import { analytics } from '../../common/analytics/analytics';
import { ISnykLib } from './interfaces';
import { configuration } from '../../common/configuration/instance';

export default class SnykLib extends LoginModule implements ISnykLib {
private _mode = SNYK_MODE_CODES.AUTO;
Expand Down
3 changes: 2 additions & 1 deletion src/snyk/base/views/welcome/welcomeViewProvider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import * as vscode from 'vscode';
import { configuration, FeaturesConfiguration } from '../../../common/configuration';
import { FeaturesConfiguration } from '../../../common/configuration/configuration';
import { configuration } from '../../../common/configuration/instance';
import { SNYK_CONTEXT } from '../../../common/constants/views';
import { IContextService } from '../../../common/services/contextService';

Expand Down
2 changes: 1 addition & 1 deletion src/snyk/common/analytics/analytics.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { configuration } from '../configuration';
import { configuration } from '../configuration/instance';
import { Logger } from '../logger/logger';
import { IAnalytics, Iteratively } from './itly';

Expand Down
2 changes: 1 addition & 1 deletion src/snyk/common/api/apiСlient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
import { configuration } from '../configuration';
import { configuration } from '../configuration/instance';

const defaultHeaders: Readonly<Record<string, string | boolean>> = {
Accept: 'application/json',
Expand Down
148 changes: 0 additions & 148 deletions src/snyk/common/configuration.ts

This file was deleted.

163 changes: 163 additions & 0 deletions src/snyk/common/configuration/configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import { URL } from 'url';
import { IDE_NAME } from '../constants/general';
import {
ADVANCED_ADVANCED_MODE_SETTING,
CODE_QUALITY_ENABLED_SETTING,
CODE_SECURITY_ENABLED_SETTING,
CONFIGURATION_IDENTIFIER,
TOKEN_SETTING,
YES_CRASH_REPORT_SETTING,
YES_TELEMETRY_SETTING,
YES_WELCOME_NOTIFICATION_SETTING,
} from '../constants/settings';
import { IVSCodeWorkspace } from '../vscode/workspace';

export type FeaturesConfiguration = {
codeSecurityEnabled: boolean | undefined;
codeQualityEnabled: boolean | undefined;
};

export interface IConfiguration {
isDevelopment: boolean;
source: string;
baseURL: string;
authHost: string;
snykCodeUrl: string;
token: string | undefined;
snykCodeToken: string | undefined;
setToken(token: string): Promise<void>;
shouldReportErrors: boolean;
shouldReportEvents: boolean;
getFeaturesConfiguration(): FeaturesConfiguration | undefined;
setFeaturesConfiguration(config: FeaturesConfiguration | undefined): Promise<void>;
}

export class Configuration implements IConfiguration {
// These attributes are used in tests
private defaultBaseURL = 'https://deeproxy.snyk.io';
private defaultAuthHost = 'https://snyk.io';

constructor(private processEnv: NodeJS.ProcessEnv = process.env, private workspace: IVSCodeWorkspace) {}

get isDevelopment(): boolean {
return !!this.processEnv.SNYK_VSCE_DEVELOPMENT;
}

get baseURL(): string {
if (this.isDevelopment) {
return this.processEnv.SNYK_VSCE_DEVELOPMENT_SNYKCODE_BASE_URL ?? 'https://deeproxy.dev.snyk.io';
}

return this.defaultBaseURL;
}

get authHost(): string {
return this.isDevelopment ? 'https://dev.snyk.io' : this.defaultAuthHost;
}

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

return `${authUrl.toString()}manage/snyk-code?from=vscode`;
}

get token(): string | undefined {
return this.workspace.getConfiguration(CONFIGURATION_IDENTIFIER, this.getConfigName(TOKEN_SETTING));
}

get snykCodeToken(): string | undefined {
return (this.isDevelopment && this.processEnv.SNYK_VSCE_DEVELOPMENT_SNYKCODE_TOKEN) || this.token;
}

async setToken(token: string | undefined): Promise<void> {
await this.workspace.updateConfiguration(CONFIGURATION_IDENTIFIER, this.getConfigName(TOKEN_SETTING), token, true);
}

get source(): string {
return this.processEnv.GITPOD_WORKSPACE_ID ? 'gitpod' : IDE_NAME;
}

getFeaturesConfiguration(): FeaturesConfiguration | undefined {
const codeSecurityEnabled = this.workspace.getConfiguration<boolean>(
CONFIGURATION_IDENTIFIER,
this.getConfigName(CODE_SECURITY_ENABLED_SETTING),
);
const codeQualityEnabled = this.workspace.getConfiguration<boolean>(
CONFIGURATION_IDENTIFIER,
this.getConfigName(CODE_QUALITY_ENABLED_SETTING),
);

if (!codeSecurityEnabled && !codeQualityEnabled) {
return undefined;
}

return {
codeSecurityEnabled,
codeQualityEnabled,
};
}

async setFeaturesConfiguration(config: FeaturesConfiguration | undefined): Promise<void> {
await this.workspace.updateConfiguration(
CONFIGURATION_IDENTIFIER,
this.getConfigName(CODE_SECURITY_ENABLED_SETTING),
config?.codeSecurityEnabled,
true,
);
await this.workspace.updateConfiguration(
CONFIGURATION_IDENTIFIER,
this.getConfigName(CODE_QUALITY_ENABLED_SETTING),
config?.codeQualityEnabled,
true,
);
}

get shouldReportErrors(): boolean {
return !!this.workspace.getConfiguration<boolean>(
CONFIGURATION_IDENTIFIER,
this.getConfigName(YES_CRASH_REPORT_SETTING),
);
}

get shouldReportEvents(): boolean {
return !!this.workspace.getConfiguration<boolean>(
CONFIGURATION_IDENTIFIER,
this.getConfigName(YES_TELEMETRY_SETTING),
);
}

async setShouldReportEvents(yesTelemetry: boolean): Promise<void> {
await this.workspace.updateConfiguration(
CONFIGURATION_IDENTIFIER,
this.getConfigName(YES_TELEMETRY_SETTING),
yesTelemetry,
true,
);
}

get shouldShowWelcomeNotification(): boolean {
return !!this.workspace.getConfiguration<boolean>(
CONFIGURATION_IDENTIFIER,
this.getConfigName(YES_WELCOME_NOTIFICATION_SETTING),
);
}

async hideWelcomeNotification(): Promise<void> {
await this.workspace.updateConfiguration(
CONFIGURATION_IDENTIFIER,
this.getConfigName(YES_WELCOME_NOTIFICATION_SETTING),
false,
true,
);
}

get shouldShowAdvancedView(): boolean {
return !!this.workspace.getConfiguration<boolean>(
CONFIGURATION_IDENTIFIER,
this.getConfigName(ADVANCED_ADVANCED_MODE_SETTING),
);
}

private getConfigName = (setting: string) => setting.replace(`${CONFIGURATION_IDENTIFIER}.`, '');
}
4 changes: 4 additions & 0 deletions src/snyk/common/configuration/instance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Configuration } from './configuration';
import { VSCodeWorkspace } from '../vscode/workspace';

export const configuration = new Configuration(process.env, new VSCodeWorkspace());
2 changes: 1 addition & 1 deletion src/snyk/common/constants/general.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Changing this requires changing display name in package.json.
export const SNYK_NAME = 'Snyk Vulnerability Scanner';
export const SNYK_NAME_EXTENSION = SNYK_NAME.toLowerCase().replaceAll(' ', '-');
export const SNYK_PUBLISHER = 'snyk-security';
export const SNYK_NAME_EXTENSION = SNYK_NAME.toLowerCase().replace(/\s/g, '-');
export const MAX_CONNECTION_RETRIES = 5; // max number of automatic retries before showing an error
export const IDE_NAME = 'vscode';
export const COMMAND_DEBOUNCE_INTERVAL = 200; // 200 milliseconds
Expand Down
Loading