From 6f117fcfd8e071ba851d5fd9402c43c6fe1a777f Mon Sep 17 00:00:00 2001 From: Leon Radley Date: Mon, 18 Nov 2013 13:35:28 +0100 Subject: [PATCH] Implemented ssl setting #52 The ssl setting works by setting the socket factory to javax.net.ssl.SSLSocketFactory --- .../radley/plugin/salat/OptionsFromConfig.scala | 2 ++ .../plugin/salat/OptionsFromConfigSpec.scala | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/main/scala/se/radley/plugin/salat/OptionsFromConfig.scala b/src/main/scala/se/radley/plugin/salat/OptionsFromConfig.scala index 84e2f91..0272a83 100644 --- a/src/main/scala/se/radley/plugin/salat/OptionsFromConfig.scala +++ b/src/main/scala/se/radley/plugin/salat/OptionsFromConfig.scala @@ -3,6 +3,7 @@ package se.radley.plugin.salat import play.api.Configuration import com.mongodb.casbah.MongoOptions import com.mongodb._ +import javax.net.ssl.SSLSocketFactory import scala.Some object OptionsFromConfig { @@ -43,6 +44,7 @@ object OptionsFromConfig { config.getInt("socketTimeout").map(v => builder.socketTimeout(v)) config.getInt("threadsAllowedToBlockForConnectionMultiplier").map(v => builder.threadsAllowedToBlockForConnectionMultiplier(v)) config.getString("writeConcern").map(name => WriteConcern.valueOf(name)).map(v => builder.writeConcern(v)) + config.getBoolean("ssl").map(v => if (v) builder.socketFactory(SSLSocketFactory.getDefault())) Some(builder.build()) } diff --git a/src/test/scala/se/radley/plugin/salat/OptionsFromConfigSpec.scala b/src/test/scala/se/radley/plugin/salat/OptionsFromConfigSpec.scala index 6af4afd..58d6e75 100644 --- a/src/test/scala/se/radley/plugin/salat/OptionsFromConfigSpec.scala +++ b/src/test/scala/se/radley/plugin/salat/OptionsFromConfigSpec.scala @@ -5,6 +5,7 @@ import com.mongodb._ import play.api.Configuration import javax.net.SocketFactory import org.specs2.specification.AllExpectations +import javax.net.ssl.SSLSocketFactory class OptionsFromConfigSpec extends Specification with AllExpectations { @@ -54,6 +55,20 @@ class OptionsFromConfigSpec extends Specification with AllExpectations { val options = OptionsFromConfig(Configuration.empty) options must beNone } + + "Set SSL factory is ssl = true" in { + val conf = Map( + ("mongodb.default.options.ssl" -> "true") + ) + + val configuration = Configuration.from(conf).getConfig("mongodb.default.options").get + val optionsOpt = OptionsFromConfig(configuration) + + optionsOpt must beSome + val options = optionsOpt.get + + options.getSocketFactory must haveClass[SSLSocketFactory] + } } }