Skip to content

Commit

Permalink
config: fix incorrect kubernetes version validation (#1155)
Browse files Browse the repository at this point in the history
Fix incorrect string comparison by replacing it with
call to semver.Compare.
Also add handling to check for missing v prefix.
  • Loading branch information
derpsteb committed Feb 9, 2023
1 parent 4c5ab7c commit fd860dd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
4 changes: 0 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
31 changes: 21 additions & 10 deletions internal/config/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion internal/versions/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down

0 comments on commit fd860dd

Please sign in to comment.