NGit устанавливает соединение с файлом закрытого ключа
Я пытаюсь использовать NGit для подключения к Github (то есть, используя личный ключ и пароль).
Может ли кто-нибудь провести меня через это?
Моя нормальная выборка будет:
var git = Git.CloneRepository()
.SetDirectory(_properties.OutputPath)
.SetURI(_properties.SourceUrlPath)
.SetBranchesToClone(new Collection<string>() { "master" })
.SetCredentialsProvider(new UsernamePasswordCredentialsProvider("username","password"))
.SetTimeout(3600)
.Call();
как бы я сделал это с закрытым ключом?
1 ответ
Решение
После долгой битвы, многих поисков в Google примеров Jgit и еще большей боли, я нашел решение!
По сути, вы переопределяете и SessionFactory своим собственным и внедряете свой сертификат во время соединения:
public class CustomConfigSessionFactory : JschConfigSessionFactory
{
public string PrivateKey { get; set; }
public string PublicKey { get; set; }
protected override void Configure(OpenSshConfig.Host hc, Session session)
{
var config = new Properties();
config["StrictHostKeyChecking"] = "no";
config["PreferredAuthentications"] = "publickey";
session.SetConfig(config);
var jsch = this.GetJSch(hc, FS.DETECTED);
jsch.AddIdentity("KeyPair", Encoding.UTF8.GetBytes(PrivateKey), Encoding.UTF8.GetBytes(PublicKey), null);
}
}
А потом впрыскивать это так:
var customConfigSessionFactory = new CustomConfigSessionFactory();
customConfigSessionFactory.PrivateKey = properties.PrivateKey;
customConfigSessionFactory.PublicKey = properties.PublicKey;
NGit.Transport.JschConfigSessionFactory.SetInstance(customConfigSessionFactory);
var git = Git.CloneRepository()
.SetDirectory(properties.OutputPath)
.SetURI(properties.SourceUrlPath)
.SetBranchesToClone(new Collection<string>() { "master" })
.Call();