Libgit2 - 无法验证 SSH 会话:无法打开公钥文件

Libgit2 - Failed to authenticate SSH session: Unable to open public key file

本文关键字:公钥 文件 SSH 验证 Libgit2 会话      更新时间:2023-10-16

我正在尝试使用 ssh 通过 Libgit2 的网络做一个非常简单的 git 克隆 - 在此过程中获得标题中的错误。我不确定我做错了什么 - 这不是网络问题,因为我可以通过命令行克隆存储库。键也位于指定的路径中。

此外,密钥已经设置好,如果我想 ssh 到我尝试克隆的机器,我只需要提供一个密码,所以不确定为什么我需要在这里重新定义它。

#include <git2.h>
#include <iostream> 
// Callback function
int cred_cb(git_cred **out, const char *url, const char *username_from_url,
unsigned int allowed_types, void *payload) {
// https://libgit2.github.com/libgit2/#HEAD/group/cred/git_cred_ssh_key_new
return git_cred_ssh_key_new(out, "username", 
"~/.ssh/id_rsa.pub", "~/.ssh/id_rsa", "password");
}
int main() {
// Start up libgit2
git_libgit2_init();
// Test clone setup
git_repository *repo = NULL;
const char *url = "ssh://username@66.66.66.666/home/git_repo_to_clone";
const char *path = "tmp";
// Test clone
git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT;
clone_opts.fetch_opts.callbacks.credentials = cred_cb;
int error = git_clone(&repo, url, path, &clone_opts);
// Prints the last error message
std::cout << giterr_last()->message << std::endl;
// Clean up
git_libgit2_shutdown();
return 0;
}

cred_cb中打印allowed_types表示22,因此如果您尝试返回不同类型的git_cred(例如,git_cred_userpass_plaintext),库会抱怨callback returned unsupported credentials type,并且如果没有指定回调(通过调用git_cloneNULL作为第三个参数),那么它说authentication required but no callback set。我可能错过了一些明显的东西,我将不胜感激任何帮助,谢谢。

编辑

相反,我尝试使用git_cred_ssh_key_from_agent(out, "username")它似乎做了一些事情(git 文件夹被克隆),尽管缺少文件。尽管现在的问题是它变成了cred_cbgit_clone之间的无限循环(似乎来回调用)。

密钥文件的路径应该是不带 ~ 的完整路径。Libgit2 不会像 shell 那样扩展 ~ 。

此外,密码应该是用于加密私钥的密码,而不是用于向遥控器进行身份验证的密码。