Skip to content

Commit

Permalink
fix: set endpoint configuration for legacy CLI (#4931)
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterSchafer committed Nov 20, 2023
1 parent 088a3a8 commit 4eb072f
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 24 deletions.
1 change: 1 addition & 0 deletions cliv2/cmd/cliv2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func initApplicationConfiguration(config configuration.Configuration) {
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{strings.ToLower(constants.SNYK_ANALYTICS_DISABLED_ENV), "snyk_cfg_disable_analytics", "disable-analytics", "disable_analytics"})
config.AddAlternativeKeys(configuration.ORGANIZATION, []string{"snyk_cfg_org"})

// 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
8 changes: 4 additions & 4 deletions cliv2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ require (
github.com/snyk/cli-extension-iac-rules v0.0.0-20230601153200-c572cfce46ce
github.com/snyk/cli-extension-sbom v0.0.0-20230926124903-9705d7d47d8f
github.com/snyk/container-cli v0.0.0-20230920093251-fe865879a91f
github.com/snyk/go-application-framework v0.0.0-20231117124326-6dff117c23a7
github.com/snyk/go-httpauth v0.0.0-20230925093100-dfb05155efc1
github.com/snyk/go-application-framework v0.0.0-20231117172046-78850b04ac34
github.com/snyk/go-httpauth v0.0.0-20231117135515-eb445fea7530
github.com/snyk/snyk-iac-capture v0.6.5
github.com/snyk/snyk-ls v0.0.0-20231110143802-54d54e374783
github.com/snyk/snyk-ls v0.0.0-20231114150608-4bb0b45433fa
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.4
Expand Down Expand Up @@ -150,7 +150,7 @@ require (
go.opencensus.io v0.24.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.15.0 // indirect
golang.org/x/exp v0.0.0-20231108232855-2478ac86f678 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/oauth2 v0.14.0 // indirect
Expand Down
16 changes: 8 additions & 8 deletions cliv2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -665,16 +665,16 @@ github.com/snyk/cli-extension-sbom v0.0.0-20230926124903-9705d7d47d8f h1:U3DQ9wn
github.com/snyk/cli-extension-sbom v0.0.0-20230926124903-9705d7d47d8f/go.mod h1:O/cjwCbKhJQWyXHPmNbZ7ToQKnhyw0VUp1Qhim3WEcw=
github.com/snyk/container-cli v0.0.0-20230920093251-fe865879a91f h1:ghajT5PEiLP8XNFIdc7Yn4Th74RH/9Q++dDOp6Cb9eo=
github.com/snyk/container-cli v0.0.0-20230920093251-fe865879a91f/go.mod h1:38w+dcAQp9eG3P5t2eNS9eG0reut10AeJjLv5lJ5lpM=
github.com/snyk/go-application-framework v0.0.0-20231117124326-6dff117c23a7 h1:6PQ+kR6tHAnw0b3yNi1YjYh3TCQoxZr8RezEsC8daAQ=
github.com/snyk/go-application-framework v0.0.0-20231117124326-6dff117c23a7/go.mod h1:YGuE2uaW3PG7Q3CcVpCdu/tOFhv5eUjcTNlYIcw6FDo=
github.com/snyk/go-httpauth v0.0.0-20230925093100-dfb05155efc1 h1:2HfjHQxOjWyD5jKJQtiZV9mptamqikAvE/H4gilFk30=
github.com/snyk/go-httpauth v0.0.0-20230925093100-dfb05155efc1/go.mod h1:88KbbvGYlmLgee4OcQ19yr0bNpXpOr2kciOthaSzCAg=
github.com/snyk/go-application-framework v0.0.0-20231117172046-78850b04ac34 h1:HqIcp46MUaRNANt3x/KKZEOoNlPFigqBThcZWJsMMKQ=
github.com/snyk/go-application-framework v0.0.0-20231117172046-78850b04ac34/go.mod h1:Yz/qxFyfhf0xbA+z8Vzr5IM9IDG+BS+2PiGaP1yAsEw=
github.com/snyk/go-httpauth v0.0.0-20231117135515-eb445fea7530 h1:s9PHNkL6ueYRiAKNfd8OVxlUOqU3qY0VDbgCD1f6WQY=
github.com/snyk/go-httpauth v0.0.0-20231117135515-eb445fea7530/go.mod h1:88KbbvGYlmLgee4OcQ19yr0bNpXpOr2kciOthaSzCAg=
github.com/snyk/policy-engine v0.22.0 h1:od9pduGrXyfWO791X+8M1qmnvWUxaIXh0gBzGKqeseA=
github.com/snyk/policy-engine v0.22.0/go.mod h1:Vvy/9VMXoABS3JlLqhTlAPWkB5LgbLh7LGn3gBwAqdY=
github.com/snyk/snyk-iac-capture v0.6.5 h1:992DXCAJSN97KtUh8T5ndaWwd/6ZCal2bDkRXqM1u/E=
github.com/snyk/snyk-iac-capture v0.6.5/go.mod h1:e47i55EmM0F69ZxyFHC4sCi7vyaJW6DLoaamJJCzWGk=
github.com/snyk/snyk-ls v0.0.0-20231110143802-54d54e374783 h1:7c+P+6DEPbTF6uzMJh5/KeL7gTcj2jiiCXhOtnVNDd8=
github.com/snyk/snyk-ls v0.0.0-20231110143802-54d54e374783/go.mod h1:WhdYTF3tH+liSQqBevmBPJZl3fhaq60lVKLvSje3MrU=
github.com/snyk/snyk-ls v0.0.0-20231114150608-4bb0b45433fa h1:uNJS0GSWbb4fQ23FWWM0+iUynOcSsiIK9lkI6zA/qNc=
github.com/snyk/snyk-ls v0.0.0-20231114150608-4bb0b45433fa/go.mod h1:T0TASo8TV2XQmZZ0a4eOlgYsy6Oxoqo7xkWns/0gC+w=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/sourcegraph/go-lsp v0.0.0-20200429204803-219e11d77f5d h1:afLbh+ltiygTOB37ymZVwKlJwWZn+86syPTbrrOAydY=
Expand Down Expand Up @@ -779,8 +779,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20231108232855-2478ac86f678 h1:mchzmB1XO2pMaKFRqk/+MV3mgGG96aqaPXaMifQU47w=
golang.org/x/exp v0.0.0-20231108232855-2478ac86f678/go.mod h1:zk2irFbV9DP96SEBUUAy67IdHUaZuSnrz1n472HUCLE=
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa h1:FRnLl4eNAQl8hwxVVC17teOw8kdjVDVAiFMtgUdTSRQ=
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa/go.mod h1:zk2irFbV9DP96SEBUUAy67IdHUaZuSnrz1n472HUCLE=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down
25 changes: 20 additions & 5 deletions cliv2/internal/cliv2/cliv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ func PrepareV1EnvironmentVariables(
integrationVersion string,
proxyAddress string,
caCertificateLocation string,
orgid string,
config configuration.Configuration,
args []string,
) (result []string, err error) {

inputAsMap := utils.ToKeyValueMap(input, "=")
Expand Down Expand Up @@ -309,7 +310,18 @@ func PrepareV1EnvironmentVariables(
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_INTERNAL_ORGID_ENV] = orgid
inputAsMap[constants.SNYK_INTERNAL_ORGID_ENV] = config.GetString(configuration.ORGANIZATION)

if config.IsSet(configuration.API_URL) {
inputAsMap[constants.SNYK_ENDPOINT_ENV] = config.GetString(configuration.API_URL)
}

_, orgEnVarExists := inputAsMap[constants.SNYK_ORG_ENV]
if !utils.ContainsPrefix(args, "--org=") &&
!orgEnVarExists &&
config.IsSet(configuration.ORGANIZATION) {
inputAsMap[constants.SNYK_ORG_ENV] = config.GetString(configuration.ORGANIZATION)
}

// merge user defined (external) and internal no_proxy configuration
if len(inputAsMap[constants.SNYK_HTTP_NO_PROXY_ENV_SYSTEM]) > 0 {
Expand All @@ -336,10 +348,9 @@ func (c *CLI) PrepareV1Command(
integrationVersion string,
) (snykCmd *exec.Cmd, err error) {
proxyAddress := fmt.Sprintf("http://%s:%s@127.0.0.1:%d", proxy.PROXY_USERNAME, proxyInfo.Password, proxyInfo.Port)
orgid := c.globalConfig.GetString(configuration.ORGANIZATION)

snykCmd = exec.Command(cmd, args...)
snykCmd.Env, err = PrepareV1EnvironmentVariables(c.env, integrationName, integrationVersion, proxyAddress, proxyInfo.CertificateLocation, orgid)
snykCmd.Env, err = PrepareV1EnvironmentVariables(c.env, integrationName, integrationVersion, proxyAddress, proxyInfo.CertificateLocation, c.globalConfig, args)

if len(c.WorkingDirectory) > 0 {
snykCmd.Dir = c.WorkingDirectory
Expand Down Expand Up @@ -369,10 +380,14 @@ func (c *CLI) executeV1Default(proxyInfo *proxy.ProxyInfo, passThroughArgs []str
constants.SNYK_HTTP_PROXY_ENV_SYSTEM,
constants.SNYK_HTTP_NO_PROXY_ENV_SYSTEM,
constants.SNYK_ANALYTICS_DISABLED_ENV,
constants.SNYK_ENDPOINT_ENV,
constants.SNYK_ORG_ENV,
}

for _, key := range listedEnvironmentVariables {
c.DebugLogger.Println(" ", key, "=", variablesMap[key])
if value, exists := variablesMap[key]; exists {
c.DebugLogger.Println(" ", key, "=", value)
}
}

}
Expand Down
133 changes: 126 additions & 7 deletions cliv2/internal/cliv2/cliv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ func getCacheDir(t *testing.T) string {

func Test_PrepareV1EnvironmentVariables_Fill_and_Filter(t *testing.T) {

orgid := "orgid"
testapi := "https://api.snyky.io"

config := configuration.NewInMemory()
config.Set(configuration.ORGANIZATION, orgid)
config.Set(configuration.API_URL, testapi)

input := []string{
"something=1",
"in=2",
Expand All @@ -56,11 +63,13 @@ func Test_PrepareV1EnvironmentVariables_Fill_and_Filter(t *testing.T) {
"SNYK_SYSTEM_NO_PROXY=noProxy",
"SNYK_SYSTEM_HTTP_PROXY=httpProxy",
"SNYK_SYSTEM_HTTPS_PROXY=httpsProxy",
"SNYK_INTERNAL_ORGID=orgid",
"SNYK_INTERNAL_ORGID=" + orgid,
"SNYK_CFG_ORG=" + orgid,
"SNYK_API=" + testapi,
"NO_PROXY=" + constants.SNYK_INTERNAL_NO_PROXY + ",noProxy",
}

actual, err := cliv2.PrepareV1EnvironmentVariables(input, "foo", "bar", "proxy", "cacertlocation", "orgid")
actual, err := cliv2.PrepareV1EnvironmentVariables(input, "foo", "bar", "proxy", "cacertlocation", config, []string{})

sort.Strings(expected)
sort.Strings(actual)
Expand All @@ -70,6 +79,13 @@ func Test_PrepareV1EnvironmentVariables_Fill_and_Filter(t *testing.T) {

func Test_PrepareV1EnvironmentVariables_DontOverrideExistingIntegration(t *testing.T) {

orgid := "orgid"
testapi := "https://api.snyky.io"

config := configuration.NewInMemory()
config.Set(configuration.ORGANIZATION, orgid)
config.Set(configuration.API_URL, testapi)

input := []string{"something=1", "in=2", "here=3", "SNYK_INTEGRATION_NAME=exists", "SNYK_INTEGRATION_VERSION=already"}
expected := []string{
"something=1",
Expand All @@ -83,11 +99,13 @@ func Test_PrepareV1EnvironmentVariables_DontOverrideExistingIntegration(t *testi
"SNYK_SYSTEM_NO_PROXY=",
"SNYK_SYSTEM_HTTP_PROXY=",
"SNYK_SYSTEM_HTTPS_PROXY=",
"SNYK_INTERNAL_ORGID=orgid",
"SNYK_INTERNAL_ORGID=" + orgid,
"SNYK_CFG_ORG=" + orgid,
"SNYK_API=" + testapi,
"NO_PROXY=" + constants.SNYK_INTERNAL_NO_PROXY,
}

actual, err := cliv2.PrepareV1EnvironmentVariables(input, "foo", "bar", "proxy", "cacertlocation", "orgid")
actual, err := cliv2.PrepareV1EnvironmentVariables(input, "foo", "bar", "proxy", "cacertlocation", config, []string{})

sort.Strings(expected)
sort.Strings(actual)
Expand All @@ -97,6 +115,13 @@ func Test_PrepareV1EnvironmentVariables_DontOverrideExistingIntegration(t *testi

func Test_PrepareV1EnvironmentVariables_OverrideProxyAndCerts(t *testing.T) {

orgid := "orgid"
testapi := "https://api.snyky.io"

config := configuration.NewInMemory()
config.Set(configuration.ORGANIZATION, orgid)
config.Set(configuration.API_URL, testapi)

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",
Expand All @@ -110,24 +135,73 @@ func Test_PrepareV1EnvironmentVariables_OverrideProxyAndCerts(t *testing.T) {
"SNYK_SYSTEM_NO_PROXY=312123",
"SNYK_SYSTEM_HTTP_PROXY=exists",
"SNYK_SYSTEM_HTTPS_PROXY=already",
"SNYK_INTERNAL_ORGID=orgid",
"SNYK_INTERNAL_ORGID=" + orgid,
"SNYK_CFG_ORG=" + orgid,
"SNYK_API=" + testapi,
"NO_PROXY=" + constants.SNYK_INTERNAL_NO_PROXY + ",312123",
}

actual, err := cliv2.PrepareV1EnvironmentVariables(input, "foo", "bar", "proxy", "cacertlocation", "orgid")
actual, err := cliv2.PrepareV1EnvironmentVariables(input, "foo", "bar", "proxy", "cacertlocation", config, []string{})

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

func Test_PrepareV1EnvironmentVariables_OnlyExplicitlySetValues(t *testing.T) {

config := configuration.NewInMemory()

t.Run("Values not set", func(t *testing.T) {
input := []string{}
notExpected := []string{"SNYK_API=", "SNYK_CFG_ORG="}

actual, err := cliv2.PrepareV1EnvironmentVariables(input, "foo", "bar", "proxy", "cacertlocation", config, []string{})

assert.NotContains(t, actual, notExpected)
assert.Nil(t, err)
})

t.Run("Values explicitly set api", func(t *testing.T) {
input := []string{}
expected := []string{"SNYK_API=https://api.snyky.io"}

config.Set(configuration.API_URL, "https://api.snyky.io")

actual, err := cliv2.PrepareV1EnvironmentVariables(input, "foo", "bar", "proxy", "cacertlocation", config, []string{})

assert.NotContains(t, actual, expected)
assert.Nil(t, err)
})

t.Run("Values explicitly set org", func(t *testing.T) {
input := []string{}
expected := []string{"SNYK_CFG_ORG=my-org"}

config.Set(configuration.ORGANIZATION, "my-org")

actual, err := cliv2.PrepareV1EnvironmentVariables(input, "foo", "bar", "proxy", "cacertlocation", config, []string{})

assert.NotContains(t, actual, expected)
assert.Nil(t, err)
})

}

func Test_PrepareV1EnvironmentVariables_Fail_DontOverrideExisting(t *testing.T) {

orgid := "orgid"
testapi := "https://api.snyky.io"

config := configuration.NewInMemory()
config.Set(configuration.ORGANIZATION, orgid)
config.Set(configuration.API_URL, testapi)

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

actual, err := cliv2.PrepareV1EnvironmentVariables(input, "foo", "bar", "unused", "unused", "orgid")
actual, err := cliv2.PrepareV1EnvironmentVariables(input, "foo", "bar", "unused", "unused", config, []string{})

sort.Strings(expected)
sort.Strings(actual)
Expand All @@ -138,6 +212,51 @@ func Test_PrepareV1EnvironmentVariables_Fail_DontOverrideExisting(t *testing.T)
assert.NotNil(t, warn)
}

func Test_PrepareV1EnvironmentVariables_Fail_DontOverrideExisting_Org(t *testing.T) {

orgid := "orgid"
testapi := "https://api.snyky.io"

config := configuration.NewInMemory()
config.Set(configuration.ORGANIZATION, orgid)
config.Set(configuration.API_URL, testapi)

notExpected := "SNYK_CFG_ORG=" + orgid

t.Run("config value is used", func(t *testing.T) {
input := []string{}
args := []string{"-d"}

actual, err := cliv2.PrepareV1EnvironmentVariables(input, "foo", "bar", "unused", "unused", config, args)
assert.Nil(t, err)

assert.Contains(t, actual, notExpected)
})

t.Run("cmd arg is given, config value not used", func(t *testing.T) {
input := []string{}
args := []string{"-d", "--org=something"}

actual, err := cliv2.PrepareV1EnvironmentVariables(input, "foo", "bar", "unused", "unused", config, args)
assert.Nil(t, err)

assert.NotContains(t, actual, notExpected)
})

t.Run("env var is given, config value not used", func(t *testing.T) {
expectedOrgEnvVar := "SNYK_CFG_ORG=myorg"
input := []string{"something=hello", expectedOrgEnvVar}
args := []string{"-d"}

actual, err := cliv2.PrepareV1EnvironmentVariables(input, "foo", "bar", "unused", "unused", config, args)
assert.Nil(t, err)

assert.NotContains(t, actual, notExpected)
assert.Contains(t, actual, expectedOrgEnvVar)
})

}

func getProxyInfoForTest() *proxy.ProxyInfo {
return &proxy.ProxyInfo{
Port: 1000,
Expand Down
2 changes: 2 additions & 0 deletions cliv2/internal/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ 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_INTERNAL_ORGID_ENV = "SNYK_INTERNAL_ORGID"
const SNYK_ENDPOINT_ENV = "SNYK_API"
const SNYK_ORG_ENV = "SNYK_CFG_ORG"
const SNYK_OPENSSL_CONF = "OPENSSL_CONF"

const SNYK_HTTPS_PROXY_ENV_SYSTEM = "SNYK_SYSTEM_HTTPS_PROXY"
Expand Down

0 comments on commit 4eb072f

Please sign in to comment.