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: override env variables instead of appending #3323

Merged
merged 1 commit into from
Jun 20, 2022
Merged
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: 16 additions & 12 deletions cliv2/internal/cliv2/cliv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ const SNYK_EXIT_CODE_ERROR = 2
const SNYK_INTEGRATION_NAME = "CLI_V1_PLUGIN"
const SNYK_INTEGRATION_NAME_ENV = "SNYK_INTEGRATION_NAME"
const SNYK_INTEGRATION_VERSION_ENV = "SNYK_INTEGRATION_VERSION"
const SNYK_HTTPS_PROXY_ENV = "HTTPS_PROXY"
const SNYK_HTTP_PROXY_ENV = "HTTP_PROXY"
const SNYK_CA_CERTIFICATE_LOCATION_ENV = "NODE_EXTRA_CA_CERTS"

const (
V1_DEFAULT Handler = iota
Expand Down Expand Up @@ -137,7 +140,7 @@ func determineHandler(passthroughArgs []string) Handler {
return result
}

func AddIntegrationEnvironment(input []string, name string, version string) (result []string, err error) {
func PrepareV1EnvironmentVariables(input []string, integrationName string, integrationVersion string, proxyAddress string, caCertificateLocation string) (result []string, err error) {

inputAsMap := utils.ToKeyValueMap(input, "=")
result = input
Expand All @@ -146,28 +149,29 @@ func AddIntegrationEnvironment(input []string, name string, version string) (res
_, integrationVersionExists := inputAsMap[SNYK_INTEGRATION_VERSION_ENV]

if !integrationNameExists && !integrationVersionExists {
inputAsMap[SNYK_INTEGRATION_NAME_ENV] = name
inputAsMap[SNYK_INTEGRATION_VERSION_ENV] = version
result = utils.ToSlice(inputAsMap, "=")
inputAsMap[SNYK_INTEGRATION_NAME_ENV] = integrationName
inputAsMap[SNYK_INTEGRATION_VERSION_ENV] = integrationVersion
} else if !(integrationNameExists && integrationVersionExists) {
err = EnvironmentWarning{message: fmt.Sprintf("Partially defined environment, please ensure to provide both %s and %s together!", SNYK_INTEGRATION_NAME_ENV, SNYK_INTEGRATION_VERSION_ENV)}
}

if err == nil {
inputAsMap[SNYK_HTTPS_PROXY_ENV] = proxyAddress
inputAsMap[SNYK_HTTP_PROXY_ENV] = proxyAddress
inputAsMap[SNYK_CA_CERTIFICATE_LOCATION_ENV] = caCertificateLocation
result = utils.ToSlice(inputAsMap, "=")
Avishagp marked this conversation as resolved.
Show resolved Hide resolved
}

return result, err

}

func PrepareV1Command(cmd string, args []string, proxyPort int, caCertLocation string, integrationName string, integrationVersion string) (snykCmd *exec.Cmd, err error) {

snykCmd = exec.Command(cmd, args...)
snykCmd.Env = append(os.Environ(),
fmt.Sprintf("HTTP_PROXY=http://127.0.0.1:%d", proxyPort),
fmt.Sprintf("HTTPS_PROXY=http://127.0.0.1:%d", proxyPort),
fmt.Sprintf("NODE_EXTRA_CA_CERTS=%s", caCertLocation),
)

snykCmd.Env, err = AddIntegrationEnvironment(snykCmd.Env, integrationName, integrationVersion)
proxyAddress := fmt.Sprintf("http://127.0.0.1:%d", proxyPort)

snykCmd = exec.Command(cmd, args...)
snykCmd.Env, err = PrepareV1EnvironmentVariables(os.Environ(), integrationName, integrationVersion, proxyAddress, caCertLocation)
snykCmd.Stdin = os.Stdin
snykCmd.Stdout = os.Stdout
snykCmd.Stderr = os.Stderr
Expand Down
19 changes: 10 additions & 9 deletions cliv2/internal/cliv2/cliv2_test.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,49 @@
package cliv2_test

import (
"github.com/snyk/cli/cliv2/internal/cliv2"
"io/ioutil"
"log"
"os"
"sort"
"testing"

"github.com/snyk/cli/cliv2/internal/cliv2"

"github.com/stretchr/testify/assert"
)

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

input := []string{"something=1", "in=2", "here=3=2"}
expected := []string{"something=1", "in=2", "here=3=2", "SNYK_INTEGRATION_NAME=foo", "SNYK_INTEGRATION_VERSION=bar"}
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"}

actual, err := cliv2.AddIntegrationEnvironment(input, "foo", "bar")
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_addIntegrationEnvironment_DontOverrideExisting(t *testing.T) {
func Test_PrepareV1EnvironmentVariables_DontOverrideExisting(t *testing.T) {

input := []string{"something=1", "in=2", "here=3", "SNYK_INTEGRATION_NAME=exists", "SNYK_INTEGRATION_VERSION=already"}
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

actual, err := cliv2.AddIntegrationEnvironment(input, "foo", "bar")
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_addIntegrationEnvironment_DontOverrideExisting2(t *testing.T) {
func Test_PrepareV1EnvironmentVariables_DontOverrideExisting2(t *testing.T) {

input := []string{"something=1", "in=2", "here=3", "SNYK_INTEGRATION_NAME=exists"}
expected := input

actual, err := cliv2.AddIntegrationEnvironment(input, "foo", "bar")
actual, err := cliv2.PrepareV1EnvironmentVariables(input, "foo", "bar", "unused", "unused")

sort.Strings(expected)
sort.Strings(actual)
Expand Down