rust-lang / rust-lang/git2-rs

Question about remote clone

Open
#1,060 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
2.1k
Forks
450
Avg merge
11m
Merged PRs (30d)
1

Description

Overview

The provided documentation for cloning with an SSH key closely follows the code below. I keep getting an authentication error even after trying most of the closed-issue solutions in this repository. I suspect that the error is coming from libssh2 dependency because when I trace the bug it appears to be erroring out in remote.c.

The SSH key does work with the environment because it clones properly when using git in terminal

Error

Failed to clone: failed to start SSH session: Unable to exchange encryption keys; class=Ssh (23) 😡

Code

Rust Code
use git2::{build::RepoBuilder, Cred, Error, FetchOptions, RemoteCallbacks, Repository};
use std::env;
use std::path::Path;

fn main() {
    let url = "ssh://git@localhost:2222/home/git/repos/test.git";
    let path = Path::new("./test");

    let mut callbacks = RemoteCallbacks::new();

    callbacks.credentials(|_url, username_from_url, _allowed_types| {
        Cred::ssh_key(
            username_from_url.unwrap(),
            None,
            std::path::Path::new(&format!("{}/.ssh/id_rsa", env::var("HOME").unwrap())),
            None,
        )
    });

    let mut fetch_options = FetchOptions::new();
    fetch_options.remote_callbacks(callbacks);

    let mut builder = RepoBuilder::new();
    builder.fetch_options(fetch_options);

    match builder.clone(&url, &path) {
        Ok(repo) => println!("Successfully cloned into {}", repo.path().display()),
        Err(e) => println!("Failed to clone: {}", e),
    }
}

Enviroment

  • Client (Me): Windows 11
  • Git Server: Docker Container with ubuntu (see docker-compose below)
docker-compose.yml
services:
  git-server:
    build: .
    ports:
      - "2222:22"
    volumes:
      - ./ssh:/home/git/.ssh
dockerfile
FROM ubuntu:latest

# Update and install git and openssh-server
# Create the directory for the sshd server
RUN apt-get update && \
    apt-get install -y git openssh-server && \
    mkdir /var/run/sshd


# Switch to the git user and create the .ssh directory with git user permissions
RUN useradd -m git && \
    su git -c "mkdir -p ~/.ssh && chmod 700 ~/.ssh && touch ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

# ⚠️ Must add the ssh key to the authorized_keys file see read me for more
RUN chmod 700 /home/git/.ssh && \
    chmod 600 /home/git/.ssh/authorized_keys

RUN chown -R git:git /home/git/.ssh

# Create a folder for the git repositories
RUN su git -c "mkdir /home/git/repos"

# Create a test repository
RUN su git -c "cd /home/git/repos && git init --bare test.git"

# Expose the ssh port
EXPOSE 22

# Start the sshd server
CMD ["/usr/sbin/sshd", "-D"]
SSH key creation

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
I then put this key in the mount .ssh folder and put it in a file called authorized_keys.

Final Remark

I am just trying to get a test environment for a server set up so I can test it with my actual application. If anyone has an alternative approach.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the SSH clone with the Rust example, docker-compose.yml, and Dockerfile, then inspect the reported libssh2 remote.c failure. Compare the Rust/libgit2 path with the working terminal Git path and establish whether the issue is in the container SSH setup or the dependency; done means a reproducible diagnosis and actionable resolution.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, git, rust
Domain
networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.