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

Check if bmc hostname follows DNS Standard #1068

Merged
merged 3 commits into from
May 10, 2022
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
33 changes: 30 additions & 3 deletions pkg/hardwareutils/bmc/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net"
"net/url"
"regexp"
"strings"

"github.com/pkg/errors"
Expand Down Expand Up @@ -86,8 +87,8 @@ type AccessDetails interface {
BuildBIOSSettings(firmwareConfig *FirmwareConfig) (settings []map[string]string, err error)
}

func getParsedURL(address string) (parsedURL *url.URL, err error) {
// Start by assuming "type://host:port"
func GetParsedURL(address string) (parsedURL *url.URL, err error) {

parsedURL, err = url.Parse(address)
if err != nil {
// We failed to parse the URL, but it may just be a host or
Expand Down Expand Up @@ -127,6 +128,12 @@ func getParsedURL(address string) (parsedURL *url.URL, err error) {
}
}
}

// Check for expected hostname format
if err := checkDNSValid(parsedURL.Hostname()); err != nil {
return nil, errors.Wrap(err, "failed to parse BMC address information")
}

return parsedURL, nil
}

Expand All @@ -138,7 +145,7 @@ func NewAccessDetails(address string, disableCertificateVerification bool) (Acce
return nil, errors.New("missing BMC address")
}

parsedURL, err := getParsedURL(address)
parsedURL, err := GetParsedURL(address)
if err != nil {
return nil, err
}
Expand All @@ -150,3 +157,23 @@ func NewAccessDetails(address string, disableCertificateVerification bool) (Acce

return factory(parsedURL, disableCertificateVerification)
}

func checkDNSValid(address string) error {

// Allowing empty BMC address
if address == "" {
return nil
}

// Check if its a IPv6/IPv4 address
if net.ParseIP(address) != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this line, we have a DNS value, why we are parsing assuming that it is IP?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In lot of tests we have IPv6 address ( and I think the BMC address hostname can be an IPv6 address) - this check is validate that. IPv4 addresses are anyways DNS standard compliant, so it doesn't need a check - I will remove IPv4 from the comment to avoid this confusion.

return nil
}

// Check if BMC address hostname follows DNS Standard
valid, _ := regexp.MatchString(`^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]{0,61}[A-Za-z0-9])$`, address)
if !valid {
return fmt.Errorf("BMC address hostname/IP : [%s] is invalid", address)
}
return nil
}
34 changes: 33 additions & 1 deletion pkg/hardwareutils/bmc/access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,38 @@ func TestParse(t *testing.T) {
Path: "",
},

{
Scenario: "valid dns hostname",
Address: "my.examplehost.com",
Type: "ipmi",
Port: "",
Host: "my.examplehost.com",
Hostname: "my.examplehost.com",
Path: "",
},

{
Scenario: "invalid dns hostname",
Address: "my-.examplehost.com",
Type: "ipmi",
Port: "",
Host: "my-.examplehost.com",
Hostname: "my-.examplehost.com",
Path: "",
ExpectError: true,
},

{
Scenario: "invalid ipv6 host address",
Address: "[fe80::fc33:62ff:fe33:8xff]:6223",
Type: "ipmi",
Port: "6223",
Host: "fe80::fc33:62ff:fe33.8xff",
Hostname: "[fe80::fc33:62ff:fe33:8xff]:6223",
Path: "",
ExpectError: true,
},

{
Scenario: "host and port",
Address: "192.168.122.1:6233",
Expand Down Expand Up @@ -368,7 +400,7 @@ func TestParse(t *testing.T) {
},
} {
t.Run(tc.Scenario, func(t *testing.T) {
url, err := getParsedURL(tc.Address)
url, err := GetParsedURL(tc.Address)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, tests including invalid dns cases might be added?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We really need lots of tests to assure that is working.


if tc.ExpectError {
if err == nil {
Expand Down