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

starting benchmark flag

This commit is contained in:
Roberto J Rojas 2019-10-14 09:46:31 -04:00
parent 16beb3e616
commit 58fc948626
3 changed files with 121 additions and 22 deletions

View File

@ -211,18 +211,93 @@ func loadConfig(nodetype check.NodeType) string {
file = federatedFile file = federatedFile
} }
runningVersion := "" // runningVersion := ""
if kubeVersion == "" { // if kubeVersion == "" {
runningVersion, err = getKubeVersion() // runningVersion, err = getKubeVersion()
// if err != nil {
// exitWithError(fmt.Errorf("Version check failed: %s\nAlternatively, you can specify the version with --version", err))
// }
// }
/*
if kubeVersion == "" && benchmarkVersion == "" {
kubeVersion, err = getKubeVersion()
if err != nil {
exitWithError(fmt.Errorf("Version check failed: %s\nAlternatively, you can specify the version with --version", err))
}
}
if kubeVersion != "" && benchmarkVersion != "" {
exitWithError("Can not specify both version and benchmarkVersion")
}
var fileVersion string
if benchmarkVersion != "" {
fileVersion = benchmarkVersion
} else {
cis-ver kube-bench config k8s-ver
1.3.0 1.11 1.11-1.12
1.4.1 1.13 1.13-
cis-ver kube-bench(--benchmark) k8s-ver
1.3.0 cis-1.3.0 1.11-1.12
1.4.1 cis-1.4.1 1.13-
k8sToCISVersions := map[string]string {
"1.11": "cis-1.3.0",
"1.12": "cis-1.3.0",
"1.13": "cis-1.4.1",
"1.14": "cis-1.4.1",
"1.15": "cis-1.4.1",
"1.16": "cis-1.4.1",
}
fileVersion, err := convertKubernetesVersionToCISVersion(kubeVersion)
if err != nil {
exitWithError(err)
}
}
path, err := getConfigFilePath(fileVersion, file)
if err != nil {
exitWithError(fmt.Errorf("can't find %s controls file in %s: %v", nodetype, cfgDir, err))
}
*/
if kubeVersion == "" && benchmarkVersion == "" {
kubeVersion, err = getKubeVersion()
if err != nil { if err != nil {
exitWithError(fmt.Errorf("Version check failed: %s\nAlternatively, you can specify the version with --version", err)) exitWithError(fmt.Errorf("Version check failed: %s\nAlternatively, you can specify the version with --version", err))
} }
} }
path, err := getConfigFilePath(kubeVersion, runningVersion, file)
if kubeVersion != "" && benchmarkVersion != "" {
exitWithError(fmt.Errorf("It is not valid to specify both --version and --benchmark"))
}
var fileVersion string
if benchmarkVersion != "" {
fileVersion = benchmarkVersion
} else {
fileVersion, err = convertToCISVersion(kubeVersion)
if err != nil {
exitWithError(fmt.Errorf("failed to convert kubernetes version to CIS version: %v", err))
}
}
path, err := getConfigFilePath(fileVersion, file)
if err != nil { if err != nil {
exitWithError(fmt.Errorf("can't find %s controls file in %s: %v", nodetype, cfgDir, err)) exitWithError(fmt.Errorf("can't find %s controls file in %s: %v", nodetype, cfgDir, err))
} }
// path, err := getConfigFilePath(kubeVersion, runningVersion, file)
// if err != nil {
// exitWithError(fmt.Errorf("can't find %s controls file in %s: %v", nodetype, cfgDir, err))
// }
// Merge kubernetes version specific config if any. // Merge kubernetes version specific config if any.
viper.SetConfigFile(path + "/config.yaml") viper.SetConfigFile(path + "/config.yaml")
err = viper.MergeInConfig() err = viper.MergeInConfig()

View File

@ -36,6 +36,7 @@ var (
envVarsPrefix = "KUBE_BENCH" envVarsPrefix = "KUBE_BENCH"
defaultKubeVersion = "1.6" defaultKubeVersion = "1.6"
kubeVersion string kubeVersion string
benchmarkVersion string
cfgFile string cfgFile string
cfgDir string cfgDir string
jsonFmt bool jsonFmt bool
@ -114,6 +115,7 @@ func init() {
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is ./cfg/config.yaml)") RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is ./cfg/config.yaml)")
RootCmd.PersistentFlags().StringVarP(&cfgDir, "config-dir", "D", "./cfg/", "config directory") RootCmd.PersistentFlags().StringVarP(&cfgDir, "config-dir", "D", "./cfg/", "config directory")
RootCmd.PersistentFlags().StringVar(&kubeVersion, "version", "", "Manually specify Kubernetes version, automatically detected if unset") RootCmd.PersistentFlags().StringVar(&kubeVersion, "version", "", "Manually specify Kubernetes version, automatically detected if unset")
RootCmd.PersistentFlags().StringVar(&benchmarkVersion, "benchmark", "", "Manually specify Kubernetes version, automatically detected if unset")
goflag.CommandLine.VisitAll(func(goflag *goflag.Flag) { goflag.CommandLine.VisitAll(func(goflag *goflag.Flag) {
RootCmd.PersistentFlags().AddGoFlag(goflag) RootCmd.PersistentFlags().AddGoFlag(goflag)

View File

@ -27,16 +27,27 @@ var (
var psFunc func(string) string var psFunc func(string) string
var statFunc func(string) (os.FileInfo, error) var statFunc func(string) (os.FileInfo, error)
var getBinariesFunc func(*viper.Viper) (map[string]string, error)
var TypeMap = map[string][]string{ var TypeMap = map[string][]string{
"ca": []string{"cafile", "defaultcafile"}, "ca": []string{"cafile", "defaultcafile"},
"kubeconfig": []string{"kubeconfig", "defaultkubeconfig"}, "kubeconfig": []string{"kubeconfig", "defaultkubeconfig"},
"service": []string{"svc", "defaultsvc"}, "service": []string{"svc", "defaultsvc"},
"config": []string{"confs", "defaultconf"}, "config": []string{"confs", "defaultconf"},
}
var k8sToCISVersions = map[string]string {
"1.11": "cis-1.3.0",
"1.12": "cis-1.3.0",
"1.13": "cis-1.4.1",
"1.14": "cis-1.4.1",
"1.15": "cis-1.4.1",
"1.16": "cis-1.4.1",
} }
func init() { func init() {
psFunc = ps psFunc = ps
statFunc = os.Stat statFunc = os.Stat
getBinariesFunc = getBinaries
} }
func exitWithError(err error) { func exitWithError(err error) {
@ -118,16 +129,16 @@ func getBinaries(v *viper.Viper) (map[string]string, error) {
return binmap, nil return binmap, nil
} }
// getConfigFilePath locates the config files we should be using based on either the specified // getConfigFilePath locates the config files we should be using based on
// version, or the running version of kubernetes if not specified // the specified version
func getConfigFilePath(specifiedVersion string, runningVersion string, filename string) (path string, err error) { func getConfigFilePath(fileVersion string, filename string) (path string, err error) {
var fileVersion string // var fileVersion string
if specifiedVersion != "" { // if specifiedVersion != "" {
fileVersion = specifiedVersion // fileVersion = specifiedVersion
} else { // } else {
fileVersion = runningVersion // fileVersion = runningVersion
} // }
glog.V(2).Info(fmt.Sprintf("Looking for config for version %s", fileVersion)) glog.V(2).Info(fmt.Sprintf("Looking for config for version %s", fileVersion))
@ -137,16 +148,19 @@ func getConfigFilePath(specifiedVersion string, runningVersion string, filename
glog.V(2).Info(fmt.Sprintf("Looking for config file: %s\n", file)) glog.V(2).Info(fmt.Sprintf("Looking for config file: %s\n", file))
if _, err = os.Stat(file); !os.IsNotExist(err) { if _, err = os.Stat(file); !os.IsNotExist(err) {
if specifiedVersion == "" && fileVersion != runningVersion { // if specifiedVersion == "" && fileVersion != runningVersion {
glog.V(1).Info(fmt.Sprintf("No test file found for %s - using tests for Kubernetes %s\n", runningVersion, fileVersion)) // glog.V(1).Info(fmt.Sprintf("No test file found for %s - using tests for Kubernetes %s\n", runningVersion, fileVersion))
// }
if fileVersion == "" {
glog.V(1).Info(fmt.Sprintf("No test file found for CIS Version %s\n", fileVersion))
} }
return path, nil return path, nil
} }
// If we were given an explicit version to look for, don't look for any others // // If we were given an explicit version to look for, don't look for any others
if specifiedVersion != "" { // if specifiedVersion != "" {
return "", err // return "", err
} // }
fileVersion = decrementVersion(fileVersion) fileVersion = decrementVersion(fileVersion)
if fileVersion == "" { if fileVersion == "" {
@ -342,3 +356,11 @@ func makeSubstitutions(s string, ext string, m map[string]string) string {
return s return s
} }
func convertToCISVersion(kubeVersion string) (string, error) {
cisVersion, found := k8sToCISVersions[kubeVersion]
if !found {
return "", fmt.Errorf("Unable to find a CIS version for Kubernetes version: %s", kubeVersion)
}
return cisVersion, nil
}