Skip to content

Commit 08a03a1

Browse files
srinivasankavithavepanimas
authored andcommitted
Address code review feedback.
1 parent 8011f2b commit 08a03a1

File tree

3 files changed

+44
-19
lines changed

3 files changed

+44
-19
lines changed

src/main/com/intellij/lang/jsgraphql/ide/introspection/GraphQLIntrospectionService.java

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
package com.intellij.lang.jsgraphql.ide.introspection;
99

10+
import com.fasterxml.jackson.databind.ObjectMapper;
1011
import com.google.gson.Gson;
1112
import com.google.gson.JsonSyntaxException;
1213
import com.intellij.ide.actions.CreateFileAction;
@@ -101,9 +102,10 @@ public class GraphQLIntrospectionService implements Disposable {
101102
private static final String DISABLE_EMPTY_ERRORS_WARNING_KEY = "graphql.empty.errors.warning.disabled";
102103
public static final String GRAPHQL_TRUST_ALL_HOSTS = "graphql.trust.all.hosts";
103104

105+
public static final String SSL_EXTENSION = "sslConfiguration";
106+
104107
private GraphQLIntrospectionTask latestIntrospection = null;
105108
private final Project myProject;
106-
private Project myProject1;
107109

108110
public static GraphQLIntrospectionService getInstance(@NotNull Project project) {
109111
return ServiceManager.getService(project, GraphQLIntrospectionService.class);
@@ -190,14 +192,46 @@ public static HttpPost createRequest(@NotNull GraphQLConfigVariableAwareEndpoint
190192
return request;
191193
}
192194

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+
193228
@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 {
195230
HttpClientBuilder builder = HttpClients.custom();
196231
builder.setRedirectStrategy(LaxRedirectStrategy.INSTANCE);
197232

198233
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) {
201235
if (sslConfig.clientCertificate.path == null || sslConfig.clientCertificateKey.path == null) {
202236
throw new RuntimeException("Path needs to be specified for the key and certificate");
203237
}
@@ -502,8 +536,8 @@ public IntrospectionQueryTask(@NotNull HttpUriRequest request,
502536
public void run(@NotNull ProgressIndicator indicator) {
503537
indicator.setIndeterminate(true);
504538
String responseJson;
505-
506-
try (final CloseableHttpClient httpClient = createHttpClient();
539+
GraphQLConfigSecurity sslConfig = getSecurityConfig(introspectionSourceFile.getParent());
540+
try (final CloseableHttpClient httpClient = createHttpClient(sslConfig);
507541
final CloseableHttpResponse response = httpClient.execute(request)) {
508542
responseJson = ObjectUtils.coalesce(EntityUtils.toString(response.getEntity()), "");
509543
} catch (IOException | GeneralSecurityException e) {

src/main/com/intellij/lang/jsgraphql/ide/project/GraphQLUIProjectService.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.intellij.lang.jsgraphql.ide.project.graphqlconfig.GraphQLConfigManager;
2828
import com.intellij.lang.jsgraphql.ide.project.graphqlconfig.GraphQLConfigurationListener;
2929
import com.intellij.lang.jsgraphql.ide.project.graphqlconfig.model.GraphQLConfigEndpoint;
30+
import com.intellij.lang.jsgraphql.ide.project.graphqlconfig.model.GraphQLConfigSecurity;
3031
import com.intellij.lang.jsgraphql.ide.project.graphqlconfig.model.GraphQLConfigVariableAwareEndpoint;
3132
import com.intellij.lang.jsgraphql.ide.project.schemastatus.GraphQLEndpointsModel;
3233
import com.intellij.lang.jsgraphql.ide.project.toolwindow.GraphQLToolWindow;
@@ -358,7 +359,9 @@ public void run(@NotNull ProgressIndicator indicator) {
358359
private void runQuery(Editor editor, VirtualFile virtualFile, GraphQLQueryContext context, String url, HttpPost request) {
359360
GraphQLIntrospectionService introspectionService = GraphQLIntrospectionService.getInstance(myProject);
360361
try {
361-
try (final CloseableHttpClient httpClient = introspectionService.createHttpClient()) {
362+
VirtualFile configFile = GraphQLConfigManager.getService(myProject).getClosestConfigFile(virtualFile).getParent();
363+
GraphQLConfigSecurity sslConfig = introspectionService.getSecurityConfig(configFile);
364+
try (final CloseableHttpClient httpClient = introspectionService.createHttpClient(sslConfig)) {
362365
editor.putUserData(GRAPH_QL_EDITOR_QUERYING, true);
363366

364367
String responseJson;

src/main/com/intellij/lang/jsgraphql/ide/project/graphqlconfig/GraphQLConfigManager.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -429,18 +429,6 @@ public List<GraphQLConfigEndpoint> getEndpoints(@NotNull VirtualFile virtualFile
429429
}
430430
}
431431

432-
@Nullable
433-
public GraphQLConfigSecurity getSSLConfiguration() {
434-
try {
435-
readLock.lock();
436-
Map<VirtualFile, GraphQLConfigData> configEntries = getService(myProject).getConfigurationsByPath();
437-
GraphQLConfigSecurity sslConfig = new ObjectMapper().convertValue(configEntries.get(myProject.getBaseDir()).extensions.get(SSL_EXTENSION), GraphQLConfigSecurity.class);
438-
return sslConfig;
439-
} finally {
440-
readLock.unlock();
441-
}
442-
}
443-
444432
void initialize() {
445433
final MessageBusConnection connection = myProject.getMessageBus().connect(this);
446434
connection.subscribe(ProjectTopics.PROJECT_ROOTS, new ModuleRootListener() {

0 commit comments

Comments
 (0)