Skip to content

Commit 388f476

Browse files
refactor(report): Replacing source_location in github report when scanning an image (aquasecurity#5999)
Co-authored-by: DmitriyLewen <[email protected]>
1 parent cd3e4bc commit 388f476

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

pkg/report/github/github.go

+23-2
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,25 @@ func (w Writer) Write(ctx context.Context, report types.Report) error {
105105
manifest.Name = string(result.Type)
106106
// show path for language-specific packages only
107107
if result.Class == types.ClassLangPkg {
108-
manifest.File = &File{
109-
SrcLocation: result.Target,
108+
if report.ArtifactType == ftypes.ArtifactContainerImage {
109+
// `RepoDigests` ~= <registry>/<image_name>@sha256:<image_hash>
110+
// `RepoTag` ~= <registry>/<image_name>:<image_tag>
111+
// By concatenating the hash from `RepoDigests` at the end of `RepoTag` we get all the information
112+
imageReference := strings.Join(report.Metadata.RepoTags, ", ")
113+
imageWithHash := strings.Join(report.Metadata.RepoDigests, ", ")
114+
_, imageHash, found := strings.Cut(imageWithHash, "@")
115+
if found {
116+
imageReference += "@" + imageHash
117+
}
118+
// Replacing `source_location` in manifest by the image name, tag and hash
119+
manifest.File = &File{
120+
SrcLocation: imageReference,
121+
}
122+
123+
} else {
124+
manifest.File = &File{
125+
SrcLocation: result.Target,
126+
}
110127
}
111128
}
112129

@@ -123,6 +140,10 @@ func (w Writer) Write(ctx context.Context, report types.Report) error {
123140
return xerrors.Errorf("unable to build purl for %s: %w", pkg.Name, err)
124141
}
125142

143+
if pkg.FilePath != "" {
144+
githubPkg.Metadata = Metadata{"source_location": pkg.FilePath}
145+
}
146+
126147
resolved[pkg.Name] = githubPkg
127148
}
128149

pkg/report/github/github_test.go

+67
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,73 @@ func TestWriter_Write(t *testing.T) {
164164
},
165165
},
166166
},
167+
{
168+
name: "pypi from image",
169+
report: types.Report{
170+
SchemaVersion: 2,
171+
ArtifactName: "fake_repo.azurecr.io/image_name",
172+
ArtifactType: "container_image",
173+
Metadata: types.Metadata{
174+
RepoDigests: []string{"fake_repo.azurecr.io/image_name@sha256:a7c92cdcb3d010f6edeb37ddcdbacab14981aa31e7f1140e0097dc1b8e834c49"},
175+
RepoTags: []string{"fake_repo.azurecr.io/image_name:latest"},
176+
},
177+
Results: types.Results{
178+
{
179+
Target: "Python",
180+
Class: "lang-pkgs",
181+
Type: "python-pkg",
182+
Packages: []ftypes.Package{
183+
{
184+
Name: "jwcrypto",
185+
Version: "0.7",
186+
Licenses: []string{
187+
"LGPLv3+",
188+
},
189+
Layer: ftypes.Layer{
190+
Digest: "sha256:ddc612ba4e74ea5633a93e19e7c32f61f5f230073b21a070302a61ef5eec5c50",
191+
DiffID: "sha256:12935ef6ce21a266aef8df75d601cebf7e935edd01e9f19fab16ccb78fbb9a5e",
192+
},
193+
FilePath: "opt/pyenv/versions/3.11.2/lib/python3.11/site-packages/jwcrypto-0.7.dist-info/METADATA",
194+
},
195+
{
196+
Name: "matplotlib",
197+
Version: "3.5.3",
198+
Licenses: []string{
199+
"PSF",
200+
},
201+
Layer: ftypes.Layer{
202+
Digest: "sha256:ddc612ba4e74ea5633a93e19e7c32f61f5f230073b21a070302a61ef5eec5c50",
203+
DiffID: "sha256:12935ef6ce21a266aef8df75d601cebf7e935edd01e9f19fab16ccb78fbb9a5e",
204+
},
205+
FilePath: "opt/pyenv/versions/3.11.2/lib/python3.11/site-packages/matplotlib-3.5.3.dist-info/METADATA",
206+
},
207+
},
208+
},
209+
},
210+
},
211+
want: map[string]github.Manifest{
212+
"Python": {
213+
Name: "python-pkg",
214+
File: &github.File{
215+
SrcLocation: "fake_repo.azurecr.io/image_name:latest@sha256:a7c92cdcb3d010f6edeb37ddcdbacab14981aa31e7f1140e0097dc1b8e834c49",
216+
},
217+
Resolved: map[string]github.Package{
218+
"jwcrypto": {
219+
PackageUrl: "pkg:pypi/[email protected]",
220+
Relationship: "direct",
221+
Scope: "runtime",
222+
Metadata: github.Metadata{"source_location": "opt/pyenv/versions/3.11.2/lib/python3.11/site-packages/jwcrypto-0.7.dist-info/METADATA"},
223+
},
224+
"matplotlib": {
225+
PackageUrl: "pkg:pypi/[email protected]",
226+
Relationship: "direct",
227+
Scope: "runtime",
228+
Metadata: github.Metadata{"source_location": "opt/pyenv/versions/3.11.2/lib/python3.11/site-packages/matplotlib-3.5.3.dist-info/METADATA"},
229+
},
230+
},
231+
},
232+
},
233+
},
167234
}
168235

169236
for _, tt := range tests {

0 commit comments

Comments
 (0)