[JAVA-2229] SocketFactory overridden if `ssl=true` is used in the URI Created: 17/Jun/16  Updated: 19/Oct/16  Resolved: 30/Sep/16

Status: Closed
Project: Java Driver
Component/s: Configuration
Affects Version/s: 3.2.2
Fix Version/s: 3.4.0-rc1, 3.4.0

Type: Bug Priority: Major - P3
Reporter: Stephane Nicoll Assignee: Jeffrey Yemin
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
is duplicated by JAVA-2242 "MongoClientOptions.builder().sslEnab... Closed

 Description   

Here is a simple test that demonstrates the issue

public class Foo {
 
    public static void main(String[] args) {
        String uri = "mongodb://localhost/test?ssl=true";
        SocketFactory socketFactory = mock(SocketFactory.class);
        MongoClientOptions.Builder options = MongoClientOptions
                .builder()
                .sslEnabled(true)
                .socketFactory(socketFactory);
        MongoClient mongoClient = new MongoClient(new MongoClientURI(uri, options));
        MongoClientOptions mongoClientOptions = mongoClient.getMongoClientOptions();
        assertThat(mongoClientOptions.isSslEnabled()).isTrue();
        assertThat(mongoClientOptions.getSocketFactory()).isEqualTo(socketFactory);
    }
}

A call to sslEnabled will reset the factory.



 Comments   
Comment by Githook User [ 30/Sep/16 ]

Author:

{u'username': u'jyemin', u'name': u'Jeff Yemin', u'email': u'jeff.yemin@10gen.com'}

Message: JAVA-2229: Separate the sslEnabled and socketFactory properties in MongoClientOptions.

The socketFactory should only get a default value based on the sslEnabled property if it wasn't already explicitly set.
Consider it an error if the socket factory doesn't produce an instance of SSLSocket when SSL is set to enabled
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/ebc67bc201f604505a94efe921017c5017a3cff0

Comment by Jeffrey Yemin [ 28/Sep/16 ]

Proposed fix here: https://github.com/jyemin/mongo-java-driver/tree/j2229

Comment by Philip Dicke [ 24/Aug/16 ]

I just ran into this exact problem and spent hours debugging to figure out that ssl=true in the URL overwrites your socket factory when you call getMongoClientOptions(). This behavior is completely unexpected. I consider it a bug.

Comment by Stephane Nicoll [ 21/Jun/16 ]

Unfortunately, I don't think we can change the current behavior without breaking compatibility.

I am confused. At the very least you could avoid overwriting the SocketFactory if the specified ssl flag has the same value as the one it has currently. The example above would work for those who want to enforce SSL via the url or something. What would you break if you implemented that?

Comment by Jeffrey Yemin [ 20/Jun/16 ]

Unfortunately, I don't think we can change the current behavior without breaking compatibility.

But, note that if the application refrains from setting ssl parameter on the URI at all, it will work as expected: SSL will be enabled, and the configured socket factory will also be used.

Comment by Stephane Nicoll [ 17/Jun/16 ]

Hello,

It's not a testing issue. It's not impacting me personally: I am working on the Spring Boot project, see this issue for more context.

Comment by Jeffrey Yemin [ 17/Jun/16 ]

Hi Stephane,

I see that your example is using a mock SocketFactory. Is this bug impacting a production application, or is it a testing issue?

Regards,
Jeff

Comment by Stephane Nicoll [ 17/Jun/16 ]

One potential fix would be to only reset the factory when the value of `sslEnabled` actually changes.

Generated at Thu Feb 08 08:56:39 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.