Last active
December 15, 2015 07:29
-
-
Save qerub/5223469 to your computer and use it in GitHub Desktop.
An internal "DSL" in Scala for setting up authorization rules programmatically in ActiveMQ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def authorizationPlugin: BrokerPlugin = { | |
val dsl = new AuthorizationDSL; import dsl._ | |
val system = Set(User("system")) | |
entry(Queue(">"), r = system, w = system, a = system) | |
entry(Topic(">"), r = system, w = system, a = system) | |
Seq("test").foreach { basename => | |
val client = Set(User(basename + "-client")) | |
val server = Set(User(basename + "-server")) | |
val both = client ++ server | |
entry(Topic("ActiveMQ.Advisory.>"), r = both, w = both, a = both) | |
entry(Queue(basename + ".to-server"), r = server, w = both, a = both) | |
entry(Queue(basename + ".to-one-client"), r = client, w = server, a = both) | |
entry(Topic(basename + ".to-all-clients"), r = client, w = server, a = both) | |
} | |
makePlugin() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.activemq.broker._ | |
import org.apache.activemq.command._ | |
import org.apache.activemq.filter._ | |
import org.apache.activemq.jaas._ | |
import org.apache.activemq.security._ | |
import scala.collection.JavaConverters._ | |
class AuthorizationDSL { | |
def Queue(name: String) = new ActiveMQQueue (name) | |
def Topic(name: String) = new ActiveMQTopic (name) | |
def User (name: String) = new UserPrincipal (name) | |
def Group(name: String) = new GroupPrincipal(name) | |
val authEntries = new java.util.ArrayList[DestinationMapEntry[_]]() | |
def entry(destination: ActiveMQDestination, r: Set[UserPrincipal], w: Set[UserPrincipal], a: Set[UserPrincipal]) { | |
def objectify[T[_]](m: T[_]): T[Object] = m.asInstanceOf[T[Object]] | |
val authEntry = new AuthorizationEntry() | |
authEntry.setDestination(destination) | |
authEntry.setReadACLs (objectify(r.asJava)) | |
authEntry.setWriteACLs(objectify(w.asJava)) | |
authEntry.setAdminACLs(objectify(a.asJava)) | |
authEntries.add(authEntry) | |
} | |
def makePlugin() = new AuthorizationPlugin(new DefaultAuthorizationMap(authEntries)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment