Skip to content

Commit 9863cf3

Browse files
fix: openvex report oci id bug (#928)
Signed-off-by: robert-cronin <robert.owen.cronin@gmail.com> Co-authored-by: Sertaç Özercan <852750+sozercan@users.noreply.github.com>
1 parent 357e9d1 commit 9863cf3

File tree

6 files changed

+66
-31
lines changed

6 files changed

+66
-31
lines changed

pkg/patch/patch.go

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,25 @@ func patchWithContext(ctx context.Context, ch chan error, image, reportFile, pat
189189
buildChannel := make(chan *client.SolveStatus)
190190
eg, ctx := errgroup.WithContext(ctx)
191191
eg.Go(func() error {
192-
_, err := bkClient.Build(ctx, solveOpt, copaProduct, func(ctx context.Context, c gwclient.Client) (*gwclient.Result, error) {
192+
var pkgType string
193+
var validatedManifest *unversioned.UpdateManifest
194+
if updates != nil {
195+
// create a new manifest with the successfully patched packages
196+
validatedManifest = &unversioned.UpdateManifest{
197+
Metadata: unversioned.Metadata{
198+
OS: unversioned.OS{
199+
Type: updates.Metadata.OS.Type,
200+
Version: updates.Metadata.OS.Version,
201+
},
202+
Config: unversioned.Config{
203+
Arch: updates.Metadata.Config.Arch,
204+
},
205+
},
206+
Updates: []unversioned.UpdatePackage{},
207+
}
208+
}
209+
210+
solveResponse, err := bkClient.Build(ctx, solveOpt, copaProduct, func(ctx context.Context, c gwclient.Client) (*gwclient.Result, error) {
193211
// Configure buildctl/client for use by package manager
194212
config, err := buildkit.InitializeBuildkitConfig(ctx, c, imageName.String())
195213
if err != nil {
@@ -263,38 +281,32 @@ func patchWithContext(ctx context.Context, ch chan error, image, reportFile, pat
263281

264282
res.AddMeta(exptypes.ExporterImageConfigKey, config.ConfigData)
265283

266-
// Currently can only validate updates if updating via scanner
267-
if reportFile != "" {
268-
// create a new manifest with the successfully patched packages
269-
validatedManifest := &unversioned.UpdateManifest{
270-
Metadata: unversioned.Metadata{
271-
OS: unversioned.OS{
272-
Type: updates.Metadata.OS.Type,
273-
Version: updates.Metadata.OS.Version,
274-
},
275-
Config: unversioned.Config{
276-
Arch: updates.Metadata.Config.Arch,
277-
},
278-
},
279-
Updates: []unversioned.UpdatePackage{},
280-
}
284+
// for the vex document, only include updates that were successfully applied
285+
pkgType = manager.GetPackageType()
286+
if validatedManifest != nil {
281287
for _, update := range updates.Updates {
282288
if !slices.Contains(errPkgs, update.Name) {
283289
validatedManifest.Updates = append(validatedManifest.Updates, update)
284290
}
285291
}
286-
// vex document must contain at least one statement
287-
if output != "" && len(validatedManifest.Updates) > 0 {
288-
if err := vex.TryOutputVexDocument(validatedManifest, manager, patchedImageName, format, output); err != nil {
289-
ch <- err
290-
return nil, err
291-
}
292-
}
293292
}
294293

295294
return res, nil
296295
}, buildChannel)
297296

297+
// Currently can only validate updates if updating via scanner
298+
if reportFile != "" && validatedManifest != nil {
299+
digest := solveResponse.ExporterResponse[exptypes.ExporterImageDigestKey]
300+
nameDigestOrTag := getRepoNameWithDigest(patchedImageName, digest)
301+
// vex document must contain at least one statement
302+
if output != "" && len(validatedManifest.Updates) > 0 {
303+
if err := vex.TryOutputVexDocument(validatedManifest, pkgType, nameDigestOrTag, format, output); err != nil {
304+
ch <- err
305+
return err
306+
}
307+
}
308+
}
309+
298310
return err
299311
})
300312

@@ -320,7 +332,9 @@ func patchWithContext(ctx context.Context, ch chan error, image, reportFile, pat
320332
return pipeR.Close()
321333
})
322334

323-
return eg.Wait()
335+
err = eg.Wait()
336+
337+
return err
324338
}
325339

326340
func getOSType(ctx context.Context, osreleaseBytes []byte) (string, error) {
@@ -390,3 +404,14 @@ func dockerLoad(ctx context.Context, pipeR io.Reader) error {
390404

391405
return cmd.Run()
392406
}
407+
408+
// e.g. "docker.io/library/nginx:1.21.6-patched".
409+
func getRepoNameWithDigest(patchedImageName, imageDigest string) string {
410+
parts := strings.Split(patchedImageName, "/")
411+
last := parts[len(parts)-1]
412+
if idx := strings.IndexRune(last, ':'); idx >= 0 {
413+
last = last[:idx]
414+
}
415+
nameWithDigest := fmt.Sprintf("%s@%s", last, imageDigest)
416+
return nameWithDigest
417+
}

pkg/patch/patch_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,3 +326,10 @@ func TestGetOSVersion(t *testing.T) {
326326
})
327327
}
328328
}
329+
330+
func TestGetRepoNameWithDigest(t *testing.T) {
331+
result := getRepoNameWithDigest("docker.io/library/nginx:1.21.6-patched", "sha256:mocked-digest")
332+
if result != "nginx@sha256:mocked-digest" {
333+
t.Fatalf("unexpected result: %s", result)
334+
}
335+
}

pkg/vex/openvex.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"time"
77

88
"github.com/openvex/go-vex/pkg/vex"
9-
"github.com/project-copacetic/copacetic/pkg/pkgmgr"
109
"github.com/project-copacetic/copacetic/pkg/types/unversioned"
1110
)
1211

@@ -29,7 +28,7 @@ type OpenVex struct{}
2928
func (o *OpenVex) CreateVEXDocument(
3029
updates *unversioned.UpdateManifest,
3130
patchedImageName string,
32-
pkgmgr pkgmgr.PackageManager,
31+
pkgType string,
3332
) (string, error) {
3433
t := now()
3534
doc := v
@@ -53,7 +52,6 @@ func (o *OpenVex) CreateVEXDocument(
5352
},
5453
}
5554

56-
pkgType := pkgmgr.GetPackageType()
5755
for _, u := range updates.Updates {
5856
subComponent := vex.Subcomponent{
5957
Component: vex.Component{

pkg/vex/openvex_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ func TestOpenVex_CreateVEXDocument(t *testing.T) {
173173
for _, tt := range tests {
174174
t.Run(tt.name, func(t *testing.T) {
175175
o := &OpenVex{}
176-
got, err := o.CreateVEXDocument(tt.args.updates, tt.args.patchedImageName, tt.args.pkgmgr)
176+
pkgType := tt.args.pkgmgr.GetPackageType()
177+
got, err := o.CreateVEXDocument(tt.args.updates, tt.args.patchedImageName, pkgType)
177178
if (err != nil) != tt.wantErr {
178179
t.Errorf("OpenVex.CreateVEXDocument() error = %v, wantErr %v", err, tt.wantErr)
179180
return

pkg/vex/vex.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ type Vex interface {
1212
CreateVEXDocument(updates *unversioned.UpdateManifest, patchedImageName string, pkgmgr pkgmgr.PackageManager) (string, error)
1313
}
1414

15-
func TryOutputVexDocument(updates *unversioned.UpdateManifest, pkgmgr pkgmgr.PackageManager, patchedImageName, format, file string) error {
15+
func TryOutputVexDocument(updates *unversioned.UpdateManifest, pkgType, patchedImageName, format, file string) error {
1616
var doc string
1717
var err error
1818

1919
switch format {
2020
case "openvex":
2121
ov := &OpenVex{}
22-
doc, err = ov.CreateVEXDocument(updates, patchedImageName, pkgmgr)
22+
doc, err = ov.CreateVEXDocument(updates, patchedImageName, pkgType)
2323
if err != nil {
2424
return err
2525
}

pkg/vex/vex_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ func TestTryOutputVexDocument(t *testing.T) {
5151
}
5252
for _, tt := range tests {
5353
t.Run(tt.name, func(t *testing.T) {
54-
if err := TryOutputVexDocument(tt.args.updates, tt.args.pkgmgr, tt.args.patchedImageName, tt.args.format, tt.args.file); (err != nil) != tt.wantErr {
54+
var pkgType string
55+
if tt.args.pkgmgr != nil {
56+
pkgType = tt.args.pkgmgr.GetPackageType()
57+
}
58+
if err := TryOutputVexDocument(tt.args.updates, pkgType, tt.args.patchedImageName, tt.args.format, tt.args.file); (err != nil) != tt.wantErr {
5559
t.Errorf("TryOutputVexDocument() error = %v, wantErr %v", err, tt.wantErr)
5660
}
5761
})

0 commit comments

Comments
 (0)