1
0
mirror of https://github.com/aquasecurity/kube-bench.git synced 2024-12-22 14:48:07 +00:00

Exit kube-bench if we can't get valid kubernetes server version and

improve error messages.
This commit is contained in:
Abubakr-Sadik Nii Nai Davis 2017-11-03 12:59:35 +00:00
parent 85fb818e41
commit f90dd925b8
3 changed files with 16 additions and 33 deletions

View File

@ -62,7 +62,7 @@ func runChecks(t check.NodeType) {
}
ver := getKubeVersion()
path := fmt.Sprintf("%s/%s/%s", cfgDir, ver.Server, file)
path := fmt.Sprintf("%s/%s/%s", cfgDir, ver, file)
in, err := ioutil.ReadFile(path)
if err != nil {
exitWithError(fmt.Errorf("error opening %s controls file: %v", t, err))

View File

@ -213,37 +213,27 @@ func multiWordReplace(s string, subname string, sub string) string {
return strings.Replace(s, subname, sub, -1)
}
type version struct {
Server string
Client string
}
func getKubeVersion() *version {
ver := new(version)
func getKubeVersion() string {
failmsg := "kubernetes version check failed"
// These executables might not be on the user's path.
_, err := exec.LookPath("kubectl")
if err != nil {
s := fmt.Sprintf("Kubernetes version check skipped with error %v", err)
continueWithError(err, sprintlnWarn(s))
return nil
exitWithError(fmt.Errorf("%s: %s", failmsg, err))
}
cmd := exec.Command("kubectl", "version")
out, err := cmd.Output()
cmd := exec.Command("kubectl", "version", "--short")
out, err := cmd.CombinedOutput()
if err != nil {
s := fmt.Sprintf("Kubernetes version check skipped, with error getting kubectl version")
continueWithError(err, sprintlnWarn(s))
return nil
exitWithError(fmt.Errorf("%s, %s", failmsg, out))
}
clientVerRe := regexp.MustCompile(`Client.*Major:"(\d+)".*Minor:"(\d+)"`)
svrVerRe := regexp.MustCompile(`Server.*Major:"(\d+)".*Minor:"(\d+)"`)
validVersionPttn := `\d.\d`
serverVersionRe := regexp.MustCompile(`Server Version: v(\d+.\d+)`)
ver := serverVersionRe.FindStringSubmatch(string(out))[1]
sub := clientVerRe.FindStringSubmatch(string(out))
ver.Client = sub[1] + "." + sub[2]
sub = svrVerRe.FindStringSubmatch(string(out))
ver.Server = sub[1] + "." + sub[2]
if matched, _ := regexp.MatchString(validVersionPttn, ver); !matched {
exitWithError(fmt.Errorf("%s: invalid server version ", failmsg, ver))
}
return ver
}

View File

@ -184,18 +184,11 @@ func TestMultiWordReplace(t *testing.T) {
func TestGetKubeVersion(t *testing.T) {
ver := getKubeVersion()
if ver == nil {
t.Log("Expected non nil version info.")
} else {
if ok, err := regexp.MatchString(`\d+.\d+`, ver.Client); !ok && err != nil {
t.Logf("Expected:%v got %v\n", "n.m", ver.Client)
}
if ok, err := regexp.MatchString(`\d+.\d+`, ver.Server); !ok && err != nil {
t.Logf("Expected:%v got %v\n", "n.m", ver.Server)
}
if ok, err := regexp.MatchString(`\d+.\d+`, ver); !ok && err != nil {
t.Logf("Expected:%v got %v\n", "n.m", ver)
}
}
func TestFindConfigFile(t *testing.T) {