59 lines
1.5 KiB
Go
59 lines
1.5 KiB
Go
package git
|
|
|
|
import (
|
|
"io"
|
|
"regexp"
|
|
|
|
"github.com/xor-gate/sshfp"
|
|
"gopkg.in/src-d/go-git.v4/plumbing/transport"
|
|
gitssh "gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
|
|
)
|
|
|
|
// repositoryDirName is the folder name where the repository is cloned
|
|
const repositoryDirName = "repository"
|
|
|
|
// Authorization returns a transport.AuthMethod that can be used to create a repository bucket. This AuthMetod uses
|
|
// SSH using a private key file.
|
|
func Authorization(username string, privateKeyFile string, sshfpEntries ...io.Reader) (transport.AuthMethod, error) {
|
|
pk, err := gitssh.NewPublicKeysFromFile(username, privateKeyFile, "")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
mc, err := sshfp.NewMemoryCache()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if len(sshfpEntries) == 1 && sshfpEntries[0] != nil {
|
|
var entries []*sshfp.Entry
|
|
entries, err = sshfp.ParseZone(sshfpEntries[0])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = mc.Add(entries...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
hkcb, err := sshfp.NewResolver(sshfp.WithCache(mc), sshfp.WithDNSClientConfigFromFile("/etc/resolv.conf"))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
pk.HostKeyCallbackHelper.HostKeyCallback = hkcb.HostKeyCallback
|
|
return pk, nil
|
|
}
|
|
|
|
// sanitizePathString converts any repository uri to a valid directory uri
|
|
func sanitizePathString(in string) string {
|
|
// TODO this is kind of a hack. Need same method as golang dep (cache).
|
|
reg, err := regexp.Compile(`[/@\\?%\*:|"<>\. ]+`)
|
|
if err != nil {
|
|
return in
|
|
}
|
|
return reg.ReplaceAllString(in, "")
|
|
}
|