From 809ee6259376c69f75e201570d230a187b920cce Mon Sep 17 00:00:00 2001 From: PeterSchafer <101886095+PeterSchafer@users.noreply.github.com> Date: Thu, 7 Sep 2023 13:25:02 +0200 Subject: [PATCH] fix: consistent telemetry setting (#4825) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: consistent telemetry setting * feat: —env option to woof Add —env option to woof, add test, and change makefile to cleanup all embedded files --------- Co-authored-by: Avishagp --- cliv2/Makefile | 2 +- cliv2/cmd/cliv2/main.go | 12 +++++++----- cliv2/internal/cliv2/cliv2.go | 7 +++++-- cliv2/internal/constants/constants.go | 1 + cliv2/pkg/basic_workflows/legacycli.go | 19 ++++++++++++++----- src/cli/commands/woof/getWoof.ts | 5 +++++ test/jest/acceptance/analytics.spec.ts | 17 +++++++++++++++++ 7 files changed, 50 insertions(+), 13 deletions(-) diff --git a/cliv2/Makefile b/cliv2/Makefile index a2488af19f..560499bc50 100644 --- a/cliv2/Makefile +++ b/cliv2/Makefile @@ -237,7 +237,7 @@ clean: @rm -f -r $(BUILD_DIR) @rm -f -r $(CACHE_DIR) @rm -f $(V1_DIRECTORY)/$(APPLICATION_NAME)-* - @rm -f $(V1_DIRECTORY)/$(V1_EMBEDDED_FILE_OUTPUT) + @rm -f $(V1_DIRECTORY)/embedded*.go @rm -f -r $(EMBEDDED_DATA_DIR)/licenses .PHONY: install diff --git a/cliv2/cmd/cliv2/main.go b/cliv2/cmd/cliv2/main.go index 9be7a5cb97..959dbadb27 100644 --- a/cliv2/cmd/cliv2/main.go +++ b/cliv2/cmd/cliv2/main.go @@ -8,7 +8,6 @@ import ( "encoding/hex" "encoding/json" "fmt" - "github.com/snyk/go-application-framework/pkg/networking/fips" "io" "net/http" "os" @@ -17,13 +16,12 @@ import ( "strings" "time" + "github.com/snyk/go-application-framework/pkg/networking/fips" + "github.com/rs/zerolog" "github.com/snyk/cli-extension-dep-graph/pkg/depgraph" "github.com/snyk/cli-extension-iac-rules/iacrules" "github.com/snyk/cli-extension-sbom/pkg/sbom" - "github.com/snyk/cli/cliv2/internal/cliv2" - "github.com/snyk/cli/cliv2/internal/constants" - "github.com/snyk/cli/cliv2/pkg/basic_workflows" "github.com/snyk/go-application-framework/pkg/analytics" "github.com/snyk/go-application-framework/pkg/app" "github.com/snyk/go-application-framework/pkg/auth" @@ -36,6 +34,10 @@ import ( snykls "github.com/snyk/snyk-ls/ls_extension" "github.com/spf13/cobra" "github.com/spf13/pflag" + + "github.com/snyk/cli/cliv2/internal/cliv2" + "github.com/snyk/cli/cliv2/internal/constants" + "github.com/snyk/cli/cliv2/pkg/basic_workflows" ) var internalOS string @@ -99,7 +101,7 @@ func initApplicationConfiguration(config configuration.Configuration) { config.AddAlternativeKeys(configuration.AUTHENTICATION_BEARER_TOKEN, []string{"snyk_docker_token"}) config.AddAlternativeKeys(configuration.API_URL, []string{"endpoint"}) config.AddAlternativeKeys(configuration.ADD_TRUSTED_CA_FILE, []string{"NODE_EXTRA_CA_CERTS"}) - config.AddAlternativeKeys(configuration.ANALYTICS_DISABLED, []string{"snyk_analytics_disabled", "snyk_cfg_disable_analytics", "disable-analytics", "disable_analytics"}) + config.AddAlternativeKeys(configuration.ANALYTICS_DISABLED, []string{strings.ToLower(constants.SNYK_ANALYTICS_DISABLED_ENV), "snyk_cfg_disable_analytics", "disable-analytics", "disable_analytics"}) // if the CONFIG_KEY_OAUTH_TOKEN is specified as env var, we don't apply any additional logic _, ok := os.LookupEnv(auth.CONFIG_KEY_OAUTH_TOKEN) diff --git a/cliv2/internal/cliv2/cliv2.go b/cliv2/internal/cliv2/cliv2.go index 82e82c52ae..5b189363d1 100644 --- a/cliv2/internal/cliv2/cliv2.go +++ b/cliv2/internal/cliv2/cliv2.go @@ -15,12 +15,13 @@ import ( "strings" "github.com/gofrs/flock" + "github.com/snyk/go-application-framework/pkg/utils" + "github.com/snyk/cli/cliv2/internal/constants" "github.com/snyk/cli/cliv2/internal/embedded" "github.com/snyk/cli/cliv2/internal/embedded/cliv1" "github.com/snyk/cli/cliv2/internal/proxy" local_utils "github.com/snyk/cli/cliv2/internal/utils" - "github.com/snyk/go-application-framework/pkg/utils" ) type Handler int @@ -243,7 +244,8 @@ func determineHandler(passthroughArgs []string) Handler { utils.Contains(passthroughArgs, "-v") || utils.Contains(passthroughArgs, "version") { result = V2_VERSION - } else if utils.Contains(passthroughArgs, "--about") { + } else if utils.Contains(passthroughArgs, "--about") || + utils.Contains(passthroughArgs, "about") { result = V2_ABOUT } @@ -355,6 +357,7 @@ func (c *CLI) executeV1Default(proxyInfo *proxy.ProxyInfo, passThroughArgs []str constants.SNYK_HTTPS_PROXY_ENV_SYSTEM, constants.SNYK_HTTP_PROXY_ENV_SYSTEM, constants.SNYK_HTTP_NO_PROXY_ENV_SYSTEM, + constants.SNYK_ANALYTICS_DISABLED_ENV, } for _, key := range listedEnvironmentVariables { diff --git a/cliv2/internal/constants/constants.go b/cliv2/internal/constants/constants.go index 85d2e33a85..80b60f62d4 100644 --- a/cliv2/internal/constants/constants.go +++ b/cliv2/internal/constants/constants.go @@ -16,6 +16,7 @@ const SNYK_NPM_ALL_PROXY = "ALL_PROXY" const SNYK_CA_CERTIFICATE_LOCATION_ENV = "NODE_EXTRA_CA_CERTS" const SNYK_INTERNAL_NO_PROXY = "localhost,127.0.0.1,::1" const SNYK_OAUTH_ACCESS_TOKEN_ENV = "SNYK_OAUTH_TOKEN" +const SNYK_ANALYTICS_DISABLED_ENV = "SNYK_DISABLE_ANALYTICS" const SNYK_HTTPS_PROXY_ENV_SYSTEM = "SNYK_SYSTEM_HTTPS_PROXY" const SNYK_HTTP_PROXY_ENV_SYSTEM = "SNYK_SYSTEM_HTTP_PROXY" diff --git a/cliv2/pkg/basic_workflows/legacycli.go b/cliv2/pkg/basic_workflows/legacycli.go index c8d31135c8..f2ba61e09b 100644 --- a/cliv2/pkg/basic_workflows/legacycli.go +++ b/cliv2/pkg/basic_workflows/legacycli.go @@ -3,18 +3,20 @@ package basic_workflows import ( "bufio" "bytes" + "net/http" + "os" + "github.com/pkg/errors" - "github.com/snyk/cli/cliv2/internal/cliv2" - "github.com/snyk/cli/cliv2/internal/constants" - "github.com/snyk/cli/cliv2/internal/proxy" "github.com/snyk/go-application-framework/pkg/auth" "github.com/snyk/go-application-framework/pkg/configuration" pkg_utils "github.com/snyk/go-application-framework/pkg/utils" "github.com/snyk/go-application-framework/pkg/workflow" "github.com/snyk/go-httpauth/pkg/httpauth" "github.com/spf13/pflag" - "net/http" - "os" + + "github.com/snyk/cli/cliv2/internal/cliv2" + "github.com/snyk/cli/cliv2/internal/constants" + "github.com/snyk/cli/cliv2/internal/proxy" ) var WORKFLOWID_LEGACY_CLI workflow.Identifier = workflow.NewWorkflowIdentifier("legacycli") @@ -69,6 +71,7 @@ func legacycliWorkflow( insecure := config.GetBool(configuration.INSECURE_HTTPS) proxyAuthenticationMechanismString := config.GetString(configuration.PROXY_AUTHENTICATION_MECHANISM) proxyAuthenticationMechanism := httpauth.AuthenticationMechanismFromString(proxyAuthenticationMechanismString) + analyticsDisabled := config.GetBool(configuration.ANALYTICS_DISABLED) debugLogger.Println("Arguments:", args) debugLogger.Println("Use StdIO:", useStdIo) @@ -83,6 +86,12 @@ func legacycliWorkflow( cli.WorkingDirectory = workingDirectory + // ensure to disable analytics based on configuration + if _, exists := os.LookupEnv(constants.SNYK_ANALYTICS_DISABLED_ENV); !exists && analyticsDisabled { + env := []string{constants.SNYK_ANALYTICS_DISABLED_ENV + "=1"} + cli.AppendEnvironmentVariables(env) + } + if oauthIsAvailable { // The Legacy CLI doesn't support oauth authentication. Oauth authentication is implemented in the Extensible CLI and is added // to the legacy CLI by forwarding network traffic through the internal proxy of the Extensible CLI. diff --git a/src/cli/commands/woof/getWoof.ts b/src/cli/commands/woof/getWoof.ts index 41b13cfc66..24350edce7 100644 --- a/src/cli/commands/woof/getWoof.ts +++ b/src/cli/commands/woof/getWoof.ts @@ -26,6 +26,11 @@ export default function getWoof(args: MethodArgs): string { for (const option in options) { console.debug(`${option}:::`, options[option], `:::${option}`); } + + const envVal = options.env as string; + if (envVal) { + console.debug(envVal + '=' + process.env[envVal]); + } } return woofs[lang]; diff --git a/test/jest/acceptance/analytics.spec.ts b/test/jest/acceptance/analytics.spec.ts index e4118c3802..36e037641d 100644 --- a/test/jest/acceptance/analytics.spec.ts +++ b/test/jest/acceptance/analytics.spec.ts @@ -471,6 +471,7 @@ describe('analytics module', () => { const lastRequest = requests.pop(); expect(lastRequest).toBeUndefined(); }); + it("won't send analytics if disable analytics is set via SNYK_CFG_DISABLE_ANALYTICS", async () => { const { code } = await runSnykCLI(`version`, { env: { @@ -530,4 +531,20 @@ describe('analytics module', () => { expect(requests.length).toBe(0); }); + + it('if analytics are disabled with --DISABLE_ANALYTICS, SNYK_DISABLE_ANALYTICS will be set to 1', async () => { + // Using woof --language=cat prints currently set environment variables. + // --env will print the environment variable's value. + const { code, stdout } = await runSnykCLI( + `woof --language=cat --env=SNYK_DISABLE_ANALYTICS --DISABLE_ANALYTICS`, + { + env: { + ...env, + }, + }, + ); + + expect(code).toBe(0); + expect(stdout).toContain('SNYK_DISABLE_ANALYTICS=1'); + }); });