AWS设备场- Mongo SSL证书异常



好的,我试图运行我的Gradle-Java测试项目在AWS设备农场,通过压缩我的整个项目,选择" build with Appium node . js&;选项,并运行:./gradlew test --tests MyTestRunner --debug但是当我尝试通过URI与MongoDB连接时,我会得到下一个错误: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.

我可以在我的本地机器上建立连接。这是我的方法:

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());
}

我正在使用下一个工具:

Jdk 1.8.0_211
  • Gradle 6.5

这些是我的依赖项:

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'
}

我已经尝试了下一步:

  • 为URI添加选项:mongodb+srv://user:pass@server?ssl=true&invalidHostNameAllowed=true
  • 将更高版本的jdk添加到zip (jdk-17.0.2corretto-11.0.8),并通过将此添加到build.gradle中强制运行:compileJava.options.fork=truecompileJava.options.forkOptions.executable='jdk-17.0.2/bin/javac'
  • 复制我的本地jdk cacerts并添加gradle.properties:systemProp.javax.net.ssl.trustStore=cacerts systemProp.javax.net.ssl.trustStorePassword=changeit
  • 更改mongo-driver依赖: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'

全部加: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)

解决。此问题是由于AWS Device Farm上的jdk版本过时导致的:jdk1.8.0_65

对于寻找一些修复的人:为了解决这个问题,我在项目zip中包含了jdk1.8.0_241,并更改了env JAVA_HOME:export JAVA_HOME="$DEVICEFARM_TEST_PACKAGE_PATH/MyProject/jdk1.8.0_241"

最新更新