Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make all SSL settings optional. #150

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ class CassandraPluginConfig(config: Config) {
}

if (config.hasPath("ssl")) {
val trustStorePath: String = config.getString("ssl.truststore.path")
val trustStorePW: String = config.getString("ssl.truststore.password")
val keyStorePath: String = config.getString("ssl.keystore.path")
val keyStorePW: String = config.getString("ssl.keystore.password")
val trustStorePath: Option[String] = config.getOptionalString("ssl.truststore.path")
val trustStorePW: Option[String] = config.getOptionalString("ssl.truststore.password")
val keyStorePath: Option[String] = config.getOptionalString("ssl.keystore.path")
val keyStorePW: Option[String] = config.getOptionalString("ssl.keystore.password")

val context = SSLSetup.constructContext(
trustStorePath,
Expand Down Expand Up @@ -142,4 +142,9 @@ object CassandraPluginConfig {
case true => tableName
case false => throw new IllegalArgumentException(s"Invalid table name. A table name may 32 or fewer alpha-numeric characters and underscores. Value was: $tableName")
}

//From: https://github.com/typesafehub/config#how-to-handle-defaults
implicit class RichConfig(val underlying: Config) extends AnyVal {
def getOptionalString(path: String): Option[String] = if (underlying.hasPath(path)) Some(underlying.getString(path)) else None
}
}
57 changes: 29 additions & 28 deletions src/main/scala/akka/persistence/cassandra/SSLSetup.scala
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
package akka.persistence.cassandra

import java.io.{File, FileInputStream, InputStream}
import java.io.{File, FileInputStream}
import java.security.{KeyStore, SecureRandom}
import javax.net.ssl.{KeyManagerFactory, SSLContext, TrustManagerFactory}
import javax.net.ssl.{KeyManagerFactory, SSLContext, TrustManagerFactory, TrustManager, KeyManager}
import scala.collection.immutable.Seq

private [cassandra] object SSLSetup {
/**
* creates a new SSLContext
*/
def constructContext(
trustStorePath:String,
trustStorePW:String,
keyStorePath:String,
keyStorePW:String):SSLContext = {

val tmf = loadTrustManagerFactory(trustStorePath, trustStorePW)
val kmf = loadKeyManagerFactory(keyStorePath, keyStorePW)
trustStorePath:Option[String],
trustStorePW:Option[String],
keyStorePath:Option[String],
keyStorePW:Option[String]):SSLContext = {

val ctx = SSLContext.getInstance("SSL")

ctx.init(
kmf.getKeyManagers,
tmf.getTrustManagers,
getKeyManagers(keyStorePath, keyStorePW).toArray,
getTrustManagers(trustStorePath, trustStorePW).toArray,
new SecureRandom())

ctx
}

Expand All @@ -42,23 +38,28 @@ private [cassandra] object SSLSetup {
ks
}

def loadTrustManagerFactory(
trustStorePath:String,
trustStorePassword:String):TrustManagerFactory = {
def getTrustManagers(
trustStorePath:Option[String],
trustStorePassword:Option[String]):Seq[TrustManager] = {

val ts = loadKeyStore(trustStorePath, trustStorePassword)
val tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm)
tmf.init(ts)
tmf
trustStorePath.toList.flatMap{ path =>
val ts = loadKeyStore(path, trustStorePassword.getOrElse("changeit"))
val tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm)
tmf.init(ts)
tmf.getTrustManagers
}
}

def loadKeyManagerFactory(
keyStorePath:String,
keyStorePassword:String):KeyManagerFactory = {

val ks = loadKeyStore(keyStorePath, keyStorePassword)
val kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm)
kmf.init(ks, keyStorePassword.toCharArray)
kmf
def getKeyManagers(
keyStorePath:Option[String],
keyStorePassword:Option[String]):Seq[KeyManager] = {

keyStorePath.toList.flatMap { path =>
val password = keyStorePassword.getOrElse("changeit")
val ks = loadKeyStore(path, password)
val kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm)
kmf.init(ks, password.toCharArray)
kmf.getKeyManagers
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package akka.persistence.cassandra.journal
import scala.concurrent.duration._
import akka.persistence.journal._
import akka.persistence.cassandra.CassandraLifecycle
import scala.concurrent.duration._

import com.typesafe.config.ConfigFactory

Expand Down