|
| 1 | +package farconverter |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/gosimple/slug" |
| 12 | + "github.com/iancoleman/strcase" |
| 13 | + "github.com/spf13/cobra" |
| 14 | + "gopkg.in/yaml.v3" |
| 15 | +) |
| 16 | + |
| 17 | +// RawRule represents decoded rules in Falco format. |
| 18 | +type RawRule map[string]any |
| 19 | + |
| 20 | +// FalcoAuditRule is a structure to encode FalcoAuditRules custom resources. |
| 21 | +type FalcoAuditRule struct { |
| 22 | + ApiVersion string `yaml:"apiVersion"` |
| 23 | + Kind string |
| 24 | + Metadata Metadata |
| 25 | + Spec FalcoAuditRuleSpec |
| 26 | +} |
| 27 | + |
| 28 | +type FalcoAuditRuleSpecRule struct { |
| 29 | + Rule Rule `yaml:"rule,omitempty"` |
| 30 | + Macro Macro `yaml:"macro,omitempty"` |
| 31 | + List List `yaml:"list,omitempty"` |
| 32 | +} |
| 33 | + |
| 34 | +type FalcoAuditRuleSpec struct { |
| 35 | + RequiredEngineVersion int `yaml:"requiredEngineVersion,omitempty"` |
| 36 | + RequiredK8sAuditPluginVersion string `yaml:"requiredK8sAuditPluginVersion,omitempty"` |
| 37 | + Rules []FalcoAuditRuleSpecRule `yaml:"rules"` |
| 38 | +} |
| 39 | + |
| 40 | +type Metadata struct { |
| 41 | + Name string |
| 42 | +} |
| 43 | + |
| 44 | +type Rule struct { |
| 45 | + Name string `yaml:"name"` |
| 46 | + Condition string `yaml:"condition"` |
| 47 | + Desc string `yaml:"desc"` |
| 48 | + Output string `yaml:"output"` |
| 49 | + Priority string `yaml:"priority"` |
| 50 | + Enabled bool `yaml:"enabled,omitempty"` |
| 51 | + Tags []any `yaml:"tags,omitempty"` |
| 52 | + Source string `yaml:"source,omitempty"` |
| 53 | +} |
| 54 | + |
| 55 | +type Macro struct { |
| 56 | + Name string `yaml:"name"` |
| 57 | + Condition string `yaml:"condition"` |
| 58 | +} |
| 59 | + |
| 60 | +type List struct { |
| 61 | + Name string `yaml:"name"` |
| 62 | + Items []any `yaml:"items"` |
| 63 | +} |
| 64 | + |
| 65 | +func Convert(cmd *cobra.Command, args []string) error { |
| 66 | + input := args[0] |
| 67 | + var rules []RawRule |
| 68 | + log.Printf("Convert rules from %q", input) |
| 69 | + |
| 70 | + data, err := os.ReadFile(input) |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + |
| 75 | + err = yaml.Unmarshal(data, &rules) |
| 76 | + if err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + |
| 80 | + buf := bytes.Buffer{} |
| 81 | + enc := yaml.NewEncoder(&buf) |
| 82 | + enc.SetIndent(2) |
| 83 | + |
| 84 | + if err := enc.Encode(convert(input, rules)); err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + |
| 88 | + io.Copy(os.Stdout, &buf) |
| 89 | + return nil |
| 90 | +} |
| 91 | + |
| 92 | +func convert(path string, rules []RawRule) FalcoAuditRule { |
| 93 | + result := FalcoAuditRule{ |
| 94 | + ApiVersion: "deckhouse.io/v1alpha1", |
| 95 | + Kind: "FalcoAuditRules", |
| 96 | + Metadata: Metadata{ |
| 97 | + Name: nameFromPath(path), |
| 98 | + }, |
| 99 | + Spec: FalcoAuditRuleSpec{}, |
| 100 | + } |
| 101 | + |
| 102 | + for _, r := range rules { |
| 103 | + if v, ok := r["required_engine_version"]; ok { |
| 104 | + result.Spec.RequiredEngineVersion = v.(int) |
| 105 | + continue |
| 106 | + } |
| 107 | + |
| 108 | + if v, ok := r["required_plugin_versions"]; ok { |
| 109 | + for _, p := range v.([]any) { |
| 110 | + plugin := p.(map[string]any) |
| 111 | + if plugin["name"].(string) == "k8saudit" { |
| 112 | + result.Spec.RequiredK8sAuditPluginVersion = plugin["version"].(string) |
| 113 | + } |
| 114 | + } |
| 115 | + continue |
| 116 | + } |
| 117 | + |
| 118 | + if _, ok := r["macro"]; ok { |
| 119 | + result.Spec.Rules = append(result.Spec.Rules, FalcoAuditRuleSpecRule{ |
| 120 | + Macro: Macro{ |
| 121 | + Name: r["macro"].(string), |
| 122 | + Condition: r["condition"].(string), |
| 123 | + }, |
| 124 | + }) |
| 125 | + continue |
| 126 | + } |
| 127 | + |
| 128 | + if _, ok := r["list"]; ok { |
| 129 | + result.Spec.Rules = append(result.Spec.Rules, FalcoAuditRuleSpecRule{ |
| 130 | + List: List{ |
| 131 | + Name: r["list"].(string), |
| 132 | + Items: r["items"].([]any), |
| 133 | + }, |
| 134 | + }) |
| 135 | + continue |
| 136 | + } |
| 137 | + |
| 138 | + if _, ok := r["rule"]; ok { |
| 139 | + ruleToAdd := Rule{ |
| 140 | + Name: r["rule"].(string), |
| 141 | + Condition: r["condition"].(string), |
| 142 | + Desc: r["desc"].(string), |
| 143 | + Output: r["output"].(string), |
| 144 | + Priority: strcase.ToCamel(strings.ToLower(r["priority"].(string))), |
| 145 | + } |
| 146 | + |
| 147 | + if tags, ok := r["tags"]; ok { |
| 148 | + ruleToAdd.Tags = tags.([]any) |
| 149 | + } |
| 150 | + |
| 151 | + if enabled, ok := r["enabled"]; ok { |
| 152 | + ruleToAdd.Enabled = enabled.(bool) |
| 153 | + } |
| 154 | + |
| 155 | + if source, ok := r["source"]; ok { |
| 156 | + switch strings.ToLower(source.(string)) { |
| 157 | + case "k8s_audit": |
| 158 | + ruleToAdd.Source = "K8sAudit" |
| 159 | + case "syscall": |
| 160 | + ruleToAdd.Source = "Syscall" |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + if _, ok := r["exceptions"]; ok { |
| 165 | + log.Printf("[WARNING] Exceptions are not supported (found in %q rule)", ruleToAdd.Name) |
| 166 | + } |
| 167 | + |
| 168 | + result.Spec.Rules = append(result.Spec.Rules, FalcoAuditRuleSpecRule{Rule: ruleToAdd}) |
| 169 | + continue |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + return result |
| 174 | +} |
| 175 | + |
| 176 | +func nameFromPath(path string) string { |
| 177 | + path = filepath.Base(path) |
| 178 | + path, _, _ = strings.Cut(path, ".") |
| 179 | + path = slug.Make(path) |
| 180 | + path = strcase.ToKebab(path) |
| 181 | + return path |
| 182 | +} |
0 commit comments