Skip to content
Open
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
4 changes: 1 addition & 3 deletions internal/install/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}
Expand All @@ -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:")
Expand Down
50 changes: 27 additions & 23 deletions internal/install/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
})
}
7 changes: 7 additions & 0 deletions internal/install/proxy_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == ""
}
Loading