diff --git a/src/main/java/io/jenkins/plugins/jfrog/CliEnvConfigurator.java b/src/main/java/io/jenkins/plugins/jfrog/CliEnvConfigurator.java index 7cae8cd1..c57fccf1 100644 --- a/src/main/java/io/jenkins/plugins/jfrog/CliEnvConfigurator.java +++ b/src/main/java/io/jenkins/plugins/jfrog/CliEnvConfigurator.java @@ -5,6 +5,7 @@ import io.jenkins.plugins.jfrog.configuration.JenkinsProxyConfiguration; import org.apache.commons.lang3.StringUtils; + /** * Configures JFrog CLI environment variables for the job. * @@ -66,7 +67,7 @@ private static void setupProxy(EnvVars env) { env.put(HTTP_PROXY_ENV, proxyUrl); env.put(HTTPS_PROXY_ENV, proxyUrl); if (StringUtils.isNotBlank(proxyConfiguration.noProxy)) { - env.put(NO_PROXY, proxyConfiguration.noProxy); + env.put(NO_PROXY, noProxyExtractor(proxyConfiguration.noProxy)); } } @@ -79,4 +80,13 @@ private static void excludeProxyEnvFromPublishing(EnvVars env) { String jfrogCliEnvExclude = env.getOrDefault(JFROG_CLI_ENV_EXCLUDE, JFROG_CLI_DEFAULT_EXCLUSIONS); env.put(JFROG_CLI_ENV_EXCLUDE, String.join(";", jfrogCliEnvExclude, HTTP_PROXY_ENV, HTTPS_PROXY_ENV)); } + + static String noProxyExtractor(String noProxyList) { + // Trim leading and trailing spaces + String noProxyListTrim = noProxyList.trim(); + // Replace '|' with spaces and normalize whitespace + String noProxyListRemoveSpaceAndPipe = noProxyListTrim.replace("|", " ").replaceAll("\\s+", ";"); + // Replace multiple spaces with a single semicolon + return noProxyListRemoveSpaceAndPipe.replaceAll(";+", ";").replaceAll(";$", ""); + } } diff --git a/src/test/java/io/jenkins/plugins/jfrog/CliEnvConfiguratorNoProxyTest.java b/src/test/java/io/jenkins/plugins/jfrog/CliEnvConfiguratorNoProxyTest.java new file mode 100644 index 00000000..dfd28cdf --- /dev/null +++ b/src/test/java/io/jenkins/plugins/jfrog/CliEnvConfiguratorNoProxyTest.java @@ -0,0 +1,47 @@ +package io.jenkins.plugins.jfrog; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.util.Arrays; +import java.util.Collection; + +import static io.jenkins.plugins.jfrog.CliEnvConfigurator.noProxyExtractor; +import static org.junit.Assert.assertEquals; + +/** + * @author nathana + **/ +@RunWith(Parameterized.class) +public class CliEnvConfiguratorNoProxyTest { + private final String noProxyList; + private final String expectedResult; + + public CliEnvConfiguratorNoProxyTest(String noProxyList, String expectedResult) { + this.noProxyList = noProxyList; + this.expectedResult = expectedResult; + } + + @Parameterized.Parameters + public static Collection dataProvider() { + return Arrays.asList( + // Positive tests + new Object[]{"artifactory.jfrog.io", "artifactory.jfrog.io"}, + new Object[]{"artifactory.jfrog.io \n artifactory1.jfrog.io ", "artifactory.jfrog.io;artifactory1.jfrog.io"}, + new Object[]{" artifactory.jfrog.io \n \r artifactory1.jfrog.io;artifactory2.jfrog.io \n artifactory3.jfrog.io | artifactory4.jfrog.io \n artifactory5.jfrog.io ", "artifactory.jfrog.io;artifactory1.jfrog.io;artifactory2.jfrog.io;artifactory3.jfrog.io;artifactory4.jfrog.io;artifactory5.jfrog.io"}, + new Object[]{"\r\n", ""}, + new Object[]{";;;", ""}, + new Object[]{"artifactory.jfrog.io;", "artifactory.jfrog.io"}, + new Object[]{"artifactory.jfrog.io;artifactory1.jfrog.io", "artifactory.jfrog.io;artifactory1.jfrog.io"}, + new Object[]{"artifactory.jfrog.io;artifactory1.jfrog.io;artifactory2.jfrog.io;artifactory3.jfrog.io", "artifactory.jfrog.io;artifactory1.jfrog.io;artifactory2.jfrog.io;artifactory3.jfrog.io"}, + new Object[]{"artifactory.jfrog.io \nartifactory1.jfrog.io", "artifactory.jfrog.io;artifactory1.jfrog.io"}, + new Object[]{"artifactory.jfrog.io \nartifactory1.jfrog.io\nartifactory2.jfrog.io \n artifactory3.jfrog.io", "artifactory.jfrog.io;artifactory1.jfrog.io;artifactory2.jfrog.io;artifactory3.jfrog.io"} + ); + } + + @Test + public void testValidateCliVersion() { + assertEquals(expectedResult, noProxyExtractor(noProxyList)); + } +}