Skip to content

Commit f339a95

Browse files
committed
Add support for object-templates-raw
Adds support for manifest files with only object-templates-raw field, which gets put into a ConfigurationPolicy. Signed-off-by: Jeffrey Luo <[email protected]>
1 parent 1a25c98 commit f339a95

File tree

10 files changed

+632
-10
lines changed

10 files changed

+632
-10
lines changed

docs/policygenerator-reference.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,10 @@ policies:
216216
# 1) Non-root policy type manifests such as IamPolicy, CertificatePolicy, and ConfigurationPolicy that have a
217217
# "Policy" suffix. These are not modified except for patches and are directly added as a Policy's
218218
# policy-templates entry.
219-
# 2) For everything else, ConfigurationPolicy objects are generated to wrap these manifests. The resulting
219+
# 2) Manifests containing only an `object-templates-raw` key. The corresponding value will be used directly in
220+
# a generated ConfigurationPolicy without modification, which will then be added as a Policy's
221+
# policy-templates entry.
222+
# 3) For everything else, ConfigurationPolicy objects are generated to wrap these manifests. The resulting
220223
# ConfigurationPolicy is added as a Policy's policy-templates entry.
221224
- path: ""
222225
# Optional. (See policyDefaults.complianceType for description.)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
object-templates-raw: |
2+
- complianceType: musthave
3+
objectDefinition:
4+
apiVersion: v1
5+
kind: ConfigMap
6+
metadata:
7+
name: game-config-aliens
8+
namespace: default
9+
data:
10+
game.properties: |
11+
enemies=aliens
12+
ui.properties: |
13+
color.good=purple

examples/policyGenerator.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ policies:
5353
- path: input-kyverno/
5454
policySets:
5555
- policyset-kyverno
56+
- name: policy-object-templates-raw
57+
disabled: true
58+
manifests:
59+
- path: input-object-templates-raw/
60+
remediationAction: enforce
5661
- name: policy-require-ns-labels
5762
manifests:
5863
- path: input-gatekeeper/

internal/ordering_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ func TestIgnorePending(t *testing.T) {
271271
t.Parallel()
272272
tmpDir := t.TempDir()
273273
createConfigMap(t, tmpDir, "configmap.yaml")
274+
createObjectTemplatesRawManifest(t, tmpDir, "object-templates-raw.yaml")
274275

275276
tests := map[string]genOutTest{
276277
"policyDefaults.ignorePending is propagated to all manifests": {
@@ -368,6 +369,30 @@ policies:
368369
wantFile: "testdata/ordering/ignore-pending-manifest-override.yaml",
369370
wantErr: "",
370371
},
372+
"policyDefaults.ignorePending is propagated with object-templates-raw": {
373+
tmpDir: tmpDir,
374+
generator: `
375+
apiVersion: policy.open-cluster-management.io/v1
376+
kind: PolicyGenerator
377+
metadata:
378+
name: test
379+
policyDefaults:
380+
consolidateManifests: false
381+
ignorePending: true
382+
namespace: my-policies
383+
policies:
384+
- name: one
385+
manifests:
386+
- path: {{printf "%v/%v" .Dir "configmap.yaml"}}
387+
- path: {{printf "%v/%v" .Dir "object-templates-raw.yaml"}}
388+
- name: two
389+
manifests:
390+
- path: {{printf "%v/%v" .Dir "configmap.yaml"}}
391+
- path: {{printf "%v/%v" .Dir "object-templates-raw.yaml"}}
392+
`,
393+
wantFile: "testdata/ordering/ignore-pending-object-templates-raw.yaml",
394+
wantErr: "",
395+
},
371396
}
372397

373398
for name := range tests {
@@ -544,6 +569,7 @@ func TestExtraDependencies(t *testing.T) {
544569
tmpDir := t.TempDir()
545570
createConfigMap(t, tmpDir, "configmap.yaml")
546571
createConfigPolicyManifest(t, tmpDir, "configpolicy.yaml")
572+
createObjectTemplatesRawManifest(t, tmpDir, "object-templates-raw.yaml")
547573

548574
tests := map[string]genOutTest{
549575
"policyDefaults.extraDependencies are propagated to all manifests": {
@@ -746,6 +772,31 @@ policies:
746772
wantFile: "testdata/ordering/extradeps-overrides.yaml",
747773
wantErr: "",
748774
},
775+
"policyDefaults.extraDependencies are propagated with object-templates-raw": {
776+
tmpDir: tmpDir,
777+
generator: `
778+
apiVersion: policy.open-cluster-management.io/v1
779+
kind: PolicyGenerator
780+
metadata:
781+
name: test
782+
policyDefaults:
783+
consolidateManifests: false
784+
namespace: my-policies
785+
extraDependencies:
786+
- name: extrafoo
787+
policies:
788+
- name: one
789+
manifests:
790+
- path: {{printf "%v/%v" .Dir "configmap.yaml"}}
791+
- path: {{printf "%v/%v" .Dir "object-templates-raw.yaml"}}
792+
- name: two
793+
manifests:
794+
- path: {{printf "%v/%v" .Dir "configmap.yaml"}}
795+
- path: {{printf "%v/%v" .Dir "object-templates-raw.yaml"}}
796+
`,
797+
wantFile: "testdata/ordering/default-extradeps-object-templates-raw.yaml",
798+
wantErr: "",
799+
},
749800
}
750801

751802
for name := range tests {

internal/plugin_config_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,29 @@ spec:
8383
}
8484
}
8585

86+
func createObjectTemplatesRawManifest(t *testing.T, tmpDir, filename string) {
87+
t.Helper()
88+
89+
manifestsPath := path.Join(tmpDir, filename)
90+
yamlContent := `
91+
object-templates-raw: |-
92+
- complianceType: musthave
93+
objectDefinition:
94+
apiVersion: v1
95+
kind: ConfigMap
96+
metadata:
97+
name: example
98+
namespace: default
99+
data:
100+
extraData: data
101+
`
102+
103+
err := os.WriteFile(manifestsPath, []byte(yamlContent), 0o666)
104+
if err != nil {
105+
t.Fatalf("Failed to write %s", manifestsPath)
106+
}
107+
}
108+
86109
func TestConfig(t *testing.T) {
87110
t.Parallel()
88111
tmpDir := t.TempDir()

internal/plugin_test.go

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,6 +1364,73 @@ spec:
13641364
assertEqual(t, output, expected)
13651365
}
13661366

1367+
func TestCreatePolicyFromObjectTemplatesRawManifest(t *testing.T) {
1368+
t.Parallel()
1369+
tmpDir := t.TempDir()
1370+
createObjectTemplatesRawManifest(t, tmpDir, "objectTemplatesRawPluginTest.yaml")
1371+
1372+
p := Plugin{}
1373+
p.PolicyDefaults.Namespace = "my-policies"
1374+
policyConf := types.PolicyConfig{
1375+
PolicyOptions: types.PolicyOptions{
1376+
Categories: []string{"AC Access Control"},
1377+
Controls: []string{"AC-3 Access Enforcement"},
1378+
Standards: []string{"NIST SP 800-53"},
1379+
},
1380+
Name: "policy-app-config",
1381+
Manifests: []types.Manifest{
1382+
{Path: path.Join(tmpDir, "objectTemplatesRawPluginTest.yaml")},
1383+
},
1384+
}
1385+
p.Policies = append(p.Policies, policyConf)
1386+
p.applyDefaults(map[string]interface{}{})
1387+
1388+
err := p.createPolicy(&p.Policies[0])
1389+
if err != nil {
1390+
t.Fatal(err.Error())
1391+
}
1392+
1393+
output := p.outputBuffer.String()
1394+
1395+
expected := `
1396+
---
1397+
apiVersion: policy.open-cluster-management.io/v1
1398+
kind: Policy
1399+
metadata:
1400+
annotations:
1401+
policy.open-cluster-management.io/categories: AC Access Control
1402+
policy.open-cluster-management.io/controls: AC-3 Access Enforcement
1403+
policy.open-cluster-management.io/description: ""
1404+
policy.open-cluster-management.io/standards: NIST SP 800-53
1405+
name: policy-app-config
1406+
namespace: my-policies
1407+
spec:
1408+
disabled: false
1409+
policy-templates:
1410+
- objectDefinition:
1411+
apiVersion: policy.open-cluster-management.io/v1
1412+
kind: ConfigurationPolicy
1413+
metadata:
1414+
name: policy-app-config
1415+
spec:
1416+
object-templates-raw: |-
1417+
- complianceType: musthave
1418+
objectDefinition:
1419+
apiVersion: v1
1420+
kind: ConfigMap
1421+
metadata:
1422+
name: example
1423+
namespace: default
1424+
data:
1425+
extraData: data
1426+
remediationAction: inform
1427+
severity: low
1428+
remediationAction: inform
1429+
`
1430+
expected = strings.TrimPrefix(expected, "\n")
1431+
assertEqual(t, output, expected)
1432+
}
1433+
13671434
func TestCreatePolicyWithGkConstraintTemplate(t *testing.T) {
13681435
t.Parallel()
13691436
tmpDir := t.TempDir()
@@ -3228,6 +3295,7 @@ func TestGenerateEvaluationInterval(t *testing.T) {
32283295
t.Parallel()
32293296
tmpDir := t.TempDir()
32303297
createConfigMap(t, tmpDir, "configmap.yaml")
3298+
createObjectTemplatesRawManifest(t, tmpDir, "object-templates-raw.yaml")
32313299

32323300
p := Plugin{}
32333301
var err error
@@ -3289,7 +3357,14 @@ func TestGenerateEvaluationInterval(t *testing.T) {
32893357
{Path: path.Join(tmpDir, "configmap.yaml")},
32903358
},
32913359
}
3292-
p.Policies = append(p.Policies, policyConf, policyConf2, policyConf3)
3360+
// Test that the policy defaults get inherited with object-templates-raw.
3361+
policyConf4 := types.PolicyConfig{
3362+
Name: "policy-app-config4",
3363+
Manifests: []types.Manifest{
3364+
{Path: path.Join(tmpDir, "object-templates-raw.yaml")},
3365+
},
3366+
}
3367+
p.Policies = append(p.Policies, policyConf, policyConf2, policyConf3, policyConf4)
32933368
p.applyDefaults(
32943369
map[string]interface{}{
32953370
"policies": []interface{}{
@@ -3331,7 +3406,7 @@ func TestGenerateEvaluationInterval(t *testing.T) {
33313406
t.Fatal(err.Error())
33323407
}
33333408

3334-
assertEqual(t, len(generatedManifests), 9)
3409+
assertEqual(t, len(generatedManifests), 12)
33353410

33363411
for _, manifest := range generatedManifests {
33373412
kind, _ := manifest["kind"].(string)
@@ -3367,6 +3442,11 @@ func TestGenerateEvaluationInterval(t *testing.T) {
33673442
assertEqual(t, len(policyTemplates), 1)
33683443
evaluationInterval := getYAMLEvaluationInterval(t, policyTemplates[0], true)
33693444
assertEqual(t, len(evaluationInterval), 0)
3445+
} else if name == "policy-app-config4" {
3446+
assertEqual(t, len(policyTemplates), 1)
3447+
evaluationInterval := getYAMLEvaluationInterval(t, policyTemplates[0], false)
3448+
assertEqual(t, evaluationInterval["compliant"], "never")
3449+
assertEqual(t, evaluationInterval["noncompliant"], "15s")
33703450
}
33713451
}
33723452
}

0 commit comments

Comments
 (0)