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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃尡 Validate Host DNS name in BareMetalHost resource #1040

Merged
merged 3 commits into from
Aug 4, 2022

Conversation

hroyrh
Copy link
Member

@hroyrh hroyrh commented Nov 24, 2021

Validate host dns name part of BareMetalHost bmc address.

@metal3-io-bot metal3-io-bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Nov 24, 2021
Copy link
Contributor

@ardaguclu ardaguclu left a 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)
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, 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) {
Copy link
Contributor

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",
},
{
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, we can add more test like happy path or more edge cases.

Copy link
Member Author

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)
Copy link
Contributor

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?

Copy link
Member Author

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.

@namnx228
Copy link
Member

namnx228 commented Dec 1, 2021

/retest-required

@@ -18,6 +21,10 @@ func (host *BareMetalHost) validateHost() []error {
errs = append(errs, err)
}

if err := validateDNSName(host.Spec.BMC.Address); err != nil {
Copy link
Contributor

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.

Copy link
Member

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 {
Copy link
Contributor

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

@metal3-io-bot metal3-io-bot added the needs-rebase Indicates that a PR cannot be merged because it has merge conflicts with HEAD. label Dec 14, 2021
@kashifest
Copy link
Member

/hold
Holding for a while. Changing the default branch reference to main from master. Once the transition is done I will unhold it.

@metal3-io-bot metal3-io-bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Dec 20, 2021
@fmuyassarov fmuyassarov changed the base branch from master to main December 20, 2021 12:23
@kashifest
Copy link
Member

/hold cancel
The switch is done. BMO's default branch is now main

@metal3-io-bot metal3-io-bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Dec 20, 2021
@metal3-io-bot
Copy link
Contributor

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues will close after an additional 30d of inactivity.

If this issue is safe to close now please do so with /close.

/lifecycle stale

@metal3-io-bot metal3-io-bot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Mar 20, 2022
@hroyrh
Copy link
Member Author

hroyrh commented Apr 11, 2022

/remove-lifecycle stale

@metal3-io-bot metal3-io-bot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Apr 11, 2022
@ardaguclu
Copy link
Contributor

/uncc

@metal3-io-bot metal3-io-bot removed the needs-rebase Indicates that a PR cannot be merged because it has merge conflicts with HEAD. label May 27, 2022

_, err := bmc.GetParsedURL(hostaddress)
if err != nil {
return fmt.Errorf("host DNS name is invalid")
Copy link
Member

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?

Copy link
Member Author

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.

@metal3-io-bot metal3-io-bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jun 1, 2022
@dtantsur
Copy link
Member

dtantsur commented Aug 3, 2022

/approve
/test-centos-integration-main
/test-ubuntu-integration-main

@metal3-io-bot
Copy link
Contributor

[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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@metal3-io-bot metal3-io-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 3, 2022
@elfosardo
Copy link
Member

/lgtm

@metal3-io-bot metal3-io-bot added the lgtm Indicates that a PR is ready to be merged. label Aug 3, 2022
@elfosardo
Copy link
Member

/test-centos-integration-main

@metal3-io-bot metal3-io-bot merged commit 22e2055 into metal3-io:main Aug 4, 2022
@furkatgofurov7 furkatgofurov7 changed the title Validate Host DNS name in BareMetalHost resource 馃尡 Validate Host DNS name in BareMetalHost resource Sep 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants