1

Has anyone managed to connect a java program to AWS DocumentDB where the java program is running outside of AWS and DocumentDB has tls enabled? Any examples or guidance provided would be greatly appreciated.

This is what I've done so far =>

I've been following AWS's developer guide and I understand to be able to do this I need an SSH tunnel set up to a jump box (EC2 instance) and then to the DB Cluster. I have done this and connected from my laptop.

I have then created the required .jks file from AWS's rds-combined-ca-bundle.pem file and referenced it in a basic java main class. From the java main class I have referenced the cluster as localhost:27017 as this is where I've set up the SSH tunnel from.

My test code is following the AWS example for Java and I get the following error when I run the program =>

Caused by: javax.net.ssl.SSLHandshakeException: No subject alternative DNS name matching localhost found.

public class CertsTestMain {

public static void main(String[] args) {

    String template = "mongodb://%s:%s@%s/test?ssl=true&replicaSet=rs0&readpreference=%s";
    String username = "dummy";
    String password = "dummy";
    String clusterEndpoint = "localhost:27017";
    String readPreference = "secondaryPreferred";
    String connectionString = String.format(template, username, password, clusterEndpoint, readPreference);

    String truststore = "C:/Users/eclipse-workspace/certs/certs/rds-truststore.jks";
    String truststorePassword = "test!";

    System.setProperty("javax.net.ssl.trustStore", truststore);
    System.setProperty("javax.net.ssl.trustStorePassword", truststorePassword);

    MongoClient mongoClient = MongoClients.create(connectionString);

    MongoDatabase testDB = mongoClient.getDatabase("test");
    MongoCollection<Document> bookingCollection = testDB.getCollection("booking");


    MongoCursor<Document> cursor = bookingCollection.find().iterator();
    try {
        while (cursor.hasNext()) {
            System.out.println(cursor.next().toJson());
        }
    } finally {
        cursor.close();
    }

}

}

liz french
  • 21
  • 2
  • You are familiar with SAN entires in the cert used in the handshake, if not see here for definition https://en.wikipedia.org/wiki/Subject_Alternative_Name and here for possible solutions https://stackoverflow.com/questions/8443081/how-are-ssl-certificate-server-names-resolved-can-i-add-alternative-names-using/8444863#8444863 – Nigel Savage Aug 26 '21 at 22:17

1 Answers1

1

So, for me, to make this work I only had to alter the template to:

String template = "mongodb://%s:%s@%s/test?sl=true&tlsAllowInvalidHostnames&readpreference=%s";

As long as you have created your .jks file correctly (I did this simply it by using a linux env and running the script AWS provide for Java in the following link in Point 2 => https://docs.aws.amazon.com/documentdb/latest/developerguide/connect_programmatically.html) and you have a fully working ssh tunnel as described in https://docs.aws.amazon.com/documentdb/latest/developerguide/connect-from-outside-a-vpc.html then the above code will work.

liz french
  • 21
  • 2