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
This commit is contained in:
Patrik Nordwall 2019-07-11 14:04:24 +02:00 committed by GitHub
parent a4f090b622
commit 3efc1c2877
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
191 changed files with 4041 additions and 2321 deletions

View file

@ -4,17 +4,16 @@
package docs.serialization {
import akka.actor.{ ExtensionId, ExtensionIdProvider }
import akka.testkit._
//#imports
import akka.actor.{ ActorRef, ActorSystem }
import akka.actor._
import akka.cluster.Cluster
import akka.serialization._
import com.typesafe.config.ConfigFactory
//#imports
import akka.testkit._
import com.typesafe.config.ConfigFactory
import akka.actor.ExtendedActorSystem
import akka.actor.Extension
import akka.actor.Address
import java.nio.charset.StandardCharsets
//#my-own-serializer
@ -99,6 +98,16 @@ package docs.serialization {
final case class Customer(name: String) extends MyOwnSerializable
final case class User(name: String) extends MyOwnSerializable
/**
* Marker trait for serialization with Jackson CBOR
*/
trait CborSerializable
/**
* Marker trait for serialization with Jackson JSON
*/
trait JsonSerializable
class SerializationDocSpec extends AkkaSpec {
"demonstrate configuration of serialize messages" in {
val config = ConfigFactory.parseString("""
@ -136,7 +145,8 @@ package docs.serialization {
akka {
actor {
serializers {
java = "akka.serialization.JavaSerializer"
jackson-json = "akka.serialization.jackson.JacksonJsonSerializer"
jackson-cbor = "akka.serialization.jackson.JacksonCborSerializer"
proto = "akka.remote.serialization.ProtobufSerializer"
myown = "docs.serialization.MyOwnSerializer"
}
@ -154,26 +164,24 @@ package docs.serialization {
akka {
actor {
serializers {
java = "akka.serialization.JavaSerializer"
jackson-json = "akka.serialization.jackson.JacksonJsonSerializer"
jackson-cbor = "akka.serialization.jackson.JacksonCborSerializer"
proto = "akka.remote.serialization.ProtobufSerializer"
myown = "docs.serialization.MyOwnSerializer"
}
serialization-bindings {
"java.lang.String" = java
"docs.serialization.Customer" = java
"docs.serialization.JsonSerializable" = jackson-json
"docs.serialization.CborSerializable" = jackson-cbor
"com.google.protobuf.Message" = proto
"docs.serialization.MyOwnSerializable" = myown
"java.lang.Boolean" = myown
}
}
}
#//#serialization-bindings-config
""")
val a = ActorSystem("system", config)
SerializationExtension(a).serializerFor(classOf[String]).getClass should be(classOf[JavaSerializer])
SerializationExtension(a).serializerFor(classOf[Customer]).getClass should be(classOf[JavaSerializer])
SerializationExtension(a).serializerFor(classOf[java.lang.Boolean]).getClass should be(classOf[MyOwnSerializer])
SerializationExtension(a).serializerFor(classOf[Customer]).getClass should be(classOf[MyOwnSerializer])
shutdown(a)
}
@ -187,80 +195,47 @@ package docs.serialization {
// Have something to serialize
val original = "woohoo"
// Find the Serializer for it
val serializer = serialization.findSerializerFor(original)
// Turn it into bytes
val bytes = serializer.toBinary(original)
// Turn it into bytes, and retrieve the serializerId and manifest, which are needed for deserialization
val bytes = serialization.serialize(original).get
val serializerId = serialization.findSerializerFor(original).identifier
val manifest = Serializers.manifestFor(serialization.findSerializerFor(original), original)
// Turn it back into an object
val back = serializer.fromBinary(bytes, manifest = None)
val back = serialization.deserialize(bytes, serializerId, manifest).get
//#programmatic
// Voilá!
back should be(original)
//#programmatic
shutdown(system)
}
"demonstrate serialization of ActorRefs" in {
def demonstrateSerializationOfActorRefs(): Unit = {
val theActorRef: ActorRef = system.deadLetters
val extendedSystem: ExtendedActorSystem = system.asInstanceOf[ExtendedActorSystem]
//#actorref-serializer
// Serialize
// (beneath toBinary)
val identifier: String = Serialization.serializedActorPath(theActorRef)
val serializedRef: String = Serialization.serializedActorPath(theActorRef)
// Then serialize the identifier however you like
// Deserialize
// (beneath fromBinary)
val deserializedActorRef = extendedSystem.provider.resolveActorRef(identifier)
val deserializedRef = extendedSystem.provider.resolveActorRef(serializedRef)
// Then use the ActorRef
//#actorref-serializer
//#external-address
object ExternalAddress extends ExtensionId[ExternalAddressExt] with ExtensionIdProvider {
override def lookup() = ExternalAddress
override def createExtension(system: ExtendedActorSystem): ExternalAddressExt =
new ExternalAddressExt(system)
override def get(system: ActorSystem): ExternalAddressExt = super.get(system)
}
class ExternalAddressExt(system: ExtendedActorSystem) extends Extension {
def addressFor(remoteAddr: Address): Address =
system.provider
.getExternalAddressFor(remoteAddr)
.getOrElse(throw new UnsupportedOperationException("cannot send to " + remoteAddr))
}
def serializeTo(ref: ActorRef, remote: Address): String =
ref.path.toSerializationFormatWithAddress(ExternalAddress(extendedSystem).addressFor(remote))
//#external-address
}
"demonstrate how to do default Akka serialization of ActorRef" in {
val theActorSystem: ActorSystem = system
def demonstrateSerializationOfActorRefs2(): Unit = {
val theActorRef: ActorRef = system.deadLetters
//#external-address-default
object ExternalAddress extends ExtensionId[ExternalAddressExt] with ExtensionIdProvider {
override def lookup() = ExternalAddress
val selfAddress = Cluster(system).selfAddress
override def createExtension(system: ExtendedActorSystem): ExternalAddressExt =
new ExternalAddressExt(system)
override def get(system: ActorSystem): ExternalAddressExt = super.get(system)
}
class ExternalAddressExt(system: ExtendedActorSystem) extends Extension {
def addressForAkka: Address = system.provider.getDefaultAddress
}
def serializeAkkaDefault(ref: ActorRef): String =
ref.path.toSerializationFormatWithAddress(ExternalAddress(theActorSystem).addressForAkka)
val serializedRef: String =
theActorRef.path.toSerializationFormatWithAddress(selfAddress)
//#external-address-default
}
}