|
60 | 60 | import com.intellij.util.ObjectUtils;
|
61 | 61 | import com.intellij.util.concurrency.annotations.RequiresWriteLock;
|
62 | 62 | import com.intellij.util.messages.MessageBusConnection;
|
| 63 | +import com.intellij.util.net.IdeHttpClientHelpers; |
| 64 | +import com.intellij.util.net.ssl.CertificateManager; |
63 | 65 | import org.apache.commons.lang.StringEscapeUtils;
|
| 66 | +import org.apache.http.client.CredentialsProvider; |
| 67 | +import org.apache.http.client.config.RequestConfig; |
64 | 68 | import org.apache.http.client.methods.CloseableHttpResponse;
|
65 | 69 | import org.apache.http.client.methods.HttpPost;
|
66 | 70 | import org.apache.http.client.methods.HttpUriRequest;
|
| 71 | +import org.apache.http.conn.ssl.DefaultHostnameVerifier; |
67 | 72 | import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
68 |
| -import org.apache.http.conn.ssl.TrustAllStrategy; |
| 73 | +import org.apache.http.conn.util.PublicSuffixMatcherLoader; |
69 | 74 | import org.apache.http.entity.ContentType;
|
70 | 75 | import org.apache.http.entity.StringEntity;
|
71 |
| -import org.apache.http.impl.client.CloseableHttpClient; |
72 |
| -import org.apache.http.impl.client.HttpClientBuilder; |
73 |
| -import org.apache.http.impl.client.HttpClients; |
74 |
| -import org.apache.http.impl.client.LaxRedirectStrategy; |
75 |
| -import org.apache.http.ssl.SSLContextBuilder; |
| 76 | +import org.apache.http.impl.client.*; |
76 | 77 | import org.apache.http.util.EntityUtils;
|
77 | 78 | import org.jetbrains.annotations.NotNull;
|
78 | 79 | import org.jetbrains.annotations.Nullable;
|
79 | 80 |
|
| 81 | +import javax.net.ssl.HostnameVerifier; |
80 | 82 | import java.io.IOException;
|
81 |
| -import java.nio.file.Path; |
82 |
| -import java.nio.file.Paths; |
83 | 83 | import java.security.*;
|
84 | 84 | import java.security.cert.CertificateException;
|
85 | 85 | import java.util.Collection;
|
@@ -227,40 +227,39 @@ public GraphQLConfigSecurity getSecurityConfig(@Nullable VirtualFile configFile)
|
227 | 227 | return null;
|
228 | 228 | }
|
229 | 229 |
|
230 |
| - @NotNull |
231 |
| - public CloseableHttpClient createHttpClient(@Nullable GraphQLConfigSecurity sslConfig) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, UnrecoverableKeyException, CertificateException { |
232 |
| - HttpClientBuilder builder = HttpClients.custom(); |
233 |
| - builder.setRedirectStrategy(LaxRedirectStrategy.INSTANCE); |
234 |
| - |
235 |
| - if (PropertiesComponent.getInstance(myProject).isTrueValue(GRAPHQL_TRUST_ALL_HOSTS)) { |
236 |
| - if (sslConfig != null && sslConfig.clientCertificate != null && sslConfig.clientCertificateKey != null) { |
237 |
| - if (sslConfig.clientCertificate.path == null || sslConfig.clientCertificateKey.path == null) { |
238 |
| - throw new RuntimeException("Path needs to be specified for the key and certificate"); |
239 |
| - } |
240 |
| - Path certPath = Paths.get(sslConfig.clientCertificate.path); |
241 |
| - Path keyPath = Paths.get(sslConfig.clientCertificateKey.path); |
242 |
| - GraphQLConfigCertificate.Encoding keyFormat = sslConfig.clientCertificateKey.format; |
243 |
| - |
244 |
| - KeyStore store = GraphQLIntrospectionSSLBuilder.makeKeyStore(certPath, keyPath, keyFormat); |
245 |
| - builder |
246 |
| - .setSSLContext( |
247 |
| - new SSLContextBuilder() |
248 |
| - .loadTrustMaterial(null, TrustAllStrategy.INSTANCE) |
249 |
| - .loadKeyMaterial(store, null) |
250 |
| - .build()) |
251 |
| - .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE); |
252 |
| - } else { |
253 |
| - builder |
254 |
| - .setSSLContext( |
255 |
| - new SSLContextBuilder() |
256 |
| - .loadTrustMaterial(null, TrustAllStrategy.INSTANCE) |
257 |
| - .build()) |
258 |
| - .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE); |
259 |
| - } |
260 |
| - } |
| 230 | + public @NotNull CloseableHttpClient createHttpClient(@NotNull String url, @Nullable GraphQLConfigSecurity sslConfig) |
| 231 | + throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, UnrecoverableKeyException, |
| 232 | + CertificateException { |
| 233 | + HttpClientBuilder builder = HttpClients.custom() |
| 234 | + .setDefaultRequestConfig(createRequestConfig(url)) |
| 235 | + .setSSLContext(CertificateManager.getInstance().getSslContext()) |
| 236 | + .setDefaultCredentialsProvider(createCredentialsProvider(url)) |
| 237 | + .setRedirectStrategy(LaxRedirectStrategy.INSTANCE) |
| 238 | + .setSSLHostnameVerifier(createHostnameVerifier()); |
| 239 | + GraphQLIntrospectionSSLBuilder.loadCustomSSLConfiguration(sslConfig, builder); |
| 240 | + return builder.build(); |
| 241 | + } |
| 242 | + |
| 243 | + private @NotNull RequestConfig createRequestConfig(@NotNull String url) { |
| 244 | + RequestConfig.Builder builder = RequestConfig.custom() |
| 245 | + .setConnectTimeout(3000) |
| 246 | + .setSocketTimeout(5000); |
| 247 | + IdeHttpClientHelpers.ApacheHttpClient4.setProxyForUrlIfEnabled(builder, url); |
261 | 248 | return builder.build();
|
262 | 249 | }
|
263 | 250 |
|
| 251 | + private @NotNull CredentialsProvider createCredentialsProvider(@NotNull String url) { |
| 252 | + CredentialsProvider provider = new BasicCredentialsProvider(); |
| 253 | + IdeHttpClientHelpers.ApacheHttpClient4.setProxyCredentialsForUrlIfEnabled(provider, url); |
| 254 | + return provider; |
| 255 | + } |
| 256 | + |
| 257 | + private @NotNull HostnameVerifier createHostnameVerifier() { |
| 258 | + return PropertiesComponent.getInstance(myProject).isTrueValue(GRAPHQL_TRUST_ALL_HOSTS) |
| 259 | + ? NoopHostnameVerifier.INSTANCE |
| 260 | + : new DefaultHostnameVerifier(PublicSuffixMatcherLoader.getDefault()); |
| 261 | + } |
| 262 | + |
264 | 263 | @Nullable
|
265 | 264 | public NotificationAction createTrustAllHostsAction() {
|
266 | 265 | final PropertiesComponent propertiesComponent = PropertiesComponent.getInstance(myProject);
|
@@ -550,7 +549,7 @@ public void run(@NotNull ProgressIndicator indicator) {
|
550 | 549 | indicator.setIndeterminate(true);
|
551 | 550 | String responseJson;
|
552 | 551 | GraphQLConfigSecurity sslConfig = getSecurityConfig(introspectionSourceFile);
|
553 |
| - try (final CloseableHttpClient httpClient = createHttpClient(sslConfig); |
| 552 | + try (final CloseableHttpClient httpClient = createHttpClient(url, sslConfig); |
554 | 553 | final CloseableHttpResponse response = httpClient.execute(request)) {
|
555 | 554 | responseJson = ObjectUtils.coalesce(EntityUtils.toString(response.getEntity()), "");
|
556 | 555 | } catch (IOException | GeneralSecurityException e) {
|
|
0 commit comments