Skip to content

Commit

Permalink
Merge pull request #37 from snyk/feat/ROAD-911_send_metrics
Browse files Browse the repository at this point in the history
feat: ✨ add preference for Telemetry [ROAD-911]
  • Loading branch information
CalamarBicefalo authored May 25, 2022
2 parents c74240a + 23da17d commit e59e9cf
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 40 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# Snyk Changelog

## [2.0.0] - Unreleased
### Changes

- add organization preference to specify organization to use for LS scans

### Fixes
- support download of Language Server for Apple M1
- fix download of Language Server in some situations

### Changes
- add organization preference to specify organization to use for LS scans
- add preference for usage statistics (metrics)

## [2.0.0] - v20220517.090738

### Changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ LsDownloader getLsDownloader() throws URISyntaxException {
IProxyData[] proxyData = runtimeEnvironment.getProxyService().select(baseUri);
var relevantProxyData = getRelevantProxyData(proxyData);
var builder = HttpClients.custom();
return new LsDownloader(runtimeEnvironment, builder, relevantProxyData);
return new LsDownloader(runtimeEnvironment, builder, relevantProxyData, logger);
}

private IProxyData getRelevantProxyData(IProxyData[] proxyData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ public class Preferences {
public static final String ADDITIONAL_PARAMETERS = "ADDITIONAL_PARAMETERS";
public static final String ADDITIONAL_ENVIRONMENT = "ADDITIONAL_ENVIRONMENT";
public static final String SEND_ERROR_REPORTS = "SEND_ERROR_REPORTS";
public static final String ENABLE_TELEMETRY = "ENABLE_TELEMETRY";

// This is a bit confusing - CLI takes DISABLE as env variable, but we ask for ENABLE, so we need to revert it
// when populating the environment
public static final String ENABLE_TELEMETRY = "SNYK_CFG_DISABLE_ANALYTICS";
public static final String ORGANIZATION_KEY = "SNYK_CFG_ORG";


Expand All @@ -40,6 +43,9 @@ public Preferences() {
if (getPref(SEND_ERROR_REPORTS) == null) {
store(SEND_ERROR_REPORTS, "true");
}
if (getPref(ENABLE_TELEMETRY) == null) {
store(ENABLE_TELEMETRY, "false");
}
}

public String getPref(String key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,10 @@
public class PreferencesPage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {

private static final Preferences PREFERENCES = new Preferences();

private final SnykCliRunner cliRunner = new SnykCliRunner();
private TokenFieldEditor tokenField;

public PreferencesPage() {
super(GRID);
Activator.getDefault().getPreferenceStore().addPropertyChangeListener(this::handlePropertyChange);
}

private void handlePropertyChange(PropertyChangeEvent event) {
// don't run 'snyk config' command, so we use settings from eclipse prefs only
// if (event.getProperty().equals(Preferences.ENDPOINT_KEY)) {
// String newEndpoint = event.getNewValue().toString();
// if (newEndpoint.isEmpty()) {
// cliRunner.snykUnsetEndpoint();
// } else {
// cliRunner.snykSetEndpoint(newEndpoint);
// }
// tokenField.emptyTextfield();
// }
}

@Override
Expand Down Expand Up @@ -97,7 +81,7 @@ protected void createFieldEditors() {

addField(new BooleanFieldEditor(Preferences.SEND_ERROR_REPORTS, "Send error reports to Snyk",
getFieldEditorParent()));
// TODO addField(new BooleanFieldEditor(Preferences.ENABLE_TELEMETRY, "Agree to send usage statistics to Snyk", getFieldEditorParent()));
addField(new BooleanFieldEditor(Preferences.ENABLE_TELEMETRY, "Send usage statistics to Snyk", getFieldEditorParent()));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ private void setupProcessBuilderBase(ProcessBuilder pb) {
String insecure = preferences.getPref(Preferences.INSECURE_KEY);
if (insecure != null) pb.command().add("--insecure");

String enableTelemetry = preferences.getPref(Preferences.ENABLE_TELEMETRY);
if (!enableTelemetry.isBlank() && Boolean.parseBoolean(enableTelemetry)) {
pb.environment().put(Preferences.ENABLE_TELEMETRY, "0");
} else {
pb.environment().put(Preferences.ENABLE_TELEMETRY, "1"); // default to disable telemetry
}

pb.environment().put(ENV_SNYK_INTEGRATION_NAME, "ECLIPSE");
pb.environment().put(ENV_SNYK_INTEGRATION_VERSION, getVersion());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Settings getCurrentSettings(Preferences preferences) {
String additionalEnv = preferences.getPref(Preferences.ADDITIONAL_ENVIRONMENT, "");
String path = preferences.getPref(Preferences.PATH_KEY, "");
String sendErrorReports = preferences.getPref(Preferences.SEND_ERROR_REPORTS, "");
String enableTelemetry = preferences.getPref(Preferences.ENABLE_TELEMETRY, "false");
String organization = preferences.getPref(Preferences.ORGANIZATION_KEY, "");
return new Settings(activateSnykOpenSource,
activateSnykCode,
Expand All @@ -59,6 +60,7 @@ Settings getCurrentSettings(Preferences preferences) {
additionalEnv,
path,
sendErrorReports,
enableTelemetry,
organization);
}

Expand All @@ -74,9 +76,20 @@ static class Settings {
private final String additionalEnv;
private final String path;
private final String sendErrorReports;
private final String enableTelemetry;
private final String organization;

public Settings(String activateSnykOpenSource, String activateSnykCode, String activateSnykIac, String insecure, String endpoint, String additionalParams, String additionalEnv, String path, String sendErrorReports, String organization) {
public Settings(String activateSnykOpenSource,
String activateSnykCode,
String activateSnykIac,
String insecure,
String endpoint,
String additionalParams,
String additionalEnv,
String path,
String sendErrorReports,
String enableTelemetry,
String organization) {
this.activateSnykOpenSource = activateSnykOpenSource;
this.activateSnykCode = activateSnykCode;
this.activateSnykIac = activateSnykIac;
Expand All @@ -86,6 +99,7 @@ public Settings(String activateSnykOpenSource, String activateSnykCode, String a
this.additionalEnv = additionalEnv;
this.path = path;
this.sendErrorReports = sendErrorReports;
this.enableTelemetry = enableTelemetry;
this.organization = organization;
}

Expand Down Expand Up @@ -125,6 +139,10 @@ public String getSendErrorReports() {
return this.sendErrorReports;
}

public String getEnableTelemetry() {
return this.enableTelemetry;
}

public String getOrganization() {
return this.organization;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,5 +213,12 @@ private void addIntegrationInfoToEnv(Map<String, String> env) {
void addTelemetry(Map<String, String> env) {
String sendErrorReports = preferences.getPref(Preferences.SEND_ERROR_REPORTS, "true");
env.put("SEND_ERROR_REPORTS", sendErrorReports);
String enableTelemetry = preferences.getPref(Preferences.ENABLE_TELEMETRY, "false");
// This is a bit confusing - CLI takes DISABLE as env variable, but we ask for ENABLE, so it's reverted
if (Boolean.parseBoolean(enableTelemetry)) {
env.put(Preferences.ENABLE_TELEMETRY, "0");
} else {
env.put(Preferences.ENABLE_TELEMETRY, "1");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,20 @@ public class LsDownloader {
private DefaultProxyRoutePlanner proxyRoutePlanner = null;
private BasicCredentialsProvider credentialsProvider = null;
private AuthScope authScope = null;
private ILog logger = Platform.getLog(getClass());
private final ILog logger;

private UsernamePasswordCredentials credentials;


public LsDownloader(LsRuntimeEnvironment environment, HttpClientBuilder httpClientBuilder, IProxyData data) {
public LsDownloader(LsRuntimeEnvironment environment, HttpClientBuilder httpClientBuilder, IProxyData data, ILog logger) {
this.runtimeEnvironment = environment;
configure(httpClientBuilder, data);
this.httpClient = httpClientBuilder.build();
if (logger == null) {
this.logger = Platform.getLog(getClass());
} else {
this.logger = logger;
}
}

private void configure(HttpClientBuilder builder, IProxyData data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ void testGetProcessBuilderLinux() {
when(preferenceMock.getPref(Preferences.INSECURE_KEY)).thenReturn("true");
when(preferenceMock.getEndpoint()).thenReturn("endpoint");
when(preferenceMock.getPref(Preferences.ORGANIZATION_KEY)).thenReturn("organization");
when(preferenceMock.getPref(Preferences.ENABLE_TELEMETRY)).thenReturn("true");
when(bundle.getVersion()).thenReturn(new Version(2, 0, 0));

ProcessRunner cut = new ProcessRunner(preferenceMock, bundle, logger);
Expand All @@ -42,9 +43,10 @@ void testGetProcessBuilderLinux() {
assertEquals("endpoint", env.get("SNYK_API"));
assertTrue(env.get("PATH").contains("good:path"));
assertEquals("organization", env.get(Preferences.ORGANIZATION_KEY));

assertEquals("0", env.get(Preferences.ENABLE_TELEMETRY));
verify(preferenceMock).getEndpoint();
verify(preferenceMock).getPref(Preferences.INSECURE_KEY);
verify(preferenceMock).getPref(Preferences.ORGANIZATION_KEY);
verify(preferenceMock).getPref(Preferences.ENABLE_TELEMETRY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,7 @@ protected void setUp() {

@Test
void testGetSettings() {
when(preferenceMock.getPref(Preferences.ACTIVATE_SNYK_IAC, "true")).thenReturn("iac");
when(preferenceMock.getPref(Preferences.ACTIVATE_SNYK_CODE, "false")).thenReturn("code");
when(preferenceMock.getPref(Preferences.ACTIVATE_SNYK_OPEN_SOURCE, "true")).thenReturn("oss");
when(preferenceMock.getPref(Preferences.INSECURE_KEY, "false")).thenReturn("true");
when(preferenceMock.getPref(Preferences.ENDPOINT_KEY, "")).thenReturn("endpoint");
when(preferenceMock.getPref(Preferences.ADDITIONAL_PARAMETERS, "")).thenReturn("addParams");
when(preferenceMock.getPref(Preferences.ADDITIONAL_ENVIRONMENT, "")).thenReturn("a=b;c=d");
when(preferenceMock.getPref(Preferences.PATH_KEY, "")).thenReturn("path");
when(preferenceMock.getPref(Preferences.SEND_ERROR_REPORTS, "")).thenReturn("true");
when(preferenceMock.getPref(Preferences.ORGANIZATION_KEY, "")).thenReturn("organization");
setupPreferenceMock();

var settings = new LsConfigurationUpdater().getCurrentSettings(preferenceMock);

Expand All @@ -41,5 +32,32 @@ void testGetSettings() {
assertEquals("path", settings.getPath());
assertEquals("true", settings.getSendErrorReports());
assertEquals("organization", settings.getOrganization());
assertEquals("true", settings.getEnableTelemetry());
}

private void setupPreferenceMock() {
when(preferenceMock.getPref(Preferences.ACTIVATE_SNYK_IAC, "true")).thenReturn("iac");
when(preferenceMock.getPref(Preferences.ACTIVATE_SNYK_IAC, "false")).thenReturn("iac");
when(preferenceMock.getPref(Preferences.ACTIVATE_SNYK_IAC, "")).thenReturn("iac");
when(preferenceMock.getPref(Preferences.ACTIVATE_SNYK_CODE, "true")).thenReturn("code");
when(preferenceMock.getPref(Preferences.ACTIVATE_SNYK_CODE, "false")).thenReturn("code");
when(preferenceMock.getPref(Preferences.ACTIVATE_SNYK_CODE, "")).thenReturn("code");
when(preferenceMock.getPref(Preferences.ACTIVATE_SNYK_OPEN_SOURCE, "true")).thenReturn("oss");
when(preferenceMock.getPref(Preferences.ACTIVATE_SNYK_OPEN_SOURCE, "false")).thenReturn("oss");
when(preferenceMock.getPref(Preferences.ACTIVATE_SNYK_OPEN_SOURCE, "")).thenReturn("oss");
when(preferenceMock.getPref(Preferences.INSECURE_KEY, "")).thenReturn("true");
when(preferenceMock.getPref(Preferences.INSECURE_KEY, "true")).thenReturn("true");
when(preferenceMock.getPref(Preferences.INSECURE_KEY, "false")).thenReturn("true");
when(preferenceMock.getPref(Preferences.ENDPOINT_KEY, "")).thenReturn("endpoint");
when(preferenceMock.getPref(Preferences.ADDITIONAL_PARAMETERS, "")).thenReturn("addParams");
when(preferenceMock.getPref(Preferences.ADDITIONAL_ENVIRONMENT, "")).thenReturn("a=b;c=d");
when(preferenceMock.getPref(Preferences.PATH_KEY, "")).thenReturn("path");
when(preferenceMock.getPref(Preferences.SEND_ERROR_REPORTS, "")).thenReturn("true");
when(preferenceMock.getPref(Preferences.SEND_ERROR_REPORTS, "true")).thenReturn("true");
when(preferenceMock.getPref(Preferences.SEND_ERROR_REPORTS, "false")).thenReturn("true");
when(preferenceMock.getPref(Preferences.ENABLE_TELEMETRY, "false")).thenReturn("true");
when(preferenceMock.getPref(Preferences.ENABLE_TELEMETRY, "true")).thenReturn("true");
when(preferenceMock.getPref(Preferences.ENABLE_TELEMETRY, "")).thenReturn("true");
when(preferenceMock.getPref(Preferences.ORGANIZATION_KEY, "")).thenReturn("organization");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,27 @@ void testOrganizationIsAddedToEnvironment() throws StorageException {

assertEquals("org", env.get(Preferences.ORGANIZATION_KEY));
}

@Test
void testEnableTelemetryIsAddedToEnvironment() throws StorageException {
HashMap<String, String> env = new HashMap<>();
when(preferenceMock.getPref(Preferences.ENABLE_TELEMETRY, "")).thenReturn("true");
when(preferenceMock.getPref(Preferences.ENABLE_TELEMETRY, "true")).thenReturn("true");
when(preferenceMock.getPref(Preferences.ENABLE_TELEMETRY, "false")).thenReturn("true");

environment.addTelemetry(env);
// This is a bit confusing - CLI takes DISABLE as env variable, but we ask for ENABLE, so it's reverted
assertEquals("0", env.get(Preferences.ENABLE_TELEMETRY));
}
@Test
void testEnableTelemetryIsAddedToEnvironmentDisabled() throws StorageException {
HashMap<String, String> env = new HashMap<>();
when(preferenceMock.getPref(Preferences.ENABLE_TELEMETRY, "")).thenReturn("false");
when(preferenceMock.getPref(Preferences.ENABLE_TELEMETRY, "true")).thenReturn("false");
when(preferenceMock.getPref(Preferences.ENABLE_TELEMETRY, "false")).thenReturn("false");

environment.addTelemetry(env);
// This is a bit confusing - CLI takes DISABLE as env variable, but we ask for ENABLE, so it's reverted
assertEquals("1", env.get(Preferences.ENABLE_TELEMETRY));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.protocol.HttpContext;
import org.eclipse.core.net.proxy.IProxyData;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IProgressMonitor;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -54,7 +55,7 @@ protected void setUp() {
void downloadShouldFailWhenShaWrongAndFileShouldNotBeOverwritten() throws IOException {
byte[] expectedLsContent = Files.readAllBytes(environment.getLSFile().toPath());

LsDownloader cut = new LsDownloader(environment, httpClientBuilder, null);
LsDownloader cut = new LsDownloader(environment, httpClientBuilder, null, mock(ILog.class));
File testFile = File.createTempFile("download-test", "tmp");
testFile.deleteOnExit();
// sha corresponds to file content (`echo "test 123" | sha256sum`)
Expand All @@ -74,7 +75,7 @@ void downloadShouldFailWhenShaWrongAndFileShouldNotBeOverwritten() throws IOExce

@Test
void downloadShouldIssueDownloadRequestForShaAndBinary() throws IOException {
LsDownloader cut = new LsDownloader(environment, httpClientBuilder, null);
LsDownloader cut = new LsDownloader(environment, httpClientBuilder, null, mock(ILog.class));
Path source = Paths.get("src/test/resources/ls-dummy-binary");
var testFile = Files.createTempFile("download-test", "tmp").toFile();
testFile.deleteOnExit();
Expand All @@ -98,7 +99,7 @@ void downloadShouldIssueDownloadRequestForShaAndBinary() throws IOException {
@Test
void downloadShouldUseGivenHttpsProxyData() {
IProxyData data = getDummyProxyData();
LsDownloader cut = new LsDownloader(environment, httpClientBuilder, data);
LsDownloader cut = new LsDownloader(environment, httpClientBuilder, data, mock(ILog.class));

assertNotNull(cut.getCredentials());
assertNotNull(cut.getCredentialsProvider());
Expand Down Expand Up @@ -169,7 +170,7 @@ public void disable() {

@Test
void getVersionShouldDownloadAndExtractTheLatestVersion() {
LsDownloader cut = new LsDownloader(environment, httpClientBuilder, null);
LsDownloader cut = new LsDownloader(environment, httpClientBuilder, null, mock(ILog.class));
var metadataObject = mockMetadata();
var actual = cut.getVersion();
assertEquals(metadataObject.getVersion(), actual);
Expand Down

0 comments on commit e59e9cf

Please sign in to comment.