Skip to content

Commit

Permalink
fix (kubernetes-client-api) : Config should use proxy-url in kubeco…
Browse files Browse the repository at this point in the history
…nfig's cluster configuration

Set Config's `httpProxy` / `httpsProxy` fields if current context's
cluster configuration has `proxy-url` set

Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia committed Aug 19, 2024
1 parent 84788c0 commit c6a16dd
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public class Config {
private static final int DEFAULT_WATCH_RECONNECT_INTERVAL = 1000;
private static final int DEFAULT_CONNECTION_TIMEOUT = 10 * 1000;
private static final String DEFAULT_CLIENT_KEY_PASSPHRASE = "changeit";
private static final String SOCKS5_PROTOCOL_PREFIX = "socks5://";

private Boolean trustCerts;
private Boolean disableHostnameVerification;
Expand Down Expand Up @@ -926,6 +927,18 @@ private static void mergeKubeConfigContents(Config config, String context, io.fa
if (currentAuthInfo != null) {
mergeKubeConfigAuthInfo(config, currentCluster, currentAuthInfo);
}
String proxyUrl = currentCluster.getProxyUrl();
if (Utils.isNotNullOrEmpty(proxyUrl)) {
if (proxyUrl.startsWith(SOCKS5_PROTOCOL_PREFIX) && config.getMasterUrl().startsWith(HTTPS_PROTOCOL_PREFIX)) {
config.setHttpsProxy(proxyUrl);
} else if (proxyUrl.startsWith(SOCKS5_PROTOCOL_PREFIX)) {
config.setHttpProxy(proxyUrl);
} else if (proxyUrl.startsWith(HTTP_PROTOCOL_PREFIX)) {
config.setHttpProxy(proxyUrl);
} else if (proxyUrl.startsWith(HTTPS_PROTOCOL_PREFIX)) {
config.setHttpsProxy(proxyUrl);
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@
*/
package io.fabric8.kubernetes.client;

import io.fabric8.kubernetes.client.utils.Serialization;
import io.fabric8.kubernetes.client.utils.Utils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;

Expand Down Expand Up @@ -79,7 +85,7 @@ void kubeConfigProxyUrl() {
// When + Then
assertThat(new ConfigBuilder().build())
.hasFieldOrPropertyWithValue("httpProxy", "http://proxy-via-http-proxy-property:3128")
.hasFieldOrPropertyWithValue("httpsProxy", null);
.hasFieldOrPropertyWithValue("httpsProxy", "socks5://proxy-via-kubeconfig-proxy-url:1080");
} finally {
System.clearProperty("kubeconfig");
}
Expand Down Expand Up @@ -201,7 +207,7 @@ void kubeConfigProxyUrl() {
.filePath(ConfigTest.class.getResource("/config-proxy-source/kubeconfig-with-proxy-url")));
// When + Then
assertThat(configBuilder.build())
.hasFieldOrPropertyWithValue("httpsProxy", null)
.hasFieldOrPropertyWithValue("httpsProxy", "socks5://proxy-via-kubeconfig-proxy-url:1080")
.hasFieldOrPropertyWithValue("httpProxy", "http://proxy-via-builder:3128");
} finally {
System.clearProperty("kubeconfig");
Expand All @@ -223,13 +229,70 @@ void tearDown() {
System.clearProperty("kubeconfig");
}

@Test
@Disabled("https://github.com/fabric8io/kubernetes-client/issues/6150")
void noOtherConfiguration() {
// When + Then
assertThat(new ConfigBuilder().build())
.hasFieldOrPropertyWithValue("httpProxy", null)
.hasFieldOrPropertyWithValue("httpsProxy", "socks5://proxy-via-kubeconfig-proxy-url:1080");
@Nested
@DisplayName("no other proxy configuration")
class NoOtherConfiguration {
@TempDir
private File temporaryFolder;

@Test
@DisplayName("proxy-url with sock5 scheme, masterUrl with https scheme, then Config sets httpsProxy")
void whenProxyUrlSocks5MasterUrlHttps_thenHttpsProxySetInConfig() {
// When + Then
assertThat(new ConfigBuilder().build())
.hasFieldOrPropertyWithValue("httpProxy", null)
.hasFieldOrPropertyWithValue("httpsProxy", "socks5://proxy-via-kubeconfig-proxy-url:1080");
}

@Test
@DisplayName("proxy-url with http scheme, then Config sets httpProxy")
void whenProxyUrlHttp_thenHttpProxySetInConfig() throws IOException {
// Given
System.setProperty("kubeconfig",
createKubeConfigWithCluster("http://kubernetes-remote-server.example:6443", "http://proxy-server.example:80")
.getAbsolutePath());
// When + Then
assertThat(new ConfigBuilder().build())
.hasFieldOrPropertyWithValue("httpProxy", "http://proxy-server.example:80")
.hasFieldOrPropertyWithValue("httpsProxy", null);
}

@Test
@DisplayName("proxy-url with https scheme, then Config sets httpsProxy")
void whenProxyUrlHttps_thenHttpsProxySetInConfig() throws IOException {
// Given
System.setProperty("kubeconfig",
createKubeConfigWithCluster("https://kubernetes-remote-server.example:6443", "https://proxy-server.example:80")
.getAbsolutePath());
// When + Then
assertThat(new ConfigBuilder().build())
.hasFieldOrPropertyWithValue("httpProxy", null)
.hasFieldOrPropertyWithValue("httpsProxy", "https://proxy-server.example:80");
}

@Test
@DisplayName("proxy-url with sock5 scheme, masterUrl with http scheme, then Config sets httpProxy")
void whenProxyUrlSocks5AndMasterUrlHttp_thenHttpProxySetInConfig() throws IOException {
// Given
System.setProperty("kubeconfig",
createKubeConfigWithCluster("http://kubernetes-remote-server.example:6443", "socks5://proxy-server.example:1080")
.getAbsolutePath());
// When + Then
assertThat(new ConfigBuilder().build())
.hasFieldOrPropertyWithValue("httpProxy", "socks5://proxy-server.example:1080")
.hasFieldOrPropertyWithValue("httpsProxy", null);
}

private File createKubeConfigWithCluster(String masterUrl, String proxyUrl) throws IOException {
io.fabric8.kubernetes.api.model.Config kubeConfig = Serialization.unmarshal(
ConfigTest.class.getResourceAsStream("/config-proxy-source/kubeconfig-with-proxy-url"),
io.fabric8.kubernetes.api.model.Config.class);
kubeConfig.getClusters().get(0).getCluster().setServer(masterUrl);
kubeConfig.getClusters().get(0).getCluster().setProxyUrl(proxyUrl);
File updatedKubeConfig = new File(temporaryFolder, "kubeconfig");
Files.write(updatedKubeConfig.toPath(), Serialization.asYaml(kubeConfig).getBytes(StandardCharsets.UTF_8));
return updatedKubeConfig;
}
}

@Test
Expand All @@ -240,7 +303,7 @@ void httpProxySystemProperty() {

assertThat(new ConfigBuilder().build())
.hasFieldOrPropertyWithValue("httpProxy", "http://proxy-via-http-proxy-property:3128")
.hasFieldOrPropertyWithValue("httpsProxy", null);
.hasFieldOrPropertyWithValue("httpsProxy", "socks5://proxy-via-kubeconfig-proxy-url:1080");
} finally {
System.clearProperty("http.proxy");
}
Expand All @@ -265,7 +328,7 @@ void allProxySystemProperty() {
void httpProxyViaBuilder() {
assertThat(new ConfigBuilder().withHttpProxy("http://proxy-via-builder:3128").build())
.hasFieldOrPropertyWithValue("httpProxy", "http://proxy-via-builder:3128")
.hasFieldOrPropertyWithValue("httpsProxy", null);
.hasFieldOrPropertyWithValue("httpsProxy", "socks5://proxy-via-kubeconfig-proxy-url:1080");
}
}
}

0 comments on commit c6a16dd

Please sign in to comment.