Ronald Randon
Ronald Randon

Reputation: 1191

MongoDB connection over SSL in Play Framework

I am using Play 1.2.5, MongoDB and Morphia module 1.2.9 in my application.

To create a secure and encrypted connection to the db, I installed MongoDB by enabling SSL using the follwoing links http://docs.mongodb.org/manual/administration/ssl/ http://www.mongodb.org/about/tutorial/build-mongodb-on-linux/

Now I'm able to connect to the mongo shell using mongo --ssl also able to verify whether MongoDB is running or not using https://mylocalhost.com:27017/

But after enabling SSL in MongoDB, I am not able to connect to it through my play application.

Following are the lines I used in the application.conf to connect to the db

morphia.db.host=localhost
morphia.db.port=27017
morphia.db.db=test

Is there any configurations available to connect over SSL?

I did some googling and I am not able to find any solutions. Please help me over this? Thanks in advance.

Upvotes: 3

Views: 970

Answers (2)

ltfishie
ltfishie

Reputation: 2987

I use spring-data and came up against the same issue. With spring-data i was able to construct a Mongo object myself and passes it as a constructor param. Morphia might have the same mechanism. The key is:

options.socketFactory = SSLSocketFactory.getDefault();

After that, make sure you install the SSL public key into your key store and it should work.

public class MongoFactory {
    public Mongo buildMongo (String replicaSet, boolean slaveOk, int writeNumber , int connectionsPerHost, boolean useSSL) throws UnknownHostException{

        ServerAddress addr = new ServerAddress();
        List<ServerAddress> addresses = new ArrayList<ServerAddress>();
        int port =0;
        String host = new String();
        if ( replicaSet == null )
            throw new UnknownHostException("Please provide hostname");
        replicaSet = replicaSet.trim();
        if ( replicaSet.length() == 0 )
            throw new UnknownHostException("Please provide hostname");
        StringTokenizer tokens = new StringTokenizer(replicaSet, ",");
        while(tokens.hasMoreTokens()){
            String token = tokens.nextToken();
            int idx = token.indexOf( ":" );
            if ( idx > 0 ){               
                port = Integer.parseInt( token.substring( idx + 1 ) );
                host = token.substring( 0 , idx ).trim();
            }
            addr = new ServerAddress(host.trim(), port);
            addresses.add(addr);
        }

        MongoOptions options = new MongoOptions();
        options.autoConnectRetry = true; 
        if (useSSL){
            options.socketFactory = SSLSocketFactory.getDefault();
        }
        options.connectionsPerHost=connectionsPerHost;
        options.w=writeNumber;
        options.fsync=false;
        options.wtimeout=5000;
        options.connectTimeout=5000;
        options.autoConnectRetry=true;
        options.socketKeepAlive=true;

        Mongo m = new Mongo(addresses, options);
        if(slaveOk){
            m.setReadPreference(ReadPreference.SECONDARY);
        }
        return m;
    }
}

Upvotes: 1

Gelin Luo
Gelin Luo

Reputation: 14373

Morphia module does not support ssl connection for the moment. And I am not sure morphia library support it. Please create an issue on github to track this requirement: https://github.com/greenlaw110/play-morphia/issues?state=open

Upvotes: 1

Related Questions