Extract common name from legal name to determine path in Driver (#613)

Rewrote node name to extract common name to use as the node path for samples, to work around characters being incorrectly treated as separators.
This commit is contained in:
Ross Nicoll 2017-05-03 16:58:48 +01:00 committed by Chris Rankin
parent 5d0bbfa6b5
commit 89a45b3ce2

View File

@ -9,6 +9,8 @@ import com.typesafe.config.ConfigRenderOptions
import net.corda.client.rpc.CordaRPCClient
import net.corda.core.ThreadBox
import net.corda.core.crypto.Party
import net.corda.core.crypto.X509Utilities
import net.corda.core.crypto.commonName
import net.corda.core.div
import net.corda.core.flatMap
import net.corda.core.map
@ -30,6 +32,7 @@ import net.corda.nodeapi.config.SSLConfiguration
import net.corda.nodeapi.config.parseAs
import okhttp3.OkHttpClient
import okhttp3.Request
import org.bouncycastle.asn1.x500.X500Name
import org.slf4j.Logger
import java.io.File
import java.net.*
@ -58,6 +61,23 @@ private val log: Logger = loggerFor<DriverDSL>()
* This is the interface that's exposed to DSL users.
*/
interface DriverDSLExposedInterface {
/**
* Starts a [net.corda.node.internal.Node] in a separate process.
*
* @param providedName Name of the node, which will be its legal name in [Party].
* Note that this must be unique as the driver uses it as a primary key!
* @param advertisedServices The set of services to be advertised by the node. Defaults to empty set.
* @param verifierType The type of transaction verifier to use. See: [VerifierType]
* @param rpcUsers List of users who are authorised to use the RPC system. Defaults to empty list.
* @return The [NodeInfo] of the started up node retrieved from the network map service.
*/
@Deprecated("To be removed once X500Name is used as legal name everywhere")
fun startNode(providedName: String?,
advertisedServices: Set<ServiceInfo> = emptySet(),
rpcUsers: List<User> = emptyList(),
verifierType: VerifierType = VerifierType.InMemory,
customOverrides: Map<String, Any?> = emptyMap()): ListenableFuture<NodeHandle>
/**
* Starts a [net.corda.node.internal.Node] in a separate process.
*
@ -68,7 +88,7 @@ interface DriverDSLExposedInterface {
* @param rpcUsers List of users who are authorised to use the RPC system. Defaults to empty list.
* @return The [NodeInfo] of the started up node retrieved from the network map service.
*/
fun startNode(providedName: String? = null,
fun startNode(providedName: X500Name? = null,
advertisedServices: Set<ServiceInfo> = emptySet(),
rpcUsers: List<User> = emptyList(),
verifierType: VerifierType = VerifierType.InMemory,
@ -416,19 +436,31 @@ class DriverDSL(
}
override fun startNode(
providedName: String?,
providedName: X500Name?,
advertisedServices: Set<ServiceInfo>,
rpcUsers: List<User>,
verifierType: VerifierType,
customOverrides: Map<String, Any?>
): ListenableFuture<NodeHandle> {
return startNode(providedName?.toString(), advertisedServices, rpcUsers, verifierType, customOverrides)
}
override fun startNode(providedName: String?,
advertisedServices: Set<ServiceInfo>,
rpcUsers: List<User>,
verifierType: VerifierType,
customOverrides: Map<String, Any?>): ListenableFuture<NodeHandle> {
val p2pAddress = portAllocation.nextHostAndPort()
val rpcAddress = portAllocation.nextHostAndPort()
val webAddress = portAllocation.nextHostAndPort()
val debugPort = if (isDebug) debugPortAllocation.nextPort() else null
val name = providedName ?: "${pickA(name)}-${p2pAddress.port}"
val baseDirectory = driverDirectory / name
val name = providedName.toString() ?: X509Utilities.getDevX509Name("${pickA(name).commonName}-${p2pAddress.port}").toString()
val commonName = try {
X500Name(name).commonName
} catch(ex: IllegalArgumentException) {
name
}
val baseDirectory = driverDirectory / commonName
val configOverrides = mapOf(
"myLegalName" to name,
"p2pAddress" to p2pAddress.toString(),
@ -475,7 +507,7 @@ class DriverDSL(
verifierType: VerifierType,
rpcUsers: List<User>
): ListenableFuture<Pair<Party, List<NodeHandle>>> {
val nodeNames = (1..clusterSize).map { "${DUMMY_NOTARY.name} $it" }
val nodeNames = (1..clusterSize).map { "Notary Node $it" }
val paths = nodeNames.map { driverDirectory / it }
ServiceIdentityGenerator.generateToDisk(paths, type.id, notaryName)
@ -533,7 +565,12 @@ class DriverDSL(
override fun startNetworkMapService() {
val debugPort = if (isDebug) debugPortAllocation.nextPort() else null
val apiAddress = portAllocation.nextHostAndPort().toString()
val baseDirectory = driverDirectory / networkMapLegalName
val nodeDirectoryName = try {
X500Name(networkMapLegalName).commonName
} catch(ex: IllegalArgumentException) {
networkMapLegalName
}
val baseDirectory = driverDirectory / nodeDirectoryName
val config = ConfigHelper.loadConfig(
baseDirectory = baseDirectory,
allowMissingConfig = true,
@ -554,9 +591,9 @@ class DriverDSL(
companion object {
val name = arrayOf(
ALICE.name,
BOB.name,
DUMMY_BANK_A.name
X500Name(ALICE.name),
X500Name(BOB.name),
X500Name(DUMMY_BANK_A.name)
)
fun <A> pickA(array: Array<A>): A = array[Math.abs(Random().nextInt()) % array.size]