|
7 | 7 | */
|
8 | 8 | package com.intellij.lang.jsgraphql.ide.introspection;
|
9 | 9 |
|
| 10 | +import com.fasterxml.jackson.databind.ObjectMapper; |
10 | 11 | import com.google.gson.Gson;
|
11 | 12 | import com.google.gson.JsonSyntaxException;
|
12 | 13 | import com.intellij.ide.actions.CreateFileAction;
|
@@ -101,9 +102,10 @@ public class GraphQLIntrospectionService implements Disposable {
|
101 | 102 | private static final String DISABLE_EMPTY_ERRORS_WARNING_KEY = "graphql.empty.errors.warning.disabled";
|
102 | 103 | public static final String GRAPHQL_TRUST_ALL_HOSTS = "graphql.trust.all.hosts";
|
103 | 104 |
|
| 105 | + public static final String SSL_EXTENSION = "sslConfiguration"; |
| 106 | + |
104 | 107 | private GraphQLIntrospectionTask latestIntrospection = null;
|
105 | 108 | private final Project myProject;
|
106 |
| - private Project myProject1; |
107 | 109 |
|
108 | 110 | public static GraphQLIntrospectionService getInstance(@NotNull Project project) {
|
109 | 111 | return ServiceManager.getService(project, GraphQLIntrospectionService.class);
|
@@ -190,14 +192,46 @@ public static HttpPost createRequest(@NotNull GraphQLConfigVariableAwareEndpoint
|
190 | 192 | return request;
|
191 | 193 | }
|
192 | 194 |
|
| 195 | + public GraphQLConfigSecurity getSecurityConfig(@NotNull VirtualFile file) { |
| 196 | + |
| 197 | + GraphQLConfigData config = GraphQLConfigManager.getService(myProject).getConfigurationsByPath().get(file); |
| 198 | + Map<String, Object> sslExtension = (Map<String, Object>) config.extensions.get(SSL_EXTENSION); |
| 199 | + if (sslExtension != null && ! sslExtension.isEmpty()) { |
| 200 | + GraphQLConfigSecurity sslConfig = new GraphQLConfigSecurity(); |
| 201 | + Map<String, Object> clientCertificate = (Map<String, Object>) sslExtension.get("clientCertificate"); |
| 202 | + if (clientCertificate != null && ! clientCertificate.isEmpty()) { |
| 203 | + sslConfig.clientCertificate = new GraphQLConfigCertificate(); |
| 204 | + String path = (String) clientCertificate.get("path"); |
| 205 | + sslConfig.clientCertificate.path = path; |
| 206 | + String format = (String) clientCertificate.get("format"); |
| 207 | + if (format != null && ! format.equals("PEM")) { |
| 208 | + throw new RuntimeException("Unsupported certificate format, only PEM is currently supported"); |
| 209 | + } |
| 210 | + sslConfig.clientCertificate.format = GraphQLConfigCertificate.Encoding.PEM; |
| 211 | + } |
| 212 | + Map<String, Object> clientCertificateKey = (Map<String, Object>) sslExtension.get("clientCertificateKey"); |
| 213 | + if (clientCertificateKey != null && ! clientCertificateKey.isEmpty()) { |
| 214 | + sslConfig.clientCertificateKey = new GraphQLConfigCertificate(); |
| 215 | + String path = (String) clientCertificateKey.get("path"); |
| 216 | + sslConfig.clientCertificateKey.path = path; |
| 217 | + String format = (String) clientCertificateKey.get("format"); |
| 218 | + if (format != null && ! format.equals("PEM")) { |
| 219 | + throw new RuntimeException("Unsupported certificate format, only PEM is currently supported"); |
| 220 | + } |
| 221 | + sslConfig.clientCertificateKey.format = GraphQLConfigCertificate.Encoding.PEM; |
| 222 | + } |
| 223 | + return sslConfig; |
| 224 | + } |
| 225 | + return null; |
| 226 | + } |
| 227 | + |
193 | 228 | @NotNull
|
194 |
| - public CloseableHttpClient createHttpClient() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, UnrecoverableKeyException, CertificateException { |
| 229 | + public CloseableHttpClient createHttpClient(GraphQLConfigSecurity sslConfig) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, UnrecoverableKeyException, CertificateException { |
195 | 230 | HttpClientBuilder builder = HttpClients.custom();
|
196 | 231 | builder.setRedirectStrategy(LaxRedirectStrategy.INSTANCE);
|
197 | 232 |
|
198 | 233 | if (PropertiesComponent.getInstance(myProject).isTrueValue(GRAPHQL_TRUST_ALL_HOSTS)) {
|
199 |
| - GraphQLConfigSecurity sslConfig = GraphQLConfigManager.getService(myProject).getSSLConfiguration(); |
200 |
| - if (sslConfig != null) { |
| 234 | + if (sslConfig != null && sslConfig.clientCertificate != null && sslConfig.clientCertificateKey != null) { |
201 | 235 | if (sslConfig.clientCertificate.path == null || sslConfig.clientCertificateKey.path == null) {
|
202 | 236 | throw new RuntimeException("Path needs to be specified for the key and certificate");
|
203 | 237 | }
|
@@ -502,8 +536,8 @@ public IntrospectionQueryTask(@NotNull HttpUriRequest request,
|
502 | 536 | public void run(@NotNull ProgressIndicator indicator) {
|
503 | 537 | indicator.setIndeterminate(true);
|
504 | 538 | String responseJson;
|
505 |
| - |
506 |
| - try (final CloseableHttpClient httpClient = createHttpClient(); |
| 539 | + GraphQLConfigSecurity sslConfig = getSecurityConfig(introspectionSourceFile.getParent()); |
| 540 | + try (final CloseableHttpClient httpClient = createHttpClient(sslConfig); |
507 | 541 | final CloseableHttpResponse response = httpClient.execute(request)) {
|
508 | 542 | responseJson = ObjectUtils.coalesce(EntityUtils.toString(response.getEntity()), "");
|
509 | 543 | } catch (IOException | GeneralSecurityException e) {
|
|
0 commit comments