git-lfs/lfsapi/endpoint_finder.go

327 lines
7.7 KiB
Go
Raw Normal View History

2016-12-19 18:45:22 +00:00
package lfsapi
import (
"fmt"
"net/url"
"os"
"path"
"strings"
"sync"
"github.com/git-lfs/git-lfs/config"
2016-12-19 18:45:22 +00:00
"github.com/git-lfs/git-lfs/git"
"github.com/git-lfs/git-lfs/lfshttp"
"github.com/rubyist/tracerx"
2016-12-19 18:45:22 +00:00
)
2018-09-20 17:21:42 +00:00
type AccessMode string
2016-12-19 18:55:47 +00:00
const (
2018-09-20 17:21:42 +00:00
NoneAccess AccessMode = "none"
BasicAccess AccessMode = "basic"
PrivateAccess AccessMode = "private"
NegotiateAccess AccessMode = "negotiate"
NTLMAccess AccessMode = "ntlm"
emptyAccess AccessMode = ""
defaultRemote = "origin"
2016-12-19 18:55:47 +00:00
)
2016-12-19 18:45:22 +00:00
type Access struct {
mode AccessMode
url string
}
// Returns a copy of an AccessMode with the mode upgraded to newMode
func (a *Access) Upgrade(newMode AccessMode) Access {
return Access{url: a.url, mode: newMode}
}
func (a *Access) GetMode() AccessMode {
return a.mode
}
2016-12-19 18:45:22 +00:00
type EndpointFinder interface {
NewEndpointFromCloneURL(rawurl string) lfshttp.Endpoint
NewEndpoint(rawurl string) lfshttp.Endpoint
Endpoint(operation, remote string) lfshttp.Endpoint
RemoteEndpoint(operation, remote string) lfshttp.Endpoint
2016-12-19 18:45:22 +00:00
GitRemoteURL(remote string, forpush bool) string
AccessFor(rawurl string) Access
SetAccess(access Access)
2016-12-19 18:45:22 +00:00
GitProtocol() string
}
type endpointGitFinder struct {
gitConfig *git.Configuration
gitEnv config.Environment
2016-12-19 18:45:22 +00:00
gitProtocol string
aliasMu sync.Mutex
aliases map[string]string
accessMu sync.Mutex
2018-09-20 17:21:42 +00:00
urlAccess map[string]AccessMode
urlConfig *config.URLConfig
2016-12-19 18:45:22 +00:00
}
func NewEndpointFinder(ctx lfshttp.Context) EndpointFinder {
if ctx == nil {
ctx = lfshttp.NewContext(nil, nil, nil)
}
2016-12-19 18:45:22 +00:00
e := &endpointGitFinder{
gitConfig: ctx.GitConfig(),
gitEnv: ctx.GitEnv(),
2016-12-19 18:45:22 +00:00
gitProtocol: "https",
aliases: make(map[string]string),
2018-09-20 17:21:42 +00:00
urlAccess: make(map[string]AccessMode),
2016-12-19 18:45:22 +00:00
}
e.urlConfig = config.NewURLConfig(e.gitEnv)
if v, ok := e.gitEnv.Get("lfs.gitprotocol"); ok {
e.gitProtocol = v
2016-12-19 18:45:22 +00:00
}
initAliases(e, e.gitEnv)
2016-12-19 18:45:22 +00:00
return e
}
func (e *endpointGitFinder) Endpoint(operation, remote string) lfshttp.Endpoint {
ep := e.getEndpoint(operation, remote)
ep.Operation = operation
return ep
}
func (e *endpointGitFinder) getEndpoint(operation, remote string) lfshttp.Endpoint {
if e.gitEnv == nil {
return lfshttp.Endpoint{}
2016-12-19 18:45:22 +00:00
}
if operation == "upload" {
if url, ok := e.gitEnv.Get("lfs.pushurl"); ok {
2016-12-19 18:45:22 +00:00
return e.NewEndpoint(url)
}
}
if url, ok := e.gitEnv.Get("lfs.url"); ok {
2016-12-19 18:45:22 +00:00
return e.NewEndpoint(url)
}
if len(remote) > 0 && remote != defaultRemote {
if e := e.RemoteEndpoint(operation, remote); len(e.Url) > 0 {
return e
}
}
return e.RemoteEndpoint(operation, defaultRemote)
}
func (e *endpointGitFinder) RemoteEndpoint(operation, remote string) lfshttp.Endpoint {
if e.gitEnv == nil {
return lfshttp.Endpoint{}
2016-12-19 18:45:22 +00:00
}
if len(remote) == 0 {
remote = defaultRemote
}
// Support separate push URL if specified and pushing
if operation == "upload" {
if url, ok := e.gitEnv.Get("remote." + remote + ".lfspushurl"); ok {
2016-12-19 18:45:22 +00:00
return e.NewEndpoint(url)
}
}
if url, ok := e.gitEnv.Get("remote." + remote + ".lfsurl"); ok {
2016-12-19 18:45:22 +00:00
return e.NewEndpoint(url)
}
// finally fall back on git remote url (also supports pushurl)
if url := e.GitRemoteURL(remote, operation == "upload"); url != "" {
return e.NewEndpointFromCloneURL(url)
}
return lfshttp.Endpoint{}
2016-12-19 18:45:22 +00:00
}
func (e *endpointGitFinder) GitRemoteURL(remote string, forpush bool) string {
if e.gitEnv != nil {
2016-12-19 18:45:22 +00:00
if forpush {
if u, ok := e.gitEnv.Get("remote." + remote + ".pushurl"); ok {
2016-12-19 18:45:22 +00:00
return u
}
}
if u, ok := e.gitEnv.Get("remote." + remote + ".url"); ok {
2016-12-19 18:45:22 +00:00
return u
}
}
if err := git.ValidateRemote(remote); err == nil {
return remote
}
return ""
}
func (e *endpointGitFinder) NewEndpointFromCloneURL(rawurl string) lfshttp.Endpoint {
2016-12-19 18:45:22 +00:00
ep := e.NewEndpoint(rawurl)
if ep.Url == lfshttp.UrlUnknown {
2016-12-19 18:45:22 +00:00
return ep
}
if strings.HasSuffix(rawurl, "/") {
ep.Url = rawurl[0 : len(rawurl)-1]
}
// When using main remote URL for HTTP, append info/lfs
if path.Ext(ep.Url) == ".git" {
ep.Url += "/info/lfs"
} else {
ep.Url += ".git/info/lfs"
}
return ep
}
func (e *endpointGitFinder) NewEndpoint(rawurl string) lfshttp.Endpoint {
2016-12-19 18:45:22 +00:00
rawurl = e.ReplaceUrlAlias(rawurl)
if strings.HasPrefix(rawurl, "/") {
return lfshttp.EndpointFromLocalPath(rawurl)
}
2016-12-19 18:45:22 +00:00
u, err := url.Parse(rawurl)
if err != nil {
return lfshttp.EndpointFromBareSshUrl(rawurl)
2016-12-19 18:45:22 +00:00
}
switch u.Scheme {
case "ssh":
return lfshttp.EndpointFromSshUrl(u)
2016-12-19 18:45:22 +00:00
case "http", "https":
return lfshttp.EndpointFromHttpUrl(u)
2016-12-19 18:45:22 +00:00
case "git":
return endpointFromGitUrl(u, e)
case "":
return lfshttp.EndpointFromBareSshUrl(u.String())
2016-12-19 18:45:22 +00:00
default:
if strings.HasPrefix(rawurl, u.Scheme+"::") {
// Looks like a remote helper; just pass it through.
return lfshttp.Endpoint{Url: rawurl}
}
// We probably got here because the "scheme" that was parsed is
// a hostname (whether FQDN or single word) and the URL parser
// didn't know what to do with it. Do what Git does and treat
// it as an SSH URL. This ensures we handle SSH config aliases
// properly.
return lfshttp.EndpointFromBareSshUrl(u.String())
2016-12-19 18:45:22 +00:00
}
}
func (e *endpointGitFinder) AccessFor(rawurl string) Access {
accessurl := urlWithoutAuth(rawurl)
if e.gitEnv == nil {
return Access{mode: NoneAccess, url: accessurl}
2016-12-19 18:55:47 +00:00
}
e.accessMu.Lock()
defer e.accessMu.Unlock()
if cached, ok := e.urlAccess[accessurl]; ok {
return Access{mode: cached, url: accessurl}
}
e.urlAccess[accessurl] = e.fetchGitAccess(accessurl)
return Access{mode: e.urlAccess[accessurl], url: accessurl}
}
func (e *endpointGitFinder) SetAccess(access Access) {
key := fmt.Sprintf("lfs.%s.access", access.url)
tracerx.Printf("setting repository access to %s", access.mode)
e.accessMu.Lock()
defer e.accessMu.Unlock()
switch access.mode {
case emptyAccess, NoneAccess:
2017-10-26 01:20:35 +00:00
e.gitConfig.UnsetLocalKey(key)
e.urlAccess[access.url] = NoneAccess
default:
e.gitConfig.SetLocal(key, string(access.mode))
e.urlAccess[access.url] = access.mode
}
}
func urlWithoutAuth(rawurl string) string {
if !strings.Contains(rawurl, "@") {
return rawurl
}
u, err := url.Parse(rawurl)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing URL %q: %s", rawurl, err)
return rawurl
}
u.User = nil
return u.String()
}
2018-09-20 17:21:42 +00:00
func (e *endpointGitFinder) fetchGitAccess(rawurl string) AccessMode {
if v, _ := e.urlConfig.Get("lfs", rawurl, "access"); len(v) > 0 {
2018-09-20 17:21:42 +00:00
access := AccessMode(strings.ToLower(v))
if access == PrivateAccess {
2016-12-19 18:55:47 +00:00
return BasicAccess
}
return access
2016-12-19 18:55:47 +00:00
}
return NoneAccess
}
2016-12-19 18:45:22 +00:00
func (e *endpointGitFinder) GitProtocol() string {
return e.gitProtocol
}
// ReplaceUrlAlias returns a url with a prefix from a `url.*.insteadof` git
// config setting. If multiple aliases match, use the longest one.
// See https://git-scm.com/docs/git-config for Git's docs.
func (e *endpointGitFinder) ReplaceUrlAlias(rawurl string) string {
e.aliasMu.Lock()
defer e.aliasMu.Unlock()
var longestalias string
for alias, _ := range e.aliases {
if !strings.HasPrefix(rawurl, alias) {
continue
}
if longestalias < alias {
longestalias = alias
}
}
if len(longestalias) > 0 {
return e.aliases[longestalias] + rawurl[len(longestalias):]
}
return rawurl
}
func initAliases(e *endpointGitFinder, git config.Environment) {
2016-12-19 18:45:22 +00:00
prefix := "url."
suffix := ".insteadof"
for gitkey, gitval := range git.All() {
if len(gitval) == 0 || !(strings.HasPrefix(gitkey, prefix) && strings.HasSuffix(gitkey, suffix)) {
2016-12-19 18:45:22 +00:00
continue
}
if _, ok := e.aliases[gitval[len(gitval)-1]]; ok {
2016-12-19 18:45:22 +00:00
fmt.Fprintf(os.Stderr, "WARNING: Multiple 'url.*.insteadof' keys with the same alias: %q\n", gitval)
}
e.aliases[gitval[len(gitval)-1]] = gitkey[len(prefix) : len(gitkey)-len(suffix)]
2016-12-19 18:45:22 +00:00
}
}
func endpointFromGitUrl(u *url.URL, e *endpointGitFinder) lfshttp.Endpoint {
u.Scheme = e.gitProtocol
return lfshttp.Endpoint{Url: u.String()}
}