You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jclouds.apache.org by na...@apache.org on 2019/10/16 07:48:52 UTC

[jclouds] branch master updated: JCLOUDS-1520: change UntrustedSSLContextSupplier to return the same SSLContext (#49)

This is an automated email from the ASF dual-hosted git repository.

nacx pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jclouds.git


The following commit(s) were added to refs/heads/master by this push:
     new 6d389b0  JCLOUDS-1520: change UntrustedSSLContextSupplier to return the same SSLContext (#49)
6d389b0 is described below

commit 6d389b0d8692fd660e77029fde40e5bb363ff6f8
Author: roded <ro...@users.noreply.github.com>
AuthorDate: Wed Oct 16 10:48:47 2019 +0300

    JCLOUDS-1520: change UntrustedSSLContextSupplier to return the same SSLContext (#49)
    
    Using the same SSLContext prevents consistent cache misses on the JVM's KeepAliveCache
    when attempting to reuse TLS connections.
---
 .../java/org/jclouds/http/config/SSLModule.java    | 18 +++++-------
 .../org/jclouds/http/config/SSLModuleTest.java     | 34 ++++++++++++++++++++++
 2 files changed, 42 insertions(+), 10 deletions(-)

diff --git a/core/src/main/java/org/jclouds/http/config/SSLModule.java b/core/src/main/java/org/jclouds/http/config/SSLModule.java
index eba1b47..8ec4810 100644
--- a/core/src/main/java/org/jclouds/http/config/SSLModule.java
+++ b/core/src/main/java/org/jclouds/http/config/SSLModule.java
@@ -74,24 +74,22 @@ public class SSLModule extends AbstractModule {
 
    @Singleton
    public static class UntrustedSSLContextSupplier implements Supplier<SSLContext> {
-      private final TrustAllCerts trustAllCerts;
+      private final SSLContext sslContext;
 
       @Inject
       UntrustedSSLContextSupplier(TrustAllCerts trustAllCerts) {
-         this.trustAllCerts = trustAllCerts;
-      }
-
-      @Override
-      public SSLContext get() {
          try {
-            SSLContext sc;
-            sc = SSLContext.getInstance("SSL");
-            sc.init(null, new TrustManager[] { trustAllCerts }, new SecureRandom());
-            return sc;
+            SSLContext sslContext = SSLContext.getInstance("SSL");
+            sslContext.init(null, new TrustManager[] { trustAllCerts }, new SecureRandom());
+            this.sslContext = sslContext;
          } catch (Exception e) {
             throw Throwables.propagate(e);
          }
+      }
 
+      @Override
+      public SSLContext get() {
+         return sslContext;
       }
    }
 
diff --git a/core/src/test/java/org/jclouds/http/config/SSLModuleTest.java b/core/src/test/java/org/jclouds/http/config/SSLModuleTest.java
new file mode 100644
index 0000000..d1d2a3a
--- /dev/null
+++ b/core/src/test/java/org/jclouds/http/config/SSLModuleTest.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jclouds.http.config;
+
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.assertSame;
+
+@Test(testName = "http.config.SSLModuleTest")
+public class SSLModuleTest {
+
+    @Test
+    public void sameUntrustedSslContext() {
+        Injector injector = Guice.createInjector(new SSLModule());
+        SSLModule.UntrustedSSLContextSupplier untrustedSSLContextSupplier = injector.getInstance(SSLModule.UntrustedSSLContextSupplier.class);
+        assertSame(untrustedSSLContextSupplier.get(), untrustedSSLContextSupplier.get());
+    }
+}