From d86faec43ec67f165685e0ae924c57ba63e9d355 Mon Sep 17 00:00:00 2001 From: Enquier Date: Sun, 7 Jun 2020 09:50:10 -0600 Subject: [PATCH] Adding fix for and tests for no proxy support for deployer --- artifactory/utils/buildinfoproperties.go | 31 +++++++++- artifactory/utils/buildinfoproperties_test.go | 62 +++++++++++++++++-- 2 files changed, 86 insertions(+), 7 deletions(-) diff --git a/artifactory/utils/buildinfoproperties.go b/artifactory/utils/buildinfoproperties.go index b323329b9..6eb021d57 100644 --- a/artifactory/utils/buildinfoproperties.go +++ b/artifactory/utils/buildinfoproperties.go @@ -2,8 +2,10 @@ package utils import ( "errors" + "github.com/jfrog/jfrog-client-go/utils/log" "io/ioutil" "net" + "net/http" "net/url" "os" "path/filepath" @@ -20,6 +22,7 @@ import ( const ( HttpProxy = "HTTP_PROXY" + NoProxy = "NO_PROXY" ) type BuildConfigMapping map[ProjectType][]*map[string]string @@ -235,6 +238,9 @@ func CreateBuildInfoPropertiesFile(buildName, buildNumber string, config *viper. func setProxyIfDefined(config *viper.Viper) error { // Add HTTP_PROXY if exists proxy := os.Getenv(HttpProxy) + noProxy := os.Getenv(NoProxy) + log.Debug("Proxy:" + proxy) + log.Debug("No Proxy:" + noProxy) if proxy != "" { url, err := url.Parse(proxy) if err != nil { @@ -244,8 +250,29 @@ func setProxyIfDefined(config *viper.Viper) error { if err != nil { return errorutils.CheckError(err) } - config.Set(PROXY+HOST, host) - config.Set(PROXY+PORT, port) + if noProxy != "" { + //TODO: This is for the Deployer as proxy properties are set only once (clientConfig). Proxy for dependency resolution can be set via the gradle client + deployRequest, err := http.NewRequest("HEAD", config.GetString(DEPLOYER_PREFIX+URL), nil) + if err != nil { + return errorutils.CheckError(err) + } + deployProxyURL, err := http.ProxyFromEnvironment(deployRequest) + log.Debug("Deploy URL:" + config.GetString(DEPLOYER_PREFIX+URL)) + if err != nil { + return errorutils.CheckError(err) + } + if deployProxyURL != nil { + log.Debug("Resolved Deploy Proxy:" + deployProxyURL.String()) + config.Set(PROXY+HOST, host) + config.Set(PROXY+PORT, port) + } else { + log.Debug("Deploy URL is within no_proxy") + } + } else { + config.Set(PROXY+HOST, host) + config.Set(PROXY+PORT, port) + } + } return nil } diff --git a/artifactory/utils/buildinfoproperties_test.go b/artifactory/utils/buildinfoproperties_test.go index 94d8528fe..46fb9eb5c 100644 --- a/artifactory/utils/buildinfoproperties_test.go +++ b/artifactory/utils/buildinfoproperties_test.go @@ -9,19 +9,24 @@ import ( ) const ( - host = "localhost" - port = "8888" - proxy = "http://" + host + ":" + port + host = "localhost" + port = "8888" + proxy = "http://" + host + ":" + port + noproxy = "some.other.url.com" + othernoproxy = "some.different.url.com" ) func TestCreateDefaultPropertiesFile(t *testing.T) { proxyOrg := getOriginalProxyValue() + noproxyOrg := getOriginalNoProxyValue() setProxy("", t) + setNoProxy("", t) for index := range ProjectTypes { testCreateDefaultPropertiesFile(ProjectType(index), t) } setProxy(proxyOrg, t) + setNoProxy(noproxyOrg, t) } func testCreateDefaultPropertiesFile(projectType ProjectType, t *testing.T) { @@ -51,9 +56,11 @@ func testCreateDefaultPropertiesFile(projectType ProjectType, t *testing.T) { compareViperConfigs(t, actualConfig, expectedConfig, projectType) } -func TestCreateSimplePropertiesFileWithProxy(t *testing.T) { +func TestCreateSimplePropertiesFileWithProxyAndWithoutNoProxy(t *testing.T) { proxyOrg := getOriginalProxyValue() + noproxyOrg := getOriginalNoProxyValue() setProxy(proxy, t) + setNoProxy("", t) var propertiesFileConfig = map[string]string{ "artifactory.resolve.contextUrl": "http://some.url.com", "artifactory.publish.contextUrl": "http://some.other.url.com", @@ -63,11 +70,14 @@ func TestCreateSimplePropertiesFileWithProxy(t *testing.T) { } createSimplePropertiesFile(t, propertiesFileConfig) setProxy(proxyOrg, t) + setNoProxy(noproxyOrg, t) } -func TestCreateSimplePropertiesFileWithoutProxy(t *testing.T) { +func TestCreateSimplePropertiesFileWithoutProxyAndNoProxy(t *testing.T) { proxyOrg := getOriginalProxyValue() + noproxyOrg := getOriginalNoProxyValue() setProxy("", t) + setNoProxy("", t) var propertiesFileConfig = map[string]string{ "artifactory.resolve.contextUrl": "http://some.url.com", "artifactory.publish.contextUrl": "http://some.other.url.com", @@ -75,9 +85,40 @@ func TestCreateSimplePropertiesFileWithoutProxy(t *testing.T) { } createSimplePropertiesFile(t, propertiesFileConfig) setProxy(proxyOrg, t) + setNoProxy(noproxyOrg, t) } +func TestCreateSimplePropertiesFileWithProxyAndNoProxy(t *testing.T) { + proxyOrg := getOriginalProxyValue() + noproxyOrg := getOriginalNoProxyValue() + setProxy(proxy, t) + setNoProxy(noproxy, t) + var propertiesFileConfig = map[string]string{ + "artifactory.resolve.contextUrl": "http://some.url.com", + "artifactory.publish.contextUrl": "http://some.other.url.com", + "artifactory.deploy.build.name": "buildName", + } + createSimplePropertiesFile(t, propertiesFileConfig) + setProxy(proxyOrg, t) + setNoProxy(noproxyOrg, t) +} + +func TestCreateSimplePropertiesFileWithProxyAndDifferentNoProxy(t *testing.T) { + proxyOrg := getOriginalProxyValue() + noproxyOrg := getOriginalNoProxyValue() + setProxy(proxy, t) + setNoProxy(othernoproxy, t) + var propertiesFileConfig = map[string]string{ + "artifactory.resolve.contextUrl": "http://some.url.com", + "artifactory.publish.contextUrl": "http://some.other.url.com", + "artifactory.deploy.build.name": "buildName", + } + createSimplePropertiesFile(t, propertiesFileConfig) + setProxy(proxyOrg, t) + setNoProxy(noproxyOrg, t) +} + func createSimplePropertiesFile(t *testing.T, propertiesFileConfig map[string]string) { var yamlConfig = map[string]string{ RESOLVER_PREFIX + URL: "http://some.url.com", @@ -191,9 +232,20 @@ func getOriginalProxyValue() string { return os.Getenv(HttpProxy) } +func getOriginalNoProxyValue() string { + return os.Getenv(NoProxy) +} + func setProxy(proxy string, t *testing.T) { err := os.Setenv(HttpProxy, proxy) if err != nil { t.Error(err) } } + +func setNoProxy(noproxy string, t *testing.T) { + err := os.Setenv(NoProxy, noproxy) + if err != nil { + t.Error(err) + } +}