Skip to content

Commit

Permalink
fix(policies): fix policy evaluation for SBOMs (#1665)
Browse files Browse the repository at this point in the history
Signed-off-by: Jose I. Paris <[email protected]>
  • Loading branch information
jiparis authored Dec 16, 2024
1 parent 4b5c833 commit 4ba74d5
Show file tree
Hide file tree
Showing 3 changed files with 132,940 additions and 11 deletions.
19 changes: 9 additions & 10 deletions pkg/attestation/crafter/api/attestation/v1/crafting_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,14 @@ func (m *Attestation_Material) GetEvaluableContent(value string) ([]byte, error)
var rawMaterial []byte
var err error

// nolint: gocritic
switch {
case m.GetArtifact() != nil:
artifact := m.GetArtifact()
if artifact == nil && m.GetSbomArtifact() != nil {
artifact = m.GetSbomArtifact().GetArtifact()
}

if artifact != nil {
if m.InlineCas {
rawMaterial = m.GetArtifact().GetContent()
rawMaterial = artifact.GetContent()
} else if value == "" {
return nil, errors.New("artifact path required")
} else if m.MaterialType != v1.CraftingSchema_Material_HELM_CHART &&
Expand All @@ -103,12 +106,6 @@ func (m *Attestation_Material) GetEvaluableContent(value string) ([]byte, error)
return nil, fmt.Errorf("failed to read material content: %w", err)
}
}
case m.GetSbomArtifact() != nil:
if m.InlineCas {
rawMaterial = m.GetSbomArtifact().GetArtifact().GetContent()
} else if value == "" {
return nil, errors.New("sbom artifact path required")
}
}

// special case for ATTESTATION materials, the statement needs to be extracted from the dsse wrapper.
Expand Down Expand Up @@ -277,6 +274,8 @@ func (m *Attestation_Material) GetID() string {
return m.GetArtifact().GetId()
} else if m.GetContainerImage() != nil {
return m.GetContainerImage().GetId()
} else if m.GetSbomArtifact() != nil {
return m.GetSbomArtifact().GetArtifact().GetId()
}
return ""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func TestNormalizeOutput(t *testing.T) {
func TestGetEvaluableContentWithMetadata(t *testing.T) {
cases := []struct {
name string
filename string
material *Attestation_Material
}{
{
Expand Down Expand Up @@ -178,11 +179,28 @@ func TestGetEvaluableContentWithMetadata(t *testing.T) {
InlineCas: true,
},
},
{
name: "sbom artifact material not inline",
material: &Attestation_Material{
MaterialType: schemaapi.CraftingSchema_Material_SBOM_CYCLONEDX_JSON,
M: &Attestation_Material_SbomArtifact{
SbomArtifact: &Attestation_Material_SBOMArtifact{
Artifact: &Attestation_Material_Artifact{
Name: "name", Digest: "sha256:deadbeef", IsSubject: true, Content: []byte("{}"),
},
MainComponent: &Attestation_Material_SBOMArtifact_MainComponent{
Name: "the-main-component",
},
},
},
},
filename: "testdata/sbom.cyclonedx.json",
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
content, err := tc.material.GetEvaluableContent("")
content, err := tc.material.GetEvaluableContent(tc.filename)
assert.NoError(t, err)
decoder := json.NewDecoder(bytes.NewReader(content))

Expand Down
Loading

0 comments on commit 4ba74d5

Please sign in to comment.