如何在JMeter中使用JGit添加ssh密钥



我正在尝试使用JMeter在Bitbucket服务器上进行负载测试。我提到了这个链接Jmeter和Bitbucket服务器负载测试,但我不确定如何在Jmeter中的JSR223 Sampler中使用JGit传递ssh密钥的详细信息。

谢谢你的帮助!

JGit使用JSch库通过SSH通道进行Git连接,因此您需要调用JSch.addIdentity((函数并提供私钥/公钥/密码的路径。

示例代码:

import com.jcraft.jsch.JSch
import com.jcraft.jsch.JSchException
import com.jcraft.jsch.Session
import org.eclipse.jgit.api.CloneCommand
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.api.TransportConfigCallback
import org.eclipse.jgit.transport.*
import org.eclipse.jgit.util.FS
CloneCommand cloneCommand = Git.cloneRepository()
cloneCommand.setURI("your Git SSH URL")
cloneCommand.setDirectory(new File('/path/to/local/folder'))
SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
@Override
protected JSch createDefaultJSch(FS fs) throws JSchException {
JSch defaultJSch = super.createDefaultJSch(fs);
defaultJSch.addIdentity("/path/to/your/private/key");
return defaultJSch;
}
@Override
protected void configure(OpenSshConfig.Host host, Session session) {
SshTransport sshTransport = (SshTransport) transport;
sshTransport.setSshSessionFactory(sshSessionFactory);
}
}
cloneCommand.setTransportConfigCallback(new TransportConfigCallback() {
@Override
void configure(Transport transport) {
SshTransport sshTransport = (SshTransport) transport;
sshTransport.setSshSessionFactory(sshSessionFactory);
}
})
cloneCommand.call()

更多信息:

  • JGit身份验证说明
  • Apache Groovy-为什么以及如何使用它
  • JSch-示例-UserAuthPubKey.java

最新更新