-
Notifications
You must be signed in to change notification settings - Fork 30
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: expand server settings returned by LanguageClientMiddleware
[HEAD-975]
#392
Merged
bastiandoetsch
merged 4 commits into
main
from
fix/HEAD-975-expand-server-settings-middleware
Nov 20, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bce4745
fix: expand server settings returned by `LanguageClientMiddleware`
cat2608 569916e
refactor: merge server settings with initialization options
cat2608 e631dd7
refactor: server settings and test assertions
cat2608 e28a6ff
chore: add CHANGELOG
cat2608 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
import _ from 'lodash'; | ||
import { firstValueFrom, ReplaySubject, Subject } from 'rxjs'; | ||
import { IAuthenticationService } from '../../base/services/authenticationService'; | ||
import { CLI_INTEGRATION_NAME } from '../../cli/contants/integration'; | ||
import { Configuration, IConfiguration } from '../configuration/configuration'; | ||
import { IConfiguration } from '../configuration/configuration'; | ||
import { | ||
SNYK_ADD_TRUSTED_FOLDERS, | ||
SNYK_CLI_PATH, | ||
|
@@ -22,9 +21,8 @@ import { IVSCodeWindow } from '../vscode/window'; | |
import { IVSCodeWorkspace } from '../vscode/workspace'; | ||
import { LsExecutable } from './lsExecutable'; | ||
import { LanguageClientMiddleware } from './middleware'; | ||
import { InitializationOptions, LanguageServerSettings } from './settings'; | ||
import { LanguageServerSettings, ServerSettings } from './settings'; | ||
import { CodeIssueData, IacIssueData, OssIssueData, Scan } from './types'; | ||
import * as fs from 'fs'; | ||
|
||
export interface ILanguageServer { | ||
start(): Promise<void>; | ||
|
@@ -111,7 +109,7 @@ export class LanguageServer implements ILanguageServer { | |
synchronize: { | ||
configurationSection: CONFIGURATION_IDENTIFIER, | ||
}, | ||
middleware: new LanguageClientMiddleware(this.configuration), | ||
middleware: new LanguageClientMiddleware(this.configuration, this.user), | ||
/** | ||
* We reuse the output channel here as it's not properly disposed of by the language client ([email protected]) | ||
* See: https://github.com/microsoft/vscode-languageserver-node/blob/cdf4d6fdaefe329ce417621cf0f8b14e0b9bb39d/client/src/common/client.ts#L2789 | ||
|
@@ -178,16 +176,9 @@ export class LanguageServer implements ILanguageServer { | |
|
||
// Initialization options are not semantically equal to server settings, thus separated here | ||
// https://github.com/microsoft/language-server-protocol/issues/567 | ||
async getInitializationOptions(): Promise<InitializationOptions> { | ||
const settings = await LanguageServerSettings.fromConfiguration(this.configuration); | ||
|
||
return { | ||
...settings, | ||
integrationName: CLI_INTEGRATION_NAME, | ||
integrationVersion: await Configuration.getVersion(), | ||
deviceId: this.user.anonymousId, | ||
automaticAuthentication: 'false', | ||
}; | ||
async getInitializationOptions(): Promise<ServerSettings> { | ||
const settings = await LanguageServerSettings.fromConfiguration(this.configuration, this.user); | ||
return settings; | ||
} | ||
|
||
showOutputChannel(): void { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,94 @@ | ||
import _ from 'lodash'; | ||
import { IConfiguration, SeverityFilter } from '../configuration/configuration'; | ||
|
||
export type InitializationOptions = ServerSettings & { | ||
integrationName?: string; | ||
integrationVersion?: string; | ||
automaticAuthentication?: string; | ||
deviceId?: string; | ||
}; | ||
import { CLI_INTEGRATION_NAME } from '../../cli/contants/integration'; | ||
import { Configuration, IConfiguration, SeverityFilter } from '../configuration/configuration'; | ||
import { User } from '../user'; | ||
|
||
export type ServerSettings = { | ||
// Feature toggles | ||
activateSnykCodeSecurity?: string; | ||
activateSnykCodeQuality?: string; | ||
activateSnykOpenSource?: string; | ||
activateSnykIac?: string; | ||
|
||
// Endpoint path, and organization | ||
path?: string; | ||
cliPath?: string; | ||
endpoint?: string; | ||
organization?: string; | ||
|
||
// Authentication and parameters | ||
token?: string; | ||
automaticAuthentication?: string; | ||
additionalParams?: string; | ||
path?: string; | ||
manageBinariesAutomatically?: string; | ||
|
||
// Reporting and telemetry | ||
sendErrorReports?: string; | ||
organization?: string; | ||
enableTelemetry?: string; | ||
manageBinariesAutomatically?: string; | ||
cliPath?: string; | ||
token?: string; | ||
|
||
// Security and scanning settings | ||
filterSeverity?: SeverityFilter; | ||
scanningMode?: string; | ||
insecure?: string; | ||
|
||
// Trusted folders feature | ||
enableTrustedFoldersFeature?: string; | ||
trustedFolders?: string[]; | ||
insecure?: string; | ||
scanningMode?: string; | ||
|
||
// Snyk integration settings | ||
integrationName?: string; | ||
integrationVersion?: string; | ||
deviceId?: string; | ||
}; | ||
|
||
/** | ||
* Transforms a boolean or undefined value into a string representation. | ||
* It guarantees that undefined values are represented as 'true'. | ||
* This utility is used to ensure feature flags are enabled by default | ||
* when not explicitly set to false. | ||
* | ||
* @param {boolean | undefined} value - The value to transform. | ||
* @returns {string} - The string 'true' if the value is undefined or truthy, 'false' if the value is false. | ||
*/ | ||
export const defaultToTrue = (value: boolean | undefined): string => { | ||
return `${value !== undefined ? value : true}`; | ||
}; | ||
|
||
export class LanguageServerSettings { | ||
static async fromConfiguration(configuration: IConfiguration): Promise<ServerSettings> { | ||
static async fromConfiguration(configuration: IConfiguration, user: User): Promise<ServerSettings> { | ||
const featuresConfiguration = configuration.getFeaturesConfiguration(); | ||
|
||
const iacEnabled = _.isUndefined(featuresConfiguration.iacEnabled) ? true : featuresConfiguration.iacEnabled; | ||
const codeSecurityEnabled = _.isUndefined(featuresConfiguration.codeSecurityEnabled) | ||
? true | ||
: featuresConfiguration.codeSecurityEnabled; | ||
const codeQualityEnabled = _.isUndefined(featuresConfiguration.codeQualityEnabled) | ||
? true | ||
: featuresConfiguration.codeQualityEnabled; | ||
const iacEnabled = defaultToTrue(featuresConfiguration.iacEnabled); | ||
const codeSecurityEnabled = defaultToTrue(featuresConfiguration.codeSecurityEnabled); | ||
const codeQualityEnabled = defaultToTrue(featuresConfiguration.codeQualityEnabled); | ||
|
||
return { | ||
activateSnykCodeSecurity: `${codeSecurityEnabled}`, | ||
activateSnykCodeQuality: `${codeQualityEnabled}`, | ||
activateSnykCodeSecurity: codeSecurityEnabled, | ||
activateSnykCodeQuality: codeQualityEnabled, | ||
activateSnykOpenSource: 'false', | ||
activateSnykIac: `${iacEnabled}`, | ||
enableTelemetry: `${configuration.shouldReportEvents}`, | ||
sendErrorReports: `${configuration.shouldReportErrors}`, | ||
activateSnykIac: iacEnabled, | ||
|
||
cliPath: configuration.getCliPath(), | ||
endpoint: configuration.snykOssApiEndpoint, | ||
additionalParams: configuration.getAdditionalCliParameters(), | ||
organization: configuration.organization, | ||
|
||
token: await configuration.getToken(), | ||
automaticAuthentication: 'false', | ||
additionalParams: configuration.getAdditionalCliParameters(), | ||
manageBinariesAutomatically: `${configuration.isAutomaticDependencyManagementEnabled()}`, | ||
|
||
sendErrorReports: `${configuration.shouldReportErrors}`, | ||
enableTelemetry: `${configuration.shouldReportEvents}`, | ||
|
||
filterSeverity: configuration.severityFilter, | ||
scanningMode: configuration.scanningMode, | ||
insecure: `${configuration.getInsecure()}`, | ||
|
||
enableTrustedFoldersFeature: 'true', | ||
trustedFolders: configuration.getTrustedFolders(), | ||
insecure: `${configuration.getInsecure()}`, | ||
scanningMode: configuration.scanningMode, | ||
|
||
integrationName: CLI_INTEGRATION_NAME, | ||
integrationVersion: await Configuration.getVersion(), | ||
deviceId: user.anonymousId, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import assert from 'assert'; | ||
import { IConfiguration } from '../../../../snyk/common/configuration/configuration'; | ||
import { LanguageServerSettings, defaultToTrue } from '../../../../snyk/common/languageServer/settings'; | ||
import { User } from '../../../../snyk/common/user'; | ||
|
||
suite('LanguageServerSettings', () => { | ||
suite('defaultToTrue', () => { | ||
test('should return "true" for undefined values', () => { | ||
assert.strictEqual(defaultToTrue(undefined), 'true'); | ||
}); | ||
|
||
test('should return "true" for truthy values', () => { | ||
assert.strictEqual(defaultToTrue(true), 'true'); | ||
}); | ||
|
||
test('should return "false" for false values', () => { | ||
assert.strictEqual(defaultToTrue(false), 'false'); | ||
}); | ||
}); | ||
|
||
suite('fromConfiguration', () => { | ||
test('should generate server settings with default true values for undefined feature toggles', async () => { | ||
const mockUser = { anonymousId: 'anonymous-id' } as User; | ||
const mockConfiguration: IConfiguration = { | ||
shouldReportEvents: true, | ||
shouldReportErrors: false, | ||
snykOssApiEndpoint: 'https://dev.snyk.io/api', | ||
organization: 'my-org', | ||
// eslint-disable-next-line @typescript-eslint/require-await | ||
getToken: async () => 'snyk-token', | ||
getFeaturesConfiguration: () => ({}), // iacEnabled, codeSecurityEnabled, codeQualityEnabled are undefined | ||
getCliPath: () => '/path/to/cli', | ||
getAdditionalCliParameters: () => '--all-projects -d', | ||
getTrustedFolders: () => ['/trusted/path'], | ||
getInsecure: () => false, | ||
isAutomaticDependencyManagementEnabled: () => true, | ||
severityFilter: { critical: true, high: true, medium: true, low: false }, | ||
scanningMode: 'scan-mode', | ||
} as IConfiguration; | ||
|
||
const serverSettings = await LanguageServerSettings.fromConfiguration(mockConfiguration, mockUser); | ||
|
||
assert.strictEqual(serverSettings.activateSnykCodeSecurity, 'true'); | ||
assert.strictEqual(serverSettings.activateSnykCodeQuality, 'true'); | ||
assert.strictEqual(serverSettings.activateSnykIac, 'true'); | ||
assert.strictEqual(serverSettings.deviceId, 'anonymous-id'); | ||
|
||
assert.strictEqual(serverSettings.enableTelemetry, 'true'); | ||
assert.strictEqual(serverSettings.sendErrorReports, 'false'); | ||
assert.strictEqual(serverSettings.cliPath, '/path/to/cli'); | ||
|
||
assert.strictEqual(serverSettings.token, 'snyk-token'); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about this version 🤔 Where can I validate it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be the next version (minor) after the currently released.