Validate protocol type and args via whitelist ahead of class loading. Also, push this work into another method on the Factory.

This commit is contained in:
Matthew Nesbit 2016-07-25 16:35:45 +01:00
parent 0542f7c509
commit 0d68523f5c
2 changed files with 19 additions and 10 deletions

View File

@ -49,6 +49,22 @@ class ProtocolLogicRefFactory(private val protocolWhitelist: Map<String, Set<Str
require(protocolWhitelist[className]!!.contains(argClassName)) { "Args to ${className} must have types on the args whitelist: $argClassName" }
}
/**
* Create a [ProtocolLogicRef] for the Kotlin primary constructor of a named [ProtocolLogic]
*/
fun createKotlin(protocolLogicClassName: String, args: Map<String,Any?>, attachments: List<SecureHash> = emptyList()): ProtocolLogicRef {
val context = AppContext(attachments)
validateProtocolClassName(protocolLogicClassName, context)
for(arg in args.values.filterNotNull()) {
validateArgClassName(protocolLogicClassName, arg.javaClass.name, context)
}
val clazz = Class.forName(protocolLogicClassName)
require(ProtocolLogic::class.java.isAssignableFrom(clazz)) { "$protocolLogicClassName is not a ProtocolLogic" }
@Suppress("UNCHECKED_CAST")
val logic = clazz as Class<ProtocolLogic<ProtocolLogic<*>>>
return createKotlin(logic, args)
}
/**
* Create a [ProtocolLogicRef] for the Kotlin primary constructor or Java constructor and the given args.
*/

View File

@ -5,7 +5,6 @@ import com.r3corda.core.contracts.*
import com.r3corda.core.crypto.DigitalSignature
import com.r3corda.core.crypto.SecureHash
import com.r3corda.core.node.services.linearHeadsOfType
import com.r3corda.core.protocols.ProtocolLogic
import com.r3corda.core.serialization.SerializedBytes
import com.r3corda.node.api.*
import java.time.LocalDateTime
@ -67,15 +66,9 @@ class APIServerImpl(val node: AbstractNode) : APIServer {
private fun invokeProtocolAsync(type: ProtocolRef, args: Map<String, Any?>): ListenableFuture<out Any?> {
if (type is ProtocolClassRef) {
val clazz = Class.forName(type.className)
if (ProtocolLogic::class.java.isAssignableFrom(clazz)) {
@Suppress("UNCHECKED_CAST")
val logic = clazz as Class<ProtocolLogic<ProtocolLogic<*>>>
val protocolLogicRef = node.services.protocolLogicRefFactory.createKotlin(logic, args)
val protocolInstance = node.services.protocolLogicRefFactory.toProtocolLogic(protocolLogicRef)
return node.services.startProtocol(clazz.name, protocolInstance)
}
throw UnsupportedOperationException("Could not find matching protocol and constructor for: $type $args")
val protocolLogicRef = node.services.protocolLogicRefFactory.createKotlin(type.className, args)
val protocolInstance = node.services.protocolLogicRefFactory.toProtocolLogic(protocolLogicRef)
return node.services.startProtocol(type.className, protocolInstance)
} else {
throw UnsupportedOperationException("Unsupported ProtocolRef type: $type")
}