AKKA远程(使用SSL)无法在类路径上找到密钥库/信任库文件

时间:2013-10-03 17:46:23

标签: scala netty akka keystore

我正在尝试配置AKKA SSL连接以使用我的密钥库和可信任文件,我希望它能够在类路径中找到它们。

我尝试将application.conf设置为:

...
remote.netty.ssl = {
        enable = on
        key-store = "keystore"
        key-store-password = "passwd"
        trust-store = "truststore"
        trust-store-password = "passwd"
        protocol = "TLSv1"
        random-number-generator = "AES128CounterSecureRNG"
        enabled-algorithms = ["TLS_RSA_WITH_AES_128_CBC_SHA"]
    }
...

如果keystoretrustore文件位于当前目录中,则此工作正常。在我的应用程序中,这些文件被打包到WAR和JAR档案中,因此我想从类路径中读取它们。

我试图在application.conf中使用getResource("keystore"),如here所述,没有任何运气。 Config从字面上将其读作字符串。

我还尝试解析String conf并强制它读取值:

val conf: Config = ConfigFactory parseString (s"""
...
"${getClass.getClassLoader.getResource("keystore").getPath}" 
...""")

在这种情况下,它在类路径上找到正确的路径file://some_dir/target/scala-2.10/server_2.10-1.1-one-jar.jar!/main/server_2.10-1.1.jar!/keystore,它正好位于它所在的位置(在jar中)。但是,基础Netty SSL传输无法找到给定此路径的文件,我得到:

Oct 03, 2013 1:02:48 PM org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink
WARNING: Failed to initialize an accepted socket.
45a13eb9-6cb1-46a7-a789-e48da9997f0fakka.remote.RemoteTransportException: Server SSL connection could not be established because key store could not be loaded
    at akka.remote.netty.NettySSLSupport$.constructServerContext$1(NettySSLSupport.scala:113)
    at akka.remote.netty.NettySSLSupport$.initializeServerSSL(NettySSLSupport.scala:130)
    at akka.remote.netty.NettySSLSupport$.apply(NettySSLSupport.scala:27)
    at akka.remote.netty.NettyRemoteTransport$PipelineFactory$.defaultStack(NettyRemoteSupport.scala:74)
    at akka.remote.netty.NettyRemoteTransport$PipelineFactory$$anon$1.getPipeline(NettyRemoteSupport.scala:67)
    at akka.remote.netty.NettyRemoteTransport$PipelineFactory$$anon$1.getPipeline(NettyRemoteSupport.scala:67)
    at org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink$Boss.registerAcceptedChannel(NioServerSocketPipelineSink.java:277)
    at org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink$Boss.run(NioServerSocketPipelineSink.java:242)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)
Caused by: java.io.FileNotFoundException: file:/some_dir/server/target/scala-2.10/server_2.10-1.1-one-jar.jar!/main/server_2.10-1.1.jar!/keystore (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:97)
    at akka.remote.netty.NettySSLSupport$.constructServerContext$1(NettySSLSupport.scala:118)
    ... 10 more

我想知道在没有实现自定义SSL传输的情况下是否有任何方法可以在AKKA中配置它。也许我应该在代码中配置Netty?

显然我可以硬编码路径或从环境变量中读取它,但我更喜欢更灵活的类路径解决方案。

我决定查看引发异常的代码中的akka.remote.netty.NettySSLSupport,这里是代码:

  def initializeServerSSL(settings: NettySettings, log: LoggingAdapter): SslHandler = {
log.debug("Server SSL is enabled, initialising ...")

def constructServerContext(settings: NettySettings, log: LoggingAdapter, keyStorePath: String, keyStorePassword: String, protocol: String): Option[SSLContext] =
  try {
    val rng = initializeCustomSecureRandom(settings.SSLRandomNumberGenerator, settings.SSLRandomSource, log)
    val factory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm)
    factory.init({
      val keyStore = KeyStore.getInstance(KeyStore.getDefaultType)
      val fin = new FileInputStream(keyStorePath)
      try keyStore.load(fin, keyStorePassword.toCharArray) finally fin.close()
      keyStore
    }, keyStorePassword.toCharArray)
    Option(SSLContext.getInstance(protocol)) map { ctx ⇒ ctx.init(factory.getKeyManagers, null, rng); ctx }
  } catch {
    case e: FileNotFoundException    ⇒ throw new RemoteTransportException("Server SSL connection could not be established because key store could not be loaded", e)
    case e: IOException              ⇒ throw new RemoteTransportException("Server SSL connection could not be established because: " + e.getMessage, e)
    case e: GeneralSecurityException ⇒ throw new RemoteTransportException("Server SSL connection could not be established because SSL context could not be constructed", e)
  }

看起来它必须是纯文件名(String),因为这就是FileInputStream所需要的。

欢迎任何建议!

2 个答案:

答案 0 :(得分:0)

在撰写此问题时,AFAIK无法做到这一点。我关闭了这个问题,但如果新版本提供此类功能或者有其他方法可以做到这一点,我欢迎更新。

答案 1 :(得分:0)

我也陷入了类似的issue,并且变得类似errors。在我的情况下,我试图使用akka-http使用自签名证书来访问https服务器,并使用以下代码:

val trustStoreConfig = TrustStoreConfig(None, Some("/etc/Project/keystore/my.cer")).withStoreType("PEM")
val trustManagerConfig = TrustManagerConfig().withTrustStoreConfigs(List(trustStoreConfig))

val badSslConfig = AkkaSSLConfig().mapSettings(s => s.withLoose(s.loose
  .withAcceptAnyCertificate(true)
  .withDisableHostnameVerification(true)
).withTrustManagerConfig(trustManagerConfig))

val badCtx = Http().createClientHttpsContext(badSslConfig)

Http().superPool[RequestTracker](badCtx)(httpMat)