[JAVA-2486] SSL encryption with MongoClient using TLS 1.2 Created: 05/Apr/17  Updated: 27/Oct/23  Resolved: 22/Jun/17

Status: Closed
Project: Java Driver
Component/s: Configuration
Affects Version/s: None
Fix Version/s: None

Type: Task Priority: Minor - P4
Reporter: Santosh Kumar Assignee: Unassigned
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

prod



 Description   

I am forcing the MongoClient to use SSL with TLS 1.2. Here is the code . I am getting the error: incompatible types when I am passing to the MongoClientOptions.Builder.

Is there a better approach for enforcing TLS 1.2 for the client connection . I am using java driver 3.4.2 connecting to mongoDB 3.0.6.

  SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
    MongoClientOptions.Builder builder = MongoClientOptions.builder().socketFactory((SSLSocketFactory)sslContext.getSocketFactory()).build();
        MongoClientOptions sslOptions builder.sslEnabled(true).sslInvalidHostNameAllowed(true).build();

java:87: error: incompatible types
        MongoClientOptions.Builder builder = MongoClientOptions.builder().socketFactory(sslContext.getSocketFactory()).build();
                                                                                                                            ^
  required: Builder
  found:    MongoClientOptions
1 error



 Comments   
Comment by Jeffrey Yemin [ 05/Apr/17 ]

You should only call MongoClientOptions.Builder#build once:

        SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
        MongoClientOptions options = MongoClientOptions.builder()
                                             .sslEnabled(true)
                                             .sslInvalidHostNameAllowed(true)
                                             .socketFactory(sslContext.getSocketFactory())
                                             .build();    // just one call to build at the very end

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