Skip to content

Commit

Permalink
fix: consistent telemetry setting (#4825)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
PeterSchafer and Avishagp authored Sep 7, 2023
1 parent 59c5c55 commit 809ee62
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cliv2/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions cliv2/cmd/cliv2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/snyk/go-application-framework/pkg/networking/fips"
"io"
"net/http"
"os"
Expand All @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions cliv2/internal/cliv2/cliv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions cliv2/internal/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
19 changes: 14 additions & 5 deletions cliv2/pkg/basic_workflows/legacycli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions src/cli/commands/woof/getWoof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
17 changes: 17 additions & 0 deletions test/jest/acceptance/analytics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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');
});
});

0 comments on commit 809ee62

Please sign in to comment.