rust-lang / git2-rs

libgit2 bindings for Rust
https://docs.rs/git2
Apache License 2.0
1.69k stars 387 forks source link

error loading known_hosts: ; class=Ssh (23) when trying to connect to remote. #937

Closed tzAcee closed 1 year ago

tzAcee commented 1 year ago

Hey, I stumble accross an error "error loading known_hosts: ; class=Ssh (23)" when I try to connect to a remote host on our company azure repo. The git cmd tool works fine of course. So the general SSH configuration should be fine. Seems like the the configuration isn't read out correctly. Do I have to set a config of the repo or something before I connect to the remote?

I wrote a simple test tool for what Im doing:

fn main() { 
   let local_repo_path = r"D:\projects\some_repo_dir"; 
   env::set_var("GIT_SSH_COMMAND", r"ssh -i *ID_RSA_PATH* -o UserKnownHostsFile=*KNOWN_HOSTS_PATH*"); // Added testwise, but it does nothing
   let repo = Repository::open(local_repo_path);
   match repo {
    Ok(repo_unwrap) => {
        let mut remote = repo_unwrap.find_remote("origin").unwrap();
        let res = remote.connect(git2::Direction::Fetch); // also tested connect_auth
        if res.is_err() {
            println!("Could not connect {}", res.err().unwrap());
        }
        },
        Err(_) => println!("Could not open Repo."),
    }
}
ehuss commented 1 year ago

libgit2 only reads known_hosts from $HOME/.ssh/known_hosts. Do you have a file at that location? Does it have any unusual syntax?

Which version of libgit2-sys are you using? Are you using vendored or system libgit2?

tzAcee commented 1 year ago

libgit2 only reads known_hosts from $HOME/.ssh/known_hosts. Do you have a file at that location? Does it have any unusual syntax?

Which version of libgit2-sys are you using? Are you using vendored or system libgit2?

The Syntax of my file known_hosts should be correct. I tried to recreate it multiple times. Tbh. I dont know which version of libgit2 im using. I just added with with cargo add git2, so in the cargo.toml the current version is v0.16.1. 😂

But I think fetching the $HOME env variable on windows is an issue or am I wrong? Since I think it should be %userprofile% on windows, because the HOME env variable isn't set there. Atleast I have the known_hosts file in '%userprofile%/.ssh/known_hosts'

ehuss commented 1 year ago

Ah, yea, I recall now that was a problem with the current version. This should be fixed by #935 which reads windows home directories correctly.

martinellison commented 1 year ago

I am getting the same error on Android. It used to work until recently.

Does anyone know a fix? Or a solution? My app is not working anymore.

tzAcee commented 1 year ago

I dont know whether the fix is released yet. But if not, you could get the cargo package directly from this repository, since the fix is already merged on master. If it's an android specific problem the fix wasn't enough.

For Windows I haven't tested it myself yet, so idk if it's working.

martinellison commented 1 year ago

As edited: I had a build error, but I think I have fixed it. I think that this was because the git_credentials package is locked to point to version 0.16 of git2 instead of the latest version 0.17.

But I am still getting the error with known_hosts.

martinellison commented 1 year ago

Edit: I think my error is different, and needs Android expertise, so I am raising a separate issue on the libgit2 tracker.

The new issue is https://github.com/libgit2/libgit2/issues/6550.

ehuss commented 1 year ago

I'm going to close since this should be fixed in the latest version via #935. If you can still reproduce with 0.17.1, feel free to comment, and please include as much detail as possible to reproduce your environment.