Skip to content

Commit be26ddb

Browse files
feat: process endpoint from hasAuthenticated message [IDE-732] (#210)
* feat: process endpoint from hasAuthenticated message * docs: update changelog
1 parent 390c9ba commit be26ddb

7 files changed

Lines changed: 98 additions & 44 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
# Snyk Security Changelog
22

3-
## [2.2.0] - Unreleased
3+
## [Unreleased]
44
### Changes
5+
- process api URL from hasAuthenticated message
6+
7+
## [2.2.0] - v20241024.154007
8+
### Changes
9+
- switch download URL to downloads.snyk.io
510
- remove keystore decoding from PR checks
611
- update jackson dependencies
712
- allow to select/deselect code quality findings

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public static synchronized Preferences getInstance(PreferenceStore store) {
5151
public static final String MANAGE_BINARIES_AUTOMATICALLY = "SNYK_CFG_MANAGE_BINARIES_AUTOMATICALLY";
5252
public static final String ORGANIZATION_KEY = EnvironmentConstants.ENV_SNYK_ORG;
5353
public static final String SCANNING_MODE_AUTOMATIC = "scanningMode";
54+
public static final String DEFAULT_ENDPOINT = "https://api.snyk.io";
5455

5556
private final PreferenceStore store;
5657

@@ -90,8 +91,11 @@ public static synchronized Preferences getInstance(PreferenceStore store) {
9091
}
9192

9293
String endpoint = SystemUtils.getEnvironmentVariable(EnvironmentConstants.ENV_SNYK_API, "");
93-
if (getPref(ENDPOINT_KEY) == null && !"".equals(endpoint)) {
94-
store(ENDPOINT_KEY, endpoint);
94+
if (getPref(ENDPOINT_KEY) == null) {
95+
if ("".equals(endpoint)) {
96+
endpoint = DEFAULT_ENDPOINT;
97+
}
98+
store(ENDPOINT_KEY, endpoint);
9599
}
96100

97101
String org = SystemUtils.getEnvironmentVariable(EnvironmentConstants.ENV_SNYK_ORG, "");

plugin/src/main/java/io/snyk/eclipse/plugin/wizards/SnykWizardConfigureAPIPage.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
public class SnykWizardConfigureAPIPage extends WizardPage implements Listener {
2121
private Text endpoint;
2222
private Button unknownCerts;
23-
private String defaultEndpoint = "https://api.snyk.io";
2423
private String initialEndpoint = Preferences.getInstance().getEndpoint();
2524

2625
public SnykWizardConfigureAPIPage() {
@@ -41,9 +40,9 @@ public void createControl(Composite parent) {
4140
composite.setLayout(gl);
4241

4342
Label endpointLabel = new Label(composite, SWT.NONE);
44-
endpointLabel.setText("Specify the Snyk API endpoint. Useful for custom Multi Tenant or Single Tenant setup (default: https://api.snyk.io):");
43+
endpointLabel.setText("Specify the Snyk API endpoint. Useful for custom Multi Tenant or Single Tenant setup (default: "+Preferences.DEFAULT_ENDPOINT+":");
4544

46-
String endpointValue = initialEndpoint == null || initialEndpoint.isBlank() ? this.defaultEndpoint : initialEndpoint;
45+
String endpointValue = initialEndpoint == null || initialEndpoint.isBlank() ? Preferences.DEFAULT_ENDPOINT : initialEndpoint;
4746
endpoint = new Text(composite, SWT.BORDER);
4847
endpoint.setText(endpointValue);
4948
gd = new GridData(GridData.FILL_HORIZONTAL);

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

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
import org.eclipse.jface.wizard.WizardDialog;
1616
import org.eclipse.lsp4e.LanguageClientImpl;
1717
import org.eclipse.lsp4j.ExecuteCommandParams;
18-
import org.eclipse.lsp4j.MessageParams;
19-
import org.eclipse.lsp4j.MessageType;
2018
import org.eclipse.lsp4j.ProgressParams;
2119
import org.eclipse.lsp4j.WorkDoneProgressCreateParams;
2220
import org.eclipse.lsp4j.jsonrpc.services.JsonNotification;
@@ -26,7 +24,6 @@
2624
import org.eclipse.ui.IWorkbenchWindow;
2725
import org.eclipse.ui.PlatformUI;
2826

29-
import com.fasterxml.jackson.core.JsonProcessingException;
3027
import com.fasterxml.jackson.databind.DeserializationFeature;
3128
import com.fasterxml.jackson.databind.ObjectMapper;
3229

@@ -36,7 +33,6 @@
3633
import io.snyk.eclipse.plugin.views.SnykView;
3734
import io.snyk.eclipse.plugin.wizards.SnykWizard;
3835
import io.snyk.languageserver.protocolextension.messageObjects.HasAuthenticatedParam;
39-
import io.snyk.languageserver.protocolextension.messageObjects.OAuthToken;
4036
import io.snyk.languageserver.protocolextension.messageObjects.SnykIsAvailableCliParams;
4137
import io.snyk.languageserver.protocolextension.messageObjects.SnykTrustedFoldersParams;
4238

@@ -45,14 +41,13 @@
4541
public class SnykExtendedLanguageClient extends LanguageClientImpl {
4642
private final ProgressManager progressMgr = new ProgressManager();
4743
private static SnykExtendedLanguageClient instance = null;
48-
private final ObjectMapper om = new ObjectMapper();
44+
// we overwrite the super-class field, so we can mock it
4945

50-
@SuppressWarnings("unused") // used in lsp4e language server instantiation
5146
public SnykExtendedLanguageClient() {
5247
super();
5348
instance = this;
5449
}
55-
50+
5651
public static SnykExtendedLanguageClient getInstance() {
5752
return instance; // we leave instantiation to LSP4e, no lazy construction here
5853
}
@@ -122,12 +117,27 @@ public boolean getSastEnabled() {
122117
@JsonNotification(value = "$/snyk.hasAuthenticated")
123118
public void hasAuthenticated(HasAuthenticatedParam param) {
124119
var prefs = Preferences.getInstance();
125-
prefs.store(Preferences.AUTH_TOKEN_KEY, param.getToken());
126-
triggerScan(null);
127-
128-
if (!param.getToken().isBlank()) {
129-
showAuthenticatedMessage();
130-
enableSnykViewRunActions();
120+
121+
var oldToken = prefs.getAuthToken();
122+
var oldApi = prefs.getEndpoint();
123+
124+
if (param.getApiUrl() != null && !param.getApiUrl().isBlank() && !param.getApiUrl().equals(oldApi)) {
125+
prefs.store(Preferences.ENDPOINT_KEY, param.getApiUrl());
126+
}
127+
128+
String newToken = param.getToken();
129+
boolean differentToken = newToken != oldToken;
130+
131+
if (differentToken) {
132+
prefs.store(Preferences.AUTH_TOKEN_KEY, newToken);
133+
}
134+
135+
if (!newToken.isBlank() && PlatformUI.isWorkbenchRunning()) {
136+
enableSnykViewRunActions();
137+
}
138+
139+
if (differentToken && !newToken.isBlank()) {
140+
triggerScan(null);
131141
}
132142
}
133143

@@ -175,13 +185,6 @@ private void enableSnykViewRunActions() {
175185
});
176186
}
177187

178-
private void showAuthenticatedMessage() {
179-
MessageParams messageParams = new MessageParams();
180-
messageParams.setType(MessageType.Info);
181-
messageParams.setMessage("The authentication token has been stored in Snyk Preferences.");
182-
super.showMessage(messageParams);
183-
}
184-
185188
private void runForProject(String projectName) {
186189
SnykView snykView = SnykStartup.getSnykView();
187190
if (snykView != null) {
@@ -197,6 +200,8 @@ private void executeCommand(@NonNull String command, List<Object> arguments) {
197200
SnykLogger.logError(e);
198201
}
199202
}
203+
204+
200205

201206
/**
202207
* Refresh the token using language server. Waits up to 2s for the token change.
Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
package io.snyk.languageserver.protocolextension.messageObjects;
22

33
public class HasAuthenticatedParam {
4-
private String token;
4+
private String token;
5+
private String apiUrl;
56

6-
public String getToken() {
7-
return token;
8-
}
7+
public String getToken() {
8+
return token;
9+
}
910

10-
public void setToken(String token) {
11-
this.token = token;
12-
}
11+
public void setToken(String token) {
12+
this.token = token;
13+
}
14+
15+
public String getApiUrl() {
16+
return apiUrl;
17+
}
18+
19+
public void setApiUrl(String apiUrl) {
20+
this.apiUrl = apiUrl;
21+
}
1322
}

target-platform/target-platform.target

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,43 +31,43 @@
3131
<dependency>
3232
<groupId>com.fasterxml.jackson.core</groupId>
3333
<artifactId>jackson-annotations</artifactId>
34-
<version>2.15.0</version>
34+
<version>2.18.0</version>
3535
<type>jar</type>
3636
</dependency>
3737
<dependency>
3838
<groupId>com.fasterxml.jackson.core</groupId>
3939
<artifactId>jackson-core</artifactId>
40-
<version>2.15.0</version>
40+
<version>2.18.0</version>
4141
<type>jar</type>
4242
</dependency>
4343
<dependency>
4444
<groupId>com.fasterxml.jackson.core</groupId>
4545
<artifactId>jackson-databind</artifactId>
46-
<version>2.15.0</version>
46+
<version>2.18.0</version>
4747
<type>jar</type>
4848
</dependency>
4949
<dependency>
5050
<groupId>org.apache.commons</groupId>
5151
<artifactId>commons-lang3</artifactId>
52-
<version>3.12.0</version>
52+
<version>3.17.0</version>
5353
<type>jar</type>
5454
</dependency>
5555
<dependency>
5656
<groupId>org.mockito</groupId>
5757
<artifactId>mockito-inline</artifactId>
58-
<version>4.5.1</version>
58+
<version>5.2.0</version>
5959
<type>jar</type>
6060
</dependency>
6161
<dependency>
6262
<groupId>org.mockito</groupId>
6363
<artifactId>mockito-junit-jupiter</artifactId>
64-
<version>4.5.1</version>
64+
<version>5.2.0</version>
6565
<type>jar</type>
6666
</dependency>
6767
<dependency>
6868
<groupId>org.objenesis</groupId>
6969
<artifactId>objenesis</artifactId>
70-
<version>3.1</version>
70+
<version>3.4</version>
7171
<type>jar</type>
7272
</dependency>
7373
</dependencies>

tests/src/test/java/io/snyk/languageserver/protocolextension/SnykExtendedLanguageClientTest.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.snyk.languageserver.protocolextension;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
import static org.junit.jupiter.api.Assertions.fail;
54

65
import org.junit.jupiter.api.BeforeEach;
76
import org.junit.jupiter.api.Test;
@@ -10,19 +9,19 @@
109
import io.snyk.eclipse.plugin.properties.preferences.Preferences;
1110
import io.snyk.eclipse.plugin.properties.preferences.PreferencesUtils;
1211
import io.snyk.languageserver.protocolextension.messageObjects.HasAuthenticatedParam;
13-
import io.snyk.languageserver.protocolextension.messageObjects.SnykIsAvailableCliParams;
1412
import io.snyk.languageserver.protocolextension.messageObjects.SnykTrustedFoldersParams;
1513

1614
class SnykExtendedLanguageClientTest {
1715
private InMemoryPreferenceStore store = new InMemoryPreferenceStore();
18-
private SnykExtendedLanguageClient cut = new SnykExtendedLanguageClient();
16+
private SnykExtendedLanguageClient cut;
1917
private Preferences pref;
2018

2119
@BeforeEach
2220
void setUp() {
2321
store = new InMemoryPreferenceStore();
2422
pref = Preferences.getInstance(store);
2523
PreferencesUtils.setPreferences(pref);
24+
cut = new SnykExtendedLanguageClient();
2625
}
2726

2827
@Test
@@ -44,5 +43,38 @@ void testAddTrustedPathsDeduplicatesAndTrims() {
4443

4544
assertEquals("trusted/path", store.getString(Preferences.TRUSTED_FOLDERS, ""));
4645
}
47-
46+
47+
@Test
48+
void testHasAuthenticatedSavesTokenAndApiURL() {
49+
HasAuthenticatedParam param = new HasAuthenticatedParam();
50+
param.setApiUrl("https://abc.d/ef");
51+
param.setToken("testToken");
52+
53+
cut.hasAuthenticated(param);
54+
55+
assertEquals(param.getToken(), pref.getAuthToken());
56+
assertEquals(param.getApiUrl(), pref.getEndpoint());
57+
}
58+
59+
@Test
60+
void testHasAuthenticatedCanHandleEmptyApiURL() {
61+
HasAuthenticatedParam param = new HasAuthenticatedParam();
62+
param.setToken("testToken");
63+
64+
cut.hasAuthenticated(param);
65+
66+
assertEquals(param.getToken(), pref.getAuthToken());
67+
assertEquals(Preferences.DEFAULT_ENDPOINT, pref.getEndpoint());
68+
}
69+
70+
@Test
71+
void testHasAuthenticatedUpdatesPrefToEmptyToken() {
72+
HasAuthenticatedParam param = new HasAuthenticatedParam();
73+
param.setToken("");
74+
75+
cut.hasAuthenticated(param);
76+
77+
assertEquals(param.getToken(), pref.getAuthToken());
78+
assertEquals(Preferences.DEFAULT_ENDPOINT, pref.getEndpoint());
79+
}
4880
}

0 commit comments

Comments
 (0)