Skip to content

Commit

Permalink
feat(policygroups): allow empty strings in material templates (#1778)
Browse files Browse the repository at this point in the history
Signed-off-by: Jose I. Paris <[email protected]>
  • Loading branch information
jiparis authored Feb 3, 2025
1 parent 38e140d commit 538fb94
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
4 changes: 4 additions & 0 deletions app/cli/internal/action/attestation_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ func getGroupMaterialsToAdd(group *v1.PolicyGroup, pgAtt *v1.PolicyGroupAttachme
if err != nil {
return nil, err
}
// skip if interpolated material name is still empty
if csm.GetName() == "" {
continue
}

// check if material already exists in the contract and skip it in that case
ignore := false
Expand Down
30 changes: 18 additions & 12 deletions app/cli/internal/action/attestation_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestTemplatedGroups(t *testing.T) {
cases := []struct {
name string
materials []*v1.CraftingSchema_Material
group string
groupFile string
args map[string]string
nMaterials int
materialName string
Expand All @@ -118,18 +118,25 @@ func TestTemplatedGroups(t *testing.T) {
{
name: "interpolates material names, with defaults",
materials: []*v1.CraftingSchema_Material{},
group: "file://testdata/policy_group_with_arguments.yaml",
groupFile: "file://testdata/policy_group_with_arguments.yaml",
nMaterials: 1,
materialName: "sbom",
},
{
name: "interpolates material names, custom material name",
materials: []*v1.CraftingSchema_Material{},
group: "file://testdata/policy_group_with_arguments.yaml",
groupFile: "file://testdata/policy_group_with_arguments.yaml",
args: map[string]string{"sbom_name": "foo"},
nMaterials: 2,
nMaterials: 1,
materialName: "foo",
},
{
name: "allows empty material name, making it anonymous",
materials: []*v1.CraftingSchema_Material{},
groupFile: "file://testdata/policy_group_with_arguments.yaml",
args: map[string]string{"sbom_name": ""},
nMaterials: 0,
},
{
name: "interpolates material names, custom name, with material override",
materials: []*v1.CraftingSchema_Material{{
Expand All @@ -138,9 +145,9 @@ func TestTemplatedGroups(t *testing.T) {
Optional: true,
},
},
group: "file://testdata/policy_group_with_arguments.yaml",
groupFile: "file://testdata/policy_group_with_arguments.yaml",
args: map[string]string{"sbom_name": "foo"},
nMaterials: 2,
nMaterials: 1,
materialName: "foo",
materialOptional: true,
},
Expand All @@ -153,19 +160,18 @@ func TestTemplatedGroups(t *testing.T) {
Materials: tc.materials,
PolicyGroups: []*v1.PolicyGroupAttachment{
{
Ref: tc.group,
Ref: tc.groupFile,
With: tc.args,
},
{
Ref: tc.group,
},
},
}
err := enrichContractMaterials(context.TODO(), &schema, nil, &l)
assert.NoError(t, err)
assert.Len(t, schema.Materials, tc.nMaterials)
assert.Equal(t, tc.materialName, schema.Materials[0].Name)
assert.Equal(t, tc.materialOptional, schema.Materials[0].Optional)
if tc.nMaterials > 0 {
assert.Equal(t, tc.materialName, schema.Materials[0].Name)
assert.Equal(t, tc.materialOptional, schema.Materials[0].Optional)
}
})
}
}

0 comments on commit 538fb94

Please sign in to comment.