Connection timeout when connecting to mongo db through groovy script through open source soap ui
I have been trying to connect to mongodb using the following groovy script through open source soap UI.
I have already plaved gmongo 1.5 jar and mongo java driver jar 3.10.1 in soap ui lib folder.
I use soap ui 5.3.0 version. Mongo db version is 3.6
I have added SSL settings in preferences, included truststore file.
-------------------------------------------------------------------------------------
import com.gmongo.GMongo
import com.mongodb.BasicDBObject
import com.mongodb.DB
import com.mongodb.DBCollection
import com.mongodb.DBCursor
import com.mongodb.*
import com.mongodb.MongoException
def javaMongo = new MongoClient(new MongoClientURI("mongodb://<username>:<password>@server:port/env?ssl=true&replicaSet=XXX"))
def db = javaMongo.getDB('XXX')
def table = db.getCollection("XXX");
def query = new BasicDBObject("XXX", "XX")
def cursor = table.find(query)
try {
while(cursor.hasNext()) {
log.info cursor.next()
}
} finally {
cursor.close()
}
--------------------------------------------------------------------------------
But I get the following error :
com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches com.mongodb.client.internal.MongoClientDelegate$1@7ff24094. Client view of cluster state is {type=REPLICA_SET, servers=[{address=XXX:XXX, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by {javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative names matching IP address XXXXX found}, caused by {java.security.cert.CertificateException: No subject alternative names matching IP address XXXXXX found}}] error at line: 25
I have tried using this script as well:
------------------------------------------------------------------
@Grab('com.gmongo:gmongo:1.5')
import com.gmongo.GMongoClient
import com.mongodb.MongoCredential
import com.mongodb.ServerAddress
import com.mongodb.BasicDBObject
import com.mongodb.*
ENV = 'XXX'
def global_config = [
stage: [ database: 'XXX', username: 'XXXX', password: 'XXXX', server: 'XXXX', port: XXX ]
]
def config = global_config[ENV];
def credential = MongoCredential.createMongoCRCredential(config.username, config.database, config.password as char[])
def mongo = new GMongoClient(new ServerAddress(config.server, config.port), [ credential ])
def db = mongo.getDB(config.database)
log.info db
def collection1 = db.getCollection("XXXXX")
log.info collection1.find().first()
--------------------------------------------------------------------
This gives me the following exception:
com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=XXXX:XX, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Exception receiving message}, caused by {java.net.SocketException: Connection reset}}] error at line: 31
Have also included changes in testrunner.bat file:
set java_opts=%java_opts% -dsoapui.https.protocols=TLSv1,TLSv1.2,SSLv3
set JAVA_OPTS=%JAVA_OPTS% -Dsoapui.browser.disabled="true"
Added this in vmoptions file:
-Dsoapui.https.protocols=SSLv3,TLSv1.2
-Dsun.security.ssl.allowUnsafeRenegotiation=true
Also imported truststore file in the project level, WS-Security Configurations tab.
Inspite of all these changes, I see the connection issue.
Please help me in resolving the errors and help in connecting to the db.
Hi,
I have solved it myself. Please find the steps below:
1) Download the pem file and generate a trust store using the following command from JDK cmd prompt
keytool -import -alias test -file <pem file path> -keystore <set path for trust store file>
2) Import the certificate to C:/Program Files/SmartBear/jre/lib/security/cacerts
Command:
keytool -import -trustcacerts -keystore "C:\Program Files\SmartBear\SoapUI-5.3.0\jre\lib\security\cacerts" -storepass <pwd> -alias <aliasname> -import -file <truststore path>
3) Open soapui and double click on the project and set the following properties
Click on truststores tab, add the path "C:\Program Files\SmartBear\SoapUI-5.3.0\jre\lib\security\cacerts", set the password
4) Open the SoapUI and navigate to tab Preferences and configure the preferences:
Give the keystore path as "C:\Program Files\SmartBear\SoapUI-5.3.0\jre\lib\security\cacerts" enter the password, enter mock trust store and mock trust store password also similar to the above one. Check the box - requires client authentication
5.Open vmoptions file and append the following properties to it.
-Dsoapui.https.protocols=SSLv3
-Dsun.security.ssl.allowUnsafeRenegotiation=true
-Djavax.net.ssl.trustStore=C:/Program Files/SmartBear/SoapUI-5.3.0/jre/lib/security/cacerts
-Djavax.net.ssl.trustStorePassword=<pwd>
-Djavax.security.auth.useSubjectCredsOnly=false
-Djavax.net.ssl.trustStoreType=JKS
Groovy Script:
import com.gmongo.GMongo
import com.mongodb.BasicDBObject
import com.mongodb.DB
import com.mongodb.DBCollection
import com.mongodb.DBCursor
import com.mongodb.*
import com.mongodb.MongoException
import java.util.Arrays
import java.util.Iterator
import org.bson.Document
import com.mongodb.MongoClient
import com.mongodb.MongoClientOptions
import com.mongodb.MongoCredential
import com.mongodb.ReadPreference
import com.mongodb.ServerAddress
import com.mongodb.client.FindIterable
import com.mongodb.client.MongoCollection
import com.mongodb.client.MongoDatabase
import com.eviware.soapui.settings.SSLSettings
import com.eviware.soapui.SoapUI
System.setProperty("javax.net.ssl.trustStore", "C:\\Program Files\\SmartBear\\SoapUI-5.3.0\\jre\\lib\\security\\cacerts")
System.setProperty("javax.net.ssl.trustStorePassword","<pwd>")
def mongoClientOptions = MongoClientOptions.builder().sslEnabled(true).sslInvalidHostNameAllowed(true).requiredReplicaSetName("<replicaset name>").readPreference(ReadPreference.primary()).build();
def mongoCredentials = MongoCredential.createCredential("<username>", "<dbname>", "<pwd>".toCharArray());
def mongoClient = new MongoClient( new ServerAddress("<IPAddress>" , <port>),
Arrays.asList(mongoCredentials),
mongoClientOptions);
def database = mongoClient.getDatabase("<dbname>")
def collection = database.getCollection("<collection name>")
def iterDoc = collection.find()
def it = iterDoc.iterator()
while (it.hasNext()) {
log.info it.next()
}