Skip to content

Commit

Permalink
fix: IDE-24 Disable Tracking via Segment and Amplitude for NON-MT-US …
Browse files Browse the repository at this point in the history
…customers (#441)

    fix: Added unit tests for analytics permitted checks.

    fix: Adding additional unit tests for disabling analytics and cleaning up.

    chore: Remove the gdpr aware sentry instrumentor

    chore: adding support in test case for MacOS arm64

    chore: updated logging to use the zerolog logger.

    chore: Update the timeout for Test_textDocumentInlineValues_InlineValues_IntegTest from 30 to 60 to allow test time 
    to succeed.

    This integration test involves initializing the language server and running a scan on a package.json file with
    one vulnerability, using the default endpoint (app.snyk.io/api). The test includes an assert.Eventually() with a 30 
    second timeout, and the scan to the server sometimes takes longer than 30 seconds, resulting in the test case 
    failure. 

    Co-authored-by: Christof Dorner <[email protected]>
  • Loading branch information
acke authored Feb 9, 2024
1 parent b1fba77 commit 949ed9a
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 227 deletions.
32 changes: 25 additions & 7 deletions application/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,16 @@ const (
)

var (
Version = "SNAPSHOT"
LsProtocolVersion = "development"
Development = "true"
currentConfig *Config
mutex = &sync.Mutex{}
LicenseInformation = "License information\n FILLED DURING BUILD"
Version = "SNAPSHOT"
LsProtocolVersion = "development"
Development = "true"
currentConfig *Config
mutex = &sync.Mutex{}
LicenseInformation = "License information\n FILLED DURING BUILD"
analyticsPermittedEnvironments = map[string]bool{
"api.snyk.io": true,
"api.us.snyk.io": true,
}
)

type CliSettings struct {
Expand Down Expand Up @@ -231,7 +235,6 @@ func New() *Config {
c.UpdateApiEndpoints(DefaultSnykApiUrl)
c.enableSnykLearnCodeActions = true
c.SetTelemetryEnabled(true)

c.clientSettingsFromEnv()
return c
}
Expand Down Expand Up @@ -872,3 +875,18 @@ func (c *Config) IdeName() string {
func (c *Config) IsFedramp() bool {
return c.Engine().GetConfiguration().GetBool(configuration.IS_FEDRAMP)
}

func (c *Config) IsAnalyticsPermitted() bool {
logger := c.Logger().With().Str("method", "IsAnalyticsPermitted").Logger()

u, err := url.Parse(c.Engine().GetConfiguration().GetString(configuration.API_URL))

if err != nil {
logger.Error().Err(err).Msg("unable to parse configured API_URL")
return false
}

_, found := analyticsPermittedEnvironments[u.Host]

return found
}
27 changes: 27 additions & 0 deletions application/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,33 @@ func Test_IsFedramp(t *testing.T) {

}

func Test_IsAnalyticsPermitted(t *testing.T) {
t.Run("Analytics not permitted for EU app", func(t *testing.T) {
c := New()
assert.True(t, c.UpdateApiEndpoints("https://app.eu.snyk.io/api"))
assert.False(t, c.IsAnalyticsPermitted())
})

t.Run("Analytics not permitted for EU api", func(t *testing.T) {
c := New()
assert.True(t, c.UpdateApiEndpoints("https://api.eu.snyk.io"))
assert.False(t, c.IsAnalyticsPermitted())
})

t.Run("Analytics permitted hostname", func(t *testing.T) {
c := New()
assert.True(t, c.UpdateApiEndpoints("https://app.snyk.io/api"))
assert.True(t, c.IsAnalyticsPermitted())
})

t.Run("Analytics permitted US hostname", func(t *testing.T) {
c := New()
assert.True(t, c.UpdateApiEndpoints("https://app.us.snyk.io/api"))
assert.True(t, c.IsAnalyticsPermitted())
})

}

func Test_IsTelemetryEnabled(t *testing.T) {
t.Setenv(EnableTelemetry, "1")
c := New()
Expand Down
2 changes: 1 addition & 1 deletion application/server/inline_values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,5 @@ func Test_textDocumentInlineValues_InlineValues_IntegTest(t *testing.T) {
assert.NoError(t, err)

return len(inlineValues) == 1 && inlineValues[0].Range.Start.Line == 17 && inlineValues[0].Range.End.Line == 17
}, 30*time.Second, 1*time.Second, "expected inline values to be served, but they were not")
}, 60*time.Second, 1*time.Second, "expected inline values to be served, but they were not")
}
4 changes: 2 additions & 2 deletions infrastructure/amplitude/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (c *Client) ScanModeIsSelected(properties ux2.ScanModeIsSelectedProperties)

func (c *Client) enqueueEvent(eventFn captureEvent) {
conf := config.CurrentConfig()
if conf.IsTelemetryEnabled() && !conf.IsFedramp() {
if conf.IsTelemetryEnabled() && conf.IsAnalyticsPermitted() {
eventFn(
c.authenticatedUserId,
ampli.EventOptions{
Expand Down Expand Up @@ -210,7 +210,7 @@ func (c *Client) Identify() {
}
c.authenticatedUserId = userId

if !conf.IsTelemetryEnabled() || conf.IsFedramp() {
if !conf.IsTelemetryEnabled() || !conf.IsAnalyticsPermitted() {
return
}
identifyEvent := ampli.Identify.Builder().UserId(userId).Build()
Expand Down
50 changes: 50 additions & 0 deletions infrastructure/amplitude/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,26 @@ func TestClient_NoIdentifyForFedramp(t *testing.T) {
assert.Equal(t, 0, len(fakeSegmentClient.trackedEvents))
}

func TestClient_NoIdentifyForEUEnvironments(t *testing.T) {
s, fakeSegmentClient, c := setupUnitTest(t)
c.SetTelemetryEnabled(true)
c.UpdateApiEndpoints("https://app.eu.snyk.io")

s.Identify()

assert.Equal(t, 0, len(fakeSegmentClient.trackedEvents))
}

func TestClient_IdentifyForUSEnvironments(t *testing.T) {
s, fakeSegmentClient, c := setupUnitTest(t)
c.SetTelemetryEnabled(true)
c.UpdateApiEndpoints("https://app.snyk.io")

s.Identify()

assert.Equal(t, 1, len(fakeSegmentClient.trackedEvents))
}

func Test_AnalyticEvents(t *testing.T) {
s, fakeSegmentClient, conf := setupUnitTest(t)
conf.SetRuntimeVersion("1.2.3")
Expand Down Expand Up @@ -181,6 +201,36 @@ func Test_AnalyticEventsNotSentForFedramp(t *testing.T) {
assert.Equal(t, 0, len(fakeSegmentClient.trackedEvents))
}

func Test_AnalyticEventsSentForAnalyticsPermittedEnvironments(t *testing.T) {
s, fakeSegmentClient, c := setupUnitTest(t)
c.SetTelemetryEnabled(true)
c.UpdateApiEndpoints("https://app.snyk.io")

s.PluginIsInstalled(ux.PluginIsInstalledProperties{})

assert.Equal(t, 1, len(fakeSegmentClient.trackedEvents))
}

func Test_AnalyticEventsSentForAnalyticsPermittedUSEnvironments(t *testing.T) {
s, fakeSegmentClient, c := setupUnitTest(t)
c.SetTelemetryEnabled(true)
c.UpdateApiEndpoints("https://app.us.snyk.io")

s.PluginIsInstalled(ux.PluginIsInstalledProperties{})

assert.Equal(t, 1, len(fakeSegmentClient.trackedEvents))
}

func Test_AnalyticEventsNotSentForAnalyticsNotPermittedEnvironments(t *testing.T) {
s, fakeSegmentClient, c := setupUnitTest(t)
c.SetTelemetryEnabled(true)
c.UpdateApiEndpoints("https://app.eu.snyk.io")

s.PluginIsInstalled(ux.PluginIsInstalledProperties{})

assert.Equal(t, 0, len(fakeSegmentClient.trackedEvents))
}

func setupUnitTest(t *testing.T) (*Client, *FakeSegmentClient, *config.Config) {
c := testutil.UnitTest(t)
authFunc := func() (string, error) { return "fakeUser", nil }
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/cli/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func AppendCliEnvironmentVariables(currentEnv []string, appendToken bool) (updat
if currentConfig.SnykApi() != "" {
updatedEnv = append(updatedEnv, ApiEnvVar+"="+currentConfig.SnykApi())
}
if !currentConfig.IsTelemetryEnabled() {
if !currentConfig.IsTelemetryEnabled() || !currentConfig.IsAnalyticsPermitted() {
updatedEnv = append(updatedEnv, DisableAnalyticsEnvVar+"=1")
}

Expand Down
10 changes: 10 additions & 0 deletions infrastructure/cli/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,14 @@ func TestAddConfigValuesToEnv(t *testing.T) {

assert.Contains(t, updatedEnv, "SNYK_CFG_DISABLE_ANALYTICS=1")
})

t.Run("Disables analytics, if analytics are not permitted for specific ApiUrl", func(t *testing.T) {
testutil.UnitTest(t)
config.CurrentConfig().SetTelemetryEnabled(true)
config.CurrentConfig().UpdateApiEndpoints("https://api.eu.snyk.io/api")

updatedEnv := AppendCliEnvironmentVariables([]string{}, true)

assert.Contains(t, updatedEnv, "SNYK_CFG_DISABLE_ANALYTICS=1")
})
}
59 changes: 0 additions & 59 deletions infrastructure/sentry/gdpr_aware_sentry_instrumentor.go

This file was deleted.

74 changes: 0 additions & 74 deletions infrastructure/sentry/gdpr_aware_sentry_instrumentor_test.go

This file was deleted.

Loading

0 comments on commit 949ed9a

Please sign in to comment.