mirror of
https://github.com/aquasecurity/kube-bench.git
synced 2024-11-29 11:28:14 +00:00
Support actual result in json output.
This commit adds the actual value of the result of the value which was returned by the test.
This commit is contained in:
parent
b1e41d345f
commit
e9076233dd
@ -70,6 +70,7 @@ type Check struct {
|
|||||||
Remediation string `json:"-"`
|
Remediation string `json:"-"`
|
||||||
TestInfo []string `json:"test_info"`
|
TestInfo []string `json:"test_info"`
|
||||||
State `json:"status"`
|
State `json:"status"`
|
||||||
|
ActualValue string `json:"actual_value"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run executes the audit commands specified in a check and outputs
|
// Run executes the audit commands specified in a check and outputs
|
||||||
@ -157,16 +158,26 @@ func (c *Check) Run() {
|
|||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
|
|
||||||
if errmsgs != "" {
|
finalOutput := c.Tests.execute(out.String())
|
||||||
glog.V(2).Info(errmsgs)
|
if finalOutput != nil {
|
||||||
}
|
c.ActualValue = finalOutput.actualResult
|
||||||
|
if finalOutput.testResult {
|
||||||
res := c.Tests.execute(out.String())
|
|
||||||
if res {
|
|
||||||
c.State = PASS
|
c.State = PASS
|
||||||
} else {
|
} else {
|
||||||
c.State = FAIL
|
c.State = FAIL
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
errmsgs += handleError(
|
||||||
|
fmt.Errorf("final output is nil"),
|
||||||
|
fmt.Sprintf("failed to run: %s\n",
|
||||||
|
c.Audit,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if errmsgs != "" {
|
||||||
|
glog.V(2).Info(errmsgs)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// textToCommand transforms an input text representation of commands to be
|
// textToCommand transforms an input text representation of commands to be
|
||||||
|
@ -17,7 +17,7 @@ groups:
|
|||||||
- id: 1
|
- id: 1
|
||||||
text: "flag is not set"
|
text: "flag is not set"
|
||||||
tests:
|
tests:
|
||||||
test_item:
|
test_items:
|
||||||
- flag: "--basic-auth"
|
- flag: "--basic-auth"
|
||||||
set: false
|
set: false
|
||||||
|
|
||||||
|
@ -49,8 +49,13 @@ type compare struct {
|
|||||||
Value string
|
Value string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *testItem) execute(s string) (result bool) {
|
type testOutput struct {
|
||||||
result = false
|
testResult bool
|
||||||
|
actualResult string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *testItem) execute(s string) *testOutput {
|
||||||
|
result := &testOutput{}
|
||||||
match := strings.Contains(s, t.Flag)
|
match := strings.Contains(s, t.Flag)
|
||||||
|
|
||||||
if t.Set {
|
if t.Set {
|
||||||
@ -78,57 +83,57 @@ func (t *testItem) execute(s string) (result bool) {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result.actualResult = strings.ToLower(flagVal)
|
||||||
switch t.Compare.Op {
|
switch t.Compare.Op {
|
||||||
case "eq":
|
case "eq":
|
||||||
value := strings.ToLower(flagVal)
|
value := strings.ToLower(flagVal)
|
||||||
// Do case insensitive comparaison for booleans ...
|
// Do case insensitive comparaison for booleans ...
|
||||||
if value == "false" || value == "true" {
|
if value == "false" || value == "true" {
|
||||||
result = value == t.Compare.Value
|
result.testResult = value == t.Compare.Value
|
||||||
} else {
|
} else {
|
||||||
result = flagVal == t.Compare.Value
|
result.testResult = flagVal == t.Compare.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
case "noteq":
|
case "noteq":
|
||||||
value := strings.ToLower(flagVal)
|
value := strings.ToLower(flagVal)
|
||||||
// Do case insensitive comparaison for booleans ...
|
// Do case insensitive comparaison for booleans ...
|
||||||
if value == "false" || value == "true" {
|
if value == "false" || value == "true" {
|
||||||
result = !(value == t.Compare.Value)
|
result.testResult = !(value == t.Compare.Value)
|
||||||
} else {
|
} else {
|
||||||
result = !(flagVal == t.Compare.Value)
|
result.testResult = !(flagVal == t.Compare.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
case "gt":
|
case "gt":
|
||||||
a, b := toNumeric(flagVal, t.Compare.Value)
|
a, b := toNumeric(flagVal, t.Compare.Value)
|
||||||
result = a > b
|
result.testResult = a > b
|
||||||
|
|
||||||
case "gte":
|
case "gte":
|
||||||
a, b := toNumeric(flagVal, t.Compare.Value)
|
a, b := toNumeric(flagVal, t.Compare.Value)
|
||||||
result = a >= b
|
result.testResult = a >= b
|
||||||
|
|
||||||
case "lt":
|
case "lt":
|
||||||
a, b := toNumeric(flagVal, t.Compare.Value)
|
a, b := toNumeric(flagVal, t.Compare.Value)
|
||||||
result = a < b
|
result.testResult = a < b
|
||||||
|
|
||||||
case "lte":
|
case "lte":
|
||||||
a, b := toNumeric(flagVal, t.Compare.Value)
|
a, b := toNumeric(flagVal, t.Compare.Value)
|
||||||
result = a <= b
|
result.testResult = a <= b
|
||||||
|
|
||||||
case "has":
|
case "has":
|
||||||
result = strings.Contains(flagVal, t.Compare.Value)
|
result.testResult = strings.Contains(flagVal, t.Compare.Value)
|
||||||
|
|
||||||
case "nothave":
|
case "nothave":
|
||||||
result = !strings.Contains(flagVal, t.Compare.Value)
|
result.testResult = !strings.Contains(flagVal, t.Compare.Value)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
result = isset
|
result.testResult = isset
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
notset := !match
|
notset := !match
|
||||||
result = notset
|
result.testResult = notset
|
||||||
}
|
}
|
||||||
|
return result
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type tests struct {
|
type tests struct {
|
||||||
@ -136,13 +141,19 @@ type tests struct {
|
|||||||
BinOp binOp `yaml:"bin_op"`
|
BinOp binOp `yaml:"bin_op"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ts *tests) execute(s string) (result bool) {
|
func (ts *tests) execute(s string) *testOutput {
|
||||||
res := make([]bool, len(ts.TestItems))
|
finalOutput := &testOutput{}
|
||||||
|
|
||||||
for i, t := range ts.TestItems {
|
res := make([]testOutput, len(ts.TestItems))
|
||||||
res[i] = t.execute(s)
|
if len(res) == 0 {
|
||||||
|
return finalOutput
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for i, t := range ts.TestItems {
|
||||||
|
res[i] = *(t.execute(s))
|
||||||
|
}
|
||||||
|
|
||||||
|
var result bool
|
||||||
// If no binary operation is specified, default to AND
|
// If no binary operation is specified, default to AND
|
||||||
switch ts.BinOp {
|
switch ts.BinOp {
|
||||||
default:
|
default:
|
||||||
@ -151,16 +162,19 @@ func (ts *tests) execute(s string) (result bool) {
|
|||||||
case and, "":
|
case and, "":
|
||||||
result = true
|
result = true
|
||||||
for i := range res {
|
for i := range res {
|
||||||
result = result && res[i]
|
result = result && res[i].testResult
|
||||||
}
|
}
|
||||||
case or:
|
case or:
|
||||||
result = false
|
result = false
|
||||||
for i := range res {
|
for i := range res {
|
||||||
result = result || res[i]
|
result = result || res[i].testResult
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
finalOutput.testResult = result
|
||||||
|
finalOutput.actualResult = res[0].actualResult
|
||||||
|
|
||||||
|
return finalOutput
|
||||||
}
|
}
|
||||||
|
|
||||||
func toNumeric(a, b string) (c, d int) {
|
func toNumeric(a, b string) (c, d int) {
|
||||||
|
@ -113,7 +113,7 @@ func TestTestExecute(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
res := c.Tests.execute(c.str)
|
res := c.Tests.execute(c.str).testResult
|
||||||
if !res {
|
if !res {
|
||||||
t.Errorf("%s, expected:%v, got:%v\n", c.Text, true, res)
|
t.Errorf("%s, expected:%v, got:%v\n", c.Text, true, res)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user