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 (
|
2019-11-13 13:03:04 +00:00
|
|
|
"bytes"
|
2017-05-26 09:25:29 +00:00
|
|
|
"encoding/json"
|
2019-11-13 13:03:04 +00:00
|
|
|
"encoding/xml"
|
2017-05-26 09:25:29 +00:00
|
|
|
"fmt"
|
2020-11-23 19:43:53 +00:00
|
|
|
"time"
|
2019-10-14 14:37:10 +00:00
|
|
|
|
2020-11-23 19:43:53 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/securityhub"
|
2019-05-01 19:43:06 +00:00
|
|
|
"github.com/golang/glog"
|
2019-11-13 13:03:04 +00:00
|
|
|
"github.com/onsi/ginkgo/reporters"
|
2020-11-23 19:43:53 +00:00
|
|
|
"github.com/spf13/viper"
|
2019-04-29 10:17:06 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
2017-05-26 09:25:29 +00:00
|
|
|
)
|
|
|
|
|
2020-11-23 19:43:53 +00:00
|
|
|
const (
|
|
|
|
// UNKNOWN is when the AWS account can't be found
|
|
|
|
UNKNOWN = "Unknown"
|
|
|
|
// ARN for the AWS Security Hub service
|
|
|
|
ARN = "arn:aws:securityhub:%s::product/aqua-security/kube-bench"
|
|
|
|
// SCHEMA for the AWS Security Hub service
|
|
|
|
SCHEMA = "2018-10-08"
|
|
|
|
// TYPE is type of Security Hub finding
|
|
|
|
TYPE = "Software and Configuration Checks/Industry and Regulatory Standards/CIS Kubernetes Benchmark"
|
|
|
|
)
|
|
|
|
|
2020-12-21 11:18:54 +00:00
|
|
|
type OverallControls struct {
|
|
|
|
Controls []*Controls
|
|
|
|
Totals Summary
|
|
|
|
}
|
|
|
|
|
2017-05-26 09:25:29 +00:00
|
|
|
// Controls holds all controls to check for master nodes.
|
|
|
|
type Controls struct {
|
2021-05-09 11:48:34 +00:00
|
|
|
ID string `yaml:"id" json:"id"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
DetectedVersion string `json:"detected_version,omitempty"`
|
|
|
|
Text string `json:"text"`
|
|
|
|
Type NodeType `json:"node_type"`
|
|
|
|
Groups []*Group `json:"tests"`
|
2017-05-26 09:25:29 +00:00
|
|
|
Summary
|
|
|
|
}
|
|
|
|
|
|
|
|
// Group is a collection of similar checks.
|
|
|
|
type Group struct {
|
2017-11-29 16:31:55 +00:00
|
|
|
ID string `yaml:"id" json:"section"`
|
2020-12-21 11:18:54 +00:00
|
|
|
Type string `yaml:"type" json:"type"`
|
2017-11-29 16:31:55 +00:00
|
|
|
Pass int `json:"pass"`
|
|
|
|
Fail int `json:"fail"`
|
|
|
|
Warn int `json:"warn"`
|
2019-01-29 14:33:58 +00:00
|
|
|
Info int `json:"info"`
|
2017-11-29 16:31:55 +00:00
|
|
|
Text string `json:"desc"`
|
|
|
|
Checks []*Check `json:"results"`
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Summary is a summary of the results of control checks run.
|
|
|
|
type Summary struct {
|
2018-07-30 12:16:28 +00:00
|
|
|
Pass int `json:"total_pass"`
|
|
|
|
Fail int `json:"total_fail"`
|
|
|
|
Warn int `json:"total_warn"`
|
2019-01-29 14:33:58 +00:00
|
|
|
Info int `json:"total_info"`
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
|
2019-05-01 19:43:06 +00:00
|
|
|
// Predicate a predicate on the given Group and Check arguments.
|
2019-04-29 10:17:06 +00:00
|
|
|
type Predicate func(group *Group, check *Check) bool
|
|
|
|
|
2017-05-26 09:25:29 +00:00
|
|
|
// NewControls instantiates a new master Controls object.
|
2021-05-09 11:48:34 +00:00
|
|
|
func NewControls(t NodeType, in []byte, detectedVersion string) (*Controls, error) {
|
2017-05-26 09:25:29 +00:00
|
|
|
c := new(Controls)
|
|
|
|
|
2017-06-23 11:04:46 +00:00
|
|
|
err := yaml.Unmarshal(in, c)
|
2017-05-26 09:25:29 +00:00
|
|
|
if err != nil {
|
2017-06-23 11:04:46 +00:00
|
|
|
return nil, fmt.Errorf("failed to unmarshal YAML: %s", err)
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if t != c.Type {
|
2017-06-23 11:04:46 +00:00
|
|
|
return nil, fmt.Errorf("non-%s controls file specified", t)
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
2021-05-09 11:48:34 +00:00
|
|
|
c.DetectedVersion = detectedVersion
|
2017-06-23 11:04:46 +00:00
|
|
|
return c, nil
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
|
2019-04-29 10:17:06 +00:00
|
|
|
// RunChecks runs the checks with the given Runner. Only checks for which the filter Predicate returns `true` will run.
|
2020-11-23 19:43:53 +00:00
|
|
|
func (controls *Controls) RunChecks(runner Runner, filter Predicate, skipIDMap map[string]bool) Summary {
|
2019-04-29 10:17:06 +00:00
|
|
|
var g []*Group
|
|
|
|
m := make(map[string]*Group)
|
2019-01-29 14:33:58 +00:00
|
|
|
controls.Summary.Pass, controls.Summary.Fail, controls.Summary.Warn, controls.Info = 0, 0, 0, 0
|
2017-05-26 09:25:29 +00:00
|
|
|
|
|
|
|
for _, group := range controls.Groups {
|
2019-04-29 10:17:06 +00:00
|
|
|
for _, check := range group.Checks {
|
2017-05-26 09:25:29 +00:00
|
|
|
|
2019-04-29 10:17:06 +00:00
|
|
|
if !filter(group, check) {
|
|
|
|
continue
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
|
2020-11-23 19:43:53 +00:00
|
|
|
_, groupSkippedViaCmd := skipIDMap[group.ID]
|
|
|
|
_, checkSkippedViaCmd := skipIDMap[check.ID]
|
2020-10-29 10:03:41 +00:00
|
|
|
|
2020-12-21 11:18:54 +00:00
|
|
|
if group.Type == SKIP || groupSkippedViaCmd || checkSkippedViaCmd {
|
2020-10-19 07:51:33 +00:00
|
|
|
check.Type = SKIP
|
|
|
|
}
|
|
|
|
|
2019-04-29 10:17:06 +00:00
|
|
|
state := runner.Run(check)
|
2020-10-19 07:51:33 +00:00
|
|
|
|
2019-04-29 10:17:06 +00:00
|
|
|
check.TestInfo = append(check.TestInfo, check.Remediation)
|
2017-05-26 09:25:29 +00:00
|
|
|
|
2019-04-29 10:17:06 +00:00
|
|
|
// Check if we have already added this checks group.
|
|
|
|
if v, ok := m[group.ID]; !ok {
|
|
|
|
// Create a group with same info
|
|
|
|
w := &Group{
|
|
|
|
ID: group.ID,
|
|
|
|
Text: group.Text,
|
|
|
|
Checks: []*Check{},
|
|
|
|
}
|
2017-05-26 09:25:29 +00:00
|
|
|
|
2019-04-29 10:17:06 +00:00
|
|
|
// Add this check to the new group
|
|
|
|
w.Checks = append(w.Checks, check)
|
|
|
|
summarizeGroup(w, state)
|
2017-05-26 09:25:29 +00:00
|
|
|
|
2019-04-29 10:17:06 +00:00
|
|
|
// Add to groups we have visited.
|
|
|
|
m[w.ID] = w
|
|
|
|
g = append(g, w)
|
|
|
|
} else {
|
|
|
|
v.Checks = append(v.Checks, check)
|
|
|
|
summarizeGroup(v, state)
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
2019-04-29 10:17:06 +00:00
|
|
|
|
|
|
|
summarize(controls, state)
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
controls.Groups = g
|
|
|
|
return controls.Summary
|
|
|
|
}
|
|
|
|
|
|
|
|
// JSON encodes the results of last run to JSON.
|
|
|
|
func (controls *Controls) JSON() ([]byte, error) {
|
|
|
|
return json.Marshal(controls)
|
|
|
|
}
|
|
|
|
|
2019-11-13 13:03:04 +00:00
|
|
|
// JUnit encodes the results of last run to JUnit.
|
|
|
|
func (controls *Controls) JUnit() ([]byte, error) {
|
|
|
|
suite := reporters.JUnitTestSuite{
|
|
|
|
Name: controls.Text,
|
|
|
|
TestCases: []reporters.JUnitTestCase{},
|
|
|
|
Tests: controls.Summary.Pass + controls.Summary.Fail + controls.Summary.Info + controls.Summary.Warn,
|
|
|
|
Failures: controls.Summary.Fail,
|
|
|
|
}
|
|
|
|
for _, g := range controls.Groups {
|
|
|
|
for _, check := range g.Checks {
|
|
|
|
jsonCheck := ""
|
|
|
|
jsonBytes, err := json.Marshal(check)
|
|
|
|
if err != nil {
|
|
|
|
jsonCheck = fmt.Sprintf("Failed to marshal test into JSON: %v. Test as text: %#v", err, check)
|
|
|
|
} else {
|
|
|
|
jsonCheck = string(jsonBytes)
|
|
|
|
}
|
|
|
|
tc := reporters.JUnitTestCase{
|
|
|
|
Name: fmt.Sprintf("%v %v", check.ID, check.Text),
|
|
|
|
ClassName: g.Text,
|
|
|
|
|
|
|
|
// Store the entire json serialization as system out so we don't lose data in cases where deeper debugging is necessary.
|
|
|
|
SystemOut: jsonCheck,
|
|
|
|
}
|
|
|
|
|
|
|
|
switch check.State {
|
|
|
|
case FAIL:
|
|
|
|
tc.FailureMessage = &reporters.JUnitFailureMessage{Message: check.Remediation}
|
|
|
|
case WARN, INFO:
|
|
|
|
// WARN and INFO are two different versions of skipped tests. Either way it would be a false positive/negative to report
|
|
|
|
// it any other way.
|
|
|
|
tc.Skipped = &reporters.JUnitSkipped{}
|
|
|
|
case PASS:
|
|
|
|
default:
|
|
|
|
glog.Warningf("Unrecognized state %s", check.State)
|
|
|
|
}
|
|
|
|
|
|
|
|
suite.TestCases = append(suite.TestCases, tc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
encoder := xml.NewEncoder(&b)
|
|
|
|
encoder.Indent("", " ")
|
|
|
|
err := encoder.Encode(suite)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to generate JUnit report: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2020-11-23 19:43:53 +00:00
|
|
|
// ASFF encodes the results of last run to AWS Security Finding Format(ASFF).
|
|
|
|
func (controls *Controls) ASFF() ([]*securityhub.AwsSecurityFinding, error) {
|
|
|
|
fs := []*securityhub.AwsSecurityFinding{}
|
|
|
|
a, err := getConfig("AWS_ACCOUNT")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
c, err := getConfig("CLUSTER_ARN")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
region, err := getConfig("AWS_REGION")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
arn := fmt.Sprintf(ARN, region)
|
|
|
|
|
|
|
|
ti := time.Now()
|
|
|
|
tf := ti.Format(time.RFC3339)
|
|
|
|
for _, g := range controls.Groups {
|
|
|
|
for _, check := range g.Checks {
|
|
|
|
if check.State == FAIL || check.State == WARN {
|
|
|
|
// ASFF ProductFields['Actual result'] can't be longer than 1024 characters
|
|
|
|
actualValue := check.ActualValue
|
2021-06-20 11:28:22 +00:00
|
|
|
remediation := check.Remediation
|
|
|
|
reason := check.Reason
|
|
|
|
|
2020-11-23 19:43:53 +00:00
|
|
|
if len(check.ActualValue) > 1024 {
|
|
|
|
actualValue = check.ActualValue[0:1023]
|
|
|
|
}
|
2021-06-20 11:28:22 +00:00
|
|
|
|
|
|
|
// Fix issue https://github.com/aquasecurity/kube-bench/issues/903
|
|
|
|
if len(check.Remediation) > 512 {
|
|
|
|
remediation = check.Remediation[0:511]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(check.Reason) > 1024 {
|
|
|
|
reason = check.Reason[0:1023]
|
|
|
|
}
|
|
|
|
|
2020-11-23 19:43:53 +00:00
|
|
|
f := securityhub.AwsSecurityFinding{
|
|
|
|
AwsAccountId: aws.String(a),
|
|
|
|
Confidence: aws.Int64(100),
|
|
|
|
GeneratorId: aws.String(fmt.Sprintf("%s/cis-kubernetes-benchmark/%s/%s", arn, controls.Version, check.ID)),
|
2022-03-27 06:36:24 +00:00
|
|
|
Id: aws.String(fmt.Sprintf("%s%sEKSnodeID+%s", arn, a, check.ID)),
|
2020-11-23 19:43:53 +00:00
|
|
|
CreatedAt: aws.String(tf),
|
|
|
|
Description: aws.String(check.Text),
|
|
|
|
ProductArn: aws.String(arn),
|
|
|
|
SchemaVersion: aws.String(SCHEMA),
|
|
|
|
Title: aws.String(fmt.Sprintf("%s %s", check.ID, check.Text)),
|
|
|
|
UpdatedAt: aws.String(tf),
|
|
|
|
Types: []*string{aws.String(TYPE)},
|
|
|
|
Severity: &securityhub.Severity{
|
|
|
|
Label: aws.String(securityhub.SeverityLabelHigh),
|
|
|
|
},
|
|
|
|
Remediation: &securityhub.Remediation{
|
|
|
|
Recommendation: &securityhub.Recommendation{
|
2021-06-20 11:28:22 +00:00
|
|
|
Text: aws.String(remediation),
|
2020-11-23 19:43:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
ProductFields: map[string]*string{
|
2021-06-20 11:28:22 +00:00
|
|
|
"Reason": aws.String(reason),
|
2020-11-23 19:43:53 +00:00
|
|
|
"Actual result": aws.String(actualValue),
|
|
|
|
"Expected result": aws.String(check.ExpectedResult),
|
|
|
|
"Section": aws.String(fmt.Sprintf("%s %s", controls.ID, controls.Text)),
|
|
|
|
"Subsection": aws.String(fmt.Sprintf("%s %s", g.ID, g.Text)),
|
|
|
|
},
|
|
|
|
Resources: []*securityhub.Resource{
|
|
|
|
{
|
|
|
|
Id: aws.String(c),
|
|
|
|
Type: aws.String(TYPE),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
fs = append(fs, &f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fs, nil
|
|
|
|
}
|
|
|
|
func getConfig(name string) (string, error) {
|
|
|
|
r := viper.GetString(name)
|
|
|
|
if len(r) == 0 {
|
|
|
|
return "", fmt.Errorf("%s not set", name)
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|
2019-04-29 10:17:06 +00:00
|
|
|
func summarize(controls *Controls, state State) {
|
|
|
|
switch state {
|
2017-05-26 09:25:29 +00:00
|
|
|
case PASS:
|
|
|
|
controls.Summary.Pass++
|
|
|
|
case FAIL:
|
|
|
|
controls.Summary.Fail++
|
|
|
|
case WARN:
|
|
|
|
controls.Summary.Warn++
|
2019-01-29 14:33:58 +00:00
|
|
|
case INFO:
|
|
|
|
controls.Summary.Info++
|
2019-05-01 19:43:06 +00:00
|
|
|
default:
|
|
|
|
glog.Warningf("Unrecognized state %s", state)
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-29 16:31:55 +00:00
|
|
|
|
2019-04-29 10:17:06 +00:00
|
|
|
func summarizeGroup(group *Group, state State) {
|
|
|
|
switch state {
|
2017-11-29 16:31:55 +00:00
|
|
|
case PASS:
|
|
|
|
group.Pass++
|
|
|
|
case FAIL:
|
|
|
|
group.Fail++
|
|
|
|
case WARN:
|
|
|
|
group.Warn++
|
2019-01-29 14:33:58 +00:00
|
|
|
case INFO:
|
|
|
|
group.Info++
|
2019-05-01 19:43:06 +00:00
|
|
|
default:
|
|
|
|
glog.Warningf("Unrecognized state %s", state)
|
2017-11-29 16:31:55 +00:00
|
|
|
}
|
|
|
|
}
|