Connect to MongoDb using X509 certificate - mongodb

I am trying to connect to MongoDB using mongoX509. I am using mongo java driver 3.3.0 jar. In api reference I can see MongoCredential to pass subject name and authenticate but i am not able to import this in my code. When I decompile the jar I am not able to see MongoCredential as well.
Am I missing any dependencies. Is there any easy way to connect to Mongodb without using MongoCredential? The details i have is server, port and certificate subject name?
I can post the code I am trying if anyone wants to take a look at it as well
Thanks in Advance
Code - I am using SoapUI to run this code
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoCredential;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
try{
def subjectName="CN=xx,OU=xx,O=xx,C=US,ST=CA,L=xx"
MongoCredential credential = MongoCredential.createMongoX509Credential(subjectName);
def URI = "mongodb://server1:27017,server2:27017,server3:27017/<<database>>?replicaSet=<<XYZ>>&authMechanism=MONGODB-X509&ssl=true"
MongoClientURI uri = new MongoClientURI(URI)
MongoClient client = new MongoClient(uri, Arrays.asList(credential));
DB database = client.getDB(<<database>>);
collection = database.isAuthenticated();
log.info collection
}
catch (Exception e){
log.info e
}

The issue was because of incorrect jar that I was using. I got the correct version and it worked.

Related

MongoDB Jmeter with SSL File

So I'm following the guide over on BlazeMeter about how to load test MongoDB with Jmeter here
https://www.blazemeter.com/blog/mongodb-performance-testing-with-jmeter/
In my case I do have a connection string because our Mongo Deployment is using a replicaset. No biggie I just hardcoded it into the MongoClient section as I was having issues with passing it through as a variable.
But here is the deal. Our Mongo servers use a SSL file as a part of the authentication mechanism.
Is there a way I can just plug the SSL into a variable inside of jmeter and then have it be set properly in the database connection portion of the load test?
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.MongoClientSettings;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import java.util.Arrays;
try {
MongoClient mongoClient = MongoClients.create("MY_CONNECTION_STRING");
MongoDatabase database = mongoClient.getDatabase(vars.get("databaseName"));
MongoCollection<Document> collection = database.getCollection(vars.get("collectionName"));
vars.putObject("collection", collection);
return "Connected to " + vars.get("collectionName");
}
catch (Exception e) {
SampleResult.setSuccessful(false);
SampleResult.setResponseCode("500");
SampleResult.setResponseMessage("Exception: " + e);
}```
As per MongoDB Driver Reference Connecting SSL
JVM system properties
A typical application will need to set several JVM system properties to ensure that the client is able to validate the SSL certificate presented by the server:
javax.net.ssl.trustStore: the path to a trust store containing the certificate of the signing authority
javax.net.ssl.trustStorePassword: the password to access this trust store
The aforementioned properties can be defined either in system.properties file (lives in "bin" folder of your JMeter installation) or passed to JMeter startup script via -D command-line arguments
Also make sure to have ?ssl=true directive in your MY_CONNECTION_STRING JMeter Variable

java.sql.SQLException: No suitable driver found for jdbc:mariadb://localhost:3306/mydatabase

I have mariadb 10.4 running on my system. The db connection is good, i have tested with db workbench with the root password.
On the other hand, I try to connect to it from my Java code, i have the exception below:
java.sql.SQLException: No suitable driver found for jdbc:mariadb://localhost:3306/mydatabase
My java code is as below:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class CountryDAO {
public void list() throws SQLException{
String databaseURL = "jdbc:mariadb://localhost:3306/mydatabase";
String user = "root";
String password = "root";
try
{
Connection connection = DriverManager.getConnection(databaseURL, user, password);
} catch (SQLException ex) {
ex.printStackTrace();
throw ex;
}
}
}
I am calling this code from a servlet.
I am using eclipse and i have added the mariadb-java-client-2.4.2.jar as external jar from build path. It should be working but it doesn't.
UPDATE: Firstly, I have tested my code in a new Eclipse Java project and it works like a charm. Secondly, i have tried to call the same method from a main method of another class, the db connection also work perfectly. So i guess my problem is because i call this connection from a Servlet. I don't know why it does so but i am kind of stuck. Do you have any suggestions?
You have to load the driver first.
Class.forName("org.mariadb.jdbc.Driver");

Testing java mongodb driver source with mongodb running remotly

I have made changes to source code, I require to run all testcases to check its effect using command
./gradlew check
I am having the mongodb running in remote machine. Can anyone help me in configuring java mongodb driver with remotely running mongodb.
you need to import the java driver to your project.
import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;
then you need to connect to the mongoDB on your server, it can be localhost or it can be your server. also you can choose which port to use:
MongoClient mongoClient = new MongoClient("localhost", 27017);
then you can connect to your db:
MongoDatabase db = mongoClient.getDatabase("test");
and to connect to one of your collections, and make actions on it:
db.getCollection("restaurants").insertOne(
new Document("address",
new Document()
.append("street", "2 Avenue")
.append("zipcode", "10075")
.append("building", "1480")
.append("coord", asList(-73.9557413, 40.7720266)))
.append("borough", "Manhattan")
.append("cuisine", "Italian")
.append("grades", asList(
new Document()
.append("grade", "A")
.append("score", 11),
new Document()
.append("grade", "B")
.append("score", 17)))
.append("name", "Vella")
.append("restaurant_id", "41704620"));
One can pass the connection string while starting testcases
./gradlew check -Dorg.mongodb.test.uri=mongodb://example.com:27017/

Hbase REST is not retuning output from MapR DB

I am trying to query a Mapr table using Hbase rest API. I started hbase rest services using ./hbase-daemon.sh and wrote a small programm to query my table but it does not seems to be working. But when I am using curl tool to get data from MapR table then it is giving me proper output.
Here is my java code that I am using:
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.rest.client.Client;
import org.apache.hadoop.hbase.rest.client.Cluster;
import org.apache.hadoop.hbase.rest.client.RemoteHTable;
public class GetRequest
{
public static void main(String[] args) throws IOException
{
Cluster cluster = new Cluster();
cluster.add(hostname, 8081);
Client client = new Client(cluster);
RemoteHTable table = new RemoteHTable(client, "/mytesttable"); //Remove forward slash to query in hbase table
Get request = new Get(Bytes.toBytes("metric"));
request.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("c"));
Result result = table.get(request);
System.out.println("Output Returned from Table---> "+ Bytes.toString( result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("c"))));
}
}
Here is the curl command which is returning me a proper output..
curl -H "Accept: text/xml" http://hostname:8081/%2Fmytesttable/metric/cf:c
Not sure what is wrong here? Any guesses, anyone?
Thanks in advance.
MapR will start its own hbase rest server using warden , you don't need to start it manually.It will be running at 8080 port(default port). Make sure your hbase rest service;you can check mapr cluster management UI.
The command that I use is,
userDetails=username+":"+password
"curl -k -u " + userDetails + " https://" + ip + ":" + port + "/rest/table/create?path=" + path + tableName

Soap UI, REST API, update database

I am going to use soapUI to test the REST API framework.
Is there a way through which i can insert/update records inside the MongoDB with data in a file type(csv, txt etc) using soapUI tool?
What i am trying to do is validate the API calls and update the database from a data file.
If you are willing to use Groovy script, then you can do this pretty easily.
Put your jdbc driver in SoapUI's bin\ext directory.
https://github.com/mongodb/mongo-java-driver/downloads
(probably where you can it for mongodb)
Then you need roughly these things in your script:
import groovy.sql.Sql
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
groovyUtils.registerJdbcDriver("org.postgresql.Driver") // NOT SURE WHAT STRING FOR MONGODB
def connectString = "....."
sql = Sql.newInstance(connectString) // TEST YOUR CONNECT STRING IN A SQL BROWSER
def misc = sql.firstRow("SELECT * from table")
groovy.sql.Sql is very nice!
http://groovy.codehaus.org/api/groovy/sql/Sql.html
You can easily use below groovy code in "Groovy test step" of your test case and connect to mongodb. Before that please ensure that mongodb java client jar file and gmongo are in {Installation Directory}\bin\ext folder of your soapUI installation
Gmongo: http://mvnrepository.com/artifact/com.gmongo/gmongo/1.5
Mongodb Java Client : http://mvnrepository.com/artifact/org.mongodb/mongo-java-driver/3.2.2
import com.gmongo.GMongoClient
import com.gmongo.GMongo
import com.mongodb.MongoCredential
import com.mongodb.ServerAddress
//def credentials = MongoCredential.createMongoCRCredential('admin', 'students', 'admin' as char[])
//def client = new GMongoClient(new ServerAddress("127.0.0.1:27017"))
context.gmongo=new GMongo()
def db=context.gmongo.getDB("test")
log.info db.fruit.find().count()
db.fruit.find().each{
doc->log.info doc
}