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-03-11 18:05:33 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2017-05-26 09:25:29 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2019-03-11 18:05:33 +00:00
|
|
|
|
|
|
|
yaml "gopkg.in/yaml.v2"
|
|
|
|
"k8s.io/client-go/util/jsonpath"
|
2017-05-26 09:25:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// test:
|
|
|
|
// flag: OPTION
|
|
|
|
// set: (true|false)
|
|
|
|
// compare:
|
|
|
|
// op: (eq|gt|gte|lt|lte|has)
|
|
|
|
// value: val
|
|
|
|
|
|
|
|
type binOp string
|
|
|
|
|
|
|
|
const (
|
2019-07-26 18:11:59 +00:00
|
|
|
and binOp = "and"
|
|
|
|
or = "or"
|
|
|
|
defaultArraySeparator = ","
|
2017-05-26 09:25:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type testItem struct {
|
2019-04-11 16:09:33 +00:00
|
|
|
Flag string
|
|
|
|
Path string
|
|
|
|
Output string
|
|
|
|
Value string
|
|
|
|
Set bool
|
|
|
|
Compare compare
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type compare struct {
|
|
|
|
Op string
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
2018-07-30 12:16:28 +00:00
|
|
|
type testOutput struct {
|
2019-07-12 06:09:27 +00:00
|
|
|
testResult bool
|
|
|
|
actualResult string
|
2019-05-06 09:30:11 +00:00
|
|
|
ExpectedResult string
|
2018-07-30 12:16:28 +00:00
|
|
|
}
|
|
|
|
|
2019-03-11 18:05:33 +00:00
|
|
|
func failTestItem(s string) *testOutput {
|
|
|
|
return &testOutput{testResult: false, actualResult: s}
|
|
|
|
}
|
|
|
|
|
2018-07-30 12:16:28 +00:00
|
|
|
func (t *testItem) execute(s string) *testOutput {
|
|
|
|
result := &testOutput{}
|
2019-03-11 18:05:33 +00:00
|
|
|
var match bool
|
|
|
|
var flagVal string
|
|
|
|
|
|
|
|
if t.Flag != "" {
|
|
|
|
// Flag comparison: check if the flag is present in the input
|
|
|
|
match = strings.Contains(s, t.Flag)
|
|
|
|
} else {
|
2019-04-11 16:09:33 +00:00
|
|
|
// Path != "" - we don't know whether it's YAML or JSON but
|
|
|
|
// we can just try one then the other
|
2019-03-11 18:05:33 +00:00
|
|
|
var jsonInterface interface{}
|
|
|
|
|
2019-04-11 16:09:33 +00:00
|
|
|
if t.Path != "" {
|
2019-07-12 06:09:27 +00:00
|
|
|
err := unmarshal(s, &jsonInterface)
|
2019-03-11 18:05:33 +00:00
|
|
|
if err != nil {
|
2019-07-12 06:09:27 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "failed to load YAML or JSON from provided input \"%s\": %v\n", s, err)
|
|
|
|
return failTestItem("failed to load YAML or JSON")
|
2019-03-11 18:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-07-12 06:09:27 +00:00
|
|
|
jsonpathResult, err := executeJSONPath(t.Path, &jsonInterface)
|
2019-03-11 18:05:33 +00:00
|
|
|
if err != nil {
|
2019-07-12 06:09:27 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "unable to parse path expression \"%s\": %v\n", t.Path, err)
|
2019-03-11 18:05:33 +00:00
|
|
|
return failTestItem("error executing path expression")
|
|
|
|
}
|
|
|
|
match = (jsonpathResult != "")
|
|
|
|
flagVal = jsonpathResult
|
|
|
|
}
|
2017-05-26 09:25:29 +00:00
|
|
|
|
|
|
|
if t.Set {
|
|
|
|
isset := match
|
|
|
|
|
|
|
|
if isset && t.Compare.Op != "" {
|
2019-03-11 18:05:33 +00:00
|
|
|
if t.Flag != "" {
|
|
|
|
// Expects flags in the form;
|
|
|
|
// --flag=somevalue
|
2019-04-11 16:09:33 +00:00
|
|
|
// flag: somevalue
|
2019-03-11 18:05:33 +00:00
|
|
|
// --flag
|
|
|
|
// somevalue
|
2019-04-11 09:03:07 +00:00
|
|
|
pttn := `(` + t.Flag + `)(=|: *)*([^\s]*) *`
|
2019-03-11 18:05:33 +00:00
|
|
|
flagRe := regexp.MustCompile(pttn)
|
|
|
|
vals := flagRe.FindStringSubmatch(s)
|
|
|
|
|
|
|
|
if len(vals) > 0 {
|
|
|
|
if vals[3] != "" {
|
|
|
|
flagVal = vals[3]
|
|
|
|
} else {
|
|
|
|
flagVal = vals[1]
|
|
|
|
}
|
2017-08-12 12:29:21 +00:00
|
|
|
} else {
|
2019-03-11 18:05:33 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "invalid flag in testitem definition")
|
|
|
|
os.Exit(1)
|
2017-08-12 12:29:21 +00:00
|
|
|
}
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
|
2019-07-17 14:08:11 +00:00
|
|
|
result.ExpectedResult, result.testResult = compareOp(t.Compare.Op, flagVal, t.Compare.Value)
|
2017-05-26 09:25:29 +00:00
|
|
|
} else {
|
2019-05-06 09:30:11 +00:00
|
|
|
result.ExpectedResult = fmt.Sprintf("'%s' is present", t.Flag)
|
2018-07-30 12:16:28 +00:00
|
|
|
result.testResult = isset
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-05-06 09:30:11 +00:00
|
|
|
result.ExpectedResult = fmt.Sprintf("'%s' is not present", t.Flag)
|
2017-05-26 09:25:29 +00:00
|
|
|
notset := !match
|
2018-07-30 12:16:28 +00:00
|
|
|
result.testResult = notset
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
2018-07-30 12:16:28 +00:00
|
|
|
return result
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
|
2019-07-17 14:08:11 +00:00
|
|
|
func compareOp(tCompareOp string, flagVal string, tCompareValue string) (string, bool) {
|
|
|
|
|
|
|
|
expectedResultPattern := ""
|
|
|
|
testResult := false
|
|
|
|
|
|
|
|
switch tCompareOp {
|
|
|
|
case "eq":
|
|
|
|
expectedResultPattern = "'%s' is equal to '%s'"
|
|
|
|
value := strings.ToLower(flagVal)
|
|
|
|
// Do case insensitive comparaison for booleans ...
|
|
|
|
if value == "false" || value == "true" {
|
|
|
|
testResult = value == tCompareValue
|
|
|
|
} else {
|
|
|
|
testResult = flagVal == tCompareValue
|
|
|
|
}
|
|
|
|
|
|
|
|
case "noteq":
|
|
|
|
expectedResultPattern = "'%s' is not equal to '%s'"
|
|
|
|
value := strings.ToLower(flagVal)
|
|
|
|
// Do case insensitive comparaison for booleans ...
|
|
|
|
if value == "false" || value == "true" {
|
|
|
|
testResult = !(value == tCompareValue)
|
|
|
|
} else {
|
|
|
|
testResult = !(flagVal == tCompareValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
case "gt":
|
|
|
|
expectedResultPattern = "%s is greater than %s"
|
|
|
|
a, b := toNumeric(flagVal, tCompareValue)
|
|
|
|
testResult = a > b
|
|
|
|
|
|
|
|
case "gte":
|
|
|
|
expectedResultPattern = "%s is greater or equal to %s"
|
|
|
|
a, b := toNumeric(flagVal, tCompareValue)
|
|
|
|
testResult = a >= b
|
|
|
|
|
|
|
|
case "lt":
|
|
|
|
expectedResultPattern = "%s is lower than %s"
|
|
|
|
a, b := toNumeric(flagVal, tCompareValue)
|
|
|
|
testResult = a < b
|
|
|
|
|
|
|
|
case "lte":
|
|
|
|
expectedResultPattern = "%s is lower or equal to %s"
|
|
|
|
a, b := toNumeric(flagVal, tCompareValue)
|
|
|
|
testResult = a <= b
|
|
|
|
|
|
|
|
case "has":
|
|
|
|
expectedResultPattern = "'%s' has '%s'"
|
|
|
|
testResult = strings.Contains(flagVal, tCompareValue)
|
|
|
|
|
|
|
|
case "nothave":
|
|
|
|
expectedResultPattern = " '%s' not have '%s'"
|
|
|
|
testResult = !strings.Contains(flagVal, tCompareValue)
|
|
|
|
|
|
|
|
case "regex":
|
|
|
|
expectedResultPattern = " '%s' matched by '%s'"
|
|
|
|
opRe := regexp.MustCompile(tCompareValue)
|
|
|
|
testResult = opRe.MatchString(flagVal)
|
2019-07-26 18:11:59 +00:00
|
|
|
|
|
|
|
case "valid_elements":
|
|
|
|
expectedResultPattern = "'%s' contains valid elements from '%s'"
|
|
|
|
s := splitAndRemoveLastSeparator(flagVal, defaultArraySeparator)
|
|
|
|
target := splitAndRemoveLastSeparator(tCompareValue, defaultArraySeparator)
|
|
|
|
testResult = allElementsValid(s, target)
|
|
|
|
|
2019-07-17 14:08:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if expectedResultPattern == "" {
|
|
|
|
return expectedResultPattern, testResult
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf(expectedResultPattern, flagVal, tCompareValue), testResult
|
|
|
|
}
|
|
|
|
|
2019-07-12 06:09:27 +00:00
|
|
|
func unmarshal(s string, jsonInterface *interface{}) error {
|
|
|
|
data := []byte(s)
|
|
|
|
err := json.Unmarshal(data, jsonInterface)
|
|
|
|
if err != nil {
|
|
|
|
err := yaml.Unmarshal(data, jsonInterface)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func executeJSONPath(path string, jsonInterface interface{}) (string, error) {
|
|
|
|
j := jsonpath.New("jsonpath")
|
|
|
|
j.AllowMissingKeys(true)
|
|
|
|
err := j.Parse(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
err = j.Execute(buf, jsonInterface)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
jsonpathResult := fmt.Sprintf("%s", buf)
|
|
|
|
return jsonpathResult, nil
|
|
|
|
}
|
|
|
|
|
2019-07-26 18:11:59 +00:00
|
|
|
func allElementsValid(s, t []string) bool {
|
|
|
|
sourceEmpty := s == nil || len(s) == 0
|
|
|
|
targetEmpty := t == nil || len(t) == 0
|
|
|
|
|
|
|
|
if sourceEmpty && targetEmpty {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// XOR comparison -
|
|
|
|
// if either value is empty and the other is not empty,
|
|
|
|
// not all elements are valid
|
|
|
|
if (sourceEmpty || targetEmpty) && !(sourceEmpty && targetEmpty) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, sv := range s {
|
|
|
|
found := false
|
|
|
|
for _, tv := range t {
|
|
|
|
if sv == tv {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func splitAndRemoveLastSeparator(s, sep string) []string {
|
|
|
|
cleanS := strings.TrimRight(strings.TrimSpace(s), sep)
|
|
|
|
if len(cleanS) == 0 {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
ts := strings.Split(cleanS, sep)
|
|
|
|
for i := range ts {
|
|
|
|
ts[i] = strings.TrimSpace(ts[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
return ts
|
|
|
|
}
|
|
|
|
|
2017-05-26 09:25:29 +00:00
|
|
|
type tests struct {
|
|
|
|
TestItems []*testItem `yaml:"test_items"`
|
|
|
|
BinOp binOp `yaml:"bin_op"`
|
|
|
|
}
|
|
|
|
|
2018-07-30 12:16:28 +00:00
|
|
|
func (ts *tests) execute(s string) *testOutput {
|
|
|
|
finalOutput := &testOutput{}
|
|
|
|
|
2019-01-24 15:40:43 +00:00
|
|
|
// If no tests are defined return with empty finalOutput.
|
2019-02-18 08:46:26 +00:00
|
|
|
// This may be the case for checks of type: "skip".
|
2019-01-24 15:40:43 +00:00
|
|
|
if ts == nil {
|
|
|
|
return finalOutput
|
|
|
|
}
|
|
|
|
|
2018-07-30 12:16:28 +00:00
|
|
|
res := make([]testOutput, len(ts.TestItems))
|
|
|
|
if len(res) == 0 {
|
|
|
|
return finalOutput
|
|
|
|
}
|
2017-05-26 09:25:29 +00:00
|
|
|
|
2019-05-21 11:10:11 +00:00
|
|
|
expectedResultArr := make([]string, len(res))
|
|
|
|
|
2017-05-26 09:25:29 +00:00
|
|
|
for i, t := range ts.TestItems {
|
2018-07-30 12:16:28 +00:00
|
|
|
res[i] = *(t.execute(s))
|
2019-05-21 11:10:11 +00:00
|
|
|
expectedResultArr[i] = res[i].ExpectedResult
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 12:16:28 +00:00
|
|
|
var result bool
|
2017-05-26 09:25:29 +00:00
|
|
|
// If no binary operation is specified, default to AND
|
|
|
|
switch ts.BinOp {
|
|
|
|
default:
|
|
|
|
fmt.Fprintf(os.Stderr, "unknown binary operator for tests %s\n", ts.BinOp)
|
|
|
|
os.Exit(1)
|
|
|
|
case and, "":
|
|
|
|
result = true
|
|
|
|
for i := range res {
|
2018-07-30 12:16:28 +00:00
|
|
|
result = result && res[i].testResult
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
2019-05-21 11:10:11 +00:00
|
|
|
// Generate an AND expected result
|
|
|
|
finalOutput.ExpectedResult = strings.Join(expectedResultArr, " AND ")
|
|
|
|
|
2017-05-26 09:25:29 +00:00
|
|
|
case or:
|
|
|
|
result = false
|
|
|
|
for i := range res {
|
2018-07-30 12:16:28 +00:00
|
|
|
result = result || res[i].testResult
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
2019-05-21 11:10:11 +00:00
|
|
|
// Generate an OR expected result
|
|
|
|
finalOutput.ExpectedResult = strings.Join(expectedResultArr, " OR ")
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 12:16:28 +00:00
|
|
|
finalOutput.testResult = result
|
|
|
|
finalOutput.actualResult = res[0].actualResult
|
|
|
|
|
2019-05-05 07:35:55 +00:00
|
|
|
if finalOutput.actualResult == "" {
|
|
|
|
finalOutput.actualResult = s
|
|
|
|
}
|
|
|
|
|
2018-07-30 12:16:28 +00:00
|
|
|
return finalOutput
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func toNumeric(a, b string) (c, d int) {
|
|
|
|
var err error
|
|
|
|
c, err = strconv.Atoi(a)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error converting %s: %s\n", a, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
d, err = strconv.Atoi(b)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error converting %s: %s\n", b, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, d
|
|
|
|
}
|