|
31 | 31 | import com.intellij.openapi.application.WriteAction; |
32 | 32 | import com.intellij.openapi.command.WriteCommandAction; |
33 | 33 | import com.intellij.openapi.components.ServiceManager; |
| 34 | +import com.intellij.openapi.diagnostic.Logger; |
34 | 35 | import com.intellij.openapi.fileEditor.FileEditor; |
35 | 36 | import com.intellij.openapi.fileEditor.FileEditorManager; |
36 | 37 | import com.intellij.openapi.fileEditor.OpenFileDescriptor; |
|
66 | 67 | import org.apache.http.client.methods.CloseableHttpResponse; |
67 | 68 | import org.apache.http.client.methods.HttpPost; |
68 | 69 | import org.apache.http.client.methods.HttpUriRequest; |
| 70 | +import org.apache.http.conn.ssl.NoopHostnameVerifier; |
| 71 | +import org.apache.http.conn.ssl.TrustAllStrategy; |
69 | 72 | import org.apache.http.entity.ContentType; |
70 | 73 | import org.apache.http.entity.StringEntity; |
71 | 74 | import org.apache.http.impl.client.CloseableHttpClient; |
| 75 | +import org.apache.http.impl.client.HttpClientBuilder; |
72 | 76 | import org.apache.http.impl.client.HttpClients; |
| 77 | +import org.apache.http.ssl.SSLContextBuilder; |
73 | 78 | import org.apache.http.util.EntityUtils; |
74 | 79 | import org.jetbrains.annotations.NotNull; |
75 | 80 | import org.jetbrains.annotations.Nullable; |
76 | 81 |
|
77 | 82 | import java.io.IOException; |
| 83 | +import java.security.GeneralSecurityException; |
| 84 | +import java.security.KeyManagementException; |
| 85 | +import java.security.KeyStoreException; |
| 86 | +import java.security.NoSuchAlgorithmException; |
78 | 87 | import java.util.Collection; |
79 | 88 | import java.util.List; |
80 | 89 | import java.util.Map; |
|
83 | 92 | import static com.intellij.lang.jsgraphql.v1.ide.project.JSGraphQLLanguageUIProjectService.setHeadersFromOptions; |
84 | 93 |
|
85 | 94 | public class GraphQLIntrospectionService implements Disposable { |
| 95 | + private static final Logger LOG = Logger.getInstance(GraphQLIntrospectionService.class); |
86 | 96 |
|
87 | 97 | private static final Set<String> DEFAULT_DIRECTIVES = Sets.newHashSet("deprecated", "skip", "include", "specifiedBy"); |
88 | 98 | private static final String DISABLE_EMPTY_ERRORS_WARNING_KEY = "graphql.empty.errors.warning.disabled"; |
| 99 | + public static final String GRAPHQL_TRUST_ALL_HOSTS = "graphql.trust.all.hosts"; |
89 | 100 |
|
90 | 101 | private GraphQLIntrospectionTask latestIntrospection = null; |
91 | 102 | private final Project myProject; |
@@ -164,6 +175,27 @@ public static HttpPost createRequest(@NotNull GraphQLConfigVariableAwareEndpoint |
164 | 175 | return request; |
165 | 176 | } |
166 | 177 |
|
| 178 | + @NotNull |
| 179 | + public CloseableHttpClient createHttpClient() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException { |
| 180 | + HttpClientBuilder builder = HttpClients.custom(); |
| 181 | + if (PropertiesComponent.getInstance(myProject).isTrueValue(GRAPHQL_TRUST_ALL_HOSTS)) { |
| 182 | + builder |
| 183 | + .setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, TrustAllStrategy.INSTANCE).build()) |
| 184 | + .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE); |
| 185 | + } |
| 186 | + return builder.build(); |
| 187 | + } |
| 188 | + |
| 189 | + @Nullable |
| 190 | + public NotificationAction createTrustAllHostsAction() { |
| 191 | + final PropertiesComponent propertiesComponent = PropertiesComponent.getInstance(myProject); |
| 192 | + if (propertiesComponent.isTrueValue(GRAPHQL_TRUST_ALL_HOSTS)) return null; |
| 193 | + |
| 194 | + return NotificationAction.createSimpleExpiring( |
| 195 | + GraphQLBundle.message("graphql.notification.trust.all.hosts"), |
| 196 | + () -> propertiesComponent.setValue(GRAPHQL_TRUST_ALL_HOSTS, true)); |
| 197 | + } |
| 198 | + |
167 | 199 | public void addIntrospectionStackTraceAction(@NotNull Notification notification, @NotNull Exception exception) { |
168 | 200 | notification.addAction(new NotificationAction(GraphQLBundle.message("graphql.notification.stack.trace")) { |
169 | 201 | @Override |
@@ -392,6 +424,7 @@ void createOrUpdateIntrospectionOutputFile(@NotNull String schemaText, |
392 | 424 | } |
393 | 425 | }); |
394 | 426 | } catch (IOException ioe) { |
| 427 | + LOG.warn(ioe); |
395 | 428 | Notifications.Bus.notify(new Notification( |
396 | 429 | GraphQLNotificationUtil.NOTIFICATION_GROUP_ID, |
397 | 430 | GraphQLBundle.message("graphql.notification.error.title"), |
@@ -457,10 +490,10 @@ public void run(@NotNull ProgressIndicator indicator) { |
457 | 490 | indicator.setIndeterminate(true); |
458 | 491 | String responseJson; |
459 | 492 |
|
460 | | - try (final CloseableHttpClient httpClient = HttpClients.createDefault(); |
| 493 | + try (final CloseableHttpClient httpClient = createHttpClient(); |
461 | 494 | final CloseableHttpResponse response = httpClient.execute(request)) { |
462 | 495 | responseJson = ObjectUtils.coalesce(EntityUtils.toString(response.getEntity()), ""); |
463 | | - } catch (IOException e) { |
| 496 | + } catch (IOException | GeneralSecurityException e) { |
464 | 497 | GraphQLNotificationUtil.showGraphQLRequestErrorNotification(myProject, url, e, NotificationType.WARNING, retry); |
465 | 498 | return; |
466 | 499 | } |
|
0 commit comments