Skip to content

Commit e6e1081

Browse files
authored
Fix duplicate names when a YAML file has multiple manifests (#28)
This only occurs with the consolidateManifests=false option. Signed-off-by: mprahl <[email protected]>
1 parent b190a91 commit e6e1081

File tree

2 files changed

+86
-44
lines changed

2 files changed

+86
-44
lines changed

internal/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func getPolicyTemplates(policyConf *types.PolicyConfig) ([]map[string]map[string
136136
policyTemplates := make([]map[string]map[string]interface{}, 0, policyTemplatesLength)
137137
for i, manifestGroup := range manifestGroups {
138138
complianceType := policyConf.Manifests[i].ComplianceType
139-
for j, manifest := range manifestGroup {
139+
for _, manifest := range manifestGroup {
140140
objTemplate := map[string]interface{}{
141141
"complianceType": complianceType,
142142
"objectDefinition": manifest,
@@ -148,7 +148,7 @@ func getPolicyTemplates(policyConf *types.PolicyConfig) ([]map[string]map[string
148148
// casting each objTemplate with manifest to objectTemplates type
149149
// build policyTemplate for each objectTemplates
150150
policyTemplate := buildPolicyTemplate(
151-
policyConf, i+j+1, &[]map[string]interface{}{objTemplate},
151+
policyConf, len(policyTemplates)+1, &[]map[string]interface{}{objTemplate},
152152
)
153153
setNamespaceSelector(policyConf, policyTemplate)
154154
policyTemplates = append(policyTemplates, *policyTemplate)

internal/utils_test.go

Lines changed: 84 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,65 @@ data:
126126
}
127127
assertEqual(t, kind2, "ConfigMap")
128128
}
129+
}
130+
131+
func TestGetPolicyTemplateNoConsolidate(t *testing.T) {
132+
t.Parallel()
133+
tmpDir := t.TempDir()
134+
manifestFiles := []types.Manifest{}
135+
for i, enemy := range []string{"goldfish", "potato"} {
136+
manifestPath := path.Join(tmpDir, fmt.Sprintf("configmap%d.yaml", i))
137+
manifestYAML := fmt.Sprintf(
138+
`
139+
apiVersion: v1
140+
kind: ConfigMap
141+
metadata:
142+
name: my-configmap
143+
data:
144+
game.properties: enemies=%s
145+
---
146+
apiVersion: v1
147+
kind: ConfigMap
148+
metadata:
149+
name: my-configmap2
150+
data:
151+
game.properties: enemies=%s
152+
`,
153+
enemy,
154+
enemy,
155+
)
156+
err := ioutil.WriteFile(manifestPath, []byte(manifestYAML), 0o666)
157+
if err != nil {
158+
t.Fatalf("Failed to write %s", manifestPath)
159+
}
160+
161+
// The applyDefaults method would normally fill in ComplianceType on each manifest entry.
162+
manifestFiles = append(
163+
manifestFiles, types.Manifest{ComplianceType: "musthave", Path: manifestPath},
164+
)
165+
}
166+
167+
// Write a bogus file to ensure it is not picked up when creating the policy
168+
// template
169+
bogusFilePath := path.Join(tmpDir, "README.md")
170+
err := ioutil.WriteFile(bogusFilePath, []byte("# My Manifests"), 0o666)
171+
if err != nil {
172+
t.Fatalf("Failed to write %s", bogusFilePath)
173+
}
174+
175+
// Test both passing in individual files and a flat directory
176+
tests := []struct {
177+
Manifests []types.Manifest
178+
}{
179+
{Manifests: manifestFiles},
180+
// The applyDefaults method would normally fill in ComplianceType on each manifest entry.
181+
{
182+
Manifests: []types.Manifest{{ComplianceType: "musthave", Path: tmpDir}},
183+
},
184+
}
129185

130186
// test ConsolidateManifests = false case
131-
// policyTemplates will skip the consolidation and have two policyTemplate
187+
// policyTemplates will skip the consolidation and have four policyTemplate
132188
// and each policyTemplate has only one objTemplate
133189
for _, test := range tests {
134190
policyConf := types.PolicyConfig{
@@ -144,48 +200,34 @@ data:
144200
if err != nil {
145201
t.Fatalf("Failed to get the policy templates: %v", err)
146202
}
147-
assertEqual(t, len(policyTemplates), 2)
148-
policyTemplate1 := policyTemplates[0]
149-
objdef1 := policyTemplate1["objectDefinition"]
150-
assertEqual(t, objdef1["metadata"].(map[string]string)["name"], "policy-app-config")
151-
spec1, ok := objdef1["spec"].(map[string]interface{})
152-
if !ok {
153-
t.Fatal("The spec field is an invalid format")
154-
}
155-
assertEqual(t, spec1["remediationAction"], "inform")
156-
assertEqual(t, spec1["severity"], "low")
157-
objTemplates1, ok := spec1["object-templates"].([]map[string]interface{})
158-
if !ok {
159-
t.Fatal("The object-templates field is an invalid format")
160-
}
161-
assertEqual(t, len(objTemplates1), 1)
162-
assertEqual(t, objTemplates1[0]["complianceType"], test.ExpectedComplianceType)
163-
kind1, ok := objTemplates1[0]["objectDefinition"].(map[string]interface{})["kind"]
164-
if !ok {
165-
t.Fatal("The objectDefinition field is an invalid format")
166-
}
167-
assertEqual(t, kind1, "ConfigMap")
168-
169-
policyTemplate2 := policyTemplates[1]
170-
objdef2 := policyTemplate2["objectDefinition"]
171-
assertEqual(t, objdef2["metadata"].(map[string]string)["name"], "policy-app-config2")
172-
spec2, ok := objdef2["spec"].(map[string]interface{})
173-
if !ok {
174-
t.Fatal("The spec field is an invalid format")
175-
}
176-
assertEqual(t, spec2["remediationAction"], "inform")
177-
assertEqual(t, spec2["severity"], "low")
178-
objTemplates2, ok := spec2["object-templates"].([]map[string]interface{})
179-
if !ok {
180-
t.Fatal("The object-templates field is an invalid format")
203+
assertEqual(t, len(policyTemplates), 4)
204+
205+
for i := 0; i < len(policyTemplates); i++ {
206+
policyTemplate := policyTemplates[i]
207+
objdef := policyTemplate["objectDefinition"]
208+
name := "policy-app-config"
209+
if i > 0 {
210+
name += fmt.Sprintf("%d", i+1)
211+
}
212+
assertEqual(t, objdef["metadata"].(map[string]string)["name"], name)
213+
spec, ok := objdef["spec"].(map[string]interface{})
214+
if !ok {
215+
t.Fatal("The spec field is an invalid format")
216+
}
217+
assertEqual(t, spec["remediationAction"], "inform")
218+
assertEqual(t, spec["severity"], "low")
219+
objTemplates, ok := spec["object-templates"].([]map[string]interface{})
220+
if !ok {
221+
t.Fatal("The object-templates field is an invalid format")
222+
}
223+
assertEqual(t, len(objTemplates), 1)
224+
assertEqual(t, objTemplates[0]["complianceType"], "musthave")
225+
kind1, ok := objTemplates[0]["objectDefinition"].(map[string]interface{})["kind"]
226+
if !ok {
227+
t.Fatal("The objectDefinition field is an invalid format")
228+
}
229+
assertEqual(t, kind1, "ConfigMap")
181230
}
182-
assertEqual(t, len(objTemplates2), 1)
183-
assertEqual(t, objTemplates2[0]["complianceType"], test.ExpectedComplianceType)
184-
kind2, ok := objTemplates2[0]["objectDefinition"].(map[string]interface{})["kind"]
185-
if !ok {
186-
t.Fatal("The objectDefinition field is an invalid format")
187-
}
188-
assertEqual(t, kind2, "ConfigMap")
189231
}
190232
}
191233

0 commit comments

Comments
 (0)