简体   繁体   中英

AWS Device Farm - Mongo SSL Certificate Exception

Ok, I'm trying to run my Gradle-Java test project on AWS Device Farm, by zipping my entire project, choosing "Built with Appium Node.js" option, and running with: ./gradlew test --tests MyTestRunner --debug But I'm getting next error when I try to connect with MongoDB via URI: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target.

I'm able to make connection on my local machine. This is my method:

public static void connectToMongo(){
    String uri = "mongodb+srv://user:pass@server";
    MongoClient client = MongoClients.create(uri);
    MongoDatabase database = client.getDatabase("database");
    MongoCollection<Document> myCollection = database.getCollection("collection");
    Document document = myCollection.find(
        eq("email", "myEmail")
    ).first();
    System.out.println(document.get("state").toString());
}

I'm using next tools:

  • Jdk 1.8.0_211
  • Gradle 6.5

And these are my dependencies:

dependencies {
    compile 'org.mongodb:mongodb-driver-sync:4.5.0'
    compile group:'io.cucumber', name:'cucumber-java', version:'4.2.0'
    compile group:'io.cucumber', name:'cucumber-junit', version:'4.2.0'
    compile group: 'com.github.javafaker', name: 'javafaker', version: '1.0.2'
}

I've tried next things:

  • Adding options to the URI: mongodb+srv://user:pass@server?ssl=true&invalidHostNameAllowed=true
  • Adding higher jdk versions to the zip ( jdk-17.0.2 and corretto-11.0.8 ), and forcing to run by adding this to the build.gradle: compileJava.options.fork=true compileJava.options.forkOptions.executable='jdk-17.0.2/bin/javac'
  • Copying my local jdk cacerts and adding a gradle.properties: systemProp.javax.net.ssl.trustStore=cacerts systemProp.javax.net.ssl.trustStorePassword=changeit
  • Changing mongo-driver dependency: compile 'org.mongodb:mongodb-driver-sync:4.0.5' compile 'org.mongodb:mongodb-driver-sync:4.5.0' implementation group:'org.mongodb',name:'mongo-java-driver',version:'3.12.11'

Full stacktrace: 2022-04-25T23:11:47.423+0000 [DEBUG] [TestEventLogger] com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches com.mongodb.client.internal.MongoClientDelegate$1@64a30f91. Client view of cluster state is {type=XXXXXX, servers=[{address=XXXXXX:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by {javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}, caused by {sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}, caused by {sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}}, {address=XXXXXX:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by {javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}, caused by {sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}, caused by {sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}}, {address=XXXXXX:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by {javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}, caused by {sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}, caused by {sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}}] 2022-04-25T23:11:47.423+0000 [DEBUG] [TestEventLogger] at com.mongodb.internal.connection.BaseCluster.createTimeoutException(BaseCluster.java:403) 2022-04-25T23:11:47.423+0000 [DEBUG] [TestEventLogger] at com.mongodb.internal.connection.BaseCluster.selectServer(BaseCluster.java:118) 2022-04-25T23:11:47.423+0000 [DEBUG] [TestEventLogger] at com.mongodb.internal.connection.AbstractMultiServerCluster.selectServer(AbstractMultiServerCluster.java:54) 2022-04-25T23:11:47.423+0000 [DEBUG] [TestEventLogger] at com.mongodb.client.internal.MongoClientDelegate.getConnectedClusterDescription(MongoClientDelegate.java:149) 2022-04-25T23:11:47.423+0000 [DEBUG] [TestEventLogger] at com.mongodb.client.internal.MongoClientDelegate.createClientSession(MongoClientDelegate.java:98) 2022-04-25T23:11:47.423+0000 [DEBUG] [TestEventLogger] at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.getClientSession(MongoClientDelegate.java:278) 2022-04-25T23:11:47.423+0000 [DEBUG] [TestEventLogger] at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:182) 2022-04-25T23:11:47.423+0000 [DEBUG] [TestEventLogger] at com.mongodb.client.internal.FindIterableImpl.first(FindIterableImpl.java:189) 2022-04-25T23:11:47.423+0000 [DEBUG] [TestEventLogger] com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches com.mongodb.client.internal.MongoClientDelegate$1@64a30f91. Client view of cluster state is {type=XXXXXX, servers=[{address=XXXXXX:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by {javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}, caused by {sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}, caused by {sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}}, {address=XXXXXX:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by {javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}, caused by {sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}, caused by {sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}}, {address=XXXXXX:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by {javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}, caused by {sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}, caused by {sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}}] 2022-04-25T23:11:47.423+0000 [DEBUG] [TestEventLogger] at com.mongodb.internal.connection.BaseCluster.createTimeoutException(BaseCluster.java:403) 2022-04-25T23:11:47.423+0000 [DEBUG] [TestEventLogger] at com.mongodb.internal.connection.BaseCluster.selectServer(BaseCluster.java:118) 2022-04-25T23:11:47.423+0000 [DEBUG] [TestEventLogger] at com.mongodb.internal.connection.AbstractMultiServerCluster.selectServer(AbstractMultiServerCluster.java:54) 2022-04-25T23:11:47.423+0000 [DEBUG] [TestEventLogger] at com.mongodb.client.internal.MongoClientDelegate.getConnectedClusterDescription(MongoClientDelegate.java:149) 2022-04-25T23:11:47.423+0000 [DEBUG] [TestEventLogger] at com.mongodb.client.internal.MongoClientDelegate.createClientSession(MongoClientDelegate.java:98) 2022-04-25T23:11:47.423+0000 [DEBUG] [TestEventLogger] at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.getClientSession(MongoClientDelegate.java:278) 2022-04-25T23:11:47.423+0000 [DEBUG] [TestEventLogger] at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:182) 2022-04-25T23:11:47.423+0000 [DEBUG] [TestEventLogger] at com.mongodb.client.internal.FindIterableImpl.first(FindIterableImpl.java:189)

Solved. This problem ocurred due to outdated jdk version on AWS Device Farm: jdk1.8.0_65

For someone looking for some fix: To solve this problem, I've included jdk1.8.0_241 on project zip, and changed env JAVA_HOME: export JAVA_HOME="$DEVICEFARM_TEST_PACKAGE_PATH/MyProject/jdk1.8.0_241"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM