1
1
package io .snyk .eclipse .plugin .preferences ;
2
2
3
+ import static io .snyk .eclipse .plugin .EnvironmentConstants .ENV_SNYK_API ;
4
+ import static io .snyk .eclipse .plugin .EnvironmentConstants .ENV_SNYK_ORG ;
3
5
import static io .snyk .eclipse .plugin .utils .FileSystemUtil .getBinaryDirectory ;
6
+ import static org .apache .commons .lang3 .SystemUtils .getEnvironmentVariable ;
4
7
5
8
import java .io .File ;
6
9
import java .util .Arrays ;
12
15
import java .util .concurrent .CompletableFuture ;
13
16
import java .util .concurrent .ConcurrentHashMap ;
14
17
15
- import org .apache .commons .lang3 .SystemUtils ;
16
18
import org .eclipse .core .runtime .preferences .IEclipsePreferences ;
17
19
import org .eclipse .core .runtime .preferences .InstanceScope ;
18
20
import org .eclipse .equinox .security .storage .ISecurePreferences ;
@@ -131,17 +133,21 @@ public static synchronized Preferences getTestInstance(IEclipsePreferences insec
131
133
insecureStore .setDefault (DEVICE_ID , UUID .randomUUID ().toString ());
132
134
insecureStore .setDefault (RELEASE_CHANNEL , "stable" );
133
135
insecureStore .setDefault (CLI_PATH , getDefaultCliPath ());
136
+ insecureStore .setDefault (ENDPOINT_KEY , DEFAULT_ENDPOINT );
137
+ insecureStore .setDefault (ORGANIZATION_KEY , "" );
134
138
135
- String endpoint = SystemUtils . getEnvironmentVariable (EnvironmentConstants . ENV_SNYK_API , "" );
136
- if (endpoint == null || endpoint .isBlank ()) {
137
- endpoint = DEFAULT_ENDPOINT ;
139
+ var endpoint = getEnvironmentVariable (ENV_SNYK_API , "" );
140
+ if (endpoint != null && ! endpoint .isBlank ()) {
141
+ store ( ENDPOINT_KEY , endpoint ) ;
138
142
}
139
- insecureStore .setDefault (ENDPOINT_KEY , endpoint );
140
- insecureStore .setDefault (ORGANIZATION_KEY ,
141
- SystemUtils .getEnvironmentVariable (EnvironmentConstants .ENV_SNYK_ORG , "" ));
142
-
143
- String token = SystemUtils .getEnvironmentVariable (EnvironmentConstants .ENV_SNYK_TOKEN , "" );
144
- if (getPref (AUTH_TOKEN_KEY ) == null && !"" .equals (token )) {
143
+
144
+ var org = getEnvironmentVariable (ENV_SNYK_ORG , "" );
145
+ if (org != null && !org .isBlank ()) {
146
+ store (ORGANIZATION_KEY , org );
147
+ }
148
+
149
+ String token = getEnvironmentVariable (EnvironmentConstants .ENV_SNYK_TOKEN , "" );
150
+ if (getPref (AUTH_TOKEN_KEY ) != null && !"" .equals (token )) {
145
151
store (AUTH_TOKEN_KEY , token );
146
152
}
147
153
@@ -162,8 +168,8 @@ private void migratePreferences() {
162
168
try {
163
169
final var defaultString = insecureStore .getDefaultString (constant );
164
170
final var value = securePreferences .get (constant , defaultString );
165
- if (insecureStore .isDefault (constant )) {
166
- this .insecurePreferences .put (constant , value );
171
+ if (insecureStore .isDefault (constant ) && ! defaultString . equals ( value ) ) {
172
+ this .insecurePreferences .put (constant , value );
167
173
}
168
174
securePreferences .remove (constant );
169
175
} catch (IllegalStateException | StorageException e ) { // NOPMD // no handling needed in this case
@@ -201,7 +207,7 @@ private final String getDefaultCliPath() {
201
207
}
202
208
203
209
public final String getPref (String key ) {
204
- return getPref (key , null );
210
+ return getPref (key , insecureStore . getDefaultString ( key ) );
205
211
}
206
212
207
213
public final String getPref (String key , String defaultValue ) {
@@ -230,11 +236,11 @@ public final String getAuthToken() {
230
236
}
231
237
232
238
public final String getEndpoint () {
233
- return getPref (ENDPOINT_KEY , DEFAULT_ENDPOINT );
239
+ return getPref (ENDPOINT_KEY );
234
240
}
235
241
236
242
public final String getLspVersion () {
237
- return getPref (LSP_VERSION );
243
+ return insecurePreferences . get (LSP_VERSION , insecureStore . getDefaultString ( LSP_VERSION ) );
238
244
}
239
245
240
246
public final Optional <String > getPath () {
@@ -250,15 +256,15 @@ public final String getCliPath() {
250
256
}
251
257
252
258
public final boolean isInsecure () {
253
- return insecurePreferences .getBoolean (INSECURE_KEY , false );
259
+ return insecurePreferences .getBoolean (INSECURE_KEY , insecureStore . getDefaultBoolean ( INSECURE_KEY ) );
254
260
}
255
261
256
262
public final void setIsInsecure (boolean isInsecure ) {
257
263
insecurePreferences .put (INSECURE_KEY , Boolean .toString (isInsecure ));
258
264
}
259
265
260
266
public final boolean isManagedBinaries () {
261
- return insecurePreferences .getBoolean (MANAGE_BINARIES_AUTOMATICALLY , true );
267
+ return insecurePreferences .getBoolean (MANAGE_BINARIES_AUTOMATICALLY , insecureStore . getDefaultBoolean ( MANAGE_BINARIES_AUTOMATICALLY ) );
262
268
}
263
269
264
270
public final void store (String key , String value ) {
@@ -280,7 +286,7 @@ public final void store(String key, String value) {
280
286
}
281
287
282
288
public final boolean getBooleanPref (String key ) {
283
- return insecurePreferences .getBoolean (key , false );
289
+ return insecurePreferences .getBoolean (key , insecureStore . getDefaultBoolean ( key ) );
284
290
}
285
291
286
292
public final boolean getBooleanPref (String key , boolean defaultValue ) {
0 commit comments