diff --git a/cmd/server-startup-msg.go b/cmd/server-startup-msg.go index a56c2ac98..9f4549eee 100644 --- a/cmd/server-startup-msg.go +++ b/cmd/server-startup-msg.go @@ -19,6 +19,7 @@ package cmd import ( "crypto/x509" "fmt" + "net/url" "runtime" "strings" @@ -44,12 +45,14 @@ func getFormatStr(strLen int, padding int) string { // Prints the formatted startup message. func printStartupMessage(apiEndPoints []string) { + strippedAPIEndpoints := stripStandardPorts(apiEndPoints) + // Prints credential, region and browser access. - printServerCommonMsg(apiEndPoints) + printServerCommonMsg(strippedAPIEndpoints) // Prints `mc` cli configuration message chooses // first endpoint as default. - printCLIAccessMsg(apiEndPoints[0]) + printCLIAccessMsg(strippedAPIEndpoints[0]) // Prints documentation message. printObjectAPIMsg() @@ -67,6 +70,34 @@ func printStartupMessage(apiEndPoints []string) { } } +// strip api endpoints list with standard ports such as +// port "80" and "443" before displaying on the startup +// banner. Returns a new list of API endpoints. +func stripStandardPorts(apiEndpoints []string) (newAPIEndpoints []string) { + newAPIEndpoints = make([]string, len(apiEndpoints)) + // Check all API endpoints for standard ports and strip them. + for i, apiEndpoint := range apiEndpoints { + url, err := url.Parse(apiEndpoint) + if err != nil { + newAPIEndpoints[i] = apiEndpoint + continue + } + host, port := mustSplitHostPort(url.Host) + // For standard HTTP(s) ports such as "80" and "443" + // apiEndpoints should only be host without port. + switch { + case url.Scheme == "http" && port == "80": + fallthrough + case url.Scheme == "https" && port == "443": + url.Host = host + newAPIEndpoints[i] = url.String() + default: + newAPIEndpoints[i] = apiEndpoint + } + } + return newAPIEndpoints +} + // Prints common server startup message. Prints credential, region and browser access. func printServerCommonMsg(apiEndpoints []string) { // Get saved credentials. @@ -76,6 +107,7 @@ func printServerCommonMsg(apiEndpoints []string) { region := serverConfig.GetRegion() apiEndpointStr := strings.Join(apiEndpoints, " ") + // Colorize the message and print. log.Println(colorBlue("\nEndpoint: ") + colorBold(fmt.Sprintf(getFormatStr(len(apiEndpointStr), 1), apiEndpointStr))) log.Println(colorBlue("AccessKey: ") + colorBold(fmt.Sprintf("%s ", cred.AccessKey))) diff --git a/cmd/server-startup-msg_test.go b/cmd/server-startup-msg_test.go index ef39b9e18..2da77901c 100644 --- a/cmd/server-startup-msg_test.go +++ b/cmd/server-startup-msg_test.go @@ -20,6 +20,7 @@ import ( "crypto/x509" "crypto/x509/pkix" "fmt" + "reflect" "strings" "testing" "time" @@ -95,6 +96,29 @@ func TestCertificateNotExpired(t *testing.T) { } } +// Tests stripping standard ports from apiEndpoints. +func TestStripStandardPorts(t *testing.T) { + apiEndpoints := []string{"http://127.0.0.1:9000", "http://127.0.0.2:80", "https://127.0.0.3:443"} + expectedAPIEndpoints := []string{"http://127.0.0.1:9000", "http://127.0.0.2", "https://127.0.0.3"} + newAPIEndpoints := stripStandardPorts(apiEndpoints) + + if !reflect.DeepEqual(expectedAPIEndpoints, newAPIEndpoints) { + t.Fatalf("Expected %#v, got %#v", expectedAPIEndpoints, newAPIEndpoints) + } + + apiEndpoints = []string{"http://%%%%%:9000"} + newAPIEndpoints = stripStandardPorts(apiEndpoints) + if !reflect.DeepEqual(apiEndpoints, newAPIEndpoints) { + t.Fatalf("Expected %#v, got %#v", apiEndpoints, newAPIEndpoints) + } + + apiEndpoints = []string{"http://127.0.0.1:443", "https://127.0.0.1:80"} + newAPIEndpoints = stripStandardPorts(apiEndpoints) + if !reflect.DeepEqual(apiEndpoints, newAPIEndpoints) { + t.Fatalf("Expected %#v, got %#v", apiEndpoints, newAPIEndpoints) + } +} + // Test printing server common message. func TestPrintServerCommonMessage(t *testing.T) { root, err := newTestConfig(globalMinioDefaultRegion) @@ -103,7 +127,7 @@ func TestPrintServerCommonMessage(t *testing.T) { } defer removeAll(root) - apiEndpoints := []string{"127.0.0.1:9000"} + apiEndpoints := []string{"http://127.0.0.1:9000"} printServerCommonMsg(apiEndpoints) } @@ -115,7 +139,7 @@ func TestPrintCLIAccessMsg(t *testing.T) { } defer removeAll(root) - apiEndpoints := []string{"127.0.0.1:9000"} + apiEndpoints := []string{"http://127.0.0.1:9000"} printCLIAccessMsg(apiEndpoints[0]) } @@ -127,6 +151,6 @@ func TestPrintStartupMessage(t *testing.T) { } defer removeAll(root) - apiEndpoints := []string{"127.0.0.1:9000"} + apiEndpoints := []string{"http://127.0.0.1:9000"} printStartupMessage(apiEndpoints) }