我编写了一个 golang 程序,它将规则附加到文件中,如下所述所需格式:
customRules:
custom-rules.yaml: |-
- rule: Pod Created in Kube Namespace
append: true
condition: and (k8s_audit_never_true)
source: k8s_audit
- rule: Create files below dev
append: true
condition: and (never_true)
source: syscall
我写了一个 go 程序,它没有采用上述格式,我无法得到我所缺少的东西。
package main
import (
"fmt"
"io/ioutil"
"log"
"gopkg.in/yaml.v2"
)
type AutoGenerated struct {
CustomRules CustomRules `yaml:"customRules"`
}
type CustomRulesYaml struct {
Rule string `yaml:"rule"`
Append bool `yaml:"append"`
Condition string `yaml:"condition"`
Source string `yaml:"source"`
}
type CustomRules struct {
CustomRulesYaml []CustomRulesYaml `yaml:"custom-rules.yaml"`
}
func main() {
// yfile, err := ioutil.ReadFile("/home/revaa/falco/custom_rules.yaml")
// if err != nil {
// log.Fatal(err)
// }
c1 := CustomRulesYaml{"K8s serviceaccount created", false, "(never_true)", "k8s-audit"}
c2 := CustomRulesYaml{"k8s service created", false, "never_true", "k8s-audit"}
c := []CustomRulesYaml{c1, c2}
c3 := CustomRules{c}
data := AutoGenerated{c3}
check, err := yaml.Marshal(&data)
if err != nil {
log.Fatal(err)
}
err2 := ioutil.WriteFile("/home/revaa/falco/custom_rules.yaml", check, 0)
if err2 != nil {
log.Fatal(err2)
}
fmt.Println("data written")
}
这是我的代码,在运行程序时,YAML 没有以上述格式附加。这些值被附加如下。
customRules:
custom-rules.yaml:
- rule: K8s serviceaccount created
append: false
condition: (never_true)
source: k8s-audit
- rule: k8s service created
append: false
condition: never_true
source: k8s-audit
为什么我没有获得所需格式的 YAML 文件?