Merge pull request #4282 from bk2204/proxy-port

config: map missing port to default for HTTP key lookups
This commit is contained in:
brian m. carlson 2020-10-14 16:48:26 +00:00 committed by GitHub
commit 8d234c690b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 12 deletions

@ -113,7 +113,7 @@ func (c *URLConfig) getAll(prefix, rawurl, key string) []string {
}
// Rule #3: Port Number must match exactly
if searchURL.Port() != configURL.Port() {
if portForURL(searchURL) != portForURL(configURL) {
continue
}
@ -165,6 +165,23 @@ func (c *URLConfig) getAll(prefix, rawurl, key string) []string {
return c.git.GetAll(bestMatch.key)
}
func portForURL(u *url.URL) string {
port := u.Port()
if port != "" {
return port
}
switch u.Scheme {
case "http":
return "80"
case "https":
return "443"
case "ssh":
return "22"
default:
return ""
}
}
// compareHosts compares a hostname with a configuration hostname to determine
// a match. It returns an integer indicating the strength of the match, or 0 if
// the two hostnames did not match.

@ -8,17 +8,19 @@ import (
func TestURLConfig(t *testing.T) {
u := NewURLConfig(EnvironmentOf(MapFetcher(map[string][]string{
"http.key": []string{"root", "root-2"},
"http.https://host.com.key": []string{"host", "host-2"},
"http.https://user@host.com/a.key": []string{"user-a", "user-b"},
"http.https://user@host.com.key": []string{"user", "user-2"},
"http.https://host.com/a.key": []string{"host-a", "host-b"},
"http.https://host.com:8080.key": []string{"port", "port-2"},
"http.https://host.com/repo.git.key": []string{".git"},
"http.https://host.com/repo.key": []string{"no .git"},
"http.https://host.com/repo2.key": []string{"no .git"},
"http.http://host.com/repo.key": []string{"http"},
"http.https://host.*/a.key": []string{"wild"},
"http.key": []string{"root", "root-2"},
"http.https://host.com.key": []string{"host", "host-2"},
"http.https://user@host.com/a.key": []string{"user-a", "user-b"},
"http.https://user@host.com.key": []string{"user", "user-2"},
"http.https://host.com/a.key": []string{"host-a", "host-b"},
"http.https://host.com:8080.key": []string{"port", "port-2"},
"http.https://host.com/repo.git.key": []string{".git"},
"http.https://host.com/repo.key": []string{"no .git"},
"http.https://host.com/repo2.key": []string{"no .git"},
"http.http://host.com/repo.key": []string{"http"},
"http.https://host.com:443/repo3.git.key": []string{"port"},
"http.ssh://host.com:22/repo3.git.key": []string{"ssh-port"},
"http.https://host.*/a.key": []string{"wild"},
})))
getOne := map[string]string{
@ -34,10 +36,14 @@ func TestURLConfig(t *testing.T) {
"https://host.com/repo": "no .git",
"https://host.com/repo2.git/info/lfs/foo/bar": "no .git",
"https://host.com/repo2.git/info/lfs": "no .git",
"https://host.com:443/repo2.git/info/lfs": "no .git",
"https://host.com/repo2.git/info": "host-2", // doesn't match /.git/info/lfs\Z/
"https://host.com/repo2.git": "host-2", // ditto
"https://host.com/repo3.git/info/lfs": "port",
"ssh://host.com/repo3.git/info/lfs": "ssh-port",
"https://host.com/repo2": "no .git",
"http://host.com/repo": "http",
"http://host.com:80/repo": "http",
"https://host.wild/a/b/c": "wild",
}