Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/cliv2 case insensitive env vars #3364

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions cliv2/internal/cliv2/cliv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,29 @@ func PrepareV1EnvironmentVariables(input []string, integrationName string, integ
}

if err == nil {

// apply blacklist: ensure that no existing no_proxy or other configuration causes redirecting internal communication that is meant to stay between cliv1 and cliv2
blackList := []string{
SNYK_HTTPS_PROXY_ENV,
SNYK_HTTP_PROXY_ENV,
SNYK_CA_CERTIFICATE_LOCATION_ENV,
SNYK_HTTP_NO_PROXY_ENV,
SNYK_NPM_NO_PROXY_ENV,
SNYK_NPM_HTTPS_PROXY_ENV,
SNYK_NPM_HTTP_PROXY_ENV,
SNYK_NPM_PROXY_ENV,
SNYK_NPM_ALL_PROXY,
}

for _, key := range blackList {
inputAsMap = utils.Remove(inputAsMap, key)
}

// fill expected values
inputAsMap[SNYK_HTTPS_PROXY_ENV] = proxyAddress
inputAsMap[SNYK_HTTP_PROXY_ENV] = proxyAddress
inputAsMap[SNYK_CA_CERTIFICATE_LOCATION_ENV] = caCertificateLocation

// ensure that no existing no_proxy or other configuration causes redirecting internal communication that is meant to stay between cliv1 and cliv2
inputAsMap[SNYK_HTTP_NO_PROXY_ENV] = ""
inputAsMap[SNYK_NPM_NO_PROXY_ENV] = ""
inputAsMap[SNYK_NPM_HTTPS_PROXY_ENV] = ""
inputAsMap[SNYK_NPM_HTTP_PROXY_ENV] = ""
inputAsMap[SNYK_NPM_PROXY_ENV] = ""
inputAsMap[SNYK_NPM_ALL_PROXY] = ""

inputAsMap = utils.RemoveEmptyValue(inputAsMap)
result = utils.ToSlice(inputAsMap, "=")
}

Expand Down
30 changes: 22 additions & 8 deletions cliv2/internal/cliv2/cliv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ import (
"github.com/stretchr/testify/assert"
)

func Test_PrepareV1EnvironmentVariables_Fill(t *testing.T) {
func Test_PrepareV1EnvironmentVariables_Fill_and_Filter(t *testing.T) {

input := []string{"something=1",
input := []string{
"something=1",
"in=2",
"here=3=2",
"NO_PROXY=something",
"no_proxy=something",
"NPM_CONFIG_PROXY=something",
"NPM_CONFIG_HTTPS_PROXY=something",
"NPM_CONFIG_HTTP_PROXY=something",
"NPM_CONFIG_NO_PROXY=something",
"npm_config_no_proxy=something",
"ALL_PROXY=something",
}
expected := []string{"something=1", "in=2", "here=3=2", "SNYK_INTEGRATION_NAME=foo", "SNYK_INTEGRATION_VERSION=bar", "HTTP_PROXY=proxy", "HTTPS_PROXY=proxy", "NODE_EXTRA_CA_CERTS=cacertlocation"}
Expand All @@ -34,10 +35,23 @@ func Test_PrepareV1EnvironmentVariables_Fill(t *testing.T) {
assert.Nil(t, err)
}

func Test_PrepareV1EnvironmentVariables_DontOverrideExisting(t *testing.T) {
func Test_PrepareV1EnvironmentVariables_DontOverrideExistingIntegration(t *testing.T) {

input := []string{"something=1", "in=2", "here=3", "SNYK_INTEGRATION_NAME=exists", "SNYK_INTEGRATION_VERSION=already", "HTTP_PROXY=proxy", "HTTPS_PROXY=proxy", "NODE_EXTRA_CA_CERTS=cacertlocation"}
expected := input
input := []string{"something=1", "in=2", "here=3", "SNYK_INTEGRATION_NAME=exists", "SNYK_INTEGRATION_VERSION=already"}
expected := []string{"something=1", "in=2", "here=3", "SNYK_INTEGRATION_NAME=exists", "SNYK_INTEGRATION_VERSION=already", "HTTP_PROXY=proxy", "HTTPS_PROXY=proxy", "NODE_EXTRA_CA_CERTS=cacertlocation"}

actual, err := cliv2.PrepareV1EnvironmentVariables(input, "foo", "bar", "proxy", "cacertlocation")

sort.Strings(expected)
sort.Strings(actual)
assert.Equal(t, expected, actual)
assert.Nil(t, err)
}

func Test_PrepareV1EnvironmentVariables_OverrideProxyAndCerts(t *testing.T) {

input := []string{"something=1", "in=2", "here=3", "http_proxy=exists", "https_proxy=already", "NODE_EXTRA_CA_CERTS=again", "no_proxy=312123"}
expected := []string{"something=1", "in=2", "here=3", "SNYK_INTEGRATION_NAME=foo", "SNYK_INTEGRATION_VERSION=bar", "HTTP_PROXY=proxy", "HTTPS_PROXY=proxy", "NODE_EXTRA_CA_CERTS=cacertlocation"}

actual, err := cliv2.PrepareV1EnvironmentVariables(input, "foo", "bar", "proxy", "cacertlocation")

Expand All @@ -47,7 +61,7 @@ func Test_PrepareV1EnvironmentVariables_DontOverrideExisting(t *testing.T) {
assert.Nil(t, err)
}

func Test_PrepareV1EnvironmentVariables_DontOverrideExisting2(t *testing.T) {
func Test_PrepareV1EnvironmentVariables_Fail_DontOverrideExisting(t *testing.T) {

input := []string{"something=1", "in=2", "here=3", "SNYK_INTEGRATION_NAME=exists"}
expected := input
Expand Down
36 changes: 29 additions & 7 deletions cliv2/internal/utils/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,36 @@ func ToSlice(input map[string]string, combineBy string) []string {
return result
}

func RemoveEmptyValue(input map[string]string) map[string]string {
result := make(map[string]string)
// 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
}

for key, value := range input {
if len(value) > 0 {
result[key] = value
}
// 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]
}

return result
// look for upper case match
if !found {
key = strings.ToUpper(key)
_, found = input[key]
}

return key, found
}