Libgit2 - 无法验证 SSH session:无法打开 public 密钥文件
Libgit2 - Failed to authenticate SSH session: Unable to open public key file
我正在尝试通过网络使用 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
,因此如果您尝试 return 不同类型的 git_cred(例如,git_cred_userpass_plaintext
) 库抱怨 callback returned unsupported credentials type
,并且如果没有指定回调(通过调用 git_clone
并将 NULL
作为第三个参数),那么它会说 authentication required but no callback set
。我可能遗漏了一些明显的东西,我将不胜感激任何帮助,谢谢。
编辑
我改为尝试使用 git_cred_ssh_key_from_agent(out, "username")
,它似乎做了一些事情(git 文件夹被克隆),尽管缺少文件。虽然现在的问题是在cred_cb
和git_clone
之间变成了死循环(好像是来回调用的)。
关键文件的路径应该是没有~的完整路径。 Libgit2 不会像 shell 那样扩展 ~。
此外,密码应该是用于加密私钥的密码,而不是用于远程验证的密码。
我正在尝试通过网络使用 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
,因此如果您尝试 return 不同类型的 git_cred(例如,git_cred_userpass_plaintext
) 库抱怨 callback returned unsupported credentials type
,并且如果没有指定回调(通过调用 git_clone
并将 NULL
作为第三个参数),那么它会说 authentication required but no callback set
。我可能遗漏了一些明显的东西,我将不胜感激任何帮助,谢谢。
编辑
我改为尝试使用 git_cred_ssh_key_from_agent(out, "username")
,它似乎做了一些事情(git 文件夹被克隆),尽管缺少文件。虽然现在的问题是在cred_cb
和git_clone
之间变成了死循环(好像是来回调用的)。
关键文件的路径应该是没有~的完整路径。 Libgit2 不会像 shell 那样扩展 ~。
此外,密码应该是用于加密私钥的密码,而不是用于远程验证的密码。