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"
|
2020-06-24 09:13:10 +00:00
|
|
|
"encoding/json"
|
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"
|
2020-06-24 09:13:10 +00:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
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
|
|
|
}
|
|
|
|
|
2019-12-02 15:40:44 +00:00
|
|
|
func runChecks(nodetype check.NodeType, testYamlFile string) {
|
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-12-02 15:40:44 +00:00
|
|
|
in, err := ioutil.ReadFile(testYamlFile)
|
2017-05-26 09:25:29 +00:00
|
|
|
if err != nil {
|
2019-12-02 15:40:44 +00:00
|
|
|
exitWithError(fmt.Errorf("error opening %s test file: %v", testYamlFile, err))
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:40:44 +00:00
|
|
|
glog.V(1).Info(fmt.Sprintf("Using test file: %s\n", testYamlFile))
|
2018-06-29 11:20:29 +00:00
|
|
|
|
2019-12-02 15:40:44 +00:00
|
|
|
// Get the viper config for this section of tests
|
2019-03-07 17:02:43 +00:00
|
|
|
typeConf := viper.Sub(string(nodetype))
|
2019-12-02 15:40:44 +00:00
|
|
|
if typeConf == nil {
|
|
|
|
colorPrint(check.FAIL, fmt.Sprintf("No config settings for %s\n", string(nodetype)))
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the set of executables we need for this section of the tests
|
2019-11-05 15:44:57 +00:00
|
|
|
binmap, err := getBinaries(typeConf, nodetype)
|
2019-03-07 17:02:43 +00:00
|
|
|
|
2019-12-02 15:40:44 +00:00
|
|
|
// Checks that the executables we need for the section are running.
|
2017-05-26 09:25:29 +00:00
|
|
|
if err != nil {
|
2020-10-03 10:51:13 +00:00
|
|
|
glog.V(1).Info(fmt.Sprintf("failed to get a set of executables needed for tests: %v", 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)
|
2020-12-21 11:18:54 +00:00
|
|
|
s, binSubs := makeSubstitutions(s, "bin", binmap)
|
|
|
|
s, _ = makeSubstitutions(s, "conf", confmap)
|
|
|
|
s, _ = makeSubstitutions(s, "svc", svcmap)
|
|
|
|
s, _ = makeSubstitutions(s, "kubeconfig", kubeconfmap)
|
|
|
|
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
|
|
|
|
2020-12-21 11:18:54 +00:00
|
|
|
generateDefaultEnvAudit(controls, binSubs)
|
|
|
|
|
2020-10-29 10:03:41 +00:00
|
|
|
controls.RunChecks(runner, filter, parseSkipIds(skipIds))
|
2020-06-24 09:13:10 +00:00
|
|
|
controlsCollection = append(controlsCollection, controls)
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
|
2020-12-21 11:18:54 +00:00
|
|
|
func generateDefaultEnvAudit(controls *check.Controls, binSubs []string){
|
|
|
|
for _, group := range controls.Groups {
|
|
|
|
for _, checkItem := range group.Checks {
|
|
|
|
if checkItem.Tests != nil && !checkItem.DisableEnvTesting {
|
|
|
|
for _, test := range checkItem.Tests.TestItems {
|
|
|
|
if test.Env != "" && checkItem.AuditEnv == "" {
|
|
|
|
binPath := ""
|
|
|
|
|
|
|
|
if len(binSubs) == 1 {
|
|
|
|
binPath = binSubs[0]
|
|
|
|
} else {
|
|
|
|
fmt.Printf("AuditEnv not explicit for check (%s), where bin path cannot be determined\n", checkItem.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if test.Env != "" && checkItem.AuditEnv == "" {
|
|
|
|
checkItem.AuditEnv = fmt.Sprintf("cat \"/proc/$(/bin/ps -C %s -o pid= | tr -d ' ')/environ\" | tr '\\0' '\\n'", binPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-29 10:03:41 +00:00
|
|
|
func parseSkipIds(skipIds string) map[string]bool {
|
2020-12-21 11:18:54 +00:00
|
|
|
var skipIdMap = make(map[string]bool, 0)
|
2020-10-29 10:03:41 +00:00
|
|
|
if skipIds != "" {
|
|
|
|
for _, id := range strings.Split(skipIds, ",") {
|
|
|
|
skipIdMap[strings.Trim(id, " ")] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return skipIdMap
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-12-21 11:18:54 +00:00
|
|
|
colors[check.WARN].Printf("== Remediations %s ==\n", r.Type)
|
2018-04-10 19:58:19 +00:00
|
|
|
for _, g := range r.Groups {
|
|
|
|
for _, c := range g.Checks {
|
2020-03-05 12:20:26 +00:00
|
|
|
if c.State == check.FAIL {
|
2018-04-10 19:58:19 +00:00
|
|
|
fmt.Printf("%s %s\n", c.ID, c.Remediation)
|
|
|
|
}
|
2020-03-05 12:20:26 +00:00
|
|
|
if c.State == check.WARN {
|
|
|
|
// Print the error if test failed due to problem with the audit command
|
2020-06-24 09:13:10 +00:00
|
|
|
if c.Reason != "" && c.Type != "manual" {
|
2020-03-05 12:20:26 +00:00
|
|
|
fmt.Printf("%s audit test did not run: %s\n", c.ID, c.Reason)
|
|
|
|
} else {
|
|
|
|
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 {
|
2020-12-21 11:18:54 +00:00
|
|
|
printSummary(summary, string(r.Type))
|
|
|
|
}
|
|
|
|
}
|
2017-05-26 09:25:29 +00:00
|
|
|
|
2020-12-21 11:18:54 +00:00
|
|
|
func printSummary(summary check.Summary, sectionName string) {
|
|
|
|
var res check.State
|
|
|
|
if summary.Fail > 0 {
|
|
|
|
res = check.FAIL
|
|
|
|
} else if summary.Warn > 0 {
|
|
|
|
res = check.WARN
|
|
|
|
} else {
|
|
|
|
res = check.PASS
|
2018-04-10 19:58:19 +00:00
|
|
|
}
|
2020-12-21 11:18:54 +00:00
|
|
|
|
|
|
|
colors[res].Printf("== Summary %s ==\n", sectionName)
|
|
|
|
fmt.Printf("%d checks PASS\n%d checks FAIL\n%d checks WARN\n%d checks INFO\n\n",
|
|
|
|
summary.Pass, summary.Fail, summary.Warn, summary.Info,
|
|
|
|
)
|
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.
|
2020-08-04 15:04:02 +00:00
|
|
|
func loadConfig(nodetype check.NodeType, benchmarkVersion string) string {
|
2019-03-07 17:02:43 +00:00
|
|
|
var file string
|
|
|
|
var err error
|
|
|
|
|
|
|
|
switch nodetype {
|
|
|
|
case check.MASTER:
|
|
|
|
file = masterFile
|
|
|
|
case check.NODE:
|
|
|
|
file = nodeFile
|
2019-12-05 20:55:44 +00:00
|
|
|
case check.CONTROLPLANE:
|
|
|
|
file = controlplaneFile
|
|
|
|
case check.ETCD:
|
|
|
|
file = etcdFile
|
|
|
|
case check.POLICIES:
|
|
|
|
file = policiesFile
|
2020-03-03 14:51:48 +00:00
|
|
|
case check.MANAGEDSERVICES:
|
|
|
|
file = managedservicesFile
|
2019-03-07 17:02:43 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 21:31:27 +00:00
|
|
|
path, err := getConfigFilePath(benchmarkVersion, 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-12-02 15:40:44 +00:00
|
|
|
// Merge version-specific config if any.
|
|
|
|
mergeConfig(path)
|
|
|
|
|
|
|
|
return filepath.Join(path, file)
|
|
|
|
}
|
|
|
|
|
|
|
|
func mergeConfig(path string) error {
|
2019-03-07 17:02:43 +00:00
|
|
|
viper.SetConfigFile(path + "/config.yaml")
|
2019-12-02 15:40:44 +00:00
|
|
|
err := viper.MergeInConfig()
|
2019-03-07 17:02:43 +00:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
glog.V(2).Info(fmt.Sprintf("No version-specific config.yaml file in %s", path))
|
|
|
|
} else {
|
2019-12-02 15:40:44 +00:00
|
|
|
return fmt.Errorf("couldn't read config file %s: %v", path+"/config.yaml", err)
|
2019-03-07 17:02:43 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-02 15:40:44 +00:00
|
|
|
|
|
|
|
glog.V(1).Info(fmt.Sprintf("Using config file: %s\n", viper.ConfigFileUsed()))
|
|
|
|
|
|
|
|
return nil
|
2019-03-07 17:02:43 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 21:31:27 +00:00
|
|
|
func mapToBenchmarkVersion(kubeToBenchmarkMap map[string]string, kv string) (string, error) {
|
2019-11-12 21:47:42 +00:00
|
|
|
kvOriginal := kv
|
2019-11-05 21:31:27 +00:00
|
|
|
cisVersion, found := kubeToBenchmarkMap[kv]
|
2019-11-12 21:47:42 +00:00
|
|
|
glog.V(2).Info(fmt.Sprintf("mapToBenchmarkVersion for k8sVersion: %q cisVersion: %q found: %t\n", kv, cisVersion, found))
|
2019-11-05 21:31:27 +00:00
|
|
|
for !found && (kv != defaultKubeVersion && !isEmpty(kv)) {
|
|
|
|
kv = decrementVersion(kv)
|
|
|
|
cisVersion, found = kubeToBenchmarkMap[kv]
|
2019-11-12 21:47:42 +00:00
|
|
|
glog.V(2).Info(fmt.Sprintf("mapToBenchmarkVersion for k8sVersion: %q cisVersion: %q found: %t\n", kv, cisVersion, found))
|
2019-11-05 21:31:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
2019-11-12 21:47:42 +00:00
|
|
|
glog.V(1).Info(fmt.Sprintf("mapToBenchmarkVersion unable to find a match for: %q", kvOriginal))
|
2020-08-30 07:16:21 +00:00
|
|
|
glog.V(3).Info(fmt.Sprintf("mapToBenchmarkVersion kubeToBenchmarkMap: %#v", kubeToBenchmarkMap))
|
2019-11-12 21:47:42 +00:00
|
|
|
return "", fmt.Errorf("unable to find a matching Benchmark Version match for kubernetes version: %s", kvOriginal)
|
2019-11-05 21:31:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cisVersion, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadVersionMapping(v *viper.Viper) (map[string]string, error) {
|
|
|
|
kubeToBenchmarkMap := v.GetStringMapString("version_mapping")
|
|
|
|
if kubeToBenchmarkMap == nil || (len(kubeToBenchmarkMap) == 0) {
|
|
|
|
return nil, fmt.Errorf("config file is missing 'version_mapping' section")
|
|
|
|
}
|
|
|
|
|
|
|
|
return kubeToBenchmarkMap, nil
|
|
|
|
}
|
|
|
|
|
2020-08-30 07:16:21 +00:00
|
|
|
func loadTargetMapping(v *viper.Viper) (map[string][]string, error) {
|
|
|
|
benchmarkVersionToTargetsMap := v.GetStringMapStringSlice("target_mapping")
|
|
|
|
if len(benchmarkVersionToTargetsMap) == 0 {
|
|
|
|
return nil, fmt.Errorf("config file is missing 'target_mapping' section")
|
|
|
|
}
|
|
|
|
|
|
|
|
return benchmarkVersionToTargetsMap, nil
|
|
|
|
}
|
|
|
|
|
2019-11-05 21:31:27 +00:00
|
|
|
func getBenchmarkVersion(kubeVersion, benchmarkVersion string, v *viper.Viper) (bv string, err error) {
|
|
|
|
if !isEmpty(kubeVersion) && !isEmpty(benchmarkVersion) {
|
|
|
|
return "", fmt.Errorf("It is an error to specify both --version and --benchmark flags")
|
|
|
|
}
|
2020-12-21 11:18:54 +00:00
|
|
|
if isEmpty(benchmarkVersion) && isEmpty(kubeVersion) {
|
|
|
|
benchmarkVersion = getPlatformBenchmarkVersion(getPlatformName())
|
|
|
|
}
|
2019-11-05 21:31:27 +00:00
|
|
|
|
|
|
|
if isEmpty(benchmarkVersion) {
|
|
|
|
if isEmpty(kubeVersion) {
|
2020-12-21 11:18:54 +00:00
|
|
|
kv, err := getKubeVersion()
|
2019-11-05 21:31:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Version check failed: %s\nAlternatively, you can specify the version with --version", err)
|
|
|
|
}
|
2020-12-21 11:18:54 +00:00
|
|
|
kubeVersion = kv.BaseVersion()
|
2019-11-05 21:31:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
kubeToBenchmarkMap, err := loadVersionMapping(v)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
benchmarkVersion, err = mapToBenchmarkVersion(kubeToBenchmarkMap, kubeVersion)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(2).Info(fmt.Sprintf("Mapped Kubernetes version: %s to Benchmark version: %s", kubeVersion, benchmarkVersion))
|
|
|
|
}
|
2019-11-12 21:47:42 +00:00
|
|
|
|
|
|
|
glog.V(1).Info(fmt.Sprintf("Kubernetes version: %q to Benchmark version: %q", kubeVersion, benchmarkVersion))
|
2019-11-05 21:31:27 +00:00
|
|
|
return benchmarkVersion, nil
|
|
|
|
}
|
|
|
|
|
2019-03-07 17:02:43 +00:00
|
|
|
// isMaster verify if master components are running on the node.
|
|
|
|
func isMaster() bool {
|
2019-12-05 20:55:44 +00:00
|
|
|
return isThisNodeRunning(check.MASTER)
|
|
|
|
}
|
|
|
|
|
|
|
|
// isEtcd verify if etcd components are running on the node.
|
|
|
|
func isEtcd() bool {
|
|
|
|
return isThisNodeRunning(check.ETCD)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isThisNodeRunning(nodeType check.NodeType) bool {
|
2020-08-30 07:16:21 +00:00
|
|
|
glog.V(3).Infof("Checking if the current node is running %s components", nodeType)
|
|
|
|
nodeTypeConf := viper.Sub(string(nodeType))
|
|
|
|
if nodeTypeConf == nil {
|
|
|
|
glog.V(2).Infof("No config for %s components found", nodeType)
|
2019-10-14 15:15:08 +00:00
|
|
|
return false
|
|
|
|
}
|
2019-04-11 16:05:57 +00:00
|
|
|
|
2020-08-30 07:16:21 +00:00
|
|
|
components, err := getBinariesFunc(nodeTypeConf, nodeType)
|
2019-04-11 16:05:57 +00:00
|
|
|
if err != nil {
|
2020-08-30 07:16:21 +00:00
|
|
|
glog.V(2).Infof("Failed to find %s binaries: %v", nodeType, err)
|
2019-03-07 17:02:43 +00:00
|
|
|
return false
|
|
|
|
}
|
2019-04-11 16:05:57 +00:00
|
|
|
if len(components) == 0 {
|
2019-12-05 20:55:44 +00:00
|
|
|
glog.V(2).Infof("No %s binaries specified", nodeType)
|
2019-04-11 16:05:57 +00:00
|
|
|
return false
|
|
|
|
}
|
2019-12-05 20:55:44 +00:00
|
|
|
|
2020-08-30 07:16:21 +00:00
|
|
|
glog.V(2).Infof("Node is running %s components", nodeType)
|
2019-03-07 17:02:43 +00:00
|
|
|
return true
|
|
|
|
}
|
2019-05-05 07:52:28 +00:00
|
|
|
|
2020-10-29 10:12:45 +00:00
|
|
|
func exitCodeSelection(controlsCollection []*check.Controls) int {
|
|
|
|
for _, control := range controlsCollection {
|
2020-12-21 11:18:54 +00:00
|
|
|
if control.Fail > 0 {
|
2020-10-29 10:12:45 +00:00
|
|
|
return exitCode
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-06-24 09:13:10 +00:00
|
|
|
func writeOutput(controlsCollection []*check.Controls) {
|
|
|
|
sort.Slice(controlsCollection, func(i, j int) bool {
|
|
|
|
iid, _ := strconv.Atoi(controlsCollection[i].ID)
|
|
|
|
jid, _ := strconv.Atoi(controlsCollection[j].ID)
|
|
|
|
return iid < jid
|
|
|
|
})
|
|
|
|
if junitFmt {
|
|
|
|
writeJunitOutput(controlsCollection)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if jsonFmt {
|
2020-08-30 07:16:21 +00:00
|
|
|
writeJSONOutput(controlsCollection)
|
2020-06-24 09:13:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if pgSQL {
|
|
|
|
writePgsqlOutput(controlsCollection)
|
|
|
|
return
|
|
|
|
}
|
2020-11-23 19:43:53 +00:00
|
|
|
if aSFF {
|
|
|
|
writeASFFOutput(controlsCollection)
|
|
|
|
return
|
|
|
|
}
|
2020-06-24 09:13:10 +00:00
|
|
|
writeStdoutOutput(controlsCollection)
|
|
|
|
}
|
|
|
|
|
2020-08-30 07:16:21 +00:00
|
|
|
func writeJSONOutput(controlsCollection []*check.Controls) {
|
2020-12-21 11:18:54 +00:00
|
|
|
var out []byte
|
|
|
|
var err error
|
|
|
|
if !noTotals {
|
|
|
|
var totals check.OverallControls
|
|
|
|
totals.Controls = controlsCollection
|
|
|
|
totals.Totals = getSummaryTotals(controlsCollection)
|
|
|
|
out, err = json.Marshal(totals)
|
|
|
|
} else {
|
|
|
|
out, err = json.Marshal(controlsCollection)
|
|
|
|
}
|
2020-06-24 09:13:10 +00:00
|
|
|
if err != nil {
|
|
|
|
exitWithError(fmt.Errorf("failed to output in JSON format: %v", err))
|
|
|
|
}
|
2020-08-30 07:16:21 +00:00
|
|
|
printOutput(string(out), outputFile)
|
2020-06-24 09:13:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func writeJunitOutput(controlsCollection []*check.Controls) {
|
|
|
|
for _, controls := range controlsCollection {
|
|
|
|
out, err := controls.JUnit()
|
|
|
|
if err != nil {
|
|
|
|
exitWithError(fmt.Errorf("failed to output in JUnit format: %v", err))
|
|
|
|
}
|
2020-08-30 07:16:21 +00:00
|
|
|
printOutput(string(out), outputFile)
|
2020-06-24 09:13:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func writePgsqlOutput(controlsCollection []*check.Controls) {
|
|
|
|
for _, controls := range controlsCollection {
|
|
|
|
out, err := controls.JSON()
|
|
|
|
if err != nil {
|
|
|
|
exitWithError(fmt.Errorf("failed to output in Postgresql format: %v", err))
|
|
|
|
}
|
|
|
|
savePgsql(string(out))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-23 19:43:53 +00:00
|
|
|
func writeASFFOutput(controlsCollection []*check.Controls) {
|
|
|
|
for _, controls := range controlsCollection {
|
|
|
|
out, err := controls.ASFF()
|
|
|
|
if err != nil {
|
|
|
|
exitWithError(fmt.Errorf("failed to format findings as ASFF: %v", err))
|
|
|
|
}
|
|
|
|
if err := writeFinding(out); err != nil {
|
|
|
|
exitWithError(fmt.Errorf("failed to output to ASFF: %v", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-24 09:13:10 +00:00
|
|
|
func writeStdoutOutput(controlsCollection []*check.Controls) {
|
|
|
|
for _, controls := range controlsCollection {
|
|
|
|
summary := controls.Summary
|
|
|
|
prettyPrint(controls, summary)
|
|
|
|
}
|
2020-12-21 11:18:54 +00:00
|
|
|
if !noTotals {
|
|
|
|
printSummary(getSummaryTotals(controlsCollection), "total")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSummaryTotals(controlsCollection []*check.Controls) check.Summary {
|
|
|
|
var totalSummary check.Summary
|
|
|
|
for _, controls := range controlsCollection {
|
|
|
|
summary := controls.Summary
|
|
|
|
totalSummary.Fail = totalSummary.Fail + summary.Fail
|
|
|
|
totalSummary.Warn = totalSummary.Warn + summary.Warn
|
|
|
|
totalSummary.Pass = totalSummary.Pass + summary.Pass
|
|
|
|
totalSummary.Info = totalSummary.Info + summary.Info
|
|
|
|
}
|
|
|
|
return totalSummary
|
2020-06-24 09:13:10 +00:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2020-08-30 07:16:21 +00:00
|
|
|
func printOutput(output string, outputFile string) {
|
2020-09-01 13:50:04 +00:00
|
|
|
if outputFile == "" {
|
2019-05-29 15:05:55 +00:00
|
|
|
fmt.Println(output)
|
|
|
|
} else {
|
|
|
|
err := writeOutputToFile(output, outputFile)
|
|
|
|
if err != nil {
|
|
|
|
exitWithError(fmt.Errorf("Failed to write to output file %s: %v", outputFile, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-05 20:55:44 +00:00
|
|
|
|
|
|
|
// validTargets helps determine if the targets
|
|
|
|
// are legitimate for the benchmarkVersion.
|
2020-08-30 07:16:21 +00:00
|
|
|
func validTargets(benchmarkVersion string, targets []string, v *viper.Viper) (bool, error) {
|
|
|
|
benchmarkVersionToTargetsMap, err := loadTargetMapping(v)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2019-12-05 20:55:44 +00:00
|
|
|
providedTargets, found := benchmarkVersionToTargetsMap[benchmarkVersion]
|
|
|
|
if !found {
|
2020-08-30 07:16:21 +00:00
|
|
|
return false, fmt.Errorf("No targets configured for %s", benchmarkVersion)
|
2019-12-05 20:55:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, pt := range targets {
|
|
|
|
f := false
|
|
|
|
for _, t := range providedTargets {
|
|
|
|
if pt == strings.ToLower(t) {
|
|
|
|
f = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !f {
|
2020-08-30 07:16:21 +00:00
|
|
|
return false, nil
|
2019-12-05 20:55:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-30 07:16:21 +00:00
|
|
|
return true, nil
|
2019-12-05 20:55:44 +00:00
|
|
|
}
|