From 6d151639476caaafb2c2658bdd260d7bd561e4c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Scha=CC=88fer?= <101886095+PeterSchafer@users.noreply.github.com> Date: Tue, 27 Dec 2022 19:36:06 +0100 Subject: [PATCH] fix: merge internal and external defined no_proxy * improve logging * cleanup unused utils * update application framework --- cliv2/go.mod | 2 +- cliv2/go.sum | 4 +- cliv2/internal/cliv2/cliv2.go | 42 ++++++++++-- cliv2/internal/cliv2/cliv2_test.go | 4 +- cliv2/internal/utils/api_tokens.go | 22 ------ cliv2/internal/utils/array.go | 93 -------------------------- cliv2/pkg/basic_workflows/legacycli.go | 3 +- 7 files changed, 43 insertions(+), 127 deletions(-) delete mode 100644 cliv2/internal/utils/api_tokens.go delete mode 100644 cliv2/internal/utils/array.go diff --git a/cliv2/go.mod b/cliv2/go.mod index bca9be2876f..1c39625ed08 100644 --- a/cliv2/go.mod +++ b/cliv2/go.mod @@ -8,7 +8,7 @@ require ( github.com/google/uuid v1.3.0 github.com/pkg/errors v0.9.1 github.com/snyk/cli-extension-sbom v0.0.0-20221212093410-6b474ed1a42a - github.com/snyk/go-application-framework v0.0.0-20221215182111-b2d10cf1e146 + github.com/snyk/go-application-framework v0.0.0-20221228152100-7ba9ce084c6e github.com/snyk/go-httpauth v0.0.0-20220915135832-0edf62cf8cdd github.com/spf13/cobra v1.6.0 github.com/spf13/pflag v1.0.5 diff --git a/cliv2/go.sum b/cliv2/go.sum index a8e7fb41e86..4e9bae67c9d 100644 --- a/cliv2/go.sum +++ b/cliv2/go.sum @@ -184,8 +184,8 @@ github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZV github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/snyk/cli-extension-sbom v0.0.0-20221212093410-6b474ed1a42a h1:kImXWA4kbwaREeC+kaJ8H0aOukWzpK8K/UzAsExj6MU= github.com/snyk/cli-extension-sbom v0.0.0-20221212093410-6b474ed1a42a/go.mod h1:ohrrgC94Gx82/cgSiac02JQrsMjFtggvhAvXGuGjDGU= -github.com/snyk/go-application-framework v0.0.0-20221215182111-b2d10cf1e146 h1:V5kc8tSGVhyiPNuEXkZ9CVmwWiYlMmaQGpjRbORuqlU= -github.com/snyk/go-application-framework v0.0.0-20221215182111-b2d10cf1e146/go.mod h1:5hLGqObbxLWnZkhn3Xc5PblESjQOfjN509ucQ4dtqz8= +github.com/snyk/go-application-framework v0.0.0-20221228152100-7ba9ce084c6e h1:zvAdv3DF71fhL6qIVH2nAPWpeV3T0VADWN87DzWDnX8= +github.com/snyk/go-application-framework v0.0.0-20221228152100-7ba9ce084c6e/go.mod h1:5hLGqObbxLWnZkhn3Xc5PblESjQOfjN509ucQ4dtqz8= github.com/snyk/go-httpauth v0.0.0-20220915135832-0edf62cf8cdd h1:zjDhcQ642rIVI8aIjfG5uVcw+OGotQtX2l9VHe7IqCQ= github.com/snyk/go-httpauth v0.0.0-20220915135832-0edf62cf8cdd/go.mod h1:v6t6wKizOcHXT3p4qKn6Bda7yNIjCQ54Xyl31NjgXkY= github.com/spf13/afero v1.9.2 h1:j49Hj62F0n+DaZ1dDCvhABaPNSGNkt32oRFxI33IEMw= diff --git a/cliv2/internal/cliv2/cliv2.go b/cliv2/internal/cliv2/cliv2.go index 347f5ddef36..37990b10c17 100644 --- a/cliv2/internal/cliv2/cliv2.go +++ b/cliv2/internal/cliv2/cliv2.go @@ -11,13 +11,14 @@ import ( "os" "os/exec" "path" + "regexp" "strings" "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" - "github.com/snyk/cli/cliv2/internal/utils" + "github.com/snyk/go-application-framework/pkg/utils" ) type Handler int @@ -249,7 +250,16 @@ func PrepareV1EnvironmentVariables(input []string, integrationName string, integ inputAsMap[constants.SNYK_HTTPS_PROXY_ENV] = proxyAddress inputAsMap[constants.SNYK_HTTP_PROXY_ENV] = proxyAddress inputAsMap[constants.SNYK_CA_CERTIFICATE_LOCATION_ENV] = caCertificateLocation - inputAsMap[constants.SNYK_HTTP_NO_PROXY_ENV] = constants.SNYK_INTERNAL_NO_PROXY + + // merge user defined (external) and internal no_proxy configuration + if len(inputAsMap[constants.SNYK_HTTP_NO_PROXY_ENV_SYSTEM]) > 0 { + internalNoProxy := strings.Split(constants.SNYK_INTERNAL_NO_PROXY, ",") + externalNoProxy := regexp.MustCompile("[,;]").Split(inputAsMap[constants.SNYK_HTTP_NO_PROXY_ENV_SYSTEM], -1) + mergedNoProxy := utils.Merge(internalNoProxy, externalNoProxy) + inputAsMap[constants.SNYK_HTTP_NO_PROXY_ENV] = strings.Join(mergedNoProxy, ",") + } else { + inputAsMap[constants.SNYK_HTTP_NO_PROXY_ENV] = constants.SNYK_INTERNAL_NO_PROXY + } result = utils.ToSlice(inputAsMap, "=") } @@ -268,8 +278,6 @@ func PrepareV1Command(cmd string, args []string, proxyInfo *proxy.ProxyInfo, int } func (c *CLI) executeV1Default(proxyInfo *proxy.ProxyInfo, passthroughArgs []string) error { - c.DebugLogger.Println("launching snyk with path: ", c.v1BinaryLocation) - c.DebugLogger.Println("CertificateLocation:", proxyInfo.CertificateLocation) snykCmd, err := PrepareV1Command( c.v1BinaryLocation, @@ -279,6 +287,30 @@ func (c *CLI) executeV1Default(proxyInfo *proxy.ProxyInfo, passthroughArgs []str GetFullVersion(), ) + if c.DebugLogger.Writer() != io.Discard { + c.DebugLogger.Println("Launching: ") + c.DebugLogger.Println(" ", c.v1BinaryLocation) + c.DebugLogger.Println(" With Arguments:") + c.DebugLogger.Println(" ", strings.Join(passthroughArgs, ", ")) + c.DebugLogger.Println(" With Environment: ") + + variablesMap := utils.ToKeyValueMap(snykCmd.Env, "=") + listedEnvironmentVariables := []string{ + constants.SNYK_CA_CERTIFICATE_LOCATION_ENV, + constants.SNYK_HTTPS_PROXY_ENV, + constants.SNYK_HTTP_PROXY_ENV, + constants.SNYK_HTTP_NO_PROXY_ENV, + constants.SNYK_HTTPS_PROXY_ENV_SYSTEM, + constants.SNYK_HTTP_PROXY_ENV_SYSTEM, + constants.SNYK_HTTP_NO_PROXY_ENV_SYSTEM, + } + + for _, key := range listedEnvironmentVariables { + c.DebugLogger.Println(" ", key, "=", variablesMap[key]) + } + + } + snykCmd.Stdin = c.stdin snykCmd.Stdout = c.stdout snykCmd.Stderr = c.stderr @@ -295,8 +327,6 @@ func (c *CLI) executeV1Default(proxyInfo *proxy.ProxyInfo, passthroughArgs []str } func (c *CLI) Execute(proxyInfo *proxy.ProxyInfo, passthroughArgs []string) error { - c.DebugLogger.Println("passthroughArgs", passthroughArgs) - var err error handler := determineHandler(passthroughArgs) diff --git a/cliv2/internal/cliv2/cliv2_test.go b/cliv2/internal/cliv2/cliv2_test.go index 4b574c5d916..7ec89d27bab 100644 --- a/cliv2/internal/cliv2/cliv2_test.go +++ b/cliv2/internal/cliv2/cliv2_test.go @@ -41,7 +41,7 @@ func Test_PrepareV1EnvironmentVariables_Fill_and_Filter(t *testing.T) { "SNYK_SYSTEM_NO_PROXY=noProxy", "SNYK_SYSTEM_HTTP_PROXY=httpProxy", "SNYK_SYSTEM_HTTPS_PROXY=httpsProxy", - "NO_PROXY=" + constants.SNYK_INTERNAL_NO_PROXY, + "NO_PROXY=" + constants.SNYK_INTERNAL_NO_PROXY + ",noProxy", } actual, err := cliv2.PrepareV1EnvironmentVariables(input, "foo", "bar", "proxy", "cacertlocation") @@ -93,7 +93,7 @@ func Test_PrepareV1EnvironmentVariables_OverrideProxyAndCerts(t *testing.T) { "SNYK_SYSTEM_NO_PROXY=312123", "SNYK_SYSTEM_HTTP_PROXY=exists", "SNYK_SYSTEM_HTTPS_PROXY=already", - "NO_PROXY=" + constants.SNYK_INTERNAL_NO_PROXY, + "NO_PROXY=" + constants.SNYK_INTERNAL_NO_PROXY + ",312123", } actual, err := cliv2.PrepareV1EnvironmentVariables(input, "foo", "bar", "proxy", "cacertlocation") diff --git a/cliv2/internal/utils/api_tokens.go b/cliv2/internal/utils/api_tokens.go deleted file mode 100644 index fa62b9f8b31..00000000000 --- a/cliv2/internal/utils/api_tokens.go +++ /dev/null @@ -1,22 +0,0 @@ -package utils - -import ( - "fmt" - - "github.com/snyk/go-application-framework/pkg/configuration" -) - -func GetAuthHeader(config configuration.Configuration) string { - - bearerToken := config.GetString(configuration.AUTHENTICATION_BEARER_TOKEN) - if len(bearerToken) > 0 { - return fmt.Sprintf("Bearer %s", bearerToken) - } - - token := config.GetString(configuration.AUTHENTICATION_TOKEN) - if len(token) > 0 { - return fmt.Sprintf("token %s", token) - } - - return "" -} diff --git a/cliv2/internal/utils/array.go b/cliv2/internal/utils/array.go deleted file mode 100644 index e365b2fb187..00000000000 --- a/cliv2/internal/utils/array.go +++ /dev/null @@ -1,93 +0,0 @@ -package utils - -import ( - "fmt" - "strings" -) - -func Contains(list []string, element string) bool { - for _, a := range list { - if a == element { - return true - } - } - return false -} - -func RemoveSimilar(list []string, element string) []string { - filteredArgs := []string{} - - for _, a := range list { - if !strings.Contains(a, element) { - filteredArgs = append(filteredArgs, a) - } - } - - return filteredArgs -} - -func ToKeyValueMap(input []string, splitBy string) map[string]string { - result := make(map[string]string) - - for _, a := range input { - splittedString := strings.SplitN(a, splitBy, 2) - if len(splittedString) == 2 { - key := splittedString[0] - value := splittedString[1] - result[key] = value - } - } - - return result -} - -func ToSlice(input map[string]string, combineBy string) []string { - result := []string{} - - for key, value := range input { - entry := fmt.Sprintf("%s%s%s", key, combineBy, value) - result = append(result, entry) - } - - return result -} - -// Removes a given key from the input map and uses FindKeyCaseInsensitive() for this. The resulting map is being returned. -func Remove(input map[string]string, key string) map[string]string { - found := false - key, found = FindKeyCaseInsensitive(input, key) - if found { - delete(input, key) - } - return input -} - -// This method determines whether the given key is in the input map, it therefore looks for the exact match and the key in all capital or lower case letters. -// If the key in any of these versions was found, it'll be returned alongside with a boolean indicating whether or not it was found. -func FindKeyCaseInsensitive(input map[string]string, key string) (string, bool) { - - found := false - - // look for exact match - _, found = input[key] - - // look for lower case match - if !found { - key = strings.ToLower(key) - _, found = input[key] - } - - // look for upper case match - if !found { - key = strings.ToUpper(key) - _, found = input[key] - } - - return key, found -} - -func FindValueCaseInsensitive(input map[string]string, key string) (string, bool) { - key, found := FindKeyCaseInsensitive(input, key) - value := input[key] - return value, found -} diff --git a/cliv2/pkg/basic_workflows/legacycli.go b/cliv2/pkg/basic_workflows/legacycli.go index c4b71d84ed0..333e75f43b3 100644 --- a/cliv2/pkg/basic_workflows/legacycli.go +++ b/cliv2/pkg/basic_workflows/legacycli.go @@ -10,6 +10,7 @@ import ( "github.com/snyk/cli/cliv2/internal/proxy" "github.com/snyk/cli/cliv2/internal/utils" "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" @@ -38,7 +39,7 @@ func FilteredArgs(args []string) []string { elementsToFilter := []string{"--" + PROXY_NOAUTH} filteredArgs := args for _, element := range elementsToFilter { - filteredArgs = utils.RemoveSimilar(filteredArgs, element) + filteredArgs = pkg_utils.RemoveSimilar(filteredArgs, element) } return filteredArgs }