Skip to content

Commit

Permalink
Support pipe and new line in the 'No proxy host' (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan770 authored Sep 23, 2024
1 parent 8b7b09c commit 885791a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/io/jenkins/plugins/jfrog/CliEnvConfigurator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand All @@ -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("^;|;$", "");
}
}
47 changes: 47 additions & 0 deletions src/test/java/io/jenkins/plugins/jfrog/CreateNoProxyValueTest.java
Original file line number Diff line number Diff line change
@@ -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<Object[]> 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));
}
}

0 comments on commit 885791a

Please sign in to comment.