From 4d19a594d63c63cd184b22f94bb56869af93d9b3 Mon Sep 17 00:00:00 2001 From: Christian Sailer Date: Mon, 20 Nov 2017 17:09:59 +0000 Subject: [PATCH] Reduce the dependencies of the JMeter project by copying (#118) one function and listing required explicit dependencies instead of depending on loadtest --- tools/jmeter/build.gradle | 11 +++- .../kotlin/com/r3/corda/jmeter/JshHelper.kt | 58 +++++++++++++++++++ .../main/kotlin/com/r3/corda/jmeter/Ssh.kt | 1 - 3 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 tools/jmeter/src/main/kotlin/com/r3/corda/jmeter/JshHelper.kt diff --git a/tools/jmeter/build.gradle b/tools/jmeter/build.gradle index 8a111b9f6e..109124c69b 100644 --- a/tools/jmeter/build.gradle +++ b/tools/jmeter/build.gradle @@ -7,7 +7,16 @@ mainClassName = 'com.r3.corda.jmeter.Launcher' dependencies { compile project(':client:rpc') compile project(':finance') - compile project(':tools:loadtest') + + // https://mvnrepository.com/artifact/com.jcraft/jsch + compile group: 'com.jcraft', name: 'jsch', version: '0.1.54' + compile group: 'com.jcraft', name: 'jsch.agentproxy.core', version: '0.0.9' + compile group: 'com.jcraft', name: 'jsch.agentproxy.sshagent', version: '0.0.9' + compile group: 'com.jcraft', name: 'jsch.agentproxy.usocket-jna', version: '0.0.9' + + // Log4J: logging framework (with SLF4J bindings) + compile "org.apache.logging.log4j:log4j-slf4j-impl:$log4j_version" + compile "org.apache.logging.log4j:log4j-core:$log4j_version" // JMeter ext.jmVersion = "3.3" diff --git a/tools/jmeter/src/main/kotlin/com/r3/corda/jmeter/JshHelper.kt b/tools/jmeter/src/main/kotlin/com/r3/corda/jmeter/JshHelper.kt new file mode 100644 index 0000000000..d2f7ad3477 --- /dev/null +++ b/tools/jmeter/src/main/kotlin/com/r3/corda/jmeter/JshHelper.kt @@ -0,0 +1,58 @@ +package com.r3.corda.jmeter + +import com.jcraft.jsch.Buffer +import com.jcraft.jsch.Identity +import com.jcraft.jsch.IdentityRepository +import com.jcraft.jsch.JSch +import com.jcraft.jsch.agentproxy.AgentProxy +import com.jcraft.jsch.agentproxy.connector.SSHAgentConnector +import com.jcraft.jsch.agentproxy.usocket.JNAUSocketFactory +import org.slf4j.LoggerFactory +import java.util.* + +private val log = LoggerFactory.getLogger(Ssh::class.java) + +/** + * Creates a new [JSch] instance with identities loaded from the running SSH agent. + */ +fun setupJSchWithSshAgent(): JSch { + val connector = SSHAgentConnector(JNAUSocketFactory()) + val agentProxy = AgentProxy(connector) + val identities = agentProxy.identities + require(identities.isNotEmpty()) { "No SSH identities found, please add one to the agent" } + require(identities.size == 1) { "Multiple SSH identities found, don't know which one to pick" } + val identity = identities[0] + log.info("Using SSH identity ${String(identity.comment)}") + + return JSch().apply { + identityRepository = object : IdentityRepository { + override fun getStatus(): Int { + if (connector.isAvailable) { + return IdentityRepository.RUNNING + } else { + return IdentityRepository.UNAVAILABLE + } + } + + override fun getName() = connector.name + override fun getIdentities(): Vector = Vector(listOf( + object : Identity { + override fun clear() {} + override fun getAlgName() = String(Buffer(identity.blob).string) + override fun getName() = String(identity.comment) + override fun isEncrypted() = false + override fun getSignature(data: ByteArray?) = agentProxy.sign(identity.blob, data) + @Suppress("OverridingDeprecatedMember") + override fun decrypt() = true + + override fun getPublicKeyBlob() = identity.blob + override fun setPassphrase(passphrase: ByteArray?) = true + } + )) + + override fun remove(blob: ByteArray?) = throw UnsupportedOperationException() + override fun removeAll() = throw UnsupportedOperationException() + override fun add(bytes: ByteArray?) = throw UnsupportedOperationException() + } + } +} diff --git a/tools/jmeter/src/main/kotlin/com/r3/corda/jmeter/Ssh.kt b/tools/jmeter/src/main/kotlin/com/r3/corda/jmeter/Ssh.kt index 93cc88cd7c..71eb682cbd 100644 --- a/tools/jmeter/src/main/kotlin/com/r3/corda/jmeter/Ssh.kt +++ b/tools/jmeter/src/main/kotlin/com/r3/corda/jmeter/Ssh.kt @@ -3,7 +3,6 @@ package com.r3.corda.jmeter import com.jcraft.jsch.JSch import com.jcraft.jsch.Session import net.corda.core.utilities.NetworkHostAndPort -import net.corda.loadtest.setupJSchWithSshAgent import net.corda.nodeapi.internal.addShutdownHook import org.slf4j.LoggerFactory import java.io.BufferedReader