diff --git a/internal/install/command.go b/internal/install/command.go index d8dce82a3..b6048e44f 100644 --- a/internal/install/command.go +++ b/internal/install/command.go @@ -181,7 +181,7 @@ func checkNetwork() error { if err == nil { if IsProxyConfigured() { - if strings.Contains(strings.ToLower(proxyConfig.HTTPSProxy), "http") && !strings.Contains(strings.ToLower(proxyConfig.HTTPSProxy), "https") { + if ShouldWarnAboutProxy(*proxyConfig) { log.Warn("Please ensure the HTTPS_PROXY environment variable is set when using a proxy server.") log.Warn("New Relic CLI exclusively supports https proxy, not http for security reasons.") } @@ -190,8 +190,6 @@ func checkNetwork() error { if err != nil { if IsProxyConfigured() { - proxyConfig := httpproxy.FromEnvironment() - log.Warn("Proxy settings have been configured, but we are still unable to connect to the New Relic platform.") log.Warn("You may need to adjust your proxy environment variables or configure your proxy to allow the specified domain.") log.Warn("Current proxy config:") diff --git a/internal/install/command_test.go b/internal/install/command_test.go index a96f9d8a8..957bc24de 100644 --- a/internal/install/command_test.go +++ b/internal/install/command_test.go @@ -6,10 +6,10 @@ import ( "net/http" "net/http/httptest" "os" - "strings" "testing" "github.com/stretchr/testify/assert" + "golang.org/x/net/http/httpproxy" "github.com/newrelic/newrelic-cli/internal/install/types" "github.com/newrelic/newrelic-cli/internal/testcobra" @@ -97,28 +97,32 @@ func initSegmentMockServer() *httptest.Server { return server } -func TestProxyNetwork(t *testing.T) { - - proxyConfig := struct { - HTTPSProxy string - HTTPProxy string - }{ - HTTPSProxy: "http://localhost:3128", - HTTPProxy: "http://localhost:8080", - } - - // Validate HTTPSProxy - if strings.HasPrefix(proxyConfig.HTTPSProxy, "http://") { - t.Log("New Relic CLI exclusively supports https proxy, not http for security reasons.") - } else if strings.HasPrefix(proxyConfig.HTTPSProxy, "https://") { - // Do nothing - } else { - t.Log("Invalid proxy provided") - } - - // Validate HTTPProxy - if strings.HasPrefix(proxyConfig.HTTPProxy, "http://") { - t.Log("If you need to use a proxy, consider setting the HTTPS_PROXY environment variable, then try again. New Relic CLI exclusively supports https proxy.") +func TestShouldWarnAboutProxy(t *testing.T) { + warn := func() bool { + return ShouldWarnAboutProxy(*httpproxy.FromEnvironment()) } + t.Run("no warning when HTTPS_PROXY is set with http scheme", func(t *testing.T) { + t.Setenv("HTTPS_PROXY", "http://localhost:8080") + t.Setenv("HTTP_PROXY", "") + assert.False(t, warn()) + }) + + t.Run("no warning when HTTPS_PROXY is set with https scheme", func(t *testing.T) { + t.Setenv("HTTPS_PROXY", "https://localhost:8080") + t.Setenv("HTTP_PROXY", "") + assert.False(t, warn()) + }) + + t.Run("warns when HTTP_PROXY is set but HTTPS_PROXY is not", func(t *testing.T) { + t.Setenv("HTTP_PROXY", "http://localhost:8080") + t.Setenv("HTTPS_PROXY", "") + assert.True(t, warn()) + }) + + t.Run("no warning when no proxy is configured", func(t *testing.T) { + t.Setenv("HTTP_PROXY", "") + t.Setenv("HTTPS_PROXY", "") + assert.False(t, warn()) + }) } diff --git a/internal/install/proxy_configuration.go b/internal/install/proxy_configuration.go index 75e6cb659..1709608e0 100644 --- a/internal/install/proxy_configuration.go +++ b/internal/install/proxy_configuration.go @@ -6,3 +6,10 @@ func IsProxyConfigured() bool { proxyConfig := httpproxy.FromEnvironment() return proxyConfig.HTTPProxy != "" || proxyConfig.HTTPSProxy != "" || proxyConfig.NoProxy != "" } + +// ShouldWarnAboutProxy reports whether the proxy environment looks misconfigured: +// HTTP_PROXY is set (suggesting the user intends to use a proxy) but HTTPS_PROXY +// is absent, meaning the CLI's HTTPS calls to New Relic will bypass the proxy entirely. +func ShouldWarnAboutProxy(cfg httpproxy.Config) bool { + return cfg.HTTPProxy != "" && cfg.HTTPSProxy == "" +}