Skip to content

Commit f82c27e

Browse files
mstemmpoiana
authored andcommitted
Add a prefix to all generated lists/rules
Add the ability to add a prefix to all generated lists, macros, and rules. This makes it easier to load multiple rules files generated from different PSPs without the generated rules/macros/lists overlapping with each other. The actual prefix to add is provided as a parameter to NewConverter(). It can be a blank string, in which case the PSP name is used as the prefix. If the name is missing for some reason, use the string "psp". Signed-off-by: Mark Stemm <[email protected]>
1 parent 02333bf commit f82c27e

File tree

3 files changed

+100
-81
lines changed

3 files changed

+100
-81
lines changed

cmd/psp_conv.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func convertPspFalcoRules(pspPath string, rulesPath string) error {
8585

8686
psp, err := ioutil.ReadAll(pspFile)
8787

88-
conv, err := converter.NewConverter(debugLog, infoLog, errorLog)
88+
conv, err := converter.NewConverter("", debugLog, infoLog, errorLog)
8989
if err != nil {
9090
return fmt.Errorf("Could not create converter: %v", err)
9191
}

pkg/converter/psp/converter.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,18 @@ import (
3636
type LogFunc func(format string, args ...interface{})
3737

3838
type Converter struct {
39+
namePrefix string
3940
pspTmpl *template.Template
4041
debugLog LogFunc
4142
infoLog LogFunc
4243
errorLog LogFunc
4344
}
4445

46+
type PspTemplate struct {
47+
NamePrefix string
48+
v1beta1.PodSecurityPolicy
49+
}
50+
4551
func joinProcMountTypes(procMountTypes []v1.ProcMountType) string {
4652
var sb strings.Builder
4753

@@ -153,7 +159,7 @@ func allowPrivilegeEscalation(spec v1beta1.PodSecurityPolicySpec) bool {
153159
return true
154160
}
155161

156-
func NewConverter(debugLog LogFunc, infoLog LogFunc, errorLog LogFunc) (*Converter, error) {
162+
func NewConverter(namePrefix string, debugLog LogFunc, infoLog LogFunc, errorLog LogFunc) (*Converter, error) {
157163

158164
tmpl := template.New("pspRules")
159165

@@ -175,6 +181,7 @@ func NewConverter(debugLog LogFunc, infoLog LogFunc, errorLog LogFunc) (*Convert
175181
}
176182

177183
return &Converter{
184+
namePrefix: namePrefix,
178185
pspTmpl: tmpl,
179186
debugLog: debugLog,
180187
infoLog: infoLog,
@@ -184,7 +191,7 @@ func NewConverter(debugLog LogFunc, infoLog LogFunc, errorLog LogFunc) (*Convert
184191

185192
func (c *Converter) GenerateRules(pspString string) (string, error) {
186193

187-
psp := v1beta1.PodSecurityPolicy{}
194+
pspTemplateArgs := PspTemplate{}
188195

189196
c.debugLog("GenerateRules() pspString=%s", pspString)
190197

@@ -193,26 +200,38 @@ func (c *Converter) GenerateRules(pspString string) (string, error) {
193200
return "", fmt.Errorf("Could not convert generic yaml document to json: %v", err)
194201
}
195202

196-
err = json.Unmarshal(pspJSON, &psp)
203+
err = json.Unmarshal(pspJSON, &pspTemplateArgs)
197204

198205
if err != nil {
199206
return "", fmt.Errorf("Could not unmarshal json document: %v", err)
200207
}
201208

202-
c.debugLog("PSP Object: %v", psp)
209+
// If namePrefix is empty, use the psp name as the prefix. If
210+
// that is missing, use "psp".
211+
if c.namePrefix == "" {
212+
if pspTemplateArgs.Name == "" {
213+
pspTemplateArgs.NamePrefix = "psp"
214+
} else {
215+
pspTemplateArgs.NamePrefix = pspTemplateArgs.Name
216+
}
217+
} else {
218+
pspTemplateArgs.NamePrefix = c.namePrefix
219+
}
220+
221+
c.debugLog("PSP Object: %v", pspTemplateArgs)
203222

204223
// The generated rules need a set of images for which
205224
// to scope the rules. A annotation with the key
206225
// "falco-rules-psp-images" provides the list of images.
207-
if _, ok := psp.Annotations["falco-rules-psp-images"]; !ok {
226+
if _, ok := pspTemplateArgs.Annotations["falco-rules-psp-images"]; !ok {
208227
return "", fmt.Errorf("PSP YAML document does not have an annotation \"falco-rules-psp-images\" that lists the images for which the generated rules should apply")
209228
}
210229

211-
c.debugLog("Images %v", psp.Annotations["falco-rules-psp-images"])
230+
c.debugLog("Images %v", pspTemplateArgs.Annotations["falco-rules-psp-images"])
212231

213232
var rulesB bytes.Buffer
214233

215-
err = c.pspTmpl.Execute(&rulesB, psp)
234+
err = c.pspTmpl.Execute(&rulesB, pspTemplateArgs)
216235

217236
if err != nil {
218237
return "", fmt.Errorf("Could not convert PSP to Falco Rules: %v", err)

0 commit comments

Comments
 (0)