2017-05-26 09:25:29 +00:00
|
|
|
// Copyright © 2017 Aqua Security Software Ltd. <info@aquasec.com>
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2019-05-29 15:05:55 +00:00
|
|
|
"bufio"
|
2017-05-26 09:25:29 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2018-06-29 11:20:29 +00:00
|
|
|
"os"
|
2018-03-23 08:27:48 +00:00
|
|
|
"path/filepath"
|
2019-05-05 07:52:28 +00:00
|
|
|
"strings"
|
2018-03-27 13:33:01 +00:00
|
|
|
|
2017-06-20 08:38:15 +00:00
|
|
|
"github.com/aquasecurity/kube-bench/check"
|
2017-08-31 16:38:11 +00:00
|
|
|
"github.com/golang/glog"
|
2017-05-26 09:25:29 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2019-05-01 19:43:06 +00:00
|
|
|
// NewRunFilter constructs a Predicate based on FilterOpts which determines whether tested Checks should be run or not.
|
|
|
|
func NewRunFilter(opts FilterOpts) (check.Predicate, error) {
|
2019-04-29 10:17:06 +00:00
|
|
|
|
|
|
|
if opts.CheckList != "" && opts.GroupList != "" {
|
2019-05-01 19:43:06 +00:00
|
|
|
return nil, fmt.Errorf("group option and check option can't be used together")
|
2019-04-29 10:17:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var groupIDs map[string]bool
|
|
|
|
if opts.GroupList != "" {
|
|
|
|
groupIDs = cleanIDs(opts.GroupList)
|
|
|
|
}
|
|
|
|
|
|
|
|
var checkIDs map[string]bool
|
|
|
|
if opts.CheckList != "" {
|
|
|
|
checkIDs = cleanIDs(opts.CheckList)
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(g *check.Group, c *check.Check) bool {
|
2019-05-01 19:43:06 +00:00
|
|
|
var test = true
|
2019-04-29 10:17:06 +00:00
|
|
|
if len(groupIDs) > 0 {
|
|
|
|
_, ok := groupIDs[g.ID]
|
2019-05-01 19:43:06 +00:00
|
|
|
test = test && ok
|
2019-04-29 10:17:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(checkIDs) > 0 {
|
|
|
|
_, ok := checkIDs[c.ID]
|
2019-05-01 19:43:06 +00:00
|
|
|
test = test && ok
|
2019-04-29 10:17:06 +00:00
|
|
|
}
|
|
|
|
|
2019-05-01 19:43:06 +00:00
|
|
|
test = test && (opts.Scored && c.Scored || opts.Unscored && !c.Scored)
|
|
|
|
|
|
|
|
return test
|
|
|
|
}, nil
|
2019-04-29 10:17:06 +00:00
|
|
|
}
|
|
|
|
|
2018-06-29 11:20:29 +00:00
|
|
|
func runChecks(nodetype check.NodeType) {
|
2017-05-26 09:25:29 +00:00
|
|
|
var summary check.Summary
|
2018-03-27 13:33:01 +00:00
|
|
|
|
2019-08-22 12:43:09 +00:00
|
|
|
// Verify config file was loaded into Viper during Cobra sub-command initialization.
|
|
|
|
if configFileError != nil {
|
|
|
|
colorPrint(check.FAIL, fmt.Sprintf("Failed to read config file: %v\n", configFileError))
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2019-03-07 17:02:43 +00:00
|
|
|
def := loadConfig(nodetype)
|
2017-11-28 17:33:42 +00:00
|
|
|
in, err := ioutil.ReadFile(def)
|
2017-05-26 09:25:29 +00:00
|
|
|
if err != nil {
|
2018-06-29 11:20:29 +00:00
|
|
|
exitWithError(fmt.Errorf("error opening %s controls file: %v", nodetype, err))
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
|
2018-06-29 11:20:29 +00:00
|
|
|
glog.V(1).Info(fmt.Sprintf("Using benchmark file: %s\n", def))
|
|
|
|
|
2019-04-29 10:17:06 +00:00
|
|
|
// Get the set of executables and config files we care about on this type of node.
|
2019-03-07 17:02:43 +00:00
|
|
|
typeConf := viper.Sub(string(nodetype))
|
|
|
|
binmap, err := getBinaries(typeConf)
|
|
|
|
|
|
|
|
// Checks that the executables we need for the node type are running.
|
2017-05-26 09:25:29 +00:00
|
|
|
if err != nil {
|
2019-03-07 17:02:43 +00:00
|
|
|
exitWithError(err)
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
2017-11-28 17:33:42 +00:00
|
|
|
|
2019-07-13 06:48:24 +00:00
|
|
|
confmap := getFiles(typeConf, "config")
|
|
|
|
svcmap := getFiles(typeConf, "service")
|
|
|
|
kubeconfmap := getFiles(typeConf, "kubeconfig")
|
|
|
|
cafilemap := getFiles(typeConf, "ca")
|
2017-05-26 09:25:29 +00:00
|
|
|
|
2017-07-13 00:24:09 +00:00
|
|
|
// Variable substitutions. Replace all occurrences of variables in controls files.
|
2017-08-30 17:37:01 +00:00
|
|
|
s := string(in)
|
|
|
|
s = makeSubstitutions(s, "bin", binmap)
|
|
|
|
s = makeSubstitutions(s, "conf", confmap)
|
2018-10-23 02:26:38 +00:00
|
|
|
s = makeSubstitutions(s, "svc", svcmap)
|
2019-02-27 22:08:57 +00:00
|
|
|
s = makeSubstitutions(s, "kubeconfig", kubeconfmap)
|
2019-07-10 09:43:15 +00:00
|
|
|
s = makeSubstitutions(s, "cafile", cafilemap)
|
2017-05-26 09:25:29 +00:00
|
|
|
|
2018-06-29 11:20:29 +00:00
|
|
|
controls, err := check.NewControls(nodetype, []byte(s))
|
2017-06-23 11:04:46 +00:00
|
|
|
if err != nil {
|
2018-06-29 11:20:29 +00:00
|
|
|
exitWithError(fmt.Errorf("error setting up %s controls: %v", nodetype, err))
|
2017-06-23 11:04:46 +00:00
|
|
|
}
|
2017-05-26 09:25:29 +00:00
|
|
|
|
2019-04-29 10:17:06 +00:00
|
|
|
runner := check.NewRunner()
|
2019-05-01 19:43:06 +00:00
|
|
|
filter, err := NewRunFilter(filterOpts)
|
|
|
|
if err != nil {
|
|
|
|
exitWithError(fmt.Errorf("error setting up run filter: %v", err))
|
|
|
|
}
|
2019-04-29 10:17:06 +00:00
|
|
|
|
|
|
|
summary = controls.RunChecks(runner, filter)
|
2017-05-26 09:25:29 +00:00
|
|
|
|
2017-06-23 09:41:40 +00:00
|
|
|
// if we successfully ran some tests and it's json format, ignore the warnings
|
2019-01-29 14:33:58 +00:00
|
|
|
if (summary.Fail > 0 || summary.Warn > 0 || summary.Pass > 0 || summary.Info > 0) && jsonFmt {
|
2017-05-26 09:25:29 +00:00
|
|
|
out, err := controls.JSON()
|
|
|
|
if err != nil {
|
2017-07-25 00:34:07 +00:00
|
|
|
exitWithError(fmt.Errorf("failed to output in JSON format: %v", err))
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 15:05:55 +00:00
|
|
|
PrintOutput(string(out), outputFile)
|
2017-05-26 09:25:29 +00:00
|
|
|
} else {
|
2017-10-31 20:08:46 +00:00
|
|
|
// if we want to store in PostgreSQL, convert to JSON and save it
|
2019-01-29 14:33:58 +00:00
|
|
|
if (summary.Fail > 0 || summary.Warn > 0 || summary.Pass > 0 || summary.Info > 0) && pgSQL {
|
2017-10-31 20:08:46 +00:00
|
|
|
out, err := controls.JSON()
|
|
|
|
if err != nil {
|
|
|
|
exitWithError(fmt.Errorf("failed to output in JSON format: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
savePgsql(string(out))
|
|
|
|
} else {
|
|
|
|
prettyPrint(controls, summary)
|
|
|
|
}
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-20 08:54:17 +00:00
|
|
|
// colorPrint outputs the state in a specific colour, along with a message string
|
|
|
|
func colorPrint(state check.State, s string) {
|
|
|
|
colors[state].Printf("[%s] ", state)
|
|
|
|
fmt.Printf("%s", s)
|
|
|
|
}
|
2017-05-26 09:25:29 +00:00
|
|
|
|
2017-06-23 09:41:40 +00:00
|
|
|
// prettyPrint outputs the results to stdout in human-readable format
|
2017-07-25 00:34:07 +00:00
|
|
|
func prettyPrint(r *check.Controls, summary check.Summary) {
|
2018-04-10 19:58:19 +00:00
|
|
|
// Print check results.
|
|
|
|
if !noResults {
|
|
|
|
colorPrint(check.INFO, fmt.Sprintf("%s %s\n", r.ID, r.Text))
|
|
|
|
for _, g := range r.Groups {
|
|
|
|
colorPrint(check.INFO, fmt.Sprintf("%s %s\n", g.ID, g.Text))
|
|
|
|
for _, c := range g.Checks {
|
|
|
|
colorPrint(c.State, fmt.Sprintf("%s %s\n", c.ID, c.Text))
|
2019-05-05 07:52:28 +00:00
|
|
|
|
|
|
|
if includeTestOutput && c.State == check.FAIL && len(c.ActualValue) > 0 {
|
|
|
|
printRawOutput(c.ActualValue)
|
|
|
|
}
|
2018-04-10 19:58:19 +00:00
|
|
|
}
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
2017-06-20 08:54:17 +00:00
|
|
|
|
2018-04-10 19:58:19 +00:00
|
|
|
fmt.Println()
|
|
|
|
}
|
2017-05-26 09:25:29 +00:00
|
|
|
|
|
|
|
// Print remediations.
|
2018-04-10 19:58:19 +00:00
|
|
|
if !noRemediations {
|
|
|
|
if summary.Fail > 0 || summary.Warn > 0 {
|
|
|
|
colors[check.WARN].Printf("== Remediations ==\n")
|
|
|
|
for _, g := range r.Groups {
|
|
|
|
for _, c := range g.Checks {
|
2019-01-24 15:40:43 +00:00
|
|
|
if c.State == check.FAIL || c.State == check.WARN {
|
2018-04-10 19:58:19 +00:00
|
|
|
fmt.Printf("%s %s\n", c.ID, c.Remediation)
|
|
|
|
}
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 19:58:19 +00:00
|
|
|
fmt.Println()
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print summary setting output color to highest severity.
|
2018-04-10 19:58:19 +00:00
|
|
|
if !noSummary {
|
|
|
|
var res check.State
|
|
|
|
if summary.Fail > 0 {
|
|
|
|
res = check.FAIL
|
|
|
|
} else if summary.Warn > 0 {
|
|
|
|
res = check.WARN
|
|
|
|
} else {
|
|
|
|
res = check.PASS
|
|
|
|
}
|
2017-05-26 09:25:29 +00:00
|
|
|
|
2018-04-10 19:58:19 +00:00
|
|
|
colors[res].Printf("== Summary ==\n")
|
2019-01-29 14:33:58 +00:00
|
|
|
fmt.Printf("%d checks PASS\n%d checks FAIL\n%d checks WARN\n%d checks INFO\n",
|
|
|
|
summary.Pass, summary.Fail, summary.Warn, summary.Info,
|
2018-04-10 19:58:19 +00:00
|
|
|
)
|
|
|
|
}
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
2019-03-07 17:02:43 +00:00
|
|
|
|
|
|
|
// loadConfig finds the correct config dir based on the kubernetes version,
|
|
|
|
// merges any specific config.yaml file found with the main config
|
|
|
|
// and returns the benchmark file to use.
|
|
|
|
func loadConfig(nodetype check.NodeType) string {
|
|
|
|
var file string
|
|
|
|
var err error
|
|
|
|
|
|
|
|
switch nodetype {
|
|
|
|
case check.MASTER:
|
|
|
|
file = masterFile
|
|
|
|
case check.NODE:
|
|
|
|
file = nodeFile
|
|
|
|
case check.FEDERATED:
|
|
|
|
file = federatedFile
|
|
|
|
}
|
|
|
|
|
2019-10-14 13:46:31 +00:00
|
|
|
// runningVersion := ""
|
|
|
|
// if kubeVersion == "" {
|
|
|
|
// 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()
|
2019-03-07 17:02:43 +00:00
|
|
|
if err != nil {
|
|
|
|
exitWithError(fmt.Errorf("Version check failed: %s\nAlternatively, you can specify the version with --version", err))
|
|
|
|
}
|
|
|
|
}
|
2019-10-14 13:46:31 +00:00
|
|
|
|
|
|
|
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)
|
2019-03-07 17:02:43 +00:00
|
|
|
if err != nil {
|
|
|
|
exitWithError(fmt.Errorf("can't find %s controls file in %s: %v", nodetype, cfgDir, err))
|
|
|
|
}
|
|
|
|
|
2019-10-14 13:46:31 +00:00
|
|
|
// path, err := getConfigFilePath(kubeVersion, runningVersion, file)
|
|
|
|
// if err != nil {
|
|
|
|
// exitWithError(fmt.Errorf("can't find %s controls file in %s: %v", nodetype, cfgDir, err))
|
|
|
|
// }
|
|
|
|
|
2019-03-07 17:02:43 +00:00
|
|
|
// Merge kubernetes version specific config if any.
|
|
|
|
viper.SetConfigFile(path + "/config.yaml")
|
|
|
|
err = viper.MergeInConfig()
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
glog.V(2).Info(fmt.Sprintf("No version-specific config.yaml file in %s", path))
|
|
|
|
} else {
|
|
|
|
exitWithError(fmt.Errorf("couldn't read config file %s: %v", path+"/config.yaml", err))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
glog.V(1).Info(fmt.Sprintf("Using config file: %s\n", viper.ConfigFileUsed()))
|
|
|
|
}
|
|
|
|
return filepath.Join(path, file)
|
|
|
|
}
|
|
|
|
|
|
|
|
// isMaster verify if master components are running on the node.
|
|
|
|
func isMaster() bool {
|
|
|
|
glog.V(2).Info("Checking if the current node is running master components")
|
|
|
|
masterConf := viper.Sub(string(check.MASTER))
|
2019-04-11 16:05:57 +00:00
|
|
|
components, err := getBinaries(masterConf)
|
|
|
|
|
|
|
|
if err != nil {
|
2019-03-07 17:02:43 +00:00
|
|
|
glog.V(2).Info(err)
|
|
|
|
return false
|
|
|
|
}
|
2019-04-11 16:05:57 +00:00
|
|
|
if len(components) == 0 {
|
|
|
|
glog.V(2).Info("No master binaries specified")
|
|
|
|
return false
|
|
|
|
}
|
2019-03-07 17:02:43 +00:00
|
|
|
return true
|
|
|
|
}
|
2019-05-05 07:52:28 +00:00
|
|
|
|
|
|
|
func printRawOutput(output string) {
|
|
|
|
for _, row := range strings.Split(output, "\n") {
|
|
|
|
fmt.Println(fmt.Sprintf("\t %s", row))
|
|
|
|
}
|
|
|
|
}
|
2019-05-29 15:05:55 +00:00
|
|
|
|
|
|
|
func writeOutputToFile(output string, outputFile string) error {
|
|
|
|
file, err := os.Create(outputFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
w := bufio.NewWriter(file)
|
|
|
|
fmt.Fprintln(w, output)
|
|
|
|
return w.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
func PrintOutput(output string, outputFile string) {
|
|
|
|
if len(outputFile) == 0 {
|
|
|
|
fmt.Println(output)
|
|
|
|
} else {
|
|
|
|
err := writeOutputToFile(output, outputFile)
|
|
|
|
if err != nil {
|
|
|
|
exitWithError(fmt.Errorf("Failed to write to output file %s: %v", outputFile, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|