From 885791ad20f1a2c7fb58c297303a1bfa24531827 Mon Sep 17 00:00:00 2001 From: Nathan Amiel <59163863+Nathan770@users.noreply.github.com> Date: Mon, 23 Sep 2024 13:46:13 +0300 Subject: [PATCH] Support pipe and new line in the 'No proxy host' (#106) --- README.md | 2 +- .../plugins/jfrog/CliEnvConfigurator.java | 15 +++++- .../plugins/jfrog/CreateNoProxyValueTest.java | 47 +++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/test/java/io/jenkins/plugins/jfrog/CreateNoProxyValueTest.java diff --git a/README.md b/README.md index daa99241..879586cd 100644 --- a/README.md +++ b/README.md @@ -188,7 +188,7 @@ If you're using a JFrog platform that's situated behind an HTTP/S proxy, you sho under `Manage Jenkins` > `Manage Plugins` > `Advanced`. To exclude the JFrog platform from going through a configured proxy, provide your JFrog platform's host details in -the `No Proxy Host` section. This should be a list of comma-separated hosts. +the `No Proxy Host` section. Notice that the JFrog CLI is typically downloaded from releases.jfrog.io. You may need to add that to your list as well. ## Jenkins Configuration as Code diff --git a/src/main/java/io/jenkins/plugins/jfrog/CliEnvConfigurator.java b/src/main/java/io/jenkins/plugins/jfrog/CliEnvConfigurator.java index 7cae8cd1..5b63e656 100644 --- a/src/main/java/io/jenkins/plugins/jfrog/CliEnvConfigurator.java +++ b/src/main/java/io/jenkins/plugins/jfrog/CliEnvConfigurator.java @@ -66,7 +66,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, createNoProxyValue(proxyConfiguration.noProxy)); } } @@ -79,4 +79,17 @@ 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)); } + + /** + * Converts a list of No Proxy Hosts received by Jenkins into a semicolon-separated string format expected by JFrog CLI. + * + * @param noProxy - A string representing the list of No Proxy Hosts. + * @return A semicolon-separated string of No Proxy Hosts. + */ + static String createNoProxyValue(String noProxy) { + // Trim leading and trailing spaces, Replace '|' with spaces and normalize whitespace + String noProxyListRemoveSpaceAndPipe = noProxy.trim().replaceAll("[\\s|]+", ";"); + // Replace multiple semicolon with a single semicolon, and remove the last one if present + return noProxyListRemoveSpaceAndPipe.replaceAll(";+", ";").replaceAll("^;|;$", ""); + } } diff --git a/src/test/java/io/jenkins/plugins/jfrog/CreateNoProxyValueTest.java b/src/test/java/io/jenkins/plugins/jfrog/CreateNoProxyValueTest.java new file mode 100644 index 00000000..22c0ba2a --- /dev/null +++ b/src/test/java/io/jenkins/plugins/jfrog/CreateNoProxyValueTest.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.createNoProxyValue; +import static org.junit.Assert.assertEquals; + +/** + * @author nathana + **/ +@RunWith(Parameterized.class) +public class CreateNoProxyValueTest { + private final String noProxy; + private final String expectedResult; + + public CreateNoProxyValueTest(String noProxy, String expectedResult) { + this.noProxy = noProxy; + this.expectedResult = expectedResult; + } + + @Parameterized.Parameters + public static Collection dataProvider() { + return Arrays.asList( + 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"}, + new Object[]{";artifactory.jfrog.io;", "artifactory.jfrog.io"} + ); + } + + @Test + public void createNoProxyValueTest() { + assertEquals(expectedResult, createNoProxyValue(noProxy)); + } +}