Skip to content

Commit 9f3e280

Browse files
committed
fix labels.json breakage
1 parent 71373c2 commit 9f3e280

2 files changed

Lines changed: 49 additions & 18 deletions

File tree

‎pkg/rhelv2/rpm/rpm.go‎

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111
"time"
1212

13+
log "github.com/sirupsen/logrus"
1314
"github.com/stackrox/rox/pkg/set"
1415
"github.com/stackrox/rox/pkg/utils"
1516
"github.com/stackrox/scanner/database"
@@ -89,28 +90,24 @@ func AddToDependencyMap(filename string, fileData analyzer.FileData, execToDeps,
8990
// getContentManifestSets returns the list of content sets defined in the content
9091
// manifest, if found in the files. Otherwise, `nil` or empty slice is returned.
9192
func getContentManifestSets(files analyzer.Files) ([]string, error) {
92-
var contents []byte
93+
var lastErr error
9394
for _, prefix := range contentManifestDirs.AsSlice() {
9495
for name, file := range files.GetFilesPrefix(prefix) {
95-
if strings.HasSuffix(name, ".json") {
96-
// Return the first one found, as we are currently assuming there is only one per
97-
// layer/node.
98-
contents = file.Contents
99-
break
96+
if !strings.HasSuffix(name, ".json") {
97+
continue
98+
}
99+
var contentManifest database.ContentManifest
100+
if err := json.Unmarshal(file.Contents, &contentManifest); err != nil {
101+
log.WithFields(log.Fields{"prefix": prefix, "file": name}).WithError(err).Warn("failed to parse content manifest, skipping")
102+
lastErr = err
103+
continue
104+
}
105+
if len(contentManifest.ContentSets) > 0 {
106+
return contentManifest.ContentSets, nil
100107
}
101108
}
102-
if contents != nil {
103-
break
104-
}
105-
}
106-
if contents == nil {
107-
return nil, nil
108-
}
109-
var contentManifest database.ContentManifest
110-
if err := json.Unmarshal(contents, &contentManifest); err != nil {
111-
return nil, err
112109
}
113-
return contentManifest.ContentSets, nil
110+
return nil, lastErr
114111
}
115112

116113
func getFeaturesFromRPMDatabase(files analyzer.Files, testing bool) ([]*database.RHELv2Package, error) {

‎pkg/rhelv2/rpm/rpm_test.go‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,41 @@ func Test_getContentManifestSets(t *testing.T) {
321321
}),
322322
wantErr: assert.Error,
323323
},
324-
324+
{
325+
name: "when labels.json without content sets and content-sets.json with content sets, then return content sets",
326+
filesArg: tarutil.CreateNewLayerFiles(map[string]analyzer.FileData{
327+
"root/buildinfo/content_manifests/labels.json": {
328+
Contents: []byte(`{"name": "ubi9/ubi", "version": "9.7"}`),
329+
},
330+
"root/buildinfo/content_manifests/content-sets.json": {
331+
Contents: []byte(`{
332+
"content_sets": [
333+
"rhel-9-for-x86_64-baseos-rpms",
334+
"rhel-9-for-x86_64-appstream-rpms"
335+
]
336+
}`),
337+
},
338+
}),
339+
want: []string{"rhel-9-for-x86_64-baseos-rpms", "rhel-9-for-x86_64-appstream-rpms"},
340+
wantErr: assert.NoError,
341+
},
342+
{
343+
name: "when invalid json alongside valid content sets, then return content sets",
344+
filesArg: tarutil.CreateNewLayerFiles(map[string]analyzer.FileData{
345+
"root/buildinfo/content_manifests/broken.json": {
346+
Contents: []byte("not json"),
347+
},
348+
"root/buildinfo/content_manifests/content-sets.json": {
349+
Contents: []byte(`{
350+
"content_sets": [
351+
"rhel-8-for-x86_64-baseos-rpms"
352+
]
353+
}`),
354+
},
355+
}),
356+
want: []string{"rhel-8-for-x86_64-baseos-rpms"},
357+
wantErr: assert.NoError,
358+
},
325359
{
326360
name: "when rhel content sets found, then return",
327361
filesArg: tarutil.CreateNewLayerFiles(map[string]analyzer.FileData{

0 commit comments

Comments
 (0)