From 2c1e084b595d37107ba6ffac2ee475f512ee617a Mon Sep 17 00:00:00 2001 From: Steve Hawkins Date: Mon, 12 Aug 2024 11:14:17 -0400 Subject: [PATCH] fix: Support for proxy authentication from proxy URL user info closes: #6247 Signed-off-by: Steve Hawkins --- CHANGELOG.md | 1 + .../kubernetes/client/utils/HttpClientUtils.java | 16 ++++++++++++---- .../client/utils/HttpClientUtilsTest.java | 11 +++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 20a09e1bce3..4dd82f29446 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * Fix #5264: Remove deprecated `Config.errorMessages` field * Fix #6008: removing the optional dependency on bouncy castle * Fix #6230: introduced Quantity.multiply(int) to allow for Quantity multiplication by an integer +* Fix #6247: Support for proxy authentication from proxy URL user info #### Dependency Upgrade * Fix #6052: Removed dependency on no longer maintained com.github.mifmif:generex diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/utils/HttpClientUtils.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/utils/HttpClientUtils.java index 3022a748fce..9af45e7d0ea 100644 --- a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/utils/HttpClientUtils.java +++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/utils/HttpClientUtils.java @@ -122,13 +122,16 @@ public static Map createA return interceptors; } - - public static String basicCredentials(String username, String password) { - String usernameAndPassword = username + ":" + password; + + public static String basicCredentials(String usernameAndPassword) { String encoded = Base64.getEncoder().encodeToString(usernameAndPassword.getBytes(StandardCharsets.UTF_8)); return "Basic " + encoded; } + public static String basicCredentials(String username, String password) { + return basicCredentials(username + ":" + password); + } + /** * @deprecated you should not need to call this method directly. Please create your own HttpClient.Factory * should you need to customize your clients. @@ -224,10 +227,15 @@ static void configureProxy(Config config, HttpClient.Builder builder) builder.proxyType(HttpClient.ProxyType.DIRECT); } else { builder.proxyAddress(new InetSocketAddress(proxyUri.getHost(), proxyUri.getPort())); - + if (config.getProxyUsername() != null) { builder.proxyAuthorization(basicCredentials(config.getProxyUsername(), config.getProxyPassword())); } + + String userInfo = proxyUri.getUserInfo(); + if (userInfo != null) { + builder.proxyAuthorization(basicCredentials(userInfo)); + } builder.proxyType(toProxyType(proxyUri.getScheme())); } diff --git a/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/utils/HttpClientUtilsTest.java b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/utils/HttpClientUtilsTest.java index 25cfcaed57e..aa49cf115a7 100644 --- a/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/utils/HttpClientUtilsTest.java +++ b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/utils/HttpClientUtilsTest.java @@ -89,6 +89,17 @@ void testConfigureSocksProxy() throws Exception { Mockito.verify(builder).proxyType(HttpClient.ProxyType.SOCKS5); Mockito.verify(builder).proxyAddress(new InetSocketAddress("192.168.0.1", 8080)); } + + @Test + void testConfigureProxyAuth() throws Exception { + Config config = new ConfigBuilder().withMasterUrl("http://localhost").withHttpProxy("http://user:password@192.168.0.1:8080").build(); + Builder builder = Mockito.mock(HttpClient.Builder.class, Mockito.RETURNS_SELF); + + HttpClientUtils.configureProxy(config, builder); + + Mockito.verify(builder).proxyType(HttpClient.ProxyType.HTTP); + Mockito.verify(builder).proxyAuthorization("Basic dXNlcjpwYXNzd29yZA=="); + } @Test void testCreateApplicableInterceptors() {