Last active
May 30, 2017 11:44
-
-
Save jrudolph/2bf6781bc81623aa59bc14c4353bbef6 to your computer and use it in GitHub Desktop.
Akka Remote Netty SSL Settings tester
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package akka.remote // needs to be in this package because to access `private[akka]` code | |
import java.net.InetSocketAddress | |
import javax.net.ssl.SSLContext | |
import akka.event.NoMarkerLogging | |
import akka.remote.transport.netty.SSLSettings | |
import com.typesafe.config.ConfigFactory | |
object TestRemoteSSLSettings extends App { | |
val customConfig = s""" | |
akka.remote.netty.ssl.security { | |
key-store = "${getClass.getClassLoader.getResource("keystore").getPath}" | |
trust-store = "${getClass.getClassLoader.getResource("truststore").getPath}" | |
key-store-password = "changeme" | |
key-password = "changeme" | |
trust-store-password = "changeme" | |
protocol = "TLSv1.2" | |
random-number-generator = "AES128CounterSecureRNG" | |
enabled-algorithms = [TLS_RSA_WITH_AES_128_CBC_SHA] | |
} | |
""" | |
// use instead if running with actual config | |
// val config = ConfigFactory.load() | |
val config = ConfigFactory.parseString(customConfig) | |
.withFallback(ConfigFactory.defaultReference()) | |
val remoteSslSettings = new SSLSettings(config.getConfig("akka.remote.netty.ssl.security")) | |
val serverCtx: SSLContext = remoteSslSettings.getOrCreateContext(NoMarkerLogging) | |
val clientCtx: SSLContext = serverCtx | |
val listenSocket = | |
serverCtx.getServerSocketFactory.createServerSocket(0) | |
println(s"Bound to port ${listenSocket.getLocalPort}") | |
val clientSocket = | |
clientCtx | |
.getSocketFactory | |
.createSocket(listenSocket.getLocalSocketAddress.asInstanceOf[InetSocketAddress].getAddress, listenSocket.getLocalPort) | |
val serverThread = | |
new Thread { | |
override def run(): Unit = { | |
val serverSocket = listenSocket.accept() | |
val serverIn = serverSocket.getInputStream | |
val serverOut = serverSocket.getOutputStream | |
serverOut.write("Hello client".getBytes("utf8")) | |
val buf = new Array[Byte](500) | |
val read = serverIn.read(buf) | |
println(s"SERVER: ${new String(buf, 0, read, "utf8")}") | |
} | |
} | |
serverThread.setDaemon(true) | |
serverThread.start() | |
val clientIn = clientSocket.getInputStream | |
val clientOut = clientSocket.getOutputStream | |
clientOut.write("Hello server".getBytes("utf8")) | |
val buf = new Array[Byte](500) | |
val read = clientIn.read(buf) | |
println(s"CLIENT: ${new String(buf, 0, read, "utf8")}") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment