mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 06:27:05 +00:00
pkg/credentialprovider: export URL parsing and matching helper functions
Signed-off-by: Andrew Sy Kim <kim.andrewsy@gmail.com>
This commit is contained in:
parent
5344afd4fb
commit
aadc1d25b3
@ -158,9 +158,10 @@ func isDefaultRegistryMatch(image string) bool {
|
||||
return !strings.ContainsAny(parts[0], ".:")
|
||||
}
|
||||
|
||||
// ParseSchemelessURL parses a schemeless url and returns a url.URL
|
||||
// url.Parse require a scheme, but ours don't have schemes. Adding a
|
||||
// scheme to make url.Parse happy, then clear out the resulting scheme.
|
||||
func parseSchemelessURL(schemelessURL string) (*url.URL, error) {
|
||||
func ParseSchemelessURL(schemelessURL string) (*url.URL, error) {
|
||||
parsed, err := url.Parse("https://" + schemelessURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -170,8 +171,8 @@ func parseSchemelessURL(schemelessURL string) (*url.URL, error) {
|
||||
return parsed, nil
|
||||
}
|
||||
|
||||
// split the host name into parts, as well as the port
|
||||
func splitURL(url *url.URL) (parts []string, port string) {
|
||||
// SplitURL splits the host name into parts, as well as the port
|
||||
func SplitURL(url *url.URL) (parts []string, port string) {
|
||||
host, port, err := net.SplitHostPort(url.Host)
|
||||
if err != nil {
|
||||
// could not parse port
|
||||
@ -180,20 +181,20 @@ func splitURL(url *url.URL) (parts []string, port string) {
|
||||
return strings.Split(host, "."), port
|
||||
}
|
||||
|
||||
// overloaded version of urlsMatch, operating on strings instead of URLs.
|
||||
func urlsMatchStr(glob string, target string) (bool, error) {
|
||||
globURL, err := parseSchemelessURL(glob)
|
||||
// URLsMatchStr is wrapper for URLsMatch, operating on strings instead of URLs.
|
||||
func URLsMatchStr(glob string, target string) (bool, error) {
|
||||
globURL, err := ParseSchemelessURL(glob)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
targetURL, err := parseSchemelessURL(target)
|
||||
targetURL, err := ParseSchemelessURL(target)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return urlsMatch(globURL, targetURL)
|
||||
return URLsMatch(globURL, targetURL)
|
||||
}
|
||||
|
||||
// check whether the given target url matches the glob url, which may have
|
||||
// URLsMatch checks whether the given target url matches the glob url, which may have
|
||||
// glob wild cards in the host name.
|
||||
//
|
||||
// Examples:
|
||||
@ -201,9 +202,9 @@ func urlsMatchStr(glob string, target string) (bool, error) {
|
||||
// globURL=*.docker.io, targetURL=not.right.io => no match
|
||||
//
|
||||
// Note that we don't support wildcards in ports and paths yet.
|
||||
func urlsMatch(globURL *url.URL, targetURL *url.URL) (bool, error) {
|
||||
globURLParts, globPort := splitURL(globURL)
|
||||
targetURLParts, targetPort := splitURL(targetURL)
|
||||
func URLsMatch(globURL *url.URL, targetURL *url.URL) (bool, error) {
|
||||
globURLParts, globPort := SplitURL(globURL)
|
||||
targetURLParts, targetPort := SplitURL(targetURL)
|
||||
if globPort != targetPort {
|
||||
// port doesn't match
|
||||
return false, nil
|
||||
@ -240,7 +241,7 @@ func (dk *BasicDockerKeyring) Lookup(image string) ([]AuthConfig, bool) {
|
||||
for _, k := range dk.index {
|
||||
// both k and image are schemeless URLs because even though schemes are allowed
|
||||
// in the credential configurations, we remove them in Add.
|
||||
if matched, _ := urlsMatchStr(k, image); matched {
|
||||
if matched, _ := URLsMatchStr(k, image); matched {
|
||||
ret = append(ret, dk.creds[k]...)
|
||||
}
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUrlsMatch(t *testing.T) {
|
||||
func TestURLsMatch(t *testing.T) {
|
||||
tests := []struct {
|
||||
globURL string
|
||||
targetURL string
|
||||
@ -112,7 +112,7 @@ func TestUrlsMatch(t *testing.T) {
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
matched, _ := urlsMatchStr(test.globURL, test.targetURL)
|
||||
matched, _ := URLsMatchStr(test.globURL, test.targetURL)
|
||||
if matched != test.matchExpected {
|
||||
t.Errorf("Expected match result of %s and %s to be %t, but was %t",
|
||||
test.globURL, test.targetURL, test.matchExpected, matched)
|
||||
|
Loading…
Reference in New Issue
Block a user