Skip to content

Commit 4ba74d5

Browse files
authored
fix(policies): fix policy evaluation for SBOMs (#1665)
Signed-off-by: Jose I. Paris <[email protected]>
1 parent 4b5c833 commit 4ba74d5

File tree

3 files changed

+132940
-11
lines changed

3 files changed

+132940
-11
lines changed

pkg/attestation/crafter/api/attestation/v1/crafting_state.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,14 @@ func (m *Attestation_Material) GetEvaluableContent(value string) ([]byte, error)
8888
var rawMaterial []byte
8989
var err error
9090

91-
// nolint: gocritic
92-
switch {
93-
case m.GetArtifact() != nil:
91+
artifact := m.GetArtifact()
92+
if artifact == nil && m.GetSbomArtifact() != nil {
93+
artifact = m.GetSbomArtifact().GetArtifact()
94+
}
95+
96+
if artifact != nil {
9497
if m.InlineCas {
95-
rawMaterial = m.GetArtifact().GetContent()
98+
rawMaterial = artifact.GetContent()
9699
} else if value == "" {
97100
return nil, errors.New("artifact path required")
98101
} else if m.MaterialType != v1.CraftingSchema_Material_HELM_CHART &&
@@ -103,12 +106,6 @@ func (m *Attestation_Material) GetEvaluableContent(value string) ([]byte, error)
103106
return nil, fmt.Errorf("failed to read material content: %w", err)
104107
}
105108
}
106-
case m.GetSbomArtifact() != nil:
107-
if m.InlineCas {
108-
rawMaterial = m.GetSbomArtifact().GetArtifact().GetContent()
109-
} else if value == "" {
110-
return nil, errors.New("sbom artifact path required")
111-
}
112109
}
113110

114111
// special case for ATTESTATION materials, the statement needs to be extracted from the dsse wrapper.
@@ -277,6 +274,8 @@ func (m *Attestation_Material) GetID() string {
277274
return m.GetArtifact().GetId()
278275
} else if m.GetContainerImage() != nil {
279276
return m.GetContainerImage().GetId()
277+
} else if m.GetSbomArtifact() != nil {
278+
return m.GetSbomArtifact().GetArtifact().GetId()
280279
}
281280
return ""
282281
}

pkg/attestation/crafter/api/attestation/v1/crafting_state_test.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ func TestNormalizeOutput(t *testing.T) {
136136
func TestGetEvaluableContentWithMetadata(t *testing.T) {
137137
cases := []struct {
138138
name string
139+
filename string
139140
material *Attestation_Material
140141
}{
141142
{
@@ -178,11 +179,28 @@ func TestGetEvaluableContentWithMetadata(t *testing.T) {
178179
InlineCas: true,
179180
},
180181
},
182+
{
183+
name: "sbom artifact material not inline",
184+
material: &Attestation_Material{
185+
MaterialType: schemaapi.CraftingSchema_Material_SBOM_CYCLONEDX_JSON,
186+
M: &Attestation_Material_SbomArtifact{
187+
SbomArtifact: &Attestation_Material_SBOMArtifact{
188+
Artifact: &Attestation_Material_Artifact{
189+
Name: "name", Digest: "sha256:deadbeef", IsSubject: true, Content: []byte("{}"),
190+
},
191+
MainComponent: &Attestation_Material_SBOMArtifact_MainComponent{
192+
Name: "the-main-component",
193+
},
194+
},
195+
},
196+
},
197+
filename: "testdata/sbom.cyclonedx.json",
198+
},
181199
}
182200

183201
for _, tc := range cases {
184202
t.Run(tc.name, func(t *testing.T) {
185-
content, err := tc.material.GetEvaluableContent("")
203+
content, err := tc.material.GetEvaluableContent(tc.filename)
186204
assert.NoError(t, err)
187205
decoder := json.NewDecoder(bytes.NewReader(content))
188206

0 commit comments

Comments
 (0)