Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix auth-url parsing if hostname misses a dot #818

Merged
merged 1 commit into from
Jul 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions pkg/converters/ingress/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"strings"
)

var parseURLRegex = regexp.MustCompile(`^([a-z]+)://([-a-z0-9]+/)?([^][/: ]+)(:[-a-z0-9]+)?([^"' ]*)$`)
var parseURLRegex = regexp.MustCompile(`^([a-z]+)://([-a-z0-9]+/)?([^][/: ]+)(:[-a-z0-9]+)?(/[^"' ]*)?$`)

// ParseURL ...
func ParseURL(url string) (urlProto, urlHost, urlPort, urlPath string, err error) {
Expand All @@ -33,22 +33,24 @@ func ParseURL(url string) (urlProto, urlHost, urlPort, urlPath string, err error
}
urlProto = urlParse[1]
namespace := urlParse[2]
urlHost = urlParse[3]
name := urlParse[3]
urlPort = strings.TrimLeft(urlParse[4], ":")
urlPath = urlParse[5]
if namespace != "" {
if urlPort == "" && urlPath == "" {
// the `proto://name/name` case, we want `name/path` instead of `namespace/name`
// this will probably never happen because, if proto == http/s, the first name
// should be an IP or a valid hostname, which doesn't match the namespace submatch;
// if proto == svc, port number is mandatory and would move the second name to the
// urlPath submatch
urlPath = "/" + urlHost
urlHost = strings.TrimSuffix(namespace, "/")
// <proto>://<namespace/><name>[:<urlPort>][<urlPath>]`
if urlPort != "" {
// port is mandatory on svc proto
// a matching namespace and a declared port
// ensures that we have a namespaced service name
urlHost = namespace + name
} else {
// this is really a namespace, so concatenate
urlHost = namespace + urlHost
// otherwise namespace is the host and name is the start of the path
urlHost = strings.TrimSuffix(namespace, "/")
urlPath = "/" + name + urlPath
}
} else {
// <proto>://<name>[:<urlPort>][<urlPath>]`
urlHost = name
}
return
}
29 changes: 27 additions & 2 deletions pkg/converters/ingress/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,45 @@ func TestParseURL(t *testing.T) {
exp: "proto | domain.local | | /app",
},
// 8
{
url: "proto://name/",
exp: "proto | name | | /",
},
// 9
{
url: "proto://name/app",
exp: "proto | name | | /app",
},
// 9
// 10
{
url: "proto://name/app/sub",
exp: "proto | name | | /app/sub",
},
// 11
{
url: "proto://name/app:8080",
exp: "proto | name/app | 8080 | ",
},
// 10
// 12
{
url: "proto://name/app:8080/sub",
exp: "proto | name/app | 8080 | /sub",
},
// 13
{
url: "proto://name:8080/app",
exp: "proto | name | 8080 | /app",
},
// 14
{
url: "proto://name:8080/app/sub",
exp: "proto | name | 8080 | /app/sub",
},
// 15
{
url: "proto://ns/name:8080/app/sub",
exp: "proto | ns/name | 8080 | /app/sub",
},
}
for i, test := range testCases {
proto, host, port, path, err := ParseURL(test.url)
Expand Down