Skip to content

Commit

Permalink
=test Fix ssl relative tests on Windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
He-Pin committed Sep 15, 2023
1 parent 46edc51 commit ee8b992
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ import pekko.util.Timeout
object Configuration {
// set this in your JAVA_OPTS to see all ssl debug info: "-Djavax.net.debug=ssl,keymanager"
// The certificate will expire in 2109
private val trustStore = getClass.getClassLoader.getResource("truststore").getPath
private val keyStore = getClass.getClassLoader.getResource("keystore").getPath
import PekkoSpec._
private val trustStore = resourcePath("truststore")
private val keyStore = resourcePath("keystore")
private val conf = """
pekko {
actor.provider = remote
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ object ArterySpecSupport {
// RotatingKeysSSLEngineProvider and, eventually, run tests twice
// (once for each provider).
lazy val tlsConfig: Config = {
val trustStore = getClass.getClassLoader.getResource("truststore").getPath
val keyStore = getClass.getClassLoader.getResource("keystore").getPath
import org.apache.pekko.testkit.PekkoSpec._
val trustStore = resourcePath("truststore")
val keyStore = resourcePath("keystore")

ConfigFactory.parseString(s"""
pekko.remote.artery.ssl.config-ssl-engine {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,19 @@ class TlsTcpWithCrappyRSAWithMD5OnlyHereToMakeSureThingsWorkSpec
}
"""))

import pekko.testkit.PekkoSpec._
class TlsTcpWithRotatingKeysSSLEngineSpec extends TlsTcpSpec(ConfigFactory.parseString(s"""
pekko.remote.artery.ssl {
ssl-engine-provider = org.apache.pekko.remote.artery.tcp.ssl.RotatingKeysSSLEngineProvider
rotating-keys-engine {
key-file = ${TlsTcpSpec.resourcePath("ssl/node.example.com.pem")}
cert-file = ${TlsTcpSpec.resourcePath("ssl/node.example.com.crt")}
ca-cert-file = ${TlsTcpSpec.resourcePath("ssl/exampleca.crt")}
key-file = "${resourcePath("ssl/node.example.com.pem")}"
cert-file = "${resourcePath("ssl/node.example.com.crt")}"
ca-cert-file = "${resourcePath("ssl/exampleca.crt")}"
}
}
"""))

object TlsTcpSpec {
def resourcePath(name: String): String = getClass.getClassLoader.getResource(name).getPath

lazy val config: Config = {
ConfigFactory.parseString(s"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ class PemManagersProviderSpec extends AnyWordSpec with Matchers {

private def withFiles(keyFile: String, certFile: String, caCertFile: String)(
block: (PrivateKey, X509Certificate, Certificate) => Unit) = {
import org.apache.pekko.testkit.PekkoSpec._
block(
PemManagersProvider.loadPrivateKey(nameToPath(keyFile)),
PemManagersProvider.loadCertificate(nameToPath(certFile)).asInstanceOf[X509Certificate],
PemManagersProvider.loadCertificate(nameToPath(caCertFile)))
PemManagersProvider.loadPrivateKey(resourcePath(keyFile)),
PemManagersProvider.loadCertificate(resourcePath(certFile)).asInstanceOf[X509Certificate],
PemManagersProvider.loadCertificate(resourcePath(caCertFile)))
}

private def nameToPath(name: String): String = getClass.getClassLoader.getResource(name).getPath
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,13 @@ object RotatingKeysSSLEngineProviderSpec {
}
"""

import org.apache.pekko.testkit.PekkoSpec._
val resourcesConfig: String = baseConfig +
s"""
pekko.remote.artery.ssl.rotating-keys-engine {
key-file = ${getClass.getClassLoader.getResource(s"$arteryNode001Id.pem").getPath}
cert-file = ${getClass.getClassLoader.getResource(s"$arteryNode001Id.crt").getPath}
ca-cert-file = ${getClass.getClassLoader.getResource("ssl/exampleca.crt").getPath}
key-file = "${resourcePath(s"$arteryNode001Id.pem")}"
cert-file = "${resourcePath(s"$arteryNode001Id.crt")}"
ca-cert-file = "${resourcePath("ssl/exampleca.crt")}"
ssl-context-cache-ttl = ${cacheTtlInSeconds}s
}
"""
Expand All @@ -196,17 +197,17 @@ object RotatingKeysSSLEngineProviderSpec {
val tempFileConfig: String = baseConfig +
s"""
pekko.remote.artery.ssl.rotating-keys-engine {
key-file = ${temporaryDirectory.toFile.getAbsolutePath}/tls.key
cert-file = ${temporaryDirectory.toFile.getAbsolutePath}/tls.crt
ca-cert-file = ${temporaryDirectory.toFile.getAbsolutePath}/ca.crt
key-file = "${normalizedPath(temporaryDirectory.toAbsolutePath.resolve("tls.key"))}"
cert-file = "${normalizedPath(temporaryDirectory.toAbsolutePath.resolve("tls.crt"))}"
ca-cert-file = "${normalizedPath(temporaryDirectory.toAbsolutePath.resolve("ca.crt"))}"
ssl-context-cache-ttl = ${cacheTtlInSeconds}s
}
"""

private def deployResource(resourceName: String, to: Path): Unit = blocking {
// manually ensuring files are deleted and copied to prevent races.
try {
val from = new File(getClass.getClassLoader.getResource(resourceName).getPath).toPath
val from = new File(resourcePath(resourceName)).toPath
to.toFile.getParentFile.mkdirs()
Files.copy(from, to, StandardCopyOption.REPLACE_EXISTING)
} catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ class TlsResourcesSpec extends AnyWordSpec with Matchers {
val nodes = arteryNodeSet + baseNode + baseRsaClient
nodes.foreach { prefix =>
try {
val pk = PemManagersProvider.loadPrivateKey(toAbsolutePath(s"ssl/$prefix.example.com.pem"))
import org.apache.pekko.testkit.PekkoSpec._
val pk = PemManagersProvider.loadPrivateKey(resourcePath(s"ssl/$prefix.example.com.pem"))
pk.getAlgorithm must be("RSA")
} catch {
case NonFatal(t) => fail(s"Failed test for $prefix", t)
Expand All @@ -90,9 +91,6 @@ class TlsResourcesSpec extends AnyWordSpec with Matchers {

object TlsResourcesSpec {

def toAbsolutePath(resourceName: String): String =
getClass.getClassLoader.getResource(resourceName).getPath

private val certFactory = CertificateFactory.getInstance("X.509")
def loadCert(resourceName: String): X509Certificate = {
val fin = classOf[X509ReadersSpec].getResourceAsStream(resourceName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package org.apache.pekko.remote.classic

import java.io.NotSerializableException
import java.nio.file.Paths
import java.util.concurrent.ThreadLocalRandom

import scala.annotation.nowarn
Expand Down Expand Up @@ -75,6 +74,7 @@ object RemotingSpec {
}
}

import PekkoSpec._
val cfg: Config = ConfigFactory.parseString(s"""
common-ssl-settings {
key-store = "${resourcePath("keystore")}"
Expand Down Expand Up @@ -142,13 +142,6 @@ object RemotingSpec {
EventFilter.warning(start = "AssociationError"),
EventFilter.warning(pattern = "received dead letter.*")))
}

private def resourcePath(resourceName: String): String = {
val normalizedPath = Paths.get(getClass.getClassLoader.getResource(resourceName).toURI).normalize()
// Make it works on Windows
normalizedPath.toString.replace('\\', '/')
}

}

@nowarn
Expand Down
18 changes: 17 additions & 1 deletion testkit/src/test/scala/org/apache/pekko/testkit/PekkoSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package org.apache.pekko.testkit
import scala.concurrent.Future
import scala.concurrent.duration._
import scala.language.postfixOps

import com.typesafe.config.Config
import com.typesafe.config.ConfigFactory
import org.scalactic.CanEqual
Expand All @@ -35,6 +34,8 @@ import pekko.event.Logging
import pekko.event.LoggingAdapter
import pekko.testkit.TestEvent._

import java.nio.file.{ Path, Paths }

object PekkoSpec {
val testConf: Config = ConfigFactory.parseString("""
pekko {
Expand All @@ -59,6 +60,21 @@ object PekkoSpec {
ConfigFactory.parseMap(map.asJava)
}

/**
* Get resource's normalized path, which works on windows too.
*/
def resourcePath(resourceName: String): String = {
val normalizedPath = Paths.get(getClass.getClassLoader.getResource(resourceName).toURI).normalize()
// Make it works on Windows
normalizedPath.toString.replace('\\', '/')
}

/**
* Get path's normalized path, which works on Windows too.
*/
def normalizedPath(path: Path): String = {
path.toAbsolutePath.normalize().toString.replace('\\', '/')
}
}

abstract class PekkoSpec(_system: ActorSystem)
Expand Down

0 comments on commit ee8b992

Please sign in to comment.