mirror of
https://github.com/aquasecurity/kube-bench.git
synced 2024-11-29 11:28:14 +00:00
Fix kubeVersion regex tests
This commit is contained in:
parent
471c02f4d7
commit
730871f330
@ -62,6 +62,8 @@ func runChecks(t check.NodeType) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ver := getKubeVersion()
|
ver := getKubeVersion()
|
||||||
|
glog.V(1).Info(fmt.Sprintf("Running tests for Kubernetes version: %s", ver))
|
||||||
|
|
||||||
path := fmt.Sprintf("%s/%s/%s", cfgDir, ver, file)
|
path := fmt.Sprintf("%s/%s/%s", cfgDir, ver, file)
|
||||||
in, err := ioutil.ReadFile(path)
|
in, err := ioutil.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
27
cmd/util.go
27
cmd/util.go
@ -214,14 +214,10 @@ 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"
|
|
||||||
// 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 {
|
||||||
continueWithError(err, failmsg)
|
exitWithError(fmt.Errorf("kubernetes version check failed: %v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := exec.Command("kubectl", "version", "--short")
|
cmd := exec.Command("kubectl", "version", "--short")
|
||||||
@ -230,22 +226,17 @@ func getKubeVersion() string {
|
|||||||
continueWithError(fmt.Errorf("%s", out), "")
|
continueWithError(fmt.Errorf("%s", out), "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return getVersionFromKubectlOutput(string(out))
|
||||||
|
}
|
||||||
|
|
||||||
|
func getVersionFromKubectlOutput(s string) string {
|
||||||
serverVersionRe := regexp.MustCompile(`Server Version: v(\d+.\d+)`)
|
serverVersionRe := regexp.MustCompile(`Server Version: v(\d+.\d+)`)
|
||||||
subs := serverVersionRe.FindStringSubmatch(string(out))
|
subs := serverVersionRe.FindStringSubmatch(s)
|
||||||
if len(subs) == 2 {
|
if len(subs) < 2 {
|
||||||
ver = string(subs[1])
|
|
||||||
validVersionPttn := `\d.\d.\d`
|
|
||||||
if matched, _ = regexp.MatchString(validVersionPttn, ver); !matched {
|
|
||||||
continueWithError(fmt.Errorf("%s: invalid server version ", ver), failmsg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ver == "" || !matched {
|
|
||||||
printlnWarn(fmt.Sprintf("Unable to get kubectl version, using default version: %s", defaultKubeVersion))
|
printlnWarn(fmt.Sprintf("Unable to get kubectl version, using default version: %s", defaultKubeVersion))
|
||||||
ver = defaultKubeVersion
|
return defaultKubeVersion
|
||||||
}
|
}
|
||||||
|
return subs[1]
|
||||||
return ver
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeSubstitutions(s string, ext string, m map[string]string) string {
|
func makeSubstitutions(s string, ext string, m map[string]string) string {
|
||||||
|
@ -191,6 +191,20 @@ func TestGetKubeVersion(t *testing.T) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestKubeVersionRegex(t *testing.T) {
|
||||||
|
ver := getVersionFromKubectlOutput(`Client Version: v1.8.0
|
||||||
|
Server Version: v1.8.12
|
||||||
|
`)
|
||||||
|
if ver != "1.8" {
|
||||||
|
t.Fatalf("Expected 1.8 got %s", ver)
|
||||||
|
}
|
||||||
|
|
||||||
|
ver = getVersionFromKubectlOutput("Something completely different")
|
||||||
|
if ver != "1.6" {
|
||||||
|
t.Fatalf("Expected 1.6 got %s", ver)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestFindConfigFile(t *testing.T) {
|
func TestFindConfigFile(t *testing.T) {
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
input []string
|
input []string
|
||||||
|
Loading…
Reference in New Issue
Block a user