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

bundle validation: check for bundle preset mismatch during setup #4343

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
37 changes: 37 additions & 0 deletions pkg/crc/machine/bundle/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ type ClusterInfo struct {
OpenshiftPullSecret string `json:"openshiftPullSecret,omitempty"`
}

type FilenameInfo struct {
Preset crcPreset.Preset
Driver string
Version string
Arch string
CustomBundleSuffix string
}

type Node struct {
Kind []string `json:"kind"`
Hostname string `json:"hostname"`
Expand Down Expand Up @@ -244,6 +252,35 @@ func GetBundleNameFromURI(bundleURI string) string {
}
}

// GetBundleInfoFromName Parses the bundle filename and returns a FilenameInfo struct
func GetBundleInfoFromName(bundleName string) (*FilenameInfo, error) {
var filenameInfo FilenameInfo
bundleName = GetBundleNameWithoutExtension(bundleName)

// crc_preset_driver_version_arch_customSuffix.crcbundle
bundleNameRegex := regexp.MustCompile(`crc(?:(?:_)([[:alpha:]]+))?_([[:alpha:]]+)_([0-9.]+)_([[:alnum:]]+)(?:(?:_)([0-9]+))?`)
filenameParts := bundleNameRegex.FindStringSubmatch(bundleName)
if filenameParts == nil {
return &filenameInfo, fmt.Errorf("bundle filename is in unrecognized format")
}

if filenameParts[1] == "" {
filenameInfo.Preset = crcPreset.OpenShift
} else {
parsedPreset, err := crcPreset.ParsePresetE(filenameParts[1])
if err != nil {
return &filenameInfo, err
}
filenameInfo.Preset = parsedPreset
}
filenameInfo.Driver = filenameParts[2]
filenameInfo.Version = filenameParts[3]
filenameInfo.Arch = filenameParts[4]
filenameInfo.CustomBundleSuffix = filenameParts[5]

return &filenameInfo, nil
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have unit test for this function which takes all the valid bundle name and provide info what it suppose to do and some dummy one to check the error.

func getBundleDownloadInfo(preset crcPreset.Preset) (*download.RemoteFile, error) {
sha256sum, err := getDefaultBundleVerifiedHash(preset)
if err != nil {
Expand Down
9 changes: 1 addition & 8 deletions pkg/crc/machine/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
return nil, errors.Wrap(err, "Error getting bundle metadata")
}

if err := bundleMismatchWithPreset(startConfig.Preset, crcBundleMetadata); err != nil {
if err := validation.BundleMismatchWithPresetMetadata(startConfig.Preset, crcBundleMetadata); err != nil {
return nil, err
}

Expand Down Expand Up @@ -854,13 +854,6 @@ func updateKubeconfig(ctx context.Context, ocConfig oc.Config, sshRunner *crcssh
return nil
}

func bundleMismatchWithPreset(preset crcPreset.Preset, bundleMetadata *bundle.CrcBundleInfo) error {
if preset != bundleMetadata.GetBundleType() {
return errors.Errorf("Preset %s is used but bundle is provided for %s preset", preset, bundleMetadata.GetBundleType())
}
return nil
}

func startMicroshift(ctx context.Context, sshRunner *crcssh.Runner, ocConfig oc.Config, pullSec cluster.PullSecretLoader) error {
logging.Infof("Starting Microshift service... [takes around 1min]")
if err := ensurePullSecretPresentInVM(sshRunner, pullSec); err != nil {
Expand Down
40 changes: 39 additions & 1 deletion pkg/crc/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,28 @@ func ValidateBundle(bundlePath string, preset crcpreset.Preset) error {
if bundlePath == constants.GetDefaultBundlePath(preset) {
return nil
}
return ValidateBundlePath(bundlePath, preset)

if err := ValidateBundlePath(bundlePath, preset); err != nil {
return err
}

bundleInfo, err := bundle.GetBundleInfoFromName(bundleName)
if err != nil {
return err
}

if err := BundleMismatchWithPresetFilename(preset, bundleInfo); err != nil {
logging.Fatal(err.Error())
return err
}
return nil
}

if err := BundleMismatchWithPresetMetadata(preset, bundleMetadata); err != nil {
logging.Fatal(err.Error())
return err
}

bundleMismatchWarning(bundleMetadata.GetBundleName(), preset)
/* 'bundle' is already unpacked in ~/.crc/cache */
return nil
Expand All @@ -117,6 +137,24 @@ func bundleMismatchWarning(userProvidedBundle string, preset crcpreset.Preset) {
}
}

// BundleMismatchWithPresetFilename checks whether the bundle matches the configured preset based on the bundle filename
// (bundle is not downloaded or uncompressed yet)
func BundleMismatchWithPresetFilename(preset crcpreset.Preset, bundleFilenameInfo *bundle.FilenameInfo) error {
if preset != bundleFilenameInfo.Preset {
return fmt.Errorf("Preset %s is used but bundle is provided for %s preset", preset, bundleFilenameInfo.Preset)
}
return nil
}

// BundleMismatchWithPresetMetadata checks whether the bundle matches the configured preset based on the bundle metadata
// (bundle is already downloaded and uncompressed)
func BundleMismatchWithPresetMetadata(preset crcpreset.Preset, bundleMetadata *bundle.CrcBundleInfo) error {
if preset != bundleMetadata.GetBundleType() {
return fmt.Errorf("Preset %s is used but bundle is provided for %s preset", preset, bundleMetadata.GetBundleType())
}
return nil
}

// ValidateIPAddress checks if provided IP is valid
func ValidateIPAddress(ipAddress string) error {
ip := net.ParseIP(ipAddress).To4()
Expand Down
Loading