DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Deploying a Kotlin App to Heroku
  • JGit Library Examples in Java
  • How to Setup the Spring Cloud Config Server With Git
  • Basic Guide for Debian Packaging (NodeJS)

Trending

  • Building Scalable and Resilient Data Pipelines With Apache Airflow
  • Apache Doris vs Elasticsearch: An In-Depth Comparative Analysis
  • Power BI Embedded Analytics — Part 2: Power BI Embedded Overview
  • Scalable System Design: Core Concepts for Building Reliable Software
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Accessing Git Repos With Java Using SSH Keys

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.

By 
Sebastian Daschner user avatar
Sebastian Daschner
·
Dec. 25, 17 · Tutorial
Likes (8)
Comment
Save
Tweet
Share
29.5K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Git Java (programming language)

Published at DZone with permission of Sebastian Daschner. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Deploying a Kotlin App to Heroku
  • JGit Library Examples in Java
  • How to Setup the Spring Cloud Config Server With Git
  • Basic Guide for Debian Packaging (NodeJS)

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!