Connection from Java pod on Kubernetes to Mongo DB instance fails with MongoSocketReadException

2/5/2020

i have a pod with tomcat:jdk8-openjdk-slim as base image and a .war spring boot app deployed onto it. The objective is to connect to a mongo service set up outside the cluster through ssl. In my local env everything works fine, i copy the mongo certificate generated using this guide inside the container and then i can perfectly read it from Java.

When i deploy the same image on Kubernetes i get the error

com.mongodb.MongoSocketReadException: Prematurely reached end of stream
    at com.mongodb.connection.SocketStream.read(SocketStream.java:87) ~[mongo-java-driver-3.6.3.jar:na]
    at com.mongodb.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:547) ~[mongo-java-driver-3.6.3.jar:na]
    at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:418) ~[mongo-java-driver-3.6.3.jar:na]
    at com.mongodb.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:290) ~[mongo-java-driver-3.6.3.jar:na]
    at com.mongodb.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255) ~[mongo-java-driver-3.6.3.jar:na]
    at com.mongodb.connection.CommandHelper.sendAndReceive(CommandHelper.java:84) ~[mongo-java-driver-3.6.3.jar:na]
    at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:34) ~[mongo-java-driver-3.6.3.jar:na]
    at com.mongodb.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:91) ~[mongo-java-driver-3.6.3.jar:na]
    at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:51) ~[mongo-java-driver-3.6.3.jar:na]
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:127) ~[mongo-java-driver-3.6.3.jar:na]
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114) ~[mongo-java-driver-3.6.3.jar:na]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_242]

i reference to the certificate file with this line of code System.setProperty("javax.net.ssl.trustStore", certPath); where certPath is read from an env var and represents the path to the mongocert file. I also reference the cert pass from an env var.

I added logs to see if the env vars were parsed correctly and they look perfectly fine.

I aslo run kubectl exec to see if there were any problems related to corruption of certfile or permissions and i don't think this is the case (permission on file are -rwxrwxrwx 1 root root)

Is there anything that Kubernetes does behind the hoods that leads to this problem or am i missing something?

Thanks in advance

-- vsam490
java
kubernetes
mongodb
ssl

0 Answers