How to hide Akka remote actors from lookup?

自闭症网瘾萝莉.ら 提交于 2019-12-21 05:27:18

问题


I am running the Akka 2.0.2 microkernel and want to implement an authentication scheme for untrusted remote actors.

The first thing that comes to mind is to set up an authentication actor which returns a reference to the work actor when authentication succeeds.

However, how should I protect the work actor from simply being directly looked up remotely via actorFor(), circumventing authentication altogether?

That is, I want to prevent remote actors from accessing actors in my microkernel actor system without authentication.

Not giving the work actor a name in actorOf() is not enough, because it will get an easily-guessed autogenerated name. Is there a way to disable remote lookup for actors, yet still be able to give out their ActorRef to remote systems?


回答1:


I think you were on the right track with the authentication actor. Have the authentication actor return both the ActorRef and a token. The remote actors must include that token in messages to your local worker actor. The worker actor will validate the token before doing the work.

trait AuthenticatingActor { this => Actor
  val authenticationService = //...

  def receive = {
    case UnauthenticatedRequest(token, msg) =>
      if (authenticationService.validate(token) 
        authenticatedRecieve(msg)
      else
        sender ! RequestNotAuthenticated(token, "token invalid")

  def authenticatedReceive: Receive
}

class Worker extends AuthenticatingActor with Actor {
  def authenticatedReceive: Receive = //..
}

class AuthenticationActor extends Actor {
  val authenticationService = //..
  var worker: ActorRef = _

  def receive = {
    case Authenticate(username, password) =>
      val token = authenticationService.authenticate(username, password)
      sender ! token.map(AuthenticationSuccess(_, worker).
                     getOrElse(AuthenticationFailure)
    //..
}


来源:https://stackoverflow.com/questions/11921754/how-to-hide-akka-remote-actors-from-lookup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!