I'm getting an Auth fail exception when attempting to clone a git repository with jgit written in Kotlin.
Having created a public and private key with
ssh-keygen -t rsa -m PEM
this is to ensure the keys are generated using rsa and not openssh which is not supported by jgit. The 2 keys are then saved to my machine under /Users/Me/.ssh
The public key has been added to the GitHub repository I want to clone in the Deploy Keys section and I've removed the user and hostname from the public key according to something I read here -> https://ngdeveloper.com/how-to-clone-git-using-ssh-in-java/ (image -> https://i0.wp.com/ngdeveloper.com/wp-content/uploads/2021/06/image-4.png?ssl=1)
The code that then does the cloning of the GitHub repository has been written in a Kotlin App but I followed instructions from an article on dzone for 'Accessing Git Repos With Java Using SSH Keys' https://dzone.com/articles/accessing-git-repos-with-java-using-ssh-keys and converted this to kotlin.
I've got a service with the following code:
fun backup() {
// Clone the repo
val workingDir: File = Files.createTempDirectory("workspace").toFile()
val transportConfigCallback: TransportConfigCallback = SshTransportConfigCallback()
var git = Git.cloneRepository()
.setDirectory(workingDir)
.setTransportConfigCallback(transportConfigCallback)
.setURI(gitUrl) // ssh://github.blah.io/Domain/project-name.git
.call()
}
and another class (SshTransportConfigCallback) that overrides the configuration
open class SshTransportConfigCallback : TransportConfigCallback {
private val sshSessionFactory: SshSessionFactory = object : JschConfigSessionFactory() {
override fun configure(hc: OpenSshConfig.Host?, session: Session) {
session.setConfig("StrictHostKeyChecking", "no")
}
@Throws(JSchException::class)
override fun createDefaultJSch(fs: FS?): JSch {
val jSch: JSch = super.createDefaultJSch(fs)
jSch.removeAllIdentity()
jSch.addIdentity("/path/to/private/key/file/in/this/app") // not the file located under ~/.ssh but copied and pasted in the Kotlin app
return jSch
}
}
override fun configure(transport: Transport) {
val sshTransport: SshTransport = transport as SshTransport
sshTransport.sshSessionFactory = sshSessionFactory
}
}
The build.gradle implementations for adding jgit are as follows:
implementation("org.eclipse.jgit:org.eclipse.jgit:6.4.0.202211300538-r")
implementation("org.eclipse.jgit:org.eclipse.jgit.ssh.jsch:6.4.0.202211300538-r")
Debugging the code and what I find is that when a session is being created jgit is looking on my local machine for the config in ~/.ssh/config
but I don't have such a file.
When the public and private keys were generated with
ssh-keygen -t rsa -m PEM
they were saved in ~/.ssh
under the Users folder on my laptop and then I ran
ssh-add git-ssh
also according to https://ngdeveloper.com/how-to-clone-git-using-ssh-in-java/
Given the config in SshTransportConfigCallback
, I thought that the private key that I have copied from ~/.ssh
and into my app adding it as an identity when overriding the defaultJSch behaviour this meant jgit would use this and not look in a config file on my local machines (~/.ssh
)?
I'm thinking that I'm missing some config somewhere to tell jgit not to do this?
If I change the filename of the private key in SshTransportConfigCallback (to test that the code is being hit) I do get an exception saying the file wasn't found so the private key it contains is being read and used somewhere but maybe not being used to initiate the session with GitHub?
Would appreciate any suggestions! Thank you