1
0
mirror of https://github.com/aquasecurity/kube-bench.git synced 2024-11-22 16:18:07 +00:00

Add default version if version check fails.

This commit is contained in:
Abubakr-Sadik Nii Nai Davis 2017-11-13 15:25:34 +00:00
parent f90dd925b8
commit 42a1068964
2 changed files with 27 additions and 16 deletions

View File

@ -25,16 +25,17 @@ import (
) )
var ( var (
envVarsPrefix = "KUBE_BENCH" envVarsPrefix = "KUBE_BENCH"
cfgDir = "./cfg" cfgDir = "./cfg"
cfgFile string defaultKubeVersion = "1.6"
jsonFmt bool cfgFile string
pgSql bool jsonFmt bool
checkList string pgSql bool
groupList string checkList string
masterFile string groupList string
nodeFile string masterFile string
federatedFile string nodeFile string
federatedFile string
) )
// RootCmd represents the base command when called without any subcommands // RootCmd represents the base command when called without any subcommands

View File

@ -214,25 +214,35 @@ func multiWordReplace(s string, subname string, sub string) string {
} }
func getKubeVersion() string { func getKubeVersion() string {
var ver string
var matched bool
failmsg := "kubernetes version check failed" failmsg := "kubernetes version check failed"
// These executables might not be on the user's path. // These executables might not be on the user's path.
_, err := exec.LookPath("kubectl") _, err := exec.LookPath("kubectl")
if err != nil { if err != nil {
exitWithError(fmt.Errorf("%s: %s", failmsg, err)) continueWithError(err, failmsg)
} }
cmd := exec.Command("kubectl", "version", "--short") cmd := exec.Command("kubectl", "version", "--short")
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err != nil { if err != nil {
exitWithError(fmt.Errorf("%s, %s", failmsg, out)) continueWithError(fmt.Errorf("%s", out), "")
} }
validVersionPttn := `\d.\d`
serverVersionRe := regexp.MustCompile(`Server Version: v(\d+.\d+)`) serverVersionRe := regexp.MustCompile(`Server Version: v(\d+.\d+)`)
ver := serverVersionRe.FindStringSubmatch(string(out))[1] subs := serverVersionRe.FindStringSubmatch(string(out))
if len(subs) > 2 {
ver = string(subs[1])
validVersionPttn := `\d.\d`
if matched, _ = regexp.MatchString(validVersionPttn, ver); !matched {
continueWithError(fmt.Errorf("%s: invalid server version ", ver), failmsg)
}
}
if matched, _ := regexp.MatchString(validVersionPttn, ver); !matched { if ver == "" || !matched {
exitWithError(fmt.Errorf("%s: invalid server version ", failmsg, ver)) printlnWarn(fmt.Sprintf("Unable to get kubectl version, using default version: %s", defaultKubeVersion))
ver = defaultKubeVersion
} }
return ver return ver