Skip to content

Commit

Permalink
Added use of SSLContextFactory service to the JRE provider
Browse files Browse the repository at this point in the history
buidler...i.e. HttpClient.Builder.sslContext(SSLContext)
  • Loading branch information
scottslewis committed Dec 5, 2024
1 parent 24f512a commit 99562db
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import java.security.*;
import java.util.Optional;
import javax.net.ssl.SSLContext;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.ecf.internal.core.identity.Activator;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;

Expand Down Expand Up @@ -65,6 +68,16 @@ protected Provider findProvider(String providerName) {
if (providerName == null) {
return this.providerTracker.getService();
}
// If providerName is same as current default SSLContext then use it
SSLContext defaultContext = null;
try {
defaultContext = SSLContext.getDefault();
} catch (NoSuchAlgorithmException e) {
Activator.getDefault().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Could not get SSLContext.getDefault()", e)); //$NON-NLS-1$
}
if (defaultContext != null && providerName.equals(defaultContext.getProvider().getName())) {
return defaultContext.getProvider();
}
Optional<Provider> optResult = this.providerTracker.getTracked().values().stream().filter(p ->
// test that providerName is equal to Provider.getName()
providerName.equals(p.getName())).findFirst();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %plugin.name
Bundle-SymbolicName: org.eclipse.ecf.provider.filetransfer.httpclientjava;singleton:=true
Bundle-Version: 2.0.300.qualifier

This comment has been minimized.

Copy link
@merks

merks Dec 23, 2024

Contributor

I think something has gone seriously wrong here that will cause problems sooner rather than later. Is there a reason why the version has gone backwards?

I can see that the the 2.0.300 version has been out for a while and is available in many places:

Image

Maybe this was an accident?

This comment has been minimized.

Copy link
@scottslewis

scottslewis via email Dec 23, 2024

Author Contributor
Bundle-Version: 2.0.0.qualifier
Bundle-Vendor: %plugin.provider
Bundle-Localization: plugin
Automatic-Module-Name: org.eclipse.ecf.provider.filetransfer.httpclientjava
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@
import java.util.List;
import java.util.Map;

import javax.net.ssl.SSLSocketFactory;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.ecf.core.security.SSLContextFactory;
import org.eclipse.ecf.core.util.ECFRuntimeException;
import org.eclipse.ecf.core.util.LogHelper;
import org.eclipse.ecf.core.util.Trace;
Expand Down Expand Up @@ -118,7 +117,7 @@ public void removedService(ServiceReference<HttpClient> reference, HttpClient se

private ServiceTracker<LogService, LogService> logServiceTracker = null;

private ServiceTracker<SSLSocketFactory, SSLSocketFactory> sslSocketFactoryTracker;
private ServiceTracker<SSLContextFactory, SSLContextFactory> sslContextFactoryTracker;

private ServiceTracker<INTLMProxyHandler, INTLMProxyHandler> ntlmProxyHandlerTracker;

Expand Down Expand Up @@ -181,8 +180,8 @@ private void applyDebugOptions(BundleContext ctxt) {

@Override
public synchronized void stop(BundleContext ctxt) throws Exception {
if (sslSocketFactoryTracker != null) {
sslSocketFactoryTracker.close();
if (sslContextFactoryTracker != null) {
sslContextFactoryTracker.close();
}

if (logServiceTracker != null) {
Expand Down Expand Up @@ -247,12 +246,15 @@ public void log(IStatus status) {
}
}

public synchronized SSLSocketFactory getSSLSocketFactory() {
if (sslSocketFactoryTracker == null) {
sslSocketFactoryTracker = new ServiceTracker<SSLSocketFactory, SSLSocketFactory>(this.context, SSLSocketFactory.class, null);
sslSocketFactoryTracker.open();
/**
* @since 2.0
*/
public synchronized SSLContextFactory getSSLContextFactory() {
if (sslContextFactoryTracker == null) {
sslContextFactoryTracker = new ServiceTracker<SSLContextFactory, SSLContextFactory>(this.context, SSLContextFactory.class, null);
sslContextFactoryTracker.open();
}
SSLSocketFactory service = sslSocketFactoryTracker.getService();
SSLContextFactory service = sslContextFactoryTracker.getService();
return service;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@
import java.net.http.HttpClient;
import java.net.http.HttpClient.Redirect;
import java.net.http.HttpRequest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.time.Duration;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.ecf.core.util.Trace;
import org.eclipse.ecf.internal.provider.filetransfer.DebugOptions;
import org.eclipse.ecf.provider.filetransfer.httpclientjava.HttpClientOptions;
Expand All @@ -48,6 +52,11 @@ public class ECFHttpClientFactory implements IHttpClientFactory {
public HttpClient.Builder newClient() {

HttpClient.Builder builder = HttpClient.newBuilder().followRedirects(Redirect.NORMAL);
try {
builder.sslContext(Activator.getDefault().getSSLContextFactory().getDefault());
} catch (NoSuchAlgorithmException | NoSuchProviderException e) {
Activator.getDefault().log(new Status(IStatus.ERROR,Activator.PLUGIN_ID,"Could not set SSLContext when creating jre HttpClient", e));
}
builder = Activator.getDefault().runModifiers(builder, new ModifierRunner<HttpClient.Builder>() {
@Override
public HttpClient.Builder run(IHttpClientModifier modifier, HttpClient.Builder value) {
Expand Down

0 comments on commit 99562db

Please sign in to comment.