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 (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2018-03-23 08:27:48 +00:00
|
|
|
"path/filepath"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-07-13 00:24:09 +00:00
|
|
|
errmsgs string
|
2017-05-26 09:25:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func runChecks(t check.NodeType) {
|
|
|
|
var summary check.Summary
|
2017-11-28 17:33:42 +00:00
|
|
|
var nodetype string
|
2017-05-26 09:25:29 +00:00
|
|
|
var file string
|
2017-08-30 17:37:01 +00:00
|
|
|
var err error
|
|
|
|
var typeConf *viper.Viper
|
2017-05-26 09:25:29 +00:00
|
|
|
|
2017-08-30 17:37:01 +00:00
|
|
|
switch t {
|
|
|
|
case check.MASTER:
|
|
|
|
file = masterFile
|
2017-11-28 17:33:42 +00:00
|
|
|
nodetype = "master"
|
2017-05-26 09:25:29 +00:00
|
|
|
case check.NODE:
|
|
|
|
file = nodeFile
|
2017-11-28 17:33:42 +00:00
|
|
|
nodetype = "node"
|
2017-05-26 09:25:29 +00:00
|
|
|
case check.FEDERATED:
|
|
|
|
file = federatedFile
|
2017-11-28 17:33:42 +00:00
|
|
|
nodetype = "federated"
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
|
2018-04-12 18:22:50 +00:00
|
|
|
var ver string
|
|
|
|
if kubeVersion != "" {
|
|
|
|
ver = kubeVersion
|
|
|
|
} else {
|
|
|
|
ver = getKubeVersion()
|
|
|
|
}
|
|
|
|
|
2018-04-04 09:44:32 +00:00
|
|
|
switch ver {
|
|
|
|
case "1.9", "1.10":
|
|
|
|
continueWithError(nil, fmt.Sprintf("No CIS spec for %s - using tests from CIS 1.2.0 spec for Kubernetes 1.8\n", ver))
|
|
|
|
ver = "1.8"
|
|
|
|
}
|
2018-03-27 13:33:01 +00:00
|
|
|
|
2018-04-04 09:44:32 +00:00
|
|
|
path := filepath.Join(cfgDir, ver)
|
2018-03-23 08:27:48 +00:00
|
|
|
def := filepath.Join(path, file)
|
2018-03-27 13:33:01 +00:00
|
|
|
|
2017-11-28 17:33:42 +00:00
|
|
|
in, err := ioutil.ReadFile(def)
|
2017-05-26 09:25:29 +00:00
|
|
|
if err != nil {
|
2017-07-25 00:34:07 +00:00
|
|
|
exitWithError(fmt.Errorf("error opening %s controls file: %v", t, err))
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
|
2017-11-28 17:33:42 +00:00
|
|
|
// Merge kubernetes version specific config if any.
|
|
|
|
viper.SetConfigFile(path + "/config.yaml")
|
|
|
|
err = viper.MergeInConfig()
|
2017-05-26 09:25:29 +00:00
|
|
|
if err != nil {
|
2017-11-28 17:47:57 +00:00
|
|
|
continueWithError(err, fmt.Sprintf("Reading %s specific configuration file", ver))
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
2017-11-28 17:33:42 +00:00
|
|
|
typeConf = viper.Sub(nodetype)
|
|
|
|
|
|
|
|
// Get the set of exectuables and config files we care about on this type of node. This also
|
|
|
|
// checks that the executables we need for the node type are running.
|
|
|
|
binmap := getBinaries(typeConf)
|
|
|
|
confmap := getConfigFiles(typeConf)
|
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)
|
2017-05-26 09:25:29 +00:00
|
|
|
|
2017-09-26 23:33:47 +00:00
|
|
|
glog.V(1).Info(fmt.Sprintf("Using config file: %s\n", viper.ConfigFileUsed()))
|
2017-11-28 17:33:42 +00:00
|
|
|
glog.V(1).Info(fmt.Sprintf("Using benchmark file: %s\n", def))
|
2017-09-26 23:33:47 +00:00
|
|
|
|
2017-06-23 11:04:46 +00:00
|
|
|
controls, err := check.NewControls(t, []byte(s))
|
|
|
|
if err != nil {
|
2017-07-25 00:34:07 +00:00
|
|
|
exitWithError(fmt.Errorf("error setting up %s controls: %v", t, err))
|
2017-06-23 11:04:46 +00:00
|
|
|
}
|
2017-05-26 09:25:29 +00:00
|
|
|
|
|
|
|
if groupList != "" && checkList == "" {
|
|
|
|
ids := cleanIDs(groupList)
|
2017-07-25 00:34:07 +00:00
|
|
|
summary = controls.RunGroup(ids...)
|
2017-05-26 09:25:29 +00:00
|
|
|
} else if checkList != "" && groupList == "" {
|
|
|
|
ids := cleanIDs(checkList)
|
2017-07-25 00:34:07 +00:00
|
|
|
summary = controls.RunChecks(ids...)
|
2017-05-26 09:25:29 +00:00
|
|
|
} else if checkList != "" && groupList != "" {
|
2017-07-25 00:34:07 +00:00
|
|
|
exitWithError(fmt.Errorf("group option and check option can't be used together"))
|
2017-05-26 09:25:29 +00:00
|
|
|
} else {
|
2017-07-25 00:34:07 +00:00
|
|
|
summary = controls.RunGroup()
|
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
|
|
|
|
if (summary.Fail > 0 || summary.Warn > 0 || summary.Pass > 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
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(string(out))
|
|
|
|
} else {
|
2017-10-31 20:08:46 +00:00
|
|
|
// if we want to store in PostgreSQL, convert to JSON and save it
|
2018-01-11 18:01:58 +00:00
|
|
|
if (summary.Fail > 0 || summary.Warn > 0 || summary.Pass > 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))
|
|
|
|
}
|
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 {
|
|
|
|
if c.State != check.PASS {
|
|
|
|
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")
|
|
|
|
fmt.Printf("%d checks PASS\n%d checks FAIL\n%d checks WARN\n",
|
|
|
|
summary.Pass, summary.Fail, summary.Warn,
|
|
|
|
)
|
|
|
|
}
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|