Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

failed to authenticate SSH session #1139

Open
harilet opened this issue Mar 7, 2025 · 4 comments
Open

failed to authenticate SSH session #1139

harilet opened this issue Mar 7, 2025 · 4 comments

Comments

@harilet
Copy link

harilet commented Mar 7, 2025

I am trying to push to a github repo using the ssh remote but I am getting an error
failed to authenticate SSH session: ; class=Ssh (23)

the code

fn push(repo_location: &str, origin: &str) {
    let repo = Repository::open(repo_location).unwrap();
    let mut origin = repo.find_remote(&origin).unwrap();

    let branch = repo.find_branch(&"main", BranchType::Local).unwrap();

    let branch_ref = branch.into_reference();
    let branch_ref_name = branch_ref.name().unwrap();
    repo.set_head(branch_ref_name).unwrap();

    let mut remote_callbacks = git2::RemoteCallbacks::new();
    remote_callbacks.credentials(|_url, _username_from_url, _allowed_types| {
        match git2::Cred::ssh_key(
            _username_from_url.unwrap(),
            // Some(std::path::Path::new("<home>/.ssh/github/id_rsa.pub")),
            None,
            std::path::Path::new("<home>/.ssh/github/id_rsa"),
            None,
        ) {
            Ok(cred) => {
                println!("credential creation success");
                Ok(cred)
            }
            Err(e) => {
                println!("credential creation Failed: {:#?}", e);
                Err(e)
            }
        }
    });

    let mut push_options = git2::PushOptions::new();
    let mut_push_options = push_options.remote_callbacks(remote_callbacks);

    match origin.push(&[branch_ref_name], Some(mut_push_options)) {
        Ok(_) => {
            println!("push-pushed to remote")
        }
        Err(e) => {
            println!("push-failed to push to remote: {}", e)
        }
    };
}

and I am getting this output

Image

and I am able to read the key file

Image

can anyone help?

@harilet
Copy link
Author

harilet commented Mar 7, 2025

if I also add the public key it's an infinite loop not stopping and re-trying

@harilet
Copy link
Author

harilet commented Mar 9, 2025

@vlad-anger I saw you in a libgit2 PR. Can you help? or tag someone who can?

@vlad-anger
Copy link
Contributor

Can you help?

@harilet Yes

  1. Your code works. I've tested (with ssh auth, all good)
  2. Problem you have is related to ssh agent. You need to ensure you have proper one running before invoke program. I would recommend you to check
    general info + agent discussion
    Run agent windows/mac

@harilet
Copy link
Author

harilet commented Mar 9, 2025

so even though git push origin main works for me and I am passing the key in the following code and not using the ssh_key_from_agent still need to use ssh agent?

git2::Cred::ssh_key(
            _username_from_url.unwrap(),
            // Some(std::path::Path::new("<home>/.ssh/github/id_rsa.pub")),
            None,
            std::path::Path::new("<home>/.ssh/github/id_rsa"),
            None,
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants