Repository: aquasecurity/kube-bench
Pull request #834 (2021-02-28 20:08:39)

Fix fallback to default version
In some cases kube-bench will crush instead of fallback to default version. 
Fix it to only log that couldn't auto-detect version and used default.

codecov[bot]: # [Codecov](https://codecov.io/gh/aquasecurity/kube-bench/pull/834?src=pr&el=h1) Report
> Merging [#834](https://codecov.io/gh/aquasecurity/kube-bench/pull/834?src=pr&el=desc) (f973c0c) into [main](https://codecov.io/gh/aquasecurity/kube-bench/commit/e308bc1eba7b9fed9fb9a189fc98ef974d33012a?el=desc) (e308bc1) will **decrease** coverage by `0.34%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/aquasecurity/kube-bench/pull/834/graphs/tree.svg?width=650&height=150&src=pr&token=MVaDEmdgi9)](https://codecov.io/gh/aquasecurity/kube-bench/pull/834?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##             main     #834      +/-   ##
==========================================
- Coverage   66.32%   65.98%   -0.35%     
==========================================
  Files          14       14              
  Lines        1268     1270       +2     
==========================================
- Hits          841      838       -3     
- Misses        371      374       +3     
- Partials       56       58       +2     
```


| [Impacted Files](https://codecov.io/gh/aquasecurity/kube-bench/pull/834?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [cmd/util.go](https://codecov.io/gh/aquasecurity/kube-bench/pull/834/diff?src=pr&el=tree#diff-Y21kL3V0aWwuZ28=) | `72.94% <100.00%> (-1.20%)` | :arrow_down: |
| [cmd/common.go](https://codecov.io/gh/aquasecurity/kube-bench/pull/834/diff?src=pr&el=tree#diff-Y21kL2NvbW1vbi5nbw==) | `55.84% <0.00%> (-0.76%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/aquasecurity/kube-bench/pull/834?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/aquasecurity/kube-bench/pull/834?src=pr&el=footer). Last update [e308bc1...f973c0c](https://codecov.io/gh/aquasecurity/kube-bench/pull/834?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/cmd/util.go b/cmd/util.go
index d2a1baf..e143a12 100644
--- a/cmd/util.go
+++ b/cmd/util.go
@@ -304,7 +304,9 @@ func getKubeVersion() (*KubeVersion, error) {
 			}
 
 			glog.Warning(missingKubectlKubeletMessage)
-			return nil, fmt.Errorf("unable to find the programs kubectl or kubelet in the PATH")
+			glog.V(1).Info("unable to find the programs kubectl or kubelet in the PATH")
+			glog.V(1).Infof("Cant detect version, assuming default %s", defaultKubeVersion)
+			return &KubeVersion{baseVersion: defaultKubeVersion}, nil 
 		}
 		return getKubeVersionFromKubelet(), nil
 	}


Test patch:
diff --git a/cmd/common_test.go b/cmd/common_test.go
index 048ed4b..eefe501 100644
--- a/cmd/common_test.go
+++ b/cmd/common_test.go
@@ -358,7 +358,7 @@ func TestGetBenchmarkVersion(t *testing.T) {
 		succeed          bool
 	}{
 		{n: "both versions", kubeVersion: "1.11", benchmarkVersion: "cis-1.3", exp: "cis-1.3", callFn: withNoPath, v: viper.New(), succeed: false},
-		{n: "no version-missing-kubectl", kubeVersion: "", benchmarkVersion: "", v: viperWithData, exp: "", callFn: withNoPath, succeed: false},
+		{n: "no version-missing-kubectl", kubeVersion: "", benchmarkVersion: "", v: viperWithData, exp: "cis-1.6", callFn: withNoPath, succeed: true},
 		{n: "no version-fakeKubectl", kubeVersion: "", benchmarkVersion: "", v: viperWithData, exp: "cis-1.5", callFn: withFakeKubectl, succeed: true},
 		{n: "kubeVersion", kubeVersion: "1.15", benchmarkVersion: "", v: viperWithData, exp: "cis-1.5", callFn: withNoPath, succeed: true},
 		{n: "ocpVersion310", kubeVersion: "ocp-3.10", benchmarkVersion: "", v: viperWithData, exp: "rh-0.7", callFn: withNoPath, succeed: true},