camel-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Julian Cable (JIRA)" <j...@apache.org>
Subject [jira] [Comment Edited] (CAMEL-8272) Camel-box socks proxy implementation is incomplete
Date Fri, 23 Jan 2015 11:50:34 GMT

    [ https://issues.apache.org/jira/browse/CAMEL-8272?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14289126#comment-14289126
] 

Julian Cable edited comment on CAMEL-8272 at 1/23/15 11:49 AM:
---------------------------------------------------------------

{quote}
Index: src/main/java/org/apache/camel/component/box/internal/BoxClientHelper.java
===================================================================
--- src/main/java/org/apache/camel/component/box/internal/BoxClientHelper.java  (revision
1598081)
+++ src/main/java/org/apache/camel/component/box/internal/BoxClientHelper.java  (working copy)
@@ -36,12 +36,19 @@
 import org.apache.camel.component.box.BoxConfiguration;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.jsse.SSLContextParameters;
+import org.apache.http.HttpHost;
 import org.apache.http.client.HttpClient;
 import org.apache.http.conn.ClientConnectionManager;
 import org.apache.http.conn.scheme.Scheme;
 import org.apache.http.conn.scheme.SchemeRegistry;
 import org.apache.http.conn.ssl.SSLSocketFactory;
+import org.apache.http.conn.params.ConnRoutePNames;
 import org.apache.http.params.HttpParams;
+import org.apache.http.protocol.HttpContext;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.net.Proxy;
+import javax.net.ssl.SSLContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;

@@ -66,7 +73,7 @@
         final String userPassword = configuration.getUserPassword();

         if ((authSecureStorage == null && ObjectHelper.isEmpty(userPassword))
-            || ObjectHelper.isEmpty(userName) || ObjectHelper.isEmpty(clientId) || ObjectHelper.isEmpty(clientSecret))
{
+                || ObjectHelper.isEmpty(userName) || ObjectHelper.isEmpty(clientId) || ObjectHelper.isEmpty(clientSecret))
{
             throw new IllegalArgumentException(
                 "Missing one or more required properties "
                 + "clientId, clientSecret, userName and either authSecureStorage or userPassword");
@@ -76,13 +83,13 @@
         // if set, use configured connection manager builder
         final BoxConnectionManagerBuilder connectionManagerBuilder = configuration.getConnectionManagerBuilder();
         final BoxConnectionManagerBuilder connectionManager = connectionManagerBuilder !=
null
-            ? connectionManagerBuilder : new BoxConnectionManagerBuilder();
+                ? connectionManagerBuilder : new BoxConnectionManagerBuilder();

         // create REST client for BoxClient
         final ClientConnectionManager[] clientConnectionManager = new ClientConnectionManager[1];
         final IBoxRESTClient restClient = new BoxRESTClient(connectionManager.build()) {
-                       @SuppressWarnings("deprecation")
-                       @Override
+            @SuppressWarnings("deprecation")
+            @Override
             public HttpClient getRawHttpClient() {
                 final HttpClient httpClient = super.getRawHttpClient();
                 clientConnectionManager[0] = httpClient.getConnectionManager();
@@ -91,33 +98,48 @@
                 if (sslContextParameters == null) {
                     sslContextParameters = new SSLContextParameters();
                 }
-                try {
-                    final SSLSocketFactory socketFactory = new SSLSocketFactory(
-                        sslContextParameters.createSSLContext(),
-                        SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
-                    schemeRegistry.register(new Scheme("https", socketFactory, 443));
-                } catch (GeneralSecurityException e) {
-                    throw ObjectHelper.wrapRuntimeCamelException(e);
-                } catch (IOException e) {
-                    throw ObjectHelper.wrapRuntimeCamelException(e);
-                }
-
-                // set custom HTTP params
                 final Map<String, Object> configParams = configuration.getHttpParams();
+                boolean useSocksProxy = false;
+                HttpHost proxyHost = null;
                 if (configParams != null && !configParams.isEmpty()) {
+                    final Boolean socksProxy = (Boolean) configParams.get("http.route.socks-proxy");
+                   if(socksProxy!=null && socksProxy) {
+                       useSocksProxy = true;
+                        proxyHost = (HttpHost) configParams.get(ConnRoutePNames.DEFAULT_PROXY);
+                    }
+                    // set custom HTTP params
                     LOG.debug("Setting {} HTTP Params", configParams.size());

                     final HttpParams httpParams = httpClient.getParams();
                     for (Map.Entry<String, Object> param : configParams.entrySet())
{
-                        httpParams.setParameter(param.getKey(), param.getValue());
+                       // don't add proxy params if socks
+                        if(!(useSocksProxy && (param.getKey().equals("http.route.socks-proxy")
|| param.getKey().equals(ConnRoutePNames.DEFAULT_PROXY)))) {
+                            httpParams.setParameter(param.getKey(), param.getValue());
+                        }
                     }
+
                 }
+               SSLContext sslContext = null;
+                try {
+                        sslContext = sslContextParameters.createSSLContext();
+                } catch (IOException e) {
+                    throw ObjectHelper.wrapRuntimeCamelException(e);
+                } catch (GeneralSecurityException e) {
+                    throw ObjectHelper.wrapRuntimeCamelException(e);
+                }
+                final SSLSocketFactory socketFactory = useSocksProxy?
+                    new SocksSSLSocketFactory(sslContext, proxyHost)
+               :
+                    new SSLSocketFactory(
+                        sslContext,
+                        SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
+                schemeRegistry.register(new Scheme("https", socketFactory, 443));

                 return httpClient;
             }
         };
         final BoxClient boxClient = new BoxClient(clientId, clientSecret, null, null,
-            restClient, configuration.getBoxConfig());
+                restClient, configuration.getBoxConfig());

         // enable OAuth auto-refresh
         boxClient.setAutoRefreshOAuth(true);
@@ -135,7 +157,7 @@
     }

     public static void getOAuthToken(BoxConfiguration configuration, CachedBoxClient cachedBoxClient)
-        throws AuthFatalFailureException, BoxRestException, BoxServerException, InterruptedException
{
+    throws AuthFatalFailureException, BoxRestException, BoxServerException, InterruptedException
{

         final BoxClient boxClient = cachedBoxClient.getBoxClient();
         synchronized (boxClient) {
@@ -169,7 +191,7 @@
                 final Exception ex = listener.getException();
                 if (ex != null) {
                     throw new RuntimeCamelException(String.format("Login error for %s: %s",
-                        cachedBoxClient, ex.getMessage()), ex);
+                                                    cachedBoxClient, ex.getMessage()), ex);
                 }
             }

@@ -188,7 +210,7 @@
     }

     public static void shutdownBoxClient(BoxConfiguration configuration, CachedBoxClient
cachedBoxClient)
-        throws BoxServerException, BoxRestException, AuthFatalFailureException {
+    throws BoxServerException, BoxRestException, AuthFatalFailureException {

         final BoxClient boxClient = cachedBoxClient.getBoxClient();
         synchronized (boxClient) {
@@ -217,7 +239,7 @@
     }

     private static void revokeOAuthToken(BoxConfiguration configuration, CachedBoxClient
cachedBoxClient)
-        throws BoxServerException, BoxRestException, AuthFatalFailureException {
+    throws BoxServerException, BoxRestException, AuthFatalFailureException {

         final BoxClient boxClient = cachedBoxClient.getBoxClient();
         synchronized (boxClient) {
@@ -228,7 +250,7 @@

                 // revoke OAuth token
                 boxClient.getOAuthManager().revokeOAuth(boxClient.getAuthData().getAccessToken(),
-                    configuration.getClientId(), configuration.getClientSecret());
+                                                        configuration.getClientId(), configuration.getClientSecret());

                 // notify the OAuthListener of revoked token
                 cachedBoxClient.getListener().onRefresh(null);
@@ -237,4 +259,20 @@
             }
         }
     }
+    static class SocksSSLSocketFactory extends SSLSocketFactory {
+        HttpHost proxyHost;
+
+        public SocksSSLSocketFactory(SSLContext sslContext, HttpHost proxyHost) {
+            super(sslContext, SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
+            this.proxyHost = proxyHost;
+        }
+
+        @Override
+        public Socket createSocket(final HttpContext context) throws IOException {
+            InetSocketAddress socksaddr = new InetSocketAddress(proxyHost.getHostName(),
proxyHost.getPort());
+            Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksaddr);
+            return new Socket(proxy);
+        }
+
+    }
 }
{quote}



was (Author: julian.cable):
Index: src/main/java/org/apache/camel/component/box/internal/BoxClientHelper.java
===================================================================
--- src/main/java/org/apache/camel/component/box/internal/BoxClientHelper.java  (revision
1598081)
+++ src/main/java/org/apache/camel/component/box/internal/BoxClientHelper.java  (working copy)
@@ -36,12 +36,19 @@
 import org.apache.camel.component.box.BoxConfiguration;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.jsse.SSLContextParameters;
+import org.apache.http.HttpHost;
 import org.apache.http.client.HttpClient;
 import org.apache.http.conn.ClientConnectionManager;
 import org.apache.http.conn.scheme.Scheme;
 import org.apache.http.conn.scheme.SchemeRegistry;
 import org.apache.http.conn.ssl.SSLSocketFactory;
+import org.apache.http.conn.params.ConnRoutePNames;
 import org.apache.http.params.HttpParams;
+import org.apache.http.protocol.HttpContext;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.net.Proxy;
+import javax.net.ssl.SSLContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;

@@ -66,7 +73,7 @@
         final String userPassword = configuration.getUserPassword();

         if ((authSecureStorage == null && ObjectHelper.isEmpty(userPassword))
-            || ObjectHelper.isEmpty(userName) || ObjectHelper.isEmpty(clientId) || ObjectHelper.isEmpty(clientSecret))
{
+                || ObjectHelper.isEmpty(userName) || ObjectHelper.isEmpty(clientId) || ObjectHelper.isEmpty(clientSecret))
{
             throw new IllegalArgumentException(
                 "Missing one or more required properties "
                 + "clientId, clientSecret, userName and either authSecureStorage or userPassword");
@@ -76,13 +83,13 @@
         // if set, use configured connection manager builder
         final BoxConnectionManagerBuilder connectionManagerBuilder = configuration.getConnectionManagerBuilder();
         final BoxConnectionManagerBuilder connectionManager = connectionManagerBuilder !=
null
-            ? connectionManagerBuilder : new BoxConnectionManagerBuilder();
+                ? connectionManagerBuilder : new BoxConnectionManagerBuilder();

         // create REST client for BoxClient
         final ClientConnectionManager[] clientConnectionManager = new ClientConnectionManager[1];
         final IBoxRESTClient restClient = new BoxRESTClient(connectionManager.build()) {
-                       @SuppressWarnings("deprecation")
-                       @Override
+            @SuppressWarnings("deprecation")
+            @Override
             public HttpClient getRawHttpClient() {
                 final HttpClient httpClient = super.getRawHttpClient();
                 clientConnectionManager[0] = httpClient.getConnectionManager();
@@ -91,33 +98,48 @@
                 if (sslContextParameters == null) {
                     sslContextParameters = new SSLContextParameters();
                 }
-                try {
-                    final SSLSocketFactory socketFactory = new SSLSocketFactory(
-                        sslContextParameters.createSSLContext(),
-                        SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
-                    schemeRegistry.register(new Scheme("https", socketFactory, 443));
-                } catch (GeneralSecurityException e) {
-                    throw ObjectHelper.wrapRuntimeCamelException(e);
-                } catch (IOException e) {
-                    throw ObjectHelper.wrapRuntimeCamelException(e);
-                }
-
-                // set custom HTTP params
                 final Map<String, Object> configParams = configuration.getHttpParams();
+                boolean useSocksProxy = false;
+                HttpHost proxyHost = null;
                 if (configParams != null && !configParams.isEmpty()) {
+                    final Boolean socksProxy = (Boolean) configParams.get("http.route.socks-proxy");
+                   if(socksProxy!=null && socksProxy) {
+                       useSocksProxy = true;
+                        proxyHost = (HttpHost) configParams.get(ConnRoutePNames.DEFAULT_PROXY);
+                    }
+                    // set custom HTTP params
                     LOG.debug("Setting {} HTTP Params", configParams.size());

                     final HttpParams httpParams = httpClient.getParams();
                     for (Map.Entry<String, Object> param : configParams.entrySet())
{
-                        httpParams.setParameter(param.getKey(), param.getValue());
+                       // don't add proxy params if socks
+                        if(!(useSocksProxy && (param.getKey().equals("http.route.socks-proxy")
|| param.getKey().equals(ConnRoutePNames.DEFAULT_PROXY)))) {
+                            httpParams.setParameter(param.getKey(), param.getValue());
+                        }
                     }
+
                 }
+               SSLContext sslContext = null;
+                try {
+                        sslContext = sslContextParameters.createSSLContext();
+                } catch (IOException e) {
+                    throw ObjectHelper.wrapRuntimeCamelException(e);
+                } catch (GeneralSecurityException e) {
+                    throw ObjectHelper.wrapRuntimeCamelException(e);
+                }
+                final SSLSocketFactory socketFactory = useSocksProxy?
+                    new SocksSSLSocketFactory(sslContext, proxyHost)
+               :
+                    new SSLSocketFactory(
+                        sslContext,
+                        SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
+                schemeRegistry.register(new Scheme("https", socketFactory, 443));

                 return httpClient;
             }
         };
         final BoxClient boxClient = new BoxClient(clientId, clientSecret, null, null,
-            restClient, configuration.getBoxConfig());
+                restClient, configuration.getBoxConfig());

         // enable OAuth auto-refresh
         boxClient.setAutoRefreshOAuth(true);
@@ -135,7 +157,7 @@
     }

     public static void getOAuthToken(BoxConfiguration configuration, CachedBoxClient cachedBoxClient)
-        throws AuthFatalFailureException, BoxRestException, BoxServerException, InterruptedException
{
+    throws AuthFatalFailureException, BoxRestException, BoxServerException, InterruptedException
{

         final BoxClient boxClient = cachedBoxClient.getBoxClient();
         synchronized (boxClient) {
@@ -169,7 +191,7 @@
                 final Exception ex = listener.getException();
                 if (ex != null) {
                     throw new RuntimeCamelException(String.format("Login error for %s: %s",
-                        cachedBoxClient, ex.getMessage()), ex);
+                                                    cachedBoxClient, ex.getMessage()), ex);
                 }
             }

@@ -188,7 +210,7 @@
     }

     public static void shutdownBoxClient(BoxConfiguration configuration, CachedBoxClient
cachedBoxClient)
-        throws BoxServerException, BoxRestException, AuthFatalFailureException {
+    throws BoxServerException, BoxRestException, AuthFatalFailureException {

         final BoxClient boxClient = cachedBoxClient.getBoxClient();
         synchronized (boxClient) {
@@ -217,7 +239,7 @@
     }

     private static void revokeOAuthToken(BoxConfiguration configuration, CachedBoxClient
cachedBoxClient)
-        throws BoxServerException, BoxRestException, AuthFatalFailureException {
+    throws BoxServerException, BoxRestException, AuthFatalFailureException {

         final BoxClient boxClient = cachedBoxClient.getBoxClient();
         synchronized (boxClient) {
@@ -228,7 +250,7 @@

                 // revoke OAuth token
                 boxClient.getOAuthManager().revokeOAuth(boxClient.getAuthData().getAccessToken(),
-                    configuration.getClientId(), configuration.getClientSecret());
+                                                        configuration.getClientId(), configuration.getClientSecret());

                 // notify the OAuthListener of revoked token
                 cachedBoxClient.getListener().onRefresh(null);
@@ -237,4 +259,20 @@
             }
         }
     }
+    static class SocksSSLSocketFactory extends SSLSocketFactory {
+        HttpHost proxyHost;
+
+        public SocksSSLSocketFactory(SSLContext sslContext, HttpHost proxyHost) {
+            super(sslContext, SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
+            this.proxyHost = proxyHost;
+        }
+
+        @Override
+        public Socket createSocket(final HttpContext context) throws IOException {
+            InetSocketAddress socksaddr = new InetSocketAddress(proxyHost.getHostName(),
proxyHost.getPort());
+            Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksaddr);
+            return new Socket(proxy);
+        }
+
+    }
 }


> Camel-box socks proxy implementation is incomplete
> --------------------------------------------------
>
>                 Key: CAMEL-8272
>                 URL: https://issues.apache.org/jira/browse/CAMEL-8272
>             Project: Camel
>          Issue Type: Bug
>    Affects Versions: 2.14.1
>         Environment: any
>            Reporter: Julian Cable
>
> org.apache.camel.component.box.internal.LoginAuthFlowUI looks for http.route.socks-proxy
and sets up a socks proxy for the webClient
> org.apache.camel.component.box.internal.BoxClientHelper just passes the httpParams on
to the underlying HttpClient but the box api uses a vanilla DefaultHttpClient which doesn't
talk SOCKS.
> The attached patch adds socks proxy support to the main restful box transactions.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message