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 1 commit
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
28 changes: 25 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,12 @@ 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) {
// Check for expected format
if err := checkDNSValid(address); err != nil {
return nil, errors.Wrap(err, "failed to parse BMC address information")
}

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 @@ -138,7 +143,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 +155,20 @@ 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 BMC address follows the format: Type://DNS_Standard_Hostname:Port
// Type and Port are optional
valid, _ := regexp.MatchString(`^([a-zA-Z]+\:\/\/)?(([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])(\:[0-9]+)?$`, address)
if !valid {
return fmt.Errorf("BMC address invalid, expected format : Type://DNS_Standard_Hostname:Port")
}

return nil
}
2 changes: 1 addition & 1 deletion pkg/hardwareutils/bmc/access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,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