Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-2242

"MongoClientOptions.builder().sslEnabled(true)" modifies the SSL factory

    • Type: Icon: Improvement Improvement
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.2.2
    • Component/s: None

      "MongoClientOptions.builder().sslEnabled(true)" modifies the SSL factory.

      https://github.com/mongodb/mongo-java-driver/blob/08f2160f30287e67d2258f31af0fbb203897c8ba/driver/src/main/com/mongodb/MongoClientOptions.java#L1041

      While this is stated in the JavaDoc comment, it's actually not the best idea.

      It breaks the builder pattern since it introduces a required order of operations on the builder. Moreover, it's simply counter intuitive as "ssl=true" is a well-known flag and nobody actually expects the method "sslEnabled()" to do other things than setting this configuration value.

      So, this will work:

      SSLContext sslContext=SSLContext.getInstance("TLS");
      sslContext.init(null,trustManagers,new SecureRandom());
      
      MongoClientOptions options=MongoClientOptions.builder().
                      sslEnabled(true).
                      sslInvalidHostNameAllowed(true).
                      socketFactory(sslContext.getSocketFactory()).
                      build();
      

      While this will not:

      SSLContext sslContext=SSLContext.getInstance("TLS");
      sslContext.init(null,trustManagers,new SecureRandom());
      
      MongoClientOptions options=MongoClientOptions.builder().
                      socketFactory(sslContext.getSocketFactory()).
                      sslEnabled(true).
                      sslInvalidHostNameAllowed(true).
                      build();
      

      The better solution would be to remove this hidden dependency and set the default factory in "build()" in case it's needed and not explicitly set by the developer.

            Assignee:
            Unassigned Unassigned
            Reporter:
            Imifos Tasha Carl [X]
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: