Skip to content

Commit

Permalink
Format server URL in automatic (PXE) installation to allow server URL…
Browse files Browse the repository at this point in the history
… without port.

Signed-off-by: Yanhong Yang <[email protected]>
  • Loading branch information
Yanhong Yang authored and guangbochen committed Dec 8, 2022
1 parent 17ac46b commit 937102f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 9 deletions.
8 changes: 8 additions & 0 deletions pkg/console/install_panels.go
Original file line number Diff line number Diff line change
Expand Up @@ -1687,6 +1687,14 @@ func addInstallPanel(c *Console) error {
if c.config.TTY == "" {
c.config.TTY = getFirstConsoleTTY()
}
if c.config.ServerURL != "" {
formatted, err := getFormattedServerURL(c.config.ServerURL)
if err != nil {
printToPanel(c.Gui, fmt.Sprintf("server url invalid: %s", err), installPanel)
return
}
c.config.ServerURL = formatted
}

// lookup MAC Address to populate device names where needed
// lookup device name to populate MAC Address
Expand Down
34 changes: 30 additions & 4 deletions pkg/console/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ You can see the full installation log by:
- Login with user "rancher" (password is "rancher").
- Run the command: less %s.
`
https = "https://"
)

func newProxyClient() http.Client {
Expand Down Expand Up @@ -277,12 +278,37 @@ func getRemoteSSHKeys(url string) ([]string, error) {
}

func getFormattedServerURL(addr string) (string, error) {
ipErr := checkIP(addr)
domainErr := checkDomain(addr)
if ipErr != nil && domainErr != nil {
if addr == "" {
return "", errors.New("management address cannot be empty")
}
addr = strings.TrimSpace(addr)

realAddr := addr
if !strings.HasPrefix(addr, https) {
realAddr = https + addr
}
parsedUrl, err := url.ParseRequestURI(realAddr)
if err != nil {
return "", fmt.Errorf("%s is invalid", addr)
}

host := parsedUrl.Hostname()
if checkIP(host) != nil && checkDomain(host) != nil {
return "", fmt.Errorf("%s is not a valid ip/domain", addr)
}
return fmt.Sprintf("https://%s:%s", addr, rancherManagementPort), nil

if parsedUrl.Path != "" {
return "", fmt.Errorf("path is not allowed in management address: %s", parsedUrl.Path)
}

port := parsedUrl.Port()
if port == "" {
parsedUrl.Host += ":443"
} else if port != "443" {
return "", fmt.Errorf("currently non-443 port are not allowed")
}

return parsedUrl.String(), nil
}

func getServerURLFromRancherdConfig(data []byte) (string, error) {
Expand Down
28 changes: 23 additions & 5 deletions pkg/console/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,34 @@ func TestGetFormattedServerURL(t *testing.T) {
err: nil,
},
{
Name: "invalid ip",
Name: "ip without port and scheme",
input: "1.1.1.1",
output: "https://1.1.1.1:443",
err: nil,
},
{
Name: "domain without port and scheme",
input: "abc.org",
output: "https://abc.org:443",
err: nil,
},
{
Name: "custom port",
input: "1.2.3.4:555",
output: "",
err: errors.New("currently non-443 port are not allowed"),
},
{
Name: "ip with path",
input: "1.2.3.4/",
output: "",
err: errors.New("1.2.3.4/ is not a valid ip/domain"),
err: errors.New("path is not allowed in management address: /"),
},
{
Name: "invalid domain",
input: "example.org/",
Name: "domain with path",
input: "abc.org/test/abc",
output: "",
err: errors.New("example.org/ is not a valid ip/domain"),
err: errors.New("path is not allowed in management address: /test/abc"),
},
}
for _, testCase := range testCases {
Expand Down

0 comments on commit 937102f

Please sign in to comment.