pekko/akka-remote/src/test/scala/akka/remote/classic/UntrustedSpec.scala

204 lines
6.4 KiB
Scala
Raw Normal View History

/*
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com>
*/
package akka.remote.classic
import scala.concurrent.duration._
Disable Java serialization by default, #22333 (#27285) * akka.actor.allow-java-serialization = off * Moved primitive (Long, Int, String, ByteString) serializers from akka-remote to akka-actor since they had no dependency and are useful also in local systems, e.g. persistence. * e.g. needed for persistence-tck * less allow-java-serialization=on in tests * CborSerializable in Jackson/test module for ease of use * JavaSerializable for Java serialization in tests, already in akka-testkit, but misconfigured * Made tests pass * allow-java-serialization=on in akka-persistence * allow-java-serialization=on in classic remoting tests * JavaSerializable and CborSerializable in other remoting tests * Added serialization for * Boolean * java.util.concurrent.TimeoutException, AskTimeoutException * support for testing serialization with the inmem journal * utility to verifySerialization, in SerializationTestKit * remove AccountExampleWithCommandHandlersInState becuase not possible to serialize State when it's not static * Effect() is factory in EventSourcedBehavior class * test the account examples * SharedLeveldbJournal.configToEnableJavaSerializationForTest * support for exceptions from remote deployed child actors * fallback to akka.remote.serialization.ThrowableNotSerializableException if exception is not serializable when wrapped in system messages from remote deployed child actors and Status.Failure messages * it's implemented in `WrappedPayloadSupport.payloadBuilder` * update reference documentation * serialize-messages=off in most places, separate ticket for improving or removing that feature * migration guide, including description of rolling update * fix 2.13 compiler error * minor review feedback
2019-07-11 14:04:24 +02:00
import com.typesafe.config.ConfigFactory
import akka.actor.Actor
import akka.actor.ActorIdentity
import akka.actor.ActorRef
import akka.actor.ActorSystem
import akka.actor.Deploy
import akka.actor.ExtendedActorSystem
import akka.actor.Identify
import akka.actor.PoisonPill
import akka.actor.Props
import akka.actor.RootActorPath
import akka.actor.Terminated
import akka.testkit.AkkaSpec
import akka.testkit.ImplicitSender
import akka.testkit.TestProbe
import akka.actor.ActorSelection
import akka.testkit.TestEvent
import akka.event.Logging
import akka.testkit.EventFilter
Disable Java serialization by default, #22333 (#27285) * akka.actor.allow-java-serialization = off * Moved primitive (Long, Int, String, ByteString) serializers from akka-remote to akka-actor since they had no dependency and are useful also in local systems, e.g. persistence. * e.g. needed for persistence-tck * less allow-java-serialization=on in tests * CborSerializable in Jackson/test module for ease of use * JavaSerializable for Java serialization in tests, already in akka-testkit, but misconfigured * Made tests pass * allow-java-serialization=on in akka-persistence * allow-java-serialization=on in classic remoting tests * JavaSerializable and CborSerializable in other remoting tests * Added serialization for * Boolean * java.util.concurrent.TimeoutException, AskTimeoutException * support for testing serialization with the inmem journal * utility to verifySerialization, in SerializationTestKit * remove AccountExampleWithCommandHandlersInState becuase not possible to serialize State when it's not static * Effect() is factory in EventSourcedBehavior class * test the account examples * SharedLeveldbJournal.configToEnableJavaSerializationForTest * support for exceptions from remote deployed child actors * fallback to akka.remote.serialization.ThrowableNotSerializableException if exception is not serializable when wrapped in system messages from remote deployed child actors and Status.Failure messages * it's implemented in `WrappedPayloadSupport.payloadBuilder` * update reference documentation * serialize-messages=off in most places, separate ticket for improving or removing that feature * migration guide, including description of rolling update * fix 2.13 compiler error * minor review feedback
2019-07-11 14:04:24 +02:00
import akka.testkit.JavaSerializable
object UntrustedSpec {
Disable Java serialization by default, #22333 (#27285) * akka.actor.allow-java-serialization = off * Moved primitive (Long, Int, String, ByteString) serializers from akka-remote to akka-actor since they had no dependency and are useful also in local systems, e.g. persistence. * e.g. needed for persistence-tck * less allow-java-serialization=on in tests * CborSerializable in Jackson/test module for ease of use * JavaSerializable for Java serialization in tests, already in akka-testkit, but misconfigured * Made tests pass * allow-java-serialization=on in akka-persistence * allow-java-serialization=on in classic remoting tests * JavaSerializable and CborSerializable in other remoting tests * Added serialization for * Boolean * java.util.concurrent.TimeoutException, AskTimeoutException * support for testing serialization with the inmem journal * utility to verifySerialization, in SerializationTestKit * remove AccountExampleWithCommandHandlersInState becuase not possible to serialize State when it's not static * Effect() is factory in EventSourcedBehavior class * test the account examples * SharedLeveldbJournal.configToEnableJavaSerializationForTest * support for exceptions from remote deployed child actors * fallback to akka.remote.serialization.ThrowableNotSerializableException if exception is not serializable when wrapped in system messages from remote deployed child actors and Status.Failure messages * it's implemented in `WrappedPayloadSupport.payloadBuilder` * update reference documentation * serialize-messages=off in most places, separate ticket for improving or removing that feature * migration guide, including description of rolling update * fix 2.13 compiler error * minor review feedback
2019-07-11 14:04:24 +02:00
final case class IdentifyReq(path: String) extends JavaSerializable
final case class StopChild(name: String) extends JavaSerializable
class Receptionist(testActor: ActorRef) extends Actor {
context.actorOf(Props(classOf[Child], testActor), "child1")
context.actorOf(Props(classOf[Child], testActor), "child2")
context.actorOf(Props(classOf[FakeUser], testActor), "user")
def receive = {
case IdentifyReq(path) => context.actorSelection(path).tell(Identify(None), sender())
2019-03-11 10:38:24 +01:00
case StopChild(name) => context.child(name).foreach(context.stop)
case msg => testActor.forward(msg)
}
}
class Child(testActor: ActorRef) extends Actor {
override def postStop(): Unit = {
testActor ! s"${self.path.name} stopped"
}
def receive = {
2019-03-11 10:38:24 +01:00
case msg => testActor.forward(msg)
}
}
class FakeUser(testActor: ActorRef) extends Actor {
context.actorOf(Props(classOf[Child], testActor), "receptionist")
def receive = {
2019-03-11 10:38:24 +01:00
case msg => testActor.forward(msg)
}
}
}
class UntrustedSpec extends AkkaSpec("""
akka.loglevel = DEBUG
akka.actor.provider = remote
akka.remote.artery.enabled = off
akka.remote.warn-about-direct-use = off
akka.remote.classic.untrusted-mode = on
akka.remote.classic.trusted-selection-paths = ["/user/receptionist", ]
akka.remote.classic.netty.tcp.port = 0
akka.loglevel = DEBUG # test verifies debug
Disable Java serialization by default, #22333 (#27285) * akka.actor.allow-java-serialization = off * Moved primitive (Long, Int, String, ByteString) serializers from akka-remote to akka-actor since they had no dependency and are useful also in local systems, e.g. persistence. * e.g. needed for persistence-tck * less allow-java-serialization=on in tests * CborSerializable in Jackson/test module for ease of use * JavaSerializable for Java serialization in tests, already in akka-testkit, but misconfigured * Made tests pass * allow-java-serialization=on in akka-persistence * allow-java-serialization=on in classic remoting tests * JavaSerializable and CborSerializable in other remoting tests * Added serialization for * Boolean * java.util.concurrent.TimeoutException, AskTimeoutException * support for testing serialization with the inmem journal * utility to verifySerialization, in SerializationTestKit * remove AccountExampleWithCommandHandlersInState becuase not possible to serialize State when it's not static * Effect() is factory in EventSourcedBehavior class * test the account examples * SharedLeveldbJournal.configToEnableJavaSerializationForTest * support for exceptions from remote deployed child actors * fallback to akka.remote.serialization.ThrowableNotSerializableException if exception is not serializable when wrapped in system messages from remote deployed child actors and Status.Failure messages * it's implemented in `WrappedPayloadSupport.payloadBuilder` * update reference documentation * serialize-messages=off in most places, separate ticket for improving or removing that feature * migration guide, including description of rolling update * fix 2.13 compiler error * minor review feedback
2019-07-11 14:04:24 +02:00
# test is using Java serialization and not priority to rewrite
akka.actor.allow-java-serialization = on
akka.actor.warn-about-java-serializer-usage = off
akka.actor.serialization-bindings {
"akka.actor.Terminated" = java-test
}
""") with ImplicitSender {
import UntrustedSpec._
2019-03-13 10:56:20 +01:00
val client = ActorSystem(
"UntrustedSpec-client",
ConfigFactory.parseString("""
akka.loglevel = DEBUG
akka.actor.provider = remote
akka.remote.artery.enabled = off
akka.remote.warn-about-direct-use = off
akka.remote.classic.netty.tcp.port = 0
Disable Java serialization by default, #22333 (#27285) * akka.actor.allow-java-serialization = off * Moved primitive (Long, Int, String, ByteString) serializers from akka-remote to akka-actor since they had no dependency and are useful also in local systems, e.g. persistence. * e.g. needed for persistence-tck * less allow-java-serialization=on in tests * CborSerializable in Jackson/test module for ease of use * JavaSerializable for Java serialization in tests, already in akka-testkit, but misconfigured * Made tests pass * allow-java-serialization=on in akka-persistence * allow-java-serialization=on in classic remoting tests * JavaSerializable and CborSerializable in other remoting tests * Added serialization for * Boolean * java.util.concurrent.TimeoutException, AskTimeoutException * support for testing serialization with the inmem journal * utility to verifySerialization, in SerializationTestKit * remove AccountExampleWithCommandHandlersInState becuase not possible to serialize State when it's not static * Effect() is factory in EventSourcedBehavior class * test the account examples * SharedLeveldbJournal.configToEnableJavaSerializationForTest * support for exceptions from remote deployed child actors * fallback to akka.remote.serialization.ThrowableNotSerializableException if exception is not serializable when wrapped in system messages from remote deployed child actors and Status.Failure messages * it's implemented in `WrappedPayloadSupport.payloadBuilder` * update reference documentation * serialize-messages=off in most places, separate ticket for improving or removing that feature * migration guide, including description of rolling update * fix 2.13 compiler error * minor review feedback
2019-07-11 14:04:24 +02:00
# test is using Java serialization and not priority to rewrite
akka.actor.allow-java-serialization = off
akka.actor.warn-about-java-serializer-usage = off
akka.actor.serialization-bindings {
"akka.actor.Terminated" = java-test
}
"""))
val address = system.asInstanceOf[ExtendedActorSystem].provider.getDefaultAddress
val receptionist = system.actorOf(Props(classOf[Receptionist], testActor), "receptionist")
lazy val remoteDaemon = {
{
val p = TestProbe()(client)
client.actorSelection(RootActorPath(address) / receptionist.path.elements).tell(IdentifyReq("/remote"), p.ref)
p.expectMsgType[ActorIdentity].ref.get
}
}
lazy val target2 = {
val p = TestProbe()(client)
2019-03-11 10:38:24 +01:00
client.actorSelection(RootActorPath(address) / receptionist.path.elements).tell(IdentifyReq("child2"), p.ref)
p.expectMsgType[ActorIdentity].ref.get
}
2018-07-25 20:38:27 +09:00
override def afterTermination(): Unit = {
shutdown(client)
}
// need to enable debug log-level without actually printing those messages
system.eventStream.publish(TestEvent.Mute(EventFilter.debug()))
"UntrustedMode" must {
"allow actor selection to configured white list" in {
val sel = client.actorSelection(RootActorPath(address) / receptionist.path.elements)
sel ! "hello"
expectMsg("hello")
}
"discard harmful messages to /remote" in {
val logProbe = TestProbe()
// but instead install our own listener
system.eventStream.subscribe(system.actorOf(Props(new Actor {
import Logging._
def receive = {
case d @ Debug(_, _, msg: String) if msg contains "dropping" => logProbe.ref ! d
2019-03-11 10:38:24 +01:00
case _ =>
}
}).withDeploy(Deploy.local), "debugSniffer"), classOf[Logging.Debug])
remoteDaemon ! "hello"
logProbe.expectMsgType[Logging.Debug]
}
"discard harmful messages to testActor" in {
target2 ! Terminated(remoteDaemon)(existenceConfirmed = true, addressTerminated = false)
target2 ! PoisonPill
client.stop(target2)
target2 ! "blech"
expectMsg("blech")
}
"discard watch messages" in {
client.actorOf(Props(new Actor {
context.watch(target2)
def receive = {
2019-03-11 10:38:24 +01:00
case x => testActor.forward(x)
}
}).withDeploy(Deploy.local))
receptionist ! StopChild("child2")
expectMsg("child2 stopped")
// no Terminated msg, since watch was discarded
expectNoMessage(1.second)
}
"discard actor selection" in {
val sel = client.actorSelection(RootActorPath(address) / testActor.path.elements)
sel ! "hello"
expectNoMessage(1.second)
}
"discard actor selection with non root anchor" in {
val p = TestProbe()(client)
2019-03-11 10:38:24 +01:00
client.actorSelection(RootActorPath(address) / receptionist.path.elements).tell(Identify(None), p.ref)
val clientReceptionistRef = p.expectMsgType[ActorIdentity].ref.get
val sel = ActorSelection(clientReceptionistRef, receptionist.path.toStringWithoutAddress)
sel ! "hello"
expectNoMessage(1.second)
}
"discard actor selection to child of matching white list" in {
val sel = client.actorSelection(RootActorPath(address) / receptionist.path.elements / "child1")
sel ! "hello"
expectNoMessage(1.second)
}
"discard actor selection with wildcard" in {
val sel = client.actorSelection(RootActorPath(address) / receptionist.path.elements / "*")
sel ! "hello"
expectNoMessage(1.second)
}
"discard actor selection containing harmful message" in {
val sel = client.actorSelection(RootActorPath(address) / receptionist.path.elements)
sel ! PoisonPill
expectNoMessage(1.second)
}
}
2013-01-09 01:47:48 +01:00
}