From 6d389b0d8692fd660e77029fde40e5bb363ff6f8 Mon Sep 17 00:00:00 2001 From: roded Date: Wed, 16 Oct 2019 10:48:47 +0300 Subject: [PATCH] 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. --- .../org/jclouds/http/config/SSLModule.java | 20 +++++------ .../jclouds/http/config/SSLModuleTest.java | 34 +++++++++++++++++++ 2 files changed, 43 insertions(+), 11 deletions(-) create mode 100644 core/src/test/java/org/jclouds/http/config/SSLModuleTest.java 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 eba1b472bb..8ec4810df1 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 { - private final TrustAllCerts trustAllCerts; + private final SSLContext sslContext; @Inject UntrustedSSLContextSupplier(TrustAllCerts trustAllCerts) { - this.trustAllCerts = trustAllCerts; + try { + 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() { - try { - SSLContext sc; - sc = SSLContext.getInstance("SSL"); - sc.init(null, new TrustManager[] { trustAllCerts }, new SecureRandom()); - return sc; - } catch (Exception e) { - throw Throwables.propagate(e); - } - + 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 0000000000..d1d2a3a3e2 --- /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()); + } +}