You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by "manoj.sahu" <ma...@yahoo.com> on 2011/04/17 22:12:22 UTC

Multiple remote connection to the same host but different users

Hi there,
I am using camel HTTP component to do an outbound connection to a remote
server.  However, depending on my inbound connection I need to communicate
to the remote server using different credentials.  The remote server uses
basicauth.  What I have noticed that camel tries to reuse the already
authenticated connection in the pool even though the subsequent request may
need to be reauthenticated using new credential.  What's the best way to
configure this in camel.  I was thinking to shutdown the connection after
each request even though it has performance penalties.  Can someone please
give me some pointers.

Here is my route:

from("direct:inbound")
    		.to("bean:requestMessageTranslator")
    		.setHeader("CamelHttpMethod", constant("POST"))
    	
.to("https://host/secure/ssl-gateway?authMethod=Basic&authUsername=username&authPassword=password")
...

The username and password gets replaced dynamically with the correct values
for each request.  But camel still tries to reuse an prior authenticated
connection, which is already in the pool I suppose.  I have not overridden
connection manager or connection manager params etc.  Using the defaults
provider by http component.

Thanks in advance for your time!


--
View this message in context: http://camel.465427.n5.nabble.com/Multiple-remote-connection-to-the-same-host-but-different-users-tp4309456p4309456.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Multiple remote connection to the same host but different users

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

Thanks. I have created a ticket to track this
https://issues.apache.org/jira/browse/CAMEL-4096

On Tue, May 24, 2011 at 6:43 AM, manoj.sahu <ma...@yahoo.com> wrote:
> Claus,
> My apologies for the delayed response.  I got pulled out of that project.
> Anyway, I did the following
>
> public class HttpProducer extends DefaultProducer implements
> ServicePoolAware {
> ...
>
> I hope that's all you wanted me to test.
>
> I reran my tests and it failed with the same error as before.  The ugly fix
> in ProducerCache class is working.
>
> Please let me know if you would like me to try anything else.
> Thanks
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Multiple-remote-connection-to-the-same-host-but-different-users-tp4309456p4420999.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/

Re: Multiple remote connection to the same host but different users

Posted by "manoj.sahu" <ma...@yahoo.com>.
Claus,
My apologies for the delayed response.  I got pulled out of that project. 
Anyway, I did the following 

public class HttpProducer extends DefaultProducer implements
ServicePoolAware {
...

I hope that's all you wanted me to test.

I reran my tests and it failed with the same error as before.  The ugly fix
in ProducerCache class is working.

Please let me know if you would like me to try anything else. 
Thanks



--
View this message in context: http://camel.465427.n5.nabble.com/Multiple-remote-connection-to-the-same-host-but-different-users-tp4309456p4420999.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Multiple remote connection to the same host but different users

Posted by Claus Ibsen <cl...@gmail.com>.
Any update on this? Did you try my suggestion?


On Tue, May 3, 2011 at 8:55 AM, Claus Ibsen <cl...@gmail.com> wrote:
> Hi
>
> Thanks for reporting back.
>
> Try instead in the camel-http component to patch by doing this:
>
> Add
>    implements ServicePoolAware
> to HttpProducer
>
> And then report back if that fixes your issue.
>
>
> On Tue, May 3, 2011 at 4:27 AM, manoj.sahu <ma...@yahoo.com> wrote:
>> After further analysis, it appears to me as a bug in apache camel.
>>
>> The bug remains in ProducerCache.doGetProducer() where a producer is
>> instantiated or retrieved from the cache. At the time of instantiating
>> producer
>>
>> Basically ProducerCache caches the Producers.  However when it retrieves the
>> producer from the cache it ignores the fact that the producer may have stale
>> credential.
>>
>> In other words following endpoints will potentially use the same  producer:
>> https://host/ssl-gateway?authMethod=Basic&authUsername=user1&authPassword=pwd1
>> https://host/ssl-gateway?authMethod=Basic&authUsername=user2&authPassword=pwd2
>>
>> I have patched the ProducerCache to fix this problem locally. Here is the
>> fix I have put.  This gets executed when the producer is retrieved from the
>> cache.
>>
>>    protected synchronized Producer doGetProducer(Endpoint endpoint, boolean
>> pooled) {
>>        String key = endpoint.getEndpointUri();
>>        Producer answer = producers.get(key);
>>        if (pooled && answer == null) {
>>            // try acquire from connection pool
>>            answer = pool.acquire(endpoint);
>>        }
>>
>>        if (answer == null) {
>>            // create a new producer
>>            try {
>>                answer = endpoint.createProducer();
>>                // must then start service so producer is ready to be used
>>                ServiceHelper.startService(answer);
>>            } catch (Exception e) {
>>                throw new FailedToCreateProducerException(endpoint, e);
>>            }
>>
>>            // add producer to cache or pool if applicable
>>            if (pooled && answer instanceof ServicePoolAware) {
>>                if (LOG.isDebugEnabled()) {
>>                    LOG.debug("Adding to producer service pool with key: " +
>> endpoint + " for producer: " + answer);
>>                }
>>                answer = pool.addAndAcquire(endpoint, answer);
>>            } else if (answer.isSingleton()) {
>>                if (LOG.isDebugEnabled()) {
>>                    LOG.debug("Adding to producer cache with key: " +
>> endpoint + " for producer: " + answer);
>>                }
>>                producers.put(key, answer);
>>            }
>>        } else {
>> // PATCH BEGIN
>>                if (endpoint instanceof HttpEndpoint && answer instanceof
>> HttpProducer){
>>                        if (((HttpEndpoint)endpoint).getHttpClientConfigurer() != null){
>>
>> ((HttpEndpoint)endpoint).getHttpClientConfigurer().configureHttpClient(((HttpProducer)answer).getHttpClient());
>>                        LOG.debug("Patched ProducerCache : " + " Refreshing the
>> authentication credential for the cached HTTP producer");
>>                        }
>>                }
>>        }
>> // PATCH END
>>        return answer;
>>    }
>>
>>
>> I am not happy about the patch due to the coupling of HttpEndpoint and
>> HttpProducer classes with the ProducerCache.  But I could not get it to work
>> otherwise.   I tried putting the fix in HttpEndpoint but it did not work
>> consistently.
>>
>> Claus/Ashwin, should this be treated as a bug? Can this be addressed in the
>> future version of the camel.  I am using version 2.4.0. However, I checked
>> the code from the latest version and it seems that the problem exists.  I am
>> sure that there is a better place other than ProducerCache where the fix can
>> be addressed.
>>
>> Thanks
>>
>> --
>> View this message in context: http://camel.465427.n5.nabble.com/Multiple-remote-connection-to-the-same-host-but-different-users-tp4309456p4366212.html
>> Sent from the Camel - Users mailing list archive at Nabble.com.
>>
>
>
>
> --
> Claus Ibsen
> -----------------
> FuseSource
> Email: cibsen@fusesource.com
> Web: http://fusesource.com
> CamelOne 2011: http://fusesource.com/camelone2011/
> Twitter: davsclaus
> Blog: http://davsclaus.blogspot.com/
> Author of Camel in Action: http://www.manning.com/ibsen/
>



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
CamelOne 2011: http://fusesource.com/camelone2011/
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/

Re: Multiple remote connection to the same host but different users

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

Thanks for reporting back.

Try instead in the camel-http component to patch by doing this:

Add
    implements ServicePoolAware
to HttpProducer

And then report back if that fixes your issue.


On Tue, May 3, 2011 at 4:27 AM, manoj.sahu <ma...@yahoo.com> wrote:
> After further analysis, it appears to me as a bug in apache camel.
>
> The bug remains in ProducerCache.doGetProducer() where a producer is
> instantiated or retrieved from the cache. At the time of instantiating
> producer
>
> Basically ProducerCache caches the Producers.  However when it retrieves the
> producer from the cache it ignores the fact that the producer may have stale
> credential.
>
> In other words following endpoints will potentially use the same  producer:
> https://host/ssl-gateway?authMethod=Basic&authUsername=user1&authPassword=pwd1
> https://host/ssl-gateway?authMethod=Basic&authUsername=user2&authPassword=pwd2
>
> I have patched the ProducerCache to fix this problem locally. Here is the
> fix I have put.  This gets executed when the producer is retrieved from the
> cache.
>
>    protected synchronized Producer doGetProducer(Endpoint endpoint, boolean
> pooled) {
>        String key = endpoint.getEndpointUri();
>        Producer answer = producers.get(key);
>        if (pooled && answer == null) {
>            // try acquire from connection pool
>            answer = pool.acquire(endpoint);
>        }
>
>        if (answer == null) {
>            // create a new producer
>            try {
>                answer = endpoint.createProducer();
>                // must then start service so producer is ready to be used
>                ServiceHelper.startService(answer);
>            } catch (Exception e) {
>                throw new FailedToCreateProducerException(endpoint, e);
>            }
>
>            // add producer to cache or pool if applicable
>            if (pooled && answer instanceof ServicePoolAware) {
>                if (LOG.isDebugEnabled()) {
>                    LOG.debug("Adding to producer service pool with key: " +
> endpoint + " for producer: " + answer);
>                }
>                answer = pool.addAndAcquire(endpoint, answer);
>            } else if (answer.isSingleton()) {
>                if (LOG.isDebugEnabled()) {
>                    LOG.debug("Adding to producer cache with key: " +
> endpoint + " for producer: " + answer);
>                }
>                producers.put(key, answer);
>            }
>        } else {
> // PATCH BEGIN
>                if (endpoint instanceof HttpEndpoint && answer instanceof
> HttpProducer){
>                        if (((HttpEndpoint)endpoint).getHttpClientConfigurer() != null){
>
> ((HttpEndpoint)endpoint).getHttpClientConfigurer().configureHttpClient(((HttpProducer)answer).getHttpClient());
>                        LOG.debug("Patched ProducerCache : " + " Refreshing the
> authentication credential for the cached HTTP producer");
>                        }
>                }
>        }
> // PATCH END
>        return answer;
>    }
>
>
> I am not happy about the patch due to the coupling of HttpEndpoint and
> HttpProducer classes with the ProducerCache.  But I could not get it to work
> otherwise.   I tried putting the fix in HttpEndpoint but it did not work
> consistently.
>
> Claus/Ashwin, should this be treated as a bug? Can this be addressed in the
> future version of the camel.  I am using version 2.4.0. However, I checked
> the code from the latest version and it seems that the problem exists.  I am
> sure that there is a better place other than ProducerCache where the fix can
> be addressed.
>
> Thanks
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Multiple-remote-connection-to-the-same-host-but-different-users-tp4309456p4366212.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
CamelOne 2011: http://fusesource.com/camelone2011/
Twitter: davsclaus
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/

Re: Multiple remote connection to the same host but different users

Posted by boday <be...@initekconsulting.com>.
this was fixed in version 2.5...see CAMEL-2945


boday wrote:
> 
> I'm looking into this, but I don't see the issue in the code
> (2.8-SNAPSHOT).  The producer cache uses the endpoint URI as the key and
> this is unique because the user/password are part of the URI...
> 
> I wrote a simple unit test and it is instantiating a different producer
> for each unique endpoint URI, etc.  Can you provide a unit test that fails
> or perhaps more details about the issue?
> 
> 


-----
Ben O'Day
IT Consultant -http://consulting-notes.com

--
View this message in context: http://camel.465427.n5.nabble.com/Multiple-remote-connection-to-the-same-host-but-different-users-tp4309456p4530587.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Multiple remote connection to the same host but different users

Posted by boday <be...@initekconsulting.com>.
I'm looking into this, but I don't see the issue in the code (2.8-SNAPSHOT). 
The producer cache uses the endpoint URI as the key and this is unique
because the user/password are part of the URI...

I wrote a simple unit test and it is instantiating a different producer for
each unique endpoint URI, etc.  Can you provide a unit test that fails or
perhaps more details about the issue?


manoj.sahu wrote:
> 
> After further analysis, it appears to me as a bug in apache camel.  
> 
> The bug remains in ProducerCache.doGetProducer() where a producer is
> instantiated or retrieved from the cache. At the time of instantiating
> producer everything is initialized properly.
> 
> Basically ProducerCache caches the Producers.  However when it retrieves
> the producer from the cache it ignores the fact that the producer may have
> stale credential. 
> 
> In other words following endpoints will potentially use the same 
> producer:
> https://host/ssl-gateway?authMethod=Basic&authUsername=user1&authPassword=pwd1
> https://host/ssl-gateway?authMethod=Basic&authUsername=user2&authPassword=pwd2
> 
> I have patched the ProducerCache to fix this problem locally. Here is the
> fix I have put.  This gets executed when the producer is retrieved from
> the cache.
> 
>     protected synchronized Producer doGetProducer(Endpoint endpoint,
> boolean pooled) {
>         String key = endpoint.getEndpointUri();
>         Producer answer = producers.get(key);
>         if (pooled && answer == null) {
>             // try acquire from connection pool
>             answer = pool.acquire(endpoint);
>         }
> 
>         if (answer == null) {
>             // create a new producer
>             try {
>                 answer = endpoint.createProducer();
>                 // must then start service so producer is ready to be used
>                 ServiceHelper.startService(answer);
>             } catch (Exception e) {
>                 throw new FailedToCreateProducerException(endpoint, e);
>             }
> 
>             // add producer to cache or pool if applicable
>             if (pooled && answer instanceof ServicePoolAware) {
>                 if (LOG.isDebugEnabled()) {
>                     LOG.debug("Adding to producer service pool with key: "
> + endpoint + " for producer: " + answer);
>                 }
>                 answer = pool.addAndAcquire(endpoint, answer);
>             } else if (answer.isSingleton()) {
>                 if (LOG.isDebugEnabled()) {
>                     LOG.debug("Adding to producer cache with key: " +
> endpoint + " for producer: " + answer);
>                 }
>                 producers.put(key, answer);
>             }
>         } else {
> *// PATCH BEGIN
> *        	if (endpoint instanceof HttpEndpoint && answer instanceof
> HttpProducer){
>         		if (((HttpEndpoint)endpoint).getHttpClientConfigurer() != null){
>             	
> ((HttpEndpoint)endpoint).getHttpClientConfigurer().configureHttpClient(((HttpProducer)answer).getHttpClient());   
>             		LOG.debug("Patched ProducerCache : " + " Refreshing the
> authentication credential for the cached HTTP producer");
>         		}
>         	}
>         }
> *// PATCH END
> *        return answer;
>     }
> 
>  
> I am not happy about the patch due to the coupling of HttpEndpoint and
> HttpProducer classes with the ProducerCache.  But I could not get it to
> work otherwise.   I tried putting the fix in HttpEndpoint but it did not
> work consistently.  
> 
> Claus/Ashwin, should this be treated as a bug? Can this be addressed in
> the future version of the camel.  I am using version 2.4.0. However, I
> checked the code from the latest version and it seems that the problem
> exists.  I am sure that there is a better place other than ProducerCache
> where the fix can be addressed.
> 
> Thanks
> 


-----
Ben O'Day
IT Consultant -http://consulting-notes.com

--
View this message in context: http://camel.465427.n5.nabble.com/Multiple-remote-connection-to-the-same-host-but-different-users-tp4309456p4515210.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Multiple remote connection to the same host but different users

Posted by "manoj.sahu" <ma...@yahoo.com>.
After further analysis, it appears to me as a bug in apache camel.  

The bug remains in ProducerCache.doGetProducer() where a producer is
instantiated or retrieved from the cache. At the time of instantiating
producer 

Basically ProducerCache caches the Producers.  However when it retrieves the
producer from the cache it ignores the fact that the producer may have stale
credential. 

In other words following endpoints will potentially use the same  producer:
https://host/ssl-gateway?authMethod=Basic&authUsername=user1&authPassword=pwd1
https://host/ssl-gateway?authMethod=Basic&authUsername=user2&authPassword=pwd2

I have patched the ProducerCache to fix this problem locally. Here is the
fix I have put.  This gets executed when the producer is retrieved from the
cache.

    protected synchronized Producer doGetProducer(Endpoint endpoint, boolean
pooled) {
        String key = endpoint.getEndpointUri();
        Producer answer = producers.get(key);
        if (pooled && answer == null) {
            // try acquire from connection pool
            answer = pool.acquire(endpoint);
        }

        if (answer == null) {
            // create a new producer
            try {
                answer = endpoint.createProducer();
                // must then start service so producer is ready to be used
                ServiceHelper.startService(answer);
            } catch (Exception e) {
                throw new FailedToCreateProducerException(endpoint, e);
            }

            // add producer to cache or pool if applicable
            if (pooled && answer instanceof ServicePoolAware) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Adding to producer service pool with key: " +
endpoint + " for producer: " + answer);
                }
                answer = pool.addAndAcquire(endpoint, answer);
            } else if (answer.isSingleton()) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Adding to producer cache with key: " +
endpoint + " for producer: " + answer);
                }
                producers.put(key, answer);
            }
        } else {
// PATCH BEGIN
        	if (endpoint instanceof HttpEndpoint && answer instanceof
HttpProducer){
        		if (((HttpEndpoint)endpoint).getHttpClientConfigurer() != null){
            	
((HttpEndpoint)endpoint).getHttpClientConfigurer().configureHttpClient(((HttpProducer)answer).getHttpClient());   
            		LOG.debug("Patched ProducerCache : " + " Refreshing the
authentication credential for the cached HTTP producer");
        		}
        	}
        }
// PATCH END
        return answer;
    }

 
I am not happy about the patch due to the coupling of HttpEndpoint and
HttpProducer classes with the ProducerCache.  But I could not get it to work
otherwise.   I tried putting the fix in HttpEndpoint but it did not work
consistently.  

Claus/Ashwin, should this be treated as a bug? Can this be addressed in the
future version of the camel.  I am using version 2.4.0. However, I checked
the code from the latest version and it seems that the problem exists.  I am
sure that there is a better place other than ProducerCache where the fix can
be addressed.

Thanks

--
View this message in context: http://camel.465427.n5.nabble.com/Multiple-remote-connection-to-the-same-host-but-different-users-tp4309456p4366212.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Multiple remote connection to the same host but different users

Posted by "manoj.sahu" <ma...@yahoo.com>.
Hi Claus,
That's exactly what I use. I use recipient list pattern. That's working as
expected.
The problem I am having is on the actual transport using HTTP component.
User A is using an authenticated connection of User B  to the same endpoint. 
It's a backend webserivce.  I am okay to reestablish connection on every
request. I am not sure how to shutdown the connection after each request is
complete.

Thanks



--
View this message in context: http://camel.465427.n5.nabble.com/Multiple-remote-connection-to-the-same-host-but-different-users-tp4309456p4311378.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Multiple remote connection to the same host but different users

Posted by Claus Ibsen <cl...@gmail.com>.
When you need a dynamic uri for an endpoint you should use the
recipient list EIP pattern.
http://camel.apache.org/recipient-list.html


On Mon, Apr 18, 2011 at 4:44 AM, manoj.sahu <ma...@yahoo.com> wrote:
> Hi Ashiwin,
> Thanks for your response.  Unfortunately I cannot do that.  First of all the
> remote server is from a Service Provider.  Their URL is fixed. Only
> connecting users need to be different.  It's the same service offered at a
> fixed endpoint.
>
> The re-authentication is needed for many different users. Hence I cannot
> statically defined in the routes. It can be n.
>
> I do understand that for connection pools it's probably not the ideal
> scenario.  Can I just close/shutdown the connection after each request in
> the worst case.  Do I have to write custom code for that.  I did not see any
> parameter that I could configure on the service route URL.
> Thanks
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Multiple-remote-connection-to-the-same-host-but-different-users-tp4309456p4309883.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
CamelOne 2011: http://fusesource.com/camelone2011/
Twitter: davsclaus
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/

Re: Multiple remote connection to the same host but different users

Posted by "manoj.sahu" <ma...@yahoo.com>.
Hi Ashiwin,
Thanks for your response.  Unfortunately I cannot do that.  First of all the
remote server is from a Service Provider.  Their URL is fixed. Only
connecting users need to be different.  It's the same service offered at a
fixed endpoint. 

The re-authentication is needed for many different users. Hence I cannot
statically defined in the routes. It can be n.

I do understand that for connection pools it's probably not the ideal
scenario.  Can I just close/shutdown the connection after each request in
the worst case.  Do I have to write custom code for that.  I did not see any
parameter that I could configure on the service route URL.  
Thanks
    

--
View this message in context: http://camel.465427.n5.nabble.com/Multiple-remote-connection-to-the-same-host-but-different-users-tp4309456p4309883.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Multiple remote connection to the same host but different users

Posted by Ashwin Karpe <ak...@fusesource.com>.
Hi,

If we are talking about 2 separate connections, can you not set up 2
different endpoints. This should enable you to create a secure connection
with different credentials and then use that to communicate with the
server.. Of course, you may need to modify your route or add a separate
route to communicate with the remote server and enable the other connection
to be used.

Cheers,

Ashwin...

 

-----
---------------------------------------------------------
Ashwin Karpe
Apache Camel Committer & Sr Principal Consultant
FUSESource (a Progress Software Corporation subsidiary)
http://fusesource.com 

Blog: http://opensourceknowledge.blogspot.com 
CamelOne 2011: http://fusesource.com/camel2011 
---------------------------------------------------------
--
View this message in context: http://camel.465427.n5.nabble.com/Multiple-remote-connection-to-the-same-host-but-different-users-tp4309456p4309637.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Multiple remote connection to the same host but different users

Posted by "manoj.sahu" <ma...@yahoo.com>.
I just want to make it clear that I have seen this behavior when I am running
integration level tests using JUnit4.  I have not tested yet running this
inside a container (Tomcat...)

--
View this message in context: http://camel.465427.n5.nabble.com/Multiple-remote-connection-to-the-same-host-but-different-users-tp4309456p4309459.html
Sent from the Camel - Users mailing list archive at Nabble.com.