Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Accessing Git Repos With Java Using SSH Keys

DZone's Guide to

Accessing Git Repos With Java Using SSH Keys

See how you can use SSH keys to gain access to Git repos from Java apps. We'll use JGit and cover some customizations you can introduce.

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

For some use cases, you might access a Git repository from a Java application. JGit offers a helpful integration with builder pattern APIs. The Git client can authenticate itself using SSH keys.

To open a Git repository, call the cloneRepository() command.

File workingDir = Files.createTempDirectory("workspace").toFile();

TransportConfigCallback transportConfigCallback = new SshTransportConfigCallback();

git = Git.cloneRepository()
        .setDirectory(workingDir)
        .setTransportConfigCallback(transportConfigCallback)
        .setURI("ssh://example.com/repo.git")
        .call();


Our own implementation of the transport config callback configures the SSH communication for accessing the repository. We want to tell JGit to use SSH communication and to ignore the host key checking.

private static class SshTransportConfigCallback implements TransportConfigCallback {

    private final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
        @Override
        protected void configure(OpenSshConfig.Host hc, Session session) {
            session.setConfig("StrictHostKeyChecking", "no");
        }
    };

    @Override
    public void configure(Transport transport) {
        SshTransport sshTransport = (SshTransport) transport;
        sshTransport.setSshSessionFactory(sshSessionFactory);
    }

}


Per default, this will take the id_rsa key file, available under the ~/.ssh. If you want to specify another file location or configure a key secured by a passphrase, change the creation of the Java Secure Channel as well.

private static class SshTransportConfigCallback implements TransportConfigCallback {

    private final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
        @Override
        protected void configure(OpenSshConfig.Host hc, Session session) {
            session.setConfig("StrictHostKeyChecking", "no");
        }

        @Override
        protected JSch createDefaultJSch(FS fs) throws JSchException {
            JSch jSch = super.createDefaultJSch(fs);
            jSch.addIdentity("/path/to/key", "super-secret-passphrase".getBytes());
            return jSch;
        }
    };

    @Override
    public void configure(Transport transport) {
        SshTransport sshTransport = (SshTransport) transport;
        sshTransport.setSshSessionFactory(sshSessionFactory);
    }

}


Now you can issue commands on your Git handle.

For a full example on how to access Git repositories, see my AsciiBlog application.

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:
java ,git repositories ,ssh keys ,jgit ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}