Skip to content

Commit

Permalink
Merge pull request #1393 from Vlatombe/proxy-username-mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Vlatombe authored Jun 23, 2023
2 parents 581298f + 19644a3 commit 88e3b0c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import java.util.Base64;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;

import com.cloudbees.plugins.credentials.CredentialsMatchers;
Expand Down Expand Up @@ -139,7 +141,7 @@ public KubernetesClient createClient() throws KubernetesAuthException {
}

LOGGER.log(FINE, "Creating Kubernetes client: {0}", this.toString());
// JENKINS-63584 If Jenkins has an configured Proxy and cloud has enabled proxy usage pass the arguments to K8S
// JENKINS-63584 If Jenkins has a configured Proxy and cloud has enabled proxy usage pass the arguments to K8S
LOGGER.log(FINE, "Proxy Settings for Cloud: " + useJenkinsProxy);
if(useJenkinsProxy) {
Jenkins jenkins = Jenkins.getInstanceOrNull();
Expand All @@ -150,9 +152,10 @@ public KubernetesClient createClient() throws KubernetesAuthException {
if (p != null) {
builder.withHttpsProxy("http://" + p.name + ":" + p.port);
builder.withHttpProxy("http://" + p.name + ":" + p.port);
if (p.name != null) {
String proxyUserName = p.getUserName();
if (proxyUserName != null) {
String password = getProxyPasswordDecrypted(p);
builder.withProxyUsername(p.name);
builder.withProxyUsername(proxyUserName);
builder.withProxyPassword(password);
}
builder.withNoProxy(getNoProxyHosts(p));
Expand Down Expand Up @@ -180,13 +183,11 @@ public KubernetesClient createClient() throws KubernetesAuthException {
* @return the array of no proxy hosts
*/
private String[] getNoProxyHosts(@NonNull ProxyConfiguration proxy) {
String[] noProxyHosts = proxy.getNoProxyHost().split("\n");
int i=0;
while(i<noProxyHosts.length) {
noProxyHosts[i] = noProxyHosts[i].replace("*", "");
i++;
Set<String> noProxyHosts = new HashSet<>();
for (String noProxyHost : proxy.getNoProxyHost().split("\n")) {
noProxyHosts.add(noProxyHost.replace("*", ""));
}
return noProxyHosts;
return noProxyHosts.toArray(new String[0]);
}

private String getProxyPasswordDecrypted(ProxyConfiguration p) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
import hudson.ProxyConfiguration;
import hudson.util.Secret;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.utils.HttpClientUtils;
import org.jenkinsci.plugins.kubernetes.auth.KubernetesAuthException;
import org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl;
import org.junit.After;
Expand All @@ -39,7 +39,6 @@
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;

import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -55,8 +54,6 @@
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

/**
Expand Down Expand Up @@ -176,32 +173,57 @@ public void autoConfigWithAuth() throws Exception {

@Test
@Issue("JENKINS-70563")
public void jenkinsProxyConfiguration() throws KubernetesAuthException, MalformedURLException {
public void jenkinsProxyConfiguration() throws KubernetesAuthException {

j.jenkins.setProxy(new ProxyConfiguration("proxy.com", 123, PROXY_USERNAME, PROXY_PASSWORD, "*acme.com\n*acme*.com\n*.example.com|192.168.*"));
KubernetesFactoryAdapter factory = new KubernetesFactoryAdapter("http://acme.com", null, null, null, false, 15, 5, 32, true);
try(KubernetesClient client = factory.createClient()) {
assertNull(HttpClientUtils.getProxyUrl(client.getConfiguration()));
try (KubernetesClient client = factory.createClient()) {
Config configuration = client.getConfiguration();
assertEquals("http://proxy.com:123", configuration.getHttpProxy());
assertEquals(PROXY_USERNAME, configuration.getProxyUsername());
assertEquals(PROXY_PASSWORD, configuration.getProxyPassword());
assertArrayEquals(new String[] {"acme.com", ".example.com|192.168."}, configuration.getNoProxy());
assertEquals("http://proxy.com:123", configuration.getHttpsProxy());
}

j.jenkins.setProxy(new ProxyConfiguration("proxy.com", 123, PROXY_USERNAME, PROXY_PASSWORD, "*acme.com"));
factory = new KubernetesFactoryAdapter("http://acme.com", null, null, null, false, 15, 5, 32, true);
try(KubernetesClient client = factory.createClient()) {
assertNull(HttpClientUtils.getProxyUrl(client.getConfiguration()));
try (KubernetesClient client = factory.createClient()) {
Config configuration = client.getConfiguration();
assertEquals("http://proxy.com:123", configuration.getHttpProxy());
assertEquals(PROXY_USERNAME, configuration.getProxyUsername());
assertEquals(PROXY_PASSWORD, configuration.getProxyPassword());
assertArrayEquals(new String[] {"acme.com"}, configuration.getNoProxy());
assertEquals("http://proxy.com:123", configuration.getHttpsProxy());
}
factory = new KubernetesFactoryAdapter("http://k8s.acme.com", null, null, null, false, 15, 5, 32, true);
try(KubernetesClient client = factory.createClient()) {
assertNull(HttpClientUtils.getProxyUrl(client.getConfiguration()));
try (KubernetesClient client = factory.createClient()) {
Config configuration = client.getConfiguration();
assertEquals("http://proxy.com:123", configuration.getHttpProxy());
assertEquals(PROXY_USERNAME, configuration.getProxyUsername());
assertEquals(PROXY_PASSWORD, configuration.getProxyPassword());
assertArrayEquals(new String[] {"acme.com"}, configuration.getNoProxy());
assertEquals("http://proxy.com:123", configuration.getHttpsProxy());
}

j.jenkins.setProxy(new ProxyConfiguration("proxy.com", 123, PROXY_USERNAME, PROXY_PASSWORD, "*.acme.com"));
factory = new KubernetesFactoryAdapter("http://acme.com", null, null, null, false, 15, 5, 32, true);
try(KubernetesClient client = factory.createClient()) {
assertNotNull(HttpClientUtils.getProxyUrl(client.getConfiguration()));
try (KubernetesClient client = factory.createClient()) {
Config configuration = client.getConfiguration();
assertEquals("http://proxy.com:123", configuration.getHttpProxy());
assertEquals(PROXY_USERNAME, configuration.getProxyUsername());
assertEquals(PROXY_PASSWORD, configuration.getProxyPassword());
assertArrayEquals(new String[] {".acme.com"}, configuration.getNoProxy());
assertEquals("http://proxy.com:123", configuration.getHttpsProxy());
}
factory = new KubernetesFactoryAdapter("http://k8s.acme.com", null, null, null, false, 15, 5, 32, true);
try(KubernetesClient client = factory.createClient()) {
assertNull(HttpClientUtils.getProxyUrl(client.getConfiguration()));
try (KubernetesClient client = factory.createClient()) {
Config configuration = client.getConfiguration();
assertEquals("http://proxy.com:123", configuration.getHttpProxy());
assertEquals(PROXY_USERNAME, configuration.getProxyUsername());
assertEquals(PROXY_PASSWORD, configuration.getProxyPassword());
assertArrayEquals(new String[] {".acme.com"}, configuration.getNoProxy());
assertEquals("http://proxy.com:123", configuration.getHttpsProxy());
}
}
}

0 comments on commit 88e3b0c

Please sign in to comment.