-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: allowing the usage of authenticated http proxies for https (6371)
fix: allowing the usage of authenticated http proxies for https --- review: remove duplicate proxy init in Jetty Co-authored-by: Steven Hawkins <shawkins@redhat.com>
- Loading branch information
Showing
9 changed files
with
319 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...jetty/src/test/java/io/fabric8/kubernetes/client/jetty/JettyHttpClientProxyHttpsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.fabric8.kubernetes.client.jetty; | ||
|
||
import io.fabric8.kubernetes.client.http.AbstractHttpClientProxyHttpsTest; | ||
import io.fabric8.kubernetes.client.http.HttpClient; | ||
|
||
@SuppressWarnings("java:S2187") | ||
public class JettyHttpClientProxyHttpsTest extends AbstractHttpClientProxyHttpsTest { | ||
@Override | ||
protected HttpClient.Factory getHttpClientFactory() { | ||
return new JettyHttpClientFactory(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...-okhttp/src/test/java/io/fabric8/kubernetes/client/okhttp/OkHttpClientProxyHttpsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.fabric8.kubernetes.client.okhttp; | ||
|
||
import io.fabric8.kubernetes.client.http.AbstractHttpClientProxyHttpsTest; | ||
import io.fabric8.kubernetes.client.http.HttpClient; | ||
import okhttp3.OkHttpClient.Builder; | ||
|
||
@SuppressWarnings("java:S2187") | ||
public class OkHttpClientProxyHttpsTest extends AbstractHttpClientProxyHttpsTest { | ||
@Override | ||
protected HttpClient.Factory getHttpClientFactory() { | ||
return new OkHttpClientFactory() { | ||
@Override | ||
protected Builder newOkHttpClientBuilder() { | ||
Builder builder = super.newOkHttpClientBuilder(); | ||
builder.hostnameVerifier((hostname, session) -> true); | ||
return builder; | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...vertx/src/test/java/io/fabric8/kubernetes/client/vertx/VertxHttpClientProxyHttpsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.fabric8.kubernetes.client.vertx; | ||
|
||
import io.fabric8.kubernetes.client.http.AbstractHttpClientProxyHttpsTest; | ||
import io.fabric8.kubernetes.client.http.HttpClient; | ||
|
||
@SuppressWarnings("java:S2187") | ||
public class VertxHttpClientProxyHttpsTest extends AbstractHttpClientProxyHttpsTest { | ||
@Override | ||
protected HttpClient.Factory getHttpClientFactory() { | ||
return new VertxHttpClientFactory(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
...api/src/test/java/io/fabric8/kubernetes/client/http/AbstractHttpClientProxyHttpsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.fabric8.kubernetes.client.http; | ||
|
||
import io.fabric8.kubernetes.client.internal.SSLUtils; | ||
import io.fabric8.mockwebserver.Context; | ||
import io.fabric8.mockwebserver.DefaultMockServer; | ||
import io.fabric8.mockwebserver.ServerRequest; | ||
import io.fabric8.mockwebserver.ServerResponse; | ||
import io.fabric8.mockwebserver.dsl.HttpMethod; | ||
import io.fabric8.mockwebserver.internal.MockDispatcher; | ||
import io.fabric8.mockwebserver.internal.MockSSLContextFactory; | ||
import io.fabric8.mockwebserver.internal.SimpleRequest; | ||
import io.fabric8.mockwebserver.internal.SimpleResponse; | ||
import io.fabric8.mockwebserver.utils.ResponseProvider; | ||
import okhttp3.Headers; | ||
import okhttp3.mockwebserver.MockResponse; | ||
import okhttp3.mockwebserver.MockWebServer; | ||
import okhttp3.mockwebserver.RecordedRequest; | ||
import okhttp3.mockwebserver.SocketPolicy; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.net.InetSocketAddress; | ||
import java.util.ArrayDeque; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Queue; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import static io.fabric8.kubernetes.client.utils.HttpClientUtils.basicCredentials; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public abstract class AbstractHttpClientProxyHttpsTest { | ||
|
||
private static SocketPolicy defaultResponseSocketPolicy; | ||
private static Map<ServerRequest, Queue<ServerResponse>> responses; | ||
private static DefaultMockServer server; | ||
|
||
@BeforeAll | ||
static void beforeAll() { | ||
defaultResponseSocketPolicy = SocketPolicy.KEEP_OPEN; | ||
responses = new HashMap<>(); | ||
final MockWebServer okHttpMockWebServer = new MockWebServer(); | ||
final MockDispatcher dispatcher = new MockDispatcher(responses) { | ||
@Override | ||
public MockResponse peek() { | ||
return new MockResponse().setSocketPolicy(defaultResponseSocketPolicy); | ||
} | ||
}; | ||
server = new DefaultMockServer(new Context(), okHttpMockWebServer, responses, dispatcher, true); | ||
server.start(); | ||
okHttpMockWebServer.useHttps(MockSSLContextFactory.create().getSocketFactory(), true); | ||
} | ||
|
||
@AfterAll | ||
static void afterAll() { | ||
server.shutdown(); | ||
} | ||
|
||
protected abstract HttpClient.Factory getHttpClientFactory(); | ||
|
||
@Test | ||
@DisplayName("Proxied HttpClient adds required headers to the request") | ||
protected void proxyConfigurationAddsRequiredHeadersForHttps() throws Exception { | ||
final AtomicReference<RecordedRequest> initialConnectRequest = new AtomicReference<>(); | ||
final ResponseProvider<String> bodyProvider = new ResponseProvider<String>() { | ||
|
||
@Override | ||
public String getBody(RecordedRequest request) { | ||
return ""; | ||
} | ||
|
||
@Override | ||
public void setHeaders(Headers headers) { | ||
} | ||
|
||
@Override | ||
public int getStatusCode(RecordedRequest request) { | ||
defaultResponseSocketPolicy = SocketPolicy.UPGRADE_TO_SSL_AT_END; // for jetty to upgrade after the challenge | ||
if (request.getHeader(StandardHttpHeaders.PROXY_AUTHORIZATION) != null) { | ||
initialConnectRequest.compareAndSet(null, request); | ||
return 200; | ||
} | ||
return 407; | ||
} | ||
|
||
@Override | ||
public Headers getHeaders() { | ||
return new Headers.Builder().add("Proxy-Authenticate", "Basic").build(); | ||
} | ||
|
||
}; | ||
responses.computeIfAbsent(new SimpleRequest(HttpMethod.CONNECT, "/"), k -> new ArrayDeque<>()) | ||
.add(new SimpleResponse(true, bodyProvider, null, 0, TimeUnit.SECONDS)); | ||
// Given | ||
final HttpClient.Builder builder = getHttpClientFactory().newBuilder() | ||
.sslContext(null, SSLUtils.trustManagers(null, null, true, null, null)) | ||
.proxyAddress(new InetSocketAddress("localhost", server.getPort())) | ||
.proxyAuthorization(basicCredentials("auth", "cred")); | ||
try (HttpClient client = builder.build()) { | ||
// When | ||
client.sendAsync(client.newHttpRequestBuilder() | ||
.uri(String.format("https://0.0.0.0:%s/not-found", server.getPort() + 1)).build(), String.class) | ||
.get(30, TimeUnit.SECONDS); | ||
|
||
// if it fails, then authorization was not set | ||
assertThat(initialConnectRequest) | ||
.doesNotHaveNullValue() | ||
.hasValueMatching(r -> r.getHeader("Proxy-Authorization").equals("Basic YXV0aDpjcmVk")); | ||
} | ||
} | ||
} |
Oops, something went wrong.