From 89a45b3ce21cf67d902027671a4037bc592876e4 Mon Sep 17 00:00:00 2001 From: Ross Nicoll Date: Wed, 3 May 2017 16:58:48 +0100 Subject: [PATCH] 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. --- .../kotlin/net/corda/node/driver/Driver.kt | 57 +++++++++++++++---- 1 file changed, 47 insertions(+), 10 deletions(-) diff --git a/node/src/main/kotlin/net/corda/node/driver/Driver.kt b/node/src/main/kotlin/net/corda/node/driver/Driver.kt index 2e17037672..4bce132a99 100644 --- a/node/src/main/kotlin/net/corda/node/driver/Driver.kt +++ b/node/src/main/kotlin/net/corda/node/driver/Driver.kt @@ -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() * 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 = emptySet(), + rpcUsers: List = emptyList(), + verifierType: VerifierType = VerifierType.InMemory, + customOverrides: Map = emptyMap()): ListenableFuture + /** * 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 = emptySet(), rpcUsers: List = emptyList(), verifierType: VerifierType = VerifierType.InMemory, @@ -416,19 +436,31 @@ class DriverDSL( } override fun startNode( - providedName: String?, + providedName: X500Name?, advertisedServices: Set, rpcUsers: List, verifierType: VerifierType, customOverrides: Map ): ListenableFuture { + return startNode(providedName?.toString(), advertisedServices, rpcUsers, verifierType, customOverrides) + } + + override fun startNode(providedName: String?, + advertisedServices: Set, + rpcUsers: List, + verifierType: VerifierType, + customOverrides: Map): ListenableFuture { 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 ): ListenableFuture>> { - 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 pickA(array: Array): A = array[Math.abs(Random().nextInt()) % array.size]