-
Notifications
You must be signed in to change notification settings - Fork 242
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
馃尡 Validate Host DNS name in BareMetalHost resource #1040
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @hroyrh for the PR
if err != nil { | ||
host = hurl.Host | ||
} | ||
valid, _ := regexp.MatchString(`^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$`, host) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, we should also handle error case in here instead hiding it.
} | ||
valid, _ := regexp.MatchString(`^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$`, host) | ||
|
||
if !(valid) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no need for parenthesis in here.
@@ -71,6 +71,17 @@ func TestValidateCreate(t *testing.T) { | |||
oldBMH: nil, | |||
wantedErr: "hardwareRAIDVolumes and softwareRAIDVolumes can not be set at the same time", | |||
}, | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, we can add more test like happy path or more edge cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I will add a few more test cases.
} | ||
|
||
hurl, _ := url.Parse(hostaddress) | ||
host, port, _ := net.SplitHostPort(hurl.Host) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hroyrh, if port is empty host = hurl.Host
seems ok to me. But why we are not checking error in here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It raises error for port empty too and as for the validity of the url, the regex checks that.
/retest-required |
@@ -18,6 +21,10 @@ func (host *BareMetalHost) validateHost() []error { | |||
errs = append(errs, err) | |||
} | |||
|
|||
if err := validateDNSName(host.Spec.BMC.Address); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hroyrh hardwareutils/bmc package is added in this module. Maybe it would be useful using bmc's functionality for that validation if it is possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, the validation itself should probably go to the bmc package, if it's not there already
@@ -53,3 +60,24 @@ func validateRAID(r *RAIDConfig) error { | |||
|
|||
return nil | |||
} | |||
|
|||
func validateDNSName(hostaddress string) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hroyrh what I was thinking is something like that;
if hostaddress == "" {
return nil
}
hurl, err := url.Parse(hostaddress)
if err != nil {
if !strings.Contains(hostaddress, ":") {
return fmt.Errorf("Invalid format")
}
}
host, port, err := net.SplitHostPort(hurl.Host)
if err != nil {
return fmt.Errorf("another invalid format")
}
if port == "" {
host = hurl.Host
}
valid, err := regexp.MatchString(`^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$`, host)
if err != nil {
return fmt.Errorf("regex error")
}
if !valid {
return fmt.Errorf("Host DNS name is invalid")
}
return nil
/hold |
/hold cancel |
Issues go stale after 90d of inactivity. If this issue is safe to close now please do so with /lifecycle stale |
/remove-lifecycle stale |
/uncc |
260d5ce
to
2061aa4
Compare
|
||
_, err := bmc.GetParsedURL(hostaddress) | ||
if err != nil { | ||
return fmt.Errorf("host DNS name is invalid") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you include the original error using errors.Wrap
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dtantsur I've wrapped the errors, and added a few more test cases, can you please take another look.
/approve |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: dtantsur The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
/lgtm |
/test-centos-integration-main |
Validate host dns name part of BareMetalHost bmc address.