Skip to content

Commit d6ff4e9

Browse files
authored
feat: enables SCLE by using LS to fetch SAST settings [nebula-1427] (#149)
feat: enables SCLE by using LS to fetch SAST settings
1 parent 52de1f7 commit d6ff4e9

3 files changed

Lines changed: 50 additions & 89 deletions

File tree

plugin/src/main/java/io/snyk/eclipse/plugin/properties/PreferencesPage.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
import org.eclipse.ui.IWorkbenchPreferencePage;
1313

1414
import io.snyk.eclipse.plugin.SnykStartup;
15-
import io.snyk.eclipse.plugin.properties.preferences.ApiClient;
1615
import io.snyk.eclipse.plugin.properties.preferences.Preferences;
1716
import io.snyk.eclipse.plugin.utils.SnykLogger;
1817
import io.snyk.languageserver.LsConfigurationUpdater;
18+
import io.snyk.languageserver.protocolextension.SnykExtendedLanguageClient;
1919

2020
public class PreferencesPage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {
2121
private BooleanFieldEditor snykCodeCheckbox;
@@ -120,8 +120,8 @@ public boolean performOk() {
120120
}
121121

122122
private void disableSnykCodeIfOrgDisabled() {
123-
var apiClient = new ApiClient();
124-
if (snykCodeCheckbox.getBooleanValue() && !apiClient.checkSnykCodeEnablement()) {
123+
boolean isSastEnabled = SnykExtendedLanguageClient.getInstance().getSastEnabled();
124+
if (snykCodeCheckbox.getBooleanValue() && !isSastEnabled) {
125125
String message = "Snyk Code disabled, because it is not enabled for your organization. After you close this preference page, it will stay disabled.";
126126
snykCodeCheckbox.setLabelText(snykCodeCheckbox.getLabelText() + " (" + message + ")");
127127
SnykLogger.logInfo(message);

plugin/src/main/java/io/snyk/eclipse/plugin/properties/preferences/ApiClient.java

Lines changed: 0 additions & 85 deletions
This file was deleted.

plugin/src/main/java/io/snyk/languageserver/protocolextension/SnykExtendedLanguageClient.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.eclipse.ui.PlatformUI;
2828

2929
import com.fasterxml.jackson.core.JsonProcessingException;
30+
import com.fasterxml.jackson.databind.DeserializationFeature;
3031
import com.fasterxml.jackson.databind.ObjectMapper;
3132

3233
import io.snyk.eclipse.plugin.SnykStartup;
@@ -39,6 +40,7 @@
3940
import io.snyk.languageserver.protocolextension.messageObjects.SnykIsAvailableCliParams;
4041
import io.snyk.languageserver.protocolextension.messageObjects.SnykTrustedFoldersParams;
4142

43+
4244
@SuppressWarnings("restriction")
4345
public class SnykExtendedLanguageClient extends LanguageClientImpl {
4446
private final ProgressManager progressMgr = new ProgressManager();
@@ -102,6 +104,22 @@ public void triggerAuthentication() {
102104
public void trustWorkspaceFolders() {
103105
executeCommand("snyk.trustWorkspaceFolders", new ArrayList<>());
104106
}
107+
108+
public boolean getSastEnabled() {
109+
ExecuteCommandParams params = new ExecuteCommandParams("snyk.getSettingsSastEnabled", new ArrayList<>());
110+
try {
111+
CompletableFuture<Object> lsSastSettings = getConnectedLanguageServer().getWorkspaceService().executeCommand(params);
112+
ObjectMapper mapper = new ObjectMapper();
113+
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
114+
SastSettings sastSettings = mapper.convertValue(lsSastSettings.get(), SastSettings.class);
115+
116+
return sastSettings.sastEnabled;
117+
} catch (Exception e) {
118+
SnykLogger.logError(e);
119+
}
120+
121+
return false;
122+
}
105123

106124
@JsonNotification(value = "$/snyk.hasAuthenticated")
107125
public void hasAuthenticated(HasAuthenticatedParam param) {
@@ -216,5 +234,33 @@ public boolean refreshOAuthToken() {
216234
var newToken = future.completeOnTimeout(token, 2, TimeUnit.SECONDS).join();
217235
return !token.equals(newToken);
218236
}
219-
237+
238+
static class SastSettings {
239+
public boolean sastEnabled;
240+
241+
public LocalCodeEngine localCodeEngine;
242+
243+
public boolean reportFalsePositivesEnabled;
244+
245+
public String org;
246+
247+
public boolean autofixEnabled;
248+
}
249+
250+
/**
251+
* SAST local code engine configuration.
252+
*/
253+
static class LocalCodeEngine {
254+
public boolean enabled;
255+
256+
public String url;
257+
}
258+
259+
public static <T> T convertInstanceOfObject(Object o, Class<T> clazz) {
260+
try {
261+
return clazz.cast(o);
262+
} catch(ClassCastException e) {
263+
return null;
264+
}
265+
}
220266
}

0 commit comments

Comments
 (0)