Skip to content

Commit

Permalink
Merge pull request #1040 from hroyrh/bmo-validate-dnsname
Browse files Browse the repository at this point in the history
Validate Host DNS name in BareMetalHost resource
  • Loading branch information
metal3-io-bot committed Aug 4, 2022
2 parents 6966480 + 2a4e272 commit 22e2055
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
19 changes: 19 additions & 0 deletions apis/metal3.io/v1alpha1/baremetalhost_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"regexp"

"github.com/google/uuid"
"github.com/pkg/errors"
logf "sigs.k8s.io/controller-runtime/pkg/log"

"github.com/metal3-io/baremetal-operator/pkg/hardwareutils/bmc"
Expand Down Expand Up @@ -38,6 +39,10 @@ func (host *BareMetalHost) validateHost() []error {
errs = append(errs, err)
}

if err := validateDNSName(host.Spec.BMC.Address); err != nil {
errs = append(errs, err)
}

return errs
}

Expand Down Expand Up @@ -143,3 +148,17 @@ func validateBMHName(bmhname string) error {

return nil
}

func validateDNSName(hostaddress string) error {

if hostaddress == "" {
return nil
}

_, err := bmc.GetParsedURL(hostaddress)
if err != nil {
return errors.Wrap(err, "BMO validation")
}

return nil
}
88 changes: 88 additions & 0 deletions apis/metal3.io/v1alpha1/baremetalhost_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,94 @@ func TestValidateCreate(t *testing.T) {
oldBMH: nil,
wantedErr: fmt.Sprintf("the 'numberOfPhysicalDisks'[%d] and number of 'physicalDisks'[2] is not same for volume 0", numberOfPhysicalDisks),
},
{
name: "validDNSName",
newBMH: &BareMetalHost{
TypeMeta: tm,
ObjectMeta: om,
Spec: BareMetalHostSpec{
BMC: BMCDetails{
Address: "ipmi://host-0.example.com.org:6223"}}},
oldBMH: nil,
wantedErr: "",
},
{
name: "validDNSName2",
newBMH: &BareMetalHost{
TypeMeta: tm,
ObjectMeta: om,
Spec: BareMetalHostSpec{
BMC: BMCDetails{
Address: "ipmi://baremetalhost"}}},
oldBMH: nil,
wantedErr: "",
},
{
name: "validDNSName3",
newBMH: &BareMetalHost{
TypeMeta: tm,
ObjectMeta: om,
Spec: BareMetalHostSpec{
BMC: BMCDetails{
Address: "ipmi://[fe80::fc33:62ff:fe83:8a76]:6233"}}},
oldBMH: nil,
wantedErr: "",
},
{
name: "invalidDNSNameinvalidhyphenuse",
newBMH: &BareMetalHost{
TypeMeta: tm,
ObjectMeta: om,
Spec: BareMetalHostSpec{
BMC: BMCDetails{
Address: "ipmi://-host.example.com.org"}}},
oldBMH: nil,
wantedErr: "BMO validation: failed to parse BMC address information: BMC address hostname/IP : [-host.example.com.org] is invalid",
},
{
name: "invalidDNSNameinvalidcharacter",
newBMH: &BareMetalHost{
TypeMeta: tm,
ObjectMeta: om,
Spec: BareMetalHostSpec{
BMC: BMCDetails{
Address: "ipmi://host+1.example.com.org"}}},
oldBMH: nil,
wantedErr: "BMO validation: failed to parse BMC address information: BMC address hostname/IP : [host+1.example.com.org] is invalid",
},
{
name: "invalidDNSNameinvalidformat",
newBMH: &BareMetalHost{
TypeMeta: tm,
ObjectMeta: om,
Spec: BareMetalHostSpec{
BMC: BMCDetails{
Address: "[@]host.example.com"}}},
oldBMH: nil,
wantedErr: "BMO validation: failed to parse BMC address information: parse \"ipmi://[@]host.example.com\": net/url: invalid userinfo",
},
{
name: "invalidDNSNameinvalidbmc",
newBMH: &BareMetalHost{
TypeMeta: tm,
ObjectMeta: om,
Spec: BareMetalHostSpec{
BMC: BMCDetails{
Address: "ipm:host.example.com:6223"}}},
oldBMH: nil,
wantedErr: "Unknown BMC type 'ipm' for address ipm:host.example.com:6223",
},
{
name: "invalidDNSNameinvalidipv6",
newBMH: &BareMetalHost{
TypeMeta: tm,
ObjectMeta: om,
Spec: BareMetalHostSpec{
BMC: BMCDetails{
Address: "ipmi://[fe80::fc33:62ff:fe33:8xff]:6223"}}},
oldBMH: nil,
wantedErr: "BMO validation: failed to parse BMC address information: BMC address hostname/IP : [fe80::fc33:62ff:fe33:8xff] is invalid",
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 22e2055

Please sign in to comment.