|
| 1 | +/* |
| 2 | + * Copyright (C) 2015 Red Hat, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.fabric8.kubernetes.client.http; |
| 17 | + |
| 18 | +import io.fabric8.kubernetes.client.internal.SSLUtils; |
| 19 | +import io.fabric8.mockwebserver.Context; |
| 20 | +import io.fabric8.mockwebserver.DefaultMockServer; |
| 21 | +import io.fabric8.mockwebserver.ServerRequest; |
| 22 | +import io.fabric8.mockwebserver.ServerResponse; |
| 23 | +import io.fabric8.mockwebserver.dsl.HttpMethod; |
| 24 | +import io.fabric8.mockwebserver.internal.MockDispatcher; |
| 25 | +import io.fabric8.mockwebserver.internal.MockSSLContextFactory; |
| 26 | +import io.fabric8.mockwebserver.internal.SimpleRequest; |
| 27 | +import io.fabric8.mockwebserver.internal.SimpleResponse; |
| 28 | +import io.fabric8.mockwebserver.utils.ResponseProvider; |
| 29 | +import okhttp3.Headers; |
| 30 | +import okhttp3.mockwebserver.MockResponse; |
| 31 | +import okhttp3.mockwebserver.MockWebServer; |
| 32 | +import okhttp3.mockwebserver.RecordedRequest; |
| 33 | +import okhttp3.mockwebserver.SocketPolicy; |
| 34 | +import org.junit.jupiter.api.AfterAll; |
| 35 | +import org.junit.jupiter.api.BeforeAll; |
| 36 | +import org.junit.jupiter.api.DisplayName; |
| 37 | +import org.junit.jupiter.api.Test; |
| 38 | + |
| 39 | +import java.net.InetSocketAddress; |
| 40 | +import java.util.ArrayDeque; |
| 41 | +import java.util.HashMap; |
| 42 | +import java.util.Map; |
| 43 | +import java.util.Queue; |
| 44 | +import java.util.concurrent.TimeUnit; |
| 45 | +import java.util.concurrent.atomic.AtomicReference; |
| 46 | + |
| 47 | +import static io.fabric8.kubernetes.client.utils.HttpClientUtils.basicCredentials; |
| 48 | +import static org.assertj.core.api.Assertions.assertThat; |
| 49 | + |
| 50 | +public abstract class AbstractHttpClientProxyHttpsTest { |
| 51 | + |
| 52 | + private static SocketPolicy defaultResponseSocketPolicy; |
| 53 | + private static Map<ServerRequest, Queue<ServerResponse>> responses; |
| 54 | + private static DefaultMockServer server; |
| 55 | + |
| 56 | + @BeforeAll |
| 57 | + static void beforeAll() { |
| 58 | + defaultResponseSocketPolicy = SocketPolicy.KEEP_OPEN; |
| 59 | + responses = new HashMap<>(); |
| 60 | + final MockWebServer okHttpMockWebServer = new MockWebServer(); |
| 61 | + final MockDispatcher dispatcher = new MockDispatcher(responses) { |
| 62 | + @Override |
| 63 | + public MockResponse peek() { |
| 64 | + return new MockResponse().setSocketPolicy(defaultResponseSocketPolicy); |
| 65 | + } |
| 66 | + }; |
| 67 | + server = new DefaultMockServer(new Context(), okHttpMockWebServer, responses, dispatcher, true); |
| 68 | + server.start(); |
| 69 | + okHttpMockWebServer.useHttps(MockSSLContextFactory.create().getSocketFactory(), true); |
| 70 | + } |
| 71 | + |
| 72 | + @AfterAll |
| 73 | + static void afterAll() { |
| 74 | + server.shutdown(); |
| 75 | + } |
| 76 | + |
| 77 | + protected abstract HttpClient.Factory getHttpClientFactory(); |
| 78 | + |
| 79 | + @Test |
| 80 | + @DisplayName("Proxied HttpClient adds required headers to the request") |
| 81 | + protected void proxyConfigurationAddsRequiredHeadersForHttps() throws Exception { |
| 82 | + final AtomicReference<RecordedRequest> initialConnectRequest = new AtomicReference<>(); |
| 83 | + final ResponseProvider<String> bodyProvider = new ResponseProvider<String>() { |
| 84 | + |
| 85 | + @Override |
| 86 | + public String getBody(RecordedRequest request) { |
| 87 | + return ""; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void setHeaders(Headers headers) { |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public int getStatusCode(RecordedRequest request) { |
| 96 | + defaultResponseSocketPolicy = SocketPolicy.UPGRADE_TO_SSL_AT_END; // for jetty to upgrade after the challenge |
| 97 | + if (request.getHeader(StandardHttpHeaders.PROXY_AUTHORIZATION) != null) { |
| 98 | + initialConnectRequest.compareAndSet(null, request); |
| 99 | + return 200; |
| 100 | + } |
| 101 | + return 407; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public Headers getHeaders() { |
| 106 | + return new Headers.Builder().add("Proxy-Authenticate", "Basic").build(); |
| 107 | + } |
| 108 | + |
| 109 | + }; |
| 110 | + responses.computeIfAbsent(new SimpleRequest(HttpMethod.CONNECT, "/"), k -> new ArrayDeque<>()) |
| 111 | + .add(new SimpleResponse(true, bodyProvider, null, 0, TimeUnit.SECONDS)); |
| 112 | + // Given |
| 113 | + final HttpClient.Builder builder = getHttpClientFactory().newBuilder() |
| 114 | + .sslContext(null, SSLUtils.trustManagers(null, null, true, null, null)) |
| 115 | + .proxyAddress(new InetSocketAddress("localhost", server.getPort())) |
| 116 | + .proxyAuthorization(basicCredentials("auth", "cred")); |
| 117 | + try (HttpClient client = builder.build()) { |
| 118 | + // When |
| 119 | + client.sendAsync(client.newHttpRequestBuilder() |
| 120 | + .uri(String.format("https://0.0.0.0:%s/not-found", server.getPort() + 1)).build(), String.class) |
| 121 | + .get(30, TimeUnit.SECONDS); |
| 122 | + |
| 123 | + // if it fails, then authorization was not set |
| 124 | + assertThat(initialConnectRequest) |
| 125 | + .doesNotHaveNullValue() |
| 126 | + .hasValueMatching(r -> r.getHeader("Proxy-Authorization").equals("Basic YXV0aDpjcmVk")); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments