Skip to content

Commit 538fb94

Browse files
authored
feat(policygroups): allow empty strings in material templates (#1778)
Signed-off-by: Jose I. Paris <[email protected]>
1 parent 38e140d commit 538fb94

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

app/cli/internal/action/attestation_init.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ func getGroupMaterialsToAdd(group *v1.PolicyGroup, pgAtt *v1.PolicyGroupAttachme
251251
if err != nil {
252252
return nil, err
253253
}
254+
// skip if interpolated material name is still empty
255+
if csm.GetName() == "" {
256+
continue
257+
}
254258

255259
// check if material already exists in the contract and skip it in that case
256260
ignore := false

app/cli/internal/action/attestation_init_test.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func TestTemplatedGroups(t *testing.T) {
109109
cases := []struct {
110110
name string
111111
materials []*v1.CraftingSchema_Material
112-
group string
112+
groupFile string
113113
args map[string]string
114114
nMaterials int
115115
materialName string
@@ -118,18 +118,25 @@ func TestTemplatedGroups(t *testing.T) {
118118
{
119119
name: "interpolates material names, with defaults",
120120
materials: []*v1.CraftingSchema_Material{},
121-
group: "file://testdata/policy_group_with_arguments.yaml",
121+
groupFile: "file://testdata/policy_group_with_arguments.yaml",
122122
nMaterials: 1,
123123
materialName: "sbom",
124124
},
125125
{
126126
name: "interpolates material names, custom material name",
127127
materials: []*v1.CraftingSchema_Material{},
128-
group: "file://testdata/policy_group_with_arguments.yaml",
128+
groupFile: "file://testdata/policy_group_with_arguments.yaml",
129129
args: map[string]string{"sbom_name": "foo"},
130-
nMaterials: 2,
130+
nMaterials: 1,
131131
materialName: "foo",
132132
},
133+
{
134+
name: "allows empty material name, making it anonymous",
135+
materials: []*v1.CraftingSchema_Material{},
136+
groupFile: "file://testdata/policy_group_with_arguments.yaml",
137+
args: map[string]string{"sbom_name": ""},
138+
nMaterials: 0,
139+
},
133140
{
134141
name: "interpolates material names, custom name, with material override",
135142
materials: []*v1.CraftingSchema_Material{{
@@ -138,9 +145,9 @@ func TestTemplatedGroups(t *testing.T) {
138145
Optional: true,
139146
},
140147
},
141-
group: "file://testdata/policy_group_with_arguments.yaml",
148+
groupFile: "file://testdata/policy_group_with_arguments.yaml",
142149
args: map[string]string{"sbom_name": "foo"},
143-
nMaterials: 2,
150+
nMaterials: 1,
144151
materialName: "foo",
145152
materialOptional: true,
146153
},
@@ -153,19 +160,18 @@ func TestTemplatedGroups(t *testing.T) {
153160
Materials: tc.materials,
154161
PolicyGroups: []*v1.PolicyGroupAttachment{
155162
{
156-
Ref: tc.group,
163+
Ref: tc.groupFile,
157164
With: tc.args,
158165
},
159-
{
160-
Ref: tc.group,
161-
},
162166
},
163167
}
164168
err := enrichContractMaterials(context.TODO(), &schema, nil, &l)
165169
assert.NoError(t, err)
166170
assert.Len(t, schema.Materials, tc.nMaterials)
167-
assert.Equal(t, tc.materialName, schema.Materials[0].Name)
168-
assert.Equal(t, tc.materialOptional, schema.Materials[0].Optional)
171+
if tc.nMaterials > 0 {
172+
assert.Equal(t, tc.materialName, schema.Materials[0].Name)
173+
assert.Equal(t, tc.materialOptional, schema.Materials[0].Optional)
174+
}
169175
})
170176
}
171177
}

0 commit comments

Comments
 (0)