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 check
|
|
|
|
|
|
|
|
import (
|
2017-06-30 11:40:39 +00:00
|
|
|
"bytes"
|
2017-05-26 09:25:29 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-08-12 19:10:31 +00:00
|
|
|
"os"
|
2017-05-26 09:25:29 +00:00
|
|
|
"os/exec"
|
2017-08-12 12:29:21 +00:00
|
|
|
"regexp"
|
2017-05-26 09:25:29 +00:00
|
|
|
"strings"
|
2017-07-25 00:34:07 +00:00
|
|
|
|
|
|
|
"github.com/golang/glog"
|
2017-05-26 09:25:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NodeType indicates the type of node (master, node, federated).
|
|
|
|
type NodeType string
|
|
|
|
|
|
|
|
// State is the state of a control check.
|
|
|
|
type State string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// PASS check passed.
|
|
|
|
PASS State = "PASS"
|
|
|
|
// FAIL check failed.
|
2019-04-29 10:17:06 +00:00
|
|
|
FAIL State = "FAIL"
|
2017-05-26 09:25:29 +00:00
|
|
|
// WARN could not carry out check.
|
2019-04-29 10:17:06 +00:00
|
|
|
WARN State = "WARN"
|
2017-06-20 08:54:17 +00:00
|
|
|
// INFO informational message
|
2019-04-29 10:17:06 +00:00
|
|
|
INFO State = "INFO"
|
2017-05-26 09:25:29 +00:00
|
|
|
|
|
|
|
// MASTER a master node
|
|
|
|
MASTER NodeType = "master"
|
|
|
|
// NODE a node
|
|
|
|
NODE NodeType = "node"
|
|
|
|
// FEDERATED a federated deployment.
|
|
|
|
FEDERATED NodeType = "federated"
|
|
|
|
)
|
|
|
|
|
2017-07-07 17:01:30 +00:00
|
|
|
func handleError(err error, context string) (errmsg string) {
|
|
|
|
if err != nil {
|
|
|
|
errmsg = fmt.Sprintf("%s, error: %s\n", context, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-26 09:25:29 +00:00
|
|
|
// Check contains information about a recommendation in the
|
|
|
|
// CIS Kubernetes 1.6+ document.
|
|
|
|
type Check struct {
|
2018-07-30 12:16:28 +00:00
|
|
|
ID string `yaml:"id" json:"test_number"`
|
|
|
|
Text string `json:"test_desc"`
|
2019-05-19 08:23:44 +00:00
|
|
|
Audit string `json:"audit"`
|
2017-08-06 15:29:55 +00:00
|
|
|
Type string `json:"type"`
|
2017-05-26 09:25:29 +00:00
|
|
|
Commands []*exec.Cmd `json:"omit"`
|
|
|
|
Tests *tests `json:"omit"`
|
|
|
|
Set bool `json:"omit"`
|
2019-05-05 23:06:13 +00:00
|
|
|
Remediation string `json:"remediation"`
|
2018-07-30 12:16:28 +00:00
|
|
|
TestInfo []string `json:"test_info"`
|
|
|
|
State `json:"status"`
|
|
|
|
ActualValue string `json:"actual_value"`
|
2019-01-29 14:33:58 +00:00
|
|
|
Scored bool `json:"scored"`
|
2019-05-06 09:30:11 +00:00
|
|
|
ExpectedResult string `json:"expected_result"`
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
|
2019-04-29 10:17:06 +00:00
|
|
|
// Runner wraps the basic Run method.
|
|
|
|
type Runner interface {
|
|
|
|
// Run runs a given check and returns the execution state.
|
|
|
|
Run(c *Check) State
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRunner constructs a default Runner.
|
|
|
|
func NewRunner() Runner {
|
|
|
|
return &defaultRunner{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type defaultRunner struct{}
|
|
|
|
|
|
|
|
func (r *defaultRunner) Run(c *Check) State {
|
|
|
|
return c.run()
|
|
|
|
}
|
|
|
|
|
2017-05-26 09:25:29 +00:00
|
|
|
// Run executes the audit commands specified in a check and outputs
|
|
|
|
// the results.
|
2019-04-29 10:17:06 +00:00
|
|
|
func (c *Check) run() State {
|
2019-01-29 14:33:58 +00:00
|
|
|
|
2019-02-18 08:40:57 +00:00
|
|
|
// If check type is skip, force result to INFO
|
2019-01-29 14:33:58 +00:00
|
|
|
if c.Type == "skip" {
|
|
|
|
c.State = INFO
|
2019-04-29 10:17:06 +00:00
|
|
|
return c.State
|
2019-01-29 14:33:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If check type is manual or the check is not scored, force result to WARN
|
|
|
|
if c.Type == "manual" || !c.Scored {
|
2017-08-06 15:29:55 +00:00
|
|
|
c.State = WARN
|
2019-04-29 10:17:06 +00:00
|
|
|
return c.State
|
2017-08-06 15:29:55 +00:00
|
|
|
}
|
|
|
|
|
2017-07-05 12:56:01 +00:00
|
|
|
var out bytes.Buffer
|
2017-07-07 17:01:30 +00:00
|
|
|
var errmsgs string
|
2017-05-26 09:25:29 +00:00
|
|
|
|
|
|
|
// Check if command exists or exit with WARN.
|
|
|
|
for _, cmd := range c.Commands {
|
2017-08-12 12:29:21 +00:00
|
|
|
if !isShellCommand(cmd.Path) {
|
2017-05-26 09:25:29 +00:00
|
|
|
c.State = WARN
|
2019-04-29 10:17:06 +00:00
|
|
|
return c.State
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run commands.
|
2017-06-30 11:40:39 +00:00
|
|
|
n := len(c.Commands)
|
|
|
|
if n == 0 {
|
2017-05-26 09:25:29 +00:00
|
|
|
// Likely a warning message.
|
|
|
|
c.State = WARN
|
2019-04-29 10:17:06 +00:00
|
|
|
return c.State
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
|
2017-06-30 11:40:39 +00:00
|
|
|
// Each command runs,
|
|
|
|
// cmd0 out -> cmd1 in, cmd1 out -> cmd2 in ... cmdn out -> os.stdout
|
|
|
|
// cmd0 err should terminate chain
|
|
|
|
cs := c.Commands
|
|
|
|
|
2017-07-04 17:04:31 +00:00
|
|
|
// Initialize command pipeline
|
2017-06-30 11:40:39 +00:00
|
|
|
cs[n-1].Stdout = &out
|
|
|
|
i := 1
|
|
|
|
|
|
|
|
var err error
|
2017-07-07 17:01:30 +00:00
|
|
|
errmsgs = ""
|
|
|
|
|
2017-06-30 11:40:39 +00:00
|
|
|
for i < n {
|
|
|
|
cs[i-1].Stdout, err = cs[i].StdinPipe()
|
2017-07-07 17:01:30 +00:00
|
|
|
errmsgs += handleError(
|
|
|
|
err,
|
2017-07-13 01:01:18 +00:00
|
|
|
fmt.Sprintf("failed to run: %s\nfailed command: %s",
|
2017-07-07 17:01:30 +00:00
|
|
|
c.Audit,
|
|
|
|
cs[i].Args,
|
|
|
|
),
|
|
|
|
)
|
2017-06-30 11:40:39 +00:00
|
|
|
i++
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-04 17:04:31 +00:00
|
|
|
// Start command pipeline
|
2017-06-30 11:40:39 +00:00
|
|
|
i = 0
|
|
|
|
for i < n {
|
2017-07-05 12:56:01 +00:00
|
|
|
err := cs[i].Start()
|
2017-07-07 17:01:30 +00:00
|
|
|
errmsgs += handleError(
|
|
|
|
err,
|
2017-07-13 01:01:18 +00:00
|
|
|
fmt.Sprintf("failed to run: %s\nfailed command: %s",
|
2017-07-07 17:01:30 +00:00
|
|
|
c.Audit,
|
|
|
|
cs[i].Args,
|
|
|
|
),
|
|
|
|
)
|
2017-07-04 17:04:31 +00:00
|
|
|
i++
|
|
|
|
}
|
2017-05-26 09:25:29 +00:00
|
|
|
|
2017-07-04 17:04:31 +00:00
|
|
|
// Complete command pipeline
|
|
|
|
i = 0
|
|
|
|
for i < n {
|
2017-07-05 12:56:01 +00:00
|
|
|
err := cs[i].Wait()
|
2017-07-07 17:01:30 +00:00
|
|
|
errmsgs += handleError(
|
|
|
|
err,
|
2017-07-13 01:01:18 +00:00
|
|
|
fmt.Sprintf("failed to run: %s\nfailed command:%s",
|
2017-07-07 17:01:30 +00:00
|
|
|
c.Audit,
|
|
|
|
cs[i].Args,
|
|
|
|
),
|
|
|
|
)
|
2017-05-26 09:25:29 +00:00
|
|
|
|
2017-06-30 11:40:39 +00:00
|
|
|
if i < n-1 {
|
|
|
|
cs[i].Stdout.(io.Closer).Close()
|
|
|
|
}
|
2017-07-04 17:04:31 +00:00
|
|
|
|
2017-06-30 11:40:39 +00:00
|
|
|
i++
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 22:47:26 +00:00
|
|
|
glog.V(3).Info(out.String())
|
|
|
|
|
2018-07-30 12:16:28 +00:00
|
|
|
finalOutput := c.Tests.execute(out.String())
|
|
|
|
if finalOutput != nil {
|
|
|
|
c.ActualValue = finalOutput.actualResult
|
2019-05-06 09:30:11 +00:00
|
|
|
c.ExpectedResult = finalOutput.ExpectedResult
|
2018-07-30 12:16:28 +00:00
|
|
|
if finalOutput.testResult {
|
|
|
|
c.State = PASS
|
|
|
|
} else {
|
|
|
|
c.State = FAIL
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
errmsgs += handleError(
|
|
|
|
fmt.Errorf("final output is nil"),
|
|
|
|
fmt.Sprintf("failed to run: %s\n",
|
|
|
|
c.Audit,
|
|
|
|
),
|
|
|
|
)
|
2017-08-31 13:41:52 +00:00
|
|
|
}
|
2017-07-07 17:01:30 +00:00
|
|
|
|
2018-07-30 12:16:28 +00:00
|
|
|
if errmsgs != "" {
|
|
|
|
glog.V(2).Info(errmsgs)
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
2019-04-29 10:17:06 +00:00
|
|
|
return c.State
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
|
2017-08-12 12:29:21 +00:00
|
|
|
// textToCommand transforms an input text representation of commands to be
|
2017-05-26 09:25:29 +00:00
|
|
|
// run into a slice of commands.
|
|
|
|
// TODO: Make this more robust.
|
|
|
|
func textToCommand(s string) []*exec.Cmd {
|
|
|
|
cmds := []*exec.Cmd{}
|
|
|
|
|
|
|
|
cp := strings.Split(s, "|")
|
|
|
|
|
|
|
|
for _, v := range cp {
|
|
|
|
v = strings.Trim(v, " ")
|
2017-08-12 12:29:21 +00:00
|
|
|
|
|
|
|
// TODO:
|
|
|
|
// GOAL: To split input text into arguments for exec.Cmd.
|
|
|
|
//
|
|
|
|
// CHALLENGE: The input text may contain quoted strings that
|
|
|
|
// must be passed as a unit to exec.Cmd.
|
|
|
|
// eg. bash -c 'foo bar'
|
|
|
|
// 'foo bar' must be passed as unit to exec.Cmd if not the command
|
|
|
|
// will fail when it is executed.
|
|
|
|
// eg. exec.Cmd("bash", "-c", "foo bar")
|
|
|
|
//
|
|
|
|
// PROBLEM: Current solution assumes the grouped string will always
|
|
|
|
// be at the end of the input text.
|
|
|
|
re := regexp.MustCompile(`^(.*)(['"].*['"])$`)
|
|
|
|
grps := re.FindStringSubmatch(v)
|
|
|
|
|
|
|
|
var cs []string
|
|
|
|
if len(grps) > 0 {
|
|
|
|
s := strings.Trim(grps[1], " ")
|
|
|
|
cs = strings.Split(s, " ")
|
|
|
|
|
|
|
|
s1 := grps[len(grps)-1]
|
|
|
|
s1 = strings.Trim(s1, "'\"")
|
|
|
|
|
|
|
|
cs = append(cs, s1)
|
|
|
|
} else {
|
|
|
|
cs = strings.Split(v, " ")
|
|
|
|
}
|
2017-05-26 09:25:29 +00:00
|
|
|
|
|
|
|
cmd := exec.Command(cs[0], cs[1:]...)
|
|
|
|
cmds = append(cmds, cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmds
|
|
|
|
}
|
2017-08-12 12:29:21 +00:00
|
|
|
|
|
|
|
func isShellCommand(s string) bool {
|
|
|
|
cmd := exec.Command("/bin/sh", "-c", "command -v "+s)
|
|
|
|
|
|
|
|
out, err := cmd.Output()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(string(out), s) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|