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: port parsing when using HTTP/HTTPS with omitted port #3524

Merged
merged 4 commits into from
Jan 12, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 20 additions & 13 deletions server/tracedb/connection/port_linting.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,29 +74,36 @@ func formatAvailablePortsMessage(ports []string) string {
return fmt.Sprintf("%s, or %s", portsSeparatedByComma, lastPort)
}

var extractPortRegex = regexp.MustCompile("([0-9]+).*")
var extractPortRegex = regexp.MustCompile(":([0-9]+).*")

func parsePort(url string) string {
port := extractPort(url)
if port != "" {
return port
}

if strings.HasPrefix(url, "https://") {
return "443"
}

if strings.HasPrefix(url, "http://") {
return "80"
}

return ""
}

func extractPort(url string) string {
index := strings.LastIndex(url, ":")
if index < 0 {
return ""
}

substring := url[index+1:]
substring := url[index:]
regexGroups := extractPortRegex.FindStringSubmatch(substring)
if len(regexGroups) < 2 {
return ""
}

port := regexGroups[1]

if port == "1" {
if strings.Contains(url, "http") {
return "80"
} else {
return "443"
}
}

return port
return regexGroups[1]
}
24 changes: 24 additions & 0 deletions server/tracedb/connection/port_linting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,30 @@ func TestPortLinter(t *testing.T) {
ExpectedPorts: []string{"9200", "9250", "9300"},
ExpectedStatus: model.StatusWarning,
},
{
Name: "shouldGetPort80InHTTP",
Endpoints: []string{"http://tempo-us-central1.grafana.net"},
ExpectedPorts: []string{"80"},
ExpectedStatus: model.StatusPassed,
},
{
Name: "shouldGetPort443InHTTPS",
Endpoints: []string{"https://tempo-us-central1.grafana.net"},
ExpectedPorts: []string{"443"},
ExpectedStatus: model.StatusPassed,
},
{
Name: "shouldGetSpecifiedInHTTP",
Endpoints: []string{"http://tempo-us-central1.grafana.net:8081"},
ExpectedPorts: []string{"8081"},
ExpectedStatus: model.StatusPassed,
},
{
Name: "shouldGetPort443InHTTPS",
Endpoints: []string{"https://tempo-us-central1.grafana.net:8082"},
ExpectedPorts: []string{"8082"},
ExpectedStatus: model.StatusPassed,
},
}

for _, testCase := range testCases {
Expand Down