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 (
|
|
|
|
"io/ioutil"
|
2017-08-12 18:54:33 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2017-05-26 09:25:29 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
in []byte
|
|
|
|
controls *Controls
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
var err error
|
|
|
|
in, err = ioutil.ReadFile("data")
|
|
|
|
if err != nil {
|
|
|
|
panic("Failed reading test data: " + err.Error())
|
|
|
|
}
|
2017-08-12 18:54:33 +00:00
|
|
|
|
|
|
|
// substitute variables in data file
|
|
|
|
user := os.Getenv("USER")
|
|
|
|
s := strings.Replace(string(in), "$user", user, -1)
|
|
|
|
|
|
|
|
controls, err = NewControls(MASTER, []byte(s))
|
|
|
|
// controls, err = NewControls(MASTER, in)
|
2017-06-23 11:04:46 +00:00
|
|
|
if err != nil {
|
|
|
|
panic("Failed creating test controls: " + err.Error())
|
|
|
|
}
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTestExecute(t *testing.T) {
|
2017-08-12 18:54:33 +00:00
|
|
|
|
2017-05-26 09:25:29 +00:00
|
|
|
cases := []struct {
|
2017-08-12 18:54:33 +00:00
|
|
|
*Check
|
|
|
|
str string
|
2017-05-26 09:25:29 +00:00
|
|
|
}{
|
|
|
|
{
|
2017-08-12 18:54:33 +00:00
|
|
|
controls.Groups[0].Checks[0],
|
2017-05-26 09:25:29 +00:00
|
|
|
"2:45 ../kubernetes/kube-apiserver --allow-privileged=false --option1=20,30,40",
|
|
|
|
},
|
|
|
|
{
|
2017-08-12 18:54:33 +00:00
|
|
|
controls.Groups[0].Checks[1],
|
2017-05-26 09:25:29 +00:00
|
|
|
"2:45 ../kubernetes/kube-apiserver --allow-privileged=false",
|
|
|
|
},
|
|
|
|
{
|
2017-08-12 18:54:33 +00:00
|
|
|
controls.Groups[0].Checks[2],
|
2017-05-26 09:25:29 +00:00
|
|
|
"niinai 13617 2635 99 19:26 pts/20 00:03:08 ./kube-apiserver --insecure-port=0 --anonymous-auth",
|
|
|
|
},
|
|
|
|
{
|
2017-08-12 18:54:33 +00:00
|
|
|
controls.Groups[0].Checks[3],
|
2017-05-26 09:25:29 +00:00
|
|
|
"2:45 ../kubernetes/kube-apiserver --secure-port=0 --audit-log-maxage=40 --option",
|
|
|
|
},
|
|
|
|
{
|
2017-08-12 18:54:33 +00:00
|
|
|
controls.Groups[0].Checks[4],
|
2017-05-26 09:25:29 +00:00
|
|
|
"2:45 ../kubernetes/kube-apiserver --max-backlog=20 --secure-port=0 --audit-log-maxage=40 --option",
|
|
|
|
},
|
|
|
|
{
|
2017-08-12 18:54:33 +00:00
|
|
|
controls.Groups[0].Checks[5],
|
2017-05-26 09:25:29 +00:00
|
|
|
"2:45 ../kubernetes/kube-apiserver --option --admission-control=WebHook,RBAC ---audit-log-maxage=40",
|
|
|
|
},
|
|
|
|
{
|
2017-08-12 18:54:33 +00:00
|
|
|
controls.Groups[0].Checks[6],
|
2017-05-26 09:25:29 +00:00
|
|
|
"2:45 .. --kubelet-clientkey=foo --kubelet-client-certificate=bar --admission-control=Webhook,RBAC",
|
|
|
|
},
|
|
|
|
{
|
2017-08-12 18:54:33 +00:00
|
|
|
controls.Groups[0].Checks[7],
|
2017-05-26 09:25:29 +00:00
|
|
|
"2:45 .. --secure-port=0 --kubelet-client-certificate=bar --admission-control=Webhook,RBAC",
|
|
|
|
},
|
|
|
|
{
|
2017-08-12 18:54:33 +00:00
|
|
|
controls.Groups[0].Checks[8],
|
2017-05-26 09:25:29 +00:00
|
|
|
"644",
|
|
|
|
},
|
2017-08-15 17:34:07 +00:00
|
|
|
{
|
|
|
|
controls.Groups[0].Checks[9],
|
|
|
|
"640",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
controls.Groups[0].Checks[9],
|
|
|
|
"600",
|
|
|
|
},
|
2017-09-13 14:32:33 +00:00
|
|
|
{
|
|
|
|
controls.Groups[0].Checks[10],
|
|
|
|
"2:45 ../kubernetes/kube-apiserver --option --admission-control=WebHook,RBAC ---audit-log-maxage=40",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
controls.Groups[0].Checks[11],
|
|
|
|
"2:45 ../kubernetes/kube-apiserver --option --admission-control=WebHook,RBAC ---audit-log-maxage=40",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
controls.Groups[0].Checks[12],
|
|
|
|
"2:45 ../kubernetes/kube-apiserver --option --admission-control=WebHook,Something,RBAC ---audit-log-maxage=40",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
controls.Groups[0].Checks[13],
|
|
|
|
"2:45 ../kubernetes/kube-apiserver --option --admission-control=Something ---audit-log-maxage=40",
|
|
|
|
},
|
2019-03-11 18:05:33 +00:00
|
|
|
{
|
2019-04-10 22:47:26 +00:00
|
|
|
// check for ':' as argument-value separator, with space between arg and val
|
2019-03-11 18:05:33 +00:00
|
|
|
controls.Groups[0].Checks[14],
|
2019-04-10 22:47:26 +00:00
|
|
|
"2:45 kube-apiserver some-arg: some-val --admission-control=Something ---audit-log-maxage=40",
|
2019-03-11 18:05:33 +00:00
|
|
|
},
|
|
|
|
{
|
2019-04-10 22:47:26 +00:00
|
|
|
// check for ':' as argument-value separator, with no space between arg and val
|
|
|
|
controls.Groups[0].Checks[14],
|
|
|
|
"2:45 kube-apiserver some-arg:some-val --admission-control=Something ---audit-log-maxage=40",
|
2019-04-11 09:21:19 +00:00
|
|
|
},
|
|
|
|
{
|
2019-03-11 18:05:33 +00:00
|
|
|
controls.Groups[0].Checks[15],
|
2019-04-11 09:03:07 +00:00
|
|
|
"{\"readOnlyPort\": 15000}",
|
2019-03-11 18:05:33 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
controls.Groups[0].Checks[16],
|
2019-04-11 09:03:07 +00:00
|
|
|
"{\"stringValue\": \"WebHook,Something,RBAC\"}",
|
2019-03-11 18:05:33 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
controls.Groups[0].Checks[17],
|
2019-04-11 09:03:07 +00:00
|
|
|
"{\"trueValue\": true}",
|
2019-03-11 18:05:33 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
controls.Groups[0].Checks[18],
|
2019-04-11 09:03:07 +00:00
|
|
|
"{\"readOnlyPort\": 15000}",
|
2019-03-11 18:05:33 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
controls.Groups[0].Checks[19],
|
2019-04-11 09:03:07 +00:00
|
|
|
"{\"authentication\": { \"anonymous\": {\"enabled\": false}}}",
|
2019-03-11 18:05:33 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
controls.Groups[0].Checks[20],
|
|
|
|
"readOnlyPort: 15000",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
controls.Groups[0].Checks[21],
|
2019-04-11 09:03:07 +00:00
|
|
|
"readOnlyPort: 15000",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
controls.Groups[0].Checks[22],
|
2019-03-11 18:05:33 +00:00
|
|
|
"authentication:\n anonymous:\n enabled: false",
|
|
|
|
},
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2018-07-30 12:16:28 +00:00
|
|
|
res := c.Tests.execute(c.str).testResult
|
2017-05-26 09:25:29 +00:00
|
|
|
if !res {
|
2017-08-12 18:54:33 +00:00
|
|
|
t.Errorf("%s, expected:%v, got:%v\n", c.Text, true, res)
|
2017-05-26 09:25:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-11 18:05:33 +00:00
|
|
|
|
|
|
|
func TestTestExecuteExceptions(t *testing.T) {
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
*Check
|
|
|
|
str string
|
|
|
|
}{
|
|
|
|
{
|
2019-04-11 09:21:19 +00:00
|
|
|
controls.Groups[0].Checks[23],
|
2019-03-11 18:05:33 +00:00
|
|
|
"this is not valid json {} at all",
|
|
|
|
},
|
|
|
|
{
|
2019-04-11 09:21:19 +00:00
|
|
|
controls.Groups[0].Checks[24],
|
2019-03-11 18:05:33 +00:00
|
|
|
"{\"key\": \"value\"}",
|
|
|
|
},
|
|
|
|
{
|
2019-04-11 09:21:19 +00:00
|
|
|
controls.Groups[0].Checks[25],
|
2019-03-11 18:05:33 +00:00
|
|
|
"broken } yaml\nenabled: true",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
res := c.Tests.execute(c.str).testResult
|
|
|
|
if res {
|
|
|
|
t.Errorf("%s, expected:%v, got:%v\n", c.Text, false, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|