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

🌱 Improve webhook verification #1107

Merged
merged 3 commits into from
Aug 16, 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
22 changes: 22 additions & 0 deletions apis/metal3.io/v1alpha1/baremetalhost_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package v1alpha1
import (
"fmt"
"net"
"net/url"
"regexp"

"github.com/google/uuid"
Expand Down Expand Up @@ -43,6 +44,12 @@ func (host *BareMetalHost) validateHost() []error {
errs = append(errs, err)
}

if host.Spec.Image != nil {
if err := validateImageURL(host.Spec.Image.URL); err != nil {
errs = append(errs, err)
}
}

return errs
}

Expand All @@ -69,6 +76,7 @@ func (host *BareMetalHost) validateChanges(old *BareMetalHost) []error {

func validateBMCAccess(s BareMetalHostSpec, bmcAccess bmc.AccessDetails) []error {
var errs []error
diskFormat := "live-iso"

if bmcAccess == nil {
return errs
Expand Down Expand Up @@ -101,6 +109,10 @@ func validateBMCAccess(s BareMetalHostSpec, bmcAccess bmc.AccessDetails) []error
errs = append(errs, fmt.Errorf("BMC driver %s does not support secure boot", bmcAccess.Type()))
}

if s.Image != nil && s.Image.DiskFormat != nil && *s.Image.DiskFormat == diskFormat && !bmcAccess.SupportsISOPreprovisioningImage() {
errs = append(errs, fmt.Errorf("BMC driver %s does not support live-iso image", bmcAccess.Type()))
}

return errs
}

Expand Down Expand Up @@ -162,3 +174,13 @@ func validateDNSName(hostaddress string) error {

return nil
}

func validateImageURL(imageURL string) error {

_, err := url.ParseRequestURI(imageURL)
if err != nil {
return fmt.Errorf("Image URL %s is an invalid URL", imageURL)
}

return nil
}
38 changes: 38 additions & 0 deletions apis/metal3.io/v1alpha1/baremetalhost_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func TestValidateCreate(t *testing.T) {

// for RAID validation test cases
numberOfPhysicalDisks := 3
diskFormat := "live-iso"

tests := []struct {
name string
Expand Down Expand Up @@ -437,6 +438,43 @@ func TestValidateCreate(t *testing.T) {
oldBMH: nil,
wantedErr: "BMO validation: failed to parse BMC address information: BMC address hostname/IP : [fe80::fc33:62ff:fe33:8xff] is invalid",
},
{
name: "invalidImageURL",
newBMH: &BareMetalHost{
TypeMeta: tm,
ObjectMeta: om,
Spec: BareMetalHostSpec{
BMC: BMCDetails{
Address: "idrac://127.0.0.1",
CredentialsName: "test1",
},
Image: &Image{
URL: "test1",
},
},
},
oldBMH: nil,
wantedErr: "Image URL test1 is an invalid URL",
},
{
name: "liveISOImageWithUnsupportedBMC",
newBMH: &BareMetalHost{
TypeMeta: tm,
ObjectMeta: om,
Spec: BareMetalHostSpec{
BMC: BMCDetails{
Address: "idrac://127.0.0.1",
CredentialsName: "test1",
},
Image: &Image{
URL: "http://127.0.0.1",
DiskFormat: &diskFormat,
},
}, // end of BMH spec
},
oldBMH: nil,
wantedErr: "BMC driver idrac does not support live-iso image",
},
}

for _, tt := range tests {
Expand Down