config: fix incorrect kubernetes version validation (#1155)

Fix incorrect string comparison by replacing it with
call to semver.Compare.
Also add handling to check for missing v prefix.
This commit is contained in:
Otto Bittner 2023-02-09 17:38:02 +01:00 committed by GitHub
parent 4c5ab7c5e9
commit fd860ddb91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 15 deletions

View File

@ -506,10 +506,6 @@ func (c *Config) Validate(force bool) error {
return err
}
if err := validate.RegisterTranslation("supported_k8s_version", trans, registerInvalidK8sVersionError, translateInvalidK8sVersionError); err != nil {
return err
}
if err := validate.RegisterValidation("no_placeholders", validateNoPlaceholder); err != nil {
return err
}

View File

@ -55,22 +55,33 @@ func translateInvalidK8sVersionError(ut ut.Translator, fe validator.FieldError)
validVersionsSorted := semver.ByVersion(validVersions)
sort.Sort(validVersionsSorted)
errorMsg := fmt.Sprintf("Supported versions: %s", strings.Join(validVersionsSorted, " "))
configured, ok := fe.Value().(string)
if !ok {
errorMsg = "The configured version is not a valid string"
if len(validVersionsSorted) == 0 {
t, _ := ut.T("supported_k8s_version", fe.Field(), "No valid versions available. This should never happen")
return t
}
maxVersion := validVersionsSorted[len(validVersionsSorted)-1]
minVersion := validVersionsSorted[0]
if configured < minVersion {
errorMsg = fmt.Sprintf("The configured version %s is older than the oldest version supported by this CLI: %s.", configured, minVersion)
var errorMsg string
configured, ok := fe.Value().(string)
if !ok {
errorMsg = fmt.Sprintf("The configured version is not a valid string. Supported versions: %s", strings.Join(validVersionsSorted, " "))
t, _ := ut.T("supported_k8s_version", fe.Field(), errorMsg)
return t
}
if configured > maxVersion {
errorMsg = fmt.Sprintf("The configured version %s is newer than the newest version supported by this CLI: %s.", configured, maxVersion)
configured = compatibility.EnsurePrefixV(configured)
switch {
case !semver.IsValid(configured):
errorMsg = "The configured version is not a valid semantic version"
case semver.Compare(configured, minVersion) == -1:
errorMsg = fmt.Sprintf("The configured version %s is older than the oldest version supported by this CLI: %s", configured, minVersion)
case semver.Compare(configured, maxVersion) == 1:
errorMsg = fmt.Sprintf("The configured version %s is newer than the newest version supported by this CLI: %s", configured, maxVersion)
}
errorMsg = errorMsg + fmt.Sprintf("Supported versions: %s", strings.Join(validVersionsSorted, " "))
t, _ := ut.T("supported_k8s_version", fe.Field(), errorMsg)
return t
@ -299,7 +310,7 @@ func getPlaceholderEntries(m Measurements) []uint32 {
}
func validateK8sVersion(fl validator.FieldLevel) bool {
return versions.IsSupportedK8sVersion(fl.Field().String())
return versions.IsSupportedK8sVersion(compatibility.EnsurePrefixV(fl.Field().String()))
}
func registerVersionCompatibilityError(ut ut.Translator) error {

View File

@ -16,6 +16,7 @@ import (
"sort"
"strings"
"github.com/edgelesssys/constellation/v2/internal/compatibility"
"github.com/edgelesssys/constellation/v2/internal/constants"
"github.com/edgelesssys/constellation/v2/internal/versions/components"
"golang.org/x/mod/semver"
@ -26,7 +27,7 @@ func SupportedK8sVersions() []string {
validVersions := make([]string, len(VersionConfigs))
i := 0
for _, conf := range VersionConfigs {
validVersions[i] = conf.ClusterVersion
validVersions[i] = compatibility.EnsurePrefixV(conf.ClusterVersion)
i++
}
validVersionsSorted := semver.ByVersion(validVersions)