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"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2017-07-25 00:34:07 +00:00
|
|
|
|
|
|
|
"github.com/golang/glog"
|
2017-05-26 09:25:29 +00:00
|
|
|
)
|
|
|
|
|
2019-10-12 23:00:26 +00:00
|
|
|
// NodeType indicates the type of node (master, node).
|
2017-05-26 09:25:29 +00:00
|
|
|
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"
|
2019-10-14 14:37:10 +00:00
|
|
|
// FEDERATED a federated deployment.
|
|
|
|
FEDERATED NodeType = "federated"
|
2017-05-26 09:25:29 +00:00
|
|
|
|
2019-12-05 20:55:44 +00:00
|
|
|
// ETCD an etcd node
|
|
|
|
ETCD NodeType = "etcd"
|
|
|
|
// CONTROLPLANE a control plane node
|
|
|
|
CONTROLPLANE NodeType = "controlplane"
|
|
|
|
// POLICIES a node to run policies from
|
|
|
|
POLICIES NodeType = "policies"
|
2020-03-03 14:51:48 +00:00
|
|
|
// MANAGEDSERVICES a node to run managedservices from
|
|
|
|
MANAGEDSERVICES = "managedservices"
|
2019-12-05 20:55:44 +00:00
|
|
|
|
2019-10-14 14:37:10 +00:00
|
|
|
// MANUAL Check Type
|
|
|
|
MANUAL string = "manual"
|
|
|
|
)
|
2017-07-07 17:01:30 +00:00
|
|
|
|
2017-05-26 09:25:29 +00:00
|
|
|
// Check contains information about a recommendation in the
|
2019-10-14 14:52:43 +00:00
|
|
|
// CIS Kubernetes document.
|
2017-05-26 09:25:29 +00:00
|
|
|
type Check struct {
|
2020-06-22 07:45:31 +00:00
|
|
|
ID string `yaml:"id" json:"test_number"`
|
|
|
|
Text string `json:"test_desc"`
|
|
|
|
Audit string `json:"audit"`
|
|
|
|
AuditConfig string `yaml:"audit_config"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Tests *tests `json:"omit"`
|
|
|
|
Set bool `json:"omit"`
|
|
|
|
Remediation string `json:"remediation"`
|
|
|
|
TestInfo []string `json:"test_info"`
|
2019-07-13 07:05:29 +00:00
|
|
|
State `json:"status"`
|
|
|
|
ActualValue string `json:"actual_value"`
|
|
|
|
Scored bool `json:"scored"`
|
2019-05-06 09:30:11 +00:00
|
|
|
ExpectedResult string `json:"expected_result"`
|
2020-06-22 07:45:31 +00:00
|
|
|
Reason string `json:"reason,omitempty"`
|
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-10-14 14:37:10 +00:00
|
|
|
// Since this is an Scored check
|
|
|
|
// without tests return a 'WARN' to alert
|
|
|
|
// the user that this check needs attention
|
|
|
|
if c.Scored && len(strings.TrimSpace(c.Type)) == 0 && c.Tests == nil {
|
2020-03-05 12:20:26 +00:00
|
|
|
c.Reason = "There are no tests"
|
2019-10-14 14:37:10 +00:00
|
|
|
c.State = WARN
|
|
|
|
return c.State
|
|
|
|
}
|
|
|
|
|
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" {
|
2020-03-05 12:20:26 +00:00
|
|
|
c.Reason = "Test marked as skip"
|
2019-01-29 14:33:58 +00:00
|
|
|
c.State = INFO
|
2019-04-29 10:17:06 +00:00
|
|
|
return c.State
|
2019-01-29 14:33:58 +00:00
|
|
|
}
|
|
|
|
|
2019-07-13 07:05:29 +00:00
|
|
|
// If check type is manual force result to WARN
|
2019-10-14 14:37:10 +00:00
|
|
|
if c.Type == MANUAL {
|
2020-03-05 12:20:26 +00:00
|
|
|
c.Reason = "Test marked as a manual test"
|
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
|
|
|
}
|
|
|
|
|
2019-10-14 14:37:10 +00:00
|
|
|
lastCommand := c.Audit
|
2020-06-22 07:45:31 +00:00
|
|
|
hasAuditConfig := c.AuditConfig != ""
|
2017-05-26 09:25:29 +00:00
|
|
|
|
2020-06-22 07:45:31 +00:00
|
|
|
state, finalOutput, retErrmsgs := performTest(c.Audit, c.Tests)
|
2019-10-14 14:37:10 +00:00
|
|
|
if len(state) > 0 {
|
2020-03-05 12:20:26 +00:00
|
|
|
c.Reason = retErrmsgs
|
2019-10-14 14:37:10 +00:00
|
|
|
c.State = state
|
2019-04-29 10:17:06 +00:00
|
|
|
return c.State
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
2019-10-14 14:37:10 +00:00
|
|
|
errmsgs := retErrmsgs
|
|
|
|
|
|
|
|
// If something went wrong with the 'Audit' command
|
|
|
|
// and an 'AuditConfig' command was provided, use it to
|
|
|
|
// execute tests
|
|
|
|
if (finalOutput == nil || !finalOutput.testResult) && hasAuditConfig {
|
|
|
|
lastCommand = c.AuditConfig
|
|
|
|
|
|
|
|
nItems := len(c.Tests.TestItems)
|
|
|
|
// The reason we're creating a copy of the "tests"
|
|
|
|
// is so that tests can executed
|
|
|
|
// with the AuditConfig command
|
|
|
|
// against the Path only
|
|
|
|
currentTests := &tests{
|
|
|
|
BinOp: c.Tests.BinOp,
|
|
|
|
TestItems: make([]*testItem, nItems),
|
|
|
|
}
|
2017-05-26 09:25:29 +00:00
|
|
|
|
2019-10-14 14:37:10 +00:00
|
|
|
for i := 0; i < nItems; i++ {
|
|
|
|
ti := c.Tests.TestItems[i]
|
|
|
|
nti := &testItem{
|
|
|
|
// Path is used to test Command Param values
|
|
|
|
// AuditConfig ==> Path
|
|
|
|
Path: ti.Path,
|
|
|
|
Set: ti.Set,
|
|
|
|
Compare: ti.Compare,
|
|
|
|
}
|
|
|
|
currentTests.TestItems[i] = nti
|
2017-06-30 11:40:39 +00:00
|
|
|
}
|
2017-07-04 17:04:31 +00:00
|
|
|
|
2020-06-22 07:45:31 +00:00
|
|
|
state, finalOutput, retErrmsgs = performTest(c.AuditConfig, currentTests)
|
2019-10-14 14:37:10 +00:00
|
|
|
if len(state) > 0 {
|
2020-03-05 12:20:26 +00:00
|
|
|
c.Reason = retErrmsgs
|
2019-10-14 14:37:10 +00:00
|
|
|
c.State = state
|
|
|
|
return c.State
|
|
|
|
}
|
|
|
|
errmsgs += retErrmsgs
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
|
2019-10-14 14:37:10 +00:00
|
|
|
if finalOutput != nil && finalOutput.testResult {
|
|
|
|
c.State = PASS
|
2018-07-30 12:16:28 +00:00
|
|
|
c.ActualValue = finalOutput.actualResult
|
2019-05-06 09:30:11 +00:00
|
|
|
c.ExpectedResult = finalOutput.ExpectedResult
|
2019-10-14 14:37:10 +00:00
|
|
|
} else {
|
|
|
|
if c.Scored {
|
|
|
|
c.State = FAIL
|
2018-07-30 12:16:28 +00:00
|
|
|
} else {
|
2020-03-05 12:20:26 +00:00
|
|
|
c.Reason = errmsgs
|
2019-10-14 14:37:10 +00:00
|
|
|
c.State = WARN
|
2018-07-30 12:16:28 +00:00
|
|
|
}
|
2019-10-14 14:37:10 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 16:04:58 +00:00
|
|
|
if finalOutput != nil {
|
|
|
|
glog.V(3).Infof("Check.ID: %s Command: %q TestResult: %t State: %q \n", c.ID, lastCommand, finalOutput.testResult, c.State)
|
|
|
|
} else {
|
2019-10-14 14:37:10 +00:00
|
|
|
glog.V(3).Infof("Check.ID: %s Command: %q TestResult: <<EMPTY>> \n", c.ID, lastCommand)
|
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
|
|
|
}
|
|
|
|
|
2020-06-22 07:45:31 +00:00
|
|
|
func performTest(audit string, tests *tests) (State, *testOutput, string) {
|
2019-10-14 14:37:10 +00:00
|
|
|
if len(strings.TrimSpace(audit)) == 0 {
|
2020-03-05 12:20:26 +00:00
|
|
|
return "", failTestItem("missing command"), "missing audit command"
|
2019-10-14 14:37:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var out bytes.Buffer
|
2020-06-22 07:45:31 +00:00
|
|
|
errmsgs := runAudit(audit, &out)
|
2019-10-14 14:37:10 +00:00
|
|
|
|
|
|
|
finalOutput := tests.execute(out.String())
|
|
|
|
if finalOutput == nil {
|
|
|
|
errmsgs += fmt.Sprintf("Final output is <<EMPTY>>. Failed to run: %s\n", audit)
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", finalOutput, errmsgs
|
|
|
|
}
|
|
|
|
|
2020-06-22 07:45:31 +00:00
|
|
|
func runAudit(audit string, out *bytes.Buffer) string {
|
2019-10-14 14:37:10 +00:00
|
|
|
errmsgs := ""
|
|
|
|
|
2020-06-22 07:45:31 +00:00
|
|
|
cmd := exec.Command("/bin/sh")
|
|
|
|
cmd.Stdin = strings.NewReader(audit)
|
|
|
|
cmd.Stdout = out
|
|
|
|
cmd.Stderr = out
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
errmsgs += fmt.Sprintf("failed to run: %q, output: %q, error: %s\n", audit, out.String(), err)
|
2019-10-14 14:37:10 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 16:04:58 +00:00
|
|
|
glog.V(3).Infof("Command %q - Output:\n\n %q\n - Error Messages:%q \n", audit, out.String(), errmsgs)
|
2020-06-22 07:45:31 +00:00
|
|
|
return errmsgs
|
2019-12-13 16:04:58 +00:00
|
|
|
}
|