mirror of
https://github.com/aquasecurity/kube-bench.git
synced 2025-01-03 04:10:55 +00:00
Allow for multiple words in executable names
This commit is contained in:
parent
34f8b8e980
commit
6b9f117f87
@ -18,7 +18,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/aquasecurity/kube-bench/check"
|
"github.com/aquasecurity/kube-bench/check"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
@ -97,26 +96,26 @@ func runChecks(t check.NodeType) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Variable substitutions. Replace all occurrences of variables in controls files.
|
// Variable substitutions. Replace all occurrences of variables in controls files.
|
||||||
s := strings.Replace(string(in), "$apiserverbin", apiserverBin, -1)
|
s := multiWordReplace(string(in), "$apiserverbin", apiserverBin)
|
||||||
s = strings.Replace(s, "$apiserverconf", apiserverConf, -1)
|
s = multiWordReplace(s, "$apiserverconf", apiserverConf)
|
||||||
s = strings.Replace(s, "$schedulerbin", schedulerBin, -1)
|
s = multiWordReplace(s, "$schedulerbin", schedulerBin)
|
||||||
s = strings.Replace(s, "$schedulerconf", schedulerConf, -1)
|
s = multiWordReplace(s, "$schedulerconf", schedulerConf)
|
||||||
s = strings.Replace(s, "$controllermanagerbin", controllerManagerBin, -1)
|
s = multiWordReplace(s, "$controllermanagerbin", controllerManagerBin)
|
||||||
s = strings.Replace(s, "$controllermanagerconf", controllerManagerConf, -1)
|
s = multiWordReplace(s, "$controllermanagerconf", controllerManagerConf)
|
||||||
s = strings.Replace(s, "$config", config, -1)
|
s = multiWordReplace(s, "$config", config)
|
||||||
|
|
||||||
s = strings.Replace(s, "$etcdbin", etcdBin, -1)
|
s = multiWordReplace(s, "$etcdbin", etcdBin)
|
||||||
s = strings.Replace(s, "$etcdconf", etcdConf, -1)
|
s = multiWordReplace(s, "$etcdconf", etcdConf)
|
||||||
s = strings.Replace(s, "$flanneldbin", flanneldBin, -1)
|
s = multiWordReplace(s, "$flanneldbin", flanneldBin)
|
||||||
s = strings.Replace(s, "$flanneldconf", flanneldConf, -1)
|
s = multiWordReplace(s, "$flanneldconf", flanneldConf)
|
||||||
|
|
||||||
s = strings.Replace(s, "$kubeletbin", kubeletBin, -1)
|
s = multiWordReplace(s, "$kubeletbin", kubeletBin)
|
||||||
s = strings.Replace(s, "$kubeletconf", kubeletConf, -1)
|
s = multiWordReplace(s, "$kubeletconf", kubeletConf)
|
||||||
s = strings.Replace(s, "$proxybin", proxyBin, -1)
|
s = multiWordReplace(s, "$proxybin", proxyBin)
|
||||||
s = strings.Replace(s, "$proxyconf", proxyConf, -1)
|
s = multiWordReplace(s, "$proxyconf", proxyConf)
|
||||||
|
|
||||||
s = strings.Replace(s, "$fedapiserverbin", fedApiserverBin, -1)
|
s = multiWordReplace(s, "$fedapiserverbin", fedApiserverBin)
|
||||||
s = strings.Replace(s, "$fedcontrollermanagerbin", fedControllerManagerBin, -1)
|
s = multiWordReplace(s, "$fedcontrollermanagerbin", fedControllerManagerBin)
|
||||||
|
|
||||||
controls, err := check.NewControls(t, []byte(s))
|
controls, err := check.NewControls(t, []byte(s))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -150,3 +150,12 @@ func versionMatch(r *regexp.Regexp, s string) string {
|
|||||||
}
|
}
|
||||||
return match[1]
|
return match[1]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func multiWordReplace(s string, subname string, sub string) string {
|
||||||
|
f := strings.Fields(sub)
|
||||||
|
if len(f) > 1 {
|
||||||
|
sub = "'" + sub + "'"
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Replace(s, subname, sub, -1)
|
||||||
|
}
|
||||||
|
@ -107,3 +107,25 @@ func TestVerifyBin(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMultiWordReplace(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
input string
|
||||||
|
sub string
|
||||||
|
subname string
|
||||||
|
output string
|
||||||
|
}{
|
||||||
|
{input: "Here's a file with no substitutions", sub: "blah", subname: "blah", output: "Here's a file with no substitutions"},
|
||||||
|
{input: "Here's a file with a substitution", sub: "blah", subname: "substitution", output: "Here's a file with a blah"},
|
||||||
|
{input: "Here's a file with multi-word substitutions", sub: "multi word", subname: "multi-word", output: "Here's a file with 'multi word' substitutions"},
|
||||||
|
{input: "Here's a file with several several substitutions several", sub: "blah", subname: "several", output: "Here's a file with blah blah substitutions blah"},
|
||||||
|
}
|
||||||
|
for id, c := range cases {
|
||||||
|
t.Run(strconv.Itoa(id), func(t *testing.T) {
|
||||||
|
s := multiWordReplace(c.input, c.subname, c.sub)
|
||||||
|
if s != c.output {
|
||||||
|
t.Fatalf("Expected %s got %s", c.output, s)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user