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

Implementing X509 authentication running into socket exceptions

    • Type: Icon: Task Task
    • Resolution: Gone away
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 4.0.1
    • Component/s: Configuration
    • None
    • Environment:
      Production

      MongodbConfiguration.java
      package com.imports.ci;
      import java.security.NoSuchAlgorithmException;

      import java.util.Arrays;
      import org.bson.Document;
      import org.slf4j.Logger;
      import org.slf4j.LoggerFactory;
      import org.springframework.beans.factory.annotation.Value;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
      import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
      import com.mongodb.MongoClient;
      import com.mongodb.MongoClientOptions;
      import com.mongodb.MongoCredential;
      import com.mongodb.ServerAddress;
      import com.mongodb.client.MongoCollection;
      import com.mongodb.client.MongoDatabase;

      @Configuration

      @EnableMongoRepositories("com.imports.ci")
      public class MongodbConfiguration extends AbstractMongoConfiguration {
             private final Logger logger = LoggerFactory.getLogger(this.getClass());
             @Value("${ssl.mongodb.host}")

             private String host;

             @Value("${ssl.mongodb.port}")

             private Integer port;

             @Value("${ssl.mongodb.username}")

             private String username;

             @Value("${ssl.mongodb.database}")

             private String database;

             @Value("${ssl.mongodb.password}")

             private String password;

             @Value("${ssl.mongodb.javax.net.ssl.trustStore.path}")

             private String trustStorePath;

             @Value("${ssl.mongodb.javax.net.ssl.trustStore.password}")

             private String trustStorePassword;

             @Override

             public MongoClient mongo() throws NoSuchAlgorithmException {
                   logger.info("Inside MongodbConfiguration.mongo Method ");
                   MongoCredential credential = MongoCredential.createMongoX509Credential(
                                 "CN=XXXX.com,OU=XX,O=XX,L=XX,ST=NC,C=US");
                   System.setProperty("javax.net.ssl.keyStore", trustStorePath);

                   System.setProperty("javax.net.ssl.keyStorePassword", "changeit");
                   logger.info("credential ::{}", credential);
                   MongoClientOptions options = new MongoClientOptions.Builder().sslEnabled(true).socketKeepAlive(true).build();

                   MongoClient client = new MongoClient(new ServerAddress(host), Arrays.asList(credential), options);
                   MongoDatabase db = client.getDatabase(database);

                   MongoCollection<Document> collection = db.getCollection("commercial_invoice_staging");
                   logger.info("No of Documents in orders collection: " + collection.count());
                   return client;

             }

             @Override
             protected String getDatabaseName() {

                   return database;

             }}

      POM.XMLPOM.XML
      <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.13.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent>
      <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>

      Application.properties:

      spring.data.mongodb.authentication-database=admin

      ssl.mongodb.javax.net.ssl.trustStore.path=/usr/iptservice/iptci/mnt/ /truststore/mongoStore.ts

      ssl.mongodb.javax.net.ssl.trustStore.password=XX

      ssl.mongodb.host=testdev001

      ssl.mongodb.port=27017

      ssl.mongodb.username=TestDev

      ssl.mongodb.database=admin

      ssl.mongodb.password=xyz

      ssl.mongodb.certificate.username=CN=XXXX.com,OU=XX,O=XX,L=XX,ST=NC,C=US

        1. screenshot-1.png
          60 kB
          Bhaskar Avisha

            Assignee:
            jeff.yemin@mongodb.com Jeffrey Yemin
            Reporter:
            developer.avisha@gmail.com Bhaskar Avisha
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: